diff options
author | Rubidium <rubidium@openttd.org> | 2021-04-18 20:29:46 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-04-21 21:12:08 +0200 |
commit | bf4fe19a6684231d04764c3bee5ed51e4124a925 (patch) | |
tree | c8b7d662f3ed0ecc236ea894c9a51279e80dfbc2 /src/widget.cpp | |
parent | b3495f1a1323eb6d236c6fecb1127a0bd716a4d5 (diff) | |
download | openttd-bf4fe19a6684231d04764c3bee5ed51e4124a925.tar.xz |
Codechange: merge duplicated logic to scroll in lists by key into a single function
Diffstat (limited to 'src/widget.cpp')
-rw-r--r-- | src/widget.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/widget.cpp b/src/widget.cpp index 9de848a63..4be4cf331 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -1975,6 +1975,66 @@ int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, in } /** + * Update the given list position as if it were on this scroll bar when the given keycode was pressed. + * This does not update the actual position of this scroll bar, that is left to the caller. It does, + * however use the capacity and count of the scroll bar for the bounds and amount to scroll. + * + * When the count is 0 or the return is ES_NOT_HANDLED, then the position is not updated. + * With WKC_UP and WKC_DOWN the position goes one up or down respectively. + * With WKC_PAGEUP and WKC_PAGEDOWN the position goes one capacity up or down respectively. + * With WKC_HOME the first position is selected and with WKC_END the last position is selected. + * This function ensures that pos is in the range [0..count). + * @param list_position The current position in the list. + * @param key_code The pressed key code. + * @return ES_NOT_HANDLED when another key than the 6 specific keys was pressed, otherwise ES_HANDLED. + */ +EventState Scrollbar::UpdateListPositionOnKeyPress(int &list_position, uint16 keycode) const +{ + int new_pos = list_position; + switch (keycode) { + case WKC_UP: + /* scroll up by one */ + new_pos--; + break; + + case WKC_DOWN: + /* scroll down by one */ + new_pos++; + break; + + case WKC_PAGEUP: + /* scroll up a page */ + new_pos -= this->GetCapacity(); + break; + + case WKC_PAGEDOWN: + /* scroll down a page */ + new_pos += this->GetCapacity(); + break; + + case WKC_HOME: + /* jump to beginning */ + new_pos = 0; + break; + + case WKC_END: + /* jump to end */ + new_pos = this->GetCount() - 1; + break; + + default: + return ES_NOT_HANDLED; + } + + /* If there are no elements, there is nothing to scroll/update. */ + if (this->GetCount() != 0) { + list_position = Clamp(new_pos, 0, this->GetCount() - 1); + } + return ES_HANDLED; +} + + +/** * Set capacity of visible elements from the size and resize properties of a widget. * @param w Window. * @param widget Widget with size and resize properties. |