Codechange: Add GetParentWidget() to widgets.

This allows to get parent widgets in the nested tree from bottom-up.
wip-string
Peter Nelson 5 months ago committed by Peter Nelson
parent 8ff0bef862
commit c2c65d66ba

@ -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);
}

@ -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 <class NWID>
NWID *GetParentWidget()
{
for (NWidgetBase *nwid_parent = this->parent; nwid_parent != nullptr; nwid_parent = nwid_parent->parent) {
if (NWID *nwid = dynamic_cast<NWID *>(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 <class NWID>
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<const NWID *>(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);
};

Loading…
Cancel
Save