(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / window_gui.h
blob0ec585e34a1513423784ef4fea24abc45a6f8e26
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"
22 /** State of handling an event. */
23 enum EventState {
24 ES_HANDLED, ///< The passed event is handled.
25 ES_NOT_HANDLED, ///< The passed event is not handled.
28 /**
29 * Flags to describe the look of the frame
31 enum FrameFlags {
32 FR_NONE = 0,
33 FR_TRANSPARENT = 1 << 0, ///< Makes the background transparent if set
34 FR_BORDERONLY = 1 << 4, ///< Draw border only, no background
35 FR_LOWERED = 1 << 5, ///< If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
36 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)
39 DECLARE_ENUM_AS_BIT_SET(FrameFlags)
41 /** Distances used in drawing widgets. */
42 enum WidgetDrawDistances {
43 /* WWT_IMGBTN(_2) */
44 WD_IMGBTN_LEFT = 1, ///< Left offset of the image in the button.
45 WD_IMGBTN_RIGHT = 2, ///< Right offset of the image in the button.
46 WD_IMGBTN_TOP = 1, ///< Top offset of image in the button.
47 WD_IMGBTN_BOTTOM = 2, ///< Bottom offset of image in the button.
49 /* WWT_INSET */
50 WD_INSET_LEFT = 2, ///< Left offset of string.
51 WD_INSET_RIGHT = 2, ///< Right offset of string.
52 WD_INSET_TOP = 1, ///< Top offset of string.
54 WD_VSCROLLBAR_WIDTH = 12, ///< Width of a vertical scrollbar.
56 WD_HSCROLLBAR_HEIGHT = 12, ///< Height of a horizontal scrollbar.
58 /* FrameRect widgets, all text buttons, panel, editbox */
59 WD_FRAMERECT_LEFT = 2, ///< Offset at left to draw the frame rectangular area
60 WD_FRAMERECT_RIGHT = 2, ///< Offset at right to draw the frame rectangular area
61 WD_FRAMERECT_TOP = 1, ///< Offset at top to draw the frame rectangular area
62 WD_FRAMERECT_BOTTOM = 1, ///< Offset at bottom to draw the frame rectangular area
64 /* Extra space at top/bottom of text panels */
65 WD_TEXTPANEL_TOP = 6, ///< Offset at top to draw above the text
66 WD_TEXTPANEL_BOTTOM = 6, ///< Offset at bottom to draw below the text
68 /* WWT_FRAME */
69 WD_FRAMETEXT_LEFT = 6, ///< Left offset of the text of the frame.
70 WD_FRAMETEXT_RIGHT = 6, ///< Right offset of the text of the frame.
71 WD_FRAMETEXT_TOP = 6, ///< Top offset of the text of the frame
72 WD_FRAMETEXT_BOTTOM = 6, ///< Bottom offset of the text of the frame
74 /* WWT_MATRIX */
75 WD_MATRIX_LEFT = 2, ///< Offset at left of a matrix cell.
76 WD_MATRIX_RIGHT = 2, ///< Offset at right of a matrix cell.
77 WD_MATRIX_TOP = 3, ///< Offset at top of a matrix cell.
78 WD_MATRIX_BOTTOM = 1, ///< Offset at bottom of a matrix cell.
80 /* WWT_SHADEBOX */
81 WD_SHADEBOX_WIDTH = 12, ///< Width of a standard shade box widget.
82 WD_SHADEBOX_LEFT = 2, ///< Left offset of shade sprite.
83 WD_SHADEBOX_RIGHT = 2, ///< Right offset of shade sprite.
84 WD_SHADEBOX_TOP = 3, ///< Top offset of shade sprite.
85 WD_SHADEBOX_BOTTOM = 3, ///< Bottom offset of shade sprite.
87 /* WWT_STICKYBOX */
88 WD_STICKYBOX_WIDTH = 12, ///< Width of a standard sticky box widget.
89 WD_STICKYBOX_LEFT = 2, ///< Left offset of sticky sprite.
90 WD_STICKYBOX_RIGHT = 2, ///< Right offset of sticky sprite.
91 WD_STICKYBOX_TOP = 3, ///< Top offset of sticky sprite.
92 WD_STICKYBOX_BOTTOM = 3, ///< Bottom offset of sticky sprite.
94 /* WWT_DEBUGBOX */
95 WD_DEBUGBOX_WIDTH = 12, ///< Width of a standard debug box widget.
96 WD_DEBUGBOX_LEFT = 2, ///< Left offset of debug sprite.
97 WD_DEBUGBOX_RIGHT = 2, ///< Right offset of debug sprite.
98 WD_DEBUGBOX_TOP = 3, ///< Top offset of debug sprite.
99 WD_DEBUGBOX_BOTTOM = 3, ///< Bottom offset of debug sprite.
101 /* WWT_RESIZEBOX */
102 WD_RESIZEBOX_WIDTH = 12, ///< Width of a resize box widget.
103 WD_RESIZEBOX_LEFT = 3, ///< Left offset of resize sprite.
104 WD_RESIZEBOX_RIGHT = 2, ///< Right offset of resize sprite.
105 WD_RESIZEBOX_TOP = 3, ///< Top offset of resize sprite.
106 WD_RESIZEBOX_BOTTOM = 2, ///< Bottom offset of resize sprite.
108 /* WWT_CLOSEBOX */
109 WD_CLOSEBOX_WIDTH = 11, ///< Width of a close box widget.
110 WD_CLOSEBOX_LEFT = 2, ///< Left offset of closebox string.
111 WD_CLOSEBOX_RIGHT = 1, ///< Right offset of closebox string.
112 WD_CLOSEBOX_TOP = 2, ///< Top offset of closebox string.
113 WD_CLOSEBOX_BOTTOM = 2, ///< Bottom offset of closebox string.
115 /* WWT_CAPTION */
116 WD_CAPTION_HEIGHT = 14, ///< Height of a title bar.
117 WD_CAPTIONTEXT_LEFT = 2, ///< Offset of the caption text at the left.
118 WD_CAPTIONTEXT_RIGHT = 2, ///< Offset of the caption text at the right.
119 WD_CAPTIONTEXT_TOP = 2, ///< Offset of the caption text at the top.
120 WD_CAPTIONTEXT_BOTTOM = 2, ///< Offset of the caption text at the bottom.
122 /* Dropdown widget. */
123 WD_DROPDOWN_HEIGHT = 12, ///< Height of a drop down widget.
124 WD_DROPDOWNTEXT_LEFT = 2, ///< Left offset of the dropdown widget string.
125 WD_DROPDOWNTEXT_RIGHT = 14, ///< Right offset of the dropdown widget string.
126 WD_DROPDOWNTEXT_TOP = 1, ///< Top offset of the dropdown widget string.
127 WD_DROPDOWNTEXT_BOTTOM = 1, ///< Bottom offset of the dropdown widget string.
129 WD_SORTBUTTON_ARROW_WIDTH = 11, ///< Width of up/down arrow of sort button state.
131 WD_PAR_VSEP_NORMAL = 2, ///< Normal amount of vertical space between two paragraphs of text.
132 WD_PAR_VSEP_WIDE = 8, ///< Large amount of vertical space between two paragraphs of text.
135 /* widget.cpp */
136 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
137 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str);
139 /* window.cpp */
140 extern Window *_z_front_window;
141 extern Window *_z_back_window;
142 extern Window *_focused_window;
145 /** How do we the window to be placed? */
146 enum WindowPosition {
147 WDP_MANUAL, ///< Manually align the window (so no automatic location finding)
148 WDP_AUTO, ///< Find a place automatically
149 WDP_CENTER, ///< Center the window
150 WDP_ALIGN_TOOLBAR, ///< Align toward the toolbar
153 Point GetToolbarAlignedWindowPosition(int window_width);
156 * High level window description
158 struct WindowDesc : ZeroedMemoryAllocator {
160 WindowDesc(WindowPosition default_pos, int16 def_width, int16 def_height,
161 WindowClass window_class, WindowClass parent_class, uint32 flags,
162 const NWidgetPart *nwid_parts, int16 nwid_length);
164 ~WindowDesc();
166 WindowPosition default_pos; ///< Prefered position of the window. @see WindowPosition()
167 int16 default_width; ///< Prefered initial width of the window.
168 int16 default_height; ///< Prefered initial height of the window.
169 WindowClass cls; ///< Class of the window, @see WindowClass.
170 WindowClass parent_cls; ///< Class of the parent window. @see WindowClass
171 uint32 flags; ///< Flags. @see WindowDefaultFlags
172 const NWidgetPart *nwid_parts; ///< Nested widget parts describing the window.
173 int16 nwid_length; ///< Length of the #nwid_parts array.
177 * Window default widget/window handling flags
179 enum WindowDefaultFlag {
180 WDF_CONSTRUCTION = 1 << 0, ///< This window is used for construction; close it whenever changing company.
181 WDF_UNCLICK_BUTTONS = 1 << 1, ///< Unclick buttons when the window event times out
182 WDF_MODAL = 1 << 2, ///< The window is a modal child of some other window, meaning the parent is 'inactive'
183 WDF_NO_FOCUS = 1 << 3, ///< This window won't get focus/make any other window lose focus when click
187 * Data structure for resizing a window
189 struct ResizeInfo {
190 uint step_width; ///< Step-size of width resize changes
191 uint step_height; ///< Step-size of height resize changes
194 /** State of a sort direction button. */
195 enum SortButtonState {
196 SBS_OFF, ///< Do not sort (with this button).
197 SBS_DOWN, ///< Sort ascending.
198 SBS_UP, ///< Sort descending.
202 * Data structure for a window viewport.
203 * A viewport is either following a vehicle (its id in then in #follow_vehicle), or it aims to display a specific
204 * location #dest_scrollpos_x, #dest_scrollpos_y (#follow_vehicle is then #INVALID_VEHICLE).
205 * The actual location being shown is #scrollpos_x, #scrollpos_y.
206 * @see InitializeViewport(), UpdateViewportPosition(), UpdateViewportCoordinates().
208 struct ViewportData : ViewPort {
209 VehicleID follow_vehicle; ///< VehicleID to follow if following a vehicle, #INVALID_VEHICLE otherwise.
210 int32 scrollpos_x; ///< Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
211 int32 scrollpos_y; ///< Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
212 int32 dest_scrollpos_x; ///< Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewport).
213 int32 dest_scrollpos_y; ///< Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewport).
217 * Data structure for an opened window
219 struct Window : ZeroedMemoryAllocator {
220 protected:
221 void InitializeData(const WindowDesc *desc, WindowNumber window_number);
222 void InitializePositionSize(int x, int y, int min_width, int min_height);
223 void FindWindowPlacementAndResize(int def_width, int def_height);
225 SmallVector<int, 4> scheduled_invalidation_data; ///< Data of scheduled OnInvalidateData() calls.
227 public:
228 Window();
230 virtual ~Window();
233 * Helper allocation function to disallow something.
234 * Don't allow arrays; arrays of Windows are pointless as you need
235 * to destruct them all at the same time too, which is kinda hard.
236 * @param size the amount of space not to allocate
238 FORCEINLINE void *operator new[](size_t size)
240 NOT_REACHED();
244 * Helper allocation function to disallow something.
245 * Don't free the window directly; it corrupts the linked list when iterating
246 * @param ptr the pointer not to free
248 FORCEINLINE void operator delete(void *ptr)
252 uint16 flags4; ///< Window flags, @see WindowFlags
253 WindowClass window_class; ///< Window class
254 WindowNumber window_number; ///< Window number within the window class
256 int left; ///< x position of left edge of the window
257 int top; ///< y position of top edge of the window
258 int width; ///< width of the window (number of pixels to the right in x direction)
259 int height; ///< Height of the window (number of pixels down in y direction)
261 ResizeInfo resize; ///< Resize information
263 Owner owner; ///< The owner of the content shown in this window. Company colour is acquired from this variable.
265 ViewportData *viewport; ///< Pointer to viewport data, if present.
266 uint32 desc_flags; ///< Window/widgets default flags setting. @see WindowDefaultFlag
267 const NWidgetCore *nested_focus; ///< Currently focused nested widget, or \c NULL if no nested widget has focus.
268 NWidgetBase *nested_root; ///< Root of the nested tree.
269 NWidgetBase **nested_array; ///< Array of pointers into the tree. Do not access directly, use #Window::GetWidget() instead.
270 uint nested_array_size; ///< Size of the nested array.
271 NWidgetStacked *shade_select; ///< Selection widget (#NWID_SELECTION) to use for shading the window. If \c NULL, window cannot shade.
272 Dimension unshaded_size; ///< Last known unshaded size (only valid while shaded).
274 int scrolling_scrollbar; ///< Widgetindex of just being dragged scrollbar. -1 of none is active.
276 Window *parent; ///< Parent window.
277 Window *z_front; ///< The window in front of us in z-order.
278 Window *z_back; ///< The window behind us in z-order.
280 template <class NWID>
281 inline const NWID *GetWidget(uint widnum) const;
282 template <class NWID>
283 inline NWID *GetWidget(uint widnum);
285 const Scrollbar *GetScrollbar(uint widnum) const;
286 Scrollbar *GetScrollbar(uint widnum);
288 void InitNested(const WindowDesc *desc, WindowNumber number = 0);
289 void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
290 void FinishInitNested(const WindowDesc *desc, WindowNumber window_number = 0);
293 * Sets the enabled/disabled status of a widget.
294 * By default, widgets are enabled.
295 * On certain conditions, they have to be disabled.
296 * @param widget_index index of this widget in the window
297 * @param disab_stat status to use ie: disabled = true, enabled = false
299 inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
301 assert(widget_index < this->nested_array_size);
302 if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
306 * Sets a widget to disabled.
307 * @param widget_index index of this widget in the window
309 inline void DisableWidget(byte widget_index)
311 SetWidgetDisabledState(widget_index, true);
315 * Sets a widget to Enabled.
316 * @param widget_index index of this widget in the window
318 inline void EnableWidget(byte widget_index)
320 SetWidgetDisabledState(widget_index, false);
324 * Gets the enabled/disabled status of a widget.
325 * @param widget_index index of this widget in the window
326 * @return status of the widget ie: disabled = true, enabled = false
328 inline bool IsWidgetDisabled(byte widget_index) const
330 assert(widget_index < this->nested_array_size);
331 return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
335 * Check if given widget is focused within this window
336 * @param widget_index : index of the widget in the window to check
337 * @return true if given widget is the focused window in this window
339 inline bool IsWidgetFocused(byte widget_index) const
341 return this->nested_focus != NULL && this->nested_focus->index == widget_index;
345 * Check if given widget has user input focus. This means that both the window
346 * has focus and that the given widget has focus within the window.
347 * @param widget_index : index of the widget in the window to check
348 * @return true if given widget is the focused window in this window and this window has focus
350 inline bool IsWidgetGloballyFocused(byte widget_index) const
352 return _focused_window == this && IsWidgetFocused(widget_index);
356 * Sets the lowered/raised status of a widget.
357 * @param widget_index index of this widget in the window
358 * @param lowered_stat status to use ie: lowered = true, raised = false
360 inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
362 assert(widget_index < this->nested_array_size);
363 this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
367 * Invert the lowered/raised status of a widget.
368 * @param widget_index index of this widget in the window
370 inline void ToggleWidgetLoweredState(byte widget_index)
372 assert(widget_index < this->nested_array_size);
373 bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
374 this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
378 * Marks a widget as lowered.
379 * @param widget_index index of this widget in the window
381 inline void LowerWidget(byte widget_index)
383 SetWidgetLoweredState(widget_index, true);
387 * Marks a widget as raised.
388 * @param widget_index index of this widget in the window
390 inline void RaiseWidget(byte widget_index)
392 SetWidgetLoweredState(widget_index, false);
396 * Gets the lowered state of a widget.
397 * @param widget_index index of this widget in the window
398 * @return status of the widget ie: lowered = true, raised= false
400 inline bool IsWidgetLowered(byte widget_index) const
402 assert(widget_index < this->nested_array_size);
403 return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
406 void UnfocusFocusedWidget();
407 bool SetFocusedWidget(byte widget_index);
409 void HandleButtonClick(byte widget);
410 int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
412 void RaiseButtons(bool autoraise = false);
413 void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
414 void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
415 void SetWidgetDirty(byte widget_index) const;
417 void DrawWidgets() const;
418 void DrawViewport() const;
419 void DrawSortButtonState(int widget, SortButtonState state) const;
421 void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
423 void SetDirty() const;
424 void ReInit(int rx = 0, int ry = 0);
426 /** Is window shaded currently? */
427 inline bool IsShaded() const
429 return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
432 void SetShaded(bool make_shaded);
435 * Mark this window's data as invalid (in need of re-computing)
436 * @param data The data to invalidate with
437 * @param gui_scope Whether the funtion is called from GUI scope.
439 void InvalidateData(int data = 0, bool gui_scope = true)
441 this->SetDirty();
442 if (!gui_scope) {
443 /* Schedule GUI-scope invalidation for next redraw. */
444 *this->scheduled_invalidation_data.Append() = data;
446 this->OnInvalidateData(data, gui_scope);
450 * Process all scheduled invalidations.
452 void ProcessScheduledInvalidations()
454 for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
455 this->OnInvalidateData(*data, true);
457 this->scheduled_invalidation_data.Clear();
460 /*** Event handling ***/
463 * Notification that the nested widget tree gets initialized. The event can be used to perform general computations.
464 * @note #nested_root and/or #nested_array (normally accessed via #GetWidget()) may not exist during this call.
466 virtual void OnInit() { }
469 * Compute the initial position of the window.
470 * @param *desc The pointer to the WindowDesc of the window to create.
471 * @param sm_width Smallest width of the window.
472 * @param sm_height Smallest height of the window.
473 * @param window_number The window number of the new window.
474 * @return Initial position of the top-left corner of the window.
476 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
479 * The window must be repainted.
480 * @note This method should not change any state, it should only use drawing functions.
482 virtual void OnPaint()
484 this->DrawWidgets();
488 * Draw the contents of a nested widget.
489 * @param r Rectangle occupied by the widget.
490 * @param widget Number of the widget to draw.
491 * @note This method may not change any state, it may only use drawing functions.
493 virtual void DrawWidget(const Rect &r, int widget) const {}
496 * Update size and resize step of a widget in the window.
497 * After retrieval of the minimal size and the resize-steps of a widget, this function is called to allow further refinement,
498 * typically by computing the real maximal size of the content. Afterwards, \a size is taken to be the minimal size of the widget
499 * and \a resize is taken to contain the resize steps. For the convenience of the callee, \a padding contains the amount of
500 * padding between the content and the edge of the widget. This should be added to the returned size.
501 * @param widget Widget number.
502 * @param size Size of the widget.
503 * @param padding Recommended amount of space between the widget content and the widget edge.
504 * @param fill Fill step of the widget.
505 * @param resize Resize step of the widget.
507 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
510 * Initialize string parameters for a widget.
511 * Calls to this function are made during initialization to measure the size (that is as part of #InitNested()), during drawing,
512 * and while re-initializing the window. Only for widgets that render text initializing is requested.
513 * @param widget Widget number.
515 virtual void SetStringParameters(int widget) const {}
518 * Called when window gains focus
520 virtual void OnFocus() {}
523 * Called when window looses focus
525 virtual void OnFocusLost() {}
528 * A key has been pressed.
529 * @param key the Unicode value of the key.
530 * @param keycode the untranslated key code including shift state.
531 * @return #ES_HANDLED if the key press has been handled and no other
532 * window should receive the event.
534 virtual EventState OnKeyPress(uint16 key, uint16 keycode) { return ES_NOT_HANDLED; }
537 * The state of the control key has changed
538 * @return #ES_HANDLED if the change has been handled and no other
539 * window should receive the event.
541 virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
545 * A click with the left mouse button has been made on the window.
546 * @param pt the point inside the window that has been clicked.
547 * @param widget the clicked widget.
548 * @param click_count Number of fast consecutive clicks at same position
550 virtual void OnClick(Point pt, int widget, int click_count) {}
553 * A click with the right mouse button has been made on the window.
554 * @param pt the point inside the window that has been clicked.
555 * @param widget the clicked widget.
556 * @return true if the click was actually handled, i.e. do not show a
557 * tooltip if tooltip-on-right-click is enabled.
559 virtual bool OnRightClick(Point pt, int widget) { return false; }
562 * The mouse is hovering over a widget in the window, perform an action for it, like opening a custom tooltip.
563 * @param pt The point where the mouse is hovering.
564 * @param widget The widget where the mouse is hovering.
566 virtual void OnHover(Point pt, int widget) {}
569 * An 'object' is being dragged at the provided position, highlight the target if possible.
570 * @param pt The point inside the window that the mouse hovers over.
571 * @param widget The widget the mouse hovers over.
573 virtual void OnMouseDrag(Point pt, int widget) {}
576 * A dragged 'object' has been released.
577 * @param pt the point inside the window where the release took place.
578 * @param widget the widget where the release took place.
580 virtual void OnDragDrop(Point pt, int widget) {}
583 * Handle the request for (viewport) scrolling.
584 * @param delta the amount the viewport must be scrolled.
586 virtual void OnScroll(Point delta) {}
589 * The mouse is currently moving over the window or has just moved outside
590 * of the window. In the latter case pt is (-1, -1).
591 * @param pt the point inside the window that the mouse hovers over.
592 * @param widget the widget the mouse hovers over.
594 virtual void OnMouseOver(Point pt, int widget) {}
597 * The mouse wheel has been turned.
598 * @param wheel the amount of movement of the mouse wheel.
600 virtual void OnMouseWheel(int wheel) {}
604 * Called for every mouse loop run, which is at least once per (game) tick.
606 virtual void OnMouseLoop() {}
609 * Called once per (game) tick.
611 virtual void OnTick() {}
614 * Called once every 100 (game) ticks.
616 virtual void OnHundredthTick() {}
619 * Called when this window's timeout has been reached.
621 virtual void OnTimeout() {}
625 * Called after the window got resized.
626 * For nested windows with a viewport, call NWidgetViewport::UpdateViewportCoordinates.
628 virtual void OnResize() {}
631 * A dropdown option associated to this window has been selected.
632 * @param widget the widget (button) that the dropdown is associated with.
633 * @param index the element in the dropdown that is selected.
635 virtual void OnDropdownSelect(int widget, int index) {}
638 * The query window opened from this window has closed.
639 * @param str the new value of the string, NULL if the window
640 * was cancelled or an empty string when the default
641 * button was pressed, i.e. StrEmpty(str).
643 virtual void OnQueryTextFinished(char *str) {}
646 * Some data on this window has become invalid.
647 * @param data information about the changed data.
648 * @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.
650 virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
653 * The user clicked some place on the map when a tile highlight mode
654 * has been set.
655 * @param pt the exact point on the map that has been clicked.
656 * @param tile the tile on the map that has been clicked.
658 virtual void OnPlaceObject(Point pt, TileIndex tile) {}
661 * The user clicked on a vehicle while HT_VEHICLE has been set.
662 * @param v clicked vehicle. It is guaranteed to be v->IsPrimaryVehicle() == true
664 virtual void OnVehicleSelect(const struct Vehicle *v) {}
667 * The user cancelled a tile highlight mode that has been set.
669 virtual void OnPlaceObjectAbort() {}
673 * The user is dragging over the map when the tile highlight mode
674 * has been set.
675 * @param select_method the method of selection (allowed directions)
676 * @param select_proc what will be created when the drag is over.
677 * @param pt the exact point on the map where the mouse is.
679 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
682 * The user has dragged over the map when the tile highlight mode
683 * has been set.
684 * @param select_method the method of selection (allowed directions)
685 * @param select_proc what should be created.
686 * @param pt the exact point on the map where the mouse was released.
687 * @param start_tile the begin tile of the drag.
688 * @param end_tile the end tile of the drag.
690 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
693 * The user moves over the map when a tile highlight mode has been set
694 * when the special mouse mode has been set to 'PRESIZE' mode. An
695 * example of this is the tile highlight for dock building.
696 * @param pt the exact point on the map where the mouse is.
697 * @param tile the tile on the map where the mouse is.
699 virtual void OnPlacePresize(Point pt, TileIndex tile) {}
701 /*** End of the event handling ***/
704 * Is the data related to this window NewGRF inspectable?
705 * @return true iff it is inspectable.
707 virtual bool IsNewGRFInspectable() const { return false; }
710 * Show the NewGRF inspection window. When this function is called it is
711 * up to the window to call and pass the right parameters to the
712 * ShowInspectWindow function.
713 * @pre this->IsNewGRFInspectable()
715 virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
719 * Get the nested widget with number \a widnum from the nested widget tree.
720 * @tparam NWID Type of the nested widget.
721 * @param widnum Widget number of the widget to retrieve.
722 * @return The requested widget if it is instantiated, \c NULL otherwise.
724 template <class NWID>
725 inline NWID *Window::GetWidget(uint widnum)
727 if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
728 NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
729 assert(nwid != NULL);
730 return nwid;
733 /** Specialized case of #Window::GetWidget for the nested widget base class. */
734 template <>
735 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
737 if (widnum >= this->nested_array_size) return NULL;
738 return this->nested_array[widnum];
742 * Get the nested widget with number \a widnum from the nested widget tree.
743 * @tparam NWID Type of the nested widget.
744 * @param widnum Widget number of the widget to retrieve.
745 * @return The requested widget if it is instantiated, \c NULL otherwise.
747 template <class NWID>
748 inline const NWID *Window::GetWidget(uint widnum) const
750 return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
755 * Base class for windows opened from a toolbar.
757 class PickerWindowBase : public Window {
759 public:
760 PickerWindowBase(Window *parent) : Window()
762 this->parent = parent;
765 virtual ~PickerWindowBase();
769 * Window flags
771 enum WindowFlags {
772 WF_TIMEOUT_TRIGGER = 1, ///< When the timeout should start triggering
773 WF_TIMEOUT_BEGIN = 7, ///< The initial value for the timeout
774 WF_TIMEOUT_MASK = 7, ///< Window timeout counter bit mask (3 bits)
775 WF_DRAGGING = 1 << 3, ///< Window is being dragged
776 WF_SIZING_RIGHT = 1 << 4, ///< Window is being resized towards the right.
777 WF_SIZING_LEFT = 1 << 5, ///< Window is being resized towards the left.
778 WF_SIZING = WF_SIZING_RIGHT | WF_SIZING_LEFT, ///< Window is being resized.
779 WF_STICKY = 1 << 6, ///< Window is made sticky by user
781 WF_DISABLE_VP_SCROLL = 1 << 7, ///< Window does not do autoscroll, @see HandleAutoscroll()
783 WF_WHITE_BORDER_ONE = 1 << 8,
784 WF_WHITE_BORDER_MASK = 1 << 9 | WF_WHITE_BORDER_ONE,
786 WF_CENTERED = 1 << 10, ///< Window is centered and shall stay centered after ReInit
789 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
790 Window *FindWindowFromPt(int x, int y);
793 * Open a new window.
794 * @param desc The pointer to the WindowDesc to be created
795 * @param window_number the window number of the new window
796 * @return see Window pointer of the newly created window
798 template <typename Wcls>
799 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
801 if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
802 return new Wcls(desc, window_number);
805 void RelocateAllWindows(int neww, int newh);
807 /* misc_gui.cpp */
808 enum TooltipCloseCondition {
809 TCC_RIGHT_CLICK,
810 TCC_LEFT_CLICK,
811 TCC_HOVER,
814 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
816 /* widget.cpp */
817 int GetWidgetFromPos(const Window *w, int x, int y);
819 /** Iterate over all windows */
820 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
821 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
822 #define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
823 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
825 extern Point _cursorpos_drag_start;
827 extern int _scrollbar_start_pos;
828 extern int _scrollbar_size;
829 extern byte _scroller_click_timeout;
831 extern bool _scrolling_viewport;
832 extern bool _mouse_hovering;
834 /** Mouse modes. */
835 enum SpecialMouseMode {
836 WSM_NONE, ///< No special mouse mode.
837 WSM_DRAGDROP, ///< Dragging an object.
838 WSM_SIZING, ///< Sizing mode.
839 WSM_PRESIZE, ///< Presizing mode (docks, tunnels).
841 extern SpecialMouseMode _special_mouse_mode;
843 void SetFocusedWindow(Window *w);
845 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
847 #endif /* WINDOW_GUI_H */