Functions | Variables

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 "table/sprites.h"
#include "table/strings.h"
#include "table/palettes.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)
 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 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, 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 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.

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

References _current_text_dir, DOWNARROW, DrawFrameRect(), DrawString(), FONT_HEIGHT_NORMAL, FR_LOWERED, max(), SA_HOR_CENTER, 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:
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 509 of file widget.cpp.

References _colour_gradient, _company_colours, DrawFrameRect(), DrawString(), FR_BORDERONLY, FR_DARKENED, FR_LOWERED, GetStringBoundingBox(), GfxFillRect(), INVALID_OWNER, max(), 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,
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 495 of file widget.cpp.

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

Referenced by NWidgetLeaf::Draw().

static void DrawDebugBox ( const Rect r,
Colours  colour,
bool  clicked 
) [inline, static]

Draw a NewGRF debug box.

Parameters:
r Rectangle of the box.
colour Colour of the debug box.
clicked Box is lowered.

Definition at line 466 of file widget.cpp.

References DrawFrameRect(), DrawSprite(), FR_LOWERED, WD_DEBUGBOX_LEFT, and WD_DEBUGBOX_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 559 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 392 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 
) [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 357 of file widget.cpp.

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

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 214 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 258 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 230 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 
) [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 271 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 479 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 442 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 454 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 245 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 319 of file widget.cpp.

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

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

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

Referenced by DropdownWindow::GetDropDownItem(), HandleMouseDragDrop(), 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 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_index Storage for collecting the biggest index used in the returned tree.
widget_first The first widget index to use.
widget_last The last widget index to use.
max_length Maximal number of company buttons in one row.
button_tooltip The 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 2734 of file widget.cpp.

References NWidgetContainer::Add(), GetSpriteSize(), NWidgetCore::SetDataTip(), NWidgetResizeBase::SetFill(), NWidgetResizeBase::SetMinimalSize(), NWidgetResizeBase::SetResize(), WD_MATRIX_BOTTOM, WD_MATRIX_LEFT, 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:
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 2432 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(), 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, 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:
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 2606 of file widget.cpp.

References NWidgetBackground::Add(), NWidgetContainer::Add(), MakeNWidget(), NWID_HORIZONTAL, NWID_HORIZONTAL_LTR, NWID_MATRIX, 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,
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 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:
w Window on which a scroll was performed.
sb Scrollbar
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 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, NWidgetBase::type, and Scrollbar::UpdatePosition().

Referenced by ScrollbarClickHandler().