(svn r28004) -Update from Eints:
[openttd.git] / src / window_gui.h
blobb81b06e391f6d25e0d43b56b1fae1b8e72ed0ee9
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 "core/smallvec_type.hpp"
21 #include "core/smallmap_type.hpp"
22 #include "string_type.h"
24 /**
25 * Flags to describe the look of the frame
27 enum FrameFlags {
28 FR_NONE = 0,
29 FR_TRANSPARENT = 1 << 0, ///< Makes the background transparent if set
30 FR_BORDERONLY = 1 << 4, ///< Draw border only, no background
31 FR_LOWERED = 1 << 5, ///< If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
32 FR_DARKENED = 1 << 6, ///< If set the background is darker, allows for lowered frames with normal background colour when used with FR_LOWERED (ie. dropdown boxes)
35 DECLARE_ENUM_AS_BIT_SET(FrameFlags)
37 /** Distances used in drawing widgets. */
38 enum WidgetDrawDistances {
39 /* WWT_IMGBTN(_2) */
40 WD_IMGBTN_LEFT = 1, ///< Left offset of the image in the button.
41 WD_IMGBTN_RIGHT = 2, ///< Right offset of the image in the button.
42 WD_IMGBTN_TOP = 1, ///< Top offset of image in the button.
43 WD_IMGBTN_BOTTOM = 2, ///< Bottom offset of image in the button.
45 /* WWT_INSET */
46 WD_INSET_LEFT = 2, ///< Left offset of string.
47 WD_INSET_RIGHT = 2, ///< Right offset of string.
48 WD_INSET_TOP = 1, ///< Top offset of string.
50 WD_SCROLLBAR_LEFT = 2, ///< Left offset of scrollbar.
51 WD_SCROLLBAR_RIGHT = 2, ///< Right offset of scrollbar.
52 WD_SCROLLBAR_TOP = 2, ///< Top offset of scrollbar.
53 WD_SCROLLBAR_BOTTOM = 2, ///< Bottom offset of scrollbar.
55 /* Size of the pure frame bevel without any padding. */
56 WD_BEVEL_LEFT = 1, ///< Width of left bevel border.
57 WD_BEVEL_RIGHT = 1, ///< Width of right bevel border.
58 WD_BEVEL_TOP = 1, ///< Height of top bevel border.
59 WD_BEVEL_BOTTOM = 1, ///< Height of bottom bevel border.
61 /* FrameRect widgets, all text buttons, panel, editbox */
62 WD_FRAMERECT_LEFT = 2, ///< Offset at left to draw the frame rectangular area
63 WD_FRAMERECT_RIGHT = 2, ///< Offset at right to draw the frame rectangular area
64 WD_FRAMERECT_TOP = 1, ///< Offset at top to draw the frame rectangular area
65 WD_FRAMERECT_BOTTOM = 1, ///< Offset at bottom to draw the frame rectangular area
67 /* Extra space at top/bottom of text panels */
68 WD_TEXTPANEL_TOP = 6, ///< Offset at top to draw above the text
69 WD_TEXTPANEL_BOTTOM = 6, ///< Offset at bottom to draw below the text
71 /* WWT_FRAME */
72 WD_FRAMETEXT_LEFT = 6, ///< Left offset of the text of the frame.
73 WD_FRAMETEXT_RIGHT = 6, ///< Right offset of the text of the frame.
74 WD_FRAMETEXT_TOP = 6, ///< Top offset of the text of the frame
75 WD_FRAMETEXT_BOTTOM = 6, ///< Bottom offset of the text of the frame
77 /* WWT_MATRIX */
78 WD_MATRIX_LEFT = 2, ///< Offset at left of a matrix cell.
79 WD_MATRIX_RIGHT = 2, ///< Offset at right of a matrix cell.
80 WD_MATRIX_TOP = 3, ///< Offset at top of a matrix cell.
81 WD_MATRIX_BOTTOM = 1, ///< Offset at bottom of a matrix cell.
83 /* WWT_SHADEBOX */
84 WD_SHADEBOX_WIDTH = 12, ///< Width of a standard shade box widget.
85 WD_SHADEBOX_LEFT = 2, ///< Left offset of shade sprite.
86 WD_SHADEBOX_RIGHT = 2, ///< Right offset of shade sprite.
87 WD_SHADEBOX_TOP = 3, ///< Top offset of shade sprite.
88 WD_SHADEBOX_BOTTOM = 3, ///< Bottom offset of shade sprite.
90 /* WWT_STICKYBOX */
91 WD_STICKYBOX_WIDTH = 12, ///< Width of a standard sticky box widget.
92 WD_STICKYBOX_LEFT = 2, ///< Left offset of sticky sprite.
93 WD_STICKYBOX_RIGHT = 2, ///< Right offset of sticky sprite.
94 WD_STICKYBOX_TOP = 3, ///< Top offset of sticky sprite.
95 WD_STICKYBOX_BOTTOM = 3, ///< Bottom offset of sticky sprite.
97 /* WWT_DEBUGBOX */
98 WD_DEBUGBOX_WIDTH = 12, ///< Width of a standard debug box widget.
99 WD_DEBUGBOX_LEFT = 2, ///< Left offset of debug sprite.
100 WD_DEBUGBOX_RIGHT = 2, ///< Right offset of debug sprite.
101 WD_DEBUGBOX_TOP = 3, ///< Top offset of debug sprite.
102 WD_DEBUGBOX_BOTTOM = 3, ///< Bottom offset of debug sprite.
104 /* WWT_DEFSIZEBOX */
105 WD_DEFSIZEBOX_WIDTH = 12, ///< Width of a standard defsize box widget.
106 WD_DEFSIZEBOX_LEFT = 2, ///< Left offset of defsize sprite.
107 WD_DEFSIZEBOX_RIGHT = 2, ///< Right offset of defsize sprite.
108 WD_DEFSIZEBOX_TOP = 3, ///< Top offset of defsize sprite.
109 WD_DEFSIZEBOX_BOTTOM = 3, ///< Bottom offset of defsize sprite.
111 /* WWT_RESIZEBOX */
112 WD_RESIZEBOX_WIDTH = 12, ///< Width of a resize box widget.
113 WD_RESIZEBOX_LEFT = 3, ///< Left offset of resize sprite.
114 WD_RESIZEBOX_RIGHT = 2, ///< Right offset of resize sprite.
115 WD_RESIZEBOX_TOP = 3, ///< Top offset of resize sprite.
116 WD_RESIZEBOX_BOTTOM = 2, ///< Bottom offset of resize sprite.
118 /* WWT_CLOSEBOX */
119 WD_CLOSEBOX_WIDTH = 11, ///< Width of a close box widget.
120 WD_CLOSEBOX_LEFT = 2, ///< Left offset of closebox string.
121 WD_CLOSEBOX_RIGHT = 1, ///< Right offset of closebox string.
122 WD_CLOSEBOX_TOP = 2, ///< Top offset of closebox string.
123 WD_CLOSEBOX_BOTTOM = 2, ///< Bottom offset of closebox string.
125 /* WWT_CAPTION */
126 WD_CAPTION_HEIGHT = 14, ///< Height of a title bar.
127 WD_CAPTIONTEXT_LEFT = 2, ///< Offset of the caption text at the left.
128 WD_CAPTIONTEXT_RIGHT = 2, ///< Offset of the caption text at the right.
129 WD_CAPTIONTEXT_TOP = 2, ///< Offset of the caption text at the top.
130 WD_CAPTIONTEXT_BOTTOM = 2, ///< Offset of the caption text at the bottom.
132 /* Dropdown widget. */
133 WD_DROPDOWN_HEIGHT = 12, ///< Height of a drop down widget.
134 WD_DROPDOWNTEXT_LEFT = 2, ///< Left offset of the dropdown widget string.
135 WD_DROPDOWNTEXT_RIGHT = 2, ///< Right offset of the dropdown widget string.
136 WD_DROPDOWNTEXT_TOP = 1, ///< Top offset of the dropdown widget string.
137 WD_DROPDOWNTEXT_BOTTOM = 1, ///< Bottom offset of the dropdown widget string.
139 WD_PAR_VSEP_NORMAL = 2, ///< Normal amount of vertical space between two paragraphs of text.
140 WD_PAR_VSEP_WIDE = 8, ///< Large amount of vertical space between two paragraphs of text.
143 /* widget.cpp */
144 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
145 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str);
147 /* window.cpp */
148 extern Window *_z_front_window;
149 extern Window *_z_back_window;
150 extern Window *_focused_window;
153 /** How do we the window to be placed? */
154 enum WindowPosition {
155 WDP_MANUAL, ///< Manually align the window (so no automatic location finding)
156 WDP_AUTO, ///< Find a place automatically
157 WDP_CENTER, ///< Center the window
158 WDP_ALIGN_TOOLBAR, ///< Align toward the toolbar
161 Point GetToolbarAlignedWindowPosition(int window_width);
163 struct HotkeyList;
166 * High level window description
168 struct WindowDesc : ZeroedMemoryAllocator {
170 WindowDesc(WindowPosition default_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
171 WindowClass window_class, WindowClass parent_class, uint32 flags,
172 const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys = NULL);
174 ~WindowDesc();
176 WindowPosition default_pos; ///< Preferred position of the window. @see WindowPosition()
177 WindowClass cls; ///< Class of the window, @see WindowClass.
178 WindowClass parent_cls; ///< Class of the parent window. @see WindowClass
179 const char *ini_key; ///< Key to store window defaults in openttd.cfg. \c NULL if nothing shall be stored.
180 uint32 flags; ///< Flags. @see WindowDefaultFlag
181 const NWidgetPart *nwid_parts; ///< Nested widget parts describing the window.
182 int16 nwid_length; ///< Length of the #nwid_parts array.
183 HotkeyList *hotkeys; ///< Hotkeys for the window.
185 bool pref_sticky; ///< Preferred stickyness.
186 int16 pref_width; ///< User-preferred width of the window. Zero if unset.
187 int16 pref_height; ///< User-preferred height of the window. Zero if unset.
189 int16 GetDefaultWidth() const;
190 int16 GetDefaultHeight() const;
192 static void LoadFromConfig();
193 static void SaveToConfig();
195 private:
196 int16 default_width_trad; ///< Preferred initial width of the window (pixels at 1x zoom).
197 int16 default_height_trad; ///< Preferred initial height of the window (pixels at 1x zoom).
200 * Dummy private copy constructor to prevent compilers from
201 * copying the structure, which fails due to _window_descs.
203 WindowDesc(const WindowDesc &other);
207 * Window default widget/window handling flags
209 enum WindowDefaultFlag {
210 WDF_CONSTRUCTION = 1 << 0, ///< This window is used for construction; close it whenever changing company.
211 WDF_MODAL = 1 << 1, ///< The window is a modal child of some other window, meaning the parent is 'inactive'
212 WDF_NO_FOCUS = 1 << 2, ///< This window won't get focus/make any other window lose focus when click
216 * Data structure for resizing a window
218 struct ResizeInfo {
219 uint step_width; ///< Step-size of width resize changes
220 uint step_height; ///< Step-size of height resize changes
223 /** State of a sort direction button. */
224 enum SortButtonState {
225 SBS_OFF, ///< Do not sort (with this button).
226 SBS_DOWN, ///< Sort ascending.
227 SBS_UP, ///< Sort descending.
231 * Window flags.
233 enum WindowFlags {
234 WF_TIMEOUT = 1 << 0, ///< Window timeout counter.
236 WF_DRAGGING = 1 << 3, ///< Window is being dragged.
237 WF_SIZING_RIGHT = 1 << 4, ///< Window is being resized towards the right.
238 WF_SIZING_LEFT = 1 << 5, ///< Window is being resized towards the left.
239 WF_SIZING = WF_SIZING_RIGHT | WF_SIZING_LEFT, ///< Window is being resized.
240 WF_STICKY = 1 << 6, ///< Window is made sticky by user
241 WF_DISABLE_VP_SCROLL = 1 << 7, ///< Window does not do autoscroll, @see HandleAutoscroll().
242 WF_WHITE_BORDER = 1 << 8, ///< Window white border counter bit mask.
243 WF_HIGHLIGHTED = 1 << 9, ///< Window has a widget that has a highlight.
244 WF_CENTERED = 1 << 10, ///< Window is centered and shall stay centered after ReInit.
246 DECLARE_ENUM_AS_BIT_SET(WindowFlags)
248 static const int TIMEOUT_DURATION = 7; ///< The initial timeout value for WF_TIMEOUT.
249 static const int WHITE_BORDER_DURATION = 3; ///< The initial timeout value for WF_WHITE_BORDER.
252 * Data structure for a window viewport.
253 * A viewport is either following a vehicle (its id in then in #follow_vehicle), or it aims to display a specific
254 * location #dest_scrollpos_x, #dest_scrollpos_y (#follow_vehicle is then #INVALID_VEHICLE).
255 * The actual location being shown is #scrollpos_x, #scrollpos_y.
256 * @see InitializeViewport(), UpdateViewportPosition(), UpdateViewportCoordinates().
258 struct ViewportData : ViewPort {
259 VehicleID follow_vehicle; ///< VehicleID to follow if following a vehicle, #INVALID_VEHICLE otherwise.
260 int32 scrollpos_x; ///< Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
261 int32 scrollpos_y; ///< Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
262 int32 dest_scrollpos_x; ///< Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewport).
263 int32 dest_scrollpos_y; ///< Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewport).
266 struct QueryString;
269 * Data structure for an opened window
271 struct Window : ZeroedMemoryAllocator {
272 protected:
273 void InitializeData(WindowNumber window_number);
274 void InitializePositionSize(int x, int y, int min_width, int min_height);
275 virtual void FindWindowPlacementAndResize(int def_width, int def_height);
277 SmallVector<int, 4> scheduled_invalidation_data; ///< Data of scheduled OnInvalidateData() calls.
279 public:
280 Window(WindowDesc *desc);
282 virtual ~Window();
285 * Helper allocation function to disallow something.
286 * Don't allow arrays; arrays of Windows are pointless as you need
287 * to destruct them all at the same time too, which is kinda hard.
288 * @param size the amount of space not to allocate
290 inline void *operator new[](size_t size)
292 NOT_REACHED();
296 * Helper allocation function to disallow something.
297 * Don't free the window directly; it corrupts the linked list when iterating
298 * @param ptr the pointer not to free
300 inline void operator delete(void *ptr)
304 WindowDesc *window_desc; ///< Window description
305 WindowFlags flags; ///< Window flags
306 WindowClass window_class; ///< Window class
307 WindowNumber window_number; ///< Window number within the window class
309 uint8 timeout_timer; ///< Timer value of the WF_TIMEOUT for flags.
310 uint8 white_border_timer; ///< Timer value of the WF_WHITE_BORDER for flags.
312 int left; ///< x position of left edge of the window
313 int top; ///< y position of top edge of the window
314 int width; ///< width of the window (number of pixels to the right in x direction)
315 int height; ///< Height of the window (number of pixels down in y direction)
317 ResizeInfo resize; ///< Resize information
319 Owner owner; ///< The owner of the content shown in this window. Company colour is acquired from this variable.
321 ViewportData *viewport; ///< Pointer to viewport data, if present.
322 const NWidgetCore *nested_focus; ///< Currently focused nested widget, or \c NULL if no nested widget has focus.
323 SmallMap<int, QueryString*> querystrings; ///< QueryString associated to WWT_EDITBOX widgets.
324 NWidgetBase *nested_root; ///< Root of the nested tree.
325 NWidgetBase **nested_array; ///< Array of pointers into the tree. Do not access directly, use #Window::GetWidget() instead.
326 uint nested_array_size; ///< Size of the nested array.
327 NWidgetStacked *shade_select; ///< Selection widget (#NWID_SELECTION) to use for shading the window. If \c NULL, window cannot shade.
328 Dimension unshaded_size; ///< Last known unshaded size (only valid while shaded).
330 int scrolling_scrollbar; ///< Widgetindex of just being dragged scrollbar. -1 if none is active.
332 Window *parent; ///< Parent window.
333 Window *z_front; ///< The window in front of us in z-order.
334 Window *z_back; ///< The window behind us in z-order.
336 template <class NWID>
337 inline const NWID *GetWidget(uint widnum) const;
338 template <class NWID>
339 inline NWID *GetWidget(uint widnum);
341 const Scrollbar *GetScrollbar(uint widnum) const;
342 Scrollbar *GetScrollbar(uint widnum);
344 const QueryString *GetQueryString(uint widnum) const;
345 QueryString *GetQueryString(uint widnum);
347 virtual const char *GetFocusedText() const;
348 virtual const char *GetCaret() const;
349 virtual const char *GetMarkedText(size_t *length) const;
350 virtual Point GetCaretPosition() const;
351 virtual Rect GetTextBoundingRect(const char *from, const char *to) const;
352 virtual const char *GetTextCharacterAtPosition(const Point &pt) const;
354 void InitNested(WindowNumber number = 0);
355 void CreateNestedTree(bool fill_nested = true);
356 void FinishInitNested(WindowNumber window_number = 0);
359 * Set the timeout flag of the window and initiate the timer.
361 inline void SetTimeout()
363 this->flags |= WF_TIMEOUT;
364 this->timeout_timer = TIMEOUT_DURATION;
368 * Set the timeout flag of the window and initiate the timer.
370 inline void SetWhiteBorder()
372 this->flags |= WF_WHITE_BORDER;
373 this->white_border_timer = WHITE_BORDER_DURATION;
376 void DisableAllWidgetHighlight();
377 void SetWidgetHighlight(byte widget_index, TextColour highlighted_colour);
378 bool IsWidgetHighlighted(byte widget_index) const;
381 * Sets the enabled/disabled status of a widget.
382 * By default, widgets are enabled.
383 * On certain conditions, they have to be disabled.
384 * @param widget_index index of this widget in the window
385 * @param disab_stat status to use ie: disabled = true, enabled = false
387 inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
389 assert(widget_index < this->nested_array_size);
390 if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
394 * Sets a widget to disabled.
395 * @param widget_index index of this widget in the window
397 inline void DisableWidget(byte widget_index)
399 SetWidgetDisabledState(widget_index, true);
403 * Sets a widget to Enabled.
404 * @param widget_index index of this widget in the window
406 inline void EnableWidget(byte widget_index)
408 SetWidgetDisabledState(widget_index, false);
412 * Gets the enabled/disabled status of a widget.
413 * @param widget_index index of this widget in the window
414 * @return status of the widget ie: disabled = true, enabled = false
416 inline bool IsWidgetDisabled(byte widget_index) const
418 assert(widget_index < this->nested_array_size);
419 return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
423 * Check if given widget is focused within this window
424 * @param widget_index : index of the widget in the window to check
425 * @return true if given widget is the focused window in this window
427 inline bool IsWidgetFocused(byte widget_index) const
429 return this->nested_focus != NULL && this->nested_focus->index == widget_index;
433 * Check if given widget has user input focus. This means that both the window
434 * has focus and that the given widget has focus within the window.
435 * @param widget_index : index of the widget in the window to check
436 * @return true if given widget is the focused window in this window and this window has focus
438 inline bool IsWidgetGloballyFocused(byte widget_index) const
440 return _focused_window == this && IsWidgetFocused(widget_index);
444 * Sets the lowered/raised status of a widget.
445 * @param widget_index index of this widget in the window
446 * @param lowered_stat status to use ie: lowered = true, raised = false
448 inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
450 assert(widget_index < this->nested_array_size);
451 this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
455 * Invert the lowered/raised status of a widget.
456 * @param widget_index index of this widget in the window
458 inline void ToggleWidgetLoweredState(byte widget_index)
460 assert(widget_index < this->nested_array_size);
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(byte 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(byte widget_index)
480 SetWidgetLoweredState(widget_index, false);
484 * Gets the lowered state of a widget.
485 * @param widget_index index of this widget in the window
486 * @return status of the widget ie: lowered = true, raised= false
488 inline bool IsWidgetLowered(byte widget_index) const
490 assert(widget_index < this->nested_array_size);
491 return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
494 void UnfocusFocusedWidget();
495 bool SetFocusedWidget(int widget_index);
497 EventState HandleEditBoxKey(int wid, WChar key, uint16 keycode);
498 virtual void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end);
500 void HandleButtonClick(byte widget);
501 int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
503 void RaiseButtons(bool autoraise = false);
504 void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
505 void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
506 void SetWidgetDirty(byte widget_index) const;
508 void DrawWidgets() const;
509 void DrawViewport() const;
510 void DrawSortButtonState(int widget, SortButtonState state) const;
511 static int SortButtonWidth();
513 void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
515 void SetDirty() const;
516 void ReInit(int rx = 0, int ry = 0);
518 /** Is window shaded currently? */
519 inline bool IsShaded() const
521 return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
524 void SetShaded(bool make_shaded);
526 void InvalidateData(int data = 0, bool gui_scope = true);
527 void ProcessScheduledInvalidations();
528 void ProcessHighlightedInvalidations();
530 /*** Event handling ***/
533 * Notification that the nested widget tree gets initialized. The event can be used to perform general computations.
534 * @note #nested_root and/or #nested_array (normally accessed via #GetWidget()) may not exist during this call.
536 virtual void OnInit() { }
538 virtual void ApplyDefaults();
541 * Compute the initial position of the window.
542 * @param sm_width Smallest width of the window.
543 * @param sm_height Smallest height of the window.
544 * @param window_number The window number of the new window.
545 * @return Initial position of the top-left corner of the window.
547 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number);
550 * The window must be repainted.
551 * @note This method should not change any state, it should only use drawing functions.
553 virtual void OnPaint()
555 this->DrawWidgets();
559 * Draw the contents of a nested widget.
560 * @param r Rectangle occupied by the widget.
561 * @param widget Number of the widget to draw.
562 * @note This method may not change any state, it may only use drawing functions.
564 virtual void DrawWidget(const Rect &r, int widget) const {}
567 * Update size and resize step of a widget in the window.
568 * After retrieval of the minimal size and the resize-steps of a widget, this function is called to allow further refinement,
569 * typically by computing the real maximal size of the content. Afterwards, \a size is taken to be the minimal size of the widget
570 * and \a resize is taken to contain the resize steps. For the convenience of the callee, \a padding contains the amount of
571 * padding between the content and the edge of the widget. This should be added to the returned size.
572 * @param widget Widget number.
573 * @param size Size of the widget.
574 * @param padding Recommended amount of space between the widget content and the widget edge.
575 * @param fill Fill step of the widget.
576 * @param resize Resize step of the widget.
578 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
581 * Initialize string parameters for a widget.
582 * Calls to this function are made during initialization to measure the size (that is as part of #InitNested()), during drawing,
583 * and while re-initializing the window. Only for widgets that render text initializing is requested.
584 * @param widget Widget number.
586 virtual void SetStringParameters(int widget) const {}
589 * Called when window gains focus
591 virtual void OnFocus() {}
593 virtual void OnFocusLost();
596 * A key has been pressed.
597 * @param key the Unicode value of the key.
598 * @param keycode the untranslated key code including shift state.
599 * @return #ES_HANDLED if the key press has been handled and no other
600 * window should receive the event.
602 virtual EventState OnKeyPress(WChar key, uint16 keycode) { return ES_NOT_HANDLED; }
604 virtual EventState OnHotkey(int hotkey);
607 * The state of the control key has changed
608 * @return #ES_HANDLED if the change has been handled and no other
609 * window should receive the event.
611 virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
615 * A click with the left mouse button has been made on the window.
616 * @param pt the point inside the window that has been clicked.
617 * @param widget the clicked widget.
618 * @param click_count Number of fast consecutive clicks at same position
620 virtual void OnClick(Point pt, int widget, int click_count) {}
623 * A click with the right mouse button has been made on the window.
624 * @param pt the point inside the window that has been clicked.
625 * @param widget the clicked widget.
626 * @return true if the click was actually handled, i.e. do not show a
627 * tooltip if tooltip-on-right-click is enabled.
629 virtual bool OnRightClick(Point pt, int widget) { return false; }
632 * The mouse is hovering over a widget in the window, perform an action for it, like opening a custom tooltip.
633 * @param pt The point where the mouse is hovering.
634 * @param widget The widget where the mouse is hovering.
636 virtual void OnHover(Point pt, int widget) {}
639 * An 'object' is being dragged at the provided position, highlight the target if possible.
640 * @param pt The point inside the window that the mouse hovers over.
641 * @param widget The widget the mouse hovers over.
643 virtual void OnMouseDrag(Point pt, int widget) {}
646 * A dragged 'object' has been released.
647 * @param pt the point inside the window where the release took place.
648 * @param widget the widget where the release took place.
650 virtual void OnDragDrop(Point pt, int widget) {}
653 * Handle the request for (viewport) scrolling.
654 * @param delta the amount the viewport must be scrolled.
656 virtual void OnScroll(Point delta) {}
659 * The mouse is currently moving over the window or has just moved outside
660 * of the window. In the latter case pt is (-1, -1).
661 * @param pt the point inside the window that the mouse hovers over.
662 * @param widget the widget the mouse hovers over.
664 virtual void OnMouseOver(Point pt, int widget) {}
667 * The mouse wheel has been turned.
668 * @param wheel the amount of movement of the mouse wheel.
670 virtual void OnMouseWheel(int wheel) {}
674 * Called for every mouse loop run, which is at least once per (game) tick.
676 virtual void OnMouseLoop() {}
679 * Called once per (game) tick.
681 virtual void OnTick() {}
684 * Called once every 100 (game) ticks.
686 virtual void OnHundredthTick() {}
689 * Called when this window's timeout has been reached.
691 virtual void OnTimeout() {}
695 * Called after the window got resized.
696 * For nested windows with a viewport, call NWidgetViewport::UpdateViewportCoordinates.
698 virtual void OnResize() {}
701 * A dropdown option associated to this window has been selected.
702 * @param widget the widget (button) that the dropdown is associated with.
703 * @param index the element in the dropdown that is selected.
705 virtual void OnDropdownSelect(int widget, int index) {}
707 virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close);
710 * The text in an editbox has been edited.
711 * @param widget The widget of the editbox.
713 virtual void OnEditboxChanged(int widget) {}
716 * The query window opened from this window has closed.
717 * @param str the new value of the string, NULL if the window
718 * was cancelled or an empty string when the default
719 * button was pressed, i.e. StrEmpty(str).
721 virtual void OnQueryTextFinished(char *str) {}
724 * Some data on this window has become invalid.
725 * @param data information about the changed data.
726 * @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.
728 virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
731 * The user clicked some place on the map when a tile highlight mode
732 * has been set.
733 * @param pt the exact point on the map that has been clicked.
734 * @param tile the tile on the map that has been clicked.
736 virtual void OnPlaceObject(Point pt, TileIndex tile) {}
739 * The user clicked on a vehicle while HT_VEHICLE has been set.
740 * @param v clicked vehicle. It is guaranteed to be v->IsPrimaryVehicle() == true
741 * @return True if the click is handled, false if it is ignored.
743 virtual bool OnVehicleSelect(const struct Vehicle *v) { return false; }
746 * The user cancelled a tile highlight mode that has been set.
748 virtual void OnPlaceObjectAbort() {}
752 * The user is dragging over the map when the tile highlight mode
753 * has been set.
754 * @param select_method the method of selection (allowed directions)
755 * @param select_proc what will be created when the drag is over.
756 * @param pt the exact point on the map where the mouse is.
758 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
761 * The user has dragged over the map when the tile highlight mode
762 * has been set.
763 * @param select_method the method of selection (allowed directions)
764 * @param select_proc what should be created.
765 * @param pt the exact point on the map where the mouse was released.
766 * @param start_tile the begin tile of the drag.
767 * @param end_tile the end tile of the drag.
769 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
772 * The user moves over the map when a tile highlight mode has been set
773 * when the special mouse mode has been set to 'PRESIZE' mode. An
774 * example of this is the tile highlight for dock building.
775 * @param pt the exact point on the map where the mouse is.
776 * @param tile the tile on the map where the mouse is.
778 virtual void OnPlacePresize(Point pt, TileIndex tile) {}
780 /*** End of the event handling ***/
783 * Is the data related to this window NewGRF inspectable?
784 * @return true iff it is inspectable.
786 virtual bool IsNewGRFInspectable() const { return false; }
789 * Show the NewGRF inspection window. When this function is called it is
790 * up to the window to call and pass the right parameters to the
791 * ShowInspectWindow function.
792 * @pre this->IsNewGRFInspectable()
794 virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
798 * Get the nested widget with number \a widnum from the nested widget tree.
799 * @tparam NWID Type of the nested widget.
800 * @param widnum Widget number of the widget to retrieve.
801 * @return The requested widget if it is instantiated, \c NULL otherwise.
803 template <class NWID>
804 inline NWID *Window::GetWidget(uint widnum)
806 if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
807 NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
808 assert(nwid != NULL);
809 return nwid;
812 /** Specialized case of #Window::GetWidget for the nested widget base class. */
813 template <>
814 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
816 if (widnum >= this->nested_array_size) return NULL;
817 return this->nested_array[widnum];
821 * Get the nested widget with number \a widnum from the nested widget tree.
822 * @tparam NWID Type of the nested widget.
823 * @param widnum Widget number of the widget to retrieve.
824 * @return The requested widget if it is instantiated, \c NULL otherwise.
826 template <class NWID>
827 inline const NWID *Window::GetWidget(uint widnum) const
829 return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
834 * Base class for windows opened from a toolbar.
836 class PickerWindowBase : public Window {
838 public:
839 PickerWindowBase(WindowDesc *desc, Window *parent) : Window(desc)
841 this->parent = parent;
844 virtual ~PickerWindowBase();
847 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
848 Window *FindWindowFromPt(int x, int y);
851 * Open a new window.
852 * @tparam Wcls %Window class to use if the window does not exist.
853 * @param desc The pointer to the WindowDesc to be created
854 * @param window_number the window number of the new window
855 * @param return_existing If set, also return the window if it already existed.
856 * @return %Window pointer of the newly created window, or the existing one if \a return_existing is set, or \c NULL.
858 template <typename Wcls>
859 Wcls *AllocateWindowDescFront(WindowDesc *desc, int window_number, bool return_existing = false)
861 Wcls *w = static_cast<Wcls *>(BringWindowToFrontById(desc->cls, window_number));
862 if (w != NULL) return return_existing ? w : NULL;
863 return new Wcls(desc, window_number);
866 void RelocateAllWindows(int neww, int newh);
868 /* misc_gui.cpp */
869 enum TooltipCloseCondition {
870 TCC_RIGHT_CLICK,
871 TCC_LEFT_CLICK,
872 TCC_HOVER,
875 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
877 /* widget.cpp */
878 int GetWidgetFromPos(const Window *w, int x, int y);
880 /** Iterate over all windows */
881 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
882 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
883 #define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
884 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
886 extern Point _cursorpos_drag_start;
888 extern int _scrollbar_start_pos;
889 extern int _scrollbar_size;
890 extern byte _scroller_click_timeout;
892 extern bool _scrolling_viewport;
893 extern bool _mouse_hovering;
895 /** Mouse modes. */
896 enum SpecialMouseMode {
897 WSM_NONE, ///< No special mouse mode.
898 WSM_DRAGDROP, ///< Dragging an object.
899 WSM_SIZING, ///< Sizing mode.
900 WSM_PRESIZE, ///< Presizing mode (docks, tunnels).
902 extern SpecialMouseMode _special_mouse_mode;
904 void SetFocusedWindow(Window *w);
906 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
908 #endif /* WINDOW_GUI_H */