Changeset - r28366:9387e8c74c85
[Not reviewed]
master
0 2 0
Peter Nelson - 4 months ago 2023-12-30 20:49:08
peter1138@openttd.org
Codechange: Add GetParentWidget() to widgets.

This allows to get parent widgets in the nested tree from bottom-up.
2 files changed with 36 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/widget.cpp
Show inline comments
 
@@ -1319,6 +1319,8 @@ void NWidgetContainer::AdjustPaddingForZ
 
 */
 
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(Wid
 
{
 
	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 
 
	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 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(uint
 
	if (this->child == nullptr) {
 
		this->child = new NWidgetVertical();
 
	}
 
	this->child->parent = this;
 
	this->child->SetPIPRatio(pip_ratio_pre, pip_ratio_inter, pip_ratio_post);
 
}
 

	
src/widget_type.h
Show inline comments
 
@@ -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);
 
};
0 comments (0 inline, 0 general)