2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file widget_type.h Definitions about widgets. */
13 #include "core/alloc_type.hpp"
14 #include "core/bitmath_func.hpp"
15 #include "core/math_func.hpp"
16 #include "strings_type.h"
18 #include "window_type.h"
20 /** Bits of the #WWT_MATRIX widget data. */
21 enum MatrixWidgetValues
{
22 /* Number of column bits of the WWT_MATRIX widget data. */
23 MAT_COL_START
= 0, ///< Lowest bit of the number of columns.
24 MAT_COL_BITS
= 8, ///< Number of bits for the number of columns in the matrix.
26 /* Number of row bits of the WWT_MATRIX widget data. */
27 MAT_ROW_START
= 8, ///< Lowest bit of the number of rows.
28 MAT_ROW_BITS
= 8, ///< Number of bits for the number of rows in the matrix.
31 /** Values for an arrow widget */
32 enum ArrowWidgetValues
{
33 AWV_DECREASE
, ///< Arrow to the left or in case of RTL to the right
34 AWV_INCREASE
, ///< Arrow to the right or in case of RTL to the left
35 AWV_LEFT
, ///< Force the arrow to the left
36 AWV_RIGHT
, ///< Force the arrow to the right
39 /** WidgetData values for a resize box widget. */
40 enum ResizeWidgetValues
{
41 RWV_SHOW_BEVEL
, ///< Bevel of resize box is shown.
42 RWV_HIDE_BEVEL
, ///< Bevel of resize box is hidden.
46 * Window widget types, nested widget types, and nested widget part types.
49 /* Window widget types. */
50 WWT_EMPTY
, ///< Empty widget, place holder to reserve space in widget tree.
52 WWT_PANEL
, ///< Simple depressed panel
53 WWT_INSET
, ///< Pressed (inset) panel, most commonly used as combo box _text_ area
54 WWT_IMGBTN
, ///< (Toggle) Button with image
55 WWT_IMGBTN_2
, ///< (Toggle) Button with diff image when clicked
56 WWT_ARROWBTN
, ///< (Toggle) Button with an arrow
57 WWT_TEXTBTN
, ///< (Toggle) Button with text
58 WWT_TEXTBTN_2
, ///< (Toggle) Button with diff text when clicked
59 WWT_LABEL
, ///< Centered label
60 WWT_TEXT
, ///< Pure simple text
61 WWT_MATRIX
, ///< Grid of rows and columns. @see MatrixWidgetValues
63 WWT_CAPTION
, ///< Window caption (window title between closebox and stickybox)
65 WWT_DEBUGBOX
, ///< NewGRF debug box (at top-right of a window, between WWT_CAPTION and WWT_SHADEBOX)
66 WWT_SHADEBOX
, ///< Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
67 WWT_DEFSIZEBOX
, ///< Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
68 WWT_STICKYBOX
, ///< Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
70 WWT_RESIZEBOX
, ///< Resize box (normally at bottom-right of a window)
71 WWT_CLOSEBOX
, ///< Close box (at top-left of a window)
72 WWT_DROPDOWN
, ///< Drop down list
73 WWT_EDITBOX
, ///< a textbox for typing
74 WWT_LAST
, ///< Last Item. use WIDGETS_END to fill up padding!!
76 /* Nested widget types. */
77 NWID_HORIZONTAL
, ///< Horizontal container.
78 NWID_HORIZONTAL_LTR
, ///< Horizontal container that doesn't change the order of the widgets for RTL languages.
79 NWID_VERTICAL
, ///< Vertical container.
80 NWID_MATRIX
, ///< Matrix container.
81 NWID_SPACER
, ///< Invisible widget that takes some space.
82 NWID_SELECTION
, ///< Stacked widgets, only one visible at a time (eg in a panel with tabs).
83 NWID_VIEWPORT
, ///< Nested widget containing a viewport.
84 NWID_BUTTON_DROPDOWN
, ///< Button with a drop-down.
85 NWID_HSCROLLBAR
, ///< Horizontal scrollbar
86 NWID_VSCROLLBAR
, ///< Vertical scrollbar
87 NWID_CUSTOM
, ///< General Custom widget.
89 /* Nested widget part types. */
90 WPT_RESIZE
, ///< Widget part for specifying resizing.
91 WPT_MINSIZE
, ///< Widget part for specifying minimal size.
92 WPT_MINTEXTLINES
, ///< Widget part for specifying minimal number of lines of text.
93 WPT_FILL
, ///< Widget part for specifying fill.
94 WPT_DATATIP
, ///< Widget part for specifying data and tooltip.
95 WPT_PADDING
, ///< Widget part for specifying a padding.
96 WPT_PIPSPACE
, ///< Widget part for specifying pre/inter/post space for containers.
97 WPT_PIPRATIO
, ///< Widget part for specifying pre/inter/post ratio for containers.
98 WPT_TEXTSTYLE
, ///< Widget part for specifying text colour.
99 WPT_ALIGNMENT
, ///< Widget part for specifying text/image alignment.
100 WPT_ENDCONTAINER
, ///< Widget part to denote end of a container.
101 WPT_FUNCTION
, ///< Widget part for calling a user function.
102 WPT_SCROLLBAR
, ///< Widget part for attaching a scrollbar.
104 /* Pushable window widget types. */
107 WWB_PUSHBUTTON
= 1 << 7,
109 WWT_PUSHBTN
= WWT_PANEL
| WWB_PUSHBUTTON
, ///< Normal push-button (no toggle button) with custom drawing
110 WWT_PUSHTXTBTN
= WWT_TEXTBTN
| WWB_PUSHBUTTON
, ///< Normal push-button (no toggle button) with text caption
111 WWT_PUSHIMGBTN
= WWT_IMGBTN
| WWB_PUSHBUTTON
, ///< Normal push-button (no toggle button) with image caption
112 WWT_PUSHARROWBTN
= WWT_ARROWBTN
| WWB_PUSHBUTTON
, ///< Normal push-button (no toggle button) with arrow caption
113 NWID_PUSHBUTTON_DROPDOWN
= NWID_BUTTON_DROPDOWN
| WWB_PUSHBUTTON
,
116 /** Different forms of sizing nested widgets, using NWidgetBase::AssignSizePosition() */
118 ST_SMALLEST
, ///< Initialize nested widget tree to smallest size. Also updates \e current_x and \e current_y.
119 ST_RESIZE
, ///< Resize the nested widget tree.
122 /* Forward declarations. */
126 /** Lookup between widget IDs and NWidget objects. */
127 using WidgetLookup
= std::map
<WidgetID
, class NWidgetBase
*>;
130 * Baseclass for nested widgets.
131 * @invariant After initialization, \f$current\_x = smallest\_x + n * resize\_x, for n \geq 0\f$.
132 * @invariant After initialization, \f$current\_y = smallest\_y + m * resize\_y, for m \geq 0\f$.
133 * @ingroup NestedWidgets
135 class NWidgetBase
: public ZeroedMemoryAllocator
{
137 NWidgetBase(WidgetType tp
);
139 virtual void AdjustPaddingForZoom();
140 virtual void SetupSmallestSize(Window
*w
) = 0;
141 virtual void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) = 0;
143 virtual void FillWidgetLookup(WidgetLookup
&widget_lookup
) = 0;
145 virtual NWidgetCore
*GetWidgetFromPos(int x
, int y
) = 0;
146 virtual NWidgetBase
*GetWidgetOfType(WidgetType tp
);
149 * Get parent widget of type NWID.
150 * @tparam NWID Type of the nested widget.
151 * @returns Parent widget, or nullptr if no widget of the specified type is found.
153 template <class NWID
>
154 NWID
*GetParentWidget()
156 for (NWidgetBase
*nwid_parent
= this->parent
; nwid_parent
!= nullptr; nwid_parent
= nwid_parent
->parent
) {
157 if (NWID
*nwid
= dynamic_cast<NWID
*>(nwid_parent
); nwid
!= nullptr) return nwid
;
163 * Get parent widget of type NWID.
164 * @tparam NWID Type of the nested widget.
165 * @returns Parent widget, or nullptr if no widget of the specified type is found.
167 template <class NWID
>
168 const NWID
*GetParentWidget() const
170 for (const NWidgetBase
*nwid_parent
= this->parent
; nwid_parent
!= nullptr; nwid_parent
= nwid_parent
->parent
) {
171 if (const NWID
*nwid
= dynamic_cast<const NWID
*>(nwid_parent
); nwid
!= nullptr) return nwid
;
176 virtual bool IsHighlighted() const { return false; }
177 virtual TextColour
GetHighlightColour() const { return TC_INVALID
; }
178 virtual void SetHighlighted([[maybe_unused
]] TextColour highlight_colour
) {}
181 * Set additional space (padding) around the widget.
182 * @param top Amount of additional space above the widget.
183 * @param right Amount of additional space right of the widget.
184 * @param bottom Amount of additional space below the widget.
185 * @param left Amount of additional space left of the widget.
187 inline void SetPadding(uint8_t top
, uint8_t right
, uint8_t bottom
, uint8_t left
)
189 this->uz_padding
.top
= top
;
190 this->uz_padding
.right
= right
;
191 this->uz_padding
.bottom
= bottom
;
192 this->uz_padding
.left
= left
;
193 this->AdjustPaddingForZoom();
197 * Set additional space (padding) around the widget.
198 * @param padding Amount of padding around the widget.
200 inline void SetPadding(const RectPadding
&padding
)
202 this->uz_padding
= padding
;
203 this->AdjustPaddingForZoom();
206 inline uint
GetHorizontalStepSize(SizingType sizing
) const;
207 inline uint
GetVerticalStepSize(SizingType sizing
) const;
209 virtual void Draw(const Window
*w
) = 0;
210 virtual void SetDirty(const Window
*w
) const;
212 Rect
GetCurrentRect() const
215 r
.left
= this->pos_x
;
217 r
.right
= this->pos_x
+ this->current_x
- 1;
218 r
.bottom
= this->pos_y
+ this->current_y
- 1;
222 WidgetType type
; ///< Type of the widget / nested widget.
223 uint fill_x
; ///< Horizontal fill stepsize (from initial size, \c 0 means not resizable).
224 uint fill_y
; ///< Vertical fill stepsize (from initial size, \c 0 means not resizable).
225 uint resize_x
; ///< Horizontal resize step (\c 0 means not resizable).
226 uint resize_y
; ///< Vertical resize step (\c 0 means not resizable).
227 /* Size of the widget in the smallest window possible.
228 * Computed by #SetupSmallestSize() followed by #AssignSizePosition().
230 uint smallest_x
; ///< Smallest horizontal size of the widget in a filled window.
231 uint smallest_y
; ///< Smallest vertical size of the widget in a filled window.
232 /* Current widget size (that is, after resizing). */
233 uint current_x
; ///< Current horizontal size (after resizing).
234 uint current_y
; ///< Current vertical size (after resizing).
236 int pos_x
; ///< Horizontal position of top-left corner of the widget in the window.
237 int pos_y
; ///< Vertical position of top-left corner of the widget in the window.
239 RectPadding padding
; ///< Padding added to the widget. Managed by parent container widget. (parent container may swap left and right for RTL)
240 RectPadding uz_padding
; ///< Unscaled padding, for resize calculation.
242 NWidgetBase
*parent
; ///< Parent widget of this widget, automatically filled in when added to container.
245 inline void StoreSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
);
249 * Get the horizontal sizing step.
250 * @param sizing Type of resize being performed.
252 inline uint
NWidgetBase::GetHorizontalStepSize(SizingType sizing
) const
254 return (sizing
== ST_RESIZE
) ? this->resize_x
: this->fill_x
;
258 * Get the vertical sizing step.
259 * @param sizing Type of resize being performed.
261 inline uint
NWidgetBase::GetVerticalStepSize(SizingType sizing
) const
263 return (sizing
== ST_RESIZE
) ? this->resize_y
: this->fill_y
;
267 * Store size and position.
268 * @param sizing Type of resizing to perform.
269 * @param x Horizontal offset of the widget relative to the left edge of the window.
270 * @param y Vertical offset of the widget relative to the top edge of the window.
271 * @param given_width Width allocated to the widget.
272 * @param given_height Height allocated to the widget.
274 inline void NWidgetBase::StoreSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
)
278 if (sizing
== ST_SMALLEST
) {
279 this->smallest_x
= given_width
;
280 this->smallest_y
= given_height
;
282 this->current_x
= given_width
;
283 this->current_y
= given_height
;
288 * Base class for a resizable nested widget.
289 * @ingroup NestedWidgets
291 class NWidgetResizeBase
: public NWidgetBase
{
293 NWidgetResizeBase(WidgetType tp
, uint fill_x
, uint fill_y
);
295 void AdjustPaddingForZoom() override
;
296 void SetMinimalSize(uint min_x
, uint min_y
);
297 void SetMinimalSizeAbsolute(uint min_x
, uint min_y
);
298 void SetMinimalTextLines(uint8_t min_lines
, uint8_t spacing
, FontSize size
);
299 void SetFill(uint fill_x
, uint fill_y
);
300 void SetResize(uint resize_x
, uint resize_y
);
302 bool UpdateMultilineWidgetSize(const std::string
&str
, int max_lines
);
303 bool UpdateSize(uint min_x
, uint min_y
);
304 bool UpdateVerticalSize(uint min_y
);
306 void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) override
;
308 uint min_x
; ///< Minimal horizontal size of only this widget.
309 uint min_y
; ///< Minimal vertical size of only this widget.
311 bool absolute
; ///< Set if minimum size is fixed and should not be resized.
312 uint uz_min_x
; ///< Unscaled Minimal horizontal size of only this widget.
313 uint uz_min_y
; ///< Unscaled Minimal vertical size of only this widget.
315 uint8_t uz_text_lines
; ///< 'Unscaled' text lines, stored for resize calculation.
316 uint8_t uz_text_spacing
; ///< 'Unscaled' text padding, stored for resize calculation.
317 FontSize uz_text_size
; ///< 'Unscaled' font size, stored for resize calculation.
320 /** Nested widget flags that affect display and interaction with 'real' widgets. */
321 enum NWidgetDisplay
{
323 NDB_LOWERED
= 0, ///< Widget is lowered (pressed down) bit.
324 NDB_DISABLED
= 1, ///< Widget is disabled (greyed out) bit.
325 /* Viewport widget. */
326 NDB_NO_TRANSPARENCY
= 2, ///< Viewport is never transparent.
327 NDB_SHADE_GREY
= 3, ///< Shade viewport to grey-scale.
328 NDB_SHADE_DIMMED
= 4, ///< Display dimmed colours in the viewport.
329 /* Button dropdown widget. */
330 NDB_DROPDOWN_ACTIVE
= 5, ///< Dropdown menu of the button dropdown widget is active. @see #NWID_BUTTON_DROPDOWN
331 /* Scrollbar widget. */
332 NDB_SCROLLBAR_UP
= 6, ///< Up-button is lowered bit.
333 NDB_SCROLLBAR_DOWN
= 7, ///< Down-button is lowered bit.
335 NDB_HIGHLIGHT
= 8, ///< Highlight of widget is on.
336 NDB_DROPDOWN_CLOSED
= 9, ///< Dropdown menu of the dropdown widget has closed.
338 ND_LOWERED
= 1 << NDB_LOWERED
, ///< Bit value of the lowered flag.
339 ND_DISABLED
= 1 << NDB_DISABLED
, ///< Bit value of the disabled flag.
340 ND_HIGHLIGHT
= 1 << NDB_HIGHLIGHT
, ///< Bit value of the highlight flag.
341 ND_NO_TRANSPARENCY
= 1 << NDB_NO_TRANSPARENCY
, ///< Bit value of the 'no transparency' flag.
342 ND_SHADE_GREY
= 1 << NDB_SHADE_GREY
, ///< Bit value of the 'shade to grey' flag.
343 ND_SHADE_DIMMED
= 1 << NDB_SHADE_DIMMED
, ///< Bit value of the 'dimmed colours' flag.
344 ND_DROPDOWN_ACTIVE
= 1 << NDB_DROPDOWN_ACTIVE
, ///< Bit value of the 'dropdown active' flag.
345 ND_SCROLLBAR_UP
= 1 << NDB_SCROLLBAR_UP
, ///< Bit value of the 'scrollbar up' flag.
346 ND_SCROLLBAR_DOWN
= 1 << NDB_SCROLLBAR_DOWN
, ///< Bit value of the 'scrollbar down' flag.
347 ND_SCROLLBAR_BTN
= ND_SCROLLBAR_UP
| ND_SCROLLBAR_DOWN
, ///< Bit value of the 'scrollbar up' or 'scrollbar down' flag.
348 ND_DROPDOWN_CLOSED
= 1 << NDB_DROPDOWN_CLOSED
, ///< Bit value of the 'dropdown closed' flag.
350 DECLARE_ENUM_AS_BIT_SET(NWidgetDisplay
)
353 * Base class for a 'real' widget.
354 * @ingroup NestedWidgets
356 class NWidgetCore
: public NWidgetResizeBase
{
358 NWidgetCore(WidgetType tp
, Colours colour
, WidgetID index
, uint fill_x
, uint fill_y
, uint32_t widget_data
, StringID tool_tip
);
360 void SetDataTip(uint32_t widget_data
, StringID tool_tip
);
361 void SetToolTip(StringID tool_tip
);
362 void SetTextStyle(TextColour colour
, FontSize size
);
363 void SetAlignment(StringAlignment align
);
365 inline void SetLowered(bool lowered
);
366 inline bool IsLowered() const;
367 inline void SetDisabled(bool disabled
);
368 inline bool IsDisabled() const;
370 void FillWidgetLookup(WidgetLookup
&widget_lookup
) override
;
371 NWidgetCore
*GetWidgetFromPos(int x
, int y
) override
;
372 bool IsHighlighted() const override
;
373 TextColour
GetHighlightColour() const override
;
374 void SetHighlighted(TextColour highlight_colour
) override
;
376 NWidgetDisplay disp_flags
; ///< Flags that affect display and interaction with the widget.
377 Colours colour
; ///< Colour of this widget.
378 const WidgetID index
; ///< Index of the nested widget (\c -1 means 'not used').
379 uint32_t widget_data
; ///< Data of the widget. @see Widget::data
380 StringID tool_tip
; ///< Tooltip of the widget. @see Widget::tootips
381 WidgetID scrollbar_index
; ///< Index of an attached scrollbar.
382 TextColour highlight_colour
; ///< Colour of highlight.
383 TextColour text_colour
; ///< Colour of text within widget.
384 FontSize text_size
; ///< Size of text within widget.
385 StringAlignment align
; ///< Alignment of text/image within widget.
389 * Highlight the widget or not.
390 * @param highlight_colour Widget must be highlighted (blink).
392 inline void NWidgetCore::SetHighlighted(TextColour highlight_colour
)
394 this->disp_flags
= highlight_colour
!= TC_INVALID
? SETBITS(this->disp_flags
, ND_HIGHLIGHT
) : CLRBITS(this->disp_flags
, ND_HIGHLIGHT
);
395 this->highlight_colour
= highlight_colour
;
398 /** Return whether the widget is highlighted. */
399 inline bool NWidgetCore::IsHighlighted() const
401 return HasBit(this->disp_flags
, NDB_HIGHLIGHT
);
404 /** Return the colour of the highlight. */
405 inline TextColour
NWidgetCore::GetHighlightColour() const
407 return this->highlight_colour
;
411 * Lower or raise the widget.
412 * @param lowered Widget must be lowered (drawn pressed down).
414 inline void NWidgetCore::SetLowered(bool lowered
)
416 this->disp_flags
= lowered
? SETBITS(this->disp_flags
, ND_LOWERED
) : CLRBITS(this->disp_flags
, ND_LOWERED
);
419 /** Return whether the widget is lowered. */
420 inline bool NWidgetCore::IsLowered() const
422 return HasBit(this->disp_flags
, NDB_LOWERED
);
426 * Disable (grey-out) or enable the widget.
427 * @param disabled Widget must be disabled.
429 inline void NWidgetCore::SetDisabled(bool disabled
)
431 this->disp_flags
= disabled
? SETBITS(this->disp_flags
, ND_DISABLED
) : CLRBITS(this->disp_flags
, ND_DISABLED
);
434 /** Return whether the widget is disabled. */
435 inline bool NWidgetCore::IsDisabled() const
437 return HasBit(this->disp_flags
, NDB_DISABLED
);
442 * Baseclass for container widgets.
443 * @ingroup NestedWidgets
445 class NWidgetContainer
: public NWidgetBase
{
447 NWidgetContainer(WidgetType tp
) : NWidgetBase(tp
) { }
449 void AdjustPaddingForZoom() override
;
450 void Add(std::unique_ptr
<NWidgetBase
> &&wid
);
451 void FillWidgetLookup(WidgetLookup
&widget_lookup
) override
;
453 void Draw(const Window
*w
) override
;
454 NWidgetCore
*GetWidgetFromPos(int x
, int y
) override
;
456 /** Return whether the container is empty. */
457 inline bool IsEmpty() { return this->children
.empty(); }
459 NWidgetBase
*GetWidgetOfType(WidgetType tp
) override
;
462 std::vector
<std::unique_ptr
<NWidgetBase
>> children
; ///< Child widgets in contaier.
465 /** Display planes with zero size for #NWidgetStacked. */
466 enum StackedZeroSizePlanes
{
467 SZSP_VERTICAL
= INT_MAX
/ 2, ///< Display plane with zero size horizontally, and filling and resizing vertically.
468 SZSP_HORIZONTAL
, ///< Display plane with zero size vertically, and filling and resizing horizontally.
469 SZSP_NONE
, ///< Display plane with zero size in both directions (none filling and resizing).
471 SZSP_BEGIN
= SZSP_VERTICAL
, ///< First zero-size plane.
475 * Stacked widgets, widgets all occupying the same space in the window.
476 * #NWID_SELECTION allows for selecting one of several panels (planes) to tbe displayed. All planes must have the same size.
477 * Since all planes are also initialized, switching between different planes can be done while the window is displayed.
479 * There are also a number of special planes (defined in #StackedZeroSizePlanes) that have zero size in one direction (and are stretchable in
480 * the other direction) or have zero size in both directions. They are used to make all child planes of the widget disappear.
481 * 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
482 * a #Window::ReInit() is needed to re-initialize the window since its size changes.
484 class NWidgetStacked
: public NWidgetContainer
{
486 NWidgetStacked(WidgetID index
);
488 void AdjustPaddingForZoom() override
;
489 void SetupSmallestSize(Window
*w
) override
;
490 void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) override
;
491 void FillWidgetLookup(WidgetLookup
&widget_lookup
) override
;
493 void Draw(const Window
*w
) override
;
494 NWidgetCore
*GetWidgetFromPos(int x
, int y
) override
;
496 bool SetDisplayedPlane(int plane
);
498 int shown_plane
; ///< Plane being displayed (for #NWID_SELECTION only).
499 const WidgetID index
; ///< If non-negative, index in the #Window::widget_lookup.
501 WidgetLookup
*widget_lookup
; ///< Window's widget lookup, updated in SetDisplayedPlane().
504 /** Nested widget container flags, */
505 enum NWidContainerFlags
{
506 NCB_EQUALSIZE
= 0, ///< Containers should keep all their (resizing) children equally large.
507 NCB_BIGFIRST
= 1, ///< Allocate space to biggest resize first.
509 NC_NONE
= 0, ///< All flags cleared.
510 NC_EQUALSIZE
= 1 << NCB_EQUALSIZE
, ///< Value of the #NCB_EQUALSIZE flag.
511 NC_BIGFIRST
= 1 << NCB_BIGFIRST
, ///< Value of the #NCB_BIGFIRST flag.
513 DECLARE_ENUM_AS_BIT_SET(NWidContainerFlags
)
515 /** Container with pre/inter/post child space. */
516 class NWidgetPIPContainer
: public NWidgetContainer
{
518 NWidgetPIPContainer(WidgetType tp
, NWidContainerFlags flags
= NC_NONE
);
520 void AdjustPaddingForZoom() override
;
521 void SetPIP(uint8_t pip_pre
, uint8_t pip_inter
, uint8_t pip_post
);
522 void SetPIPRatio(uint8_t pip_ratio_pre
, uint8_t pip_ratio_inter
, uint8_t pip_rato_post
);
525 NWidContainerFlags flags
; ///< Flags of the container.
526 uint8_t pip_pre
; ///< Amount of space before first widget.
527 uint8_t pip_inter
; ///< Amount of space between widgets.
528 uint8_t pip_post
; ///< Amount of space after last widget.
529 uint8_t pip_ratio_pre
; ///< Ratio of remaining space before first widget.
530 uint8_t pip_ratio_inter
; ///< Ratio of remaining space between widgets.
531 uint8_t pip_ratio_post
; ///< Ratio of remaining space after last widget.
533 uint8_t uz_pip_pre
; ///< Unscaled space before first widget.
534 uint8_t uz_pip_inter
; ///< Unscaled space between widgets.
535 uint8_t uz_pip_post
; ///< Unscaled space after last widget.
537 uint8_t gaps
; ///< Number of gaps between widgets.
541 * Horizontal container.
542 * @ingroup NestedWidgets
544 class NWidgetHorizontal
: public NWidgetPIPContainer
{
546 NWidgetHorizontal(NWidContainerFlags flags
= NC_NONE
);
548 void SetupSmallestSize(Window
*w
) override
;
549 void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) override
;
553 * Horizontal container that doesn't change the direction of the widgets for RTL languages.
554 * @ingroup NestedWidgets
556 class NWidgetHorizontalLTR
: public NWidgetHorizontal
{
558 NWidgetHorizontalLTR(NWidContainerFlags flags
= NC_NONE
);
560 void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) override
;
564 * Vertical container.
565 * @ingroup NestedWidgets
567 class NWidgetVertical
: public NWidgetPIPContainer
{
569 NWidgetVertical(NWidContainerFlags flags
= NC_NONE
);
571 void SetupSmallestSize(Window
*w
) override
;
572 void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) override
;
576 * Matrix container with implicitly equal sized (virtual) sub-widgets.
577 * This widget must have exactly one sub-widget. After that this sub-widget
578 * is used to draw all of the data within the matrix piece by piece.
579 * DrawWidget and OnClick calls will be done to that sub-widget, where the
580 * 16 high bits are used to encode the index into the matrix.
581 * @ingroup NestedWidgets
583 class NWidgetMatrix
: public NWidgetPIPContainer
{
585 NWidgetMatrix(Colours colour
, WidgetID index
);
587 void SetClicked(int clicked
);
588 void SetCount(int count
);
589 void SetScrollbar(Scrollbar
*sb
);
590 int GetCurrentElement() const;
592 void SetupSmallestSize(Window
*w
) override
;
593 void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) override
;
594 void FillWidgetLookup(WidgetLookup
&widget_lookup
) override
;
596 NWidgetCore
*GetWidgetFromPos(int x
, int y
) override
;
597 void Draw(const Window
*w
) override
;
599 const WidgetID index
; ///< If non-negative, index in the #Window::widget_lookup.
600 Colours colour
; ///< Colour of this widget.
601 int clicked
; ///< The currently clicked element.
602 int count
; ///< Amount of valid elements.
603 int current_element
; ///< The element currently being processed.
604 Scrollbar
*sb
; ///< The scrollbar we're associated with.
606 int widget_w
; ///< The width of the child widget including inter spacing.
607 int widget_h
; ///< The height of the child widget including inter spacing.
608 int widgets_x
; ///< The number of visible widgets in horizontal direction.
609 int widgets_y
; ///< The number of visible widgets in vertical direction.
611 void GetScrollOffsets(int &start_x
, int &start_y
, int &base_offs_x
, int &base_offs_y
);
617 * @ingroup NestedWidgets
619 class NWidgetSpacer
: public NWidgetResizeBase
{
621 NWidgetSpacer(int width
, int height
);
623 void SetupSmallestSize(Window
*w
) override
;
624 void FillWidgetLookup(WidgetLookup
&widget_lookup
) override
;
626 void Draw(const Window
*w
) override
;
627 void SetDirty(const Window
*w
) const override
;
628 NWidgetCore
*GetWidgetFromPos(int x
, int y
) override
;
632 * Nested widget with a child.
633 * @ingroup NestedWidgets
635 class NWidgetBackground
: public NWidgetCore
{
637 NWidgetBackground(WidgetType tp
, Colours colour
, WidgetID index
, std::unique_ptr
<NWidgetPIPContainer
> &&child
= nullptr);
639 void Add(std::unique_ptr
<NWidgetBase
> &&nwid
);
640 void SetPIP(uint8_t pip_pre
, uint8_t pip_inter
, uint8_t pip_post
);
641 void SetPIPRatio(uint8_t pip_ratio_pre
, uint8_t pip_ratio_inter
, uint8_t pip_ratio_post
);
643 void AdjustPaddingForZoom() override
;
644 void SetupSmallestSize(Window
*w
) override
;
645 void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) override
;
647 void FillWidgetLookup(WidgetLookup
&widget_lookup
) override
;
649 void Draw(const Window
*w
) override
;
650 NWidgetCore
*GetWidgetFromPos(int x
, int y
) override
;
651 NWidgetBase
*GetWidgetOfType(WidgetType tp
) override
;
654 std::unique_ptr
<NWidgetPIPContainer
> child
; ///< Child widget.
658 * Nested widget to display a viewport in a window.
659 * After initializing the nested widget tree, call #InitializeViewport(). After changing the window size,
660 * call #UpdateViewportCoordinates() eg from Window::OnResize().
661 * If the #disp_flags field contains the #ND_NO_TRANSPARENCY bit, the viewport will disable transparency.
662 * 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).
663 * @todo Class derives from #NWidgetCore, but does not use #colour, #widget_data, or #tool_tip.
664 * @ingroup NestedWidgets
666 class NWidgetViewport
: public NWidgetCore
{
668 NWidgetViewport(WidgetID index
);
670 void SetupSmallestSize(Window
*w
) override
;
671 void Draw(const Window
*w
) override
;
673 void InitializeViewport(Window
*w
, std::variant
<TileIndex
, VehicleID
> focus
, ZoomLevel zoom
);
674 void UpdateViewportCoordinates(Window
*w
);
678 * Scrollbar data structure
682 const bool is_vertical
; ///< Scrollbar has vertical orientation.
683 uint16_t count
; ///< Number of elements in the list.
684 uint16_t cap
; ///< Number of visible elements of the scroll bar.
685 uint16_t pos
; ///< Index of first visible item of the list.
686 uint16_t stepsize
; ///< Distance to scroll, when pressing the buttons or using the wheel.
689 /** Stepping sizes when scrolling */
690 enum ScrollbarStepping
{
691 SS_RAW
, ///< Step in single units.
692 SS_SMALL
, ///< Step in #stepsize units.
693 SS_BIG
, ///< Step in #cap units.
696 Scrollbar(bool is_vertical
) : is_vertical(is_vertical
), stepsize(1)
701 * Gets the number of elements in the list
702 * @return the number of elements
704 inline uint16_t GetCount() const
710 * Gets the number of visible elements of the scrollbar
711 * @return the number of visible elements
713 inline uint16_t GetCapacity() const
719 * Gets the position of the first visible element in the list
720 * @return the position of the element
722 inline uint16_t GetPosition() const
728 * Checks whether given current item is visible in the list
729 * @param item to check
730 * @return true iff the item is visible
732 inline bool IsVisible(uint16_t item
) const
734 return IsInsideBS(item
, this->GetPosition(), this->GetCapacity());
738 * Is the scrollbar vertical or not?
739 * @return True iff the scrollbar is vertical.
741 inline bool IsVertical() const
743 return this->is_vertical
;
747 * Set the distance to scroll when using the buttons or the wheel.
748 * @param stepsize Scrolling speed.
750 void SetStepSize(size_t stepsize
)
752 assert(stepsize
> 0);
754 this->stepsize
= ClampTo
<uint16_t>(stepsize
);
758 * Sets the number of elements in the list
759 * @param num the number of elements in the list
760 * @note updates the position if needed
762 void SetCount(size_t num
)
764 assert(num
<= MAX_UVALUE(uint16_t));
766 this->count
= ClampTo
<uint16_t>(num
);
767 /* Ensure position is within bounds */
768 this->SetPosition(this->pos
);
772 * Set the capacity of visible elements.
773 * @param capacity the new capacity
774 * @note updates the position if needed
776 void SetCapacity(size_t capacity
)
778 assert(capacity
<= MAX_UVALUE(uint16_t));
780 this->cap
= ClampTo
<uint16_t>(capacity
);
781 /* Ensure position is within bounds */
782 this->SetPosition(this->pos
);
785 void SetCapacityFromWidget(Window
*w
, WidgetID widget
, int padding
= 0);
788 * Sets the position of the first visible element
789 * @param position the position of the element
790 * @return true iff the position has changed
792 bool SetPosition(int position
)
794 uint16_t old_pos
= this->pos
;
795 this->pos
= Clamp(position
, 0, std::max(this->count
- this->cap
, 0));
796 return this->pos
!= old_pos
;
800 * Updates the position of the first visible element by the given amount.
801 * If the position would be too low or high it will be clamped appropriately
802 * @param difference the amount of change requested
803 * @param unit The stepping unit of \a difference
804 * @return true iff the position has changed
806 bool UpdatePosition(int difference
, ScrollbarStepping unit
= SS_SMALL
)
808 if (difference
== 0) return false;
810 case SS_SMALL
: difference
*= this->stepsize
; break;
811 case SS_BIG
: difference
*= this->cap
; break;
814 return this->SetPosition(this->pos
+ difference
);
818 * Scroll towards the given position; if the item is visible nothing
819 * happens, otherwise it will be shown either at the bottom or top of
820 * the window depending on where in the list it was.
821 * @param position the position to scroll towards.
823 void ScrollTowards(int position
)
825 if (position
< this->GetPosition()) {
826 /* scroll up to the item */
827 this->SetPosition(position
);
828 } else if (position
>= this->GetPosition() + this->GetCapacity()) {
829 /* scroll down so that the item is at the bottom */
830 this->SetPosition(position
- this->GetCapacity() + 1);
834 int GetScrolledRowFromWidget(int clickpos
, const Window
* const w
, WidgetID widget
, int padding
= 0, int line_height
= -1) const;
837 * Return an iterator pointing to the element of a scrolled widget that a user clicked in.
838 * @param container Container of elements represented by the scrollbar.
839 * @param clickpos Vertical position of the mouse click (without taking scrolling into account).
840 * @param w The window the click was in.
841 * @param widget Widget number of the widget clicked in.
842 * @param padding Amount of empty space between the widget edge and the top of the first row. Default value is \c 0.
843 * @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
844 * @return Iterator to the element clicked at. If clicked at a wrong position, returns as interator to the end of the container.
846 template <typename Tcontainer
>
847 typename
Tcontainer::iterator
GetScrolledItemFromWidget(Tcontainer
&container
, int clickpos
, const Window
* const w
, WidgetID widget
, int padding
= 0, int line_height
= -1) const
849 assert(this->GetCount() == container
.size()); // Scrollbar and container size must match.
850 int row
= this->GetScrolledRowFromWidget(clickpos
, w
, widget
, padding
, line_height
);
851 if (row
== INT_MAX
) return std::end(container
);
853 typename
Tcontainer::iterator it
= std::begin(container
);
854 std::advance(it
, row
);
858 EventState
UpdateListPositionOnKeyPress(int &list_position
, uint16_t keycode
) const;
862 * Nested widget to display and control a scrollbar in a window.
863 * Also assign the scrollbar to other widgets using #SetScrollbar() to make the mousewheel work.
864 * @ingroup NestedWidgets
866 class NWidgetScrollbar
: public NWidgetCore
, public Scrollbar
{
868 NWidgetScrollbar(WidgetType tp
, Colours colour
, WidgetID index
);
870 void SetupSmallestSize(Window
*w
) override
;
871 void Draw(const Window
*w
) override
;
873 static void InvalidateDimensionCache();
874 static Dimension
GetVerticalDimension();
875 static Dimension
GetHorizontalDimension();
878 static Dimension vertical_dimension
; ///< Cached size of vertical scrollbar button.
879 static Dimension horizontal_dimension
; ///< Cached size of horizontal scrollbar button.
884 * @ingroup NestedWidgets
886 class NWidgetLeaf
: public NWidgetCore
{
888 NWidgetLeaf(WidgetType tp
, Colours colour
, WidgetID index
, uint32_t data
, StringID tip
);
890 void SetupSmallestSize(Window
*w
) override
;
891 void Draw(const Window
*w
) override
;
893 bool ButtonHit(const Point
&pt
);
895 static void InvalidateDimensionCache();
897 static Dimension dropdown_dimension
; ///< Cached size of a dropdown widget.
898 static Dimension resizebox_dimension
; ///< Cached size of a resizebox widget.
899 static Dimension closebox_dimension
; ///< Cached size of a closebox widget.
901 static Dimension shadebox_dimension
; ///< Cached size of a shadebox widget.
902 static Dimension debugbox_dimension
; ///< Cached size of a debugbox widget.
903 static Dimension defsizebox_dimension
; ///< Cached size of a defsizebox widget.
904 static Dimension stickybox_dimension
; ///< Cached size of a stickybox widget.
908 * Return the biggest possible size of a nested widget.
909 * @param base Base size of the widget.
910 * @param max_space Available space for the widget.
911 * @param step Stepsize of the widget.
912 * @return Biggest possible size of the widget, assuming that \a base may only be incremented by \a step size steps.
914 inline uint
ComputeMaxSize(uint base
, uint max_space
, uint step
)
916 if (base
>= max_space
|| step
== 0) return base
;
917 if (step
== 1) return max_space
;
918 uint increment
= max_space
- base
;
919 increment
-= increment
% step
;
920 return base
+ increment
;
924 * @defgroup NestedWidgetParts Hierarchical widget parts
925 * 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.
927 * - Leaf widgets start with a #NWidget(WidgetType tp, Colours col, int16_t idx) part.
928 * Next, specify its properties with one or more of
929 * - #SetMinimalSize Define the minimal size of the widget.
930 * - #SetFill Define how the widget may grow to make it nicely.
931 * - #SetDataTip Define the data and the tooltip of the widget.
932 * - #SetResize Define how the widget may resize.
933 * - #SetPadding Create additional space around the widget.
935 * - To insert a nested widget tree from an external source, nested widget part #NWidgetFunction exists.
936 * For further customization, the #SetPadding part may be used.
938 * - Space widgets (#NWidgetSpacer) start with a #NWidget(WidgetType tp), followed by one or more of
939 * - #SetMinimalSize Define the minimal size of the widget.
940 * - #SetFill Define how the widget may grow to make it nicely.
941 * - #SetResize Define how the widget may resize.
942 * - #SetPadding Create additional space around the widget.
944 * - Container widgets #NWidgetHorizontal, #NWidgetHorizontalLTR, #NWidgetVertical, and #NWidgetMatrix, start with a #NWidget(WidgetType tp) part.
945 * Their properties are derived from the child widgets so they cannot be specified.
946 * You can however use
947 * - #SetPadding Define additional padding around the container.
948 * - #SetPIP Set additional pre/inter/post child widget space.
950 * 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
951 * the child widgets (it has no meaning for the compiler but it makes the widget parts easier to read).
952 * Below the last child widget, use an #EndContainer part. This part should be aligned with the #NWidget part that started the container.
954 * - 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,
955 * so the widget does not support #SetPIP. #SetPadding is allowed though.
956 * Like the other container widgets, below the last child widgets, a #EndContainer part should be used to denote the end of the stacked widget.
958 * - Background widgets #NWidgetBackground start with a #NWidget(WidgetType tp, Colours col, int16_t idx) part.
959 * What follows depends on how the widget is used.
960 * - 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
961 * leaf widgets to define how it behaves.
962 * - 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
965 * In both cases, the background widget \b MUST end with a #EndContainer widget part.
971 * Widget part for storing data and tooltip information.
972 * @ingroup NestedWidgetParts
974 struct NWidgetPartDataTip
{
975 uint32_t data
; ///< Data value of the widget.
976 StringID tooltip
; ///< Tooltip of the widget.
980 * Widget part for storing basic widget information.
981 * @ingroup NestedWidgetParts
983 struct NWidgetPartWidget
{
984 Colours colour
; ///< Widget colour.
985 WidgetID index
; ///< Index of the widget.
989 * Widget part for storing padding.
990 * @ingroup NestedWidgetParts
992 struct NWidgetPartPaddings
: RectPadding
{
996 * Widget part for storing pre/inter/post spaces.
997 * @ingroup NestedWidgetParts
999 struct NWidgetPartPIP
{
1000 uint8_t pre
, inter
, post
; ///< Amount of space before/between/after child widgets.
1004 * Widget part for storing minimal text line data.
1005 * @ingroup NestedWidgetParts
1007 struct NWidgetPartTextLines
{
1008 uint8_t lines
; ///< Number of text lines.
1009 uint8_t spacing
; ///< Extra spacing around lines.
1010 FontSize size
; ///< Font size of text lines.
1014 * Widget part for storing text colour.
1015 * @ingroup NestedWidgetParts
1017 struct NWidgetPartTextStyle
{
1018 TextColour colour
; ///< TextColour for DrawString.
1019 FontSize size
; ///< Font size of text.
1023 * Widget part for setting text/image alignment within a widget.
1024 * @ingroup NestedWidgetParts
1026 struct NWidgetPartAlignment
{
1027 StringAlignment align
; ///< Alignment of text/image.
1031 * Pointer to function returning a nested widget.
1032 * @return Nested widget (tree).
1034 typedef std::unique_ptr
<NWidgetBase
> NWidgetFunctionType();
1037 * Partial widget specification to allow NWidgets to be written nested.
1038 * @ingroup NestedWidgetParts
1040 struct NWidgetPart
{
1041 WidgetType type
; ///< Type of the part. @see NWidgetPartType.
1042 union NWidgetPartUnion
{
1043 Point xy
; ///< Part with an x/y size.
1044 NWidgetPartDataTip data_tip
; ///< Part with a data/tooltip.
1045 NWidgetPartWidget widget
; ///< Part with a start of a widget.
1046 NWidgetPartPaddings padding
; ///< Part with paddings.
1047 NWidgetPartPIP pip
; ///< Part with pre/inter/post spaces.
1048 NWidgetPartTextLines text_lines
; ///< Part with text line data.
1049 NWidgetPartTextStyle text_style
; ///< Part with text style data.
1050 NWidgetPartAlignment align
; ///< Part with internal alignment.
1051 NWidgetFunctionType
*func_ptr
; ///< Part with a function call.
1052 NWidContainerFlags cont_flags
; ///< Part with container flags.
1054 /* Constructors for each NWidgetPartUnion data type. */
1055 constexpr NWidgetPartUnion() : xy() {}
1056 constexpr NWidgetPartUnion(Point xy
) : xy(xy
) {}
1057 constexpr NWidgetPartUnion(NWidgetPartDataTip data_tip
) : data_tip(data_tip
) {}
1058 constexpr NWidgetPartUnion(NWidgetPartWidget widget
) : widget(widget
) {}
1059 constexpr NWidgetPartUnion(NWidgetPartPaddings padding
) : padding(padding
) {}
1060 constexpr NWidgetPartUnion(NWidgetPartPIP pip
) : pip(pip
) {}
1061 constexpr NWidgetPartUnion(NWidgetPartTextLines text_lines
) : text_lines(text_lines
) {}
1062 constexpr NWidgetPartUnion(NWidgetPartTextStyle text_style
) : text_style(text_style
) {}
1063 constexpr NWidgetPartUnion(NWidgetPartAlignment align
) : align(align
) {}
1064 constexpr NWidgetPartUnion(NWidgetFunctionType
*func_ptr
) : func_ptr(func_ptr
) {}
1065 constexpr NWidgetPartUnion(NWidContainerFlags cont_flags
) : cont_flags(cont_flags
) {}
1068 /* Constructors for each NWidgetPart data type. */
1069 explicit constexpr NWidgetPart(WidgetType type
) : type(type
), u() {}
1070 constexpr NWidgetPart(WidgetType type
, Point xy
) : type(type
), u(xy
) {}
1071 constexpr NWidgetPart(WidgetType type
, NWidgetPartDataTip data_tip
) : type(type
), u(data_tip
) {}
1072 constexpr NWidgetPart(WidgetType type
, NWidgetPartWidget widget
) : type(type
), u(widget
) {}
1073 constexpr NWidgetPart(WidgetType type
, NWidgetPartPaddings padding
) : type(type
), u(padding
) {}
1074 constexpr NWidgetPart(WidgetType type
, NWidgetPartPIP pip
) : type(type
), u(pip
) {}
1075 constexpr NWidgetPart(WidgetType type
, NWidgetPartTextLines text_lines
) : type(type
), u(text_lines
) {}
1076 constexpr NWidgetPart(WidgetType type
, NWidgetPartTextStyle text_style
) : type(type
), u(text_style
) {}
1077 constexpr NWidgetPart(WidgetType type
, NWidgetPartAlignment align
) : type(type
), u(align
) {}
1078 constexpr NWidgetPart(WidgetType type
, NWidgetFunctionType
*func_ptr
) : type(type
), u(func_ptr
) {}
1079 constexpr NWidgetPart(WidgetType type
, NWidContainerFlags cont_flags
) : type(type
), u(cont_flags
) {}
1083 * Widget part function for setting the resize step.
1084 * @param dx Horizontal resize step. 0 means no horizontal resizing.
1085 * @param dy Vertical resize step. 0 means no vertical resizing.
1086 * @ingroup NestedWidgetParts
1088 constexpr NWidgetPart
SetResize(int16_t dx
, int16_t dy
)
1090 return NWidgetPart
{WPT_RESIZE
, Point
{dx
, dy
}};
1094 * Widget part function for setting the minimal size.
1095 * @param x Horizontal minimal size.
1096 * @param y Vertical minimal size.
1097 * @ingroup NestedWidgetParts
1099 constexpr NWidgetPart
SetMinimalSize(int16_t x
, int16_t y
)
1101 return NWidgetPart
{WPT_MINSIZE
, Point
{x
, y
}};
1105 * Widget part function for setting the minimal text lines.
1106 * @param lines Number of text lines.
1107 * @param spacing Extra spacing required.
1108 * @param size Font size of text.
1109 * @ingroup NestedWidgetParts
1111 constexpr NWidgetPart
SetMinimalTextLines(uint8_t lines
, uint8_t spacing
, FontSize size
= FS_NORMAL
)
1113 return NWidgetPart
{WPT_MINTEXTLINES
, NWidgetPartTextLines
{lines
, spacing
, size
}};
1117 * Widget part function for setting the text style.
1118 * @param colour Colour to draw string within widget.
1119 * @param size Font size to draw string within widget.
1120 * @ingroup NestedWidgetParts
1122 constexpr NWidgetPart
SetTextStyle(TextColour colour
, FontSize size
= FS_NORMAL
)
1124 return NWidgetPart
{WPT_TEXTSTYLE
, NWidgetPartTextStyle
{colour
, size
}};
1128 * Widget part function for setting the alignment of text/images.
1129 * @param align Alignment of text/image within widget.
1130 * @ingroup NestedWidgetParts
1132 constexpr NWidgetPart
SetAlignment(StringAlignment align
)
1134 return NWidgetPart
{WPT_ALIGNMENT
, NWidgetPartAlignment
{align
}};
1138 * Widget part function for setting filling.
1139 * @param fill_x Horizontal filling step from minimal size.
1140 * @param fill_y Vertical filling step from minimal size.
1141 * @ingroup NestedWidgetParts
1143 constexpr NWidgetPart
SetFill(uint16_t fill_x
, uint16_t fill_y
)
1145 return NWidgetPart
{WPT_FILL
, Point
{fill_x
, fill_y
}};
1149 * Widget part function for denoting the end of a container
1150 * (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
1151 * @ingroup NestedWidgetParts
1153 constexpr NWidgetPart
EndContainer()
1155 return NWidgetPart
{WPT_ENDCONTAINER
};
1159 * Widget part function for setting the data and tooltip.
1160 * @param data Data of the widget.
1161 * @param tip Tooltip of the widget.
1162 * @ingroup NestedWidgetParts
1164 constexpr NWidgetPart
SetDataTip(uint32_t data
, StringID tip
)
1166 return NWidgetPart
{WPT_DATATIP
, NWidgetPartDataTip
{data
, tip
}};
1170 * Widget part function for setting the data and tooltip of WWT_MATRIX widgets
1171 * @param cols Number of columns. \c 0 means to use draw columns with width according to the resize step size.
1172 * @param rows Number of rows. \c 0 means to use draw rows with height according to the resize step size.
1173 * @param tip Tooltip of the widget.
1174 * @ingroup NestedWidgetParts
1176 constexpr NWidgetPart
SetMatrixDataTip(uint8_t cols
, uint8_t rows
, StringID tip
)
1178 return SetDataTip((rows
<< MAT_ROW_START
) | (cols
<< MAT_COL_START
), tip
);
1182 * Widget part function for setting additional space around a widget.
1183 * Parameters start above the widget, and are specified in clock-wise direction.
1184 * @param top The padding above the widget.
1185 * @param right The padding right of the widget.
1186 * @param bottom The padding below the widget.
1187 * @param left The padding left of the widget.
1188 * @ingroup NestedWidgetParts
1190 constexpr NWidgetPart
SetPadding(uint8_t top
, uint8_t right
, uint8_t bottom
, uint8_t left
)
1192 return NWidgetPart
{WPT_PADDING
, NWidgetPartPaddings
{left
, top
, right
, bottom
}};
1196 * Widget part function for setting additional space around a widget.
1197 * @param r The padding around the widget.
1198 * @ingroup NestedWidgetParts
1200 constexpr NWidgetPart
SetPadding(const RectPadding
&padding
)
1202 return NWidgetPart
{WPT_PADDING
, NWidgetPartPaddings
{padding
}};
1206 * Widget part function for setting a padding.
1207 * @param padding The padding to use for all directions.
1208 * @ingroup NestedWidgetParts
1210 constexpr NWidgetPart
SetPadding(uint8_t padding
)
1212 return SetPadding(padding
, padding
, padding
, padding
);
1216 * Widget part function for setting a pre/inter/post spaces.
1217 * @param pre The amount of space before the first widget.
1218 * @param inter The amount of space between widgets.
1219 * @param post The amount of space after the last widget.
1220 * @ingroup NestedWidgetParts
1222 constexpr NWidgetPart
SetPIP(uint8_t pre
, uint8_t inter
, uint8_t post
)
1224 return NWidgetPart
{WPT_PIPSPACE
, NWidgetPartPIP
{pre
, inter
, post
}};
1228 * Widget part function for setting a pre/inter/post ratio.
1229 * @param pre The ratio of space before the first widget.
1230 * @param inter The ratio of space between widgets.
1231 * @param post The ratio of space after the last widget.
1232 * @ingroup NestedWidgetParts
1234 constexpr NWidgetPart
SetPIPRatio(uint8_t ratio_pre
, uint8_t ratio_inter
, uint8_t ratio_post
)
1236 return NWidgetPart
{WPT_PIPRATIO
, NWidgetPartPIP
{ratio_pre
, ratio_inter
, ratio_post
}};
1240 * Attach a scrollbar to a widget.
1241 * The scrollbar is controlled when using the mousewheel on the widget.
1242 * Multiple widgets can refer to the same scrollbar to make the mousewheel work in all of them.
1243 * @param index Widget index of the scrollbar.
1244 * @ingroup NestedWidgetParts
1246 constexpr NWidgetPart
SetScrollbar(WidgetID index
)
1248 return NWidgetPart
{WPT_SCROLLBAR
, NWidgetPartWidget
{INVALID_COLOUR
, index
}};
1252 * Widget part function for starting a new 'real' widget.
1253 * @param tp Type of the new nested widget.
1254 * @param col Colour of the new widget.
1255 * @param idx Index of the widget.
1256 * @note with #WWT_PANEL, #WWT_FRAME, #WWT_INSET, a new container is started.
1257 * Child widgets must have a index bigger than the parent index.
1258 * @ingroup NestedWidgetParts
1260 constexpr NWidgetPart
NWidget(WidgetType tp
, Colours col
, WidgetID idx
= -1)
1262 return NWidgetPart
{tp
, NWidgetPartWidget
{col
, idx
}};
1266 * Widget part function for starting a new horizontal container, vertical container, or spacer widget.
1267 * @param tp Type of the new nested widget, #NWID_HORIZONTAL, #NWID_VERTICAL, #NWID_SPACER, #NWID_SELECTION, and #NWID_MATRIX.
1268 * @param cont_flags Flags for the containers (#NWID_HORIZONTAL and #NWID_VERTICAL).
1269 * @ingroup NestedWidgetParts
1271 constexpr NWidgetPart
NWidget(WidgetType tp
, NWidContainerFlags cont_flags
= NC_NONE
)
1273 return NWidgetPart
{tp
, NWidContainerFlags
{cont_flags
}};
1277 * Obtain a nested widget (sub)tree from an external source.
1278 * @param func_ptr Pointer to function that returns the tree.
1279 * @ingroup NestedWidgetParts
1281 constexpr NWidgetPart
NWidgetFunction(NWidgetFunctionType
*func_ptr
)
1283 return NWidgetPart
{WPT_FUNCTION
, func_ptr
};
1286 bool IsContainerWidgetType(WidgetType tp
);
1287 std::unique_ptr
<NWidgetBase
> MakeNWidgets(const NWidgetPart
*nwid_begin
, const NWidgetPart
*nwid_end
, std::unique_ptr
<NWidgetBase
> &&container
);
1288 std::unique_ptr
<NWidgetBase
> MakeWindowNWidgetTree(const NWidgetPart
*nwid_begin
, const NWidgetPart
*nwid_end
, NWidgetStacked
**shade_select
);
1290 std::unique_ptr
<NWidgetBase
> MakeCompanyButtonRows(WidgetID widget_first
, WidgetID widget_last
, Colours button_colour
, int max_length
, StringID button_tooltip
, bool resizable
= true);
1292 void SetupWidgetDimensions();
1294 #endif /* WIDGET_TYPE_H */