2009-08-21 20:21:05 +00:00
/*
* This file is part of OpenTTD .
* OpenTTD is free software ; you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation , version 2.
* OpenTTD is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
* See the GNU General Public License for more details . You should have received a copy of the GNU General Public License along with OpenTTD . If not , see < http : //www.gnu.org/licenses/>.
*/
2009-02-09 01:06:23 +00:00
/** @file widget_type.h Definitions about widgets. */
# ifndef WIDGET_TYPE_H
# define WIDGET_TYPE_H
2010-01-15 16:41:15 +00:00
# include "core/alloc_type.hpp"
2009-02-09 01:06:23 +00:00
# include "core/bitmath_func.hpp"
2010-08-26 22:01:16 +00:00
# include "core/math_func.hpp"
2009-02-09 01:06:23 +00:00
# include "strings_type.h"
# include "gfx_type.h"
2010-01-15 16:41:15 +00:00
# include "window_type.h"
2009-02-09 01:06:23 +00:00
2010-05-13 09:44:44 +00:00
static const int WIDGET_LIST_END = - 1 ; ///< indicate the end of widgets' list for vararg functions
2009-02-09 01:06:23 +00:00
2009-07-16 16:22:23 +00:00
/** Bits of the #WWT_MATRIX widget data. */
enum MatrixWidgetValues {
/* Number of column bits of the WWT_MATRIX widget data. */
MAT_COL_START = 0 , ///< Lowest bit of the number of columns.
MAT_COL_BITS = 8 , ///< Number of bits for the number of columns in the matrix.
/* Number of row bits of the WWT_MATRIX widget data. */
MAT_ROW_START = 8 , ///< Lowest bit of the number of rows.
MAT_ROW_BITS = 8 , ///< Number of bits for the number of rows in the matrix.
} ;
2009-11-19 17:56:03 +00:00
/** Values for an arrow widget */
enum ArrowWidgetValues {
AWV_DECREASE , ///< Arrow to the left or in case of RTL to the right
AWV_INCREASE , ///< Arrow to the right or in case of RTL to the left
AWV_LEFT , ///< Force the arrow to the left
AWV_RIGHT , ///< Force the arrow to the right
} ;
2009-02-09 01:06:23 +00:00
/**
2009-03-22 21:15:45 +00:00
* Window widget types , nested widget types , and nested widget part types .
2009-02-09 01:06:23 +00:00
*/
enum WidgetType {
2009-03-22 21:15:45 +00:00
/* Window widget types. */
2009-02-09 01:06:23 +00:00
WWT_EMPTY , ///< Empty widget, place holder to reserve space in widget array
WWT_PANEL , ///< Simple depressed panel
WWT_INSET , ///< Pressed (inset) panel, most commonly used as combo box _text_ area
2012-11-11 16:01:34 +00:00
WWT_IMGBTN , ///< (Toggle) Button with image
WWT_IMGBTN_2 , ///< (Toggle) Button with diff image when clicked
WWT_ARROWBTN , ///< (Toggle) Button with an arrow
WWT_TEXTBTN , ///< (Toggle) Button with text
WWT_TEXTBTN_2 , ///< (Toggle) Button with diff text when clicked
2009-02-09 01:06:23 +00:00
WWT_LABEL , ///< Centered label
WWT_TEXT , ///< Pure simple text
2009-07-16 16:22:23 +00:00
WWT_MATRIX , ///< Grid of rows and columns. @see MatrixWidgetValues
2009-02-09 01:06:23 +00:00
WWT_FRAME , ///< Frame
WWT_CAPTION , ///< Window caption (window title between closebox and stickybox)
2013-05-26 19:30:07 +00:00
WWT_DEBUGBOX , ///< NewGRF debug box (at top-right of a window, between WWT_CAPTION and WWT_SHADEBOX)
WWT_SHADEBOX , ///< Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
WWT_DEFSIZEBOX , ///< Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
WWT_STICKYBOX , ///< Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
2009-02-09 01:06:23 +00:00
WWT_RESIZEBOX , ///< Resize box (normally at bottom-right of a window)
WWT_CLOSEBOX , ///< Close box (at top-left of a window)
2009-03-23 00:08:59 +00:00
WWT_DROPDOWN , ///< Drop down list
2009-02-09 01:06:23 +00:00
WWT_EDITBOX , ///< a textbox for typing
WWT_LAST , ///< Last Item. use WIDGETS_END to fill up padding!!
2009-03-22 21:15:45 +00:00
/* Nested widget types. */
2009-11-19 18:01:24 +00:00
NWID_HORIZONTAL , ///< Horizontal container.
NWID_HORIZONTAL_LTR , ///< Horizontal container that doesn't change the order of the widgets for RTL languages.
NWID_VERTICAL , ///< Vertical container.
2010-12-10 17:47:11 +00:00
NWID_MATRIX , ///< Matrix container.
2009-11-19 18:01:24 +00:00
NWID_SPACER , ///< Invisible widget that takes some space.
NWID_SELECTION , ///< Stacked widgets, only one visible at a time (eg in a panel with tabs).
NWID_VIEWPORT , ///< Nested widget containing a viewport.
NWID_BUTTON_DROPDOWN , ///< Button with a drop-down.
2010-08-12 09:16:43 +00:00
NWID_HSCROLLBAR , ///< Horizontal scrollbar
NWID_VSCROLLBAR , ///< Vertical scrollbar
2009-03-22 21:15:45 +00:00
/* Nested widget part types. */
WPT_RESIZE , ///< Widget part for specifying resizing.
WPT_MINSIZE , ///< Widget part for specifying minimal size.
2009-11-17 13:04:05 +00:00
WPT_MINTEXTLINES , ///< Widget part for specifying minimal number of lines of text.
2009-03-22 21:15:45 +00:00
WPT_FILL , ///< Widget part for specifying fill.
WPT_DATATIP , ///< Widget part for specifying data and tooltip.
2009-03-28 11:13:39 +00:00
WPT_PADDING , ///< Widget part for specifying a padding.
WPT_PIPSPACE , ///< Widget part for specifying pre/inter/post space for containers.
2021-04-19 15:37:10 +00:00
WPT_TEXTCOLOUR , ///< Widget part for specifying text colour.
2021-04-19 11:12:07 +00:00
WPT_ALIGNMENT , ///< Widget part for specifying text/image alignment.
2009-03-22 21:15:45 +00:00
WPT_ENDCONTAINER , ///< Widget part to denote end of a container.
2009-04-25 11:59:36 +00:00
WPT_FUNCTION , ///< Widget part for calling a user function.
2010-08-12 06:44:45 +00:00
WPT_SCROLLBAR , ///< Widget part for attaching a scrollbar.
2009-03-22 21:15:45 +00:00
/* Pushable window widget types. */
2009-03-22 18:46:56 +00:00
WWT_MASK = 0x7F ,
2009-02-09 01:06:23 +00:00
2010-08-12 16:44:51 +00:00
WWB_PUSHBUTTON = 1 < < 7 ,
2009-02-09 01:06:23 +00:00
2012-11-11 16:01:34 +00:00
WWT_PUSHBTN = WWT_PANEL | WWB_PUSHBUTTON , ///< Normal push-button (no toggle button) with custom drawing
WWT_PUSHTXTBTN = WWT_TEXTBTN | WWB_PUSHBUTTON , ///< Normal push-button (no toggle button) with text caption
WWT_PUSHIMGBTN = WWT_IMGBTN | WWB_PUSHBUTTON , ///< Normal push-button (no toggle button) with image caption
WWT_PUSHARROWBTN = WWT_ARROWBTN | WWB_PUSHBUTTON , ///< Normal push-button (no toggle button) with arrow caption
2012-04-17 19:44:02 +00:00
NWID_PUSHBUTTON_DROPDOWN = NWID_BUTTON_DROPDOWN | WWB_PUSHBUTTON ,
2009-02-09 01:06:23 +00:00
} ;
2009-06-01 20:45:46 +00:00
/** Different forms of sizing nested widgets, using NWidgetBase::AssignSizePosition() */
enum SizingType {
ST_SMALLEST , ///< Initialize nested widget tree to smallest size. Also updates \e current_x and \e current_y.
ST_RESIZE , ///< Resize the nested widget tree.
} ;
2009-06-03 20:00:33 +00:00
/* Forward declarations. */
class NWidgetCore ;
2009-09-02 12:02:55 +00:00
class Scrollbar ;
2009-06-03 20:00:33 +00:00
2009-03-22 21:15:45 +00:00
/**
* Baseclass for nested widgets .
2009-06-01 20:45:46 +00:00
* @ invariant After initialization , \ f $ current \ _x = smallest \ _x + n * resize \ _x , for n \ geq 0 \ f $ .
* @ invariant After initialization , \ f $ current \ _y = smallest \ _y + m * resize \ _y , for m \ geq 0 \ f $ .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgets
2009-03-22 21:15:45 +00:00
*/
class NWidgetBase : public ZeroedMemoryAllocator {
public :
NWidgetBase ( WidgetType tp ) ;
2021-04-05 17:43:12 +00:00
virtual void AdjustPaddingForZoom ( ) ;
2009-07-15 22:51:37 +00:00
virtual void SetupSmallestSize ( Window * w , bool init_array ) = 0 ;
2009-11-20 20:10:06 +00:00
virtual void AssignSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height , bool rtl ) = 0 ;
2009-03-22 21:15:45 +00:00
2009-09-19 11:31:12 +00:00
virtual void FillNestedArray ( NWidgetBase * * array , uint length ) = 0 ;
2009-03-22 21:15:45 +00:00
2009-06-04 12:46:37 +00:00
virtual NWidgetCore * GetWidgetFromPos ( int x , int y ) = 0 ;
2009-07-26 13:19:23 +00:00
virtual NWidgetBase * GetWidgetOfType ( WidgetType tp ) ;
2009-06-04 12:46:37 +00:00
2011-12-19 21:05:14 +00:00
virtual bool IsHighlighted ( ) const { return false ; }
virtual TextColour GetHighlightColour ( ) const { return TC_INVALID ; }
virtual void SetHighlighted ( TextColour highlight_colour ) { }
2009-05-02 16:33:28 +00:00
/**
* Set additional space ( padding ) around the widget .
* @ param top Amount of additional space above the widget .
* @ param right Amount of additional space right of the widget .
* @ param bottom Amount of additional space below the widget .
* @ param left Amount of additional space left of the widget .
*/
2011-12-20 17:57:56 +00:00
inline void SetPadding ( uint8 top , uint8 right , uint8 bottom , uint8 left )
2009-05-02 16:33:28 +00:00
{
2021-04-05 17:43:12 +00:00
this - > uz_padding_top = top ;
this - > uz_padding_right = right ;
this - > uz_padding_bottom = bottom ;
this - > uz_padding_left = left ;
this - > AdjustPaddingForZoom ( ) ;
2011-01-15 15:36:58 +00:00
}
2009-05-02 16:33:28 +00:00
2011-12-20 17:57:56 +00:00
inline uint GetHorizontalStepSize ( SizingType sizing ) const ;
inline uint GetVerticalStepSize ( SizingType sizing ) const ;
2009-05-21 18:12:28 +00:00
2009-06-03 21:13:13 +00:00
virtual void Draw ( const Window * w ) = 0 ;
2009-09-13 19:15:59 +00:00
virtual void SetDirty ( const Window * w ) const ;
2009-06-03 21:13:13 +00:00
2021-04-04 08:34:37 +00:00
Rect GetCurrentRect ( ) const
{
Rect r ;
r . left = this - > pos_x ;
r . top = this - > pos_y ;
2021-05-03 23:24:14 +00:00
r . right = this - > pos_x + this - > current_x - 1 ;
r . bottom = this - > pos_y + this - > current_y - 1 ;
2021-04-04 08:34:37 +00:00
return r ;
}
2009-03-28 11:13:39 +00:00
WidgetType type ; ///< Type of the widget / nested widget.
2009-11-22 18:26:01 +00:00
uint fill_x ; ///< Horizontal fill stepsize (from initial size, \c 0 means not resizable).
uint fill_y ; ///< Vertical fill stepsize (from initial size, \c 0 means not resizable).
2009-03-28 11:13:39 +00:00
uint resize_x ; ///< Horizontal resize step (\c 0 means not resizable).
uint resize_y ; ///< Vertical resize step (\c 0 means not resizable).
2009-05-21 15:48:27 +00:00
/* Size of the widget in the smallest window possible.
2009-05-21 16:14:04 +00:00
* Computed by # SetupSmallestSize ( ) followed by # AssignSizePosition ( ) .
2009-05-21 15:48:27 +00:00
*/
uint smallest_x ; ///< Smallest horizontal size of the widget in a filled window.
uint smallest_y ; ///< Smallest vertical size of the widget in a filled window.
2009-06-01 20:45:46 +00:00
/* Current widget size (that is, after resizing). */
uint current_x ; ///< Current horizontal size (after resizing).
uint current_y ; ///< Current vertical size (after resizing).
2009-03-22 21:15:45 +00:00
2019-09-17 00:52:36 +00:00
int pos_x ; ///< Horizontal position of top-left corner of the widget in the window.
int pos_y ; ///< Vertical position of top-left corner of the widget in the window.
2009-03-22 21:15:45 +00:00
2009-03-28 11:13:39 +00:00
NWidgetBase * next ; ///< Pointer to next widget in container. Managed by parent container widget.
NWidgetBase * prev ; ///< Pointer to previous widget in container. Managed by parent container widget.
uint8 padding_top ; ///< Paddings added to the top of the widget. Managed by parent container widget.
2013-08-18 15:32:45 +00:00
uint8 padding_right ; ///< Paddings added to the right of the widget. Managed by parent container widget. (parent container may swap this with padding_left for RTL)
2009-03-28 11:13:39 +00:00
uint8 padding_bottom ; ///< Paddings added to the bottom of the widget. Managed by parent container widget.
2013-08-18 15:32:45 +00:00
uint8 padding_left ; ///< Paddings added to the left of the widget. Managed by parent container widget. (parent container may swap this with padding_right for RTL)
2009-06-01 20:45:46 +00:00
2021-04-05 17:43:12 +00:00
uint8 uz_padding_top ; ///< Unscaled top padding, for resize calculation.
uint8 uz_padding_right ; ///< Unscaled right padding, for resize calculation.
uint8 uz_padding_bottom ; ///< Unscaled bottom padding, for resize calculation.
uint8 uz_padding_left ; ///< Unscaled left padding, for resize calculation.
2009-06-01 20:45:46 +00:00
protected :
2011-12-20 17:57:56 +00:00
inline void StoreSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height ) ;
2009-03-22 21:15:45 +00:00
} ;
2009-06-01 20:45:46 +00:00
/**
* Get the horizontal sizing step .
* @ param sizing Type of resize being performed .
*/
2011-12-20 17:57:56 +00:00
inline uint NWidgetBase : : GetHorizontalStepSize ( SizingType sizing ) const
2009-05-21 18:12:28 +00:00
{
2009-11-22 18:26:01 +00:00
return ( sizing = = ST_RESIZE ) ? this - > resize_x : this - > fill_x ;
2009-05-21 18:12:28 +00:00
}
2009-06-01 20:45:46 +00:00
/**
* Get the vertical sizing step .
* @ param sizing Type of resize being performed .
*/
2011-12-20 17:57:56 +00:00
inline uint NWidgetBase : : GetVerticalStepSize ( SizingType sizing ) const
2009-05-21 18:12:28 +00:00
{
2009-11-22 18:26:01 +00:00
return ( sizing = = ST_RESIZE ) ? this - > resize_y : this - > fill_y ;
2009-05-21 18:12:28 +00:00
}
2010-05-16 19:17:02 +00:00
/**
* Store size and position .
* @ param sizing Type of resizing to perform .
* @ param x Horizontal offset of the widget relative to the left edge of the window .
* @ param y Vertical offset of the widget relative to the top edge of the window .
* @ param given_width Width allocated to the widget .
* @ param given_height Height allocated to the widget .
*/
2011-12-20 17:57:56 +00:00
inline void NWidgetBase : : StoreSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height )
2010-05-16 19:17:02 +00:00
{
this - > pos_x = x ;
this - > pos_y = y ;
if ( sizing = = ST_SMALLEST ) {
this - > smallest_x = given_width ;
this - > smallest_y = given_height ;
}
this - > current_x = given_width ;
this - > current_y = given_height ;
}
2010-08-01 19:22:34 +00:00
/**
* Base class for a resizable nested widget .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-03-22 21:15:45 +00:00
class NWidgetResizeBase : public NWidgetBase {
public :
2009-11-22 18:26:01 +00:00
NWidgetResizeBase ( WidgetType tp , uint fill_x , uint fill_y ) ;
2009-03-22 21:15:45 +00:00
2021-04-05 17:43:12 +00:00
void AdjustPaddingForZoom ( ) override ;
2009-03-22 21:15:45 +00:00
void SetMinimalSize ( uint min_x , uint min_y ) ;
2021-04-05 17:43:12 +00:00
void SetMinimalSizeAbsolute ( uint min_x , uint min_y ) ;
2009-11-17 13:04:05 +00:00
void SetMinimalTextLines ( uint8 min_lines , uint8 spacing , FontSize size ) ;
2009-11-22 18:26:01 +00:00
void SetFill ( uint fill_x , uint fill_y ) ;
2009-03-22 21:15:45 +00:00
void SetResize ( uint resize_x , uint resize_y ) ;
2021-04-08 08:02:38 +00:00
void AssignSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height , bool rtl ) override ;
2009-05-21 16:00:52 +00:00
uint min_x ; ///< Minimal horizontal size of only this widget.
uint min_y ; ///< Minimal vertical size of only this widget.
2021-04-05 17:43:12 +00:00
bool absolute ; ///< Set if minimum size is fixed and should not be resized.
uint uz_min_x ; ///< Unscaled Minimal horizontal size of only this widget.
uint uz_min_y ; ///< Unscaled Minimal vertical size of only this widget.
uint8 uz_text_lines ; ///< 'Unscaled' text lines, stored for resize calculation.
uint8 uz_text_spacing ; ///< 'Unscaled' text padding, stored for resize calculation.
FontSize uz_text_size ; ///< 'Unscaled' font size, stored for resize calculation.
2009-03-22 21:15:45 +00:00
} ;
2019-09-29 20:27:32 +00:00
/** Nested widget flags that affect display and interaction with 'real' widgets. */
2009-06-03 21:13:13 +00:00
enum NWidgetDisplay {
2009-09-19 13:08:37 +00:00
/* Generic. */
2009-09-02 20:28:41 +00:00
NDB_LOWERED = 0 , ///< Widget is lowered (pressed down) bit.
NDB_DISABLED = 1 , ///< Widget is disabled (greyed out) bit.
2009-09-19 13:08:37 +00:00
/* Viewport widget. */
2009-09-02 20:28:41 +00:00
NDB_NO_TRANSPARENCY = 2 , ///< Viewport is never transparent.
NDB_SHADE_GREY = 3 , ///< Shade viewport to grey-scale.
NDB_SHADE_DIMMED = 4 , ///< Display dimmed colours in the viewport.
2009-09-19 13:08:37 +00:00
/* Button dropdown widget. */
2018-10-28 02:17:36 +00:00
NDB_DROPDOWN_ACTIVE = 5 , ///< Dropdown menu of the button dropdown widget is active. @see #NWID_BUTTON_DROPDOWN
2010-08-12 09:14:34 +00:00
/* Scrollbar widget. */
NDB_SCROLLBAR_UP = 6 , ///< Up-button is lowered bit.
NDB_SCROLLBAR_DOWN = 7 , ///< Down-button is lowered bit.
2011-12-19 21:05:14 +00:00
/* Generic. */
NDB_HIGHLIGHT = 8 , ///< Highlight of widget is on.
2009-09-02 20:28:41 +00:00
ND_LOWERED = 1 < < NDB_LOWERED , ///< Bit value of the lowered flag.
ND_DISABLED = 1 < < NDB_DISABLED , ///< Bit value of the disabled flag.
2011-12-19 21:05:14 +00:00
ND_HIGHLIGHT = 1 < < NDB_HIGHLIGHT , ///< Bit value of the highlight flag.
2009-09-02 20:28:41 +00:00
ND_NO_TRANSPARENCY = 1 < < NDB_NO_TRANSPARENCY , ///< Bit value of the 'no transparency' flag.
ND_SHADE_GREY = 1 < < NDB_SHADE_GREY , ///< Bit value of the 'shade to grey' flag.
ND_SHADE_DIMMED = 1 < < NDB_SHADE_DIMMED , ///< Bit value of the 'dimmed colours' flag.
2009-09-19 13:08:37 +00:00
ND_DROPDOWN_ACTIVE = 1 < < NDB_DROPDOWN_ACTIVE , ///< Bit value of the 'dropdown active' flag.
2010-08-12 09:14:34 +00:00
ND_SCROLLBAR_UP = 1 < < NDB_SCROLLBAR_UP , ///< Bit value of the 'scrollbar up' flag.
ND_SCROLLBAR_DOWN = 1 < < NDB_SCROLLBAR_DOWN , ///< Bit value of the 'scrollbar down' flag.
2010-10-15 11:16:14 +00:00
ND_SCROLLBAR_BTN = ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN , ///< Bit value of the 'scrollbar up' or 'scrollbar down' flag.
2009-06-03 21:13:13 +00:00
} ;
2010-03-23 22:25:43 +00:00
DECLARE_ENUM_AS_BIT_SET ( NWidgetDisplay )
2009-06-03 21:13:13 +00:00
2010-08-01 19:22:34 +00:00
/**
* Base class for a ' real ' widget .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-03-22 21:15:45 +00:00
class NWidgetCore : public NWidgetResizeBase {
public :
2012-12-25 22:10:43 +00:00
NWidgetCore ( WidgetType tp , Colours colour , uint fill_x , uint fill_y , uint32 widget_data , StringID tool_tip ) ;
2009-03-22 21:15:45 +00:00
void SetIndex ( int index ) ;
2012-12-25 22:10:43 +00:00
void SetDataTip ( uint32 widget_data , StringID tool_tip ) ;
2021-01-07 22:06:23 +00:00
void SetToolTip ( StringID tool_tip ) ;
2021-04-19 15:37:10 +00:00
void SetTextColour ( TextColour colour ) ;
2021-04-19 11:12:07 +00:00
void SetAlignment ( StringAlignment align ) ;
2009-03-22 21:15:45 +00:00
2009-06-03 21:13:13 +00:00
inline void SetLowered ( bool lowered ) ;
2009-10-30 07:51:33 +00:00
inline bool IsLowered ( ) const ;
2009-06-03 21:13:13 +00:00
inline void SetDisabled ( bool disabled ) ;
2009-10-30 07:51:33 +00:00
inline bool IsDisabled ( ) const ;
2009-06-03 21:13:13 +00:00
2019-03-03 22:25:13 +00:00
void FillNestedArray ( NWidgetBase * * array , uint length ) override ;
NWidgetCore * GetWidgetFromPos ( int x , int y ) override ;
bool IsHighlighted ( ) const override ;
TextColour GetHighlightColour ( ) const override ;
void SetHighlighted ( TextColour highlight_colour ) override ;
2009-03-22 21:15:45 +00:00
2009-06-03 21:13:13 +00:00
NWidgetDisplay disp_flags ; ///< Flags that affect display and interaction with the widget.
Colours colour ; ///< Colour of this widget.
int index ; ///< Index of the nested widget in the widget array of the window (\c -1 means 'not used').
2012-12-25 22:10:43 +00:00
uint32 widget_data ; ///< Data of the widget. @see Widget::data
2009-06-03 21:13:13 +00:00
StringID tool_tip ; ///< Tooltip of the widget. @see Widget::tootips
2010-08-12 06:44:45 +00:00
int scrollbar_index ; ///< Index of an attached scrollbar.
2011-12-19 21:05:14 +00:00
TextColour highlight_colour ; ///< Colour of highlight.
2021-04-19 15:37:10 +00:00
TextColour text_colour ; ///< Colour of text within widget.
2021-04-19 11:12:07 +00:00
StringAlignment align ; ///< Alignment of text/image within widget.
2009-03-22 21:15:45 +00:00
} ;
2011-12-19 21:05:14 +00:00
/**
* Highlight the widget or not .
2013-01-08 22:46:42 +00:00
* @ param highlight_colour Widget must be highlighted ( blink ) .
2011-12-19 21:05:14 +00:00
*/
inline void NWidgetCore : : SetHighlighted ( TextColour highlight_colour )
{
this - > disp_flags = highlight_colour ! = TC_INVALID ? SETBITS ( this - > disp_flags , ND_HIGHLIGHT ) : CLRBITS ( this - > disp_flags , ND_HIGHLIGHT ) ;
this - > highlight_colour = highlight_colour ;
}
/** Return whether the widget is highlighted. */
inline bool NWidgetCore : : IsHighlighted ( ) const
{
return HasBit ( this - > disp_flags , NDB_HIGHLIGHT ) ;
}
/** Return the colour of the highlight. */
inline TextColour NWidgetCore : : GetHighlightColour ( ) const
{
return this - > highlight_colour ;
}
2009-06-03 21:13:13 +00:00
/**
* Lower or raise the widget .
* @ param lowered Widget must be lowered ( drawn pressed down ) .
*/
inline void NWidgetCore : : SetLowered ( bool lowered )
{
this - > disp_flags = lowered ? SETBITS ( this - > disp_flags , ND_LOWERED ) : CLRBITS ( this - > disp_flags , ND_LOWERED ) ;
}
/** Return whether the widget is lowered. */
2009-10-30 07:51:33 +00:00
inline bool NWidgetCore : : IsLowered ( ) const
2009-06-03 21:13:13 +00:00
{
return HasBit ( this - > disp_flags , NDB_LOWERED ) ;
}
/**
* Disable ( grey - out ) or enable the widget .
* @ param disabled Widget must be disabled .
*/
inline void NWidgetCore : : SetDisabled ( bool disabled )
{
this - > disp_flags = disabled ? SETBITS ( this - > disp_flags , ND_DISABLED ) : CLRBITS ( this - > disp_flags , ND_DISABLED ) ;
}
/** Return whether the widget is disabled. */
2009-10-30 07:51:33 +00:00
inline bool NWidgetCore : : IsDisabled ( ) const
2009-06-03 21:13:13 +00:00
{
return HasBit ( this - > disp_flags , NDB_DISABLED ) ;
}
2010-08-01 19:22:34 +00:00
/**
* Baseclass for container widgets .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-03-22 21:15:45 +00:00
class NWidgetContainer : public NWidgetBase {
public :
NWidgetContainer ( WidgetType tp ) ;
~ NWidgetContainer ( ) ;
2021-04-05 17:43:12 +00:00
void AdjustPaddingForZoom ( ) override ;
2009-03-22 21:15:45 +00:00
void Add ( NWidgetBase * wid ) ;
2019-03-03 22:25:13 +00:00
void FillNestedArray ( NWidgetBase * * array , uint length ) override ;
2009-03-28 11:13:39 +00:00
2009-04-26 16:30:12 +00:00
/** Return whether the container is empty. */
2019-04-10 21:07:06 +00:00
inline bool IsEmpty ( ) { return head = = nullptr ; }
2009-04-26 16:30:12 +00:00
2019-03-03 22:25:13 +00:00
NWidgetBase * GetWidgetOfType ( WidgetType tp ) override ;
2009-06-04 12:46:37 +00:00
2009-04-30 19:21:51 +00:00
protected :
NWidgetBase * head ; ///< Pointer to first widget in container.
NWidgetBase * tail ; ///< Pointer to last widget in container.
} ;
2009-12-22 20:43:25 +00:00
/** Display planes with zero size for #NWidgetStacked. */
enum StackedZeroSizePlanes {
SZSP_VERTICAL = INT_MAX / 2 , ///< Display plane with zero size horizontally, and filling and resizing vertically.
SZSP_HORIZONTAL , ///< Display plane with zero size vertically, and filling and resizing horizontally.
SZSP_NONE , ///< Display plane with zero size in both directions (none filling and resizing).
SZSP_BEGIN = SZSP_VERTICAL , ///< First zero-size plane.
} ;
2009-10-10 14:20:38 +00:00
2010-08-01 19:22:34 +00:00
/**
* Stacked widgets , widgets all occupying the same space in the window .
2009-10-10 14:20:38 +00:00
* # NWID_SELECTION allows for selecting one of several panels ( planes ) to tbe displayed . All planes must have the same size .
* Since all planes are also initialized , switching between different planes can be done while the window is displayed .
*
2009-12-22 20:43:25 +00:00
* There are also a number of special planes ( defined in # StackedZeroSizePlanes ) that have zero size in one direction ( and are stretchable in
* the other direction ) or have zero size in both directions . They are used to make all child planes of the widget disappear .
* Unlike switching between the regular display planes ( that all have the same size ) , switching from or to one of the zero - sized planes means that
2018-10-28 02:17:36 +00:00
* a # Window : : ReInit ( ) is needed to re - initialize the window since its size changes .
2009-04-30 19:21:51 +00:00
*/
class NWidgetStacked : public NWidgetContainer {
public :
2009-11-15 10:30:32 +00:00
NWidgetStacked ( ) ;
2009-04-30 19:21:51 +00:00
2009-09-19 11:55:44 +00:00
void SetIndex ( int index ) ;
2021-04-05 17:43:12 +00:00
void AdjustPaddingForZoom ( ) override ;
2019-03-03 22:25:13 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override ;
void AssignSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height , bool rtl ) override ;
void FillNestedArray ( NWidgetBase * * array , uint length ) override ;
2009-06-03 21:13:13 +00:00
2019-03-03 22:25:13 +00:00
void Draw ( const Window * w ) override ;
NWidgetCore * GetWidgetFromPos ( int x , int y ) override ;
2009-09-19 11:55:44 +00:00
void SetDisplayedPlane ( int plane ) ;
int shown_plane ; ///< Plane being displayed (for #NWID_SELECTION only).
int index ; ///< If non-negative, index in the #Window::nested_array.
2009-04-30 19:21:51 +00:00
} ;
2009-06-26 21:25:25 +00:00
/** Nested widget container flags, */
enum NWidContainerFlags {
NCB_EQUALSIZE = 0 , ///< Containers should keep all their (resizing) children equally large.
NC_NONE = 0 , ///< All flags cleared.
NC_EQUALSIZE = 1 < < NCB_EQUALSIZE , ///< Value of the #NCB_EQUALSIZE flag.
} ;
2010-03-23 22:25:43 +00:00
DECLARE_ENUM_AS_BIT_SET ( NWidContainerFlags )
2009-06-26 21:25:25 +00:00
2009-04-30 19:21:51 +00:00
/** Container with pre/inter/post child space. */
class NWidgetPIPContainer : public NWidgetContainer {
public :
2009-06-26 21:25:25 +00:00
NWidgetPIPContainer ( WidgetType tp , NWidContainerFlags flags = NC_NONE ) ;
2009-04-30 19:21:51 +00:00
2021-04-05 17:43:12 +00:00
void AdjustPaddingForZoom ( ) override ;
2009-04-30 19:21:51 +00:00
void SetPIP ( uint8 pip_pre , uint8 pip_inter , uint8 pip_post ) ;
2019-03-03 22:25:13 +00:00
void Draw ( const Window * w ) override ;
NWidgetCore * GetWidgetFromPos ( int x , int y ) override ;
2009-06-04 12:46:37 +00:00
2009-03-29 13:25:01 +00:00
protected :
2009-06-26 21:25:25 +00:00
NWidContainerFlags flags ; ///< Flags of the container.
uint8 pip_pre ; ///< Amount of space before first widget.
uint8 pip_inter ; ///< Amount of space between widgets.
uint8 pip_post ; ///< Amount of space after last widget.
2021-04-05 17:43:12 +00:00
uint8 uz_pip_pre ; ///< Unscaled space before first widget.
uint8 uz_pip_inter ; ///< Unscaled space between widgets.
uint8 uz_pip_post ; ///< Unscaled space after last widget.
2009-03-22 21:15:45 +00:00
} ;
2010-08-01 19:22:34 +00:00
/**
* Horizontal container .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-04-30 19:21:51 +00:00
class NWidgetHorizontal : public NWidgetPIPContainer {
2009-03-22 21:15:45 +00:00
public :
2009-06-26 21:25:25 +00:00
NWidgetHorizontal ( NWidContainerFlags flags = NC_NONE ) ;
2009-03-22 21:15:45 +00:00
2021-04-08 08:02:38 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override ;
void AssignSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height , bool rtl ) override ;
2009-03-22 21:15:45 +00:00
} ;
2010-08-01 19:22:34 +00:00
/**
* Horizontal container that doesn ' t change the direction of the widgets for RTL languages .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-03-26 20:39:04 +00:00
class NWidgetHorizontalLTR : public NWidgetHorizontal {
public :
2009-06-26 21:25:25 +00:00
NWidgetHorizontalLTR ( NWidContainerFlags flags = NC_NONE ) ;
2009-03-26 20:39:04 +00:00
2021-04-08 08:02:38 +00:00
void AssignSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height , bool rtl ) override ;
2009-03-26 20:39:04 +00:00
} ;
2010-08-01 19:22:34 +00:00
/**
* Vertical container .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-04-30 19:21:51 +00:00
class NWidgetVertical : public NWidgetPIPContainer {
2009-03-22 21:15:45 +00:00
public :
2009-06-26 21:25:25 +00:00
NWidgetVertical ( NWidContainerFlags flags = NC_NONE ) ;
2009-03-22 21:15:45 +00:00
2021-04-08 08:02:38 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override ;
void AssignSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height , bool rtl ) override ;
2009-03-22 21:15:45 +00:00
} ;
2010-12-10 17:47:11 +00:00
/**
* Matrix container with implicitly equal sized ( virtual ) sub - widgets .
* This widget must have exactly one sub - widget . After that this sub - widget
* is used to draw all of the data within the matrix piece by piece .
* DrawWidget and OnClick calls will be done to that sub - widget , where the
* 16 high bits are used to encode the index into the matrix .
* @ ingroup NestedWidgets
*/
class NWidgetMatrix : public NWidgetPIPContainer {
public :
NWidgetMatrix ( ) ;
void SetIndex ( int index ) ;
void SetColour ( Colours colour ) ;
void SetClicked ( int clicked ) ;
void SetCount ( int count ) ;
void SetScrollbar ( Scrollbar * sb ) ;
2019-03-03 22:25:13 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override ;
void AssignSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height , bool rtl ) override ;
void FillNestedArray ( NWidgetBase * * array , uint length ) override ;
2010-12-10 17:47:11 +00:00
2019-03-03 22:25:13 +00:00
NWidgetCore * GetWidgetFromPos ( int x , int y ) override ;
void Draw ( const Window * w ) override ;
2010-12-10 17:47:11 +00:00
protected :
int index ; ///< If non-negative, index in the #Window::nested_array.
Colours colour ; ///< Colour of this widget.
int clicked ; ///< The currently clicked widget.
int count ; ///< Amount of valid widgets.
Scrollbar * sb ; ///< The scrollbar we're associated with.
private :
int widget_w ; ///< The width of the child widget including inter spacing.
int widget_h ; ///< The height of the child widget including inter spacing.
int widgets_x ; ///< The number of visible widgets in horizontal direction.
int widgets_y ; ///< The number of visible widgets in vertical direction.
void GetScrollOffsets ( int & start_x , int & start_y , int & base_offs_x , int & base_offs_y ) ;
} ;
2009-03-22 21:15:45 +00:00
2010-08-01 19:22:34 +00:00
/**
* Spacer widget .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-03-22 21:15:45 +00:00
class NWidgetSpacer : public NWidgetResizeBase {
public :
2021-04-22 00:18:22 +00:00
NWidgetSpacer ( int width , int height ) ;
2009-03-22 21:15:45 +00:00
2019-03-03 22:25:13 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override ;
void FillNestedArray ( NWidgetBase * * array , uint length ) override ;
2009-06-03 21:13:13 +00:00
2019-03-03 22:25:13 +00:00
void Draw ( const Window * w ) override ;
void SetDirty ( const Window * w ) const override ;
NWidgetCore * GetWidgetFromPos ( int x , int y ) override ;
2009-03-22 21:15:45 +00:00
} ;
2010-08-01 19:22:34 +00:00
/**
* Nested widget with a child .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-03-22 21:15:45 +00:00
class NWidgetBackground : public NWidgetCore {
public :
2019-04-10 21:07:06 +00:00
NWidgetBackground ( WidgetType tp , Colours colour , int index , NWidgetPIPContainer * child = nullptr ) ;
2009-03-22 21:15:45 +00:00
~ NWidgetBackground ( ) ;
void Add ( NWidgetBase * nwid ) ;
2009-03-29 13:25:01 +00:00
void SetPIP ( uint8 pip_pre , uint8 pip_inter , uint8 pip_post ) ;
2009-03-22 21:15:45 +00:00
2021-04-05 17:43:12 +00:00
void AdjustPaddingForZoom ( ) override ;
2019-03-03 22:25:13 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override ;
void AssignSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height , bool rtl ) override ;
2009-03-22 21:15:45 +00:00
2019-03-03 22:25:13 +00:00
void FillNestedArray ( NWidgetBase * * array , uint length ) override ;
2009-06-03 20:00:33 +00:00
2019-03-03 22:25:13 +00:00
void Draw ( const Window * w ) override ;
NWidgetCore * GetWidgetFromPos ( int x , int y ) override ;
NWidgetBase * GetWidgetOfType ( WidgetType tp ) override ;
2009-06-03 21:13:13 +00:00
2009-03-22 21:15:45 +00:00
private :
2009-04-30 19:21:51 +00:00
NWidgetPIPContainer * child ; ///< Child widget.
2009-03-22 21:15:45 +00:00
} ;
2009-07-26 17:29:01 +00:00
/**
* Nested widget to display a viewport in a window .
* After initializing the nested widget tree , call # InitializeViewport ( ) . After changing the window size ,
* call # UpdateViewportCoordinates ( ) eg from Window : : OnResize ( ) .
2018-10-28 02:17:36 +00:00
* If the # disp_flags field contains the # ND_NO_TRANSPARENCY bit , the viewport will disable transparency .
2009-09-02 20:28:41 +00:00
* Shading to grey - scale is controlled with the # ND_SHADE_GREY bit ( used for B & W news papers ) , the # ND_SHADE_DIMMED gives dimmed colours ( for colour news papers ) .
2009-07-26 17:29:01 +00:00
* @ todo Class derives from # NWidgetCore , but does not use # colour , # widget_data , or # tool_tip .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-07-26 17:29:01 +00:00
class NWidgetViewport : public NWidgetCore {
public :
NWidgetViewport ( int index ) ;
2019-03-03 22:25:13 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override ;
void Draw ( const Window * w ) override ;
2009-07-26 17:29:01 +00:00
void InitializeViewport ( Window * w , uint32 follow_flags , ZoomLevel zoom ) ;
void UpdateViewportCoordinates ( Window * w ) ;
} ;
2010-08-12 09:11:12 +00:00
/**
* Scrollbar data structure
*/
class Scrollbar {
private :
const bool is_vertical ; ///< Scrollbar has vertical orientation.
uint16 count ; ///< Number of elements in the list.
uint16 cap ; ///< Number of visible elements of the scroll bar.
uint16 pos ; ///< Index of first visible item of the list.
2011-01-09 20:39:06 +00:00
uint16 stepsize ; ///< Distance to scroll, when pressing the buttons or using the wheel.
2010-08-12 09:11:12 +00:00
public :
2011-01-09 20:39:06 +00:00
/** Stepping sizes when scrolling */
enum ScrollbarStepping {
SS_RAW , ///< Step in single units.
SS_SMALL , ///< Step in #stepsize units.
SS_BIG , ///< Step in #cap units.
} ;
Scrollbar ( bool is_vertical ) : is_vertical ( is_vertical ) , stepsize ( 1 )
2010-08-12 09:11:12 +00:00
{
}
/**
* Gets the number of elements in the list
* @ return the number of elements
*/
2011-12-20 17:57:56 +00:00
inline uint16 GetCount ( ) const
2010-08-12 09:11:12 +00:00
{
return this - > count ;
}
/**
* Gets the number of visible elements of the scrollbar
* @ return the number of visible elements
*/
2011-12-20 17:57:56 +00:00
inline uint16 GetCapacity ( ) const
2010-08-12 09:11:12 +00:00
{
return this - > cap ;
}
/**
* Gets the position of the first visible element in the list
* @ return the position of the element
*/
2011-12-20 17:57:56 +00:00
inline uint16 GetPosition ( ) const
2010-08-12 09:11:12 +00:00
{
return this - > pos ;
}
/**
* Checks whether given current item is visible in the list
* @ param item to check
* @ return true iff the item is visible
*/
2011-12-20 17:57:56 +00:00
inline bool IsVisible ( uint16 item ) const
2010-08-12 09:11:12 +00:00
{
return IsInsideBS ( item , this - > GetPosition ( ) , this - > GetCapacity ( ) ) ;
}
2010-12-10 17:33:50 +00:00
/**
* Is the scrollbar vertical or not ?
* @ return True iff the scrollbar is vertical .
*/
2011-12-20 17:57:56 +00:00
inline bool IsVertical ( ) const
2010-12-10 17:33:50 +00:00
{
return this - > is_vertical ;
}
2011-01-09 20:39:06 +00:00
/**
* Set the distance to scroll when using the buttons or the wheel .
* @ param stepsize Scrolling speed .
*/
void SetStepSize ( uint16 stepsize )
{
assert ( stepsize > 0 ) ;
this - > stepsize = stepsize ;
}
2010-08-12 09:11:12 +00:00
/**
* Sets the number of elements in the list
* @ param num the number of elements in the list
* @ note updates the position if needed
*/
void SetCount ( int num )
{
assert ( num > = 0 ) ;
assert ( num < = MAX_UVALUE ( uint16 ) ) ;
this - > count = num ;
num - = this - > cap ;
if ( num < 0 ) num = 0 ;
if ( num < this - > pos ) this - > pos = num ;
}
/**
* Set the capacity of visible elements .
* @ param capacity the new capacity
* @ note updates the position if needed
*/
void SetCapacity ( int capacity )
{
assert ( capacity > 0 ) ;
assert ( capacity < = MAX_UVALUE ( uint16 ) ) ;
this - > cap = capacity ;
2021-01-08 10:16:18 +00:00
if ( this - > cap + this - > pos > this - > count ) this - > pos = std : : max ( 0 , this - > count - this - > cap ) ;
2010-08-12 09:11:12 +00:00
}
void SetCapacityFromWidget ( Window * w , int widget , int padding = 0 ) ;
/**
* Sets the position of the first visible element
* @ param position the position of the element
2021-05-07 11:36:08 +00:00
* @ return true iff the position has changed
2010-08-12 09:11:12 +00:00
*/
2021-05-07 11:36:08 +00:00
bool SetPosition ( int position )
2010-08-12 09:11:12 +00:00
{
assert ( position > = 0 ) ;
assert ( this - > count < = this - > cap ? ( position = = 0 ) : ( position + this - > cap < = this - > count ) ) ;
2021-05-07 11:36:08 +00:00
uint16 old_pos = this - > pos ;
2010-08-12 09:11:12 +00:00
this - > pos = position ;
2021-05-07 11:36:08 +00:00
return this - > pos ! = old_pos ;
2010-08-12 09:11:12 +00:00
}
/**
* Updates the position of the first visible element by the given amount .
* If the position would be too low or high it will be clamped appropriately
* @ param difference the amount of change requested
2011-01-09 20:39:06 +00:00
* @ param unit The stepping unit of \ a difference
2021-05-07 11:36:08 +00:00
* @ return true iff the position has changed
2010-08-12 09:11:12 +00:00
*/
2021-05-07 11:36:08 +00:00
bool UpdatePosition ( int difference , ScrollbarStepping unit = SS_SMALL )
2010-08-12 09:11:12 +00:00
{
2021-05-07 11:36:08 +00:00
if ( difference = = 0 ) return false ;
2011-01-09 20:39:06 +00:00
switch ( unit ) {
case SS_SMALL : difference * = this - > stepsize ; break ;
case SS_BIG : difference * = this - > cap ; break ;
default : break ;
}
2021-05-07 11:36:08 +00:00
return this - > SetPosition ( Clamp ( this - > pos + difference , 0 , std : : max ( this - > count - this - > cap , 0 ) ) ) ;
2010-08-12 09:11:12 +00:00
}
/**
* Scroll towards the given position ; if the item is visible nothing
* happens , otherwise it will be shown either at the bottom or top of
* the window depending on where in the list it was .
* @ param position the position to scroll towards .
*/
void ScrollTowards ( int position )
{
if ( position < this - > GetPosition ( ) ) {
/* scroll up to the item */
this - > SetPosition ( position ) ;
} else if ( position > = this - > GetPosition ( ) + this - > GetCapacity ( ) ) {
/* scroll down so that the item is at the bottom */
this - > SetPosition ( position - this - > GetCapacity ( ) + 1 ) ;
}
}
2021-04-21 14:22:45 +00:00
int GetScrolledRowFromWidget ( int clickpos , const Window * const w , int widget , int padding = 0 ) const ;
2021-04-18 18:29:46 +00:00
EventState UpdateListPositionOnKeyPress ( int & list_position , uint16 keycode ) const ;
2010-08-12 09:11:12 +00:00
} ;
2010-08-12 09:07:45 +00:00
/**
* Nested widget to display and control a scrollbar in a window .
* Also assign the scrollbar to other widgets using # SetScrollbar ( ) to make the mousewheel work .
* @ ingroup NestedWidgets
*/
2010-08-12 09:13:04 +00:00
class NWidgetScrollbar : public NWidgetCore , public Scrollbar {
2010-08-12 09:07:45 +00:00
public :
NWidgetScrollbar ( WidgetType tp , Colours colour , int index ) ;
2019-03-03 22:25:13 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override ;
void Draw ( const Window * w ) override ;
2011-10-11 08:07:47 +00:00
static void InvalidateDimensionCache ( ) ;
static Dimension GetVerticalDimension ( ) ;
static Dimension GetHorizontalDimension ( ) ;
private :
static Dimension vertical_dimension ; ///< Cached size of vertical scrollbar button.
static Dimension horizontal_dimension ; ///< Cached size of horizontal scrollbar button.
2010-08-12 09:07:45 +00:00
} ;
2010-08-01 19:22:34 +00:00
/**
* Leaf widget .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgets
*/
2009-03-22 21:15:45 +00:00
class NWidgetLeaf : public NWidgetCore {
public :
2014-10-06 19:16:29 +00:00
NWidgetLeaf ( WidgetType tp , Colours colour , int index , uint32 data , StringID tip ) ;
2009-06-03 21:13:13 +00:00
2019-03-03 22:25:13 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override ;
void Draw ( const Window * w ) override ;
2009-07-12 12:19:41 +00:00
2009-09-19 13:08:37 +00:00
bool ButtonHit ( const Point & pt ) ;
2009-07-12 12:19:41 +00:00
static void InvalidateDimensionCache ( ) ;
2013-10-13 19:18:30 +00:00
static Dimension dropdown_dimension ; ///< Cached size of a dropdown widget.
2017-08-27 11:48:38 +00:00
static Dimension resizebox_dimension ; ///< Cached size of a resizebox widget.
static Dimension closebox_dimension ; ///< Cached size of a closebox widget.
2009-07-12 12:19:41 +00:00
private :
2009-12-21 16:06:20 +00:00
static Dimension shadebox_dimension ; ///< Cached size of a shadebox widget.
2010-04-24 13:27:22 +00:00
static Dimension debugbox_dimension ; ///< Cached size of a debugbox widget.
2013-05-26 19:30:07 +00:00
static Dimension defsizebox_dimension ; ///< Cached size of a defsizebox widget.
2009-07-12 12:19:41 +00:00
static Dimension stickybox_dimension ; ///< Cached size of a stickybox widget.
2009-03-22 21:15:45 +00:00
} ;
2010-05-16 19:13:31 +00:00
/**
* Return the biggest possible size of a nested widget .
* @ param base Base size of the widget .
* @ param max_space Available space for the widget .
* @ param step Stepsize of the widget .
* @ return Biggest possible size of the widget , assuming that \ a base may only be incremented by \ a step size steps .
*/
2011-12-20 17:57:56 +00:00
static inline uint ComputeMaxSize ( uint base , uint max_space , uint step )
2010-05-16 19:13:31 +00:00
{
if ( base > = max_space | | step = = 0 ) return base ;
if ( step = = 1 ) return max_space ;
uint increment = max_space - base ;
increment - = increment % step ;
return base + increment ;
}
2009-04-30 17:35:38 +00:00
/**
* @ defgroup NestedWidgetParts Hierarchical widget parts
* To make nested widgets easier to enter , nested widget parts have been created . They allow the tree to be defined in a flat array of parts .
*
* - Leaf widgets start with a # NWidget ( WidgetType tp , Colours col , int16 idx ) part .
* Next , specify its properties with one or more of
* - # SetMinimalSize Define the minimal size of the widget .
* - # SetFill Define how the widget may grow to make it nicely .
* - # SetDataTip Define the data and the tooltip of the widget .
* - # SetResize Define how the widget may resize .
* - # SetPadding Create additional space around the widget .
*
* - To insert a nested widget tree from an external source , nested widget part # NWidgetFunction exists .
* For further customization , the # SetPadding part may be used .
*
* - Space widgets ( # NWidgetSpacer ) start with a # NWidget ( WidgetType tp ) , followed by one or more of
* - # SetMinimalSize Define the minimal size of the widget .
* - # SetFill Define how the widget may grow to make it nicely .
* - # SetResize Define how the widget may resize .
* - # SetPadding Create additional space around the widget .
*
2010-12-10 17:47:11 +00:00
* - Container widgets # NWidgetHorizontal , # NWidgetHorizontalLTR , # NWidgetVertical , and # NWidgetMatrix , start with a # NWidget ( WidgetType tp ) part .
2009-04-30 17:35:38 +00:00
* Their properties are derived from the child widgets so they cannot be specified .
* You can however use
* - # SetPadding Define additional padding around the container .
* - # SetPIP Set additional pre / inter / post child widget space .
* .
* Underneath these properties , all child widgets of the container must be defined . To denote that they are childs , add an indent before the nested widget parts of
* the child widgets ( it has no meaning for the compiler but it makes the widget parts easier to read ) .
* Below the last child widget , use an # EndContainer part . This part should be aligned with the # NWidget part that started the container .
*
2013-01-08 22:46:42 +00:00
* - Stacked widgets # NWidgetStacked map each of their children onto the same space . It behaves like a container , except there is no pre / inter / post space ,
2009-04-30 19:21:51 +00:00
* so the widget does not support # SetPIP . # SetPadding is allowed though .
* Like the other container widgets , below the last child widgets , a # EndContainer part should be used to denote the end of the stacked widget .
*
2009-04-30 17:35:38 +00:00
* - Background widgets # NWidgetBackground start with a # NWidget ( WidgetType tp , Colours col , int16 idx ) part .
* What follows depends on how the widget is used .
* - If the widget is used as a leaf widget , that is , to create some space in the window to display a viewport or some text , use the properties of the
* leaf widgets to define how it behaves .
* - If the widget is used a background behind other widgets , it is considered to be a container widgets . Use the properties listed there to define its
* behaviour .
* .
* In both cases , the background widget \ b MUST end with a # EndContainer widget part .
*
* @ see NestedWidgets
*/
2009-03-22 21:15:45 +00:00
2010-08-01 19:22:34 +00:00
/**
* Widget part for storing data and tooltip information .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgetParts
*/
2009-03-22 21:15:45 +00:00
struct NWidgetPartDataTip {
2014-10-06 19:16:29 +00:00
uint32 data ; ///< Data value of the widget.
2009-03-22 21:15:45 +00:00
StringID tooltip ; ///< Tooltip of the widget.
} ;
2010-08-01 19:22:34 +00:00
/**
* Widget part for storing basic widget information .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgetParts
*/
2009-03-22 21:15:45 +00:00
struct NWidgetPartWidget {
Colours colour ; ///< Widget colour.
int16 index ; ///< Widget index in the widget array.
} ;
2010-08-01 19:22:34 +00:00
/**
* Widget part for storing padding .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgetParts
*/
2009-03-28 11:13:39 +00:00
struct NWidgetPartPaddings {
uint8 top , right , bottom , left ; ///< Paddings for all directions.
} ;
2010-08-01 19:22:34 +00:00
/**
* Widget part for storing pre / inter / post spaces .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgetParts
*/
2009-03-28 11:13:39 +00:00
struct NWidgetPartPIP {
uint8 pre , inter , post ; ///< Amount of space before/between/after child widgets.
} ;
2010-08-01 19:22:34 +00:00
/**
* Widget part for storing minimal text line data .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgetParts
*/
2009-11-17 13:04:05 +00:00
struct NWidgetPartTextLines {
uint8 lines ; ///< Number of text lines.
uint8 spacing ; ///< Extra spacing around lines.
FontSize size ; ///< Font size of text lines.
} ;
2021-04-19 15:37:10 +00:00
/**
* Widget part for storing text colour .
* @ ingroup NestedWidgetParts
*/
struct NWidgetPartTextColour {
TextColour colour ; ///< TextColour for DrawString.
} ;
2021-04-19 11:12:07 +00:00
/**
* Widget part for setting text / image alignment within a widget .
* @ ingroup NestedWidgetParts
*/
struct NWidgetPartAlignment {
StringAlignment align ; ///< Alignment of text/image.
} ;
2010-08-01 19:22:34 +00:00
/**
* Pointer to function returning a nested widget .
2009-07-15 22:17:08 +00:00
* @ param biggest_index Pointer to storage for collecting the biggest index used in the nested widget .
* @ return Nested widget ( tree ) .
2009-12-13 20:54:52 +00:00
* @ post \ c * biggest_index must contain the value of the biggest index in the returned tree .
2009-07-15 22:17:08 +00:00
*/
typedef NWidgetBase * NWidgetFunctionType ( int * biggest_index ) ;
2009-04-25 11:59:36 +00:00
2010-08-01 19:22:34 +00:00
/**
* Partial widget specification to allow NWidgets to be written nested .
2010-08-01 19:44:49 +00:00
* @ ingroup NestedWidgetParts
*/
2009-03-22 21:15:45 +00:00
struct NWidgetPart {
WidgetType type ; ///< Type of the part. @see NWidgetPartType.
union {
Point xy ; ///< Part with an x/y size.
NWidgetPartDataTip data_tip ; ///< Part with a data/tooltip.
NWidgetPartWidget widget ; ///< Part with a start of a widget.
2009-03-28 11:13:39 +00:00
NWidgetPartPaddings padding ; ///< Part with paddings.
NWidgetPartPIP pip ; ///< Part with pre/inter/post spaces.
2009-11-17 13:04:05 +00:00
NWidgetPartTextLines text_lines ; ///< Part with text line data.
2021-04-19 15:37:10 +00:00
NWidgetPartTextColour colour ; ///< Part with text colour data.
2021-04-19 11:12:07 +00:00
NWidgetPartAlignment align ; ///< Part with internal alignment.
2009-04-25 11:59:36 +00:00
NWidgetFunctionType * func_ptr ; ///< Part with a function call.
2009-06-26 21:25:25 +00:00
NWidContainerFlags cont_flags ; ///< Part with container flags.
2009-03-22 21:15:45 +00:00
} u ;
} ;
/**
* Widget part function for setting the resize step .
* @ param dx Horizontal resize step . 0 means no horizontal resizing .
2009-08-14 18:41:03 +00:00
* @ param dy Vertical resize step . 0 means no vertical resizing .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-22 21:15:45 +00:00
*/
static inline NWidgetPart SetResize ( int16 dx , int16 dy )
{
NWidgetPart part ;
part . type = WPT_RESIZE ;
part . u . xy . x = dx ;
part . u . xy . y = dy ;
return part ;
}
/**
* Widget part function for setting the minimal size .
2009-09-19 09:51:14 +00:00
* @ param x Horizontal minimal size .
* @ param y Vertical minimal size .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-22 21:15:45 +00:00
*/
static inline NWidgetPart SetMinimalSize ( int16 x , int16 y )
{
NWidgetPart part ;
part . type = WPT_MINSIZE ;
part . u . xy . x = x ;
part . u . xy . y = y ;
return part ;
}
2009-11-17 13:04:05 +00:00
/**
* Widget part function for setting the minimal text lines .
* @ param lines Number of text lines .
* @ param spacing Extra spacing required .
* @ param size Font size of text .
* @ ingroup NestedWidgetParts
*/
static inline NWidgetPart SetMinimalTextLines ( uint8 lines , uint8 spacing , FontSize size = FS_NORMAL )
{
NWidgetPart part ;
part . type = WPT_MINTEXTLINES ;
part . u . text_lines . lines = lines ;
part . u . text_lines . spacing = spacing ;
part . u . text_lines . size = size ;
return part ;
}
2021-04-19 15:37:10 +00:00
/**
* Widget part function for setting the text colour .
* @ param colour Colour to draw string within widget .
* @ ingroup NestedWidgetParts
*/
static inline NWidgetPart SetTextColour ( TextColour colour )
{
NWidgetPart part ;
part . type = WPT_TEXTCOLOUR ;
part . u . colour . colour = colour ;
return part ;
}
2021-04-19 11:12:07 +00:00
/**
* Widget part function for setting the alignment of text / images .
* @ param align Alignment of text / image within widget .
* @ ingroup NestedWidgetParts
*/
static inline NWidgetPart SetAlignment ( StringAlignment align )
{
NWidgetPart part ;
part . type = WPT_ALIGNMENT ;
part . u . align . align = align ;
return part ;
}
2009-03-22 21:15:45 +00:00
/**
* Widget part function for setting filling .
2009-12-13 21:12:36 +00:00
* @ param fill_x Horizontal filling step from minimal size .
* @ param fill_y Vertical filling step from minimal size .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-22 21:15:45 +00:00
*/
2009-11-22 18:26:01 +00:00
static inline NWidgetPart SetFill ( uint fill_x , uint fill_y )
2009-03-22 21:15:45 +00:00
{
NWidgetPart part ;
part . type = WPT_FILL ;
2009-11-22 18:26:01 +00:00
part . u . xy . x = fill_x ;
part . u . xy . y = fill_y ;
2009-03-22 21:15:45 +00:00
return part ;
}
/**
* Widget part function for denoting the end of a container
* ( horizontal , vertical , WWT_FRAME , WWT_INSET , or WWT_PANEL ) .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-22 21:15:45 +00:00
*/
static inline NWidgetPart EndContainer ( )
{
NWidgetPart part ;
part . type = WPT_ENDCONTAINER ;
return part ;
}
2010-08-01 19:22:34 +00:00
/**
* Widget part function for setting the data and tooltip .
2009-03-22 21:15:45 +00:00
* @ param data Data of the widget .
* @ param tip Tooltip of the widget .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-22 21:15:45 +00:00
*/
2014-10-06 19:16:29 +00:00
static inline NWidgetPart SetDataTip ( uint32 data , StringID tip )
2009-03-22 21:15:45 +00:00
{
NWidgetPart part ;
part . type = WPT_DATATIP ;
part . u . data_tip . data = data ;
part . u . data_tip . tooltip = tip ;
return part ;
}
2013-06-30 14:32:31 +00:00
/**
* Widget part function for setting the data and tooltip of WWT_MATRIX widgets
2013-06-30 14:36:31 +00:00
* @ param cols Number of columns . \ c 0 means to use draw columns with width according to the resize step size .
* @ param rows Number of rows . \ c 0 means to use draw rows with height according to the resize step size .
2013-06-30 14:32:31 +00:00
* @ param tip Tooltip of the widget .
* @ ingroup NestedWidgetParts
*/
static inline NWidgetPart SetMatrixDataTip ( uint8 cols , uint8 rows , StringID tip )
{
return SetDataTip ( ( rows < < MAT_ROW_START ) | ( cols < < MAT_COL_START ) , tip ) ;
}
2009-03-28 11:13:39 +00:00
/**
2009-04-30 17:35:38 +00:00
* Widget part function for setting additional space around a widget .
* Parameters start above the widget , and are specified in clock - wise direction .
2009-03-28 11:13:39 +00:00
* @ param top The padding above the widget .
* @ param right The padding right of the widget .
* @ param bottom The padding below the widget .
* @ param left The padding left of the widget .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-28 11:13:39 +00:00
*/
static inline NWidgetPart SetPadding ( uint8 top , uint8 right , uint8 bottom , uint8 left )
{
NWidgetPart part ;
part . type = WPT_PADDING ;
part . u . padding . top = top ;
part . u . padding . right = right ;
part . u . padding . bottom = bottom ;
part . u . padding . left = left ;
return part ;
}
/**
* Widget part function for setting a padding .
* @ param padding The padding to use for all directions .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-28 11:13:39 +00:00
*/
static inline NWidgetPart SetPadding ( uint8 padding )
{
return SetPadding ( padding , padding , padding , padding ) ;
}
/**
* Widget part function for setting a pre / inter / post spaces .
* @ param pre The amount of space before the first widget .
* @ param inter The amount of space between widgets .
* @ param post The amount of space after the last widget .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-28 11:13:39 +00:00
*/
static inline NWidgetPart SetPIP ( uint8 pre , uint8 inter , uint8 post )
{
NWidgetPart part ;
part . type = WPT_PIPSPACE ;
part . u . pip . pre = pre ;
part . u . pip . inter = inter ;
part . u . pip . post = post ;
return part ;
}
2010-08-12 06:44:45 +00:00
/**
* Attach a scrollbar to a widget .
* The scrollbar is controlled when using the mousewheel on the widget .
2013-01-08 22:46:42 +00:00
* Multiple widgets can refer to the same scrollbar to make the mousewheel work in all of them .
2010-08-12 06:44:45 +00:00
* @ param index Widget index of the scrollbar .
* @ ingroup NestedWidgetParts
*/
static inline NWidgetPart SetScrollbar ( int index )
{
NWidgetPart part ;
part . type = WPT_SCROLLBAR ;
part . u . widget . index = index ;
return part ;
}
2009-03-22 21:15:45 +00:00
/**
* Widget part function for starting a new ' real ' widget .
* @ param tp Type of the new nested widget .
* @ param col Colour of the new widget .
* @ param idx Index of the widget in the widget array .
* @ note with # WWT_PANEL , # WWT_FRAME , # WWT_INSET , a new container is started .
* Child widgets must have a index bigger than the parent index .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-22 21:15:45 +00:00
*/
2009-11-24 17:13:24 +00:00
static inline NWidgetPart NWidget ( WidgetType tp , Colours col , int16 idx = - 1 )
2009-03-22 21:15:45 +00:00
{
NWidgetPart part ;
part . type = tp ;
part . u . widget . colour = col ;
part . u . widget . index = idx ;
return part ;
}
/**
* Widget part function for starting a new horizontal container , vertical container , or spacer widget .
2018-10-28 02:17:36 +00:00
* @ param tp Type of the new nested widget , # NWID_HORIZONTAL , # NWID_VERTICAL , # NWID_SPACER , # NWID_SELECTION , and # NWID_MATRIX .
* @ param cont_flags Flags for the containers ( # NWID_HORIZONTAL and # NWID_VERTICAL ) .
2009-04-30 17:35:38 +00:00
* @ ingroup NestedWidgetParts
2009-03-22 21:15:45 +00:00
*/
2009-06-26 21:25:25 +00:00
static inline NWidgetPart NWidget ( WidgetType tp , NWidContainerFlags cont_flags = NC_NONE )
2009-03-22 21:15:45 +00:00
{
NWidgetPart part ;
part . type = tp ;
2009-06-26 21:25:25 +00:00
part . u . cont_flags = cont_flags ;
2009-03-22 21:15:45 +00:00
return part ;
}
2009-04-30 17:35:38 +00:00
/**
* Obtain a nested widget ( sub ) tree from an external source .
* @ param func_ptr Pointer to function that returns the tree .
* @ ingroup NestedWidgetParts
*/
2009-04-25 11:59:36 +00:00
static inline NWidgetPart NWidgetFunction ( NWidgetFunctionType * func_ptr )
{
NWidgetPart part ;
part . type = WPT_FUNCTION ;
part . u . func_ptr = func_ptr ;
return part ;
}
2009-12-21 16:16:40 +00:00
NWidgetContainer * MakeNWidgets ( const NWidgetPart * parts , int count , int * biggest_index , NWidgetContainer * container ) ;
NWidgetContainer * MakeWindowNWidgetTree ( const NWidgetPart * parts , int count , int * biggest_index , NWidgetStacked * * shade_select ) ;
2009-03-22 21:15:45 +00:00
2021-03-11 14:30:29 +00:00
NWidgetBase * MakeCompanyButtonRows ( int * biggest_index , int widget_first , int widget_last , Colours button_colour , int max_length , StringID button_tooltip ) ;
2010-08-19 12:56:31 +00:00
2009-02-09 01:06:23 +00:00
# endif /* WIDGET_TYPE_H */