From bfc8ae6c52c3500a0ba64ba57c73b8e9db6f73c2 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 4 Jan 2025 07:56:01 +0100 Subject: [PATCH] Codechange: use accessor for (scrollbar_)index as they are read only --- src/picker_gui.cpp | 4 ++-- src/toolbar_gui.cpp | 2 +- src/widget.cpp | 26 ++++++++++++++++++++++---- src/widget_type.h | 2 ++ src/window.cpp | 30 +++++++++++++++--------------- src/window_gui.h | 2 +- 6 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/picker_gui.cpp b/src/picker_gui.cpp index a65c4a8dd4..22132ea5ee 100644 --- a/src/picker_gui.cpp +++ b/src/picker_gui.cpp @@ -414,9 +414,9 @@ EventState PickerWindow::OnHotkey(int hotkey) switch (hotkey) { case PCWHK_FOCUS_FILTER_BOX: /* Cycle between the two edit boxes. */ - if (this->has_type_picker && (this->nested_focus == nullptr || this->nested_focus->index != WID_PW_TYPE_FILTER)) { + if (this->has_type_picker && (this->nested_focus == nullptr || this->nested_focus->GetIndex() != WID_PW_TYPE_FILTER)) { this->SetFocusedWidget(WID_PW_TYPE_FILTER); - } else if (this->has_class_picker && (this->nested_focus == nullptr || this->nested_focus->index != WID_PW_CLASS_FILTER)) { + } else if (this->has_class_picker && (this->nested_focus == nullptr || this->nested_focus->GetIndex() != WID_PW_CLASS_FILTER)) { this->SetFocusedWidget(WID_PW_CLASS_FILTER); } SetFocusedWindow(this); diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index cf67f7c02f..51ad8849c3 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -1410,7 +1410,7 @@ public: NWidgetBase *nwid = it->get(); nwid->current_x = 0; /* Hide widget, it will be revealed in the next step. */ if (nwid->type == NWID_SPACER) continue; - lookup[dynamic_cast(nwid)->index] = std::distance(this->children.begin(), it); + lookup[dynamic_cast(nwid)->GetIndex()] = std::distance(this->children.begin(), it); } /* Now assign the widgets to their rightful place */ diff --git a/src/widget.cpp b/src/widget.cpp index efc25f5f47..1ecfd25e86 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -199,7 +199,7 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in _scroller_click_timeout = 3; changed = sb->UpdatePosition(rtl ? 1 : -1); } - w->mouse_capture_widget = sb->index; + w->mouse_capture_widget = sb->GetIndex(); } else if (pos >= ma - button_size) { /* Pressing the lower button? */ SetBit(sb->disp_flags, NDB_SCROLLBAR_DOWN); @@ -208,7 +208,7 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in _scroller_click_timeout = 3; changed = sb->UpdatePosition(rtl ? -1 : 1); } - w->mouse_capture_widget = sb->index; + w->mouse_capture_widget = sb->GetIndex(); } else { Point pt = HandleScrollbarHittest(sb, mi, ma, sb->type == NWID_HSCROLLBAR); @@ -219,7 +219,7 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in } else { _scrollbar_start_pos = pt.x - mi - button_size; _scrollbar_size = ma - mi - button_size * 2 - (pt.y - pt.x); - w->mouse_capture_widget = sb->index; + w->mouse_capture_widget = sb->GetIndex(); _cursorpos_drag_start = _cursor.pos; } } @@ -268,7 +268,7 @@ void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y) WidgetID GetWidgetFromPos(const Window *w, int x, int y) { NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y); - return (nw != nullptr) ? nw->index : -1; + return (nw != nullptr) ? nw->GetIndex() : -1; } /** @@ -1228,6 +1228,24 @@ StringID NWidgetCore::GetString() const return this->widget_data.string; } +/** + * Get the \c WidgetID of this nested widget. + * @return The \c WidgetID. + */ +WidgetID NWidgetCore::GetIndex() const +{ + return this->index; +} + +/** + * Get the \c WidgetID of this nested widget's scrollbar. + * @return The \c WidgetID. + */ +WidgetID NWidgetCore::GetScrollbarIndex() const +{ + return this->scrollbar_index; +} + void NWidgetCore::FillWidgetLookup(WidgetLookup &widget_lookup) { if (this->index >= 0) widget_lookup[this->index] = this; diff --git a/src/widget_type.h b/src/widget_type.h index 5f820a5a69..6385c6153d 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -384,6 +384,8 @@ public: void SetAlignment(StringAlignment align); StringID GetString() const; + WidgetID GetIndex() const; + WidgetID GetScrollbarIndex() const; inline void SetLowered(bool lowered); inline bool IsLowered() const; diff --git a/src/window.cpp b/src/window.cpp index 99a87da82a..2aa4b2bc08 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -365,7 +365,7 @@ void Window::UpdateQueryStringSize() /* virtual */ const Textbuf *Window::GetFocusedTextbuf() const { if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) { - return &this->GetQueryString(this->nested_focus->index)->text; + return &this->GetQueryString(this->nested_focus->GetIndex())->text; } return nullptr; @@ -378,7 +378,7 @@ void Window::UpdateQueryStringSize() /* virtual */ Point Window::GetCaretPosition() const { if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX && !this->querystrings.empty()) { - return this->GetQueryString(this->nested_focus->index)->GetCaretPosition(this, this->nested_focus->index); + return this->GetQueryString(this->nested_focus->GetIndex())->GetCaretPosition(this, this->nested_focus->GetIndex()); } Point pt = {0, 0}; @@ -394,7 +394,7 @@ void Window::UpdateQueryStringSize() /* virtual */ Rect Window::GetTextBoundingRect(const char *from, const char *to) const { if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) { - return this->GetQueryString(this->nested_focus->index)->GetBoundingRect(this, this->nested_focus->index, from, to); + return this->GetQueryString(this->nested_focus->GetIndex())->GetBoundingRect(this, this->nested_focus->GetIndex(), from, to); } Rect r = {0, 0, 0, 0}; @@ -409,7 +409,7 @@ void Window::UpdateQueryStringSize() /* virtual */ ptrdiff_t Window::GetTextCharacterAtPosition(const Point &pt) const { if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) { - return this->GetQueryString(this->nested_focus->index)->GetCharAtPosition(this, this->nested_focus->index, pt); + return this->GetQueryString(this->nested_focus->GetIndex())->GetCharAtPosition(this, this->nested_focus->GetIndex(), pt); } return -1; @@ -626,7 +626,7 @@ static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count) /* don't allow any interaction if the button has been disabled */ if (nw->IsDisabled()) return; - WidgetID widget_index = nw->index; ///< Index of the widget + WidgetID widget_index = nw->GetIndex(); ///< Index of the widget /* Clicked on a widget that is not disabled. * So unless the clicked widget is the caption bar, change focus to this widget. @@ -746,8 +746,8 @@ static void DispatchRightClickEvent(Window *w, int x, int y) Point pt = { x, y }; /* No widget to handle, or the window is not interested in it. */ - if (wid->index >= 0) { - if (w->OnRightClick(pt, wid->index)) return; + if (wid->GetIndex() >= 0) { + if (w->OnRightClick(pt, wid->GetIndex())) return; } /* Right-click close is enabled and there is a closebox. */ @@ -756,7 +756,7 @@ static void DispatchRightClickEvent(Window *w, int x, int y) } else if (_settings_client.gui.right_click_wnd_close == RCC_YES_EXCEPT_STICKY && (w->flags & WF_STICKY) == 0 && (w->window_desc.flags & WDF_NO_CLOSE) == 0) { /* Right-click close is enabled, but excluding sticky windows. */ w->Close(); - } else if (_settings_client.gui.hover_delay_ms == 0 && !w->OnTooltip(pt, wid->index, TCC_RIGHT_CLICK) && wid->GetToolTip() != STR_NULL) { + } else if (_settings_client.gui.hover_delay_ms == 0 && !w->OnTooltip(pt, wid->GetIndex(), TCC_RIGHT_CLICK) && wid->GetToolTip() != STR_NULL) { GuiShowTooltips(w, wid->GetToolTip(), TCC_RIGHT_CLICK); } } @@ -777,15 +777,15 @@ static void DispatchHoverEvent(Window *w, int x, int y) Point pt = { x, y }; /* Show the tooltip if there is any */ - if (!w->OnTooltip(pt, wid->index, TCC_HOVER) && wid->GetToolTip() != STR_NULL) { + if (!w->OnTooltip(pt, wid->GetIndex(), TCC_HOVER) && wid->GetToolTip() != STR_NULL) { GuiShowTooltips(w, wid->GetToolTip(), TCC_HOVER); return; } /* Widget has no index, so the window is not interested in it. */ - if (wid->index < 0) return; + if (wid->GetIndex() < 0) return; - w->OnHover(pt, wid->index); + w->OnHover(pt, wid->GetIndex()); } /** @@ -815,7 +815,7 @@ static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel) } /* Scroll the widget attached to the scrollbar. */ - Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : nullptr); + Scrollbar *sb = (nwid->GetScrollbarIndex() >= 0 ? w->GetScrollbar(nwid->GetScrollbarIndex()) : nullptr); if (sb != nullptr && sb->GetCount() > sb->GetCapacity()) { if (sb->UpdatePosition(wheel)) w->SetDirty(); } @@ -1922,7 +1922,7 @@ static void HandleMouseOver() /* send an event in client coordinates. */ Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top }; const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y); - if (widget != nullptr) w->OnMouseOver(pt, widget->index); + if (widget != nullptr) w->OnMouseOver(pt, widget->GetIndex()); } } @@ -2592,7 +2592,7 @@ void HandleKeypress(uint keycode, char32_t key) if (_focused_window->window_class == WC_CONSOLE) { if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return; } else { - if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->index, key, keycode) == ES_HANDLED) return; + if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->GetIndex(), key, keycode) == ES_HANDLED) return; } } @@ -2656,7 +2656,7 @@ void HandleTextInput(const char *str, bool marked, const char *caret, const char { if (!EditBoxInGlobalFocus()) return; - _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->index, str, marked, caret, insert_location, replacement_end); + _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->GetIndex(), str, marked, caret, insert_location, replacement_end); } /** diff --git a/src/window_gui.h b/src/window_gui.h index 10a22ff58e..49cf17a9b4 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -425,7 +425,7 @@ public: */ inline bool IsWidgetFocused(WidgetID widget_index) const { - return this->nested_focus != nullptr && this->nested_focus->index == widget_index; + return this->nested_focus != nullptr && this->nested_focus->GetIndex() == widget_index; } /**