From b856899a13d7c8e5233aea797c3be7c24f614f51 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 23 Mar 2026 19:10:27 +0000 Subject: [PATCH] Codechange: use scoped enum for SizingType --- src/smallmap_gui.cpp | 10 +++++----- src/widget.cpp | 30 +++++++++++++++--------------- src/widget_type.h | 17 +++++++++-------- src/window.cpp | 6 +++--- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index 3c87b25758..9a53889b6a 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -1953,18 +1953,18 @@ public: NWidgetBase *display = this->children.front().get(); NWidgetBase *bar = this->children.back().get(); - if (sizing == ST_SMALLEST) { + if (sizing == SizingType::Smallest) { this->smallest_x = given_width; this->smallest_y = given_height; /* Make display and bar exactly equal to their minimal size. */ - display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl); - bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl); + display->AssignSizePosition(SizingType::Smallest, x, y, display->smallest_x, display->smallest_y, rtl); + bar->AssignSizePosition(SizingType::Smallest, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl); } uint bar_height = std::max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x))); uint display_height = given_height - bar_height; - display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl); - bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl); + display->AssignSizePosition(SizingType::Resize, x, y, given_width, display_height, rtl); + bar->AssignSizePosition(SizingType::Resize, x, y + display_height, given_width, bar_height, rtl); } }; diff --git a/src/widget.cpp b/src/widget.cpp index 4af742238a..6a0d95c31a 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -884,11 +884,11 @@ static void DrawOutline(const Window *, const NWidgetBase *wid) *
    *
  1. A bottom-up sweep by recursively calling NWidgetBase::SetupSmallestSize() to initialize the smallest size (\e smallest_x, \e smallest_y) and * to propagate filling and resize steps upwards to the root of the tree. - *
  2. A top-down sweep by recursively calling NWidgetBase::AssignSizePosition() with #ST_SMALLEST to make the smallest sizes consistent over + *
  3. A top-down sweep by recursively calling NWidgetBase::AssignSizePosition() with #SizingType::Smallest to make the smallest sizes consistent over * the entire tree, and to assign the top-left (\e pos_x, \e pos_y) position of each widget in the tree. This step uses \e fill_x and \e fill_y at each * node in the tree to decide how to fill each widget towards consistent sizes. Also the current size (\e current_x and \e current_y) is set. - *
  4. After initializing the smallest size in the widget tree with #ST_SMALLEST, the tree can be resized (the current size modified) by calling - * NWidgetBase::AssignSizePosition() at the root with #ST_RESIZE and the new size of the window. For proper functioning, the new size should be the smallest + *
  5. After initializing the smallest size in the widget tree with #SizingType::Smallest, the tree can be resized (the current size modified) by calling + * NWidgetBase::AssignSizePosition() at the root with #SizingType::Resize and the new size of the window. For proper functioning, the new size should be the smallest * size + a whole number of resize steps in both directions (ie you can only resize in steps of length resize_{x,y} from smallest_{x,y}). *
* After the second step, the current size of the widgets are set to the smallest size. @@ -1404,11 +1404,11 @@ void NWidgetStacked::AssignSizePosition(SizingType sizing, int x, int y, uint gi if (this->shown_plane >= SZSP_BEGIN) return; for (const auto &child_wid : this->children) { - uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing); + uint hor_step = (sizing == SizingType::Smallest) ? 1 : child_wid->GetHorizontalStepSize(sizing); uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding.Horizontal(), hor_step); uint child_pos_x = (rtl ? child_wid->padding.right : child_wid->padding.left); - uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing); + uint vert_step = (sizing == SizingType::Smallest) ? 1 : child_wid->GetVerticalStepSize(sizing); uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding.Vertical(), vert_step); uint child_pos_y = child_wid->padding.top; @@ -1497,11 +1497,11 @@ void NWidgetLayer::AssignSizePosition(SizingType sizing, int x, int y, uint give this->StoreSizePosition(sizing, x, y, given_width, given_height); for (const auto &child_wid : this->children) { - uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing); + uint hor_step = (sizing == SizingType::Smallest) ? 1 : child_wid->GetHorizontalStepSize(sizing); uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding.Horizontal(), hor_step); uint child_pos_x = (rtl ? child_wid->padding.right : child_wid->padding.left); - uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing); + uint vert_step = (sizing == SizingType::Smallest) ? 1 : child_wid->GetVerticalStepSize(sizing); uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding.Vertical(), vert_step); uint child_pos_y = child_wid->padding.top; @@ -1579,7 +1579,7 @@ void NWidgetHorizontal::SetupSmallestSize(Window *w) for (const auto &child_wid : this->children) { child_wid->SetupSmallestSize(w); longest = std::max(longest, child_wid->smallest_x); - max_vert_fill = std::max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST)); + max_vert_fill = std::max(max_vert_fill, child_wid->GetVerticalStepSize(SizingType::Smallest)); this->smallest_y = std::max(this->smallest_y, child_wid->smallest_y + child_wid->padding.Vertical()); if (child_wid->smallest_x != 0 || child_wid->fill_x != 0) this->gaps++; } @@ -1589,7 +1589,7 @@ void NWidgetHorizontal::SetupSmallestSize(Window *w) uint cur_height = this->smallest_y; for (;;) { for (const auto &child_wid : this->children) { - uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST); + uint step_size = child_wid->GetVerticalStepSize(SizingType::Smallest); uint child_height = child_wid->smallest_y + child_wid->padding.Vertical(); if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting children are not interesting. uint remainder = (cur_height - child_height) % step_size; @@ -1669,7 +1669,7 @@ void NWidgetHorizontal::AssignSizePosition(SizingType sizing, int x, int y, uint child_wid->current_x = child_wid->smallest_x; } - uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing); + uint vert_step = (sizing == SizingType::Smallest) ? 1 : child_wid->GetVerticalStepSize(sizing); child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding.Vertical(), vert_step); } @@ -1762,7 +1762,7 @@ void NWidgetVertical::SetupSmallestSize(Window *w) for (const auto &child_wid : this->children) { child_wid->SetupSmallestSize(w); highest = std::max(highest, child_wid->smallest_y); - max_hor_fill = std::max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST)); + max_hor_fill = std::max(max_hor_fill, child_wid->GetHorizontalStepSize(SizingType::Smallest)); this->smallest_x = std::max(this->smallest_x, child_wid->smallest_x + child_wid->padding.Horizontal()); if (child_wid->smallest_y != 0 || child_wid->fill_y != 0) this->gaps++; } @@ -1772,7 +1772,7 @@ void NWidgetVertical::SetupSmallestSize(Window *w) uint cur_width = this->smallest_x; for (;;) { for (const auto &child_wid : this->children) { - uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST); + uint step_size = child_wid->GetHorizontalStepSize(SizingType::Smallest); uint child_width = child_wid->smallest_x + child_wid->padding.Horizontal(); if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting children are not interesting. uint remainder = (cur_width - child_width) % step_size; @@ -1843,7 +1843,7 @@ void NWidgetVertical::AssignSizePosition(SizingType sizing, int x, int y, uint g child_wid->current_y = child_wid->smallest_y; } - uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing); + uint hor_step = (sizing == SizingType::Smallest) ? 1 : child_wid->GetHorizontalStepSize(sizing); child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding.Horizontal(), hor_step); } @@ -2082,7 +2082,7 @@ NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y) NWidgetCore *child = dynamic_cast(this->children.front().get()); assert(child != nullptr); - child->AssignSizePosition(ST_RESIZE, + child->AssignSizePosition(SizingType::Resize, this->pos_x + (rtl ? this->pip_post - widget_col * this->widget_w : this->pip_pre + widget_col * this->widget_w) + base_offs_x, this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y, child->smallest_x, child->smallest_y, rtl); @@ -2128,7 +2128,7 @@ NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y) this->current_element = y * this->widgets_x + x; if (this->current_element >= this->count) break; - child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl); + child->AssignSizePosition(SizingType::Resize, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl); child->SetLowered(this->clicked == this->current_element); child->Draw(w); } diff --git a/src/widget_type.h b/src/widget_type.h index 1069839b00..c6fb9dbd58 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -110,14 +110,15 @@ enum WidgetType : uint8_t { }; /** Different forms of sizing nested widgets, using NWidgetBase::AssignSizePosition() */ -enum SizingType : uint8_t { - ST_SMALLEST, ///< Initialize nested widget tree to smallest size. Also updates \e current_x and \e current_y. - ST_RESIZE, ///< Resize the nested widget tree. +enum class SizingType : uint8_t { + Smallest, ///< Initialize nested widget tree to smallest size. Also updates \e current_x and \e current_y. + Resize, ///< Resize the nested widget tree. }; +/** Flags to control how a widgeet is resized to reach its aspect ratio. */ enum class AspectFlag : uint8_t { - ResizeX, - ResizeY, + ResizeX, ///< Resize horizontally to reach desired aspect ratio. + ResizeY, ///< Resize vertically to reach desired aspect ratio. }; using AspectFlags = EnumBitSet; @@ -281,7 +282,7 @@ protected: */ inline uint NWidgetBase::GetHorizontalStepSize(SizingType sizing) const { - return (sizing == ST_RESIZE) ? this->resize_x : this->fill_x; + return (sizing == SizingType::Resize) ? this->resize_x : this->fill_x; } /** @@ -291,7 +292,7 @@ inline uint NWidgetBase::GetHorizontalStepSize(SizingType sizing) const */ inline uint NWidgetBase::GetVerticalStepSize(SizingType sizing) const { - return (sizing == ST_RESIZE) ? this->resize_y : this->fill_y; + return (sizing == SizingType::Resize) ? this->resize_y : this->fill_y; } /** @@ -306,7 +307,7 @@ inline void NWidgetBase::StoreSizePosition(SizingType sizing, int x, int y, uint { this->pos_x = x; this->pos_y = y; - if (sizing == ST_SMALLEST) { + if (sizing == SizingType::Smallest) { this->smallest_x = given_width; this->smallest_y = given_height; } diff --git a/src/window.cpp b/src/window.cpp index e649e44ad6..d80161c385 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1001,7 +1001,7 @@ void Window::ReInit(int rx, int ry, bool reposition) this->OnInit(); /* Re-initialize window smallest size. */ this->nested_root->SetupSmallestSize(this); - this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL); + this->nested_root->AssignSizePosition(SizingType::Smallest, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL); this->width = this->nested_root->smallest_x; this->height = this->nested_root->smallest_y; this->resize.step_width = this->nested_root->resize_x; @@ -1444,7 +1444,7 @@ void Window::InitializeData(WindowNumber window_number) /* Initialize smallest size. */ this->nested_root->SetupSmallestSize(this); /* Initialize to smallest size. */ - this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL); + this->nested_root->AssignSizePosition(SizingType::Smallest, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL); /* Further set up window properties, * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */ @@ -2129,7 +2129,7 @@ void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen, boo assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0); assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0); - w->nested_root->AssignSizePosition(ST_RESIZE, 0, 0, w->nested_root->smallest_x + new_xinc, w->nested_root->smallest_y + new_yinc, _current_text_dir == TD_RTL); + w->nested_root->AssignSizePosition(SizingType::Resize, 0, 0, w->nested_root->smallest_x + new_xinc, w->nested_root->smallest_y + new_yinc, _current_text_dir == TD_RTL); w->width = w->nested_root->current_x; w->height = w->nested_root->current_y; }