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/>.
8 /** @file window_gui.h Functions, definitions and such used only by the GUI. */
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"
25 * Flags to describe the look of the frame
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)
35 DECLARE_ENUM_AS_BIT_SET(FrameFlags
)
37 /** Distances used in drawing widgets. */
38 enum WidgetDrawDistances
{
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.
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.
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.
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.
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
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
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
);
148 using WindowList
= std::list
<Window
*>;
149 extern WindowList _z_windows
;
150 extern Window
*_focused_window
;
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
161 Point
GetToolbarAlignedWindowPosition(int window_width
);
166 * High level window description
168 struct WindowDesc
: ZeroedMemoryAllocator
{
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);
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.
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.
189 int16
GetDefaultWidth() const;
190 int16
GetDefaultHeight() const;
192 static void LoadFromConfig();
193 static void SaveToConfig();
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).
200 * Dummy private copy constructor to prevent compilers from
201 * copying the structure, which fails due to _window_descs.
203 WindowDesc(const WindowDesc
&other
);
207 * Window default widget/window handling flags
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
216 * Data structure for resizing a window
219 uint step_width
; ///< Step-size of width resize changes
220 uint step_height
; ///< Step-size of height resize changes
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.
234 WF_TIMEOUT
= 1 << 0, ///< Window timeout counter.
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.
246 DECLARE_ENUM_AS_BIT_SET(WindowFlags
)
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.
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().
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).
269 enum TooltipCloseCondition
{
277 * Data structure for an opened window
279 struct Window
: ZeroedMemoryAllocator
{
281 static std::vector
<Window
*> closed_windows
;
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
);
288 std::vector
<int> scheduled_invalidation_data
; ///< Data of scheduled OnInvalidateData() calls.
290 /* Protected to prevent deletion anywhere outside Window::DeleteClosedWindows(). */
294 Window(WindowDesc
*desc
);
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
302 inline void *operator new[](size_t size
) = delete;
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
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.
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)
317 ResizeInfo resize
; ///< Resize information
319 Owner owner
; ///< The owner of the content shown in this window. Company colour is acquired from this variable.
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).
330 int mouse_capture_widget
; ///< Widgetindex of current mouse capture widget (e.g. dragged scrollbar). -1 if no widget has mouse capture.
332 Window
*parent
; ///< Parent window.
333 WindowList::iterator z_position
;
335 template <class NWID
>
336 inline const NWID
*GetWidget(uint widnum
) const;
337 template <class NWID
>
338 inline NWID
*GetWidget(uint widnum
);
340 const Scrollbar
*GetScrollbar(uint widnum
) const;
341 Scrollbar
*GetScrollbar(uint widnum
);
343 const QueryString
*GetQueryString(uint widnum
) const;
344 QueryString
*GetQueryString(uint widnum
);
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;
353 void InitNested(WindowNumber number
= 0);
354 void CreateNestedTree(bool fill_nested
= true);
355 void FinishInitNested(WindowNumber window_number
= 0);
358 * Set the timeout flag of the window and initiate the timer.
360 inline void SetTimeout()
362 this->flags
|= WF_TIMEOUT
;
363 this->timeout_timer
= TIMEOUT_DURATION
;
367 * Set the timeout flag of the window and initiate the timer.
369 inline void SetWhiteBorder()
371 this->flags
|= WF_WHITE_BORDER
;
372 this->white_border_timer
= WHITE_BORDER_DURATION
;
375 void DisableAllWidgetHighlight();
376 void SetWidgetHighlight(byte widget_index
, TextColour highlighted_colour
);
377 bool IsWidgetHighlighted(byte widget_index
) const;
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
386 inline void SetWidgetDisabledState(byte widget_index
, bool disab_stat
)
388 assert(widget_index
< this->nested_array_size
);
389 if (this->nested_array
[widget_index
] != nullptr) this->GetWidget
<NWidgetCore
>(widget_index
)->SetDisabled(disab_stat
);
393 * Sets a widget to disabled.
394 * @param widget_index index of this widget in the window
396 inline void DisableWidget(byte widget_index
)
398 SetWidgetDisabledState(widget_index
, true);
402 * Sets a widget to Enabled.
403 * @param widget_index index of this widget in the window
405 inline void EnableWidget(byte widget_index
)
407 SetWidgetDisabledState(widget_index
, false);
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
415 inline bool IsWidgetDisabled(byte widget_index
) const
417 assert(widget_index
< this->nested_array_size
);
418 return this->GetWidget
<NWidgetCore
>(widget_index
)->IsDisabled();
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
426 inline bool IsWidgetFocused(byte widget_index
) const
428 return this->nested_focus
!= nullptr && this->nested_focus
->index
== widget_index
;
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
437 inline bool IsWidgetGloballyFocused(byte widget_index
) const
439 return _focused_window
== this && IsWidgetFocused(widget_index
);
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
447 inline void SetWidgetLoweredState(byte widget_index
, bool lowered_stat
)
449 assert(widget_index
< this->nested_array_size
);
450 this->GetWidget
<NWidgetCore
>(widget_index
)->SetLowered(lowered_stat
);
454 * Invert the lowered/raised status of a widget.
455 * @param widget_index index of this widget in the window
457 inline void ToggleWidgetLoweredState(byte widget_index
)
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
);
465 * Marks a widget as lowered.
466 * @param widget_index index of this widget in the window
468 inline void LowerWidget(byte widget_index
)
470 SetWidgetLoweredState(widget_index
, true);
474 * Marks a widget as raised.
475 * @param widget_index index of this widget in the window
477 inline void RaiseWidget(byte widget_index
)
479 SetWidgetLoweredState(widget_index
, false);
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
487 inline bool IsWidgetLowered(byte widget_index
) const
489 assert(widget_index
< this->nested_array_size
);
490 return this->GetWidget
<NWidgetCore
>(widget_index
)->IsLowered();
493 void UnfocusFocusedWidget();
494 bool SetFocusedWidget(int widget_index
);
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
);
499 void HandleButtonClick(byte widget
);
500 int GetRowFromWidget(int clickpos
, int widget
, int padding
, int line_height
= -1) const;
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;
507 void DrawWidgets() const;
508 void DrawViewport() const;
509 void DrawSortButtonState(int widget
, SortButtonState state
) const;
510 static int SortButtonWidth();
512 void CloseChildWindows(WindowClass wc
= WC_INVALID
) const;
513 virtual void Close();
514 static void DeleteClosedWindows();
516 void SetDirty() const;
517 void ReInit(int rx
= 0, int ry
= 0);
519 /** Is window shaded currently? */
520 inline bool IsShaded() const
522 return this->shade_select
!= nullptr && this->shade_select
->shown_plane
== SZSP_HORIZONTAL
;
525 void SetShaded(bool make_shaded
);
527 void InvalidateData(int data
= 0, bool gui_scope
= true);
528 void ProcessScheduledInvalidations();
529 void ProcessHighlightedInvalidations();
531 /*** Event handling ***/
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.
537 virtual void OnInit() { }
539 virtual void ApplyDefaults();
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.
548 virtual Point
OnInitialPosition(int16 sm_width
, int16 sm_height
, int window_number
);
551 * The window must be repainted.
552 * @note This method should not change any state, it should only use drawing functions.
554 virtual void OnPaint()
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.
565 virtual void DrawWidget(const Rect
&r
, int widget
) const {}
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.
579 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) {}
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.
587 virtual void SetStringParameters(int widget
) const {}
589 virtual void OnFocus();
591 virtual void OnFocusLost();
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.
600 virtual EventState
OnKeyPress(WChar key
, uint16 keycode
) { return ES_NOT_HANDLED
; }
602 virtual EventState
OnHotkey(int hotkey
);
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.
609 virtual EventState
OnCTRLStateChange() { return ES_NOT_HANDLED
; }
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
618 virtual void OnClick(Point pt
, int widget
, int click_count
) {}
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.
627 virtual bool OnRightClick(Point pt
, int widget
) { return false; }
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.
634 virtual void OnHover(Point pt
, int widget
) {}
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.
642 virtual bool OnTooltip(Point pt
, int widget
, TooltipCloseCondition close_cond
) { return false; }
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.
649 virtual void OnMouseDrag(Point pt
, int widget
) {}
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.
656 virtual void OnDragDrop(Point pt
, int widget
) {}
659 * Handle the request for (viewport) scrolling.
660 * @param delta the amount the viewport must be scrolled.
662 virtual void OnScroll(Point delta
) {}
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.
670 virtual void OnMouseOver(Point pt
, int widget
) {}
673 * The mouse wheel has been turned.
674 * @param wheel the amount of movement of the mouse wheel.
676 virtual void OnMouseWheel(int wheel
) {}
680 * Called for every mouse loop run, which is at least once per (game) tick.
682 virtual void OnMouseLoop() {}
685 * Called once per (game) tick.
687 virtual void OnGameTick() {}
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).
694 virtual void OnHundredthTick() {}
697 * Called periodically.
699 virtual void OnRealtimeTick(uint delta_ms
) {}
702 * Called when this window's timeout has been reached.
704 virtual void OnTimeout() {}
708 * Called after the window got resized.
709 * For nested windows with a viewport, call NWidgetViewport::UpdateViewportCoordinates.
711 virtual void OnResize() {}
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.
718 virtual void OnDropdownSelect(int widget
, int index
) {}
720 virtual void OnDropdownClose(Point pt
, int widget
, int index
, bool instant_close
);
723 * The text in an editbox has been edited.
724 * @param widget The widget of the editbox.
726 virtual void OnEditboxChanged(int widget
) {}
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).
734 virtual void OnQueryTextFinished(char *str
) {}
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.
741 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true) {}
744 * The user clicked some place on the map when a tile highlight mode
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.
749 virtual void OnPlaceObject(Point pt
, TileIndex tile
) {}
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.
756 virtual bool OnVehicleSelect(const struct Vehicle
*v
) { return false; }
759 * The user cancelled a tile highlight mode that has been set.
761 virtual void OnPlaceObjectAbort() {}
765 * The user is dragging over the map when the tile highlight mode
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.
771 virtual void OnPlaceDrag(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
) {}
774 * The user has dragged over the map when the tile highlight mode
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.
782 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
, TileIndex start_tile
, TileIndex end_tile
) {}
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.
791 virtual void OnPlacePresize(Point pt
, TileIndex tile
) {}
793 /*** End of the event handling ***/
796 * Is the data related to this window NewGRF inspectable?
797 * @return true iff it is inspectable.
799 virtual bool IsNewGRFInspectable() const { return false; }
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()
807 virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
810 * Iterator to iterate all valid Windows
811 * @tparam TtoBack whether we iterate towards the back.
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
;
821 explicit WindowIterator(WindowList::iterator start
) : it(start
)
825 explicit WindowIterator(const Window
*w
) : it(w
->z_position
) {}
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; }
832 bool IsEnd() const { return this->it
== _z_windows
.end(); }
835 WindowList::iterator it
;
838 while (!this->IsEnd() && *this->it
== nullptr) this->Next();
842 if constexpr (!TtoBack
) {
844 } else if (this->it
== _z_windows
.begin()) {
845 this->it
= _z_windows
.end();
851 using IteratorToFront
= WindowIterator
<false>; //!< Iterate in Z order towards front.
852 using IteratorToBack
= WindowIterator
<true>; //!< Iterate in Z order towards back.
855 * Iterable ensemble of all valid Windows
856 * @tparam Tfront Wether we iterate from front
858 template <bool Tfront
>
861 WindowIterator
<Tfront
> begin()
863 if constexpr (Tfront
) {
864 auto back
= _z_windows
.end();
865 if (back
!= _z_windows
.begin()) --back
;
866 return WindowIterator
<Tfront
>(back
);
868 return WindowIterator
<Tfront
>(_z_windows
.begin());
871 WindowIterator
<Tfront
> end() { return WindowIterator
<Tfront
>(_z_windows
.end()); }
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.
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.
884 template <class NWID
>
885 inline NWID
*Window::GetWidget(uint widnum
)
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);
893 /** Specialized case of #Window::GetWidget for the nested widget base class. */
895 inline const NWidgetBase
*Window::GetWidget
<NWidgetBase
>(uint widnum
) const
897 if (widnum
>= this->nested_array_size
) return nullptr;
898 return this->nested_array
[widnum
];
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.
907 template <class NWID
>
908 inline const NWID
*Window::GetWidget(uint widnum
) const
910 return const_cast<Window
*>(this)->GetWidget
<NWID
>(widnum
);
915 * Base class for windows opened from a toolbar.
917 class PickerWindowBase
: public Window
{
920 PickerWindowBase(WindowDesc
*desc
, Window
*parent
) : Window(desc
)
922 this->parent
= parent
;
925 void Close() override
;
928 Window
*BringWindowToFrontById(WindowClass cls
, WindowNumber number
);
929 Window
*FindWindowFromPt(int x
, int y
);
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.
939 template <typename Wcls
>
940 Wcls
*AllocateWindowDescFront(WindowDesc
*desc
, int window_number
, bool return_existing
= false)
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
);
947 void RelocateAllWindows(int neww
, int newh
);
949 void GuiShowTooltips(Window
*parent
, StringID str
, uint paramcount
= 0, const uint64 params
[] = nullptr, TooltipCloseCondition close_tooltip
= TCC_HOVER
);
952 int GetWidgetFromPos(const Window
*w
, int x
, int y
);
954 extern Point _cursorpos_drag_start
;
956 extern int _scrollbar_start_pos
;
957 extern int _scrollbar_size
;
958 extern byte _scroller_click_timeout
;
960 extern bool _scrolling_viewport
;
961 extern bool _mouse_hovering
;
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).
971 extern SpecialMouseMode _special_mouse_mode
;
973 void SetFocusedWindow(Window
*w
);
975 void ScrollbarClickHandler(Window
*w
, NWidgetCore
*nw
, int x
, int y
);
977 #endif /* WINDOW_GUI_H */