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.
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.
void ScrollbarClickHandler (Window *w, 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, uint resize_x, uint resize_y)
 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 DrawDefSizeBox (const Rect &r, Colours colour, bool clicked)
 Draw a defsize box.
static void DrawDebugBox (const Rect &r, Colours colour, bool clicked)
 Draw a NewGRF debug 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)
 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 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.
NWidgetContainerMakeNWidgets (const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
 Construct a nested widget tree from an array of parts.
NWidgetContainerMakeWindowNWidgetTree (const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
 Make a nested widget tree for a window from a parts array.
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.

Detailed Description

Handling of the default/simple widgets.

Definition in file widget.cpp.

Function Documentation

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 567 of file widget.cpp.

References _current_text_dir, DrawFrameRect(), DrawSprite(), DrawString(), NWidgetLeaf::dropdown_dimension, FONT_HEIGHT_NORMAL, FR_LOWERED, max(), TD_LTR, WD_DROPDOWNTEXT_LEFT, and WD_DROPDOWNTEXT_RIGHT.

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

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 539 of file widget.cpp.

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

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

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 526 of file widget.cpp.

References DrawFrameRect(), DrawSprite(), PALETTE_TEXT_RECOLOUR, WD_CLOSEBOX_LEFT, and WD_CLOSEBOX_TOP.

Referenced by NWidgetLeaf::Draw().

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 496 of file widget.cpp.

References DrawFrameRect(), DrawSprite(), FR_LOWERED, WD_DEBUGBOX_LEFT, and WD_DEBUGBOX_TOP.

Referenced by NWidgetLeaf::Draw().

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 484 of file widget.cpp.

References DrawFrameRect(), DrawSprite(), FR_LOWERED, WD_DEFSIZEBOX_LEFT, and WD_DEFSIZEBOX_TOP.

Referenced by NWidgetLeaf::Draw().

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 595 of file widget.cpp.

References DrawButtonDropdown().

Referenced by NWidgetLeaf::Draw().

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 410 of file widget.cpp.

References _colour_gradient, _current_text_dir, DrawString(), FONT_HEIGHT_NORMAL, GfxFillRect(), TD_LTR, 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 
)
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 375 of file widget.cpp.

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

Referenced by NWidgetScrollbar::Draw().

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.

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 
)
inlinestatic

Draw an inset widget.

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

Definition at line 262 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 
)
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 234 of file widget.cpp.

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

Referenced by NWidgetLeaf::Draw().

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 277 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 
)
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 509 of file widget.cpp.

References DrawFrameRect(), DrawSprite(), FR_LOWERED, GetSpriteSize(), WD_RESIZEBOX_BOTTOM, WD_RESIZEBOX_LEFT, and WD_RESIZEBOX_RIGHT.

Referenced by NWidgetLeaf::Draw().

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 460 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 
)
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 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 
)
inlinestatic

Draw text.

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

Definition at line 249 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 
)
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 337 of file widget.cpp.

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

Referenced by NWidgetScrollbar::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
*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.

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

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

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.

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

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

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 2854 of file widget.cpp.

References NWidgetContainer::Add(), GetSpriteSize(), NWidgetCore::SetDataTip(), NWidgetResizeBase::SetFill(), NWidgetResizeBase::SetMinimalSize(), NWidgetResizeBase::SetResize(), WD_MATRIX_BOTTOM, WD_MATRIX_LEFT, WD_MATRIX_RIGHT, WD_MATRIX_TOP, and WWT_PANEL.

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

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 2552 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_DROPDOWN, NWID_HORIZONTAL, NWID_HORIZONTAL_LTR, NWID_HSCROLLBAR, NWID_MATRIX, NWID_SELECTION, NWID_SPACER, NWID_VERTICAL, NWID_VIEWPORT, NWID_VSCROLLBAR, NWidgetPart::padding, NWidgetPart::pip, NWidgetPartPIP::post, NWidgetCore::scrollbar_index, NWidgetResizeBase::SetFill(), NWidgetResizeBase::SetMinimalSize(), NWidgetResizeBase::SetMinimalTextLines(), NWidgetPIPContainer::SetPIP(), NWidgetBackground::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, WPT_SCROLLBAR, 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
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 2726 of file widget.cpp.

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

Referenced by MakeNWidgets(), and MakeWindowNWidgetTree().

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.

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

Referenced by DispatchLeftClickEvent().

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.

References _current_text_dir, NWidgetCore::disp_flags, HandleScrollbarHittest(), NWidgetCore::index, NDB_SCROLLBAR_DOWN, NDB_SCROLLBAR_UP, NWID_HSCROLLBAR, Window::scrolling_scrollbar, SetBit(), Window::SetDirty(), Scrollbar::SS_BIG, TD_RTL, NWidgetBase::type, and Scrollbar::UpdatePosition().

Referenced by ScrollbarClickHandler().