4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @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 "station_base.h"
21 #include "core/smallvec_type.hpp"
22 #include "core/smallmap_type.hpp"
23 #include "string_type.h"
24 #include "station_type.h"
27 * Flags to describe the look of the frame
31 FR_TRANSPARENT
= 1 << 0, ///< Makes the background transparent if set
32 FR_BORDERONLY
= 1 << 4, ///< Draw border only, no background
33 FR_LOWERED
= 1 << 5, ///< If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
34 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)
37 DECLARE_ENUM_AS_BIT_SET(FrameFlags
)
39 /** Distances used in drawing widgets. */
40 enum WidgetDrawDistances
{
42 WD_IMGBTN_LEFT
= 1, ///< Left offset of the image in the button.
43 WD_IMGBTN_RIGHT
= 2, ///< Right offset of the image in the button.
44 WD_IMGBTN_TOP
= 1, ///< Top offset of image in the button.
45 WD_IMGBTN_BOTTOM
= 2, ///< Bottom offset of image in the button.
48 WD_INSET_LEFT
= 2, ///< Left offset of string.
49 WD_INSET_RIGHT
= 2, ///< Right offset of string.
50 WD_INSET_TOP
= 1, ///< Top offset of string.
52 WD_SCROLLBAR_LEFT
= 2, ///< Left offset of scrollbar.
53 WD_SCROLLBAR_RIGHT
= 2, ///< Right offset of scrollbar.
54 WD_SCROLLBAR_TOP
= 2, ///< Top offset of scrollbar.
55 WD_SCROLLBAR_BOTTOM
= 2, ///< Bottom offset of scrollbar.
57 /* Size of the pure frame bevel without any padding. */
58 WD_BEVEL_LEFT
= 1, ///< Width of left bevel border.
59 WD_BEVEL_RIGHT
= 1, ///< Width of right bevel border.
60 WD_BEVEL_TOP
= 1, ///< Height of top bevel border.
61 WD_BEVEL_BOTTOM
= 1, ///< Height of bottom bevel border.
63 /* FrameRect widgets, all text buttons, panel, editbox */
64 WD_FRAMERECT_LEFT
= 2, ///< Offset at left to draw the frame rectangular area
65 WD_FRAMERECT_RIGHT
= 2, ///< Offset at right to draw the frame rectangular area
66 WD_FRAMERECT_TOP
= 1, ///< Offset at top to draw the frame rectangular area
67 WD_FRAMERECT_BOTTOM
= 1, ///< Offset at bottom to draw the frame rectangular area
69 /* Extra space at top/bottom of text panels */
70 WD_TEXTPANEL_TOP
= 6, ///< Offset at top to draw above the text
71 WD_TEXTPANEL_BOTTOM
= 6, ///< Offset at bottom to draw below the text
74 WD_FRAMETEXT_LEFT
= 6, ///< Left offset of the text of the frame.
75 WD_FRAMETEXT_RIGHT
= 6, ///< Right offset of the text of the frame.
76 WD_FRAMETEXT_TOP
= 6, ///< Top offset of the text of the frame
77 WD_FRAMETEXT_BOTTOM
= 6, ///< Bottom offset of the text of the frame
80 WD_MATRIX_LEFT
= 2, ///< Offset at left of a matrix cell.
81 WD_MATRIX_RIGHT
= 2, ///< Offset at right of a matrix cell.
82 WD_MATRIX_TOP
= 3, ///< Offset at top of a matrix cell.
83 WD_MATRIX_BOTTOM
= 1, ///< Offset at bottom of a matrix cell.
86 WD_SHADEBOX_WIDTH
= 12, ///< Width of a standard shade box widget.
87 WD_SHADEBOX_LEFT
= 2, ///< Left offset of shade sprite.
88 WD_SHADEBOX_RIGHT
= 2, ///< Right offset of shade sprite.
89 WD_SHADEBOX_TOP
= 3, ///< Top offset of shade sprite.
90 WD_SHADEBOX_BOTTOM
= 3, ///< Bottom offset of shade sprite.
93 WD_STICKYBOX_WIDTH
= 12, ///< Width of a standard sticky box widget.
94 WD_STICKYBOX_LEFT
= 2, ///< Left offset of sticky sprite.
95 WD_STICKYBOX_RIGHT
= 2, ///< Right offset of sticky sprite.
96 WD_STICKYBOX_TOP
= 3, ///< Top offset of sticky sprite.
97 WD_STICKYBOX_BOTTOM
= 3, ///< Bottom offset of sticky sprite.
100 WD_DEBUGBOX_WIDTH
= 12, ///< Width of a standard debug box widget.
101 WD_DEBUGBOX_LEFT
= 2, ///< Left offset of debug sprite.
102 WD_DEBUGBOX_RIGHT
= 2, ///< Right offset of debug sprite.
103 WD_DEBUGBOX_TOP
= 3, ///< Top offset of debug sprite.
104 WD_DEBUGBOX_BOTTOM
= 3, ///< Bottom offset of debug sprite.
107 WD_DEFSIZEBOX_WIDTH
= 12, ///< Width of a standard defsize box widget.
108 WD_DEFSIZEBOX_LEFT
= 2, ///< Left offset of defsize sprite.
109 WD_DEFSIZEBOX_RIGHT
= 2, ///< Right offset of defsize sprite.
110 WD_DEFSIZEBOX_TOP
= 3, ///< Top offset of defsize sprite.
111 WD_DEFSIZEBOX_BOTTOM
= 3, ///< Bottom offset of defsize sprite.
114 WD_RESIZEBOX_WIDTH
= 12, ///< Width of a resize box widget.
115 WD_RESIZEBOX_LEFT
= 3, ///< Left offset of resize sprite.
116 WD_RESIZEBOX_RIGHT
= 2, ///< Right offset of resize sprite.
117 WD_RESIZEBOX_TOP
= 3, ///< Top offset of resize sprite.
118 WD_RESIZEBOX_BOTTOM
= 2, ///< Bottom offset of resize sprite.
121 WD_CLOSEBOX_WIDTH
= 11, ///< Width of a close box widget.
122 WD_CLOSEBOX_LEFT
= 2, ///< Left offset of closebox string.
123 WD_CLOSEBOX_RIGHT
= 1, ///< Right offset of closebox string.
124 WD_CLOSEBOX_TOP
= 2, ///< Top offset of closebox string.
125 WD_CLOSEBOX_BOTTOM
= 2, ///< Bottom offset of closebox string.
128 WD_CAPTION_HEIGHT
= 14, ///< Height of a title bar.
129 WD_CAPTIONTEXT_LEFT
= 2, ///< Offset of the caption text at the left.
130 WD_CAPTIONTEXT_RIGHT
= 2, ///< Offset of the caption text at the right.
131 WD_CAPTIONTEXT_TOP
= 2, ///< Offset of the caption text at the top.
132 WD_CAPTIONTEXT_BOTTOM
= 2, ///< Offset of the caption text at the bottom.
134 /* Dropdown widget. */
135 WD_DROPDOWN_HEIGHT
= 12, ///< Height of a drop down widget.
136 WD_DROPDOWNTEXT_LEFT
= 2, ///< Left offset of the dropdown widget string.
137 WD_DROPDOWNTEXT_RIGHT
= 2, ///< Right offset of the dropdown widget string.
138 WD_DROPDOWNTEXT_TOP
= 1, ///< Top offset of the dropdown widget string.
139 WD_DROPDOWNTEXT_BOTTOM
= 1, ///< Bottom offset of the dropdown widget string.
141 WD_PAR_VSEP_NORMAL
= 2, ///< Normal amount of vertical space between two paragraphs of text.
142 WD_PAR_VSEP_WIDE
= 8, ///< Large amount of vertical space between two paragraphs of text.
146 void DrawFrameRect(int left
, int top
, int right
, int bottom
, Colours colour
, FrameFlags flags
);
147 void DrawCaption(const Rect
&r
, Colours colour
, Owner owner
, StringID str
);
150 extern Window
*_z_front_window
;
151 extern Window
*_z_back_window
;
152 extern Window
*_focused_window
;
155 /** How do we the window to be placed? */
156 enum WindowPosition
{
157 WDP_MANUAL
, ///< Manually align the window (so no automatic location finding)
158 WDP_AUTO
, ///< Find a place automatically
159 WDP_CENTER
, ///< Center the window
160 WDP_ALIGN_TOOLBAR
, ///< Align toward the toolbar
163 Point
GetToolbarAlignedWindowPosition(int window_width
);
168 * High level window description
172 WindowDesc(WindowPosition default_pos
, const char *ini_key
, int16 def_width_trad
, int16 def_height_trad
,
173 WindowClass window_class
, WindowClass parent_class
, uint32 flags
,
174 const NWidgetPart
*nwid_parts
, int16 nwid_length
, HotkeyList
*hotkeys
= NULL
);
178 WindowPosition default_pos
; ///< Preferred position of the window. @see WindowPosition()
179 WindowClass cls
; ///< Class of the window, @see WindowClass.
180 WindowClass parent_cls
; ///< Class of the parent window. @see WindowClass
181 const char *ini_key
; ///< Key to store window defaults in openttd.cfg. \c NULL if nothing shall be stored.
182 uint32 flags
; ///< Flags. @see WindowDefaultFlag
183 const NWidgetPart
*nwid_parts
; ///< Nested widget parts describing the window.
184 int16 nwid_length
; ///< Length of the #nwid_parts array.
185 HotkeyList
*hotkeys
; ///< Hotkeys for the window.
187 bool pref_sticky
; ///< Preferred stickyness.
188 int16 pref_width
; ///< User-preferred width of the window. Zero if unset.
189 int16 pref_height
; ///< User-preferred height of the window. Zero if unset.
191 int16
GetDefaultWidth() const;
192 int16
GetDefaultHeight() const;
194 static void LoadFromConfig();
195 static void SaveToConfig();
198 int16 default_width_trad
; ///< Preferred initial width of the window (pixels at 1x zoom).
199 int16 default_height_trad
; ///< Preferred initial height of the window (pixels at 1x zoom).
202 * Dummy private copy constructor to prevent compilers from
203 * copying the structure, which fails due to _window_descs.
205 WindowDesc(const WindowDesc
&other
);
209 * Window default widget/window handling flags
211 enum WindowDefaultFlag
{
212 WDF_CONSTRUCTION
= 1 << 0, ///< This window is used for construction; close it whenever changing company.
213 WDF_MODAL
= 1 << 1, ///< The window is a modal child of some other window, meaning the parent is 'inactive'
214 WDF_NO_FOCUS
= 1 << 2, ///< This window won't get focus/make any other window lose focus when click
218 * Data structure for resizing a window
221 uint step_width
; ///< Step-size of width resize changes
222 uint step_height
; ///< Step-size of height resize changes
225 /** State of a sort direction button. */
226 enum SortButtonState
{
227 SBS_OFF
, ///< Do not sort (with this button).
228 SBS_DOWN
, ///< Sort ascending.
229 SBS_UP
, ///< Sort descending.
236 WF_TIMEOUT
= 1 << 0, ///< Window timeout counter.
238 WF_DRAGGING
= 1 << 3, ///< Window is being dragged.
239 WF_SIZING_RIGHT
= 1 << 4, ///< Window is being resized towards the right.
240 WF_SIZING_LEFT
= 1 << 5, ///< Window is being resized towards the left.
241 WF_SIZING
= WF_SIZING_RIGHT
| WF_SIZING_LEFT
, ///< Window is being resized.
242 WF_STICKY
= 1 << 6, ///< Window is made sticky by user
243 WF_DISABLE_VP_SCROLL
= 1 << 7, ///< Window does not do autoscroll, @see HandleAutoscroll().
244 WF_WHITE_BORDER
= 1 << 8, ///< Window white border counter bit mask.
245 WF_HIGHLIGHTED
= 1 << 9, ///< Window has a widget that has a highlight.
246 WF_CENTERED
= 1 << 10, ///< Window is centered and shall stay centered after ReInit.
248 DECLARE_ENUM_AS_BIT_SET(WindowFlags
)
250 static const int TIMEOUT_DURATION
= 7; ///< The initial timeout value for WF_TIMEOUT.
251 static const int WHITE_BORDER_DURATION
= 3; ///< The initial timeout value for WF_WHITE_BORDER.
254 * Data structure for a window viewport.
255 * A viewport is either following a vehicle (its id in then in #follow_vehicle), or it aims to display a specific
256 * location #dest_scrollpos_x, #dest_scrollpos_y (#follow_vehicle is then #INVALID_VEHICLE).
257 * The actual location being shown is #scrollpos_x, #scrollpos_y.
258 * @see InitializeViewport(), UpdateViewportPosition(), UpdateViewportCoordinates().
260 struct ViewportData
: ViewPort
{
261 VehicleID follow_vehicle
; ///< VehicleID to follow if following a vehicle, #INVALID_VEHICLE otherwise.
262 int32 scrollpos_x
; ///< Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
263 int32 scrollpos_y
; ///< Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
264 int32 dest_scrollpos_x
; ///< Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewport).
265 int32 dest_scrollpos_y
; ///< Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewport).
271 enum TooltipCloseCondition
{
276 void GuiShowTooltips(Window
*parent
, StringID str
, TooltipCloseCondition close_tooltip
= TCC_HOVER
);
277 void GuiShowTooltips(Window
*parent
, const char *str
, TooltipCloseCondition close_tooltip
= TCC_HOVER
);
278 void GuiShowTooltipsForTile(Window
*parent
, TileIndex tile
, TooltipCloseCondition close_tooltip
= TCC_HOVER
);
279 void GetTileTooltipsForStation(StationID sid
, char *buff
, const char *last
);
283 * Data structure for an opened window
285 struct Window
: ZeroedMemoryAllocator
{
287 void InitializeData(WindowNumber window_number
);
288 void InitializePositionSize(int x
, int y
, int min_width
, int min_height
);
289 virtual void FindWindowPlacementAndResize(int def_width
, int def_height
);
291 SmallVector
<int, 4> scheduled_invalidation_data
; ///< Data of scheduled OnInvalidateData() calls.
294 Window(WindowDesc
*desc
);
299 * Helper allocation function to disallow something.
300 * Don't allow arrays; arrays of Windows are pointless as you need
301 * to destruct them all at the same time too, which is kinda hard.
302 * @param size the amount of space not to allocate
304 inline void *operator new[](size_t size
)
310 * Helper allocation function to disallow something.
311 * Don't free the window directly; it corrupts the linked list when iterating
312 * @param ptr the pointer not to free
314 inline void operator delete(void *ptr
)
318 WindowDesc
*window_desc
; ///< Window description
319 WindowFlags flags
; ///< Window flags
320 WindowClass window_class
; ///< Window class
321 WindowNumber window_number
; ///< Window number within the window class
323 uint8 timeout_timer
; ///< Timer value of the WF_TIMEOUT for flags.
324 uint8 white_border_timer
; ///< Timer value of the WF_WHITE_BORDER for flags.
326 int left
; ///< x position of left edge of the window
327 int top
; ///< y position of top edge of the window
328 int width
; ///< width of the window (number of pixels to the right in x direction)
329 int height
; ///< Height of the window (number of pixels down in y direction)
331 ResizeInfo resize
; ///< Resize information
333 Owner owner
; ///< The owner of the content shown in this window. Company colour is acquired from this variable.
335 ViewportData
*viewport
; ///< Pointer to viewport data, if present.
336 const NWidgetCore
*nested_focus
; ///< Currently focused nested widget, or \c NULL if no nested widget has focus.
337 SmallMap
<int, QueryString
*> querystrings
; ///< QueryString associated to WWT_EDITBOX widgets.
338 NWidgetBase
*nested_root
; ///< Root of the nested tree.
339 NWidgetBase
**nested_array
; ///< Array of pointers into the tree. Do not access directly, use #Window::GetWidget() instead.
340 uint nested_array_size
; ///< Size of the nested array.
341 NWidgetStacked
*shade_select
; ///< Selection widget (#NWID_SELECTION) to use for shading the window. If \c NULL, window cannot shade.
342 Dimension unshaded_size
; ///< Last known unshaded size (only valid while shaded).
344 int scrolling_scrollbar
; ///< Widgetindex of just being dragged scrollbar. -1 if none is active.
346 Window
*parent
; ///< Parent window.
347 Window
*z_front
; ///< The window in front of us in z-order.
348 Window
*z_back
; ///< The window behind us in z-order.
350 template <class NWID
>
351 inline const NWID
*GetWidget(uint widnum
) const;
352 template <class NWID
>
353 inline NWID
*GetWidget(uint widnum
);
355 const Scrollbar
*GetScrollbar(uint widnum
) const;
356 Scrollbar
*GetScrollbar(uint widnum
);
358 const QueryString
*GetQueryString(uint widnum
) const;
359 QueryString
*GetQueryString(uint widnum
);
361 virtual const char *GetFocusedText() const;
362 virtual const char *GetCaret() const;
363 virtual const char *GetMarkedText(size_t *length
) const;
364 virtual Point
GetCaretPosition() const;
365 virtual Rect
GetTextBoundingRect(const char *from
, const char *to
) const;
366 virtual const char *GetTextCharacterAtPosition(const Point
&pt
) const;
368 void InitNested(WindowNumber number
= 0);
369 void CreateNestedTree(bool fill_nested
= true);
370 void FinishInitNested(WindowNumber window_number
= 0);
373 * Set the timeout flag of the window and initiate the timer.
375 inline void SetTimeout()
377 this->flags
|= WF_TIMEOUT
;
378 this->timeout_timer
= TIMEOUT_DURATION
;
382 * Set the timeout flag of the window and initiate the timer.
384 inline void SetWhiteBorder()
386 this->flags
|= WF_WHITE_BORDER
;
387 this->white_border_timer
= WHITE_BORDER_DURATION
;
390 void DisableAllWidgetHighlight();
391 void SetWidgetHighlight(byte widget_index
, TextColour highlighted_colour
);
392 bool IsWidgetHighlighted(byte widget_index
) const;
395 * Sets the enabled/disabled status of a widget.
396 * By default, widgets are enabled.
397 * On certain conditions, they have to be disabled.
398 * @param widget_index index of this widget in the window
399 * @param disab_stat status to use ie: disabled = true, enabled = false
401 inline void SetWidgetDisabledState(byte widget_index
, bool disab_stat
)
403 assert(widget_index
< this->nested_array_size
);
404 if (this->nested_array
[widget_index
] != NULL
) this->GetWidget
<NWidgetCore
>(widget_index
)->SetDisabled(disab_stat
);
408 * Sets a widget to disabled.
409 * @param widget_index index of this widget in the window
411 inline void DisableWidget(byte widget_index
)
413 SetWidgetDisabledState(widget_index
, true);
417 * Sets a widget to Enabled.
418 * @param widget_index index of this widget in the window
420 inline void EnableWidget(byte widget_index
)
422 SetWidgetDisabledState(widget_index
, false);
426 * Gets the enabled/disabled status of a widget.
427 * @param widget_index index of this widget in the window
428 * @return status of the widget ie: disabled = true, enabled = false
430 inline bool IsWidgetDisabled(byte widget_index
) const
432 assert(widget_index
< this->nested_array_size
);
433 return this->GetWidget
<NWidgetCore
>(widget_index
)->IsDisabled();
437 * Check if given widget is focused within this window
438 * @param widget_index : index of the widget in the window to check
439 * @return true if given widget is the focused window in this window
441 inline bool IsWidgetFocused(byte widget_index
) const
443 return this->nested_focus
!= NULL
&& this->nested_focus
->index
== widget_index
;
447 * Check if given widget has user input focus. This means that both the window
448 * has focus and that the given widget has focus within the window.
449 * @param widget_index : index of the widget in the window to check
450 * @return true if given widget is the focused window in this window and this window has focus
452 inline bool IsWidgetGloballyFocused(byte widget_index
) const
454 return _focused_window
== this && IsWidgetFocused(widget_index
);
458 * Sets the lowered/raised status of a widget.
459 * @param widget_index index of this widget in the window
460 * @param lowered_stat status to use ie: lowered = true, raised = false
462 inline void SetWidgetLoweredState(byte widget_index
, bool lowered_stat
)
464 assert(widget_index
< this->nested_array_size
);
465 this->GetWidget
<NWidgetCore
>(widget_index
)->SetLowered(lowered_stat
);
469 * Invert the lowered/raised status of a widget.
470 * @param widget_index index of this widget in the window
472 inline void ToggleWidgetLoweredState(byte widget_index
)
474 assert(widget_index
< this->nested_array_size
);
475 bool lowered_state
= this->GetWidget
<NWidgetCore
>(widget_index
)->IsLowered();
476 this->GetWidget
<NWidgetCore
>(widget_index
)->SetLowered(!lowered_state
);
480 * Marks a widget as lowered.
481 * @param widget_index index of this widget in the window
483 inline void LowerWidget(byte widget_index
)
485 SetWidgetLoweredState(widget_index
, true);
489 * Marks a widget as raised.
490 * @param widget_index index of this widget in the window
492 inline void RaiseWidget(byte widget_index
)
494 SetWidgetLoweredState(widget_index
, false);
498 * Gets the lowered state of a widget.
499 * @param widget_index index of this widget in the window
500 * @return status of the widget ie: lowered = true, raised= false
502 inline bool IsWidgetLowered(byte widget_index
) const
504 assert(widget_index
< this->nested_array_size
);
505 return this->GetWidget
<NWidgetCore
>(widget_index
)->IsLowered();
508 void UnfocusFocusedWidget();
509 bool SetFocusedWidget(int widget_index
);
511 EventState
HandleEditBoxKey(int wid
, WChar key
, uint16 keycode
);
512 virtual void InsertTextString(int wid
, const char *str
, bool marked
, const char *caret
, const char *insert_location
, const char *replacement_end
);
514 void HandleButtonClick(byte widget
);
515 int GetRowFromWidget(int clickpos
, int widget
, int padding
, int line_height
= -1) const;
517 void RaiseButtons(bool autoraise
= false);
518 void CDECL
SetWidgetsDisabledState(bool disab_stat
, int widgets
, ...);
519 void CDECL
SetWidgetsLoweredState(bool lowered_stat
, int widgets
, ...);
520 void SetWidgetDirty(byte widget_index
) const;
522 void DrawWidgets() const;
523 void DrawViewport() const;
524 void DrawSortButtonState(int widget
, SortButtonState state
) const;
525 static int SortButtonWidth();
527 void DeleteChildWindows(WindowClass wc
= WC_INVALID
) const;
529 void SetDirty() const;
530 void ReInit(int rx
= 0, int ry
= 0);
532 /** Is window shaded currently? */
533 inline bool IsShaded() const
535 return this->shade_select
!= NULL
&& this->shade_select
->shown_plane
== SZSP_HORIZONTAL
;
538 void SetShaded(bool make_shaded
);
540 void InvalidateData(int data
= 0, bool gui_scope
= true);
541 void ProcessScheduledInvalidations();
542 void ProcessHighlightedInvalidations();
544 /*** Event handling ***/
547 * Notification that the nested widget tree gets initialized. The event can be used to perform general computations.
548 * @note #nested_root and/or #nested_array (normally accessed via #GetWidget()) may not exist during this call.
550 virtual void OnInit() { }
552 virtual void ApplyDefaults();
555 * Compute the initial position of the window.
556 * @param sm_width Smallest width of the window.
557 * @param sm_height Smallest height of the window.
558 * @param window_number The window number of the new window.
559 * @return Initial position of the top-left corner of the window.
561 virtual Point
OnInitialPosition(int16 sm_width
, int16 sm_height
, int window_number
);
564 * The window must be repainted.
565 * @note This method should not change any state, it should only use drawing functions.
567 virtual void OnPaint()
573 * Draw the contents of a nested widget.
574 * @param r Rectangle occupied by the widget.
575 * @param widget Number of the widget to draw.
576 * @note This method may not change any state, it may only use drawing functions.
578 virtual void DrawWidget(const Rect
&r
, int widget
) const {}
581 * Update size and resize step of a widget in the window.
582 * After retrieval of the minimal size and the resize-steps of a widget, this function is called to allow further refinement,
583 * typically by computing the real maximal size of the content. Afterwards, \a size is taken to be the minimal size of the widget
584 * and \a resize is taken to contain the resize steps. For the convenience of the callee, \a padding contains the amount of
585 * padding between the content and the edge of the widget. This should be added to the returned size.
586 * @param widget Widget number.
587 * @param size Size of the widget.
588 * @param padding Recommended amount of space between the widget content and the widget edge.
589 * @param fill Fill step of the widget.
590 * @param resize Resize step of the widget.
592 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) {}
595 * Initialize string parameters for a widget.
596 * Calls to this function are made during initialization to measure the size (that is as part of #InitNested()), during drawing,
597 * and while re-initializing the window. Only for widgets that render text initializing is requested.
598 * @param widget Widget number.
600 virtual void SetStringParameters(int widget
) const {}
603 * Called when window gains focus
604 * @param previously_focused_window The window that lost the focus.
606 virtual void OnFocus(Window
*previously_focused_window
) {}
608 virtual void OnFocusLost(Window
*newly_focused_window
);
611 * A key has been pressed.
612 * @param key the Unicode value of the key.
613 * @param keycode the untranslated key code including shift state.
614 * @return #ES_HANDLED if the key press has been handled and no other
615 * window should receive the event.
617 virtual EventState
OnKeyPress(WChar key
, uint16 keycode
) { return ES_NOT_HANDLED
; }
619 virtual EventState
OnHotkey(int hotkey
);
622 * The state of the control key has changed
623 * @return #ES_HANDLED if the change has been handled and no other
624 * window should receive the event.
626 virtual EventState
OnCTRLStateChange() { return ES_NOT_HANDLED
; }
630 * A click with the left mouse button has been made on the window.
631 * @param pt the point inside the window that has been clicked.
632 * @param widget the clicked widget.
633 * @param click_count Number of fast consecutive clicks at same position
635 virtual void OnClick(Point pt
, int widget
, int click_count
) {}
638 * A click with the right mouse button has been made on the window.
639 * @param pt the point inside the window that has been clicked.
640 * @param widget the clicked widget.
641 * @return true if the click was actually handled, i.e. do not show a
642 * tooltip if tooltip-on-right-click is enabled.
644 virtual bool OnRightClick(Point pt
, int widget
) { return false; }
647 * The mouse is hovering over a widget in the window, perform an action for it, like opening a custom tooltip.
648 * @param pt The point where the mouse is hovering.
649 * @param widget The widget where the mouse is hovering.
651 //virtual void OnHover(Point pt, int widget) {}
654 * An 'object' is being dragged at the provided position, highlight the target if possible.
655 * @param pt The point inside the window that the mouse hovers over.
656 * @param widget The widget the mouse hovers over.
658 virtual void OnMouseDrag(Point pt
, int widget
) {}
661 * A dragged 'object' has been released.
662 * @param pt the point inside the window where the release took place.
663 * @param widget the widget where the release took place.
665 virtual void OnDragDrop(Point pt
, int widget
) {}
668 * Handle the request for (viewport) scrolling.
669 * @param delta the amount the viewport must be scrolled.
671 virtual void OnScroll(Point delta
) {}
674 * The mouse is currently moving over the window or has just moved outside
675 * of the window. In the latter case pt is (-1, -1).
676 * @param pt the point inside the window that the mouse hovers over.
677 * @param widget the widget the mouse hovers over.
679 virtual void OnMouseOver(Point pt
, int widget
) {}
682 * The mouse wheel has been turned.
683 * @param wheel the amount of movement of the mouse wheel.
685 virtual void OnMouseWheel(int wheel
) {}
689 * Called for every mouse loop run, which is at least once per (game) tick.
691 virtual void OnMouseLoop() {}
694 * Called once per (game) tick.
696 virtual void OnTick() {}
699 * Called once every 100 (game) ticks.
701 virtual void OnHundredthTick() {}
704 * Called when this window's timeout has been reached.
706 virtual void OnTimeout() {}
710 * Called after the window got resized.
711 * For nested windows with a viewport, call NWidgetViewport::UpdateViewportCoordinates.
713 virtual void OnResize() {}
716 * A dropdown option associated to this window has been selected.
717 * @param widget the widget (button) that the dropdown is associated with.
718 * @param index the element in the dropdown that is selected.
720 virtual void OnDropdownSelect(int widget
, int index
) {}
722 virtual void OnDropdownClose(Point pt
, int widget
, int index
, bool instant_close
);
725 * A tooltip is about to be shown (e.g. the mouse is hovering). Perform
726 * an action for it, default is to show the tip of the widget.
728 * @param pt The point where the mouse is hovering.
729 * @param widget The widget where the mouse is hovering.
730 * @param close_cond How to close tooltips, depands on the way the event was initiated.
732 * @see GuiShowTooltips
734 virtual void OnToolTip(Point pt
, int widget
, TooltipCloseCondition close_cond
)
736 NWidgetCore
*wid
= this->GetWidget
<NWidgetCore
>(widget
);
737 if (wid
!= NULL
) GuiShowTooltips(this, wid
->tool_tip
, close_cond
);
741 * The text in an editbox has been edited.
742 * @param widget The widget of the editbox.
744 virtual void OnEditboxChanged(int widget
) {}
747 * The query window opened from this window has closed.
748 * @param str the new value of the string, NULL if the window
749 * was cancelled or an empty string when the default
750 * button was pressed, i.e. StrEmpty(str).
752 virtual void OnQueryTextFinished(char *str
) {}
755 * Some data on this window has become invalid.
756 * @param data information about the changed data.
757 * @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.
759 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true) {}
762 * The user clicked some place on the map when a tile highlight mode
764 * @param pt the exact point on the map that has been clicked.
765 * @param tile the tile on the map that has been clicked.
767 virtual void OnPlaceObject(Point pt
, TileIndex tile
) {}
770 * The user clicked on a vehicle while HT_VEHICLE has been set.
771 * @param v clicked vehicle. It is guaranteed to be v->IsPrimaryVehicle() == true
772 * @return True if the click is handled, false if it is ignored.
774 virtual bool OnVehicleSelect(const struct Vehicle
*v
) { return false; }
777 * The user cancelled a tile highlight mode that has been set.
779 virtual void OnPlaceObjectAbort() {}
783 * The user is dragging over the map when the tile highlight mode
785 * @param select_method the method of selection (allowed directions)
786 * @param select_proc what will be created when the drag is over.
787 * @param pt the exact point on the map where the mouse is.
789 virtual void OnPlaceDrag(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
) {}
792 * The user has dragged over the map when the tile highlight mode
794 * @param select_method the method of selection (allowed directions)
795 * @param select_proc what should be created.
796 * @param pt the exact point on the map where the mouse was released.
797 * @param start_tile the begin tile of the drag.
798 * @param end_tile the end tile of the drag.
800 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
, TileIndex start_tile
, TileIndex end_tile
) {}
803 * The user moves over the map when a tile highlight mode has been set
804 * when the special mouse mode has been set to 'PRESIZE' mode. An
805 * example of this is the tile highlight for dock building.
806 * @param pt the exact point on the map where the mouse is.
807 * @param tile the tile on the map where the mouse is.
809 virtual void OnPlacePresize(Point pt
, TileIndex tile
) {}
811 /*** End of the event handling ***/
814 * Is the data related to this window NewGRF inspectable?
815 * @return true iff it is inspectable.
817 virtual bool IsNewGRFInspectable() const { return false; }
820 * Show the NewGRF inspection window. When this function is called it is
821 * up to the window to call and pass the right parameters to the
822 * ShowInspectWindow function.
823 * @pre this->IsNewGRFInspectable()
825 virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
829 * Get the nested widget with number \a widnum from the nested widget tree.
830 * @tparam NWID Type of the nested widget.
831 * @param widnum Widget number of the widget to retrieve.
832 * @return The requested widget if it is instantiated, \c NULL otherwise.
834 template <class NWID
>
835 inline NWID
*Window::GetWidget(uint widnum
)
837 if (widnum
>= this->nested_array_size
|| this->nested_array
[widnum
] == NULL
) return NULL
;
838 NWID
*nwid
= dynamic_cast<NWID
*>(this->nested_array
[widnum
]);
839 assert(nwid
!= NULL
);
843 /** Specialized case of #Window::GetWidget for the nested widget base class. */
845 inline const NWidgetBase
*Window::GetWidget
<NWidgetBase
>(uint widnum
) const
847 if (widnum
>= this->nested_array_size
) return NULL
;
848 return this->nested_array
[widnum
];
852 * Get the nested widget with number \a widnum from the nested widget tree.
853 * @tparam NWID Type of the nested widget.
854 * @param widnum Widget number of the widget to retrieve.
855 * @return The requested widget if it is instantiated, \c NULL otherwise.
857 template <class NWID
>
858 inline const NWID
*Window::GetWidget(uint widnum
) const
860 return const_cast<Window
*>(this)->GetWidget
<NWID
>(widnum
);
865 * Base class for windows opened from a toolbar.
867 class PickerWindowBase
: public Window
{
870 PickerWindowBase(WindowDesc
*desc
, Window
*parent
) : Window(desc
)
872 this->parent
= parent
;
875 virtual ~PickerWindowBase();
878 Window
*BringWindowToFrontById(WindowClass cls
, WindowNumber number
);
879 Window
*FindWindowFromPt(int x
, int y
);
883 * @tparam Wcls %Window class to use if the window does not exist.
884 * @param desc The pointer to the WindowDesc to be created
885 * @param window_number the window number of the new window
886 * @param return_existing If set, also return the window if it already existed.
887 * @return %Window pointer of the newly created window, or the existing one if \a return_existing is set, or \c NULL.
889 template <typename Wcls
>
890 Wcls
*AllocateWindowDescFront(WindowDesc
*desc
, int window_number
, bool return_existing
= false)
892 Wcls
*w
= static_cast<Wcls
*>(BringWindowToFrontById(desc
->cls
, window_number
));
893 if (w
!= NULL
) return return_existing
? w
: NULL
;
894 return new Wcls(desc
, window_number
);
897 void RelocateAllWindows(int neww
, int newh
);
899 void GuiShowStationRatingTooltip(Window
*parent
, const Station
*st
, const CargoSpec
*cs
);
902 int GetWidgetFromPos(const Window
*w
, int x
, int y
);
904 /** Iterate over all windows */
905 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
906 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
907 #define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
908 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
910 extern Point _cursorpos_drag_start
;
912 extern int _scrollbar_start_pos
;
913 extern int _scrollbar_size
;
914 extern byte _scroller_click_timeout
;
916 extern Window
*_scrolling_viewport
;
917 extern bool _mouse_hovering
;
920 enum SpecialMouseMode
{
921 WSM_NONE
, ///< No special mouse mode.
922 WSM_DRAGDROP
, ///< Dragging an object.
923 WSM_SIZING
, ///< Sizing mode.
924 WSM_PRESIZE
, ///< Presizing mode (docks, tunnels).
926 extern SpecialMouseMode _special_mouse_mode
;
928 void SetFocusedWindow(Window
*w
);
930 void ScrollbarClickHandler(Window
*w
, NWidgetCore
*nw
, int x
, int y
);
932 #endif /* WINDOW_GUI_H */