1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file window_gui.h Functions, definitions and such used only by the GUI. */
9 
10 #ifndef WINDOW_GUI_H
11 #define WINDOW_GUI_H
12 
13 #include <list>
14 
15 #include "vehicle_type.h"
16 #include "viewport_type.h"
17 #include "company_type.h"
18 #include "tile_type.h"
19 #include "widget_type.h"
20 #include "core/smallvec_type.hpp"
21 #include "core/smallmap_type.hpp"
22 #include "string_type.h"
23 
24 /**
25  * Flags to describe the look of the frame
26  */
27 enum FrameFlags {
28 	FR_NONE         =  0,
29 	FR_TRANSPARENT  =  1 << 0,  ///< Makes the background transparent if set
30 	FR_BORDERONLY   =  1 << 4,  ///< Draw border only, no background
31 	FR_LOWERED      =  1 << 5,  ///< If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
32 	FR_DARKENED     =  1 << 6,  ///< If set the background is darker, allows for lowered frames with normal background colour when used with FR_LOWERED (ie. dropdown boxes)
33 };
34 
35 DECLARE_ENUM_AS_BIT_SET(FrameFlags)
36 
37 /** Distances used in drawing widgets. */
38 enum WidgetDrawDistances {
39 	/* WWT_IMGBTN(_2) */
40 	WD_IMGBTN_LEFT    = 1,      ///< Left offset of the image in the button.
41 	WD_IMGBTN_RIGHT   = 2,      ///< Right offset of the image in the button.
42 	WD_IMGBTN_TOP     = 1,      ///< Top offset of image in the button.
43 	WD_IMGBTN_BOTTOM  = 2,      ///< Bottom offset of image in the button.
44 
45 	/* WWT_INSET */
46 	WD_INSET_LEFT  = 2,         ///< Left offset of string.
47 	WD_INSET_RIGHT = 2,         ///< Right offset of string.
48 	WD_INSET_TOP   = 1,         ///< Top offset of string.
49 
50 	WD_SCROLLBAR_LEFT   = 2,    ///< Left offset of scrollbar.
51 	WD_SCROLLBAR_RIGHT  = 2,    ///< Right offset of scrollbar.
52 	WD_SCROLLBAR_TOP    = 2,    ///< Top offset of scrollbar.
53 	WD_SCROLLBAR_BOTTOM = 2,    ///< Bottom offset of scrollbar.
54 
55 	/* Size of the pure frame bevel without any padding. */
56 	WD_BEVEL_LEFT       = 1,    ///< Width of left bevel border.
57 	WD_BEVEL_RIGHT      = 1,    ///< Width of right bevel border.
58 	WD_BEVEL_TOP        = 1,    ///< Height of top bevel border.
59 	WD_BEVEL_BOTTOM     = 1,    ///< Height of bottom bevel border.
60 
61 	/* FrameRect widgets, all text buttons, panel, editbox */
62 	WD_FRAMERECT_LEFT   = 2,    ///< Offset at left to draw the frame rectangular area
63 	WD_FRAMERECT_RIGHT  = 2,    ///< Offset at right to draw the frame rectangular area
64 	WD_FRAMERECT_TOP    = 1,    ///< Offset at top to draw the frame rectangular area
65 	WD_FRAMERECT_BOTTOM = 1,    ///< Offset at bottom to draw the frame rectangular area
66 
67 	/* Extra space at top/bottom of text panels */
68 	WD_TEXTPANEL_TOP    = 6,    ///< Offset at top to draw above the text
69 	WD_TEXTPANEL_BOTTOM = 6,    ///< Offset at bottom to draw below the text
70 
71 	/* WWT_FRAME */
72 	WD_FRAMETEXT_LEFT   = 6,    ///< Left offset of the text of the frame.
73 	WD_FRAMETEXT_RIGHT  = 6,    ///< Right offset of the text of the frame.
74 	WD_FRAMETEXT_TOP    = 6,    ///< Top offset of the text of the frame
75 	WD_FRAMETEXT_BOTTOM = 6,    ///< Bottom offset of the text of the frame
76 
77 	/* WWT_MATRIX */
78 	WD_MATRIX_LEFT   = 2,       ///< Offset at left of a matrix cell.
79 	WD_MATRIX_RIGHT  = 2,       ///< Offset at right of a matrix cell.
80 	WD_MATRIX_TOP    = 3,       ///< Offset at top of a matrix cell.
81 	WD_MATRIX_BOTTOM = 1,       ///< Offset at bottom of a matrix cell.
82 
83 	/* WWT_SHADEBOX */
84 	WD_SHADEBOX_WIDTH  = 12,    ///< Width of a standard shade box widget.
85 	WD_SHADEBOX_LEFT   = 2,     ///< Left offset of shade sprite.
86 	WD_SHADEBOX_RIGHT  = 2,     ///< Right offset of shade sprite.
87 	WD_SHADEBOX_TOP    = 3,     ///< Top offset of shade sprite.
88 	WD_SHADEBOX_BOTTOM = 3,     ///< Bottom offset of shade sprite.
89 
90 	/* WWT_STICKYBOX */
91 	WD_STICKYBOX_WIDTH  = 12,   ///< Width of a standard sticky box widget.
92 	WD_STICKYBOX_LEFT   = 2,    ///< Left offset of sticky sprite.
93 	WD_STICKYBOX_RIGHT  = 2,    ///< Right offset of sticky sprite.
94 	WD_STICKYBOX_TOP    = 3,    ///< Top offset of sticky sprite.
95 	WD_STICKYBOX_BOTTOM = 3,    ///< Bottom offset of sticky sprite.
96 
97 	/* WWT_DEBUGBOX */
98 	WD_DEBUGBOX_WIDTH  = 12,    ///< Width of a standard debug box widget.
99 	WD_DEBUGBOX_LEFT   = 2,     ///< Left offset of debug sprite.
100 	WD_DEBUGBOX_RIGHT  = 2,     ///< Right offset of debug sprite.
101 	WD_DEBUGBOX_TOP    = 3,     ///< Top offset of debug sprite.
102 	WD_DEBUGBOX_BOTTOM = 3,     ///< Bottom offset of debug sprite.
103 
104 	/* WWT_DEFSIZEBOX */
105 	WD_DEFSIZEBOX_WIDTH  = 12,  ///< Width of a standard defsize box widget.
106 	WD_DEFSIZEBOX_LEFT   = 2,   ///< Left offset of defsize sprite.
107 	WD_DEFSIZEBOX_RIGHT  = 2,   ///< Right offset of defsize sprite.
108 	WD_DEFSIZEBOX_TOP    = 3,   ///< Top offset of defsize sprite.
109 	WD_DEFSIZEBOX_BOTTOM = 3,   ///< Bottom offset of defsize sprite.
110 
111 	/* WWT_RESIZEBOX */
112 	WD_RESIZEBOX_WIDTH  = 12,   ///< Width of a resize box widget.
113 	WD_RESIZEBOX_LEFT   = 3,    ///< Left offset of resize sprite.
114 	WD_RESIZEBOX_RIGHT  = 2,    ///< Right offset of resize sprite.
115 	WD_RESIZEBOX_TOP    = 3,    ///< Top offset of resize sprite.
116 	WD_RESIZEBOX_BOTTOM = 2,    ///< Bottom offset of resize sprite.
117 
118 	/* WWT_CLOSEBOX */
119 	WD_CLOSEBOX_WIDTH  = 11,    ///< Width of a close box widget.
120 	WD_CLOSEBOX_LEFT   = 2,     ///< Left offset of closebox string.
121 	WD_CLOSEBOX_RIGHT  = 1,     ///< Right offset of closebox string.
122 	WD_CLOSEBOX_TOP    = 2,     ///< Top offset of closebox string.
123 	WD_CLOSEBOX_BOTTOM = 2,     ///< Bottom offset of closebox string.
124 
125 	/* WWT_CAPTION */
126 	WD_CAPTION_HEIGHT     = 14, ///< Height of a title bar.
127 	WD_CAPTIONTEXT_LEFT   = 2,  ///< Offset of the caption text at the left.
128 	WD_CAPTIONTEXT_RIGHT  = 2,  ///< Offset of the caption text at the right.
129 	WD_CAPTIONTEXT_TOP    = 2,  ///< Offset of the caption text at the top.
130 	WD_CAPTIONTEXT_BOTTOM = 2,  ///< Offset of the caption text at the bottom.
131 
132 	/* Dropdown widget. */
133 	WD_DROPDOWN_HEIGHT     = 12, ///< Height of a drop down widget.
134 	WD_DROPDOWNTEXT_LEFT   = 2,  ///< Left offset of the dropdown widget string.
135 	WD_DROPDOWNTEXT_RIGHT  = 2,  ///< Right offset of the dropdown widget string.
136 	WD_DROPDOWNTEXT_TOP    = 1,  ///< Top offset of the dropdown widget string.
137 	WD_DROPDOWNTEXT_BOTTOM = 1,  ///< Bottom offset of the dropdown widget string.
138 
139 	WD_PAR_VSEP_NORMAL = 2,      ///< Normal amount of vertical space between two paragraphs of text.
140 	WD_PAR_VSEP_WIDE   = 8,      ///< Large amount of vertical space between two paragraphs of text.
141 };
142 
143 /* widget.cpp */
144 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
145 void DrawCaption(const Rect &r, Colours colour, Owner owner, TextColour text_colour, StringID str, StringAlignment align);
146 
147 /* window.cpp */
148 using WindowList = std::list<Window *>;
149 extern WindowList _z_windows;
150 extern Window *_focused_window;
151 
152 
153 /** How do we the window to be placed? */
154 enum WindowPosition {
155 	WDP_MANUAL,        ///< Manually align the window (so no automatic location finding)
156 	WDP_AUTO,          ///< Find a place automatically
157 	WDP_CENTER,        ///< Center the window
158 	WDP_ALIGN_TOOLBAR, ///< Align toward the toolbar
159 };
160 
161 Point GetToolbarAlignedWindowPosition(int window_width);
162 
163 struct HotkeyList;
164 
165 /**
166  * High level window description
167  */
168 struct WindowDesc : ZeroedMemoryAllocator {
169 
170 	WindowDesc(WindowPosition default_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
171 			WindowClass window_class, WindowClass parent_class, uint32 flags,
172 			const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys = nullptr);
173 
174 	~WindowDesc();
175 
176 	WindowPosition default_pos;    ///< Preferred position of the window. @see WindowPosition()
177 	WindowClass cls;               ///< Class of the window, @see WindowClass.
178 	WindowClass parent_cls;        ///< Class of the parent window. @see WindowClass
179 	const char *ini_key;           ///< Key to store window defaults in openttd.cfg. \c nullptr if nothing shall be stored.
180 	uint32 flags;                  ///< Flags. @see WindowDefaultFlag
181 	const NWidgetPart *nwid_parts; ///< Nested widget parts describing the window.
182 	int16 nwid_length;             ///< Length of the #nwid_parts array.
183 	HotkeyList *hotkeys;           ///< Hotkeys for the window.
184 
185 	bool pref_sticky;              ///< Preferred stickyness.
186 	int16 pref_width;              ///< User-preferred width of the window. Zero if unset.
187 	int16 pref_height;             ///< User-preferred height of the window. Zero if unset.
188 
189 	int16 GetDefaultWidth() const;
190 	int16 GetDefaultHeight() const;
191 
192 	static void LoadFromConfig();
193 	static void SaveToConfig();
194 
195 private:
196 	int16 default_width_trad;      ///< Preferred initial width of the window (pixels at 1x zoom).
197 	int16 default_height_trad;     ///< Preferred initial height of the window (pixels at 1x zoom).
198 
199 	/**
200 	 * Dummy private copy constructor to prevent compilers from
201 	 * copying the structure, which fails due to _window_descs.
202 	 */
203 	WindowDesc(const WindowDesc &other);
204 };
205 
206 /**
207  * Window default widget/window handling flags
208  */
209 enum WindowDefaultFlag {
210 	WDF_CONSTRUCTION    =   1 << 0, ///< This window is used for construction; close it whenever changing company.
211 	WDF_MODAL           =   1 << 1, ///< The window is a modal child of some other window, meaning the parent is 'inactive'
212 	WDF_NO_FOCUS        =   1 << 2, ///< This window won't get focus/make any other window lose focus when click
213 };
214 
215 /**
216  * Data structure for resizing a window
217  */
218 struct ResizeInfo {
219 	uint step_width;  ///< Step-size of width resize changes
220 	uint step_height; ///< Step-size of height resize changes
221 };
222 
223 /** State of a sort direction button. */
224 enum SortButtonState {
225 	SBS_OFF,  ///< Do not sort (with this button).
226 	SBS_DOWN, ///< Sort ascending.
227 	SBS_UP,   ///< Sort descending.
228 };
229 
230 /**
231  * Window flags.
232  */
233 enum WindowFlags {
234 	WF_TIMEOUT           = 1 <<  0, ///< Window timeout counter.
235 
236 	WF_DRAGGING          = 1 <<  3, ///< Window is being dragged.
237 	WF_SIZING_RIGHT      = 1 <<  4, ///< Window is being resized towards the right.
238 	WF_SIZING_LEFT       = 1 <<  5, ///< Window is being resized towards the left.
239 	WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT, ///< Window is being resized.
240 	WF_STICKY            = 1 <<  6, ///< Window is made sticky by user
241 	WF_DISABLE_VP_SCROLL = 1 <<  7, ///< Window does not do autoscroll, @see HandleAutoscroll().
242 	WF_WHITE_BORDER      = 1 <<  8, ///< Window white border counter bit mask.
243 	WF_HIGHLIGHTED       = 1 <<  9, ///< Window has a widget that has a highlight.
244 	WF_CENTERED          = 1 << 10, ///< Window is centered and shall stay centered after ReInit.
245 };
246 DECLARE_ENUM_AS_BIT_SET(WindowFlags)
247 
248 static const int TIMEOUT_DURATION = 7; ///< The initial timeout value for WF_TIMEOUT.
249 static const int WHITE_BORDER_DURATION = 3; ///< The initial timeout value for WF_WHITE_BORDER.
250 
251 /**
252  * Data structure for a window viewport.
253  * A viewport is either following a vehicle (its id in then in #follow_vehicle), or it aims to display a specific
254  * location #dest_scrollpos_x, #dest_scrollpos_y (#follow_vehicle is then #INVALID_VEHICLE).
255  * The actual location being shown is #scrollpos_x, #scrollpos_y.
256  * @see InitializeViewport(), UpdateViewportPosition(), UpdateViewportCoordinates().
257  */
258 struct ViewportData : Viewport {
259 	VehicleID follow_vehicle; ///< VehicleID to follow if following a vehicle, #INVALID_VEHICLE otherwise.
260 	int32 scrollpos_x;        ///< Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
261 	int32 scrollpos_y;        ///< Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
262 	int32 dest_scrollpos_x;   ///< Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewport).
263 	int32 dest_scrollpos_y;   ///< Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewport).
264 };
265 
266 struct QueryString;
267 
268 /* misc_gui.cpp */
269 enum TooltipCloseCondition {
270 	TCC_RIGHT_CLICK,
271 	TCC_HOVER,
272 	TCC_NONE,
273 	TCC_EXIT_VIEWPORT,
274 };
275 
276 /**
277  * Data structure for an opened window
278  */
279 struct Window : ZeroedMemoryAllocator {
280 private:
281 	static std::vector<Window *> closed_windows;
282 
283 protected:
284 	void InitializeData(WindowNumber window_number);
285 	void InitializePositionSize(int x, int y, int min_width, int min_height);
286 	virtual void FindWindowPlacementAndResize(int def_width, int def_height);
287 
288 	std::vector<int> scheduled_invalidation_data;  ///< Data of scheduled OnInvalidateData() calls.
289 
290 	/* Protected to prevent deletion anywhere outside Window::DeleteClosedWindows(). */
291 	virtual ~Window();
292 
293 public:
294 	Window(WindowDesc *desc);
295 
296 	/**
297 	 * Helper allocation function to disallow something.
298 	 * Don't allow arrays; arrays of Windows are pointless as you need
299 	 * to destruct them all at the same time too, which is kinda hard.
300 	 * @param size the amount of space not to allocate
301 	 */
302 	inline void *operator new[](size_t size) = delete;
303 
304 	WindowDesc *window_desc;    ///< Window description
305 	WindowFlags flags;          ///< Window flags
306 	WindowClass window_class;   ///< Window class
307 	WindowNumber window_number; ///< Window number within the window class
308 
309 	uint8 timeout_timer;      ///< Timer value of the WF_TIMEOUT for flags.
310 	uint8 white_border_timer; ///< Timer value of the WF_WHITE_BORDER for flags.
311 
312 	int left;   ///< x position of left edge of the window
313 	int top;    ///< y position of top edge of the window
314 	int width;  ///< width of the window (number of pixels to the right in x direction)
315 	int height; ///< Height of the window (number of pixels down in y direction)
316 
317 	ResizeInfo resize;  ///< Resize information
318 
319 	Owner owner;        ///< The owner of the content shown in this window. Company colour is acquired from this variable.
320 
321 	ViewportData *viewport;          ///< Pointer to viewport data, if present.
322 	const NWidgetCore *nested_focus; ///< Currently focused nested widget, or \c nullptr if no nested widget has focus.
323 	SmallMap<int, QueryString*> querystrings; ///< QueryString associated to WWT_EDITBOX widgets.
324 	NWidgetBase *nested_root;        ///< Root of the nested tree.
325 	NWidgetBase **nested_array;      ///< Array of pointers into the tree. Do not access directly, use #Window::GetWidget() instead.
326 	uint nested_array_size;          ///< Size of the nested array.
327 	NWidgetStacked *shade_select;    ///< Selection widget (#NWID_SELECTION) to use for shading the window. If \c nullptr, window cannot shade.
328 	Dimension unshaded_size;         ///< Last known unshaded size (only valid while shaded).
329 
330 	int mouse_capture_widget;        ///< Widgetindex of current mouse capture widget (e.g. dragged scrollbar). -1 if no widget has mouse capture.
331 
332 	Window *parent;                  ///< Parent window.
333 	WindowList::iterator z_position;
334 
335 	template <class NWID>
336 	inline const NWID *GetWidget(uint widnum) const;
337 	template <class NWID>
338 	inline NWID *GetWidget(uint widnum);
339 
340 	const Scrollbar *GetScrollbar(uint widnum) const;
341 	Scrollbar *GetScrollbar(uint widnum);
342 
343 	const QueryString *GetQueryString(uint widnum) const;
344 	QueryString *GetQueryString(uint widnum);
345 
346 	virtual const char *GetFocusedText() const;
347 	virtual const char *GetCaret() const;
348 	virtual const char *GetMarkedText(size_t *length) const;
349 	virtual Point GetCaretPosition() const;
350 	virtual Rect GetTextBoundingRect(const char *from, const char *to) const;
351 	virtual const char *GetTextCharacterAtPosition(const Point &pt) const;
352 
353 	void InitNested(WindowNumber number = 0);
354 	void CreateNestedTree(bool fill_nested = true);
355 	void FinishInitNested(WindowNumber window_number = 0);
356 
357 	/**
358 	 * Set the timeout flag of the window and initiate the timer.
359 	 */
SetTimeoutWindow360 	inline void SetTimeout()
361 	{
362 		this->flags |= WF_TIMEOUT;
363 		this->timeout_timer = TIMEOUT_DURATION;
364 	}
365 
366 	/**
367 	 * Set the timeout flag of the window and initiate the timer.
368 	 */
SetWhiteBorderWindow369 	inline void SetWhiteBorder()
370 	{
371 		this->flags |= WF_WHITE_BORDER;
372 		this->white_border_timer = WHITE_BORDER_DURATION;
373 	}
374 
375 	void DisableAllWidgetHighlight();
376 	void SetWidgetHighlight(byte widget_index, TextColour highlighted_colour);
377 	bool IsWidgetHighlighted(byte widget_index) const;
378 
379 	/**
380 	 * Sets the enabled/disabled status of a widget.
381 	 * By default, widgets are enabled.
382 	 * On certain conditions, they have to be disabled.
383 	 * @param widget_index index of this widget in the window
384 	 * @param disab_stat status to use ie: disabled = true, enabled = false
385 	 */
SetWidgetDisabledStateWindow386 	inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
387 	{
388 		assert(widget_index < this->nested_array_size);
389 		if (this->nested_array[widget_index] != nullptr) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
390 	}
391 
392 	/**
393 	 * Sets a widget to disabled.
394 	 * @param widget_index index of this widget in the window
395 	 */
DisableWidgetWindow396 	inline void DisableWidget(byte widget_index)
397 	{
398 		SetWidgetDisabledState(widget_index, true);
399 	}
400 
401 	/**
402 	 * Sets a widget to Enabled.
403 	 * @param widget_index index of this widget in the window
404 	 */
EnableWidgetWindow405 	inline void EnableWidget(byte widget_index)
406 	{
407 		SetWidgetDisabledState(widget_index, false);
408 	}
409 
410 	/**
411 	 * Gets the enabled/disabled status of a widget.
412 	 * @param widget_index index of this widget in the window
413 	 * @return status of the widget ie: disabled = true, enabled = false
414 	 */
IsWidgetDisabledWindow415 	inline bool IsWidgetDisabled(byte widget_index) const
416 	{
417 		assert(widget_index < this->nested_array_size);
418 		return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
419 	}
420 
421 	/**
422 	 * Check if given widget is focused within this window
423 	 * @param widget_index : index of the widget in the window to check
424 	 * @return true if given widget is the focused window in this window
425 	 */
IsWidgetFocusedWindow426 	inline bool IsWidgetFocused(byte widget_index) const
427 	{
428 		return this->nested_focus != nullptr && this->nested_focus->index == widget_index;
429 	}
430 
431 	/**
432 	 * Check if given widget has user input focus. This means that both the window
433 	 * has focus and that the given widget has focus within the window.
434 	 * @param widget_index : index of the widget in the window to check
435 	 * @return true if given widget is the focused window in this window and this window has focus
436 	 */
IsWidgetGloballyFocusedWindow437 	inline bool IsWidgetGloballyFocused(byte widget_index) const
438 	{
439 		return _focused_window == this && IsWidgetFocused(widget_index);
440 	}
441 
442 	/**
443 	 * Sets the lowered/raised status of a widget.
444 	 * @param widget_index index of this widget in the window
445 	 * @param lowered_stat status to use ie: lowered = true, raised = false
446 	 */
SetWidgetLoweredStateWindow447 	inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
448 	{
449 		assert(widget_index < this->nested_array_size);
450 		this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
451 	}
452 
453 	/**
454 	 * Invert the lowered/raised  status of a widget.
455 	 * @param widget_index index of this widget in the window
456 	 */
ToggleWidgetLoweredStateWindow457 	inline void ToggleWidgetLoweredState(byte widget_index)
458 	{
459 		assert(widget_index < this->nested_array_size);
460 		bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
461 		this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
462 	}
463 
464 	/**
465 	 * Marks a widget as lowered.
466 	 * @param widget_index index of this widget in the window
467 	 */
LowerWidgetWindow468 	inline void LowerWidget(byte widget_index)
469 	{
470 		SetWidgetLoweredState(widget_index, true);
471 	}
472 
473 	/**
474 	 * Marks a widget as raised.
475 	 * @param widget_index index of this widget in the window
476 	 */
RaiseWidgetWindow477 	inline void RaiseWidget(byte widget_index)
478 	{
479 		SetWidgetLoweredState(widget_index, false);
480 	}
481 
482 	/**
483 	 * Gets the lowered state of a widget.
484 	 * @param widget_index index of this widget in the window
485 	 * @return status of the widget ie: lowered = true, raised= false
486 	 */
IsWidgetLoweredWindow487 	inline bool IsWidgetLowered(byte widget_index) const
488 	{
489 		assert(widget_index < this->nested_array_size);
490 		return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
491 	}
492 
493 	void UnfocusFocusedWidget();
494 	bool SetFocusedWidget(int widget_index);
495 
496 	EventState HandleEditBoxKey(int wid, WChar key, uint16 keycode);
497 	virtual void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end);
498 
499 	void HandleButtonClick(byte widget);
500 	int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
501 
502 	void RaiseButtons(bool autoraise = false);
503 	void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
504 	void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
505 	void SetWidgetDirty(byte widget_index) const;
506 
507 	void DrawWidgets() const;
508 	void DrawViewport() const;
509 	void DrawSortButtonState(int widget, SortButtonState state) const;
510 	static int SortButtonWidth();
511 
512 	void CloseChildWindows(WindowClass wc = WC_INVALID) const;
513 	virtual void Close();
514 	static void DeleteClosedWindows();
515 
516 	void SetDirty() const;
517 	void ReInit(int rx = 0, int ry = 0);
518 
519 	/** Is window shaded currently? */
IsShadedWindow520 	inline bool IsShaded() const
521 	{
522 		return this->shade_select != nullptr && this->shade_select->shown_plane == SZSP_HORIZONTAL;
523 	}
524 
525 	void SetShaded(bool make_shaded);
526 
527 	void InvalidateData(int data = 0, bool gui_scope = true);
528 	void ProcessScheduledInvalidations();
529 	void ProcessHighlightedInvalidations();
530 
531 	/*** Event handling ***/
532 
533 	/**
534 	 * Notification that the nested widget tree gets initialized. The event can be used to perform general computations.
535 	 * @note #nested_root and/or #nested_array (normally accessed via #GetWidget()) may not exist during this call.
536 	 */
OnInitWindow537 	virtual void OnInit() { }
538 
539 	virtual void ApplyDefaults();
540 
541 	/**
542 	 * Compute the initial position of the window.
543 	 * @param sm_width      Smallest width of the window.
544 	 * @param sm_height     Smallest height of the window.
545 	 * @param window_number The window number of the new window.
546 	 * @return Initial position of the top-left corner of the window.
547 	 */
548 	virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number);
549 
550 	/**
551 	 * The window must be repainted.
552 	 * @note This method should not change any state, it should only use drawing functions.
553 	 */
OnPaintWindow554 	virtual void OnPaint()
555 	{
556 		this->DrawWidgets();
557 	}
558 
559 	/**
560 	 * Draw the contents of a nested widget.
561 	 * @param r      Rectangle occupied by the widget.
562 	 * @param widget Number of the widget to draw.
563 	 * @note This method may not change any state, it may only use drawing functions.
564 	 */
DrawWidgetWindow565 	virtual void DrawWidget(const Rect &r, int widget) const {}
566 
567 	/**
568 	 * Update size and resize step of a widget in the window.
569 	 * After retrieval of the minimal size and the resize-steps of a widget, this function is called to allow further refinement,
570 	 * typically by computing the real maximal size of the content. Afterwards, \a size is taken to be the minimal size of the widget
571 	 * and \a resize is taken to contain the resize steps. For the convenience of the callee, \a padding contains the amount of
572 	 * padding between the content and the edge of the widget. This should be added to the returned size.
573 	 * @param widget  Widget number.
574 	 * @param size    Size of the widget.
575 	 * @param padding Recommended amount of space between the widget content and the widget edge.
576 	 * @param fill    Fill step of the widget.
577 	 * @param resize  Resize step of the widget.
578 	 */
UpdateWidgetSizeWindow579 	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
580 
581 	/**
582 	 * Initialize string parameters for a widget.
583 	 * Calls to this function are made during initialization to measure the size (that is as part of #InitNested()), during drawing,
584 	 * and while re-initializing the window. Only for widgets that render text initializing is requested.
585 	 * @param widget  Widget number.
586 	 */
SetStringParametersWindow587 	virtual void SetStringParameters(int widget) const {}
588 
589 	virtual void OnFocus();
590 
591 	virtual void OnFocusLost();
592 
593 	/**
594 	 * A key has been pressed.
595 	 * @param key     the Unicode value of the key.
596 	 * @param keycode the untranslated key code including shift state.
597 	 * @return #ES_HANDLED if the key press has been handled and no other
598 	 *         window should receive the event.
599 	 */
OnKeyPressWindow600 	virtual EventState OnKeyPress(WChar key, uint16 keycode) { return ES_NOT_HANDLED; }
601 
602 	virtual EventState OnHotkey(int hotkey);
603 
604 	/**
605 	 * The state of the control key has changed
606 	 * @return #ES_HANDLED if the change has been handled and no other
607 	 *         window should receive the event.
608 	 */
OnCTRLStateChangeWindow609 	virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
610 
611 
612 	/**
613 	 * A click with the left mouse button has been made on the window.
614 	 * @param pt     the point inside the window that has been clicked.
615 	 * @param widget the clicked widget.
616 	 * @param click_count Number of fast consecutive clicks at same position
617 	 */
OnClickWindow618 	virtual void OnClick(Point pt, int widget, int click_count) {}
619 
620 	/**
621 	 * A click with the right mouse button has been made on the window.
622 	 * @param pt     the point inside the window that has been clicked.
623 	 * @param widget the clicked widget.
624 	 * @return true if the click was actually handled, i.e. do not show a
625 	 *         tooltip if tooltip-on-right-click is enabled.
626 	 */
OnRightClickWindow627 	virtual bool OnRightClick(Point pt, int widget) { return false; }
628 
629 	/**
630 	 * The mouse is hovering over a widget in the window, perform an action for it.
631 	 * @param pt     The point where the mouse is hovering.
632 	 * @param widget The widget where the mouse is hovering.
633 	 */
OnHoverWindow634 	virtual void OnHover(Point pt, int widget) {}
635 
636 	/**
637 	 * Event to display a custom tooltip.
638 	 * @param pt     The point where the mouse is located.
639 	 * @param widget The widget where the mouse is located.
640 	 * @return True if the event is handled, false if it is ignored.
641 	 */
OnTooltipWindow642 	virtual bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond) { return false; }
643 
644 	/**
645 	 * An 'object' is being dragged at the provided position, highlight the target if possible.
646 	 * @param pt     The point inside the window that the mouse hovers over.
647 	 * @param widget The widget the mouse hovers over.
648 	 */
OnMouseDragWindow649 	virtual void OnMouseDrag(Point pt, int widget) {}
650 
651 	/**
652 	 * A dragged 'object' has been released.
653 	 * @param pt     the point inside the window where the release took place.
654 	 * @param widget the widget where the release took place.
655 	 */
OnDragDropWindow656 	virtual void OnDragDrop(Point pt, int widget) {}
657 
658 	/**
659 	 * Handle the request for (viewport) scrolling.
660 	 * @param delta the amount the viewport must be scrolled.
661 	 */
OnScrollWindow662 	virtual void OnScroll(Point delta) {}
663 
664 	/**
665 	 * The mouse is currently moving over the window or has just moved outside
666 	 * of the window. In the latter case pt is (-1, -1).
667 	 * @param pt     the point inside the window that the mouse hovers over.
668 	 * @param widget the widget the mouse hovers over.
669 	 */
OnMouseOverWindow670 	virtual void OnMouseOver(Point pt, int widget) {}
671 
672 	/**
673 	 * The mouse wheel has been turned.
674 	 * @param wheel the amount of movement of the mouse wheel.
675 	 */
OnMouseWheelWindow676 	virtual void OnMouseWheel(int wheel) {}
677 
678 
679 	/**
680 	 * Called for every mouse loop run, which is at least once per (game) tick.
681 	 */
OnMouseLoopWindow682 	virtual void OnMouseLoop() {}
683 
684 	/**
685 	 * Called once per (game) tick.
686 	 */
OnGameTickWindow687 	virtual void OnGameTick() {}
688 
689 	/**
690 	 * Called once every 100 (game) ticks, or once every 3s, whichever comes last.
691 	 * In normal game speed the frequency is 1 call every 100 ticks (can be more than 3s).
692 	 * In fast-forward the frequency is 1 call every ~3s (can be more than 100 ticks).
693 	 */
OnHundredthTickWindow694 	virtual void OnHundredthTick() {}
695 
696 	/**
697 	 * Called periodically.
698 	 */
OnRealtimeTickWindow699 	virtual void OnRealtimeTick(uint delta_ms) {}
700 
701 	/**
702 	 * Called when this window's timeout has been reached.
703 	 */
OnTimeoutWindow704 	virtual void OnTimeout() {}
705 
706 
707 	/**
708 	 * Called after the window got resized.
709 	 * For nested windows with a viewport, call NWidgetViewport::UpdateViewportCoordinates.
710 	 */
OnResizeWindow711 	virtual void OnResize() {}
712 
713 	/**
714 	 * A dropdown option associated to this window has been selected.
715 	 * @param widget the widget (button) that the dropdown is associated with.
716 	 * @param index  the element in the dropdown that is selected.
717 	 */
OnDropdownSelectWindow718 	virtual void OnDropdownSelect(int widget, int index) {}
719 
720 	virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close);
721 
722 	/**
723 	 * The text in an editbox has been edited.
724 	 * @param widget The widget of the editbox.
725 	 */
OnEditboxChangedWindow726 	virtual void OnEditboxChanged(int widget) {}
727 
728 	/**
729 	 * The query window opened from this window has closed.
730 	 * @param str the new value of the string, nullptr if the window
731 	 *            was cancelled or an empty string when the default
732 	 *            button was pressed, i.e. StrEmpty(str).
733 	 */
OnQueryTextFinishedWindow734 	virtual void OnQueryTextFinished(char *str) {}
735 
736 	/**
737 	 * Some data on this window has become invalid.
738 	 * @param data information about the changed data.
739 	 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
740 	 */
741 	virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
742 
743 	/**
744 	 * The user clicked some place on the map when a tile highlight mode
745 	 * has been set.
746 	 * @param pt   the exact point on the map that has been clicked.
747 	 * @param tile the tile on the map that has been clicked.
748 	 */
OnPlaceObjectWindow749 	virtual void OnPlaceObject(Point pt, TileIndex tile) {}
750 
751 	/**
752 	 * The user clicked on a vehicle while HT_VEHICLE has been set.
753 	 * @param v clicked vehicle. It is guaranteed to be v->IsPrimaryVehicle() == true
754 	 * @return True if the click is handled, false if it is ignored.
755 	 */
OnVehicleSelectWindow756 	virtual bool OnVehicleSelect(const struct Vehicle *v) { return false; }
757 
758 	/**
759 	 * The user cancelled a tile highlight mode that has been set.
760 	 */
OnPlaceObjectAbortWindow761 	virtual void OnPlaceObjectAbort() {}
762 
763 
764 	/**
765 	 * The user is dragging over the map when the tile highlight mode
766 	 * has been set.
767 	 * @param select_method the method of selection (allowed directions)
768 	 * @param select_proc   what will be created when the drag is over.
769 	 * @param pt            the exact point on the map where the mouse is.
770 	 */
OnPlaceDragWindow771 	virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
772 
773 	/**
774 	 * The user has dragged over the map when the tile highlight mode
775 	 * has been set.
776 	 * @param select_method the method of selection (allowed directions)
777 	 * @param select_proc   what should be created.
778 	 * @param pt            the exact point on the map where the mouse was released.
779 	 * @param start_tile    the begin tile of the drag.
780 	 * @param end_tile      the end tile of the drag.
781 	 */
OnPlaceMouseUpWindow782 	virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
783 
784 	/**
785 	 * The user moves over the map when a tile highlight mode has been set
786 	 * when the special mouse mode has been set to 'PRESIZE' mode. An
787 	 * example of this is the tile highlight for dock building.
788 	 * @param pt   the exact point on the map where the mouse is.
789 	 * @param tile the tile on the map where the mouse is.
790 	 */
OnPlacePresizeWindow791 	virtual void OnPlacePresize(Point pt, TileIndex tile) {}
792 
793 	/*** End of the event handling ***/
794 
795 	/**
796 	 * Is the data related to this window NewGRF inspectable?
797 	 * @return true iff it is inspectable.
798 	 */
IsNewGRFInspectableWindow799 	virtual bool IsNewGRFInspectable() const { return false; }
800 
801 	/**
802 	 * Show the NewGRF inspection window. When this function is called it is
803 	 * up to the window to call and pass the right parameters to the
804 	 * ShowInspectWindow function.
805 	 * @pre this->IsNewGRFInspectable()
806 	 */
ShowNewGRFInspectWindowWindow807 	virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
808 
809 	/**
810 	 * Iterator to iterate all valid Windows
811 	 * @tparam TtoBack whether we iterate towards the back.
812 	 */
813 	template <bool TtoBack>
814 	struct WindowIterator {
815 		typedef Window *value_type;
816 		typedef value_type *pointer;
817 		typedef value_type &reference;
818 		typedef size_t difference_type;
819 		typedef std::forward_iterator_tag iterator_category;
820 
WindowIteratorWindow::WindowIterator821 		explicit WindowIterator(WindowList::iterator start) : it(start)
822 		{
823 			this->Validate();
824 		}
WindowIteratorWindow::WindowIterator825 		explicit WindowIterator(const Window *w) : it(w->z_position) {}
826 
827 		bool operator==(const WindowIterator &other) const { return this->it == other.it; }
828 		bool operator!=(const WindowIterator &other) const { return !(*this == other); }
829 		Window * operator*() const { return *this->it; }
830 		WindowIterator & operator++() { this->Next(); this->Validate(); return *this; }
831 
IsEndWindow::WindowIterator832 		bool IsEnd() const { return this->it == _z_windows.end(); }
833 
834 	private:
835 		WindowList::iterator it;
ValidateWindow::WindowIterator836 		void Validate()
837 		{
838 			while (!this->IsEnd() && *this->it == nullptr) this->Next();
839 		}
NextWindow::WindowIterator840 		void Next()
841 		{
842 			if constexpr (!TtoBack) {
843 				++this->it;
844 			} else if (this->it == _z_windows.begin()) {
845 				this->it = _z_windows.end();
846 			} else {
847 				--this->it;
848 			}
849 		}
850 	};
851 	using IteratorToFront = WindowIterator<false>; //!< Iterate in Z order towards front.
852 	using IteratorToBack = WindowIterator<true>; //!< Iterate in Z order towards back.
853 
854 	/**
855 	 * Iterable ensemble of all valid Windows
856 	 * @tparam Tfront Wether we iterate from front
857 	 */
858 	template <bool Tfront>
859 	struct AllWindows {
AllWindowsWindow::AllWindows860 		AllWindows() {}
beginWindow::AllWindows861 		WindowIterator<Tfront> begin()
862 		{
863 			if constexpr (Tfront) {
864 				auto back = _z_windows.end();
865 				if (back != _z_windows.begin()) --back;
866 				return WindowIterator<Tfront>(back);
867 			} else {
868 				return WindowIterator<Tfront>(_z_windows.begin());
869 			}
870 		}
endWindow::AllWindows871 		WindowIterator<Tfront> end() { return WindowIterator<Tfront>(_z_windows.end()); }
872 	};
873 	using Iterate = AllWindows<false>; //!< Iterate all windows in whatever order is easiest.
874 	using IterateFromBack = AllWindows<false>; //!< Iterate all windows in Z order from back to front.
875 	using IterateFromFront = AllWindows<true>; //!< Iterate all windows in Z order from front to back.
876 };
877 
878 /**
879  * Get the nested widget with number \a widnum from the nested widget tree.
880  * @tparam NWID Type of the nested widget.
881  * @param widnum Widget number of the widget to retrieve.
882  * @return The requested widget if it is instantiated, \c nullptr otherwise.
883  */
884 template <class NWID>
GetWidget(uint widnum)885 inline NWID *Window::GetWidget(uint widnum)
886 {
887 	if (widnum >= this->nested_array_size || this->nested_array[widnum] == nullptr) return nullptr;
888 	NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
889 	assert(nwid != nullptr);
890 	return nwid;
891 }
892 
893 /** Specialized case of #Window::GetWidget for the nested widget base class. */
894 template <>
895 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
896 {
897 	if (widnum >= this->nested_array_size) return nullptr;
898 	return this->nested_array[widnum];
899 }
900 
901 /**
902  * Get the nested widget with number \a widnum from the nested widget tree.
903  * @tparam NWID Type of the nested widget.
904  * @param widnum Widget number of the widget to retrieve.
905  * @return The requested widget if it is instantiated, \c nullptr otherwise.
906  */
907 template <class NWID>
GetWidget(uint widnum)908 inline const NWID *Window::GetWidget(uint widnum) const
909 {
910 	return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
911 }
912 
913 
914 /**
915  * Base class for windows opened from a toolbar.
916  */
917 class PickerWindowBase : public Window {
918 
919 public:
PickerWindowBase(WindowDesc * desc,Window * parent)920 	PickerWindowBase(WindowDesc *desc, Window *parent) : Window(desc)
921 	{
922 		this->parent = parent;
923 	}
924 
925 	void Close() override;
926 };
927 
928 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
929 Window *FindWindowFromPt(int x, int y);
930 
931 /**
932  * Open a new window.
933  * @tparam Wcls %Window class to use if the window does not exist.
934  * @param desc The pointer to the WindowDesc to be created
935  * @param window_number the window number of the new window
936  * @param return_existing If set, also return the window if it already existed.
937  * @return %Window pointer of the newly created window, or the existing one if \a return_existing is set, or \c nullptr.
938  */
939 template <typename Wcls>
940 Wcls *AllocateWindowDescFront(WindowDesc *desc, int window_number, bool return_existing = false)
941 {
942 	Wcls *w = static_cast<Wcls *>(BringWindowToFrontById(desc->cls, window_number));
943 	if (w != nullptr) return return_existing ? w : nullptr;
944 	return new Wcls(desc, window_number);
945 }
946 
947 void RelocateAllWindows(int neww, int newh);
948 
949 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = nullptr, TooltipCloseCondition close_tooltip = TCC_HOVER);
950 
951 /* widget.cpp */
952 int GetWidgetFromPos(const Window *w, int x, int y);
953 
954 extern Point _cursorpos_drag_start;
955 
956 extern int _scrollbar_start_pos;
957 extern int _scrollbar_size;
958 extern byte _scroller_click_timeout;
959 
960 extern bool _scrolling_viewport;
961 extern bool _mouse_hovering;
962 
963 /** Mouse modes. */
964 enum SpecialMouseMode {
965 	WSM_NONE,     ///< No special mouse mode.
966 	WSM_DRAGDROP, ///< Drag&drop an object.
967 	WSM_SIZING,   ///< Sizing mode.
968 	WSM_PRESIZE,  ///< Presizing mode (docks, tunnels).
969 	WSM_DRAGGING, ///< Dragging mode (trees).
970 };
971 extern SpecialMouseMode _special_mouse_mode;
972 
973 void SetFocusedWindow(Window *w);
974 
975 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
976 
977 #endif /* WINDOW_GUI_H */
978