changeset 13394:ff0e22d5b376 draft

(svn r17903) -Codechange: don't get a modifiable NWidget from a const Window
author rubidium <rubidium@openttd.org>
date Fri, 30 Oct 2009 07:51:33 +0000
parents ed215cab9f4a
children 1c024f74a929
files src/widget.cpp src/widget_type.h src/window_gui.h
diffstat 3 files changed, 21 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/widget.cpp
+++ b/src/widget.cpp
@@ -855,7 +855,7 @@
 		top = this->widget[widget].top;
 	} else {
 		assert(this->nested_array != NULL);
-		NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
+		const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
 		base = offset + nwid->pos_x + (_dynlang.text_dir == TD_LTR ? nwid->current_x - WD_SORTBUTTON_ARROW_WIDTH : 0);
 		top = nwid->pos_y;
 	}
--- a/src/widget_type.h
+++ b/src/widget_type.h
@@ -308,9 +308,9 @@
 	void SetDataTip(uint16 widget_data, StringID tool_tip);
 
 	inline void SetLowered(bool lowered);
-	inline bool IsLowered();
+	inline bool IsLowered() const;
 	inline void SetDisabled(bool disabled);
-	inline bool IsDisabled();
+	inline bool IsDisabled() const;
 
 	void StoreWidgets(Widget *widgets, int length, bool left_moving, bool top_moving, bool rtl);
 	/* virtual */ void FillNestedArray(NWidgetBase **array, uint length);
@@ -335,7 +335,7 @@
 }
 
 /** Return whether the widget is lowered. */
-inline bool NWidgetCore::IsLowered()
+inline bool NWidgetCore::IsLowered() const
 {
 	return HasBit(this->disp_flags, NDB_LOWERED);
 }
@@ -350,7 +350,7 @@
 }
 
 /** Return whether the widget is disabled. */
-inline bool NWidgetCore::IsDisabled()
+inline bool NWidgetCore::IsDisabled() const
 {
 	return HasBit(this->disp_flags, NDB_DISABLED);
 }
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -389,7 +389,9 @@
 	Window *z_back;                  ///< The window behind us in z-order.
 
 	template <class NWID>
-	inline NWID *GetWidget(uint widnum) const;
+	inline const NWID *GetWidget(uint widnum) const;
+	template <class NWID>
+	inline NWID *GetWidget(uint widnum);
 
 
 	void InitNested(const WindowDesc *desc, WindowNumber number = 0);
@@ -847,7 +849,7 @@
  * @return The requested widget if it is instantiated, \c NULL otherwise.
  */
 template <class NWID>
-inline NWID *Window::GetWidget(uint widnum) const
+inline NWID *Window::GetWidget(uint widnum)
 {
 	if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
 	NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
@@ -857,12 +859,23 @@
 
 /** Specialized case of #Window::GetWidget for the nested widget base class. */
 template <>
-inline NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
+inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
 {
 	if (widnum >= this->nested_array_size) return NULL;
 	return this->nested_array[widnum];
 }
 
+/** 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 const NWID *Window::GetWidget(uint widnum) const
+{
+	return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
+}
+
 
 /**
  * Base class for windows opened from a toolbar.