From c2c65d66bad5802e0d97b4c76443a755ea9f6ed1 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 30 Dec 2023 20:49:08 +0000 Subject: [PATCH] Codechange: Add GetParentWidget() to widgets. This allows to get parent widgets in the nested tree from bottom-up. --- src/widget.cpp | 6 ++++++ src/widget_type.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/widget.cpp b/src/widget.cpp index 85465c4743..151a6d40cb 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -1319,6 +1319,8 @@ void NWidgetContainer::AdjustPaddingForZoom() */ void NWidgetContainer::Add(NWidgetBase *wid) { + assert(wid != nullptr); + wid->parent = this; assert(wid->next == nullptr && wid->prev == nullptr); if (this->head == nullptr) { @@ -2170,6 +2172,7 @@ NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, WidgetID ind { assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME); this->child = child; + if (this->child != nullptr) this->child->parent = this; this->SetAlignment(SA_TOP | SA_LEFT); } @@ -2190,6 +2193,7 @@ void NWidgetBackground::Add(NWidgetBase *nwid) if (this->child == nullptr) { this->child = new NWidgetVertical(); } + nwid->parent = this->child; this->child->Add(nwid); } @@ -2208,6 +2212,7 @@ void NWidgetBackground::SetPIP(uint8_t pip_pre, uint8_t pip_inter, uint8_t pip_p if (this->child == nullptr) { this->child = new NWidgetVertical(); } + this->child->parent = this; this->child->SetPIP(pip_pre, pip_inter, pip_post); } @@ -2226,6 +2231,7 @@ void NWidgetBackground::SetPIPRatio(uint8_t pip_ratio_pre, uint8_t pip_ratio_int if (this->child == nullptr) { this->child = new NWidgetVertical(); } + this->child->parent = this; this->child->SetPIPRatio(pip_ratio_pre, pip_ratio_inter, pip_ratio_post); } diff --git a/src/widget_type.h b/src/widget_type.h index 8c2cac59f2..9c2620cad6 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -144,6 +144,34 @@ public: virtual NWidgetCore *GetWidgetFromPos(int x, int y) = 0; virtual NWidgetBase *GetWidgetOfType(WidgetType tp); + /** + * Get parent widget of type NWID. + * @tparam NWID Type of the nested widget. + * @returns Parent widget, or nullptr if no widget of the specified type is found. + */ + template + NWID *GetParentWidget() + { + for (NWidgetBase *nwid_parent = this->parent; nwid_parent != nullptr; nwid_parent = nwid_parent->parent) { + if (NWID *nwid = dynamic_cast(nwid_parent); nwid != nullptr) return nwid; + } + return nullptr; + } + + /** + * Get parent widget of type NWID. + * @tparam NWID Type of the nested widget. + * @returns Parent widget, or nullptr if no widget of the specified type is found. + */ + template + const NWID *GetParentWidget() const + { + for (const NWidgetBase *nwid_parent = this->parent; nwid_parent != nullptr; nwid_parent = nwid_parent->parent) { + if (const NWID *nwid = dynamic_cast(nwid_parent); nwid != nullptr) return nwid; + } + return nullptr; + } + virtual bool IsHighlighted() const { return false; } virtual TextColour GetHighlightColour() const { return TC_INVALID; } virtual void SetHighlighted([[maybe_unused]] TextColour highlight_colour) {} @@ -213,6 +241,8 @@ public: RectPadding padding; ///< Padding added to the widget. Managed by parent container widget. (parent container may swap left and right for RTL) RectPadding uz_padding; ///< Unscaled padding, for resize calculation. + NWidgetBase *parent; ///< Parent widget of this widget, automatically filled in when added to container. + protected: inline void StoreSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height); };