From 9ac1bad480fe9f6ac4991950eb6a9fb6dc0ca56a Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 2 Jan 2025 13:05:49 +0100 Subject: [PATCH] Codechange: add and use GetString over directly accessing widget_data --- src/autoreplace_gui.cpp | 4 ++-- src/bridge_gui.cpp | 2 +- src/build_vehicle_gui.cpp | 2 +- src/fios_gui.cpp | 2 +- src/group_gui.cpp | 2 +- src/industry_gui.cpp | 2 +- src/newgrf_gui.cpp | 2 +- src/news_gui.cpp | 2 +- src/osk_gui.cpp | 2 +- src/station_gui.cpp | 8 ++++---- src/town_gui.cpp | 2 +- src/vehicle_gui.cpp | 2 +- src/widget.cpp | 39 ++++++++++++++++++++++++--------------- src/widget_type.h | 2 ++ 14 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp index d790471c4e..0d85280db9 100644 --- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -295,7 +295,7 @@ public: { switch (widget) { case WID_RV_SORT_ASCENDING_DESCENDING: { - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; size = maxdim(size, d); @@ -314,7 +314,7 @@ public: break; case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: { - StringID str = this->GetWidget(widget)->widget_data; + StringID str = this->GetWidget(widget)->GetString(); SetDParam(0, STR_CONFIG_SETTING_ON); Dimension d = GetStringBoundingBox(str); SetDParam(0, STR_CONFIG_SETTING_OFF); diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp index 99c2813c45..d20dd284a6 100644 --- a/src/bridge_gui.cpp +++ b/src/bridge_gui.cpp @@ -182,7 +182,7 @@ public: { switch (widget) { case WID_BBS_DROPDOWN_ORDER: { - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; size = maxdim(size, d); diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index f07c37a54f..1bb1b2fde4 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -1776,7 +1776,7 @@ struct BuildVehicleWindow : Window { break; case WID_BV_SORT_ASCENDING_DESCENDING: { - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; size = maxdim(size, d); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 8862c300b0..526acdb985 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -634,7 +634,7 @@ public: break; case WID_SL_SORT_BYNAME: case WID_SL_SORT_BYDATE: { - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; size = maxdim(size, d); diff --git a/src/group_gui.cpp b/src/group_gui.cpp index 7642aa713e..839ca6577f 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -442,7 +442,7 @@ public: break; case WID_GL_SORT_BY_ORDER: { - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; size = maxdim(size, d); diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 93c2378df6..f1ece7f3e2 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1753,7 +1753,7 @@ public: { switch (widget) { case WID_ID_DROPDOWN_ORDER: { - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; size = maxdim(size, d); diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 130ff1159a..2f1403713e 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -222,7 +222,7 @@ struct NewGRFParametersWindow : public Window { case WID_NP_NUMPAR: { SetDParamMaxValue(0, GRFConfig::MAX_NUM_PARAMS); - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width; d.height += padding.height; size = maxdim(size, d); diff --git a/src/news_gui.cpp b/src/news_gui.cpp index efec75a900..543677593a 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -465,7 +465,7 @@ struct NewsWindow : Window { case WID_N_SHOW_GROUP: if (this->ni->reftype1 == NR_VEHICLE) { - Dimension d2 = GetStringBoundingBox(this->GetWidget(WID_N_SHOW_GROUP)->widget_data); + Dimension d2 = GetStringBoundingBox(this->GetWidget(WID_N_SHOW_GROUP)->GetString()); d2.height += WidgetDimensions::scaled.captiontext.Vertical(); d2.width += WidgetDimensions::scaled.captiontext.Horizontal(); size = d2; diff --git a/src/osk_gui.cpp b/src/osk_gui.cpp index 25f5aa3645..9b56d3144a 100644 --- a/src/osk_gui.cpp +++ b/src/osk_gui.cpp @@ -52,7 +52,7 @@ struct OskWindow : public Window { assert(parent->querystrings.count(button) != 0); this->qs = parent->querystrings.find(button)->second; - this->caption = (par_wid->widget_data != STR_NULL) ? par_wid->widget_data : this->qs->caption; + this->caption = (par_wid->GetString() != STR_NULL) ? par_wid->GetString() : this->qs->caption; this->text_btn = button; this->text = &this->qs->text; this->querystrings[WID_OSK_TEXT] = this->qs; diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 9aba4de12b..00867ea20c 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -455,7 +455,7 @@ public: { switch (widget) { case WID_STL_SORTBY: { - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; size = maxdim(size, d); @@ -1440,7 +1440,7 @@ struct StationViewWindow : public Window { break; case WID_SV_ACCEPT_RATING_LIST: - size.height = ((this->GetWidget(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) ? this->accepts_lines : this->rating_lines) * GetCharacterHeight(FS_NORMAL) + padding.height; + size.height = ((this->GetWidget(WID_SV_ACCEPTS_RATINGS)->GetString() == STR_STATION_VIEW_RATINGS_BUTTON) ? this->accepts_lines : this->rating_lines) * GetCharacterHeight(FS_NORMAL) + padding.height; break; case WID_SV_CLOSE_AIRPORT: @@ -1481,7 +1481,7 @@ struct StationViewWindow : public Window { /* Draw 'accepted cargo' or 'cargo ratings'. */ const NWidgetBase *wid = this->GetWidget(WID_SV_ACCEPT_RATING_LIST); const Rect r = wid->GetCurrentRect(); - if (this->GetWidget(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) { + if (this->GetWidget(WID_SV_ACCEPTS_RATINGS)->GetString() == STR_STATION_VIEW_RATINGS_BUTTON) { int lines = this->DrawAcceptedCargo(r); if (lines > this->accepts_lines) { // Resize the widget, and perform re-initialization of the window. this->accepts_lines = lines; @@ -1992,7 +1992,7 @@ struct StationViewWindow : public Window { /* Swap between 'accepts' and 'ratings' view. */ int height_change; NWidgetCore *nwi = this->GetWidget(WID_SV_ACCEPTS_RATINGS); - if (this->GetWidget(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) { + if (this->GetWidget(WID_SV_ACCEPTS_RATINGS)->GetString() == STR_STATION_VIEW_RATINGS_BUTTON) { nwi->SetStringTip(STR_STATION_VIEW_ACCEPTS_BUTTON, STR_STATION_VIEW_ACCEPTS_TOOLTIP); // Switch to accepts view. height_change = this->rating_lines - this->accepts_lines; } else { diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 303d670280..6293e16889 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -906,7 +906,7 @@ public: { switch (widget) { case WID_TD_SORT_ORDER: { - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; size = maxdim(size, d); diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 35e011334a..9768e33e39 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -2002,7 +2002,7 @@ public: break; case WID_VL_SORT_ORDER: { - Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); + Dimension d = GetStringBoundingBox(this->GetWidget(widget)->GetString()); d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; size = maxdim(size, d); diff --git a/src/widget.cpp b/src/widget.cpp index 53559743cf..ef5ed0cb80 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -1172,6 +1172,15 @@ void NWidgetCore::SetAlignment(StringAlignment align) this->align = align; } +/** + * Get the string that has been set for this nested widget. + * @return The string. + */ +StringID NWidgetCore::GetString() const +{ + return this->widget_data; +} + void NWidgetCore::FillWidgetLookup(WidgetLookup &widget_lookup) { if (this->index >= 0) widget_lookup[this->index] = this; @@ -2191,13 +2200,13 @@ void NWidgetBackground::SetupSmallestSize(Window *w) if (this->type == WWT_FRAME) { /* Account for the size of the frame's text if that exists */ this->child->padding = WidgetDimensions::scaled.frametext; - this->child->padding.top = std::max(WidgetDimensions::scaled.frametext.top, this->widget_data != STR_NULL ? GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.frametext.top / 2 : 0); + this->child->padding.top = std::max(WidgetDimensions::scaled.frametext.top, this->GetString() != STR_NULL ? GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.frametext.top / 2 : 0); this->smallest_x += this->child->padding.Horizontal(); this->smallest_y += this->child->padding.Vertical(); if (this->index >= 0) w->SetStringParameters(this->index); - this->smallest_x = std::max(this->smallest_x, GetStringBoundingBox(this->widget_data, this->text_size).width + WidgetDimensions::scaled.frametext.Horizontal()); + this->smallest_x = std::max(this->smallest_x, GetStringBoundingBox(this->GetString(), this->text_size).width + WidgetDimensions::scaled.frametext.Horizontal()); } else if (this->type == WWT_INSET) { /* Apply automatic padding for bevel thickness. */ this->child->padding = WidgetDimensions::scaled.bevel; @@ -2213,7 +2222,7 @@ void NWidgetBackground::SetupSmallestSize(Window *w) if (w != nullptr) { // A non-nullptr window pointer acts as switch to turn dynamic widget size on. if (this->type == WWT_FRAME || this->type == WWT_INSET) { if (this->index >= 0) w->SetStringParameters(this->index); - Dimension background = GetStringBoundingBox(this->widget_data, this->text_size); + Dimension background = GetStringBoundingBox(this->GetString(), this->text_size); background.width += (this->type == WWT_FRAME) ? (WidgetDimensions::scaled.frametext.Horizontal()) : (WidgetDimensions::scaled.inset.Horizontal()); d = maxdim(d, background); } @@ -2273,12 +2282,12 @@ void NWidgetBackground::Draw(const Window *w) case WWT_FRAME: if (this->index >= 0) w->SetStringParameters(this->index); - DrawFrame(r, this->colour, this->text_colour, this->widget_data, this->align, this->text_size); + DrawFrame(r, this->colour, this->text_colour, this->GetString(), this->align, this->text_size); break; case WWT_INSET: if (this->index >= 0) w->SetStringParameters(this->index); - DrawInset(r, this->colour, this->text_colour, this->widget_data, this->align, this->text_size); + DrawInset(r, this->colour, this->text_colour, this->GetString(), this->align, this->text_size); break; default: @@ -2851,7 +2860,7 @@ void NWidgetLeaf::SetupSmallestSize(Window *w) case WWT_TEXTBTN_2: { padding = {WidgetDimensions::scaled.framerect.Horizontal(), WidgetDimensions::scaled.framerect.Vertical()}; if (this->index >= 0) w->SetStringParameters(this->index); - Dimension d2 = GetStringBoundingBox(this->widget_data, this->text_size); + Dimension d2 = GetStringBoundingBox(this->GetString(), this->text_size); d2.width += padding.width; d2.height += padding.height; size = maxdim(size, d2); @@ -2860,13 +2869,13 @@ void NWidgetLeaf::SetupSmallestSize(Window *w) case WWT_LABEL: case WWT_TEXT: { if (this->index >= 0) w->SetStringParameters(this->index); - size = maxdim(size, GetStringBoundingBox(this->widget_data, this->text_size)); + size = maxdim(size, GetStringBoundingBox(this->GetString(), this->text_size)); break; } case WWT_CAPTION: { padding = {WidgetDimensions::scaled.captiontext.Horizontal(), WidgetDimensions::scaled.captiontext.Vertical()}; if (this->index >= 0) w->SetStringParameters(this->index); - Dimension d2 = GetStringBoundingBox(this->widget_data, this->text_size); + Dimension d2 = GetStringBoundingBox(this->GetString(), this->text_size); d2.width += padding.width; d2.height += padding.height; size = maxdim(size, d2); @@ -2882,7 +2891,7 @@ void NWidgetLeaf::SetupSmallestSize(Window *w) } padding = {WidgetDimensions::scaled.dropdowntext.Horizontal() + NWidgetLeaf::dropdown_dimension.width + WidgetDimensions::scaled.fullbevel.Horizontal(), WidgetDimensions::scaled.dropdowntext.Vertical()}; if (this->index >= 0) w->SetStringParameters(this->index); - Dimension d2 = GetStringBoundingBox(this->widget_data, this->text_size); + Dimension d2 = GetStringBoundingBox(this->GetString(), this->text_size); d2.width += padding.width; d2.height = std::max(d2.height + padding.height, NWidgetLeaf::dropdown_dimension.height); size = maxdim(size, d2); @@ -2944,7 +2953,7 @@ void NWidgetLeaf::Draw(const Window *w) case WWT_TEXTBTN_2: if (this->index >= 0) w->SetStringParameters(this->index); DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE); - DrawLabel(r, this->type, clicked, this->text_colour, this->widget_data, this->align, this->text_size); + DrawLabel(r, this->type, clicked, this->text_colour, this->GetString(), this->align, this->text_size); break; case WWT_ARROWBTN: @@ -2963,12 +2972,12 @@ void NWidgetLeaf::Draw(const Window *w) case WWT_LABEL: if (this->index >= 0) w->SetStringParameters(this->index); - DrawLabel(r, this->type, clicked, this->text_colour, this->widget_data, this->align, this->text_size); + DrawLabel(r, this->type, clicked, this->text_colour, this->GetString(), this->align, this->text_size); break; case WWT_TEXT: if (this->index >= 0) w->SetStringParameters(this->index); - DrawText(r, this->text_colour, this->widget_data, this->align, this->text_size); + DrawText(r, this->text_colour, this->GetString(), this->align, this->text_size); break; case WWT_MATRIX: @@ -2983,7 +2992,7 @@ void NWidgetLeaf::Draw(const Window *w) case WWT_CAPTION: if (this->index >= 0) w->SetStringParameters(this->index); - DrawCaption(r, this->colour, w->owner, this->text_colour, this->widget_data, this->align, this->text_size); + DrawCaption(r, this->colour, w->owner, this->text_colour, this->GetString(), this->align, this->text_size); break; case WWT_SHADEBOX: @@ -3015,13 +3024,13 @@ void NWidgetLeaf::Draw(const Window *w) case WWT_DROPDOWN: if (this->index >= 0) w->SetStringParameters(this->index); - DrawButtonDropdown(r, this->colour, false, clicked, this->widget_data, this->align); + DrawButtonDropdown(r, this->colour, false, clicked, this->GetString(), this->align); break; case NWID_BUTTON_DROPDOWN: case NWID_PUSHBUTTON_DROPDOWN: if (this->index >= 0) w->SetStringParameters(this->index); - DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data, this->align); + DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->GetString(), this->align); break; default: diff --git a/src/widget_type.h b/src/widget_type.h index 5fd2fedc01..52fb757f69 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -381,6 +381,8 @@ public: void SetTextStyle(TextColour colour, FontSize size); void SetAlignment(StringAlignment align); + StringID GetString() const; + inline void SetLowered(bool lowered); inline bool IsLowered() const; inline void SetDisabled(bool disabled);