Rework ChoosePylonPosition to simplify the loop
[openttd/fttd.git] / src / window_gui.h
blob99a2174a6989dc893f8a991f53f288468d7e9fe6
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 #ES_HANDLED if the key press has been handled and no other
621 * window should receive the event.
623 virtual EventState OnKeyPress(WChar key, uint16 keycode) { return ES_NOT_HANDLED; }
625 virtual EventState OnHotkey(int hotkey);
628 * The state of the control key has changed
629 * @return #ES_HANDLED if the change has been handled and no other
630 * window should receive the event.
632 virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
636 * A click with the left mouse button has been made on the window.
637 * @param pt the point inside the window that has been clicked.
638 * @param widget the clicked widget.
639 * @param click_count Number of fast consecutive clicks at same position
641 virtual void OnClick(Point pt, int widget, int click_count) {}
644 * A click with the right mouse button has been made on the window.
645 * @param pt the point inside the window that has been clicked.
646 * @param widget the clicked widget.
647 * @return true if the click was actually handled, i.e. do not show a
648 * tooltip if tooltip-on-right-click is enabled.
650 virtual bool OnRightClick(Point pt, int widget) { return false; }
653 * The mouse is hovering over a widget in the window, perform an action for it, like opening a custom tooltip.
654 * @param pt The point where the mouse is hovering.
655 * @param widget The widget where the mouse is hovering.
657 virtual void OnHover(Point pt, int widget) {}
660 * An 'object' is being dragged at the provided position, highlight the target if possible.
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 OnMouseDrag(Point pt, int widget) {}
667 * A dragged 'object' has been released.
668 * @param pt the point inside the window where the release took place.
669 * @param widget the widget where the release took place.
671 virtual void OnDragDrop(Point pt, int widget) {}
674 * Handle the request for (viewport) scrolling.
675 * @param delta the amount the viewport must be scrolled.
677 virtual void OnScroll(Point delta) {}
680 * The mouse is currently moving over the window or has just moved outside
681 * of the window. In the latter case pt is (-1, -1).
682 * @param pt the point inside the window that the mouse hovers over.
683 * @param widget the widget the mouse hovers over.
685 virtual void OnMouseOver(Point pt, int widget) {}
688 * The mouse wheel has been turned.
689 * @param wheel the amount of movement of the mouse wheel.
691 virtual void OnMouseWheel(int wheel) {}
695 * Called for every mouse loop run, which is at least once per (game) tick.
697 virtual void OnMouseLoop() {}
700 * Called once per (game) tick.
702 virtual void OnTick() {}
705 * Called once every 100 (game) ticks.
707 virtual void OnHundredthTick() {}
710 * Called when this window's timeout has been reached.
712 virtual void OnTimeout() {}
716 * Called after the window got resized.
717 * For nested windows with a viewport, call NWidgetViewport::UpdateViewportCoordinates.
719 virtual void OnResize() {}
722 * A dropdown option associated to this window has been selected.
723 * @param widget the widget (button) that the dropdown is associated with.
724 * @param index the element in the dropdown that is selected.
726 virtual void OnDropdownSelect(int widget, int index) {}
728 virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close);
731 * The text in an editbox has been edited.
732 * @param widget The widget of the editbox.
734 virtual void OnEditboxChanged(int widget) {}
737 * The query window opened from this window has closed.
738 * @param str the new value of the string, NULL if the window
739 * was cancelled or an empty string when the default
740 * button was pressed, i.e. StrEmpty(str).
742 virtual void OnQueryTextFinished(char *str) {}
745 * Some data on this window has become invalid.
746 * @param data information about the changed data.
747 * @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.
749 virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
752 * The user clicked some place on the map when a tile highlight mode
753 * has been set.
754 * @param pt the exact point on the map that has been clicked.
755 * @param tile the tile on the map that has been clicked.
757 virtual void OnPlaceObject(Point pt, TileIndex tile) {}
760 * The user clicked on a vehicle.
761 * @param v clicked vehicle. It is guaranteed to be v->IsPrimaryVehicle() == true
762 * @return True if the click is handled, false if it is ignored.
764 virtual bool OnVehicleSelect(const struct Vehicle *v) { return false; }
767 * The user cancelled a tile highlight mode that has been set.
769 virtual void OnPlaceObjectAbort() {}
773 * The user is dragging over the map when the tile highlight mode
774 * has been set.
775 * @param userdata data set by the function that started the selection
776 * @param pt the exact point on the map where the mouse is.
777 * @return Whether the tile selection should be updated.
779 virtual bool OnPlaceDrag (int userdata, Point pt) { return true; }
782 * The user has dragged over the map when the tile highlight mode
783 * has been set.
784 * @param userdata data set by the function that started the selection
785 * @param pt the exact point on the map where the mouse was released.
786 * @param start_tile the begin tile of the drag.
787 * @param end_tile the end tile of the drag.
789 virtual void OnPlaceMouseUp (int userdata, Point pt, TileIndex start_tile, TileIndex end_tile) {}
792 * The user moves over the map when a tile highlight mode has been set
793 * when the special mouse mode has been set to 'PRESIZE' mode. An
794 * example of this is the tile highlight for dock building.
795 * @param tile the tile on the map where the mouse is (in), start of selection (out)
796 * @param tile2 the tile on the map where the mouse is (in), end of selection (out)
798 virtual void OnPlacePresize (TileIndex *tile, TileIndex *tile2) {}
800 /*** End of the event handling ***/
803 * Is the data related to this window NewGRF inspectable?
804 * @return true iff it is inspectable.
806 virtual bool IsNewGRFInspectable() const { return false; }
809 * Show the NewGRF inspection window. When this function is called it is
810 * up to the window to call and pass the right parameters to the
811 * ShowInspectWindow function.
812 * @pre this->IsNewGRFInspectable()
814 virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
818 * Get the nested widget with number \a widnum from the nested widget tree.
819 * @tparam NWID Type of the nested widget.
820 * @param widnum Widget number of the widget to retrieve.
821 * @return The requested widget if it is instantiated, \c NULL otherwise.
823 template <class NWID>
824 inline NWID *Window::GetWidget(uint widnum)
826 if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
827 NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
828 assert(nwid != NULL);
829 return nwid;
832 /** Specialized case of #Window::GetWidget for the nested widget base class. */
833 template <>
834 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
836 if (widnum >= this->nested_array_size) return NULL;
837 return this->nested_array[widnum];
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 const NWID *Window::GetWidget(uint widnum) const
849 return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
854 * Base class for windows opened from a toolbar.
856 class PickerWindowBase : public Window {
858 public:
859 PickerWindowBase (const WindowDesc *desc, Window *parent)
860 : Window(desc)
862 this->parent = parent;
865 void OnDelete (void) OVERRIDE;
868 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
869 Window *FindWindowFromPt(int x, int y);
872 * Open a new window.
873 * @tparam Wcls %Window class to use if the window does not exist.
874 * @param desc The pointer to the WindowDesc to be created
875 * @param window_number the window number of the new window
876 * @param return_existing If set, also return the window if it already existed.
877 * @return %Window pointer of the newly created window, or the existing one if \a return_existing is set, or \c NULL.
879 template <typename Wcls>
880 Wcls *AllocateWindowDescFront (const WindowDesc *desc, int window_number, bool return_existing = false)
882 Wcls *w = static_cast<Wcls *>(BringWindowToFrontById(desc->cls, window_number));
883 if (w != NULL) return return_existing ? w : NULL;
884 return new Wcls(desc, window_number);
887 void RelocateAllWindows(int neww, int newh);
889 /* misc_gui.cpp */
890 enum TooltipCloseCondition {
891 TCC_RIGHT_CLICK,
892 TCC_LEFT_CLICK,
893 TCC_HOVER,
896 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
898 /* widget.cpp */
899 int GetWidgetFromPos(const Window *w, int x, int y);
901 /** Iterate over all windows */
902 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
903 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
904 #define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
905 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
907 extern Point _cursorpos_drag_start;
909 extern int _scrollbar_start_pos;
910 extern int _scrollbar_size;
911 extern byte _scroller_click_timeout;
913 extern bool _scrolling_viewport;
914 extern bool _mouse_hovering;
916 /** Pointer modes. */
917 enum PointerMode {
918 POINTER_NONE, ///< Normal pointer mode.
919 POINTER_TILE, ///< Highlight the tile under the pointer.
920 POINTER_CORNER, ///< Highlight the corner under the pointer.
921 POINTER_AREA, ///< Highlight a custom tile area (docks, tunnels).
922 POINTER_RAIL_X, ///< Rail construction highlighting, X axis.
923 POINTER_RAIL_Y, ///< Rail construction highlighting, Y axis.
924 POINTER_RAIL_H, ///< Rail construction highlighting, horizontal.
925 POINTER_RAIL_V, ///< Rail construction highlighting, vertical.
926 POINTER_RAIL_AUTO, ///< Autorail highlighting.
927 POINTER_DRAG, ///< Drag a vehicle.
928 POINTER_VEHICLE, ///< Normal pointer mode, but allow to select a vehicle.
929 POINTER_TILE_VEHICLE, ///< Highlight the tile under the pointer and allow to select a vehicle.
931 POINTER_RAIL_FIRST = POINTER_RAIL_X,
932 POINTER_RAIL_LAST = POINTER_RAIL_AUTO,
934 extern PointerMode _pointer_mode;
936 void SetFocusedWindow(Window *w);
938 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
940 #endif /* WINDOW_GUI_H */