# window.cpp File Reference

windowing system, widgets and events More...

`#include "stdafx.h"`
`#include <stdarg.h>`
`#include "openttd.h"`
`#include "debug.h"`
`#include "player_func.h"`
`#include "gfx_func.h"`
`#include "console.h"`
`#include "viewport_func.h"`
`#include "variables.h"`
`#include "genworld.h"`
`#include "blitter/factory.hpp"`
`#include "window_gui.h"`
`#include "zoom_func.h"`
`#include "core/alloc_func.hpp"`
`#include "map_func.h"`
`#include "vehicle_base.h"`
`#include "settings_type.h"`
`#include "table/sprites.h"`

Go to the source code of this file.

## Data Structures
struct | SizeRect
## Defines
#define | scrollspeed 3
## Enumerations
enum | MouseClick { MC_NONE = 0, MC_LEFT, MC_RIGHT, MC_DOUBLE_LEFT, MAX_OFFSET_DOUBLE_CLICK = 5, TIME_BETWEEN_DOUBLE_CLICK = 500 }
## Functions
static void | StartWindowDrag (Window *w)
static void | StartWindowSizing (Window *w)
static void | DispatchLeftClickEvent (Window *w, int x, int y, bool double_click)
static void | DispatchRightClickEvent (Window *w, int x, int y)
static void | DispatchMouseWheelEvent (Window *w, int widget, int wheel)
| Dispatch the mousewheel-action to the window which will scroll any compatible scrollbars if the mouse is pointed over the bar or its contents.
static void | DrawOverlappedWindow (Window *const *wz, int left, int top, int right, int bottom)
void | DrawOverlappedWindowForAll (int left, int top, int right, int bottom)
void | CallWindowEventNP (Window *w, int event)
void | SetWindowDirty (const Window *w)
| Marks the window as dirty for repaint.
static Window * | FindChildWindow (const Window *w)
| Find the Window whose parent pointer points to this window.
Window ** | FindWindowZPosition (const Window *w)
| Find the z-value of a window.
void | DeleteWindow (Window *w)
Window * | FindWindowById (WindowClass cls, WindowNumber number)
void | DeleteWindowById (WindowClass cls, WindowNumber number)
void | DeleteWindowByClass (WindowClass cls)
void | DeletePlayerWindows (PlayerID id)
| Delete all windows of a player.
void | ChangeWindowOwner (PlayerID old_player, PlayerID new_player)
| Change the owner of all the windows one player can take over from another player in the case of a company merger.
static void | BringWindowToFront (const Window *w)
| On clicking on a window, make it the frontmost window of all.
Window * | BringWindowToFrontById (WindowClass cls, WindowNumber number)
| Find a window and make it the top-window on the screen.
static bool | IsVitalWindow (const Window *w)
static Window * | FindDeletableWindow ()
| We have run out of windows, so find a suitable candidate for replacement.
static Window * | ForceFindDeletableWindow ()
| A window must be freed, and all are marked as important windows.
bool | IsWindowOfPrototype (const Window *w, const Widget *widget)
void | AssignWidgetToWindow (Window *w, const Widget *widget)
| Copies 'widget' to 'w->widget' to allow for resizable windows.
static Window * | FindFreeWindow ()
static Window * | LocalAllocateWindow (int x, int y, int min_width, int min_height, int def_width, int def_height, WindowProc *proc, WindowClass cls, const Widget *widget, int window_number, void *data)
| Open a new window.
Window * | AllocateWindow (int x, int y, int width, int height, WindowProc *proc, WindowClass cls, const Widget *widget, void *data)
| Open a new window.
static bool | IsGoodAutoPlace1 (int left, int top, int width, int height, Point &pos)
static bool | IsGoodAutoPlace2 (int left, int top, int width, int height, Point &pos)
static Point | GetAutoPlacePosition (int width, int height)
static Window * | LocalAllocateWindowDesc (const WindowDesc *desc, int window_number, void *data)
| Set the x and y coordinates of a new window.
Window * | AllocateWindowDesc (const WindowDesc *desc, void *data)
| Open a new window.
Window * | AllocateWindowDescFront (const WindowDesc *desc, int window_number, void *data)
| Open a new window.
Window * | FindWindowFromPt (int x, int y)
| Do a search for a window at specific coordinates.
void | InitWindowSystem ()
void | UnInitWindowSystem ()
void | ResetWindowSystem ()
static void | DecreaseWindowCounters ()
Window * | GetCallbackWnd ()
static void | HandlePlacePresize ()
static bool | HandleDragDrop ()
static bool | HandlePopupMenu ()
static bool | HandleMouseOver ()
void | ResizeWindow (Window *w, int x, int y)
| Update all the widgets of a window based on their resize flags Both the areas of the old window and the new sized window are set dirty ensuring proper redrawal.
static bool | HandleWindowDragging ()
static bool | HandleScrollbarScrolling ()
static bool | HandleViewportScroll ()
static bool | MaybeBringWindowToFront (const Window *w)
| Check if a window can be made top-most window, and if so do it.
static void | SendWindowMessageW (Window *w, uint msg, uint wparam, uint lparam)
| Send a message from one window to another.
void | SendWindowMessage (WindowClass wnd_class, WindowNumber wnd_num, int msg, int wparam, int lparam)
| Send a message from one window to another.
void | SendWindowMessageClass (WindowClass wnd_class, int msg, int wparam, int lparam)
| Send a message from one window to another.
void | HandleKeypress (uint32 key)
| Handle keyboard input.
void | HandleCtrlChanged ()
void | UpdateTileSelection ()
| Updates tile highlighting for all cases.
bool | VpHandlePlaceSizingDrag ()
| while dragging
static void | HandleAutoscroll ()
void | MouseLoop (MouseClick click, int mousewheel)
void | HandleMouseEvents ()
void | InputLoop ()
void | UpdateWindows ()
int | GetMenuItemIndex (const Window *w, int x, int y)
void | InvalidateWindow (WindowClass cls, WindowNumber number)
void | InvalidateWindowWidget (WindowClass cls, WindowNumber number, byte widget_index)
void | InvalidateWindowClasses (WindowClass cls)
void | InvalidateThisWindowData (Window *w)
void | InvalidateWindowData (WindowClass cls, WindowNumber number)
void | InvalidateWindowClassesData (WindowClass cls)
void | CallWindowTickEvent ()
void | DeleteNonVitalWindows ()
void | DeleteAllNonVitalWindows ()
| It is possible that a stickied window gets to a position where the 'close' button is outside the gaming area.
void | HideVitalWindows ()
| Delete all always on-top windows to get an empty screen.
int | PositionMainToolbar (Window *w)
void | RelocateAllWindows (int neww, int newh)
## Variables
static Point | _drag_delta
static Window | _windows [MAX_NUMBER_OF_WINDOWS]
Window * | _z_windows [lengthof(_windows)]
Window ** | _last_z_window
| always points to the next free space in the z-array
Point | _cursorpos_drag_start
int | _scrollbar_start_pos
int | _scrollbar_size
byte | _scroller_click_timeout
bool | _scrolling_scrollbar
bool | _scrolling_viewport
bool | _popup_menu_active
byte | _special_mouse_mode
static bool | _dragging_window
static int | _input_events_this_tick = 0

## Detailed Description

windowing system, widgets and events

Definition in file window.cpp.

## Enumeration Type Documentation

enum MouseClick

Enumerator:

  MAX_OFFSET_DOUBLE_CLICK | How much the mouse is allowed to move to call it a double click.
  TIME_BETWEEN_DOUBLE_CLICK | Time between 2 left clicks before it becoming a double click, in ms.

Definition at line 1770 of file window.cpp.

## Function Documentation

Window* AllocateWindow | ( | int | x,
|  | int | y,
|  | int | width,
|  | int | height,
|  | WindowProc * | proc,
|  | WindowClass | cls,
|  | const Widget * | widget,
|  | void * | data |
| ) |  |  |

Open a new window.

If there is no space for a new window, close an open window. Try to avoid stickied windows, but if there is no else, close one of those as well. Then make sure all created windows are below some always-on-top ones. Finally set all variables and call the WE_CREATE event

Parameters:

  | x | offset in pixels from the left of the screen
  | y | offset in pixels from the top of the screen
  | width | width in pixels of the window
  | height | height in pixels of the window
  | *proc | see WindowProc function to call when any messages/updates happen to the window
  | cls | see WindowClass class of the window, used for identification and grouping
  | *widget | see Widget pointer to the window layout and various elements

Returns:
  Window pointer of the newly created window

Definition at line 755 of file window.cpp.

References LocalAllocateWindow().

Referenced by GuiShowTooltipsWithArgs(), and PopupMainToolbMenu().

Window* AllocateWindowDesc | ( | const WindowDesc * | desc,
|  | void * | data |
| ) |  |  |

Open a new window.

Parameters:

  | *desc | The pointer to the WindowDesc to be created
  | data | arbitrary data that is send with the WE_CREATE message

Returns:
  Window pointer of the newly created window

Definition at line 949 of file window.cpp.

References LocalAllocateWindowDesc().

Referenced by ShowEndGameChart(), ShowHighscoreTable(), ShowMessageHistory(), ShowNewGRFSettings(), ShowNewspaper(), ShowQuery(), ShowQueryString(), and ShowSignalBuilder().

Window* AllocateWindowDescFront | ( | const WindowDesc * | desc,
|  | int | window_number,
|  | void * | data |
| ) |  |  |

Open a new window.

Parameters:

  | *desc | The pointer to the WindowDesc to be created
  | window_number | the window number of the new window
  | data | arbitrary data that is send with the WE_CREATE message

Returns:
  see Window pointer of the newly created window

Definition at line 961 of file window.cpp.

References BringWindowToFrontById(), and LocalAllocateWindowDesc().

Referenced by DoSelectPlayerFace(), PlayerCompanyWndProc(), ShowDepotWindow(), ShowGenerateWorldProgress(), ShowPlayerStations(), ShowStationViewWindow(), ShowVehicleDetailsWindow(), ShowVehicleRefitWindow(), and ShowVehicleViewWindow().

void AssignWidgetToWindow | ( | Window * | w,
|  | const Widget * | widget |
| ) |  |  |

Copies 'widget' to 'w->widget' to allow for resizable windows.

Parameters:

  | w | Window on which to attach the widget array
  | widget | pointer of widget array to fill the window with

Definition at line 570 of file window.cpp.

References ReallocT(), and WWT_LAST.

Referenced by LocalAllocateWindow().

static void BringWindowToFront | ( | const Window * | w | ) | `[static]`

On clicking on a window, make it the frontmost window of all.

However there are certain windows that always need to be on-top; these include

- Toolbar, Statusbar (always on)
- New window, Chatbar (only if open) The window is marked dirty for a repaint if the window is actually moved
  Parameters:

    | w | window that is put into the foreground

  Returns:
    pointer to the window, the same as the input pointer

Definition at line 503 of file window.cpp.

References _last_z_window, FindWindowZPosition(), and SetWindowDirty().

Referenced by BringWindowToFrontById(), and MaybeBringWindowToFront().

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

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

The window gets a white border 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 476 of file window.cpp.

References BringWindowToFront(), and SetWindowDirty().

Referenced by AllocateWindowDescFront().

void ChangeWindowOwner | ( | PlayerID | old_player,
|  | PlayerID | new_player |
| ) |  |  |

Change the owner of all the windows one player can take over from another player in the case of a company merger.

Do not change ownership of windows that need to be deleted once takeover is complete

Parameters:

  | old_player | PlayerID of original owner of the window
  | new_player | PlayerID of the new owner of the window

Definition at line 447 of file window.cpp.

void DeleteAllNonVitalWindows | ( |  | ) |

It is possible that a stickied window gets to a position where the 'close' button is outside the gaming area.

You cannot close it then; except with this function. It closes all windows calling the standard function, then, does a little hacked loop of closing all stickied windows. Note that standard windows (status bar, etc.) are not stickied, so these aren't affected

Definition at line 2069 of file window.cpp.

Referenced by GenerateWorld().

void DeletePlayerWindows | ( | PlayerID | id | ) |

Delete all windows of a player.

We identify windows of a player by looking at the caption colour. If it is equal to the player ID then we say the window belongs to the player and should be deleted

Parameters:

  | id | PlayerID player identifier

Definition at line 422 of file window.cpp.

Referenced by CmdPlayerCtrl().

static void DispatchMouseWheelEvent | ( | Window * | w,
|  | int | widget,
|  | int | wheel |
| ) |  |  | `[static]`

Dispatch the mousewheel-action to the window which will scroll any compatible scrollbars if the mouse is pointed over the bar or its contents.

Parameters:

  | *w | Window
  | widget | the widget where the scrollwheel was used
  | wheel | scroll up or down

Definition at line 206 of file window.cpp.

References Clamp(), SetWindowDirty(), and WWT_SCROLL2BAR.

static Window* FindChildWindow | ( | const Window * | w | ) | `[static]`

Find the Window whose parent pointer points to this window.

Parameters:

  | w | parent Window to find child of

Returns:
  a Window pointer that is the child of w, or NULL otherwise

Definition at line 322 of file window.cpp.

static Window* FindDeletableWindow | ( |  | ) | `[static]`

We have run out of windows, so find a suitable candidate for replacement.

Keep all important windows intact. These are

- Main window (gamefield), Toolbar, Statusbar (always on)
- News window, Chatbar (when on)
- Any sticked windows since we wanted to keep these
  Returns:
    w pointer to the window that is going to be deleted

Definition at line 531 of file window.cpp.

Referenced by LocalAllocateWindow().

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, NULL otherwise

Definition at line 975 of file window.cpp.

References _last_z_window, and IsInsideBS().

Window** FindWindowZPosition | ( | const Window * | w | ) |

Find the z-value of a window.

A window must already be open or the behaviour is undefined but function should never fail

Parameters:

  | w | window to query Z Position

Returns:
  the window that matches it

Definition at line 338 of file window.cpp.

References _last_z_window.

Referenced by BringWindowToFront(), and MaybeBringWindowToFront().

static Window* ForceFindDeletableWindow | ( |  | ) | `[static]`

A window must be freed, and all are marked as important windows.

Ease the restriction a bit by allowing to delete sticky windows. Keep important/vital windows intact (Main window, Toolbar, Statusbar, News Window, Chatbar) Start finding an appropiate candidate from the lowest z-values (bottom)

See also:
  FindDeletableWindow()

Returns:
  w Pointer to the window that is being deleted

Definition at line 551 of file window.cpp.

References _last_z_window.

Referenced by LocalAllocateWindow().

void HandleKeypress | ( | uint32 | key | ) |

Handle keyboard input.

Parameters:

  | key | Lower 8 bits contain the ASCII character, the higher 16 bits the keycode

Definition at line 1637 of file window.cpp.

References _last_z_window, GB(), and IsGeneratingWorld().

static Window* LocalAllocateWindow | ( | int | x,
|  | int | y,
|  | int | min_width,
|  | int | min_height,
|  | int | def_width,
|  | int | def_height,
|  | WindowProc * | proc,
|  | WindowClass | cls,
|  | const Widget * | widget,
|  | int | window_number,
|  | void * | data |
| ) |  |  | `[static]`

Open a new window.

This function is called from AllocateWindow() or AllocateWindowDesc() See descriptions for those functions for usage See AllocateWindow() for description of arguments. Only addition here is window_number, which is the window_number being assigned to the new window

Parameters:

  | x | offset in pixels from the left of the screen
  | y | offset in pixels from the top of the screen
  | min_width | minimum width in pixels of the window
  | min_height | minimum height in pixels of the window
  | def_width | default width in pixels of the window
  | def_height | default height in pixels of the window
  | *proc | see WindowProc function to call when any messages/updates happen to the window
  | cls | see WindowClass class of the window, used for identification and grouping
  | *widget | see Widget pointer to the window layout and various elements
  | window_number | number being assigned to the new window
  | data | the data to be given during the WE_CREATE message

Returns:
  Window pointer of the newly created window

Definition at line 628 of file window.cpp.

References _last_z_window, AssignWidgetToWindow(), FindDeletableWindow(), ForceFindDeletableWindow(), max(), min(), ResizeWindow(), and SetWindowDirty().

Referenced by AllocateWindow(), and LocalAllocateWindowDesc().

static Window* LocalAllocateWindowDesc | ( | const WindowDesc * | desc,
|  | int | window_number,
|  | void * | data |
| ) |  |  | `[static]`

Set the x and y coordinates of a new window.

Parameters:

  | *desc | The pointer to the WindowDesc to be created
  | window_number | the window number of the new window
  | data | arbitrary data that is send with the WE_CREATE message

Returns:
  see Window pointer of the newly created window

Definition at line 880 of file window.cpp.

References LocalAllocateWindow(), WDP_ALIGN_TBL, WDP_ALIGN_TBR, WDP_AUTO, and WDP_CENTER.

Referenced by AllocateWindowDesc(), and AllocateWindowDescFront().

static bool MaybeBringWindowToFront | ( | const Window * | w | ) | `[static]`

Check if a window can be made top-most window, and if so do it.

If a window does not obscure any other windows, it will not be brought to the foreground. Also if the only obscuring windows are so-called system-windows, the window will not be moved. The function will return false when a child window of this window is a modal-popup; function returns a false and child window gets a white border

Parameters:

  | w | Window to bring on-top

Returns:
  false if the window has an active modal child, true otherwise

Definition at line 1541 of file window.cpp.

References _last_z_window, BringWindowToFront(), FindWindowZPosition(), SetWindowDirty(), and WDF_MODAL.

void ResizeWindow | ( | Window * | w,
|  | int | x,
|  | int | y |
| ) |  |  |

Update all the widgets of a window based on their resize flags Both the areas of the old window and the new sized window are set dirty ensuring proper redrawal.

Parameters:

  | w | Window to resize
  | x | delta x-size of changed window (positive if larger, etc.)
  | y | delta y-size of changed window

Definition at line 1167 of file window.cpp.

References GB(), RESIZE_BOTTOM, RESIZE_LEFT, RESIZE_NONE, RESIZE_RIGHT, RESIZE_TOP, SetWindowDirty(), and WWT_LAST.

Referenced by CreateVehicleDetailsWindow(), LocalAllocateWindow(), and ShowPlayerStations().

void SendWindowMessage | ( | WindowClass | wnd_class,
|  | WindowNumber | wnd_num,
|  | int | msg,
|  | int | wparam,
|  | int | lparam |
| ) |  |  |

Send a message from one window to another.

The receiving window is found by

Parameters:

  | wnd_class | see WindowClass class AND
  | wnd_num | see WindowNumber number, mostly 0
  | msg | Specifies the message to be sent
  | wparam | Specifies additional message-specific information
  | lparam | Specifies additional message-specific information

Definition at line 1612 of file window.cpp.

References SendWindowMessageW().

Referenced by SaveFileDone(), and SaveFileStart().

void SendWindowMessageClass | ( | WindowClass | wnd_class,
|  | int | msg,
|  | int | wparam,
|  | int | lparam |
| ) |  |  |

Send a message from one window to another.

The message will be sent to ALL windows of the windowclass specified in the first parameter

Parameters:

  | wnd_class | see WindowClass class
  | msg | Specifies the message to be sent
  | wparam | Specifies additional message-specific information
  | lparam | Specifies additional message-specific information

Definition at line 1625 of file window.cpp.

References SendWindowMessageW().

static void SendWindowMessageW | ( | Window * | w,
|  | uint | msg,
|  | uint | wparam,
|  | uint | lparam |
| ) |  |  | `[static]`

Send a message from one window to another.

The receiving window is found by

Parameters:

  | w | Window pointer pointing to the other window
  | msg | Specifies the message to be sent
  | wparam | Specifies additional message-specific information
  | lparam | Specifies additional message-specific information

Definition at line 1593 of file window.cpp.

Referenced by SendWindowMessage(), and SendWindowMessageClass().

void UpdateTileSelection | ( |  | ) |

Updates tile highlighting for all cases.

Uses _thd.selstart and _thd.selend and _thd.place_mode (set elsewhere) to determine _thd.pos and _thd.size Also drawstyle is determined. Uses _thd.new.* as a buffer and calls SetSelectionTilesDirty() twice, Once for the old and once for the new selection. _thd is TileHighlightData, found in viewport.h Called by MouseLoop() in windows.cpp

Definition at line 2250 of file viewport.cpp.

References GetAutorailHT(), SetSelectionTilesDirty(), Swap(), TILE_SIZE, VHM_NONE, VHM_POINT, VHM_RAIL, VHM_RECT, and VHM_SPECIAL.