Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / window_gui.h
blob2f1804b0f52f67efb943f9837c3cead81a121f9f
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file window_gui.h Functions, definitions and such used only by the GUI. */
12 #ifndef WINDOW_GUI_H
13 #define WINDOW_GUI_H
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"
26 /**
27 * Flags to describe the look of the frame
29 enum FrameFlags {
30 FR_NONE = 0,
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 {
41 /* WWT_IMGBTN(_2) */
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.
47 /* WWT_INSET */
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
73 /* WWT_FRAME */
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
79 /* WWT_MATRIX */
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.
85 /* WWT_SHADEBOX */
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.
92 /* WWT_STICKYBOX */
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.
99 /* WWT_DEBUGBOX */
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.
106 /* WWT_DEFSIZEBOX */
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.
113 /* WWT_RESIZEBOX */
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.
120 /* WWT_CLOSEBOX */
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.
127 /* WWT_CAPTION */
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.
145 /* widget.cpp */
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);
149 /* window.cpp */
150 extern Window *_z_front_window;
151 extern Window *_z_back_window;
152 extern Window *_focused_window;
154 inline uint64 GetWindowUpdateNumber()
156 extern uint64 _window_update_number;
157 return _window_update_number;
160 inline void IncrementWindowUpdateNumber()
162 extern uint64 _window_update_number;
163 _window_update_number++;
167 /** How do we the window to be placed? */
168 enum WindowPosition {
169 WDP_MANUAL, ///< Manually align the window (so no automatic location finding)
170 WDP_AUTO, ///< Find a place automatically
171 WDP_CENTER, ///< Center the window
172 WDP_ALIGN_TOOLBAR, ///< Align toward the toolbar
175 Point GetToolbarAlignedWindowPosition(int window_width);
177 struct HotkeyList;
180 * High level window description
182 struct WindowDesc {
184 WindowDesc(WindowPosition default_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
185 WindowClass window_class, WindowClass parent_class, uint32 flags,
186 const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys = NULL);
188 ~WindowDesc();
190 WindowPosition default_pos; ///< Preferred position of the window. @see WindowPosition()
191 WindowClass cls; ///< Class of the window, @see WindowClass.
192 WindowClass parent_cls; ///< Class of the parent window. @see WindowClass
193 const char *ini_key; ///< Key to store window defaults in openttd.cfg. \c NULL if nothing shall be stored.
194 uint32 flags; ///< Flags. @see WindowDefaultFlag
195 const NWidgetPart *nwid_parts; ///< Nested widget parts describing the window.
196 int16 nwid_length; ///< Length of the #nwid_parts array.
197 HotkeyList *hotkeys; ///< Hotkeys for the window.
199 bool pref_sticky; ///< Preferred stickyness.
200 int16 pref_width; ///< User-preferred width of the window. Zero if unset.
201 int16 pref_height; ///< User-preferred height of the window. Zero if unset.
203 int16 GetDefaultWidth() const;
204 int16 GetDefaultHeight() const;
206 static void LoadFromConfig();
207 static void SaveToConfig();
209 private:
210 int16 default_width_trad; ///< Preferred initial width of the window (pixels at 1x zoom).
211 int16 default_height_trad; ///< Preferred initial height of the window (pixels at 1x zoom).
214 * Dummy private copy constructor to prevent compilers from
215 * copying the structure, which fails due to _window_descs.
217 WindowDesc(const WindowDesc &other);
221 * Window default widget/window handling flags
223 enum WindowDefaultFlag {
224 WDF_CONSTRUCTION = 1 << 0, ///< This window is used for construction; close it whenever changing company.
225 WDF_MODAL = 1 << 1, ///< The window is a modal child of some other window, meaning the parent is 'inactive'
226 WDF_NO_FOCUS = 1 << 2, ///< This window won't get focus/make any other window lose focus when click
230 * Data structure for resizing a window
232 struct ResizeInfo {
233 uint step_width; ///< Step-size of width resize changes
234 uint step_height; ///< Step-size of height resize changes
237 /** State of a sort direction button. */
238 enum SortButtonState {
239 SBS_OFF, ///< Do not sort (with this button).
240 SBS_DOWN, ///< Sort ascending.
241 SBS_UP, ///< Sort descending.
245 * Window flags.
247 enum WindowFlags {
248 WF_TIMEOUT = 1 << 0, ///< Window timeout counter.
250 WF_DRAGGING = 1 << 3, ///< Window is being dragged.
251 WF_SIZING_RIGHT = 1 << 4, ///< Window is being resized towards the right.
252 WF_SIZING_LEFT = 1 << 5, ///< Window is being resized towards the left.
253 WF_SIZING = WF_SIZING_RIGHT | WF_SIZING_LEFT, ///< Window is being resized.
254 WF_STICKY = 1 << 6, ///< Window is made sticky by user
255 WF_DISABLE_VP_SCROLL = 1 << 7, ///< Window does not do autoscroll, @see HandleAutoscroll().
256 WF_WHITE_BORDER = 1 << 8, ///< Window white border counter bit mask.
257 WF_HIGHLIGHTED = 1 << 9, ///< Window has a widget that has a highlight.
258 WF_CENTERED = 1 << 10, ///< Window is centered and shall stay centered after ReInit.
260 DECLARE_ENUM_AS_BIT_SET(WindowFlags)
262 static const int TIMEOUT_DURATION = 7; ///< The initial timeout value for WF_TIMEOUT.
263 static const int WHITE_BORDER_DURATION = 3; ///< The initial timeout value for WF_WHITE_BORDER.
266 * Data structure for a window viewport.
267 * A viewport is either following a vehicle (its id in then in #follow_vehicle), or it aims to display a specific
268 * location #dest_scrollpos_x, #dest_scrollpos_y (#follow_vehicle is then #INVALID_VEHICLE).
269 * The actual location being shown is #scrollpos_x, #scrollpos_y.
270 * @see InitializeViewport(), UpdateViewportPosition(), UpdateViewportCoordinates().
272 struct ViewportData : ViewPort {
273 VehicleID follow_vehicle; ///< VehicleID to follow if following a vehicle, #INVALID_VEHICLE otherwise.
274 int32 scrollpos_x; ///< Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
275 int32 scrollpos_y; ///< Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
276 int32 dest_scrollpos_x; ///< Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewport).
277 int32 dest_scrollpos_y; ///< Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewport).
280 struct QueryString;
282 /* misc_gui.cpp */
283 enum TooltipCloseCondition {
284 TCC_RIGHT_CLICK,
285 TCC_LEFT_CLICK,
286 TCC_HOVER,
288 void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip = TCC_HOVER);
289 void GuiShowTooltips(Window *parent, const char *str, TooltipCloseCondition close_tooltip = TCC_HOVER);
290 void GuiShowTooltipsForTile(Window *parent, TileIndex tile, TooltipCloseCondition close_tooltip = TCC_HOVER);
291 void GetTileTooltipsForStation(StationID sid, char *buff, const char *last);
295 * Data structure for an opened window
297 struct Window : ZeroedMemoryAllocator {
298 protected:
299 void InitializeData(WindowNumber window_number);
300 void InitializePositionSize(int x, int y, int min_width, int min_height);
301 virtual void FindWindowPlacementAndResize(int def_width, int def_height);
303 SmallVector<int, 4> scheduled_invalidation_data; ///< Data of scheduled OnInvalidateData() calls.
305 public:
306 Window(WindowDesc *desc);
308 virtual ~Window();
311 * Helper allocation function to disallow something.
312 * Don't allow arrays; arrays of Windows are pointless as you need
313 * to destruct them all at the same time too, which is kinda hard.
314 * @param size the amount of space not to allocate
316 inline void *operator new[](size_t size)
318 NOT_REACHED();
322 * Helper allocation function to disallow something.
323 * Don't free the window directly; it corrupts the linked list when iterating
324 * @param ptr the pointer not to free
326 inline void operator delete(void *ptr)
330 WindowDesc *window_desc; ///< Window description
331 WindowFlags flags; ///< Window flags
332 WindowClass window_class; ///< Window class
333 WindowNumber window_number; ///< Window number within the window class
335 uint8 timeout_timer; ///< Timer value of the WF_TIMEOUT for flags.
336 uint8 white_border_timer; ///< Timer value of the WF_WHITE_BORDER for flags.
338 int left; ///< x position of left edge of the window
339 int top; ///< y position of top edge of the window
340 int width; ///< width of the window (number of pixels to the right in x direction)
341 int height; ///< Height of the window (number of pixels down in y direction)
343 ResizeInfo resize; ///< Resize information
345 Owner owner; ///< The owner of the content shown in this window. Company colour is acquired from this variable.
347 ViewportData *viewport; ///< Pointer to viewport data, if present.
348 const NWidgetCore *nested_focus; ///< Currently focused nested widget, or \c NULL if no nested widget has focus.
349 SmallMap<int, QueryString*> querystrings; ///< QueryString associated to WWT_EDITBOX widgets.
350 NWidgetBase *nested_root; ///< Root of the nested tree.
351 NWidgetBase **nested_array; ///< Array of pointers into the tree. Do not access directly, use #Window::GetWidget() instead.
352 uint nested_array_size; ///< Size of the nested array.
353 NWidgetStacked *shade_select; ///< Selection widget (#NWID_SELECTION) to use for shading the window. If \c NULL, window cannot shade.
354 Dimension unshaded_size; ///< Last known unshaded size (only valid while shaded).
356 int scrolling_scrollbar; ///< Widgetindex of just being dragged scrollbar. -1 if none is active.
358 Window *parent; ///< Parent window.
359 Window *z_front; ///< The window in front of us in z-order.
360 Window *z_back; ///< The window behind us in z-order.
362 template <class NWID>
363 inline const NWID *GetWidget(uint widnum) const;
364 template <class NWID>
365 inline NWID *GetWidget(uint widnum);
367 const Scrollbar *GetScrollbar(uint widnum) const;
368 Scrollbar *GetScrollbar(uint widnum);
370 const QueryString *GetQueryString(uint widnum) const;
371 QueryString *GetQueryString(uint widnum);
373 virtual const char *GetFocusedText() const;
374 virtual const char *GetCaret() const;
375 virtual const char *GetMarkedText(size_t *length) const;
376 virtual Point GetCaretPosition() const;
377 virtual Rect GetTextBoundingRect(const char *from, const char *to) const;
378 virtual const char *GetTextCharacterAtPosition(const Point &pt) const;
380 void InitNested(WindowNumber number = 0);
381 void CreateNestedTree(bool fill_nested = true);
382 void FinishInitNested(WindowNumber window_number = 0);
385 * Set the timeout flag of the window and initiate the timer.
387 inline void SetTimeout()
389 this->flags |= WF_TIMEOUT;
390 this->timeout_timer = TIMEOUT_DURATION;
394 * Set the timeout flag of the window and initiate the timer.
396 inline void SetWhiteBorder()
398 this->flags |= WF_WHITE_BORDER;
399 this->white_border_timer = WHITE_BORDER_DURATION;
402 void DisableAllWidgetHighlight();
403 void SetWidgetHighlight(byte widget_index, TextColour highlighted_colour);
404 bool IsWidgetHighlighted(byte widget_index) const;
407 * Sets the enabled/disabled status of a widget.
408 * By default, widgets are enabled.
409 * On certain conditions, they have to be disabled.
410 * @param widget_index index of this widget in the window
411 * @param disab_stat status to use ie: disabled = true, enabled = false
413 inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
415 assert(widget_index < this->nested_array_size);
416 if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
420 * Sets a widget to disabled.
421 * @param widget_index index of this widget in the window
423 inline void DisableWidget(byte widget_index)
425 SetWidgetDisabledState(widget_index, true);
429 * Sets a widget to Enabled.
430 * @param widget_index index of this widget in the window
432 inline void EnableWidget(byte widget_index)
434 SetWidgetDisabledState(widget_index, false);
438 * Gets the enabled/disabled status of a widget.
439 * @param widget_index index of this widget in the window
440 * @return status of the widget ie: disabled = true, enabled = false
442 inline bool IsWidgetDisabled(byte widget_index) const
444 assert(widget_index < this->nested_array_size);
445 return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
449 * Check if given widget is focused within this window
450 * @param widget_index : index of the widget in the window to check
451 * @return true if given widget is the focused window in this window
453 inline bool IsWidgetFocused(byte widget_index) const
455 return this->nested_focus != NULL && this->nested_focus->index == widget_index;
459 * Check if given widget has user input focus. This means that both the window
460 * has focus and that the given widget has focus within the window.
461 * @param widget_index : index of the widget in the window to check
462 * @return true if given widget is the focused window in this window and this window has focus
464 inline bool IsWidgetGloballyFocused(byte widget_index) const
466 return _focused_window == this && IsWidgetFocused(widget_index);
470 * Sets the lowered/raised status of a widget.
471 * @param widget_index index of this widget in the window
472 * @param lowered_stat status to use ie: lowered = true, raised = false
474 inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
476 assert(widget_index < this->nested_array_size);
477 this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
481 * Invert the lowered/raised status of a widget.
482 * @param widget_index index of this widget in the window
484 inline void ToggleWidgetLoweredState(byte widget_index)
486 assert(widget_index < this->nested_array_size);
487 bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
488 this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
492 * Marks a widget as lowered.
493 * @param widget_index index of this widget in the window
495 inline void LowerWidget(byte widget_index)
497 SetWidgetLoweredState(widget_index, true);
501 * Marks a widget as raised.
502 * @param widget_index index of this widget in the window
504 inline void RaiseWidget(byte widget_index)
506 SetWidgetLoweredState(widget_index, false);
510 * Gets the lowered state of a widget.
511 * @param widget_index index of this widget in the window
512 * @return status of the widget ie: lowered = true, raised= false
514 inline bool IsWidgetLowered(byte widget_index) const
516 assert(widget_index < this->nested_array_size);
517 return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
520 void UnfocusFocusedWidget();
521 bool SetFocusedWidget(int widget_index);
523 EventState HandleEditBoxKey(int wid, WChar key, uint16 keycode);
524 virtual void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end);
526 void HandleButtonClick(byte widget);
527 int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
529 void RaiseButtons(bool autoraise = false);
530 void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
531 void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
532 void SetWidgetDirty(byte widget_index) const;
534 void DrawWidgets() const;
535 void DrawViewport() const;
536 void DrawSortButtonState(int widget, SortButtonState state) const;
537 static int SortButtonWidth();
539 void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
541 void SetDirty() const;
542 void ReInit(int rx = 0, int ry = 0);
544 /** Is window shaded currently? */
545 inline bool IsShaded() const
547 return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
550 void SetShaded(bool make_shaded);
552 void InvalidateData(int data = 0, bool gui_scope = true);
553 void ProcessScheduledInvalidations();
554 void ProcessHighlightedInvalidations();
556 /*** Event handling ***/
559 * Notification that the nested widget tree gets initialized. The event can be used to perform general computations.
560 * @note #nested_root and/or #nested_array (normally accessed via #GetWidget()) may not exist during this call.
562 virtual void OnInit() { }
564 virtual void ApplyDefaults();
567 * Compute the initial position of the window.
568 * @param sm_width Smallest width of the window.
569 * @param sm_height Smallest height of the window.
570 * @param window_number The window number of the new window.
571 * @return Initial position of the top-left corner of the window.
573 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number);
576 * The window must be repainted.
577 * @note This method should not change any state, it should only use drawing functions.
579 virtual void OnPaint()
581 this->DrawWidgets();
585 * Draw the contents of a nested widget.
586 * @param r Rectangle occupied by the widget.
587 * @param widget Number of the widget to draw.
588 * @note This method may not change any state, it may only use drawing functions.
590 virtual void DrawWidget(const Rect &r, int widget) const {}
593 * Update size and resize step of a widget in the window.
594 * After retrieval of the minimal size and the resize-steps of a widget, this function is called to allow further refinement,
595 * typically by computing the real maximal size of the content. Afterwards, \a size is taken to be the minimal size of the widget
596 * and \a resize is taken to contain the resize steps. For the convenience of the callee, \a padding contains the amount of
597 * padding between the content and the edge of the widget. This should be added to the returned size.
598 * @param widget Widget number.
599 * @param size Size of the widget.
600 * @param padding Recommended amount of space between the widget content and the widget edge.
601 * @param fill Fill step of the widget.
602 * @param resize Resize step of the widget.
604 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
607 * Initialize string parameters for a widget.
608 * Calls to this function are made during initialization to measure the size (that is as part of #InitNested()), during drawing,
609 * and while re-initializing the window. Only for widgets that render text initializing is requested.
610 * @param widget Widget number.
612 virtual void SetStringParameters(int widget) const {}
615 * Called when window gains focus
616 * @param previously_focused_window The window that lost the focus.
618 virtual void OnFocus(Window *previously_focused_window) {}
620 virtual void OnFocusLost(Window *newly_focused_window);
623 * A key has been pressed.
624 * @param key the Unicode value of the key.
625 * @param keycode the untranslated key code including shift state.
626 * @return #ES_HANDLED if the key press has been handled and no other
627 * window should receive the event.
629 virtual EventState OnKeyPress(WChar key, uint16 keycode) { return ES_NOT_HANDLED; }
631 virtual EventState OnHotkey(int hotkey);
634 * The state of the control key has changed
635 * @return #ES_HANDLED if the change has been handled and no other
636 * window should receive the event.
638 virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
642 * A click with the left mouse button has been made on the window.
643 * @param pt the point inside the window that has been clicked.
644 * @param widget the clicked widget.
645 * @param click_count Number of fast consecutive clicks at same position
647 virtual void OnClick(Point pt, int widget, int click_count) {}
650 * A click with the right mouse button has been made on the window.
651 * @param pt the point inside the window that has been clicked.
652 * @param widget the clicked widget.
653 * @return true if the click was actually handled, i.e. do not show a
654 * tooltip if tooltip-on-right-click is enabled.
656 virtual bool OnRightClick(Point pt, int widget) { return false; }
659 * The mouse is hovering over a widget in the window, perform an action for it, like opening a custom tooltip.
660 * @param pt The point where the mouse is hovering.
661 * @param widget The widget where the mouse is hovering.
663 //virtual void OnHover(Point pt, int widget) {}
666 * An 'object' is being dragged at the provided position, highlight the target if possible.
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 OnMouseDrag(Point pt, int widget) {}
673 * A dragged 'object' has been released.
674 * @param pt the point inside the window where the release took place.
675 * @param widget the widget where the release took place.
677 virtual void OnDragDrop(Point pt, int widget) {}
680 * Handle the request for (viewport) scrolling.
681 * @param delta the amount the viewport must be scrolled.
683 virtual void OnScroll(Point delta) {}
686 * The mouse is currently moving over the window or has just moved outside
687 * of the window. In the latter case pt is (-1, -1).
688 * @param pt the point inside the window that the mouse hovers over.
689 * @param widget the widget the mouse hovers over.
691 virtual void OnMouseOver(Point pt, int widget) {}
694 * The mouse wheel has been turned.
695 * @param wheel the amount of movement of the mouse wheel.
697 virtual void OnMouseWheel(int wheel) {}
701 * Called for every mouse loop run, which is at least once per (game) tick.
703 virtual void OnMouseLoop() {}
706 * Called once per (game) tick.
708 virtual void OnTick() {}
711 * Called once every 100 (game) ticks.
713 virtual void OnHundredthTick() {}
716 * Called when this window's timeout has been reached.
718 virtual void OnTimeout() {}
722 * Called after the window got resized.
723 * For nested windows with a viewport, call NWidgetViewport::UpdateViewportCoordinates.
725 virtual void OnResize() {}
728 * A dropdown option associated to this window has been selected.
729 * @param widget the widget (button) that the dropdown is associated with.
730 * @param index the element in the dropdown that is selected.
732 virtual void OnDropdownSelect(int widget, int index) {}
734 virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close);
737 * A tooltip is about to be shown (e.g. the mouse is hovering). Perform
738 * an action for it, default is to show the tip of the widget.
740 * @param pt The point where the mouse is hovering.
741 * @param widget The widget where the mouse is hovering.
742 * @param close_cond How to close tooltips, depands on the way the event was initiated.
744 * @see GuiShowTooltips
746 virtual void OnToolTip(Point pt, int widget, TooltipCloseCondition close_cond)
748 NWidgetCore *wid = this->GetWidget<NWidgetCore>(widget);
749 if (wid != NULL) GuiShowTooltips(this, wid->tool_tip, close_cond);
753 * The text in an editbox has been edited.
754 * @param widget The widget of the editbox.
756 virtual void OnEditboxChanged(int widget) {}
759 * The query window opened from this window has closed.
760 * @param str the new value of the string, NULL if the window
761 * was cancelled or an empty string when the default
762 * button was pressed, i.e. StrEmpty(str).
764 virtual void OnQueryTextFinished(char *str) {}
767 * Some data on this window has become invalid.
768 * @param data information about the changed data.
769 * @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.
771 virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
774 * The user clicked some place on the map when a tile highlight mode
775 * has been set.
776 * @param pt the exact point on the map that has been clicked.
777 * @param tile the tile on the map that has been clicked.
779 virtual void OnPlaceObject(Point pt, TileIndex tile) {}
782 * The user clicked on a vehicle while HT_VEHICLE has been set.
783 * @param v clicked vehicle. It is guaranteed to be v->IsPrimaryVehicle() == true
784 * @return True if the click is handled, false if it is ignored.
786 virtual bool OnVehicleSelect(const struct Vehicle *v) { return false; }
789 * The user cancelled a tile highlight mode that has been set.
791 virtual void OnPlaceObjectAbort() {}
795 * The user is dragging over the map when the tile highlight mode
796 * has been set.
797 * @param select_method the method of selection (allowed directions)
798 * @param select_proc what will be created when the drag is over.
799 * @param pt the exact point on the map where the mouse is.
801 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
804 * The user has dragged over the map when the tile highlight mode
805 * has been set.
806 * @param select_method the method of selection (allowed directions)
807 * @param select_proc what should be created.
808 * @param pt the exact point on the map where the mouse was released.
809 * @param start_tile the begin tile of the drag.
810 * @param end_tile the end tile of the drag.
812 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
815 * The user moves over the map when a tile highlight mode has been set
816 * when the special mouse mode has been set to 'PRESIZE' mode. An
817 * example of this is the tile highlight for dock building.
818 * @param pt the exact point on the map where the mouse is.
819 * @param tile the tile on the map where the mouse is.
821 virtual void OnPlacePresize(Point pt, TileIndex tile) {}
823 /*** End of the event handling ***/
826 * Is the data related to this window NewGRF inspectable?
827 * @return true iff it is inspectable.
829 virtual bool IsNewGRFInspectable() const { return false; }
832 * Show the NewGRF inspection window. When this function is called it is
833 * up to the window to call and pass the right parameters to the
834 * ShowInspectWindow function.
835 * @pre this->IsNewGRFInspectable()
837 virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
841 * Get the nested widget with number \a widnum from the nested widget tree.
842 * @tparam NWID Type of the nested widget.
843 * @param widnum Widget number of the widget to retrieve.
844 * @return The requested widget if it is instantiated, \c NULL otherwise.
846 template <class NWID>
847 inline NWID *Window::GetWidget(uint widnum)
849 if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
850 NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
851 assert(nwid != NULL);
852 return nwid;
855 /** Specialized case of #Window::GetWidget for the nested widget base class. */
856 template <>
857 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
859 if (widnum >= this->nested_array_size) return NULL;
860 return this->nested_array[widnum];
864 * Get the nested widget with number \a widnum from the nested widget tree.
865 * @tparam NWID Type of the nested widget.
866 * @param widnum Widget number of the widget to retrieve.
867 * @return The requested widget if it is instantiated, \c NULL otherwise.
869 template <class NWID>
870 inline const NWID *Window::GetWidget(uint widnum) const
872 return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
877 * Base class for windows opened from a toolbar.
879 class PickerWindowBase : public Window {
881 public:
882 PickerWindowBase(WindowDesc *desc, Window *parent) : Window(desc)
884 this->parent = parent;
887 virtual ~PickerWindowBase();
890 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
891 Window *FindWindowFromPt(int x, int y);
894 * Open a new window.
895 * @tparam Wcls %Window class to use if the window does not exist.
896 * @param desc The pointer to the WindowDesc to be created
897 * @param window_number the window number of the new window
898 * @param return_existing If set, also return the window if it already existed.
899 * @return %Window pointer of the newly created window, or the existing one if \a return_existing is set, or \c NULL.
901 template <typename Wcls>
902 Wcls *AllocateWindowDescFront(WindowDesc *desc, int window_number, bool return_existing = false)
904 Wcls *w = static_cast<Wcls *>(BringWindowToFrontById(desc->cls, window_number));
905 if (w != NULL) return return_existing ? w : NULL;
906 return new Wcls(desc, window_number);
909 void RelocateAllWindows(int neww, int newh);
911 void GuiShowStationRatingTooltip(Window *parent, const Station *st, const CargoSpec *cs);
913 /* widget.cpp */
914 int GetWidgetFromPos(const Window *w, int x, int y);
916 /** Iterate over all windows */
917 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
918 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
919 #define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
920 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
922 extern Point _cursorpos_drag_start;
924 extern int _scrollbar_start_pos;
925 extern int _scrollbar_size;
926 extern byte _scroller_click_timeout;
928 extern Window *_scrolling_viewport;
929 extern bool _mouse_hovering;
931 /** Mouse modes. */
932 enum SpecialMouseMode {
933 WSM_NONE, ///< No special mouse mode.
934 WSM_DRAGDROP, ///< Dragging an object.
935 WSM_SIZING, ///< Sizing mode.
936 WSM_PRESIZE, ///< Presizing mode (docks, tunnels).
938 extern SpecialMouseMode _special_mouse_mode;
940 void SetFocusedWindow(Window *w);
942 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
944 #endif /* WINDOW_GUI_H */