70 static constexpr float ASPECT_LOCATION = 12.f / 14.f;
71 static constexpr float ASPECT_RENAME = 12.f / 14.f;
72 static constexpr float ASPECT_SETTINGS_BUTTON = 21.f / 12.f;
73 static constexpr float ASPECT_TOGGLE_SIZE = 12.f / 14.f;
74 static constexpr float ASPECT_LEFT_RIGHT_BUTTON = 8.f / 12.f;
75 static constexpr float ASPECT_UP_DOWN_BUTTON = 11.f / 12.f;
76 static constexpr float ASPECT_VEHICLE_ICON = 15.f / 12.f;
77 static constexpr float ASPECT_VEHICLE_FLAG = 11.f / 12.f;
84 static constexpr uint WD_SHADEBOX_WIDTH = 12;
85 static constexpr uint WD_STICKYBOX_WIDTH = 12;
86 static constexpr uint WD_DEBUGBOX_WIDTH = 12;
87 static constexpr uint WD_DEFSIZEBOX_WIDTH = 12;
88 static constexpr uint WD_RESIZEBOX_WIDTH = 12;
89 static constexpr uint WD_CLOSEBOX_WIDTH = 11;
90 static constexpr uint WD_CAPTION_HEIGHT = 14;
91 static constexpr uint WD_DROPDOWN_HEIGHT = 12;
97 .
imgbtn = { .left = 1, .top = 1, .right = 1, .bottom = 1},
98 .inset = { .left = 2, .top = 1, .right = 2, .bottom = 1},
99 .vscrollbar = { .left = 2, .top = 3, .right = 2, .bottom = 3},
100 .hscrollbar = { .left = 3, .top = 2, .right = 3, .bottom = 2},
101 .bevel = { .left = 1, .top = 1, .right = 1, .bottom = 1},
102 .fullbevel = { .left = 1, .top = 1, .right = 1, .bottom = 1},
103 .framerect = { .left = 2, .top = 1, .right = 2, .bottom = 1},
104 .frametext = { .left = 6, .top = 6, .right = 6, .bottom = 6},
105 .matrix = { .left = 2, .top = 3, .right = 2, .bottom = 1},
106 .shadebox = { .left = 2, .top = 3, .right = 2, .bottom = 3},
107 .stickybox = { .left = 2, .top = 3, .right = 2, .bottom = 3},
108 .debugbox = { .left = 2, .top = 3, .right = 2, .bottom = 3},
109 .defsizebox = { .left = 2, .top = 3, .right = 2, .bottom = 3},
110 .resizebox = { .left = 2, .top = 2, .right = 2, .bottom = 2},
111 .closebox = { .left = 2, .top = 2, .right = 1, .bottom = 2},
112 .captiontext = { .left = 2, .top = 2, .right = 2, .bottom = 2},
113 .dropdowntext = { .left = 2, .top = 1, .right = 2, .bottom = 1},
114 .dropdownlist = { .left = 1, .top = 2, .right = 1, .bottom = 2},
115 .modalpopup = { .left = 20, .top = 10, .right = 20, .bottom = 10},
116 .picker = { .left = 3, .top = 3, .right = 3, .bottom = 3},
117 .sparse = { .left = 10, .top = 8, .right = 10, .bottom = 8},
118 .sparse_resize = { .left = 10, .top = 8, .right = 10, .bottom = 0},
133 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, flags);
139using WindowList = std::list<Window *>;
141extern Window *_focused_window;
164 const std::source_location location = std::source_location::current());
263enum TooltipCloseCondition {
297 inline void *
operator new[](
size_t size) =
delete;
329 WindowList::iterator z_position;
331 template <
class NWID>
333 template <
class NWID>
428 return this->nested_focus !=
nullptr && this->nested_focus->
index ==
widget_index;
520 template<
typename... Args>
531 template<
typename... Args>
541 template<
typename... Args>
556 virtual void Close(
int data = 0);
867 template <
bool TtoBack>
872 typedef size_t difference_type;
873 typedef std::forward_iterator_tag iterator_category;
883 Window * operator*()
const {
return *this->it; }
884 WindowIterator & operator++() { this->Next(); this->Validate();
return *
this; }
886 bool IsEnd()
const {
return this->it ==
_z_windows.end(); }
889 WindowList::iterator it;
892 while (!this->IsEnd() && *this->it ==
nullptr) this->Next();
912 template <
bool Tfront>
939template <
class It,
class Pred>
942 return std::adjacent_find(begin, end, std::not_fn(pred)) == end;
1000template<typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value,
int> = 0>
1014template <
typename Wcls>
1018 if (w !=
nullptr)
return return_existing ? w :
nullptr;
1019 return new Wcls(desc, window_number);
1029extern Point _cursorpos_drag_start;
1031extern int _scrollbar_start_pos;
1032extern int _scrollbar_size;
1033extern uint8_t _scroller_click_timeout;
Base class for windows opened from a toolbar.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
Base class that provides memory initialization on dynamically created objects.
Types related to companies.
Owner
Enum for all companies/owners.
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
Operators to allow to work with enum as with type safe bit set in C++.
StringAlignment
How to align the to-be drawn text.
FontSize
Available font sizes.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Dimensions (a width and height) of a rectangle in 2D.
List of hotkeys for a window.
Coordinates of a point in 2D.
Data stored about a string that can be modified in the GUI.
Padding dimensions to apply to each side of a Rect.
Specification of a rectangle with absolute coordinates of all edges.
Data structure for resizing a window.
uint step_height
Step-size of height resize changes.
uint step_width
Step-size of width resize changes.
Helper/buffer for input fields.
Data structure for a window viewport.
int32_t dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
int32_t scrollpos_y
Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
int32_t dest_scrollpos_x
Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewp...
VehicleID follow_vehicle
VehicleID to follow if following a vehicle, INVALID_VEHICLE otherwise.
int32_t scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Data structure for viewport, display of a part of the world.
High level window description.
int16_t GetDefaultWidth() const
Determine default width of window.
static void SaveToConfig()
Save all WindowDesc settings to _windows_file.
int16_t pref_width
User-preferred width of the window. Zero if unset.
bool pref_sticky
Preferred stickyness.
HotkeyList * hotkeys
Hotkeys for the window.
WindowDesc(const WindowDesc &)=delete
Delete copy constructor to prevent compilers from copying the structure, which fails due to _window_d...
int16_t pref_height
User-preferred height of the window. Zero if unset.
int16_t GetDefaultHeight() const
Determine default height of window.
WindowClass cls
Class of the window,.
WindowPosition default_pos
Preferred position of the window.
const char * ini_key
Key to store window defaults in openttd.cfg. nullptr if nothing shall be stored.
int16_t default_height_trad
Preferred initial height of the window (pixels at 1x zoom).
WindowClass parent_cls
Class of the parent window.
int16_t default_width_trad
Preferred initial width of the window (pixels at 1x zoom).
const std::source_location source_location
Source location of this definition.
static void LoadFromConfig()
Load all WindowDesc settings from _windows_file.
const std::span< const NWidgetPart > nwid_parts
Span of nested widget parts describing the window.
Iterable ensemble of all valid Windows.
Iterator to iterate all valid Windows.
Data structure for an opened window.
virtual const struct Textbuf * GetFocusedTextbuf() const
Get the current input text buffer.
void SetWidgetHighlight(WidgetID widget_index, TextColour highlighted_colour)
Sets the highlighted status of a widget.
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
bool IsWidgetGloballyFocused(WidgetID widget_index) const
Check if given widget has user input focus.
virtual void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize)
Update size and resize step of a widget in the window.
virtual void OnInvalidateData(int data=0, bool gui_scope=true)
Some data on this window has become invalid.
static int SortButtonWidth()
Get width of up/down arrow of sort button state.
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
std::map< WidgetID, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
virtual void ApplyDefaults()
Read default values from WindowDesc configuration an apply them to the window.
uint8_t white_border_timer
Timer value of the WF_WHITE_BORDER for flags.
NWidgetStacked * shade_select
Selection widget (NWID_SELECTION) to use for shading the window. If nullptr, window cannot shade.
void InitializePositionSize(int x, int y, int min_width, int min_height)
Set the position and smallest size of the window.
void DrawWidgets() const
Paint all widgets of a window.
virtual void SetStringParameters(WidgetID widget) const
Initialize string parameters for a widget.
virtual bool OnVehicleSelect(VehicleList::const_iterator begin, VehicleList::const_iterator end)
The user clicked on a vehicle while HT_VEHICLE has been set.
Dimension unshaded_size
Last known unshaded size (only valid while shaded).
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing)
virtual EventState OnKeyPress(char32_t key, uint16_t keycode)
A key has been pressed.
Window * parent
Parent window.
virtual ~Window()
Remove window and all its child windows from the window stack.
void RaiseWidget(WidgetID widget_index)
Marks a widget as raised.
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
uint8_t timeout_timer
Timer value of the WF_TIMEOUT for flags.
WidgetID mouse_capture_widget
ID of current mouse capture widget (e.g. dragged scrollbar). -1 if no widget has mouse capture.
virtual void OnGameTick()
Called once per (game) tick.
virtual void ShowNewGRFInspectWindow() const
Show the NewGRF inspection window.
virtual bool OnRightClick(Point pt, WidgetID widget)
A click with the right mouse button has been made on the window.
void RaiseWidgetWhenLowered(WidgetID widget_index)
Marks a widget as raised and dirty (redraw), when it is marked as lowered.
void DrawSortButtonState(WidgetID widget, SortButtonState state) const
Draw a sort button's up or down arrow symbol.
void ProcessScheduledInvalidations()
Process all scheduled invalidations.
void CloseChildWindows(WindowClass wc=WC_INVALID) const
Close all children a window might have in a head-recursive manner.
ResizeInfo resize
Resize information.
void UnfocusFocusedWidget()
Makes no widget on this window have focus.
void DisableWidget(WidgetID widget_index)
Sets a widget to disabled.
virtual void OnMouseLoop()
Called for every mouse loop run, which is at least once per (game) tick.
void SetShaded(bool make_shaded)
Set the shaded state of the window to make_shaded.
virtual void OnPlaceObjectAbort()
The user cancelled a tile highlight mode that has been set.
int scale
Scale of this window – used to determine how to resize.
void ScheduleResize()
Mark this window as resized and in need of OnResize() event.
virtual bool OnVehicleSelect(const struct Vehicle *v)
The user clicked on a vehicle while HT_VEHICLE has been set.
bool IsWidgetFocused(WidgetID widget_index) const
Check if given widget is focused within this window.
void SetWidgetsDisabledState(bool disab_stat, Args... widgets)
Sets the enabled/disabled status of a list of widgets.
virtual void OnPaint()
The window must be repainted.
virtual void OnDragDrop(Point pt, WidgetID widget)
A dragged 'object' has been released.
virtual bool IsNewGRFInspectable() const
Is the data related to this window NewGRF inspectable?
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
WindowDesc & window_desc
Window description.
WindowClass window_class
Window class.
virtual void OnRealtimeTick(uint delta_ms)
Called periodically.
ViewportData * viewport
Pointer to viewport data, if present.
virtual void OnQueryTextFinished(std::optional< std::string > str)
The query window opened from this window has closed.
void SetWhiteBorder()
Set the timeout flag of the window and initiate the timer.
bool SetFocusedWidget(WidgetID widget_index)
Set focus within this window to the given widget.
virtual void OnFocusLost(bool closing)
The window has lost focus.
virtual void OnDropdownClose(Point pt, WidgetID widget, int index, bool instant_close)
A dropdown window associated to this window has been closed.
bool IsWidgetLowered(WidgetID widget_index) const
Gets the lowered state of a widget.
bool IsWidgetDisabled(WidgetID widget_index) const
Gets the enabled/disabled status of a widget.
void DrawViewport() const
Draw the viewport of this window.
static std::vector< Window * > closed_windows
List of closed windows to delete.
void RaiseButtons(bool autoraise=false)
Raise the buttons of the window.
virtual void DrawWidget(const Rect &r, WidgetID widget) const
Draw the contents of a nested widget.
void SetWidgetsLoweredState(bool lowered_stat, Args... widgets)
Sets the lowered/raised status of a list of widgets.
virtual Point OnInitialPosition(int16_t sm_width, int16_t sm_height, int window_number)
Compute the initial position of the window.
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
virtual void OnDropdownSelect(WidgetID widget, int index)
A dropdown option associated to this window has been selected.
virtual void OnMouseWheel(int wheel)
The mouse wheel has been turned.
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
void EnableWidget(WidgetID widget_index)
Sets a widget to Enabled.
virtual Point GetCaretPosition() const
Get the current caret position if an edit box has the focus.
virtual void OnPlaceObject(Point pt, TileIndex tile)
The user clicked some place on the map when a tile highlight mode has been set.
int left
x position of left edge of the window
bool IsShaded() const
Is window shaded currently?
Window * FindChildWindow(WindowClass wc=WC_INVALID) const
Find the Window whose parent pointer points to this window.
void SetTimeout()
Set the timeout flag of the window and initiate the timer.
const NWidgetCore * nested_focus
Currently focused nested widget, or nullptr if no nested widget has focus.
virtual void OnEditboxChanged(WidgetID widget)
The text in an editbox has been edited.
void UpdateQueryStringSize()
Update size of all QueryStrings of this window.
virtual void OnScroll(Point delta)
Handle the request for (viewport) scrolling.
void RaiseWidgetsWhenLowered(Args... widgets)
Raises the widgets and sets widgets dirty that are lowered.
int top
y position of top edge of the window
virtual void OnClick(Point pt, WidgetID widget, int click_count)
A click with the left mouse button has been made on the window.
const QueryString * GetQueryString(WidgetID widnum) const
Return the querystring associated to a editbox.
WidgetLookup widget_lookup
Indexed access to the nested widget tree. Do not access directly, use Window::GetWidget() instead.
virtual ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const
Get the character that is rendered at a position by the focused edit box.
std::vector< int > scheduled_invalidation_data
Data of scheduled OnInvalidateData() calls.
void InitializeData(WindowNumber window_number)
Initializes the data (except the position and initial size) of a new Window.
virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
The user has dragged over the map when the tile highlight mode has been set.
virtual void OnMouseOver(Point pt, WidgetID widget)
The mouse is currently moving over the window or has just moved outside of the window.
int GetRowFromWidget(int clickpos, WidgetID widget, int padding, int line_height=-1) const
Compute the row of a widget that a user clicked in.
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
virtual bool OnTooltip(Point pt, WidgetID widget, TooltipCloseCondition close_cond)
Event to display a custom tooltip.
void LowerWidget(WidgetID widget_index)
Marks a widget as lowered.
virtual EventState OnCTRLStateChange()
The state of the control key has changed.
void ProcessScheduledResize()
Process scheduled OnResize() event.
EventState HandleEditBoxKey(WidgetID wid, char32_t key, uint16_t keycode)
Process keypress for editbox widget.
virtual void InsertTextString(WidgetID wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
Insert a text string at the cursor position into the edit box widget.
virtual void OnMouseDrag(Point pt, WidgetID widget)
An 'object' is being dragged at the provided position, highlight the target if possible.
void HandleButtonClick(WidgetID widget)
Do all things to make a button look clicked and mark it to be unclicked in a few ticks.
virtual void OnResize()
Called after the window got resized.
virtual void OnFocus()
The window has gained focus.
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
virtual void OnTimeout()
Called when this window's timeout has been reached.
WindowFlags flags
Window flags.
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
void ProcessHighlightedInvalidations()
Process all invalidation of highlighted widgets.
virtual Rect GetTextBoundingRect(const char *from, const char *to) const
Get the bounding rectangle for a text range if an edit box has the focus.
virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
The user is dragging over the map when the tile highlight mode has been set.
virtual EventState OnHotkey(int hotkey)
A hotkey has been pressed.
static void DeleteClosedWindows()
Delete all closed windows.
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
std::unique_ptr< NWidgetBase > nested_root
Root of the nested tree.
bool scheduled_resize
Set if window has been resized.
bool IsWidgetHighlighted(WidgetID widget_index) const
Gets the highlighted status of a widget.
void DisableAllWidgetHighlight()
Disable the highlighted status of all widgets.
virtual void OnPlacePresize(Point pt, TileIndex tile)
The user moves over the map when a tile highlight mode has been set when the special mouse mode has b...
int height
Height of the window (number of pixels down in y direction)
virtual void OnHover(Point pt, WidgetID widget)
The mouse is hovering over a widget in the window, perform an action for it.
int width
width of the window (number of pixels to the right in x direction)
virtual void OnInit()
Notification that the nested widget tree gets initialized.
void ToggleWidgetLoweredState(WidgetID widget_index)
Invert the lowered/raised status of a widget.
virtual void FindWindowPlacementAndResize(int def_width, int def_height)
Resize window towards the default size.
WindowNumber window_number
Window number within the window class.
Types related to vehicles.
uint32_t VehicleID
The type all our vehicle IDs have.
Functions and type for generating vehicle lists.
Types related to viewports.
ViewportDragDropSelectionProcess
Drag and drop selection process, or, what to do with an area of land when you've selected it.
ViewportPlaceMethod
Viewport place method (type of highlighted area and placed objects)
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
SpecialMouseMode
Mouse modes.
@ WSM_DRAGDROP
Drag&drop an object.
@ WSM_DRAGGING
Dragging mode (trees).
@ WSM_PRESIZE
Presizing mode (docks, tunnels).
@ WSM_NONE
No special mouse mode.
Wcls * AllocateWindowDescFront(WindowDesc &desc, int window_number, bool return_existing=false)
Open a new window.
bool _scrolling_viewport
A viewport is being scrolled with the mouse.
bool AllEqual(It begin, It end, Pred pred)
Generic helper function that checks if all elements of the range are equal with respect to the given ...
void DrawCaption(const Rect &r, Colours colour, Owner owner, TextColour text_colour, StringID str, StringAlignment align, FontSize fs)
Draw a caption bar.
void SetFocusedWindow(Window *w)
Set the window that has the focus.
SortButtonState
State of a sort direction button.
@ SBS_DOWN
Sort ascending.
@ SBS_OFF
Do not sort (with this button).
Window * FindWindowFromPt(int x, int y)
Do a search for a window at specific coordinates.
static const int TIMEOUT_DURATION
The initial timeout value for WF_TIMEOUT.
WidgetID GetWidgetFromPos(const Window *w, int x, int y)
Returns the index for the widget located at the given position relative to the window.
Point GetToolbarAlignedWindowPosition(int window_width)
Computer the position of the top-left corner of a window to be opened right under the toolbar.
@ WF_DRAGGING
Window is being dragged.
@ WF_WHITE_BORDER
Window white border counter bit mask.
@ WF_TIMEOUT
Window timeout counter.
@ WF_SIZING_LEFT
Window is being resized towards the left.
@ WF_DISABLE_VP_SCROLL
Window does not do autoscroll,.
@ WF_SIZING
Window is being resized.
@ WF_HIGHLIGHTED
Window has a widget that has a highlight.
@ WF_STICKY
Window is made sticky by user.
@ WF_SIZING_RIGHT
Window is being resized towards the right.
@ WF_CENTERED
Window is centered and shall stay centered after ReInit.
FrameFlags
Flags to describe the look of the frame.
@ FR_DARKENED
If set the background is darker, allows for lowered frames with normal background colour when used wi...
@ FR_BORDERONLY
Draw border only, no background.
@ FR_LOWERED
If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
@ FR_TRANSPARENT
Makes the background transparent if set.
Window * BringWindowToFrontById(WindowClass cls, WindowNumber number)
Find a window and make it the relative top-window on the screen.
void RelocateAllWindows(int neww, int newh)
Relocate all windows to fit the new size of the game application screen.
void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
Special handling for the scrollbar widget type.
bool _mouse_hovering
The mouse is hovering over the same point.
WindowDefaultFlag
Window default widget/window handling flags.
@ WDF_NO_CLOSE
This window can't be interactively closed.
@ WDF_CONSTRUCTION
This window is used for construction; close it whenever changing company.
@ WDF_MODAL
The window is a modal child of some other window, meaning the parent is 'inactive'.
@ WDF_NO_FOCUS
This window won't get focus/make any other window lose focus when click.
void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip, uint paramcount=0)
Shows a tooltip.
WindowList _z_windows
List of windows opened at the screen sorted from the front to back.
SpecialMouseMode _special_mouse_mode
Mode of the mouse.
static const int WHITE_BORDER_DURATION
The initial timeout value for WF_WHITE_BORDER.
Rect ScrollRect(Rect r, const Scrollbar &sb, int resize_step=1)
Apply 'scroll' to a rect to be drawn in.
WindowPosition
How do we the window to be placed?
@ WDP_CENTER
Center the window.
@ WDP_AUTO
Find a place automatically.
@ WDP_ALIGN_TOOLBAR
Align toward the toolbar.
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
int32_t WindowNumber
Number to differentiate different windows of the same class.
EventState
State of handling an event.
@ ES_NOT_HANDLED
The passed event is not handled.
WindowClass
Window classes.
@ WC_INVALID
Invalid window.