Factor out OnClick dispatching in Window::HandleEditBoxKey
[openttd/fttd.git] / src / window_gui.h
blob3d2433c3afcb69e6344ab212769670dfbdfbc244
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 "map/coord.h"
16 #include "vehicle_type.h"
17 #include "viewport_type.h"
18 #include "company_type.h"
19 #include "widget_type.h"
20 #include "core/smallvec_type.hpp"
21 #include "core/smallmap_type.hpp"
22 #include "string.h"
23 #include "gfx_func.h"
25 /**
26 * Flags to describe the look of the frame
28 enum FrameFlags {
29 FR_NONE = 0,
30 FR_TRANSPARENT = 1 << 0, ///< Makes the background transparent if set
31 FR_BORDERONLY = 1 << 4, ///< Draw border only, no background
32 FR_LOWERED = 1 << 5, ///< If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
33 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)
36 DECLARE_ENUM_AS_BIT_SET(FrameFlags)
38 /** Distances used in drawing widgets. */
39 enum WidgetDrawDistances {
40 /* WWT_IMGBTN(_2) */
41 WD_IMGBTN_LEFT = 1, ///< Left offset of the image in the button.
42 WD_IMGBTN_RIGHT = 1, ///< Right offset of the image in the button.
43 WD_IMGBTN_TOP = 1, ///< Top offset of image in the button.
44 WD_IMGBTN_BOTTOM = 1, ///< Bottom offset of image in the button.
46 /* WWT_INSET */
47 WD_INSET_LEFT = 2, ///< Left offset of string.
48 WD_INSET_RIGHT = 2, ///< Right offset of string.
49 WD_INSET_TOP = 1, ///< Top offset of string.
51 WD_SCROLLBAR_LEFT = 2, ///< Left offset of scrollbar.
52 WD_SCROLLBAR_RIGHT = 2, ///< Right offset of scrollbar.
53 WD_SCROLLBAR_TOP = 2, ///< Top offset of scrollbar.
54 WD_SCROLLBAR_BOTTOM = 2, ///< Bottom offset of scrollbar.
56 /* Size of the pure frame bevel without any padding. */
57 WD_BEVEL_LEFT = 1, ///< Width of left bevel border.
58 WD_BEVEL_RIGHT = 1, ///< Width of right bevel border.
59 WD_BEVEL_TOP = 1, ///< Height of top bevel border.
60 WD_BEVEL_BOTTOM = 1, ///< Height of bottom bevel border.
62 /* FrameRect widgets, all text buttons, panel, editbox */
63 WD_FRAMERECT_LEFT = 2, ///< Offset at left to draw the frame rectangular area
64 WD_FRAMERECT_RIGHT = 2, ///< Offset at right to draw the frame rectangular area
65 WD_FRAMERECT_TOP = 1, ///< Offset at top to draw the frame rectangular area
66 WD_FRAMERECT_BOTTOM = 1, ///< Offset at bottom to draw the frame rectangular area
68 /* Extra space at top/bottom of text panels */
69 WD_TEXTPANEL_TOP = 6, ///< Offset at top to draw above the text
70 WD_TEXTPANEL_BOTTOM = 6, ///< Offset at bottom to draw below the text
72 /* WWT_FRAME */
73 WD_FRAMETEXT_LEFT = 6, ///< Left offset of the text of the frame.
74 WD_FRAMETEXT_RIGHT = 6, ///< Right offset of the text of the frame.
75 WD_FRAMETEXT_TOP = 6, ///< Top offset of the text of the frame
76 WD_FRAMETEXT_BOTTOM = 6, ///< Bottom offset of the text of the frame
78 /* WWT_MATRIX */
79 WD_MATRIX_LEFT = 2, ///< Offset at left of a matrix cell.
80 WD_MATRIX_RIGHT = 2, ///< Offset at right of a matrix cell.
81 WD_MATRIX_TOP = 3, ///< Offset at top of a matrix cell.
82 WD_MATRIX_BOTTOM = 1, ///< Offset at bottom of a matrix cell.
84 /* WWT_SHADEBOX */
85 WD_SHADEBOX_WIDTH = 12, ///< Width of a standard shade box widget.
86 WD_SHADEBOX_LEFT = 2, ///< Left offset of shade sprite.
87 WD_SHADEBOX_RIGHT = 2, ///< Right offset of shade sprite.
88 WD_SHADEBOX_TOP = 3, ///< Top offset of shade sprite.
89 WD_SHADEBOX_BOTTOM = 3, ///< Bottom offset of shade sprite.
91 /* WWT_STICKYBOX */
92 WD_STICKYBOX_WIDTH = 12, ///< Width of a standard sticky box widget.
93 WD_STICKYBOX_LEFT = 2, ///< Left offset of sticky sprite.
94 WD_STICKYBOX_RIGHT = 2, ///< Right offset of sticky sprite.
95 WD_STICKYBOX_TOP = 3, ///< Top offset of sticky sprite.
96 WD_STICKYBOX_BOTTOM = 3, ///< Bottom offset of sticky sprite.
98 /* WWT_DEBUGBOX */
99 WD_DEBUGBOX_WIDTH = 12, ///< Width of a standard debug box widget.
100 WD_DEBUGBOX_LEFT = 2, ///< Left offset of debug sprite.
101 WD_DEBUGBOX_RIGHT = 2, ///< Right offset of debug sprite.
102 WD_DEBUGBOX_TOP = 3, ///< Top offset of debug sprite.
103 WD_DEBUGBOX_BOTTOM = 3, ///< Bottom offset of debug sprite.
105 /* WWT_DEFSIZEBOX */
106 WD_DEFSIZEBOX_WIDTH = 12, ///< Width of a standard defsize box widget.
107 WD_DEFSIZEBOX_LEFT = 2, ///< Left offset of defsize sprite.
108 WD_DEFSIZEBOX_RIGHT = 2, ///< Right offset of defsize sprite.
109 WD_DEFSIZEBOX_TOP = 3, ///< Top offset of defsize sprite.
110 WD_DEFSIZEBOX_BOTTOM = 3, ///< Bottom offset of defsize sprite.
112 /* WWT_RESIZEBOX */
113 WD_RESIZEBOX_WIDTH = 12, ///< Width of a resize box widget.
114 WD_RESIZEBOX_LEFT = 2, ///< Left offset of resize sprite.
115 WD_RESIZEBOX_RIGHT = 2, ///< Right offset of resize sprite.
116 WD_RESIZEBOX_TOP = 2, ///< Top offset of resize sprite.
117 WD_RESIZEBOX_BOTTOM = 2, ///< Bottom offset of resize sprite.
119 /* WWT_CLOSEBOX */
120 WD_CLOSEBOX_WIDTH = 11, ///< Width of a close box widget.
121 WD_CLOSEBOX_LEFT = 2, ///< Left offset of closebox string.
122 WD_CLOSEBOX_RIGHT = 1, ///< Right offset of closebox string.
123 WD_CLOSEBOX_TOP = 2, ///< Top offset of closebox string.
124 WD_CLOSEBOX_BOTTOM = 2, ///< Bottom offset of closebox string.
126 /* WWT_CAPTION */
127 WD_CAPTION_HEIGHT = 14, ///< Height of a title bar.
128 WD_CAPTIONTEXT_LEFT = 2, ///< Offset of the caption text at the left.
129 WD_CAPTIONTEXT_RIGHT = 2, ///< Offset of the caption text at the right.
130 WD_CAPTIONTEXT_TOP = 2, ///< Offset of the caption text at the top.
131 WD_CAPTIONTEXT_BOTTOM = 2, ///< Offset of the caption text at the bottom.
133 /* Dropdown widget. */
134 WD_DROPDOWN_HEIGHT = 12, ///< Height of a drop down widget.
135 WD_DROPDOWNTEXT_LEFT = 2, ///< Left offset of the dropdown widget string.
136 WD_DROPDOWNTEXT_RIGHT = 2, ///< Right offset of the dropdown widget string.
137 WD_DROPDOWNTEXT_TOP = 1, ///< Top offset of the dropdown widget string.
138 WD_DROPDOWNTEXT_BOTTOM = 1, ///< Bottom offset of the dropdown widget string.
140 WD_PAR_VSEP_NORMAL = 2, ///< Normal amount of vertical space between two paragraphs of text.
141 WD_PAR_VSEP_WIDE = 8, ///< Large amount of vertical space between two paragraphs of text.
144 /* widget.cpp */
145 void DrawFrameRect (BlitArea *dpi, int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
146 void DrawCaption (BlitArea *dpi, const Rect &r, Colours colour, Owner owner, StringID str);
148 /* window.cpp */
149 extern Window *_z_front_window;
150 extern Window *_z_back_window;
151 extern Window *_focused_window;
154 /** How do we the window to be placed? */
155 enum WindowPosition {
156 WDP_MANUAL, ///< Manually align the window (so no automatic location finding)
157 WDP_AUTO, ///< Find a place automatically
158 WDP_CENTER, ///< Center the window
159 WDP_ALIGN_TOOLBAR, ///< Align toward the toolbar
162 Point GetToolbarAlignedWindowPosition(int window_width);
164 struct HotkeyList;
167 * High level window description
169 struct WindowDesc {
170 struct Prefs {
171 const char *const key; ///< Key to store window defaults in the config file.
172 int16 pref_width; ///< User-preferred width of the window. Zero if unset.
173 int16 pref_height; ///< User-preferred height of the window. Zero if unset.
174 bool pref_sticky; ///< Preferred stickyness.
176 Prefs (const char *key);
177 ~Prefs();
180 /** Window description constructor. */
181 CONSTEXPR WindowDesc (WindowPosition def_pos, int16 def_width_trad, int16 def_height_trad,
182 WindowClass window_class, WindowClass parent_class, uint32 flags,
183 const NWidgetPart *nwid_parts, int16 nwid_length,
184 Prefs *prefs = NULL, HotkeyList *hotkeys = NULL) :
185 default_pos(def_pos),
186 cls(window_class),
187 parent_cls(parent_class),
188 flags(flags),
189 nwid_parts(nwid_parts),
190 nwid_length(nwid_length),
191 prefs(prefs),
192 hotkeys(hotkeys),
193 default_width_trad(def_width_trad),
194 default_height_trad(def_height_trad)
198 WindowPosition default_pos; ///< Preferred position of the window. @see WindowPosition()
199 WindowClass cls; ///< Class of the window, @see WindowClass.
200 WindowClass parent_cls; ///< Class of the parent window. @see WindowClass
201 uint32 flags; ///< Flags. @see WindowDefaultFlag
202 const NWidgetPart *nwid_parts; ///< Nested widget parts describing the window.
203 int16 nwid_length; ///< Length of the #nwid_parts array.
204 Prefs *prefs; ///< Preferred dimensions and state.
205 HotkeyList *hotkeys; ///< Hotkeys for the window.
207 int16 GetDefaultWidth() const;
208 int16 GetDefaultHeight() const;
210 static void LoadFromConfig();
211 static void SaveToConfig();
213 private:
214 int16 default_width_trad; ///< Preferred initial width of the window (pixels at 1x zoom).
215 int16 default_height_trad; ///< Preferred initial height of the window (pixels at 1x zoom).
218 * Dummy private copy constructor to prevent compilers from
219 * copying the structure, which fails due to _window_descs.
221 WindowDesc(const WindowDesc &other);
225 * Window default widget/window handling flags
227 enum WindowDefaultFlag {
228 WDF_CONSTRUCTION = 1 << 0, ///< This window is used for construction; close it whenever changing company.
229 WDF_MODAL = 1 << 1, ///< The window is a modal child of some other window, meaning the parent is 'inactive'
230 WDF_NO_FOCUS = 1 << 2, ///< This window won't get focus/make any other window lose focus when click
234 * Data structure for resizing a window
236 struct ResizeInfo {
237 uint step_width; ///< Step-size of width resize changes
238 uint step_height; ///< Step-size of height resize changes
241 /** State of a sort direction button. */
242 enum SortButtonState {
243 SBS_OFF, ///< Do not sort (with this button).
244 SBS_DOWN, ///< Sort ascending.
245 SBS_UP, ///< Sort descending.
249 * Window flags.
251 enum WindowFlags {
252 WF_TIMEOUT = 1 << 0, ///< Window timeout counter.
254 WF_DRAGGING = 1 << 3, ///< Window is being dragged.
255 WF_SIZING_RIGHT = 1 << 4, ///< Window is being resized towards the right.
256 WF_SIZING_LEFT = 1 << 5, ///< Window is being resized towards the left.
257 WF_SIZING = WF_SIZING_RIGHT | WF_SIZING_LEFT, ///< Window is being resized.
258 WF_STICKY = 1 << 6, ///< Window is made sticky by user
259 WF_DISABLE_VP_SCROLL = 1 << 7, ///< Window does not do autoscroll, @see HandleAutoscroll().
260 WF_WHITE_BORDER = 1 << 8, ///< Window white border counter bit mask.
261 WF_HIGHLIGHTED = 1 << 9, ///< Window has a widget that has a highlight.
262 WF_CENTERED = 1 << 10, ///< Window is centered and shall stay centered after ReInit.
264 DECLARE_ENUM_AS_BIT_SET(WindowFlags)
266 static const int TIMEOUT_DURATION = 7; ///< The initial timeout value for WF_TIMEOUT.
267 static const int WHITE_BORDER_DURATION = 3; ///< The initial timeout value for WF_WHITE_BORDER.
270 * Data structure for a window viewport.
271 * A viewport is either following a vehicle (its id in then in #follow_vehicle), or it aims to display a specific
272 * location #dest_scrollpos_x, #dest_scrollpos_y (#follow_vehicle is then #INVALID_VEHICLE).
273 * The actual location being shown is #scrollpos_x, #scrollpos_y.
274 * @see InitializeViewport(), UpdateViewportPosition(), UpdateViewportCoordinates().
276 struct ViewportData : ViewPort {
277 VehicleID follow_vehicle; ///< VehicleID to follow if following a vehicle, #INVALID_VEHICLE otherwise.
278 int32 scrollpos_x; ///< Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
279 int32 scrollpos_y; ///< Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
280 int32 dest_scrollpos_x; ///< Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewport).
281 int32 dest_scrollpos_y; ///< Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewport).
284 struct QueryString;
287 * Data structure for an opened window
289 struct Window {
290 protected:
291 virtual void FindWindowPlacementAndResize(int def_width, int def_height);
293 SmallVector<int, 4> scheduled_invalidation_data; ///< Data of scheduled OnInvalidateData() calls.
295 public:
296 Window (const WindowDesc *desc);
298 virtual ~Window();
301 * Helper allocation function to disallow something.
302 * Don't allow arrays; arrays of Windows are pointless as you need
303 * to destruct them all at the same time too, which is kinda hard.
304 * @param size the amount of space not to allocate
306 inline void *operator new[](size_t size)
308 NOT_REACHED();
312 * Memory release for an array of class instances.
313 * @param ptr the memory to free.
315 inline void operator delete[](void *ptr)
317 NOT_REACHED();
320 const WindowDesc *window_desc; ///< Window description
321 WindowFlags flags; ///< Window flags
322 WindowClass window_class; ///< Window class
323 WindowNumber window_number; ///< Window number within the window class
325 uint8 timeout_timer; ///< Timer value of the WF_TIMEOUT for flags.
326 uint8 white_border_timer; ///< Timer value of the WF_WHITE_BORDER for flags.
328 int left; ///< x position of left edge of the window
329 int top; ///< y position of top edge of the window
330 int width; ///< width of the window (number of pixels to the right in x direction)
331 int height; ///< Height of the window (number of pixels down in y direction)
333 ResizeInfo resize; ///< Resize information
335 Owner owner; ///< The owner of the content shown in this window. Company colour is acquired from this variable.
337 ViewportData *viewport; ///< Pointer to viewport data, if present.
338 const NWidgetCore *nested_focus; ///< Currently focused nested widget, or \c NULL if no nested widget has focus.
339 SmallMap<int, QueryString*> querystrings; ///< QueryString associated to WWT_EDITBOX widgets.
340 NWidgetBase *nested_root; ///< Root of the nested tree.
341 NWidgetBase **nested_array; ///< Array of pointers into the tree. Do not access directly, use #Window::GetWidget() instead.
342 uint nested_array_size; ///< Size of the nested array.
343 NWidgetStacked *shade_select; ///< Selection widget (#NWID_SELECTION) to use for shading the window. If \c NULL, window cannot shade.
344 Dimension unshaded_size; ///< Last known unshaded size (only valid while shaded).
346 int scrolling_scrollbar; ///< Widgetindex of just being dragged scrollbar. -1 if none is active.
348 Window *parent; ///< Parent window.
349 Window *z_front; ///< The window in front of us in z-order.
350 Window *z_back; ///< The window behind us in z-order.
352 template <class NWID>
353 inline const NWID *GetWidget(uint widnum) const;
354 template <class NWID>
355 inline NWID *GetWidget(uint widnum);
357 const Scrollbar *GetScrollbar(uint widnum) const;
358 Scrollbar *GetScrollbar(uint widnum);
360 const QueryString *GetQueryString(uint widnum) const;
361 QueryString *GetQueryString(uint widnum);
363 virtual const char *GetFocusedText() const;
364 virtual const char *GetCaret() const;
365 virtual const char *GetMarkedText(size_t *length) const;
366 virtual Point GetCaretPosition() const;
367 virtual Rect GetTextBoundingRect(const char *from, const char *to) const;
368 virtual const char *GetTextCharacterAtPosition(const Point &pt) const;
370 void InitNested(WindowNumber number = 0);
371 void CreateNestedTree(void);
373 void Delete (void);
376 * Set the timeout flag of the window and initiate the timer.
378 inline void SetTimeout()
380 this->flags |= WF_TIMEOUT;
381 this->timeout_timer = TIMEOUT_DURATION;
385 * Set the timeout flag of the window and initiate the timer.
387 inline void SetWhiteBorder()
389 this->flags |= WF_WHITE_BORDER;
390 this->white_border_timer = WHITE_BORDER_DURATION;
393 void DisableAllWidgetHighlight();
394 void SetWidgetHighlight(byte widget_index, TextColour highlighted_colour);
395 bool IsWidgetHighlighted(byte widget_index) const;
398 * Sets the enabled/disabled status of a widget.
399 * By default, widgets are enabled.
400 * On certain conditions, they have to be disabled.
401 * @param widget_index index of this widget in the window
402 * @param disab_stat status to use ie: disabled = true, enabled = false
404 inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
406 assert(widget_index < this->nested_array_size);
407 if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
411 * Sets a widget to disabled.
412 * @param widget_index index of this widget in the window
414 inline void DisableWidget(byte widget_index)
416 SetWidgetDisabledState(widget_index, true);
420 * Sets a widget to Enabled.
421 * @param widget_index index of this widget in the window
423 inline void EnableWidget(byte widget_index)
425 SetWidgetDisabledState(widget_index, false);
429 * Gets the enabled/disabled status of a widget.
430 * @param widget_index index of this widget in the window
431 * @return status of the widget ie: disabled = true, enabled = false
433 inline bool IsWidgetDisabled(byte widget_index) const
435 assert(widget_index < this->nested_array_size);
436 return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
440 * Check if given widget is focused within this window
441 * @param widget_index : index of the widget in the window to check
442 * @return true if given widget is the focused window in this window
444 inline bool IsWidgetFocused(byte widget_index) const
446 return this->nested_focus != NULL && this->nested_focus->index == widget_index;
450 * Check if given widget has user input focus. This means that both the window
451 * has focus and that the given widget has focus within the window.
452 * @param widget_index : index of the widget in the window to check
453 * @return true if given widget is the focused window in this window and this window has focus
455 inline bool IsWidgetGloballyFocused(byte widget_index) const
457 return _focused_window == this && IsWidgetFocused(widget_index);
461 * Sets the lowered/raised status of a widget.
462 * @param widget_index index of this widget in the window
463 * @param lowered_stat status to use ie: lowered = true, raised = false
465 inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
467 assert(widget_index < this->nested_array_size);
468 this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
472 * Invert the lowered/raised status of a widget.
473 * @param widget_index index of this widget in the window
475 inline void ToggleWidgetLoweredState(byte widget_index)
477 assert(widget_index < this->nested_array_size);
478 bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
479 this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
483 * Marks a widget as lowered.
484 * @param widget_index index of this widget in the window
486 inline void LowerWidget(byte widget_index)
488 SetWidgetLoweredState(widget_index, true);
492 * Marks a widget as raised.
493 * @param widget_index index of this widget in the window
495 inline void RaiseWidget(byte widget_index)
497 SetWidgetLoweredState(widget_index, false);
501 * Gets the lowered state of a widget.
502 * @param widget_index index of this widget in the window
503 * @return status of the widget ie: lowered = true, raised= false
505 inline bool IsWidgetLowered(byte widget_index) const
507 assert(widget_index < this->nested_array_size);
508 return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
511 void UnfocusFocusedWidget();
512 bool SetFocusedWidget(int widget_index);
514 EventState HandleEditBoxKey(int wid, WChar key, uint16 keycode);
515 virtual void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end);
517 void HandleButtonClick(byte widget);
518 int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
520 void RaiseButtons(bool autoraise = false);
521 void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
522 void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
523 void SetWidgetDirty(byte widget_index) const;
525 void DrawWidgets (BlitArea *dpi) const;
526 void DrawViewport (BlitArea *dpi) const;
527 void DrawSortButtonState (BlitArea *dpi, int widget, SortButtonState state) const;
528 static int SortButtonWidth();
530 void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
532 void SetDirty() const;
533 void ReInit(int rx = 0, int ry = 0);
535 /** Is window shaded currently? */
536 inline bool IsShaded() const
538 return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
541 void SetShaded(bool make_shaded);
543 void InvalidateData(int data = 0, bool gui_scope = true);
544 void ProcessScheduledInvalidations();
545 void ProcessHighlightedInvalidations();
547 /*** Event handling ***/
550 * Notification that the nested widget tree gets initialized. The event can be used to perform general computations.
551 * @note #nested_root and/or #nested_array (normally accessed via #GetWidget()) may not exist during this call.
553 virtual void OnInit() { }
555 /** Notification that the window is about to be deleted. */
556 virtual void OnDelete (void) { }
559 * Compute the initial position of the window.
560 * @param sm_width Smallest width of the window.
561 * @param sm_height Smallest height of the window.
562 * @param window_number The window number of the new window.
563 * @return Initial position of the top-left corner of the window.
565 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number);
568 * The window must be repainted.
569 * @note This method should not change any state, it should only use drawing functions.
571 virtual void OnPaint (BlitArea *dpi)
573 this->DrawWidgets (dpi);
577 * Draw the contents of a nested widget.
578 * @param dpi Area to draw on.
579 * @param r Rectangle occupied by the widget.
580 * @param widget Number of the widget to draw.
581 * @note This method may not change any state, it may only use drawing functions.
583 virtual void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const
588 * Update size and resize step of a widget in the window.
589 * After retrieval of the minimal size and the resize-steps of a widget, this function is called to allow further refinement,
590 * typically by computing the real maximal size of the content. Afterwards, \a size is taken to be the minimal size of the widget
591 * and \a resize is taken to contain the resize steps. For the convenience of the callee, \a padding contains the amount of
592 * padding between the content and the edge of the widget. This should be added to the returned size.
593 * @param widget Widget number.
594 * @param size Size of the widget.
595 * @param padding Recommended amount of space between the widget content and the widget edge.
596 * @param fill Fill step of the widget.
597 * @param resize Resize step of the widget.
599 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
602 * Initialize string parameters for a widget.
603 * Calls to this function are made during initialization to measure the size (that is as part of #InitNested()), during drawing,
604 * and while re-initializing the window. Only for widgets that render text initializing is requested.
605 * @param widget Widget number.
607 virtual void SetStringParameters(int widget) const {}
610 * Called when window gains focus
612 virtual void OnFocus() {}
614 virtual void OnFocusLost();
617 * A key has been pressed.
618 * @param key the Unicode value of the key.
619 * @param keycode the untranslated key code including shift state.
620 * @return Whether the key press has been handled and no other
621 * window should receive the event.
623 virtual bool OnKeyPress (WChar key, uint16 keycode)
625 return false;
628 virtual bool OnHotkey (int hotkey);
631 * The state of the control key has changed
632 * @return Whether the change has been handled and no other
633 * window should receive the event.
635 virtual bool OnCTRLStateChange (void)
637 return false;
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 * The text in an editbox has been edited.
738 * @param widget The widget of the editbox.
740 virtual void OnEditboxChanged(int widget) {}
743 * The query window opened from this window has closed.
744 * @param str the new value of the string, NULL if the window
745 * was cancelled or an empty string when the default
746 * button was pressed, i.e. StrEmpty(str).
748 virtual void OnQueryTextFinished(char *str) {}
751 * Some data on this window has become invalid.
752 * @param data information about the changed data.
753 * @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.
755 virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
758 * The user clicked some place on the map when a tile highlight mode
759 * has been set.
760 * @param pt the exact point on the map that has been clicked.
761 * @param tile the tile on the map that has been clicked.
763 virtual void OnPlaceObject(Point pt, TileIndex tile) {}
766 * The user clicked on a vehicle.
767 * @param v clicked vehicle. It is guaranteed to be v->IsPrimaryVehicle() == true
768 * @return True if the click is handled, false if it is ignored.
770 virtual bool OnVehicleSelect(const struct Vehicle *v) { return false; }
773 * The user cancelled a tile highlight mode that has been set.
775 virtual void OnPlaceObjectAbort() {}
779 * The user is dragging over the map when the tile highlight mode
780 * has been set.
781 * @param userdata data set by the function that started the selection
782 * @param pt the exact point on the map where the mouse is.
783 * @return Whether the tile selection should be updated.
785 virtual bool OnPlaceDrag (int userdata, Point pt) { return true; }
788 * The user has dragged over the map when the tile highlight mode
789 * has been set.
790 * @param userdata data set by the function that started the selection
791 * @param pt the exact point on the map where the mouse was released.
792 * @param start_tile the begin tile of the drag.
793 * @param end_tile the end tile of the drag.
795 virtual void OnPlaceMouseUp (int userdata, Point pt, TileIndex start_tile, TileIndex end_tile) {}
798 * The user moves over the map when a tile highlight mode has been set
799 * when the special mouse mode has been set to 'PRESIZE' mode. An
800 * example of this is the tile highlight for dock building.
801 * @param tile the tile on the map where the mouse is (in), start of selection (out)
802 * @param tile2 the tile on the map where the mouse is (in), end of selection (out)
804 virtual void OnPlacePresize (TileIndex *tile, TileIndex *tile2) {}
806 /*** End of the event handling ***/
809 * Is the data related to this window NewGRF inspectable?
810 * @return true iff it is inspectable.
812 virtual bool IsNewGRFInspectable() const { return false; }
815 * Show the NewGRF inspection window. When this function is called it is
816 * up to the window to call and pass the right parameters to the
817 * ShowInspectWindow function.
818 * @pre this->IsNewGRFInspectable()
820 virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
824 * Get the nested widget with number \a widnum from the nested widget tree.
825 * @tparam NWID Type of the nested widget.
826 * @param widnum Widget number of the widget to retrieve.
827 * @return The requested widget if it is instantiated, \c NULL otherwise.
829 template <class NWID>
830 inline NWID *Window::GetWidget(uint widnum)
832 if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
833 NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
834 assert(nwid != NULL);
835 return nwid;
838 /** Specialized case of #Window::GetWidget for the nested widget base class. */
839 template <>
840 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
842 if (widnum >= this->nested_array_size) return NULL;
843 return this->nested_array[widnum];
847 * Get the nested widget with number \a widnum from the nested widget tree.
848 * @tparam NWID Type of the nested widget.
849 * @param widnum Widget number of the widget to retrieve.
850 * @return The requested widget if it is instantiated, \c NULL otherwise.
852 template <class NWID>
853 inline const NWID *Window::GetWidget(uint widnum) const
855 return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
860 * Base class for windows opened from a toolbar.
862 class PickerWindowBase : public Window {
864 public:
865 PickerWindowBase (const WindowDesc *desc, Window *parent)
866 : Window(desc)
868 this->parent = parent;
871 void OnDelete (void) OVERRIDE;
874 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
875 Window *FindWindowFromPt(int x, int y);
878 * Open a new window.
879 * @tparam Wcls %Window class to use if the window does not exist.
880 * @param desc The pointer to the WindowDesc to be created
881 * @param window_number the window number of the new window
882 * @param return_existing If set, also return the window if it already existed.
883 * @return %Window pointer of the newly created window, or the existing one if \a return_existing is set, or \c NULL.
885 template <typename Wcls>
886 Wcls *AllocateWindowDescFront (const WindowDesc *desc, int window_number, bool return_existing = false)
888 Wcls *w = static_cast<Wcls *>(BringWindowToFrontById(desc->cls, window_number));
889 if (w != NULL) return return_existing ? w : NULL;
890 return new Wcls(desc, window_number);
893 void RelocateAllWindows(int neww, int newh);
895 /* misc_gui.cpp */
896 enum TooltipCloseCondition {
897 TCC_RIGHT_CLICK,
898 TCC_LEFT_CLICK,
899 TCC_HOVER,
902 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
904 /* widget.cpp */
905 int GetWidgetFromPos(const Window *w, int x, int y);
907 /** Iterate over all windows */
908 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
909 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
910 #define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
911 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
913 extern Point _cursorpos_drag_start;
915 extern int _scrollbar_start_pos;
916 extern int _scrollbar_size;
917 extern byte _scroller_click_timeout;
919 extern bool _scrolling_viewport;
920 extern bool _mouse_hovering;
922 /** Pointer modes. */
923 enum PointerMode {
924 POINTER_NONE, ///< Normal pointer mode.
925 POINTER_TILE, ///< Highlight the tile under the pointer.
926 POINTER_CORNER, ///< Highlight the corner under the pointer.
927 POINTER_AREA, ///< Highlight a custom tile area (docks, tunnels).
928 POINTER_RAIL_X, ///< Rail construction highlighting, X axis.
929 POINTER_RAIL_Y, ///< Rail construction highlighting, Y axis.
930 POINTER_RAIL_H, ///< Rail construction highlighting, horizontal.
931 POINTER_RAIL_V, ///< Rail construction highlighting, vertical.
932 POINTER_RAIL_AUTO, ///< Autorail highlighting.
933 POINTER_DRAG, ///< Drag a vehicle.
934 POINTER_VEHICLE, ///< Normal pointer mode, but allow to select a vehicle.
935 POINTER_TILE_VEHICLE, ///< Highlight the tile under the pointer and allow to select a vehicle.
937 POINTER_RAIL_FIRST = POINTER_RAIL_X,
938 POINTER_RAIL_LAST = POINTER_RAIL_AUTO,
940 extern PointerMode _pointer_mode;
942 void SetFocusedWindow(Window *w);
944 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
946 #endif /* WINDOW_GUI_H */