# 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 "table/sprites.h"`
`#include "table/strings.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.
static void | ScrollbarClickPositioning (Window *w, WidgetType wtp, int x, int y, int mi, int ma)
| Compute new position of the scrollbar after a click and updates the window flags.
void | ScrollbarClickHandler (Window *w, const NWidgetCore *nw, int x, int y)
| Special handling for the scrollbar widget type.
int | GetWidgetFromPos (const Window *w, int x, int y)
| Returns the index for the widget located at the given position relative to the window.
void | DrawFrameRect (int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
| Draw frame rectangle.
static void | DrawImageButtons (const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
| Draw an image button.
static void | DrawLabel (const Rect &r, WidgetType type, bool clicked, StringID str)
| Draw the label-part of a widget.
static void | DrawText (const Rect &r, TextColour colour, StringID str)
| Draw text.
static void | DrawInset (const Rect &r, Colours colour, StringID str)
| Draw an inset widget.
static void | DrawMatrix (const Rect &r, Colours colour, bool clicked, uint16 data)
| Draw a matrix widget.
static void | DrawVerticalScrollbar (const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
| Draw a vertical scrollbar.
static void | DrawHorizontalScrollbar (const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
| Draw a horizontal scrollbar.
static void | DrawFrame (const Rect &r, Colours colour, StringID str)
| Draw a frame widget.
static void | DrawShadeBox (const Rect &r, Colours colour, bool clicked)
| Draw a shade box.
static void | DrawStickyBox (const Rect &r, Colours colour, bool clicked)
| Draw a sticky box.
static void | DrawResizeBox (const Rect &r, Colours colour, bool at_left, bool clicked)
| Draw a resize box.
static void | DrawCloseBox (const Rect &r, Colours colour, StringID str)
| Draw a close box.
void | DrawCaption (const Rect &r, Colours colour, Owner owner, StringID str)
| Draw a caption bar.
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).
static void | DrawDropdown (const Rect &r, Colours colour, bool clicked, StringID str)
| Draw a dropdown WWT_DROPDOWN widget.
static uint | ComputeMaxSize (uint base, uint max_space, uint step)
| Return the biggest possible size of a nested widget.
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.
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.
NWidgetContainer * | MakeNWidgets (const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
| Construct a nested widget tree from an array of parts.
NWidgetContainer * | MakeWindowNWidgetTree (const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
| Make a nested widget tree for a window from a parts array.
## Variables
static const char * | UPARROW = "\xEE\x8A\xA0"
| String containing an upwards pointing arrow.
static const char * | DOWNARROW = "\xEE\x8A\xAA"
| String containing a downwards pointing arrow.

## Detailed Description

Handling of the default/simple widgets.

Definition in file widget.cpp.

## Function Documentation

static uint ComputeMaxSize | ( | uint | base,
|  | uint | max_space,
|  | uint | step |
| ) |  |  | `[inline, static]`

Return the biggest possible size of a nested widget.

Parameters:

  | base | Base size of the widget.
  | max_space | Available space for the widget.
  | step | Stepsize of the widget.

Returns:
  Biggest possible size of the widget, assuming that base may only be incremented by step size steps.

Definition at line 938 of file widget.cpp.

Referenced by NWidgetVertical::AssignSizePosition(), NWidgetHorizontal::AssignSizePosition(), and NWidgetStacked::AssignSizePosition().

static void DrawButtonDropdown | ( | const Rect & | r,
|  | Colours | colour,
|  | bool | clicked_button,
|  | bool | clicked_dropdown,
|  | StringID | str |
| ) |  |  | `[inline, static]`

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

Parameters:

  | r | Rectangle containing the widget.
  | colour | Background colour of the widget.
  | clicked_button | The button-part is lowered.
  | clicked_dropdown | The drop-down part is lowered.
  | str | Text of the button.

Note:
  Magic constants are also used in NWidgetLeaf::ButtonHit.

Definition at line 538 of file widget.cpp.

References _dynlang, DOWNARROW, DrawFrameRect(), DrawString(), FR_LOWERED, SA_CENTER, TD_LTR, DynamicLanguages::text_dir, WD_DROPDOWNTEXT_LEFT, WD_DROPDOWNTEXT_RIGHT, and WD_DROPDOWNTEXT_TOP.

Referenced by NWidgetLeaf::Draw(), and DrawDropdown().

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

Draw a caption bar.

Parameters:

  | r | Rectangle of the bar.
  | colour | Colour of the window.
  | owner | 'Owner' of the window.
  | str | Text to draw in the bar.

Definition at line 516 of file widget.cpp.

References _colour_gradient, _company_colours, DrawFrameRect(), DrawString(), FR_BORDERONLY, FR_DARKENED, FR_LOWERED, GfxFillRect(), INVALID_OWNER, SA_CENTER, WD_CAPTIONTEXT_LEFT, WD_CAPTIONTEXT_RIGHT, and WD_CAPTIONTEXT_TOP.

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

static void DrawCloseBox | ( | const Rect & | r,
|  | Colours | colour,
|  | StringID | str |
| ) |  |  | `[inline, static]`

Draw a close box.

Parameters:

  | r | Rectangle of the box.
  | colour | Colour of the close box.
  | str | Cross to draw (STR_BLACK_CROSS or STR_SILVER_CROSS).

Definition at line 502 of file widget.cpp.

References DrawFrameRect(), DrawString(), SA_CENTER, WD_CLOSEBOX_LEFT, WD_CLOSEBOX_RIGHT, and WD_CLOSEBOX_TOP.

Referenced by NWidgetLeaf::Draw().

static void DrawDropdown | ( | const Rect & | r,
|  | Colours | colour,
|  | bool | clicked,
|  | StringID | str |
| ) |  |  | `[inline, static]`

Draw a dropdown WWT_DROPDOWN widget.

Parameters:

  | r | Rectangle containing the widget.
  | colour | Background colour of the widget.
  | clicked | The widget is lowered.
  | str | Text of the button.

Definition at line 560 of file widget.cpp.

References DrawButtonDropdown().

Referenced by NWidgetLeaf::Draw().

static void DrawFrame | ( | const Rect & | r,
|  | Colours | colour,
|  | StringID | str |
| ) |  |  | `[inline, static]`

Draw a frame widget.

Parameters:

  | r | Rectangle of the frame.
  | colour | Colour of the frame.
  | str | Text of the frame.

Definition at line 411 of file widget.cpp.

References _colour_gradient, _dynlang, DrawString(), FONT_HEIGHT_NORMAL, GfxFillRect(), TD_LTR, DynamicLanguages::text_dir, WD_FRAMETEXT_LEFT, and WD_FRAMETEXT_RIGHT.

Referenced by NWidgetBackground::Draw().

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:
  _colour_gradient

Parameters:

  | flags | Flags controlling how to draw the frame.

See also:
  FrameFlags

Definition at line 200 of file widget.cpp.

References _colour_gradient, FILLRECT_RECOLOUR, FR_BORDERONLY, FR_DARKENED, FR_LOWERED, FR_TRANSPARENT, and GfxFillRect().

Referenced by NWidgetLeaf::Draw(), NWidgetBackground::Draw(), DrawAircraftImage(), DrawArrowButtons(), DrawButtonDropdown(), DrawCaption(), DrawCloseBox(), DrawHorizontalScrollbar(), DrawImageButtons(), DrawInset(), DrawMatrix(), DrawResizeBox(), DrawRoadVehImage(), SettingEntry::DrawSetting(), DrawShadeBox(), DrawShipImage(), DrawStickyBox(), DrawTrainImage(), DrawVerticalScrollbar(), TransparenciesWindow::DrawWidget(), NetworkJoinStatusWindow::DrawWidget(), NetworkContentDownloadStatusWindow::DrawWidget(), MusicWindow::DrawWidget(), GenerateProgressWindow::DrawWidget(), CheatWindow::DrawWidget(), AISettingsWindow::DrawWidget(), and Window::DrawWidgets().

static void DrawHorizontalScrollbar | ( | const Rect & | r,
|  | Colours | colour,
|  | bool | left_clicked,
|  | bool | bar_dragged,
|  | bool | right_clicked,
|  | const Scrollbar * | scrollbar |
| ) |  |  | `[inline, static]`

Draw a horizontal scrollbar.

Parameters:

  | r | Rectangle of the scrollbar widget.
  | colour | Colour of the scrollbar widget.
  | left_clicked | Left-arrow is clicked.
  | bar_dragged | Bar is dragged.
  | right_clicked | Right-arrow is clicked.
  | scrollbar | Scrollbar size, offset, and capacity information.

Definition at line 379 of file widget.cpp.

References _colour_gradient, DrawFrameRect(), DrawSprite(), FILLRECT_CHECKER, FR_LOWERED, GfxFillRect(), and HandleScrollbarHittest().

Referenced by NWidgetLeaf::Draw().

static void DrawImageButtons | ( | const Rect & | r,
|  | WidgetType | type,
|  | Colours | colour,
|  | bool | clicked,
|  | SpriteID | img |
| ) |  |  | `[inline, static]`

Draw an image button.

Parameters:

  | r | Rectangle of the button.
  | type | Widget type (WWT_IMGBTN or WWT_IMGBTN_2).
  | colour | Colour of the button.
  | clicked | Button is lowered.
  | img | Sprite to draw.

Definition at line 239 of file widget.cpp.

References DrawFrameRect(), DrawSprite(), FR_LOWERED, WD_IMGBTN_LEFT, WD_IMGBTN_TOP, and WWT_IMGBTN_2.

Referenced by NWidgetLeaf::Draw().

static void DrawInset | ( | const Rect & | r,
|  | Colours | colour,
|  | StringID | str |
| ) |  |  | `[inline, static]`

Draw an inset widget.

Parameters:

  | r | Rectangle of the background.
  | colour | Colour of the inset.
  | str | Text to draw.

Definition at line 283 of file widget.cpp.

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

Referenced by NWidgetBackground::Draw().

static void DrawLabel | ( | const Rect & | r,
|  | WidgetType | type,
|  | bool | clicked,
|  | StringID | str |
| ) |  |  | `[inline, static]`

Draw the label-part of a widget.

Parameters:

  | r | Rectangle of the label background.
  | type | Widget type (WWT_TEXTBTN, WWT_TEXTBTN_2, or WWT_LABEL).
  | clicked | Label is rendered lowered.
  | str | Text to draw.

Definition at line 255 of file widget.cpp.

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

Referenced by NWidgetLeaf::Draw().

static void DrawMatrix | ( | const Rect & | r,
|  | Colours | colour,
|  | bool | clicked,
|  | uint16 | data |
| ) |  |  | `[inline, static]`

Draw a matrix widget.

Parameters:

  | r | Rectangle of the matrix background.
  | colour | Colour of the background.
  | clicked | Matrix is rendered lowered.
  | data | Data of the widget, number of rows and columns of the widget.

Definition at line 296 of file widget.cpp.

References _colour_gradient, DrawFrameRect(), FR_LOWERED, GB(), GfxFillRect(), MAT_COL_BITS, MAT_COL_START, MAT_ROW_BITS, and MAT_ROW_START.

Referenced by NWidgetLeaf::Draw().

static void DrawResizeBox | ( | const Rect & | r,
|  | Colours | colour,
|  | bool | at_left,
|  | bool | clicked |
| ) |  |  | `[inline, static]`

Draw a resize box.

Parameters:

  | r | Rectangle of the box.
  | colour | Colour of the resize box.
  | at_left | Resize box is at left-side of the window,
  | clicked | Box is lowered.

Definition at line 486 of file widget.cpp.

References DrawFrameRect(), DrawSprite(), FR_LOWERED, WD_RESIZEBOX_LEFT, WD_RESIZEBOX_RIGHT, and WD_RESIZEBOX_TOP.

Referenced by NWidgetLeaf::Draw().

static void DrawShadeBox | ( | const Rect & | r,
|  | Colours | colour,
|  | bool | clicked |
| ) |  |  | `[inline, static]`

Draw a shade box.

Parameters:

  | r | Rectangle of the box.
  | colour | Colour of the shade box.
  | clicked | Box is lowered.

Definition at line 461 of file widget.cpp.

References DrawFrameRect(), DrawSprite(), FR_LOWERED, WD_SHADEBOX_LEFT, and WD_SHADEBOX_TOP.

Referenced by NWidgetLeaf::Draw().

static void DrawStickyBox | ( | const Rect & | r,
|  | Colours | colour,
|  | bool | clicked |
| ) |  |  | `[inline, static]`

Draw a sticky box.

Parameters:

  | r | Rectangle of the box.
  | colour | Colour of the sticky box.
  | clicked | Box is lowered.

Definition at line 473 of file widget.cpp.

References DrawFrameRect(), DrawSprite(), FR_LOWERED, WD_STICKYBOX_LEFT, and WD_STICKYBOX_TOP.

Referenced by NWidgetLeaf::Draw().

static void DrawText | ( | const Rect & | r,
|  | TextColour | colour,
|  | StringID | str |
| ) |  |  | `[inline, static]`

Draw text.

Parameters:

  | r | Rectangle of the background.
  | colour | Colour of the text.
  | str | Text to draw.

Definition at line 270 of file widget.cpp.

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

Referenced by NWidgetLeaf::Draw().

static void DrawVerticalScrollbar | ( | const Rect & | r,
|  | Colours | colour,
|  | bool | up_clicked,
|  | bool | bar_dragged,
|  | bool | down_clicked,
|  | const Scrollbar * | scrollbar |
| ) |  |  | `[inline, static]`

Draw a vertical scrollbar.

Parameters:

  | r | Rectangle of the scrollbar widget.
  | colour | Colour of the scrollbar widget.
  | up_clicked | Up-arrow is clicked.
  | bar_dragged | Bar is dragged.
  | down_clicked | Down-arrow is clicked.
  | scrollbar | Scrollbar size, offset, and capacity information.

Definition at line 344 of file widget.cpp.

References _colour_gradient, DOWNARROW, DrawFrameRect(), DrawString(), FILLRECT_CHECKER, FR_LOWERED, GfxFillRect(), HandleScrollbarHittest(), SA_CENTER, and UPARROW.

Referenced by NWidgetLeaf::Draw().

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:

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

Returns:
  A widget index, or -1 if no widget was found.

Definition at line 185 of file widget.cpp.

References NWidgetBase::GetWidgetFromPos(), NWidgetCore::index, and Window::nested_root.

Referenced by DropdownWindow::GetDropDownItem(), and DropdownWindow::OnMouseLoop().

static Point HandleScrollbarHittest | ( | const Scrollbar * | sb,
|  | int | top,
|  | int | bottom,
|  | bool | horizontal |
| ) |  |  | `[static]`

Compute the vertical position of the draggable part of scrollbar.

Parameters:

  | sb | Scrollbar list data
  | top | Top position of the scrollbar (top position of the up-button)
  | bottom | Bottom position of the scrollbar (bottom position of the down-button)
  | horizontal | Whether 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 36 of file widget.cpp.

References _dynlang, Scrollbar::GetCapacity(), Scrollbar::GetCount(), Scrollbar::GetPosition(), TD_RTL, and DynamicLanguages::text_dir.

Referenced by DrawHorizontalScrollbar(), DrawVerticalScrollbar(), and ScrollbarClickPositioning().

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:

  | parts | Array with parts of the nested widget.
  | count | Length of the parts array.
  | dest | Address of pointer to use for returning the composed widget.
  | fill_dest | Fill the composed widget with child widgets.
  | biggest_index | Pointer 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 2136 of file widget.cpp.

References NWidgetPartWidget::colour, NWidgetPart::cont_flags, NWidgetPartDataTip::data, NWidgetPart::data_tip, FS_BEGIN, NWidgetPart::func_ptr, NWidgetPartWidget::index, NWidgetPartPaddings::left, NWidgetPartTextLines::lines, max(), NWID_BUTTON_ARROW, NWID_BUTTON_DROPDOWN, NWID_HORIZONTAL, NWID_HORIZONTAL_LTR, NWID_SELECTION, NWID_SPACER, NWID_VERTICAL, NWID_VIEWPORT, NWidgetPart::padding, NWidgetPart::pip, NWidgetPartPIP::post, NWidgetResizeBase::SetFill(), NWidgetResizeBase::SetMinimalSize(), NWidgetResizeBase::SetMinimalTextLines(), NWidgetBackground::SetPIP(), NWidgetPIPContainer::SetPIP(), NWidgetResizeBase::SetResize(), NWidgetPartTextLines::size, NWidgetPartTextLines::spacing, NWidgetPart::text_lines, NWidgetCore::tool_tip, NWidgetPartDataTip::tooltip, NWidgetPart::type, NWidgetPart::widget, NWidgetCore::widget_data, WPT_DATATIP, WPT_ENDCONTAINER, WPT_FILL, WPT_FUNCTION, WPT_MINSIZE, WPT_MINTEXTLINES, WPT_PADDING, WPT_PIPSPACE, WPT_RESIZE, WWT_FRAME, WWT_INSET, WWT_LAST, WWT_PANEL, and NWidgetPart::xy.

Referenced by 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:

  | parts | Array with parts of the nested widgets.
  | count | Length of the parts array.
  | parent | Pointer or container to use for storing the child widgets (*parent == NULL or *parent == container or background widget).
  | biggest_index | Pointer 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 2284 of file widget.cpp.

References NWidgetBackground::Add(), NWidgetContainer::Add(), MakeNWidget(), NWID_HORIZONTAL, NWID_HORIZONTAL_LTR, NWID_SELECTION, NWID_VERTICAL, NWidgetPart::type, NWidgetBase::type, WPT_ENDCONTAINER, WWT_FRAME, WWT_INSET, and WWT_PANEL.

Referenced by MakeNWidgets(), and MakeWindowNWidgetTree().

void ScrollbarClickHandler | ( | Window * | w,
|  | const 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 150 of file widget.cpp.

References NWidgetBase::current_x, NWidgetBase::current_y, NWidgetBase::pos_x, NWidgetBase::pos_y, ScrollbarClickPositioning(), NWidgetBase::type, WWT_HSCROLLBAR, WWT_SCROLL2BAR, and WWT_SCROLLBAR.

Referenced by DispatchLeftClickEvent().

static void ScrollbarClickPositioning | ( | Window * | w,
|  | WidgetType | wtp,
|  | int | x,
|  | int | y,
|  | int | mi,
|  | int | ma |
| ) |  |  | `[static]`

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

Parameters:

  | w | Window on which a scroll was performed.
  | wtp | Scrollbar widget type.
  | mi | Minimum coordinate of the scroll bar.
  | ma | Maximum coordinate of the scroll bar.
  | x | The X coordinate of the mouse click.
  | y | The Y coordinate of the mouse click.

Definition at line 73 of file widget.cpp.

References _dynlang, _left_button_clicked, Window::flags4, Scrollbar::GetCapacity(), HandleScrollbarHittest(), Window::hscroll, Window::SetDirty(), TD_RTL, DynamicLanguages::text_dir, Scrollbar::UpdatePosition(), Window::vscroll, Window::vscroll2, WF_SCROLL_DOWN, WF_SCROLL_MIDDLE, WF_SCROLL_UP, WWT_HSCROLLBAR, WWT_SCROLL2BAR, and WWT_SCROLLBAR.

Referenced by ScrollbarClickHandler().