OpenTTD
Functions
widget.cpp File Reference

Handling of the default/simple widgets. More...

#include "stdafx.h"
#include "company_func.h"
#include "window_gui.h"
#include "viewport_func.h"
#include "zoom_func.h"
#include "strings_func.h"
#include "transparency.h"
#include "core/geometry_func.hpp"
#include "settings_type.h"
#include "querystring_gui.h"
#include "table/sprites.h"
#include "table/strings.h"
#include "table/string_colours.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

static Point HandleScrollbarHittest (const Scrollbar *sb, int top, int bottom, bool horizontal)
 Compute the vertical position of the draggable part of scrollbar. More...
 
static void ScrollbarClickPositioning (Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma)
 Compute new position of the scrollbar after a click and updates the window flags. More...
 
void ScrollbarClickHandler (Window *w, NWidgetCore *nw, int x, int y)
 Special handling for the scrollbar widget type. More...
 
int GetWidgetFromPos (const Window *w, int x, int y)
 Returns the index for the widget located at the given position relative to the window. More...
 
void DrawFrameRect (int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
 Draw frame rectangle. More...
 
static void DrawImageButtons (const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
 Draw an image button. More...
 
static void DrawLabel (const Rect &r, WidgetType type, bool clicked, StringID str)
 Draw the label-part of a widget. More...
 
static void DrawText (const Rect &r, TextColour colour, StringID str)
 Draw text. More...
 
static void DrawInset (const Rect &r, Colours colour, StringID str)
 Draw an inset widget. More...
 
static void DrawMatrix (const Rect &r, Colours colour, bool clicked, uint16 data, uint resize_x, uint resize_y)
 Draw a matrix widget. More...
 
static void DrawVerticalScrollbar (const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
 Draw a vertical scrollbar. More...
 
static void DrawHorizontalScrollbar (const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
 Draw a horizontal scrollbar. More...
 
static void DrawFrame (const Rect &r, Colours colour, StringID str)
 Draw a frame widget. More...
 
static void DrawShadeBox (const Rect &r, Colours colour, bool clicked)
 Draw a shade box. More...
 
static void DrawStickyBox (const Rect &r, Colours colour, bool clicked)
 Draw a sticky box. More...
 
static void DrawDefSizeBox (const Rect &r, Colours colour, bool clicked)
 Draw a defsize box. More...
 
static void DrawDebugBox (const Rect &r, Colours colour, bool clicked)
 Draw a NewGRF debug box. More...
 
static void DrawResizeBox (const Rect &r, Colours colour, bool at_left, bool clicked)
 Draw a resize box. More...
 
static void DrawCloseBox (const Rect &r, Colours colour)
 Draw a close box. More...
 
void DrawCaption (const Rect &r, Colours colour, Owner owner, StringID str)
 Draw a caption bar. More...
 
static void DrawButtonDropdown (const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
 Draw a button with a dropdown (WWT_DROPDOWN and NWID_BUTTON_DROPDOWN). More...
 
static void DrawDropdown (const Rect &r, Colours colour, bool clicked, StringID str)
 Draw a dropdown WWT_DROPDOWN widget. More...
 
static int MakeNWidget (const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
 Construct a single nested widget in *dest from its parts. More...
 
static int MakeWidgetTree (const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
 Build a nested widget tree by recursively filling containers with nested widgets read from their parts. More...
 
NWidgetContainerMakeNWidgets (const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
 Construct a nested widget tree from an array of parts. More...
 
NWidgetContainerMakeWindowNWidgetTree (const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
 Make a nested widget tree for a window from a parts array. More...
 
NWidgetBaseMakeCompanyButtonRows (int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
 Make a number of rows with button-like graphics, for enabling/disabling each company. More...
 

Detailed Description

Handling of the default/simple widgets.

Definition in file widget.cpp.

Function Documentation

◆ DrawButtonDropdown()

static void DrawButtonDropdown ( const Rect r,
Colours  colour,
bool  clicked_button,
bool  clicked_dropdown,
StringID  str 
)
inlinestatic

Draw a button with a dropdown (WWT_DROPDOWN and NWID_BUTTON_DROPDOWN).

Parameters
rRectangle containing the widget.
colourBackground colour of the widget.
clicked_buttonThe button-part is lowered.
clicked_dropdownThe drop-down part is lowered.
strText of the button.
Note
Magic constants are also used in NWidgetLeaf::ButtonHit.

Definition at line 568 of file widget.cpp.

References _current_text_dir, DrawFrameRect(), NWidgetLeaf::dropdown_dimension, FONT_HEIGHT_NORMAL, FR_LOWERED, max(), and TD_LTR.

Referenced by DrawDropdown().

◆ DrawCaption()

void DrawCaption ( const Rect r,
Colours  colour,
Owner  owner,
StringID  str 
)

Draw a caption bar.

Parameters
rRectangle of the bar.
colourColour of the window.
owner'Owner' of the window.
strText to draw in the bar.

Definition at line 540 of file widget.cpp.

References _colour_gradient, _company_colours, DrawFrameRect(), DrawString(), FR_BORDERONLY, FR_DARKENED, FR_LOWERED, GetStringBoundingBox(), GfxFillRect(), max(), MAX_COMPANIES, WD_CAPTIONTEXT_LEFT, and WD_CAPTIONTEXT_RIGHT.

Referenced by NewsWindow::DrawWidget().

◆ DrawCloseBox()

static void DrawCloseBox ( const Rect r,
Colours  colour 
)
inlinestatic

Draw a close box.

Parameters
rRectangle of the box.
colourColour of the close box.

Definition at line 525 of file widget.cpp.

◆ DrawDebugBox()

static void DrawDebugBox ( const Rect r,
Colours  colour,
bool  clicked 
)
inlinestatic

Draw a NewGRF debug box.

Parameters
rRectangle of the box.
colourColour of the debug box.
clickedBox is lowered.

Definition at line 494 of file widget.cpp.

References DrawImageButtons(), and WWT_DEBUGBOX.

◆ DrawDefSizeBox()

static void DrawDefSizeBox ( const Rect r,
Colours  colour,
bool  clicked 
)
inlinestatic

Draw a defsize box.

Parameters
rRectangle of the box.
colourColour of the defsize box.
clickedBox is lowered.

Definition at line 483 of file widget.cpp.

References DrawImageButtons(), and WWT_DEFSIZEBOX.

◆ DrawDropdown()

static void DrawDropdown ( const Rect r,
Colours  colour,
bool  clicked,
StringID  str 
)
inlinestatic

Draw a dropdown WWT_DROPDOWN widget.

Parameters
rRectangle containing the widget.
colourBackground colour of the widget.
clickedThe widget is lowered.
strText of the button.

Definition at line 596 of file widget.cpp.

References DrawButtonDropdown().

◆ DrawFrame()

static void DrawFrame ( const Rect r,
Colours  colour,
StringID  str 
)
inlinestatic

Draw a frame widget.

Parameters
rRectangle of the frame.
colourColour of the frame.
strText of the frame.

Definition at line 411 of file widget.cpp.

References _colour_gradient, _current_text_dir, DrawString(), FONT_HEIGHT_NORMAL, GfxFillRect(), TD_LTR, WD_FRAMETEXT_LEFT, and WD_FRAMETEXT_RIGHT.

◆ DrawFrameRect()

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

Draw frame rectangle.

Parameters
leftLeft edge of the frame
topTop edge of the frame
rightRight edge of the frame
bottomBottom edge of the frame
colourColour table to use.
See also
_colour_gradient
Parameters
flagsFlags controlling how to draw the frame.
See also
FrameFlags

Definition at line 177 of file widget.cpp.

Referenced by DrawButtonDropdown(), DrawCaption(), DrawInset(), DrawMatrix(), DrawResizeBox(), BaseNetworkContentDownloadStatusWindow::DrawWidget(), GenerateProgressWindow::DrawWidget(), ScanProgressWindow::DrawWidget(), and Window::DrawWidgets().

◆ DrawHorizontalScrollbar()

static void DrawHorizontalScrollbar ( const Rect r,
Colours  colour,
bool  left_clicked,
bool  bar_dragged,
bool  right_clicked,
const Scrollbar scrollbar 
)
inlinestatic

Draw a horizontal scrollbar.

Parameters
rRectangle of the scrollbar widget.
colourColour of the scrollbar widget.
left_clickedLeft-arrow is clicked.
bar_draggedBar is dragged.
right_clickedRight-arrow is clicked.
scrollbarScrollbar size, offset, and capacity information.

Definition at line 376 of file widget.cpp.

◆ DrawImageButtons()

static void DrawImageButtons ( const Rect r,
WidgetType  type,
Colours  colour,
bool  clicked,
SpriteID  img 
)
inlinestatic

Draw an image button.

Parameters
rRectangle of the button.
typeWidget type (WWT_IMGBTN or WWT_IMGBTN_2).
colourColour of the button.
clickedButton is lowered.
imgSprite to draw.

Definition at line 218 of file widget.cpp.

Referenced by DrawDebugBox(), DrawDefSizeBox(), DrawShadeBox(), and DrawStickyBox().

◆ DrawInset()

static void DrawInset ( const Rect r,
Colours  colour,
StringID  str 
)
inlinestatic

Draw an inset widget.

Parameters
rRectangle of the background.
colourColour of the inset.
strText to draw.

Definition at line 263 of file widget.cpp.

References DrawFrameRect(), DrawString(), FR_DARKENED, FR_LOWERED, WD_INSET_LEFT, WD_INSET_RIGHT, and WD_INSET_TOP.

◆ DrawLabel()

static void DrawLabel ( const Rect r,
WidgetType  type,
bool  clicked,
StringID  str 
)
inlinestatic

Draw the label-part of a widget.

Parameters
rRectangle of the label background.
typeWidget type (WWT_TEXTBTN, WWT_TEXTBTN_2, or WWT_LABEL).
clickedLabel is rendered lowered.
strText to draw.

Definition at line 235 of file widget.cpp.

◆ DrawMatrix()

static void DrawMatrix ( const Rect r,
Colours  colour,
bool  clicked,
uint16  data,
uint  resize_x,
uint  resize_y 
)
inlinestatic

Draw a matrix widget.

Parameters
rRectangle of the matrix background.
colourColour of the background.
clickedMatrix is rendered lowered.
dataData of the widget, number of rows and columns of the widget.
resize_xMatrix resize unit size.
resize_yMatrix resize unit size.

Definition at line 278 of file widget.cpp.

References DrawFrameRect(), and FR_LOWERED.

Referenced by NetworkLobbyWindow::DrawWidget(), and NetworkLobbyWindow::OnPaint().

◆ DrawResizeBox()

static void DrawResizeBox ( const Rect r,
Colours  colour,
bool  at_left,
bool  clicked 
)
inlinestatic

Draw a resize box.

Parameters
rRectangle of the box.
colourColour of the resize box.
at_leftResize box is at left-side of the window,
clickedBox is lowered.

Definition at line 506 of file widget.cpp.

References DrawFrameRect(), and FR_LOWERED.

◆ DrawShadeBox()

static void DrawShadeBox ( const Rect r,
Colours  colour,
bool  clicked 
)
inlinestatic

Draw a shade box.

Parameters
rRectangle of the box.
colourColour of the shade box.
clickedBox is lowered.

Definition at line 461 of file widget.cpp.

References DrawImageButtons(), and WWT_SHADEBOX.

◆ DrawStickyBox()

static void DrawStickyBox ( const Rect r,
Colours  colour,
bool  clicked 
)
inlinestatic

Draw a sticky box.

Parameters
rRectangle of the box.
colourColour of the sticky box.
clickedBox is lowered.

Definition at line 472 of file widget.cpp.

References DrawImageButtons(), and WWT_STICKYBOX.

◆ DrawText()

static void DrawText ( const Rect r,
TextColour  colour,
StringID  str 
)
inlinestatic

Draw text.

Parameters
rRectangle of the background.
colourColour of the text.
strText to draw.

Definition at line 250 of file widget.cpp.

References DrawString(), GetStringBoundingBox(), and max().

◆ DrawVerticalScrollbar()

static void DrawVerticalScrollbar ( const Rect r,
Colours  colour,
bool  up_clicked,
bool  bar_dragged,
bool  down_clicked,
const Scrollbar scrollbar 
)
inlinestatic

Draw a vertical scrollbar.

Parameters
rRectangle of the scrollbar widget.
colourColour of the scrollbar widget.
up_clickedUp-arrow is clicked.
bar_draggedBar is dragged.
down_clickedDown-arrow is clicked.
scrollbarScrollbar size, offset, and capacity information.

Definition at line 338 of file widget.cpp.

◆ GetWidgetFromPos()

int 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
*wWindow to look inside
xThe Window client X coordinate
yThe Window client y coordinate
Returns
A widget index, or -1 if no widget was found.

Definition at line 162 of file widget.cpp.

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

◆ HandleScrollbarHittest()

static Point HandleScrollbarHittest ( const Scrollbar sb,
int  top,
int  bottom,
bool  horizontal 
)
static

Compute the vertical position of the draggable part of scrollbar.

Parameters
sbScrollbar list data
topTop position of the scrollbar (top position of the up-button)
bottomBottom position of the scrollbar (bottom position of the down-button)
horizontalWhether the scrollbar is horizontal or not
Returns
A Point, with x containing the top coordinate of the draggable part, and y containing the bottom coordinate of the draggable part

Definition at line 38 of file widget.cpp.

◆ MakeCompanyButtonRows()

NWidgetBase* MakeCompanyButtonRows ( int *  biggest_index,
int  widget_first,
int  widget_last,
int  max_length,
StringID  button_tooltip 
)

Make a number of rows with button-like graphics, for enabling/disabling each company.

Parameters
biggest_indexStorage for collecting the biggest index used in the returned tree.
widget_firstThe first widget index to use.
widget_lastThe last widget index to use.
max_lengthMaximal number of company buttons in one row.
button_tooltipThe tooltip-string of every button.
Returns
Panel with rows of company buttons.
Postcondition
*biggest_index contains the largest used index in the tree.

Definition at line 2864 of file widget.cpp.

Referenced by MakeCompanyButtonRowsAIDebug(), MakeCompanyButtonRowsGraphGUI(), MakeCompanyButtonRowsLinkGraphGUI(), and NWidgetFunction().

◆ MakeNWidget()

static int MakeNWidget ( const NWidgetPart parts,
int  count,
NWidgetBase **  dest,
bool *  fill_dest,
int *  biggest_index 
)
static

Construct a single nested widget in *dest from its parts.

Construct a NWidgetBase object from a NWidget function, and apply all settings that follow it, until encountering a EndContainer, another NWidget, or the end of the parts array.

Parameters
partsArray with parts of the nested widget.
countLength of the parts array.
destAddress of pointer to use for returning the composed widget.
fill_destFill the composed widget with child widgets.
biggest_indexPointer to biggest nested widget index in the tree encountered so far.
Returns
Number of widget part elements used to compose the widget.
Precondition
biggest_index != NULL.

Definition at line 2562 of file widget.cpp.

References NWidgetPartWidget::colour, NWidgetPart::cont_flags, NWidgetPart::func_ptr, NWidgetPartWidget::index, max(), NWID_HORIZONTAL, NWID_HORIZONTAL_LTR, NWID_MATRIX, NWID_SPACER, NWID_VERTICAL, NWidgetPart::type, NWidgetPart::widget, WPT_FUNCTION, WPT_RESIZE, WWT_FRAME, WWT_INSET, and WWT_PANEL.

◆ MakeWidgetTree()

static int MakeWidgetTree ( const NWidgetPart parts,
int  count,
NWidgetBase **  parent,
int *  biggest_index 
)
static

Build a nested widget tree by recursively filling containers with nested widgets read from their parts.

Parameters
partsArray with parts of the nested widgets.
countLength of the parts array.
parentPointer or container to use for storing the child widgets (*parent == NULL or *parent == container or background widget).
biggest_indexPointer to biggest nested widget index in the tree.
Returns
Number of widget part elements used to fill the container.
Postcondition
*biggest_index contains the largest widget index of the tree and -1 if no index is used.

Definition at line 2736 of file widget.cpp.

Referenced by MakeNWidgets(), and MakeWindowNWidgetTree().

◆ 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
wWindow on which a scroll was performed.
nwPointer to the scrollbar widget.
xThe X coordinate of the mouse click.
yThe Y coordinate of the mouse click.

Definition at line 138 of file widget.cpp.

◆ ScrollbarClickPositioning()

static void ScrollbarClickPositioning ( Window w,
NWidgetScrollbar sb,
int  x,
int  y,
int  mi,
int  ma 
)
static

Compute new position of the scrollbar after a click and updates the window flags.

Parameters
wWindow on which a scroll was performed.
sbScrollbar
miMinimum coordinate of the scroll bar.
maMaximum coordinate of the scroll bar.
xThe X coordinate of the mouse click.
yThe Y coordinate of the mouse click.

Definition at line 81 of file widget.cpp.