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. */
13 #include "vehiclelist.h"
14 #include "vehicle_type.h"
15 #include "viewport_type.h"
16 #include "company_type.h"
17 #include "tile_type.h"
18 #include "widget_type.h"
19 #include "string_type.h"
22 * Flags to describe the look of the frame
26 FR_TRANSPARENT
= 1 << 0, ///< Makes the background transparent if set
27 FR_BORDERONLY
= 1 << 4, ///< Draw border only, no background
28 FR_LOWERED
= 1 << 5, ///< If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
29 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)
32 DECLARE_ENUM_AS_BIT_SET(FrameFlags
)
34 class WidgetDimensions
{
36 RectPadding imgbtn
; ///< Padding around image button image.
37 RectPadding inset
; ///< Padding inside inset container.
38 RectPadding vscrollbar
; ///< Padding inside vertical scrollbar buttons.
39 RectPadding hscrollbar
; ///< Padding inside horizontal scrollbar buttons.
40 RectPadding bevel
; ///< Bevel thickness, affected by "scaled bevels" game option.
41 RectPadding fullbevel
; ///< Always-scaled bevel thickness.
42 RectPadding framerect
; ///< Standard padding inside many panels.
43 RectPadding frametext
; ///< Padding inside frame with text.
44 RectPadding matrix
; ///< Padding of WWT_MATRIX items.
45 RectPadding shadebox
; ///< Padding around image in shadebox widget.
46 RectPadding stickybox
; ///< Padding around image in stickybox widget.
47 RectPadding debugbox
; ///< Padding around image in debugbox widget.
48 RectPadding defsizebox
; ///< Padding around image in defsizebox widget.
49 RectPadding resizebox
; ///< Padding around image in resizebox widget.
50 RectPadding closebox
; ///< Padding around image in closebox widget.
51 RectPadding captiontext
; ///< Padding for text within caption widget.
52 RectPadding dropdowntext
; ///< Padding of drop down list item.
53 RectPadding dropdownlist
; ///< Padding of complete drop down list.
54 RectPadding modalpopup
; ///< Spacing for popup warning/information windows.
55 RectPadding picker
; ///< Padding for a picker (dock, station, etc) window.
56 RectPadding sparse
; ///< Padding used for 'sparse' widget window, usually containing multiple frames.
57 RectPadding sparse_resize
; ///< Padding used for a resizeable 'sparse' widget window, usually containing multiple frames.
59 int vsep_picker
; ///< Vertical spacing of picker-window widgets.
60 int vsep_normal
; ///< Normal vertical spacing.
61 int vsep_sparse
; ///< Normal vertical spacing for 'sparse' widget window.
62 int vsep_wide
; ///< Wide vertical spacing.
63 int hsep_normal
; ///< Normal horizontal spacing.
64 int hsep_wide
; ///< Wide horizontal spacing.
65 int hsep_indent
; ///< Width of identation for tree layouts.
67 static const WidgetDimensions unscaled
; ///< Unscaled widget dimensions.
68 static WidgetDimensions scaled
; ///< Widget dimensions scaled for current zoom level.
70 static constexpr float ASPECT_LOCATION
= 12.f
/ 14.f
;
71 static constexpr float ASPECT_RENAME
= 12.f
/ 14.f
;
72 static constexpr float ASPECT_SETTINGS_BUTTON
= 21.f
/ 12.f
;
73 static constexpr float ASPECT_TOGGLE_SIZE
= 12.f
/ 14.f
;
74 static constexpr float ASPECT_LEFT_RIGHT_BUTTON
= 8.f
/ 12.f
;
75 static constexpr float ASPECT_UP_DOWN_BUTTON
= 11.f
/ 12.f
;
76 static constexpr float ASPECT_VEHICLE_ICON
= 15.f
/ 12.f
;
77 static constexpr float ASPECT_VEHICLE_FLAG
= 11.f
/ 12.f
;
81 * Distances used in drawing widgets.
82 * These constants should not be used elsewhere, use scaled/unscaled WidgetDimensions instead.
84 enum WidgetDrawDistances
{
85 WD_SHADEBOX_WIDTH
= 12, ///< Minimum width of a standard shade box widget.
86 WD_STICKYBOX_WIDTH
= 12, ///< Minimum width of a standard sticky box widget.
87 WD_DEBUGBOX_WIDTH
= 12, ///< Minimum width of a standard debug box widget.
88 WD_DEFSIZEBOX_WIDTH
= 12, ///< Minimum width of a standard defsize box widget.
89 WD_RESIZEBOX_WIDTH
= 12, ///< Minimum width of a resize box widget.
90 WD_CLOSEBOX_WIDTH
= 11, ///< Minimum width of a close box widget.
92 WD_CAPTION_HEIGHT
= 14, ///< Minimum height of a title bar.
93 WD_DROPDOWN_HEIGHT
= 12, ///< Minimum height of a drop down widget.
99 inline constexpr WidgetDimensions
WidgetDimensions::unscaled
= {
100 .imgbtn
= { .left
= 1, .top
= 1, .right
= 1, .bottom
= 1},
101 .inset
= { .left
= 2, .top
= 1, .right
= 2, .bottom
= 1},
102 .vscrollbar
= { .left
= 2, .top
= 3, .right
= 2, .bottom
= 3},
103 .hscrollbar
= { .left
= 3, .top
= 2, .right
= 3, .bottom
= 2},
104 .bevel
= { .left
= 1, .top
= 1, .right
= 1, .bottom
= 1},
105 .fullbevel
= { .left
= 1, .top
= 1, .right
= 1, .bottom
= 1},
106 .framerect
= { .left
= 2, .top
= 1, .right
= 2, .bottom
= 1},
107 .frametext
= { .left
= 6, .top
= 6, .right
= 6, .bottom
= 6},
108 .matrix
= { .left
= 2, .top
= 3, .right
= 2, .bottom
= 1},
109 .shadebox
= { .left
= 2, .top
= 3, .right
= 2, .bottom
= 3},
110 .stickybox
= { .left
= 2, .top
= 3, .right
= 2, .bottom
= 3},
111 .debugbox
= { .left
= 2, .top
= 3, .right
= 2, .bottom
= 3},
112 .defsizebox
= { .left
= 2, .top
= 3, .right
= 2, .bottom
= 3},
113 .resizebox
= { .left
= 2, .top
= 2, .right
= 2, .bottom
= 2},
114 .closebox
= { .left
= 2, .top
= 2, .right
= 1, .bottom
= 2},
115 .captiontext
= { .left
= 2, .top
= 2, .right
= 2, .bottom
= 2},
116 .dropdowntext
= { .left
= 2, .top
= 1, .right
= 2, .bottom
= 1},
117 .dropdownlist
= { .left
= 1, .top
= 2, .right
= 1, .bottom
= 2},
118 .modalpopup
= { .left
= 20, .top
= 10, .right
= 20, .bottom
= 10},
119 .picker
= { .left
= 3, .top
= 3, .right
= 3, .bottom
= 3},
120 .sparse
= { .left
= 10, .top
= 8, .right
= 10, .bottom
= 8},
121 .sparse_resize
= { .left
= 10, .top
= 8, .right
= 10, .bottom
= 0},
132 void DrawFrameRect(int left
, int top
, int right
, int bottom
, Colours colour
, FrameFlags flags
);
134 inline void DrawFrameRect(const Rect
&r
, Colours colour
, FrameFlags flags
)
136 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, flags
);
139 void DrawCaption(const Rect
&r
, Colours colour
, Owner owner
, TextColour text_colour
, StringID str
, StringAlignment align
, FontSize fs
);
142 using WindowList
= std::list
<Window
*>;
143 extern WindowList _z_windows
;
144 extern Window
*_focused_window
;
147 /** How do we the window to be placed? */
148 enum WindowPosition
{
149 WDP_MANUAL
, ///< Manually align the window (so no automatic location finding)
150 WDP_AUTO
, ///< Find a place automatically
151 WDP_CENTER
, ///< Center the window
152 WDP_ALIGN_TOOLBAR
, ///< Align toward the toolbar
155 Point
GetToolbarAlignedWindowPosition(int window_width
);
160 * High level window description
162 struct WindowDesc
: ZeroedMemoryAllocator
{
164 WindowDesc(WindowPosition default_pos
, const char *ini_key
, int16_t def_width_trad
, int16_t def_height_trad
,
165 WindowClass window_class
, WindowClass parent_class
, uint32_t flags
,
166 const std::span
<const NWidgetPart
> nwid_parts
, HotkeyList
*hotkeys
= nullptr,
167 const std::source_location location
= std::source_location::current());
171 const std::source_location source_location
; ///< Source location of this definition
172 WindowPosition default_pos
; ///< Preferred position of the window. @see WindowPosition()
173 WindowClass cls
; ///< Class of the window, @see WindowClass.
174 WindowClass parent_cls
; ///< Class of the parent window. @see WindowClass
175 const char *ini_key
; ///< Key to store window defaults in openttd.cfg. \c nullptr if nothing shall be stored.
176 uint32_t flags
; ///< Flags. @see WindowDefaultFlag
177 const std::span
<const NWidgetPart
> nwid_parts
; ///< Span of nested widget parts describing the window.
178 HotkeyList
*hotkeys
; ///< Hotkeys for the window.
180 bool pref_sticky
; ///< Preferred stickyness.
181 int16_t pref_width
; ///< User-preferred width of the window. Zero if unset.
182 int16_t pref_height
; ///< User-preferred height of the window. Zero if unset.
184 int16_t GetDefaultWidth() const;
185 int16_t GetDefaultHeight() const;
187 static void LoadFromConfig();
188 static void SaveToConfig();
191 int16_t default_width_trad
; ///< Preferred initial width of the window (pixels at 1x zoom).
192 int16_t default_height_trad
; ///< Preferred initial height of the window (pixels at 1x zoom).
195 * Delete copy constructor to prevent compilers from
196 * copying the structure, which fails due to _window_descs.
198 WindowDesc(const WindowDesc
&) = delete;
199 WindowDesc
& operator=(const WindowDesc
&) = delete;
203 * Window default widget/window handling flags
205 enum WindowDefaultFlag
{
206 WDF_CONSTRUCTION
= 1 << 0, ///< This window is used for construction; close it whenever changing company.
207 WDF_MODAL
= 1 << 1, ///< The window is a modal child of some other window, meaning the parent is 'inactive'
208 WDF_NO_FOCUS
= 1 << 2, ///< This window won't get focus/make any other window lose focus when click
209 WDF_NO_CLOSE
= 1 << 3, ///< This window can't be interactively closed
213 * Data structure for resizing a window
216 uint step_width
; ///< Step-size of width resize changes
217 uint step_height
; ///< Step-size of height resize changes
220 /** State of a sort direction button. */
221 enum SortButtonState
{
222 SBS_OFF
, ///< Do not sort (with this button).
223 SBS_DOWN
, ///< Sort ascending.
224 SBS_UP
, ///< Sort descending.
231 WF_TIMEOUT
= 1 << 0, ///< Window timeout counter.
233 WF_DRAGGING
= 1 << 3, ///< Window is being dragged.
234 WF_SIZING_RIGHT
= 1 << 4, ///< Window is being resized towards the right.
235 WF_SIZING_LEFT
= 1 << 5, ///< Window is being resized towards the left.
236 WF_SIZING
= WF_SIZING_RIGHT
| WF_SIZING_LEFT
, ///< Window is being resized.
237 WF_STICKY
= 1 << 6, ///< Window is made sticky by user
238 WF_DISABLE_VP_SCROLL
= 1 << 7, ///< Window does not do autoscroll, @see HandleAutoscroll().
239 WF_WHITE_BORDER
= 1 << 8, ///< Window white border counter bit mask.
240 WF_HIGHLIGHTED
= 1 << 9, ///< Window has a widget that has a highlight.
241 WF_CENTERED
= 1 << 10, ///< Window is centered and shall stay centered after ReInit.
243 DECLARE_ENUM_AS_BIT_SET(WindowFlags
)
245 static const int TIMEOUT_DURATION
= 7; ///< The initial timeout value for WF_TIMEOUT.
246 static const int WHITE_BORDER_DURATION
= 3; ///< The initial timeout value for WF_WHITE_BORDER.
249 * Data structure for a window viewport.
250 * A viewport is either following a vehicle (its id in then in #follow_vehicle), or it aims to display a specific
251 * location #dest_scrollpos_x, #dest_scrollpos_y (#follow_vehicle is then #INVALID_VEHICLE).
252 * The actual location being shown is #scrollpos_x, #scrollpos_y.
253 * @see InitializeViewport(), UpdateViewportPosition(), UpdateViewportCoordinates().
255 struct ViewportData
: Viewport
{
256 VehicleID follow_vehicle
; ///< VehicleID to follow if following a vehicle, #INVALID_VEHICLE otherwise.
257 int32_t scrollpos_x
; ///< Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
258 int32_t scrollpos_y
; ///< Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
259 int32_t dest_scrollpos_x
; ///< Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewport).
260 int32_t dest_scrollpos_y
; ///< Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewport).
266 enum TooltipCloseCondition
{
274 * Data structure for an opened window
276 struct Window
: ZeroedMemoryAllocator
{
278 static std::vector
<Window
*> closed_windows
;
281 void InitializeData(WindowNumber window_number
);
282 void InitializePositionSize(int x
, int y
, int min_width
, int min_height
);
283 virtual void FindWindowPlacementAndResize(int def_width
, int def_height
);
285 std::vector
<int> scheduled_invalidation_data
; ///< Data of scheduled OnInvalidateData() calls.
286 bool scheduled_resize
; ///< Set if window has been resized.
288 /* Protected to prevent deletion anywhere outside Window::DeleteClosedWindows(). */
292 Window(WindowDesc
&desc
);
295 * Helper allocation function to disallow something.
296 * Don't allow arrays; arrays of Windows are pointless as you need
297 * to destruct them all at the same time too, which is kinda hard.
298 * @param size the amount of space not to allocate
300 inline void *operator new[](size_t size
) = delete;
302 WindowDesc
&window_desc
; ///< Window description
303 WindowFlags flags
; ///< Window flags
304 WindowClass window_class
; ///< Window class
305 WindowNumber window_number
; ///< Window number within the window class
307 int scale
; ///< Scale of this window -- used to determine how to resize.
309 uint8_t timeout_timer
; ///< Timer value of the WF_TIMEOUT for flags.
310 uint8_t 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 std::map
<WidgetID
, QueryString
*> querystrings
; ///< QueryString associated to WWT_EDITBOX widgets.
324 std::unique_ptr
<NWidgetBase
> nested_root
; ///< Root of the nested tree.
325 WidgetLookup widget_lookup
; ///< Indexed access to the nested widget tree. Do not access directly, use #Window::GetWidget() instead.
326 NWidgetStacked
*shade_select
; ///< Selection widget (#NWID_SELECTION) to use for shading the window. If \c nullptr, window cannot shade.
327 Dimension unshaded_size
; ///< Last known unshaded size (only valid while shaded).
329 WidgetID mouse_capture_widget
; ///< ID of current mouse capture widget (e.g. dragged scrollbar). -1 if no widget has mouse capture.
331 Window
*parent
; ///< Parent window.
332 WindowList::iterator z_position
;
334 template <class NWID
>
335 inline const NWID
*GetWidget(WidgetID widnum
) const;
336 template <class NWID
>
337 inline NWID
*GetWidget(WidgetID widnum
);
339 const Scrollbar
*GetScrollbar(WidgetID widnum
) const;
340 Scrollbar
*GetScrollbar(WidgetID widnum
);
342 const QueryString
*GetQueryString(WidgetID widnum
) const;
343 QueryString
*GetQueryString(WidgetID widnum
);
344 void UpdateQueryStringSize();
346 virtual const struct Textbuf
*GetFocusedTextbuf() const;
347 virtual Point
GetCaretPosition() const;
348 virtual Rect
GetTextBoundingRect(const char *from
, const char *to
) const;
349 virtual ptrdiff_t GetTextCharacterAtPosition(const Point
&pt
) const;
351 void InitNested(WindowNumber number
= 0);
352 void CreateNestedTree();
353 void FinishInitNested(WindowNumber window_number
= 0);
355 template<typename T
, std::enable_if_t
<std::is_base_of
<StrongTypedefBase
, T
>::value
, int> = 0>
356 void FinishInitNested(T number
)
358 this->FinishInitNested(number
.base());
362 * Set the timeout flag of the window and initiate the timer.
364 inline void SetTimeout()
366 this->flags
|= WF_TIMEOUT
;
367 this->timeout_timer
= TIMEOUT_DURATION
;
371 * Set the timeout flag of the window and initiate the timer.
373 inline void SetWhiteBorder()
375 this->flags
|= WF_WHITE_BORDER
;
376 this->white_border_timer
= WHITE_BORDER_DURATION
;
379 void DisableAllWidgetHighlight();
380 void SetWidgetHighlight(WidgetID widget_index
, TextColour highlighted_colour
);
381 bool IsWidgetHighlighted(WidgetID widget_index
) const;
384 * Sets the enabled/disabled status of a widget.
385 * By default, widgets are enabled.
386 * On certain conditions, they have to be disabled.
387 * @param widget_index index of this widget in the window
388 * @param disab_stat status to use ie: disabled = true, enabled = false
390 inline void SetWidgetDisabledState(WidgetID widget_index
, bool disab_stat
)
392 NWidgetCore
*nwid
= this->GetWidget
<NWidgetCore
>(widget_index
);
393 if (nwid
!= nullptr) nwid
->SetDisabled(disab_stat
);
397 * Sets a widget to disabled.
398 * @param widget_index index of this widget in the window
400 inline void DisableWidget(WidgetID widget_index
)
402 SetWidgetDisabledState(widget_index
, true);
406 * Sets a widget to Enabled.
407 * @param widget_index index of this widget in the window
409 inline void EnableWidget(WidgetID widget_index
)
411 SetWidgetDisabledState(widget_index
, false);
415 * Gets the enabled/disabled status of a widget.
416 * @param widget_index index of this widget in the window
417 * @return status of the widget ie: disabled = true, enabled = false
419 inline bool IsWidgetDisabled(WidgetID widget_index
) const
421 return this->GetWidget
<NWidgetCore
>(widget_index
)->IsDisabled();
425 * Check if given widget is focused within this window
426 * @param widget_index : index of the widget in the window to check
427 * @return true if given widget is the focused window in this window
429 inline bool IsWidgetFocused(WidgetID widget_index
) const
431 return this->nested_focus
!= nullptr && this->nested_focus
->index
== widget_index
;
435 * Check if given widget has user input focus. This means that both the window
436 * has focus and that the given widget has focus within the window.
437 * @param widget_index : index of the widget in the window to check
438 * @return true if given widget is the focused window in this window and this window has focus
440 inline bool IsWidgetGloballyFocused(WidgetID widget_index
) const
442 return _focused_window
== this && IsWidgetFocused(widget_index
);
446 * Sets the lowered/raised status of a widget.
447 * @param widget_index index of this widget in the window
448 * @param lowered_stat status to use ie: lowered = true, raised = false
450 inline void SetWidgetLoweredState(WidgetID widget_index
, bool lowered_stat
)
452 this->GetWidget
<NWidgetCore
>(widget_index
)->SetLowered(lowered_stat
);
456 * Invert the lowered/raised status of a widget.
457 * @param widget_index index of this widget in the window
459 inline void ToggleWidgetLoweredState(WidgetID widget_index
)
461 bool lowered_state
= this->GetWidget
<NWidgetCore
>(widget_index
)->IsLowered();
462 this->GetWidget
<NWidgetCore
>(widget_index
)->SetLowered(!lowered_state
);
466 * Marks a widget as lowered.
467 * @param widget_index index of this widget in the window
469 inline void LowerWidget(WidgetID widget_index
)
471 SetWidgetLoweredState(widget_index
, true);
475 * Marks a widget as raised.
476 * @param widget_index index of this widget in the window
478 inline void RaiseWidget(WidgetID widget_index
)
480 SetWidgetLoweredState(widget_index
, false);
484 * Marks a widget as raised and dirty (redraw), when it is marked as lowered.
485 * @param widget_index index of this widget in the window
487 inline void RaiseWidgetWhenLowered(WidgetID widget_index
)
489 if (this->IsWidgetLowered(widget_index
)) {
490 this->RaiseWidget(widget_index
);
491 this->SetWidgetDirty(widget_index
);
496 * Gets the lowered state of a widget.
497 * @param widget_index index of this widget in the window
498 * @return status of the widget ie: lowered = true, raised= false
500 inline bool IsWidgetLowered(WidgetID widget_index
) const
502 return this->GetWidget
<NWidgetCore
>(widget_index
)->IsLowered();
505 void UnfocusFocusedWidget();
506 bool SetFocusedWidget(WidgetID widget_index
);
508 EventState
HandleEditBoxKey(WidgetID wid
, char32_t key
, uint16_t keycode
);
509 virtual void InsertTextString(WidgetID wid
, const char *str
, bool marked
, const char *caret
, const char *insert_location
, const char *replacement_end
);
511 void HandleButtonClick(WidgetID widget
);
512 int GetRowFromWidget(int clickpos
, WidgetID widget
, int padding
, int line_height
= -1) const;
514 void RaiseButtons(bool autoraise
= false);
517 * Sets the enabled/disabled status of a list of widgets.
518 * By default, widgets are enabled.
519 * On certain conditions, they have to be disabled.
520 * @param disab_stat status to use ie: disabled = true, enabled = false
521 * @param widgets list of widgets
523 template<typename
... Args
>
524 void SetWidgetsDisabledState(bool disab_stat
, Args
... widgets
)
526 (SetWidgetDisabledState(widgets
, disab_stat
), ...);
530 * Sets the lowered/raised status of a list of widgets.
531 * @param lowered_stat status to use ie: lowered = true, raised = false
532 * @param widgets list of widgets
534 template<typename
... Args
>
535 void SetWidgetsLoweredState(bool lowered_stat
, Args
... widgets
)
537 (SetWidgetLoweredState(widgets
, lowered_stat
), ...);
541 * Raises the widgets and sets widgets dirty that are lowered.
542 * @param widgets list of widgets
544 template<typename
... Args
>
545 void RaiseWidgetsWhenLowered(Args
... widgets
)
547 (this->RaiseWidgetWhenLowered(widgets
), ...);
550 void SetWidgetDirty(WidgetID widget_index
) const;
552 void DrawWidgets() const;
553 void DrawViewport() const;
554 void DrawSortButtonState(WidgetID widget
, SortButtonState state
) const;
555 static int SortButtonWidth();
557 Window
*FindChildWindow(WindowClass wc
= WC_INVALID
) const;
558 void CloseChildWindows(WindowClass wc
= WC_INVALID
) const;
559 virtual void Close(int data
= 0);
560 static void DeleteClosedWindows();
562 void SetDirty() const;
563 void ReInit(int rx
= 0, int ry
= 0, bool reposition
= false);
565 /** Is window shaded currently? */
566 inline bool IsShaded() const
568 return this->shade_select
!= nullptr && this->shade_select
->shown_plane
== SZSP_HORIZONTAL
;
571 void SetShaded(bool make_shaded
);
573 void ScheduleResize();
574 void ProcessScheduledResize();
575 void InvalidateData(int data
= 0, bool gui_scope
= true);
576 void ProcessScheduledInvalidations();
577 void ProcessHighlightedInvalidations();
579 /*** Event handling ***/
582 * Notification that the nested widget tree gets initialized. The event can be used to perform general computations.
583 * @note #nested_root and/or #widget_lookup (normally accessed via #GetWidget()) may not exist during this call.
585 virtual void OnInit() { }
587 virtual void ApplyDefaults();
590 * Compute the initial position of the window.
591 * @param sm_width Smallest width of the window.
592 * @param sm_height Smallest height of the window.
593 * @param window_number The window number of the new window.
594 * @return Initial position of the top-left corner of the window.
596 virtual Point
OnInitialPosition(int16_t sm_width
, int16_t sm_height
, int window_number
);
599 * The window must be repainted.
600 * @note This method should not change any state, it should only use drawing functions.
602 virtual void OnPaint()
608 * Draw the contents of a nested widget.
609 * @param r Rectangle occupied by the widget.
610 * @param widget Number of the widget to draw.
611 * @note This method may not change any state, it may only use drawing functions.
613 virtual void DrawWidget([[maybe_unused
]] const Rect
&r
, [[maybe_unused
]] WidgetID widget
) const {}
616 * Update size and resize step of a widget in the window.
617 * After retrieval of the minimal size and the resize-steps of a widget, this function is called to allow further refinement,
618 * typically by computing the real maximal size of the content. Afterwards, \a size is taken to be the minimal size of the widget
619 * and \a resize is taken to contain the resize steps. For the convenience of the callee, \a padding contains the amount of
620 * padding between the content and the edge of the widget. This should be added to the returned size.
621 * @param widget Widget number.
622 * @param[in,out] size Size of the widget.
623 * @param padding Recommended amount of space between the widget content and the widget edge.
624 * @param[in,out] fill Fill step of the widget.
625 * @param[in,out] resize Resize step of the widget.
627 virtual void UpdateWidgetSize([[maybe_unused
]] WidgetID widget
, [[maybe_unused
]] Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) {}
630 * Initialize string parameters for a widget.
631 * Calls to this function are made during initialization to measure the size (that is as part of #InitNested()), during drawing,
632 * and while re-initializing the window. Only for widgets that render text initializing is requested.
633 * @param widget Widget number.
635 virtual void SetStringParameters([[maybe_unused
]] WidgetID widget
) const {}
638 * The window has gained focus.
640 virtual void OnFocus();
643 * The window has lost focus.
644 * @param closing True iff the window has lost focus in the process of closing.
646 virtual void OnFocusLost(bool closing
);
649 * A key has been pressed.
650 * @param key the Unicode value of the key.
651 * @param keycode the untranslated key code including shift state.
652 * @return #ES_HANDLED if the key press has been handled and no other
653 * window should receive the event.
655 virtual EventState
OnKeyPress([[maybe_unused
]] char32_t key
, [[maybe_unused
]] uint16_t keycode
) { return ES_NOT_HANDLED
; }
657 virtual EventState
OnHotkey(int hotkey
);
660 * The state of the control key has changed
661 * @return #ES_HANDLED if the change has been handled and no other
662 * window should receive the event.
664 virtual EventState
OnCTRLStateChange() { return ES_NOT_HANDLED
; }
668 * A click with the left mouse button has been made on the window.
669 * @param pt the point inside the window that has been clicked.
670 * @param widget the clicked widget.
671 * @param click_count Number of fast consecutive clicks at same position
673 virtual void OnClick([[maybe_unused
]] Point pt
, [[maybe_unused
]] WidgetID widget
, [[maybe_unused
]] int click_count
) {}
676 * A click with the right mouse button has been made on the window.
677 * @param pt the point inside the window that has been clicked.
678 * @param widget the clicked widget.
679 * @return true if the click was actually handled, i.e. do not show a
680 * tooltip if tooltip-on-right-click is enabled.
682 virtual bool OnRightClick([[maybe_unused
]] Point pt
, [[maybe_unused
]] WidgetID widget
) { return false; }
685 * The mouse is hovering over a widget in the window, perform an action for it.
686 * @param pt The point where the mouse is hovering.
687 * @param widget The widget where the mouse is hovering.
689 virtual void OnHover([[maybe_unused
]] Point pt
, [[maybe_unused
]] WidgetID widget
) {}
692 * Event to display a custom tooltip.
693 * @param pt The point where the mouse is located.
694 * @param widget The widget where the mouse is located.
695 * @return True if the event is handled, false if it is ignored.
697 virtual bool OnTooltip([[maybe_unused
]] Point pt
, [[maybe_unused
]] WidgetID widget
, [[maybe_unused
]] TooltipCloseCondition close_cond
) { return false; }
700 * An 'object' is being dragged at the provided position, highlight the target if possible.
701 * @param pt The point inside the window that the mouse hovers over.
702 * @param widget The widget the mouse hovers over.
704 virtual void OnMouseDrag([[maybe_unused
]] Point pt
, [[maybe_unused
]] WidgetID widget
) {}
707 * A dragged 'object' has been released.
708 * @param pt the point inside the window where the release took place.
709 * @param widget the widget where the release took place.
711 virtual void OnDragDrop([[maybe_unused
]] Point pt
, [[maybe_unused
]] WidgetID widget
) {}
714 * Handle the request for (viewport) scrolling.
715 * @param delta the amount the viewport must be scrolled.
717 virtual void OnScroll([[maybe_unused
]] Point delta
) {}
720 * The mouse is currently moving over the window or has just moved outside
721 * of the window. In the latter case pt is (-1, -1).
722 * @param pt the point inside the window that the mouse hovers over.
723 * @param widget the widget the mouse hovers over.
725 virtual void OnMouseOver([[maybe_unused
]] Point pt
, [[maybe_unused
]] WidgetID widget
) {}
728 * The mouse wheel has been turned.
729 * @param wheel the amount of movement of the mouse wheel.
731 virtual void OnMouseWheel([[maybe_unused
]] int wheel
) {}
735 * Called for every mouse loop run, which is at least once per (game) tick.
737 virtual void OnMouseLoop() {}
740 * Called once per (game) tick.
742 virtual void OnGameTick() {}
745 * Called periodically.
747 virtual void OnRealtimeTick([[maybe_unused
]] uint delta_ms
) {}
750 * Called when this window's timeout has been reached.
752 virtual void OnTimeout() {}
756 * Called after the window got resized.
757 * For nested windows with a viewport, call NWidgetViewport::UpdateViewportCoordinates.
759 virtual void OnResize() {}
762 * A dropdown option associated to this window has been selected.
763 * @param widget the widget (button) that the dropdown is associated with.
764 * @param index the element in the dropdown that is selected.
766 virtual void OnDropdownSelect([[maybe_unused
]] WidgetID widget
, [[maybe_unused
]] int index
) {}
768 virtual void OnDropdownClose(Point pt
, WidgetID widget
, int index
, bool instant_close
);
771 * The text in an editbox has been edited.
772 * @param widget The widget of the editbox.
774 virtual void OnEditboxChanged([[maybe_unused
]] WidgetID widget
) {}
777 * The query window opened from this window has closed.
778 * @param str the new value of the string, \c std::nullopt if the window
779 * was cancelled or an empty string when the default
780 * button was pressed, i.e. \c str->empty().
782 virtual void OnQueryTextFinished([[maybe_unused
]] std::optional
<std::string
> str
) {}
785 * Some data on this window has become invalid.
786 * @param data information about the changed data.
787 * @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.
789 virtual void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) {}
792 * The user clicked some place on the map when a tile highlight mode
794 * @param pt the exact point on the map that has been clicked.
795 * @param tile the tile on the map that has been clicked.
797 virtual void OnPlaceObject([[maybe_unused
]] Point pt
, [[maybe_unused
]] TileIndex tile
) {}
800 * The user clicked on a vehicle while HT_VEHICLE has been set.
801 * @param v clicked vehicle
802 * @return true if the click is handled, false if it is ignored
803 * @pre v->IsPrimaryVehicle() == true
805 virtual bool OnVehicleSelect([[maybe_unused
]] const struct Vehicle
*v
) { return false; }
808 * The user clicked on a vehicle while HT_VEHICLE has been set.
809 * @param v clicked vehicle
810 * @return True if the click is handled, false if it is ignored
811 * @pre v->IsPrimaryVehicle() == true
813 virtual bool OnVehicleSelect([[maybe_unused
]] VehicleList::const_iterator begin
, [[maybe_unused
]] VehicleList::const_iterator end
) { return false; }
816 * The user cancelled a tile highlight mode that has been set.
818 virtual void OnPlaceObjectAbort() {}
822 * The user is dragging over the map when the tile highlight mode
824 * @param select_method the method of selection (allowed directions)
825 * @param select_proc what will be created when the drag is over.
826 * @param pt the exact point on the map where the mouse is.
828 virtual void OnPlaceDrag([[maybe_unused
]] ViewportPlaceMethod select_method
, [[maybe_unused
]] ViewportDragDropSelectionProcess select_proc
, [[maybe_unused
]] Point pt
) {}
831 * The user has dragged over the map when the tile highlight mode
833 * @param select_method the method of selection (allowed directions)
834 * @param select_proc what should be created.
835 * @param pt the exact point on the map where the mouse was released.
836 * @param start_tile the begin tile of the drag.
837 * @param end_tile the end tile of the drag.
839 virtual void OnPlaceMouseUp([[maybe_unused
]] ViewportPlaceMethod select_method
, [[maybe_unused
]] ViewportDragDropSelectionProcess select_proc
, [[maybe_unused
]] Point pt
, [[maybe_unused
]] TileIndex start_tile
, [[maybe_unused
]] TileIndex end_tile
) {}
842 * The user moves over the map when a tile highlight mode has been set
843 * when the special mouse mode has been set to 'PRESIZE' mode. An
844 * example of this is the tile highlight for dock building.
845 * @param pt the exact point on the map where the mouse is.
846 * @param tile the tile on the map where the mouse is.
848 virtual void OnPlacePresize([[maybe_unused
]] Point pt
, [[maybe_unused
]] TileIndex tile
) {}
850 /*** End of the event handling ***/
853 * Is the data related to this window NewGRF inspectable?
854 * @return true iff it is inspectable.
856 virtual bool IsNewGRFInspectable() const { return false; }
859 * Show the NewGRF inspection window. When this function is called it is
860 * up to the window to call and pass the right parameters to the
861 * ShowInspectWindow function.
862 * @pre this->IsNewGRFInspectable()
864 virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
867 * Iterator to iterate all valid Windows
868 * @tparam TtoBack whether we iterate towards the back.
870 template <bool TtoBack
>
871 struct WindowIterator
{
872 typedef Window
*value_type
;
873 typedef value_type
*pointer
;
874 typedef value_type
&reference
;
875 typedef size_t difference_type
;
876 typedef std::forward_iterator_tag iterator_category
;
878 explicit WindowIterator(WindowList::iterator start
) : it(start
)
882 explicit WindowIterator(const Window
*w
) : it(w
->z_position
) {}
884 bool operator==(const WindowIterator
&other
) const { return this->it
== other
.it
; }
885 bool operator!=(const WindowIterator
&other
) const { return !(*this == other
); }
886 Window
* operator*() const { return *this->it
; }
887 WindowIterator
& operator++() { this->Next(); this->Validate(); return *this; }
889 bool IsEnd() const { return this->it
== _z_windows
.end(); }
892 WindowList::iterator it
;
895 while (!this->IsEnd() && *this->it
== nullptr) this->Next();
899 if constexpr (!TtoBack
) {
901 } else if (this->it
== _z_windows
.begin()) {
902 this->it
= _z_windows
.end();
908 using IteratorToFront
= WindowIterator
<false>; //!< Iterate in Z order towards front.
909 using IteratorToBack
= WindowIterator
<true>; //!< Iterate in Z order towards back.
912 * Iterable ensemble of all valid Windows
913 * @tparam Tfront Wether we iterate from front
915 template <bool Tfront
>
918 WindowIterator
<Tfront
> begin()
920 if constexpr (Tfront
) {
921 auto back
= _z_windows
.end();
922 if (back
!= _z_windows
.begin()) --back
;
923 return WindowIterator
<Tfront
>(back
);
925 return WindowIterator
<Tfront
>(_z_windows
.begin());
928 WindowIterator
<Tfront
> end() { return WindowIterator
<Tfront
>(_z_windows
.end()); }
930 using Iterate
= AllWindows
<false>; //!< Iterate all windows in whatever order is easiest.
931 using IterateFromBack
= AllWindows
<false>; //!< Iterate all windows in Z order from back to front.
932 using IterateFromFront
= AllWindows
<true>; //!< Iterate all windows in Z order from front to back.
936 * Generic helper function that checks if all elements of the range are equal with respect to the given predicate.
937 * @param begin The start of the range.
938 * @param end The end of the range.
939 * @param pred The predicate to use.
940 * @return True if all elements are equal, false otherwise.
942 template <class It
, class Pred
>
943 inline bool AllEqual(It begin
, It end
, Pred pred
)
945 return std::adjacent_find(begin
, end
, std::not_fn(pred
)) == end
;
949 * Get the nested widget with number \a widnum from the nested widget tree.
950 * @tparam NWID Type of the nested widget.
951 * @param widnum Widget number of the widget to retrieve.
952 * @return The requested widget if it is instantiated, \c nullptr otherwise.
954 template <class NWID
>
955 inline NWID
*Window::GetWidget(WidgetID widnum
)
957 auto it
= this->widget_lookup
.find(widnum
);
958 if (it
== std::end(this->widget_lookup
)) return nullptr;
959 NWID
*nwid
= dynamic_cast<NWID
*>(it
->second
);
960 assert(nwid
!= nullptr);
964 /** Specialized case of #Window::GetWidget for the nested widget base class. */
966 inline const NWidgetBase
*Window::GetWidget
<NWidgetBase
>(WidgetID widnum
) const
968 auto it
= this->widget_lookup
.find(widnum
);
969 if (it
== std::end(this->widget_lookup
)) return nullptr;
974 * Get the nested widget with number \a widnum from the nested widget tree.
975 * @tparam NWID Type of the nested widget.
976 * @param widnum Widget number of the widget to retrieve.
977 * @return The requested widget if it is instantiated, \c nullptr otherwise.
979 template <class NWID
>
980 inline const NWID
*Window::GetWidget(WidgetID widnum
) const
982 return const_cast<Window
*>(this)->GetWidget
<NWID
>(widnum
);
987 * Base class for windows opened from a toolbar.
989 class PickerWindowBase
: public Window
{
992 PickerWindowBase(WindowDesc
&desc
, Window
*parent
) : Window(desc
)
994 this->parent
= parent
;
997 void Close([[maybe_unused
]] int data
= 0) override
;
1000 Window
*BringWindowToFrontById(WindowClass cls
, WindowNumber number
);
1001 Window
*FindWindowFromPt(int x
, int y
);
1003 template<typename T
, std::enable_if_t
<std::is_base_of
<StrongTypedefBase
, T
>::value
, int> = 0>
1004 Window
*BringWindowToFrontById(WindowClass cls
, T number
)
1006 return BringWindowToFrontById(cls
, number
.base());
1010 * Open a new window.
1011 * @tparam Wcls %Window class to use if the window does not exist.
1012 * @param desc The pointer to the WindowDesc to be created
1013 * @param window_number the window number of the new window
1014 * @param return_existing If set, also return the window if it already existed.
1015 * @return %Window pointer of the newly created window, or the existing one if \a return_existing is set, or \c nullptr.
1017 template <typename Wcls
>
1018 Wcls
*AllocateWindowDescFront(WindowDesc
&desc
, int window_number
, bool return_existing
= false)
1020 Wcls
*w
= static_cast<Wcls
*>(BringWindowToFrontById(desc
.cls
, window_number
));
1021 if (w
!= nullptr) return return_existing
? w
: nullptr;
1022 return new Wcls(desc
, window_number
);
1025 void RelocateAllWindows(int neww
, int newh
);
1027 void GuiShowTooltips(Window
*parent
, StringID str
, TooltipCloseCondition close_tooltip
, uint paramcount
= 0);
1030 WidgetID
GetWidgetFromPos(const Window
*w
, int x
, int y
);
1032 extern Point _cursorpos_drag_start
;
1034 extern int _scrollbar_start_pos
;
1035 extern int _scrollbar_size
;
1036 extern uint8_t _scroller_click_timeout
;
1038 extern bool _scrolling_viewport
;
1039 extern bool _mouse_hovering
;
1042 enum SpecialMouseMode
{
1043 WSM_NONE
, ///< No special mouse mode.
1044 WSM_DRAGDROP
, ///< Drag&drop an object.
1045 WSM_SIZING
, ///< Sizing mode.
1046 WSM_PRESIZE
, ///< Presizing mode (docks, tunnels).
1047 WSM_DRAGGING
, ///< Dragging mode (trees).
1049 extern SpecialMouseMode _special_mouse_mode
;
1051 void SetFocusedWindow(Window
*w
);
1053 void ScrollbarClickHandler(Window
*w
, NWidgetCore
*nw
, int x
, int y
);
1055 #endif /* WINDOW_GUI_H */