summaryrefslogtreecommitdiff
path: root/src/window_gui.h
diff options
context:
space:
mode:
authoralberth <alberth@openttd.org>2009-09-19 11:31:12 +0000
committeralberth <alberth@openttd.org>2009-09-19 11:31:12 +0000
commit116c77c342d0f628235a7f5dbacfbb5bc3fb0829 (patch)
treed2dd74828426b0c82b4dab368e6aff5ff71f3bad /src/window_gui.h
parent9d7fe9bd8688f2d4123dc98e2b222a5be70b306f (diff)
downloadopenttd-116c77c342d0f628235a7f5dbacfbb5bc3fb0829.tar.xz
(svn r17572) -Codechange: Use the Window::GetWidget() function to access nested widgets through the nested_array.
Diffstat (limited to 'src/window_gui.h')
-rw-r--r--src/window_gui.h41
1 files changed, 34 insertions, 7 deletions
diff --git a/src/window_gui.h b/src/window_gui.h
index 13deb83a0..e697a344a 100644
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -391,13 +391,17 @@ public:
const Widget *focused_widget; ///< Currently focused widget, or \c NULL if no widget has focus.
const NWidgetCore *nested_focus; ///< Currently focused nested widget, or \c NULL if no nested widget has focus.
NWidgetBase *nested_root; ///< Root of the nested tree.
- NWidgetCore **nested_array; ///< Array of pointers into the tree.
+ NWidgetBase **nested_array; ///< Array of pointers into the tree. Do not access directly, use #Window::GetWidget() instead.
uint nested_array_size; ///< Size of the nested array.
Window *parent; ///< Parent window.
Window *z_front; ///< The window in front of us in z-order.
Window *z_back; ///< The window behind us in z-order.
+ template <class NWID = NWidgetBase>
+ inline NWID *GetWidget(uint widnum) const;
+
+
void InitNested(const WindowDesc *desc, WindowNumber number = 0);
void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
void FinishInitNested(const WindowDesc *desc, WindowNumber window_number);
@@ -417,7 +421,7 @@ public:
}
if (this->nested_array != NULL) {
assert(widget_index < this->nested_array_size);
- if (this->nested_array[widget_index] != NULL) this->nested_array[widget_index]->SetDisabled(disab_stat);
+ if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
}
}
@@ -448,7 +452,7 @@ public:
{
if (this->nested_array != NULL) {
assert(widget_index < this->nested_array_size);
- return this->nested_array[widget_index]->IsDisabled();
+ return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
}
assert(widget_index < this->widget_count);
return HasBit(this->widget[widget_index].display_flags, WIDG_DISABLED);
@@ -531,7 +535,7 @@ public:
}
if (this->nested_array != NULL) {
assert(widget_index < this->nested_array_size);
- this->nested_array[widget_index]->SetLowered(lowered_stat);
+ this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
}
}
@@ -547,8 +551,8 @@ public:
}
if (this->nested_array != NULL) {
assert(widget_index < this->nested_array_size);
- bool lowered_state = this->nested_array[widget_index]->IsLowered();
- this->nested_array[widget_index]->SetLowered(!lowered_state);
+ bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
+ this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
}
}
@@ -579,7 +583,7 @@ public:
{
if (this->nested_array != NULL) {
assert(widget_index < this->nested_array_size);
- return this->nested_array[widget_index]->IsLowered();
+ return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
}
assert(widget_index < this->widget_count);
return HasBit(this->widget[widget_index].display_flags, WIDG_LOWERED);
@@ -838,6 +842,29 @@ public:
/*** End of the event handling ***/
};
+/** Get the nested widget with number \a widnum from the nested widget tree.
+ * @tparam NWID Type of the nested widget.
+ * @param widnum Widget number of the widget to retrieve.
+ * @return The requested widget if it is instantiated, \c NULL otherwise.
+ */
+template <class NWID>
+inline NWID *Window::GetWidget(uint widnum) const
+{
+ if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
+ NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
+ assert(nwid != NULL);
+ return nwid;
+}
+
+/** Specialized case of #Window::GetWidget for the nested widget base class. */
+template <>
+inline NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
+{
+ if (widnum >= this->nested_array_size) return NULL;
+ return this->nested_array[widnum];
+}
+
+
/**
* Base class for windows opened from a toolbar.
*/