OpenTTD Source: src/window_gui.h File Reference

OpenTTD Source 15.3

Data Structures | Typedefs | Enumerations | Functions | Variables
window_gui.h File Reference

Functions, definitions and such used only by the GUI. More...

`#include "vehiclelist.h"`
`#include "vehicle_type.h"`
`#include "viewport_type.h"`
`#include "company_type.h"`
`#include "tile_type.h"`
`#include "widget_type.h"`
`#include "string_type.h"`

Go to the source code of this file.

## Data Structures
class | WidgetDimensions
struct | WindowDesc
| High level window description. More...
struct | ResizeInfo
| Data structure for resizing a window. More...
struct | ViewportData
| Data structure for a window viewport. More...
struct | Window
| Data structure for an opened window. More...
struct | Window::WindowIterator< TtoBack >
| Iterator to iterate all valid Windows. More...
struct | Window::AllWindows< Tfront >
| Iterable ensemble of all valid Windows. More...
class | PickerWindowBase
| Base class for windows opened from a toolbar. More...

## Typedefs
using | FrameFlags = EnumBitSet< FrameFlag, uint8_t >
using | WindowList = std::list< Window * >
using | WindowDefaultFlags = EnumBitSet< WindowDefaultFlag, uint8_t >
using | WindowFlags = EnumBitSet< WindowFlag, uint16_t >

## Enumerations
enum class | FrameFlag : uint8_t { Transparent , BorderOnly , Lowered , Darkened }
| Flags to describe the look of the frame. More...
enum | WindowPosition : uint8_t { WDP_MANUAL , WDP_AUTO , WDP_CENTER , WDP_ALIGN_TOOLBAR }
| How do we the window to be placed? More...
enum class | WindowDefaultFlag : uint8_t { Construction , Modal , NoFocus , NoClose }
| Window default widget/window handling flags. More...
enum | SortButtonState : uint8_t { SBS_OFF , SBS_DOWN , SBS_UP }
| State of a sort direction button. More...
enum class | WindowFlag : uint8_t { Timeout , Dragging , SizingRight , SizingLeft , Sticky , DisableVpScroll , WhiteBorder , Highlighted , Centred }
| Window flags. More...
enum | TooltipCloseCondition : uint8_t { TCC_RIGHT_CLICK , TCC_HOVER , TCC_NONE , TCC_EXIT_VIEWPORT }
enum | SpecialMouseMode : uint8_t { WSM_NONE , WSM_DRAGDROP , WSM_SIZING , WSM_PRESIZE , WSM_DRAGGING }
| Mouse modes. More...

## Functions
void | DrawFrameRect (int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
| Draw frame rectangle.
void | DrawFrameRect (const Rect &r, Colours colour, FrameFlags flags)
void | DrawCaption (const Rect &r, Colours colour, Owner owner, TextColour text_colour, std::string_view str, StringAlignment align, FontSize fs)
| Draw a caption bar.
Point | GetToolbarAlignedWindowPosition (int window_width)
| Computer the position of the top-left corner of a window to be opened right under the toolbar.
Point | AlignInitialConstructionToolbar (int window_width)
| Compute the position of the construction toolbars.
template<class It , class Pred >
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 predicate.
Window * | BringWindowToFrontById (WindowClass cls, WindowNumber number)
| Find a window and make it the relative top-window on the screen.
Window * | FindWindowFromPt (int x, int y)
| Do a search for a window at specific coordinates.
template<typename Twindow , bool Treturn_existing = false, typename... Targs>
Twindow * | AllocateWindowDescFront (WindowDesc &desc, WindowNumber window_number, Targs... extra_arguments)
| Open a new window.
void | RelocateAllWindows (int neww, int newh)
| Relocate all windows to fit the new size of the game application screen.
void | GuiShowTooltips (Window *parent, EncodedString &&text, TooltipCloseCondition close_tooltip)
| Shows a tooltip.
WidgetID | GetWidgetFromPos (const Window *w, int x, int y)
| Returns the index for the widget located at the given position relative to the window.
void | SetFocusedWindow (Window *w)
| Set the window that has the focus.
void | ScrollbarClickHandler (Window *w, NWidgetCore *nw, int x, int y)
| Special handling for the scrollbar widget type.
Rect | ScrollRect (Rect r, const Scrollbar &sb, int resize_step=1)
| Apply 'scroll' to a rect to be drawn in.

## Variables
WindowList | _z_windows
| List of windows opened at the screen sorted from the front to back.
Window * | _focused_window
static const int | TIMEOUT_DURATION = 7
| The initial timeout value for WindowFlag::Timeout.
static const int | WHITE_BORDER_DURATION = 3
| The initial timeout value for WindowFlag::WhiteBorder.
Point | _cursorpos_drag_start
int | _scrollbar_start_pos
int | _scrollbar_size
uint8_t | _scroller_click_timeout
bool | _scrolling_viewport
| A viewport is being scrolled with the mouse.
bool | _mouse_hovering
| The mouse is hovering over the same point.
SpecialMouseMode | _special_mouse_mode
| Mode of the mouse.

## Detailed Description

Functions, definitions and such used only by the GUI.

Definition in file window_gui.h.

## Typedef Documentation

## ◆ FrameFlags

using FrameFlags = EnumBitSet<FrameFlag, uint8_t>

Definition at line 30 of file window_gui.h.

## ◆ WindowDefaultFlags

using WindowDefaultFlags = EnumBitSet<WindowDefaultFlag, uint8_t>

Definition at line 158 of file window_gui.h.

## ◆ WindowFlags

using WindowFlags = EnumBitSet<WindowFlag, uint16_t>

Definition at line 239 of file window_gui.h.

## ◆ WindowList

using WindowList = std::list<Window *>

Definition at line 136 of file window_gui.h.

## Enumeration Type Documentation

## ◆ FrameFlag

enum class FrameFlag : uint8_t
| strong

Flags to describe the look of the frame.

Enumerator
Transparent | Makes the background transparent if set.
BorderOnly | Draw border only, no background.
Lowered | If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
Darkened | If set the background is darker, allows for lowered frames with normal background colour when used with FrameFlag::Lowered (ie. dropdown boxes)

Definition at line 24 of file window_gui.h.

## ◆ SortButtonState

enum SortButtonState : uint8_t

State of a sort direction button.

Enumerator
SBS_OFF | Do not sort (with this button).
SBS_DOWN | Sort ascending.
SBS_UP | Sort descending.

Definition at line 217 of file window_gui.h.

## ◆ SpecialMouseMode

enum SpecialMouseMode : uint8_t

Mouse modes.

Enumerator
WSM_NONE | No special mouse mode.
WSM_DRAGDROP | Drag&drop an object.
WSM_SIZING | Sizing mode.
WSM_PRESIZE | Presizing mode (docks, tunnels).
WSM_DRAGGING | Dragging mode (trees).

Definition at line 1040 of file window_gui.h.

## ◆ TooltipCloseCondition

enum TooltipCloseCondition : uint8_t

Definition at line 264 of file window_gui.h.

## ◆ WindowDefaultFlag

enum class WindowDefaultFlag : uint8_t
| strong

Window default widget/window handling flags.

Enumerator
Construction | This window is used for construction; close it whenever changing company.
Modal | The window is a modal child of some other window, meaning the parent is 'inactive'.
NoFocus | This window won't get focus/make any other window lose focus when click.
NoClose | This window can't be interactively closed.

Definition at line 152 of file window_gui.h.

## ◆ WindowFlag

enum class WindowFlag : uint8_t
| strong

Window flags.

Enumerator
Timeout | Window timeout counter.
Dragging | Window is being dragged.
SizingRight | Window is being resized towards the right.
SizingLeft | Window is being resized towards the left.
Sticky | Window is made sticky by user.
DisableVpScroll | Window does not do autoscroll,. See also HandleAutoscroll().
WhiteBorder | Window white border counter bit mask.
Highlighted | Window has a widget that has a highlight.
Centred | Window is centered and shall stay centered after ReInit.

Definition at line 226 of file window_gui.h.

## ◆ WindowPosition

enum WindowPosition : uint8_t

How do we the window to be placed?

Enumerator
WDP_MANUAL | Manually align the window (so no automatic location finding)
WDP_AUTO | Find a place automatically.
WDP_CENTER | Center the window.
WDP_ALIGN_TOOLBAR | Align toward the toolbar.

Definition at line 142 of file window_gui.h.

## Function Documentation

## ◆ AlignInitialConstructionToolbar()

Point AlignInitialConstructionToolbar | ( | int | window_width | ) |

Compute the position of the construction toolbars.

If the terraform toolbar is open place them to the right/left of it, otherwise use default toolbar aligned position.

Parameters

  window_width | the width of the toolbar to get the position for.

Returns
  Coordinate of the top-left corner of the new toolbar.

Definition at line 1701 of file window.cpp.

References _current_text_dir, _settings_client, FindWindowByClass(), GetToolbarAlignedWindowPosition(), ClientSettings::gui, Window::left, GUISettings::link_terraform_toolbar, TD_RTL, Window::top, WC_SCEN_LAND_GEN, Window::width, Coord2D< T >::x, and Coord2D< T >::y.

Referenced by BuildAirToolbarWindow::OnInitialPosition(), BuildDocksToolbarWindow::OnInitialPosition(), BuildRailToolbarWindow::OnInitialPosition(), and BuildRoadToolbarWindow::OnInitialPosition().

## ◆ AllEqual()

template<class It , class Pred >

bool AllEqual | ( | It | begin,
|  | It | end,
|  | Pred | pred
| ) |  |
| inline

Generic helper function that checks if all elements of the range are equal with respect to the given predicate.

Parameters

  begin | The start of the range.
  end | The end of the range.
  pred | The predicate to use.

Returns
  True if all elements are equal, false otherwise.

Definition at line 946 of file window_gui.h.

Referenced by DepotWindow::OnVehicleSelect(), and OrdersWindow::OnVehicleSelect().

## ◆ AllocateWindowDescFront()

template<typename Twindow , bool Treturn_existing = false, typename... Targs>

Twindow * AllocateWindowDescFront | ( | WindowDesc & | desc,
|  | WindowNumber | window_number,
|  | Targs... | extra_arguments
| ) |  |

Open a new window.

Template Parameters

  Twindow | Window class to use if the window does not exist.
  Treturn_existing | If set, also return the window if it already existed.

Parameters

  desc | The pointer to the WindowDesc to be created
  window_number | the window number of the new window
  extra_arguments | optional extra arguments to pass to the window's constructor.

Returns
  Window pointer of the newly created window, or the existing one if Treturn_existing is set, or `nullptr`.

Definition at line 1016 of file window_gui.h.

References BringWindowToFrontById(), and WindowDesc::cls.

## ◆ BringWindowToFrontById()

Window * BringWindowToFrontById | ( | WindowClass | cls,
|  | WindowNumber | number
| ) |  |

Find a window and make it the relative top-window on the screen.

The window gets unshaded if it was shaded, and a white border is drawn at its edges for a brief period of time to visualize its "activation".

Parameters

  cls | WindowClass of the window to activate
  number | WindowNumber of the window to activate

Returns
  a pointer to the window thus activated

Definition at line 1278 of file window.cpp.

References BringWindowToFront(), FindWindowById(), Window::IsShaded(), Window::SetDirty(), Window::SetShaded(), and Window::SetWhiteBorder().

Referenced by AllocateWindowDescFront(), DoSelectCompanyManagerFace(), NetworkContentListWindow::OnClick(), ShowBuyCompanyDialog(), ShowCompanyFinances(), ShowDepotWindow(), ShowGenerateWorldProgress(), ShowIndustryCargoesWindow(), and ShowScriptDebugWindow().

## ◆ DrawCaption()

void DrawCaption | ( | const Rect & | r,
|  | Colours | colour,
|  | Owner | owner,
|  | TextColour | text_colour,
|  | std::string_view | str,
|  | StringAlignment | align,
|  | FontSize | fs
| ) |  |

Draw a caption bar.

Parameters

  r | Rectangle of the bar.
  colour | Colour of the window.
  owner | 'Owner' of the window.
  text_colour | Colour of the text.
  str | Text to draw in the bar.
  align | Alignment of the text.
  fs | Font size of the text.

Definition at line 736 of file widget.cpp.

References _company_colours, WidgetDimensions::bevel, BorderOnly, WidgetDimensions::captiontext, Darkened, DrawFrameRect(), DrawString(), GetAlignedPosition(), GetColourGradient(), GetStringBoundingBox(), GfxFillRect(), Lowered, WidgetDimensions::scaled, Rect::Shrink(), and Coord2D< T >::y.

Referenced by NWidgetLeaf::Draw(), and NewsWindow::DrawWidget().

## ◆ DrawFrameRect() [1/2]

void DrawFrameRect | ( | const Rect & | r,
|  | Colours | colour,
|  | FrameFlags | flags
| ) |  |
| inline

Definition at line 128 of file window_gui.h.

## ◆ DrawFrameRect() [2/2]

void DrawFrameRect | ( | int | left,
|  | int | top,
|  | int | right,
|  | int | bottom,
|  | Colours | colour,
|  | FrameFlags | flags
| ) |  |

Draw frame rectangle.

Parameters

  left | Left edge of the frame
  top | Top edge of the frame
  right | Right edge of the frame
  bottom | Bottom edge of the frame
  colour | Colour table to use.

See also
  Colours

Parameters

  flags | Flags controlling how to draw the frame.

See also
  FrameFlags

Definition at line 307 of file widget.cpp.

References BorderOnly, Darkened, FILLRECT_RECOLOUR, GetColourGradient(), GfxFillRect(), Lowered, PALETTE_TO_TRANSPARENT, WidgetDimensions::scaled, Rect::Shrink(), BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), and Transparent.

Referenced by NWidgetBackground::Draw(), NWidgetLeaf::Draw(), DrawAircraftImage(), DrawArrowButtons(), DrawBoolButton(), DrawButtonDropdown(), ButtonLine::DrawButtons(), DrawCaption(), DrawCloseBox(), DrawDropDownButton(), DrawHorizontalScrollbar(), DrawImageButtons(), DrawImageTextButtons(), DrawInset(), DrawMatrix(), DrawResizeBox(), DrawRoadVehImage(), DrawShipImage(), DrawSliderWidget(), DrawTrainImage(), DrawUpDownButtons(), NewGRFInspectWindow::DrawVehicleChainWidget(), DrawVerticalScrollbar(), GenerateProgressWindow::DrawWidget(), BaseGraphWindow::DrawWidget(), BaseCargoGraphWindow::DrawWidget(), BaseNetworkContentDownloadStatusWindow::DrawWidget(), NetworkJoinStatusWindow::DrawWidget(), ScanProgressWindow::DrawWidget(), StoryBookWindow::DrawWidget(), TransparenciesWindow::DrawWidget(), RefitWindow::DrawWidget(), and Window::DrawWidgets().

## ◆ FindWindowFromPt()

Window * FindWindowFromPt | ( | int | x,
|  | int | y
| ) |  |

Do a search for a window at specific coordinates.

For this we start at the topmost window, obviously and work our way down to the bottom

Parameters

  x | position x to query
  y | position y to query

Returns
  a pointer to the found window if any, nullptr otherwise

Definition at line 1845 of file window.cpp.

References Window::height, IsInsideBS(), Window::left, MayBeShown(), Window::top, and Window::width.

Referenced by HandleAutoscroll(), HandleMouseOver(), HandleViewportScroll(), and TooltipsWindow::OnMouseLoop().

## ◆ GetToolbarAlignedWindowPosition()

Point GetToolbarAlignedWindowPosition | ( | int | window_width | ) |

Computer the position of the top-left corner of a window to be opened right under the toolbar.

Parameters

  window_width | the width of the window to get the position for.

Returns
  Coordinate of the top-left corner of the new window.

Definition at line 1685 of file window.cpp.

References _current_text_dir, FindWindowById(), Window::height, Window::left, TD_RTL, Window::top, WC_MAIN_TOOLBAR, and Window::width.

Referenced by AlignInitialConstructionToolbar(), LocalGetWindowPlacement(), TerraformToolbarWindow::OnInitialPosition(), and TransparenciesWindow::OnInitialPosition().

## ◆ GetWidgetFromPos()

WidgetID GetWidgetFromPos | ( | const Window * | w,
|  | int | x,
|  | int | y
| ) |  |

Returns the index for the widget located at the given position relative to the window.

It includes all widget-corner pixels as well.

Parameters

  *w | Window to look inside
  x | The Window client X coordinate
  y | The Window client y coordinate

Returns
  A widget index, or `INVALID_WIDGET` if no widget was found.

Definition at line 292 of file widget.cpp.

References INVALID_WIDGET, and Window::nested_root.

Referenced by DropdownWindow::GetDropDownItem(), HandleMouseDragDrop(), and Window::OnDropdownClose().

## ◆ GuiShowTooltips()

void GuiShowTooltips | ( | Window * | parent,
|  | EncodedString && | text,
|  | TooltipCloseCondition | close_tooltip
| ) |  |

Shows a tooltip.

Parameters

  parent | The window this tooltip is related to.
  text | String to be displayed. May include encoded parameters.
  close_tooltip | the condition under which the tooltip closes

Definition at line 693 of file misc_gui.cpp.

References CloseWindowById(), CursorVars::in_window, and WC_TOOLTIPS.

Referenced by DispatchHoverEvent(), DispatchRightClickEvent(), DepotWindow::OnRightClick(), ReplaceVehicleWindow::OnTooltip(), CheatWindow::OnTooltip(), IndustryCargoesWindow::OnTooltip(), LinkGraphLegendWindow::OnTooltip(), NetworkClientListWindow::OnTooltip(), BuildRailToolbarWindow::OnTooltip(), TextfileWindow::OnTooltip(), VehicleDetailsWindow::OnTooltip(), and ShowMeasurementTooltips().

## ◆ RelocateAllWindows()

void RelocateAllWindows | ( | int | neww,
|  | int | newh
| ) |  |

Relocate all windows to fit the new size of the game application screen.

Parameters

  neww | New width of the game application screen
  newh | New height of the game application screen.

Definition at line 3529 of file window.cpp.

References _toolbar_width, Centred, CloseWindowByClass(), EnsureVisibleCaption(), FindWindowById(), Window::flags, Window::height, IConsoleResize(), Window::left, PositionMainToolbar(), PositionNetworkChatWindow(), PositionNewsMessage(), PositionStatusbar(), ResizeWindow(), BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), Window::top, WC_BOOTSTRAP, WC_CONSOLE, WC_DROPDOWN_MENU, WC_ENDSCREEN, WC_HIGHSCORE, WC_MAIN_TOOLBAR, WC_MAIN_WINDOW, WC_NEWS_WINDOW, WC_SEND_NETWORK_MSG, WC_STATUS_BAR, Window::width, and Window::window_class.

Referenced by GameSizeChanged(), and ReInitAllWindows().

## ◆ ScrollbarClickHandler()

void ScrollbarClickHandler | ( | Window * | w,
|  | NWidgetCore * | nw,
|  | int | x,
|  | int | y
| ) |  |

Special handling for the scrollbar widget type.

Handles the special scrolling buttons and other scrolling.

Parameters

  w | Window on which a scroll was performed.
  nw | Pointer to the scrollbar widget.
  x | The X coordinate of the mouse click.
  y | The Y coordinate of the mouse click.

Definition at line 268 of file widget.cpp.

References NWidgetBase::current_x, NWidgetBase::current_y, NWID_HSCROLLBAR, NWidgetBase::pos_x, NWidgetBase::pos_y, ScrollbarClickPositioning(), and NWidgetBase::type.

Referenced by DispatchLeftClickEvent().

## ◆ ScrollRect()

Rect ScrollRect | ( | Rect | r,
|  | const Scrollbar & | sb,
|  | int | resize_step
| ) |  |

Apply 'scroll' to a rect to be drawn in.

Parameters

  r | Rect to be 'scrolled'.
  sb | The scrollbar affecting the scroll.
  resize_step | Resize step of the widget/scrollbar (1 if the scrollbar is pixel-based.)

Returns
  Scrolled rect.

Definition at line 2544 of file widget.cpp.

References _current_text_dir, Scrollbar::GetCount(), Scrollbar::GetPosition(), Scrollbar::IsVertical(), and TD_RTL.

Referenced by IndustryDirectoryWindow::DrawWidget(), TextfileWindow::DrawWidget(), and ScriptDebugWindow::DrawWidgetLog().

## ◆ SetFocusedWindow()

void SetFocusedWindow | ( | Window * | w | ) |

Set the window that has the focus.

Parameters

  w | The window to set the focus on

Definition at line 423 of file window.cpp.

References Window::nested_focus, Window::OnFocus(), Window::OnFocusLost(), NWidgetBase::SetDirty(), WC_TOOLTIPS, and Window::window_class.

Referenced by DispatchLeftClickEvent(), Window::InitializeData(), Window::OnHotkey(), PickerWindow::OnHotkey(), and ShowQuery().

## Variable Documentation

## ◆ _cursorpos_drag_start

Point _cursorpos_drag_start
| extern

Definition at line 87 of file window.cpp.

## ◆ _focused_window

Window* _focused_window
| extern

Definition at line 85 of file window.cpp.

## ◆ _mouse_hovering

bool _mouse_hovering
| extern

The mouse is hovering over the same point.

Definition at line 94 of file window.cpp.

Referenced by HandleMouseEvents(), InitWindowSystem(), and TooltipsWindow::OnMouseLoop().

## ◆ _scrollbar_size

int _scrollbar_size
| extern

Definition at line 90 of file window.cpp.

## ◆ _scrollbar_start_pos

int _scrollbar_start_pos
| extern

Definition at line 89 of file window.cpp.

## ◆ _scroller_click_timeout

uint8_t _scroller_click_timeout
| extern

Definition at line 91 of file window.cpp.

## ◆ _scrolling_viewport

bool _scrolling_viewport
| extern

A viewport is being scrolled with the mouse.

Definition at line 93 of file window.cpp.

Referenced by HandleViewportScroll(), InitWindowSystem(), and SmallMapWindow::OnRightClick().

## ◆ _special_mouse_mode

SpecialMouseMode _special_mouse_mode
| extern

Mode of the mouse.

Definition at line 96 of file window.cpp.

Referenced by DeleteDepotHighlightOfVehicle(), DeleteGroupHighlightOfVehicle(), HandleMouseDragDrop(), SetObjectToPlace(), VpHandlePlaceSizingDrag(), VpStartDragging(), and VpStartPlaceSizing().

## ◆ _z_windows

WindowList _z_windows
| extern

List of windows opened at the screen sorted from the front to back.

Definition at line 60 of file window.cpp.

Referenced by BringWindowToFront(), Window::DeleteClosedWindows(), UnInitWindowSystem(), and Window::Window().

## ◆ TIMEOUT_DURATION

const int TIMEOUT_DURATION = 7
| static

The initial timeout value for WindowFlag::Timeout.

Definition at line 241 of file window_gui.h.

Referenced by Window::HandleButtonClick(), and Window::SetTimeout().

## ◆ WHITE_BORDER_DURATION

const int WHITE_BORDER_DURATION = 3
| static

The initial timeout value for WindowFlag::WhiteBorder.

Definition at line 242 of file window_gui.h.

Referenced by Window::SetWhiteBorder().