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.cpp Handling of the default/simple widgets. */
11 #include "core/backup_type.hpp"
12 #include "company_func.h"
13 #include "window_gui.h"
14 #include "viewport_func.h"
15 #include "zoom_func.h"
16 #include "strings_func.h"
17 #include "transparency.h"
18 #include "core/geometry_func.hpp"
19 #include "settings_type.h"
20 #include "querystring_gui.h"
22 #include "table/sprites.h"
23 #include "table/strings.h"
24 #include "table/string_colours.h"
26 #include "safeguards.h"
28 WidgetDimensions
WidgetDimensions::scaled
= {};
31 * Scale a RectPadding to GUI zoom level.
32 * @param r RectPadding at ZOOM_BASE (traditional "normal" interface size).
33 * @return RectPadding at #ZOOM_LVL_GUI (current interface size).
35 static inline RectPadding
ScaleGUITrad(const RectPadding
&r
)
37 return {(uint8_t)ScaleGUITrad(r
.left
), (uint8_t)ScaleGUITrad(r
.top
), (uint8_t)ScaleGUITrad(r
.right
), (uint8_t)ScaleGUITrad(r
.bottom
)};
41 * Scale a Dimension to GUI zoom level.
42 * @param d Dimension at ZOOM_BASE (traditional "normal" interface size).
43 * @return Dimension at #ZOOM_LVL_GUI (current interface size).
45 static inline Dimension
ScaleGUITrad(const Dimension
&dim
)
47 return {(uint
)ScaleGUITrad(dim
.width
), (uint
)ScaleGUITrad(dim
.height
)};
51 * Scale sprite size for GUI.
54 Dimension
GetScaledSpriteSize(SpriteID sprid
)
57 Dimension d
= GetSpriteSize(sprid
, &offset
, ZOOM_LVL_NORMAL
);
60 return ScaleGUITrad(d
);
64 * Set up pre-scaled versions of Widget Dimensions.
66 void SetupWidgetDimensions()
68 WidgetDimensions::scaled
.imgbtn
= ScaleGUITrad(WidgetDimensions::unscaled
.imgbtn
);
69 WidgetDimensions::scaled
.inset
= ScaleGUITrad(WidgetDimensions::unscaled
.inset
);
70 WidgetDimensions::scaled
.vscrollbar
= ScaleGUITrad(WidgetDimensions::unscaled
.vscrollbar
);
71 WidgetDimensions::scaled
.hscrollbar
= ScaleGUITrad(WidgetDimensions::unscaled
.hscrollbar
);
72 if (_settings_client
.gui
.scale_bevels
) {
73 WidgetDimensions::scaled
.bevel
= ScaleGUITrad(WidgetDimensions::unscaled
.bevel
);
75 WidgetDimensions::scaled
.bevel
= WidgetDimensions::unscaled
.bevel
;
77 WidgetDimensions::scaled
.fullbevel
= ScaleGUITrad(WidgetDimensions::unscaled
.fullbevel
);
78 WidgetDimensions::scaled
.framerect
= ScaleGUITrad(WidgetDimensions::unscaled
.framerect
);
79 WidgetDimensions::scaled
.frametext
= ScaleGUITrad(WidgetDimensions::unscaled
.frametext
);
80 WidgetDimensions::scaled
.matrix
= ScaleGUITrad(WidgetDimensions::unscaled
.matrix
);
81 WidgetDimensions::scaled
.shadebox
= ScaleGUITrad(WidgetDimensions::unscaled
.shadebox
);
82 WidgetDimensions::scaled
.stickybox
= ScaleGUITrad(WidgetDimensions::unscaled
.stickybox
);
83 WidgetDimensions::scaled
.debugbox
= ScaleGUITrad(WidgetDimensions::unscaled
.debugbox
);
84 WidgetDimensions::scaled
.defsizebox
= ScaleGUITrad(WidgetDimensions::unscaled
.defsizebox
);
85 WidgetDimensions::scaled
.resizebox
= ScaleGUITrad(WidgetDimensions::unscaled
.resizebox
);
86 WidgetDimensions::scaled
.closebox
= ScaleGUITrad(WidgetDimensions::unscaled
.closebox
);
87 WidgetDimensions::scaled
.captiontext
= ScaleGUITrad(WidgetDimensions::unscaled
.captiontext
);
88 WidgetDimensions::scaled
.dropdowntext
= ScaleGUITrad(WidgetDimensions::unscaled
.dropdowntext
);
89 WidgetDimensions::scaled
.dropdownlist
= ScaleGUITrad(WidgetDimensions::unscaled
.dropdownlist
);
90 WidgetDimensions::scaled
.modalpopup
= ScaleGUITrad(WidgetDimensions::unscaled
.modalpopup
);
92 WidgetDimensions::scaled
.vsep_normal
= ScaleGUITrad(WidgetDimensions::unscaled
.vsep_normal
);
93 WidgetDimensions::scaled
.vsep_wide
= ScaleGUITrad(WidgetDimensions::unscaled
.vsep_wide
);
94 WidgetDimensions::scaled
.hsep_normal
= ScaleGUITrad(WidgetDimensions::unscaled
.hsep_normal
);
95 WidgetDimensions::scaled
.hsep_wide
= ScaleGUITrad(WidgetDimensions::unscaled
.hsep_wide
);
96 WidgetDimensions::scaled
.hsep_indent
= ScaleGUITrad(WidgetDimensions::unscaled
.hsep_indent
);
100 * Calculate x and y coordinates for an aligned object within a window.
101 * @param r Rectangle of the widget to be drawn in.
102 * @param d Dimension of the object to be drawn.
103 * @param align Alignment of the object.
104 * @return A point containing the position at which to draw.
106 static inline Point
GetAlignedPosition(const Rect
&r
, const Dimension
&d
, StringAlignment align
)
109 /* In case we have a RTL language we swap the alignment. */
110 if (!(align
& SA_FORCE
) && _current_text_dir
== TD_RTL
&& (align
& SA_HOR_MASK
) != SA_HOR_CENTER
) align
^= SA_RIGHT
;
111 switch (align
& SA_HOR_MASK
) {
112 case SA_LEFT
: p
.x
= r
.left
; break;
113 case SA_HOR_CENTER
: p
.x
= CenterBounds(r
.left
, r
.right
, d
.width
); break;
114 case SA_RIGHT
: p
.x
= r
.right
+ 1 - d
.width
; break;
115 default: NOT_REACHED();
117 switch (align
& SA_VERT_MASK
) {
118 case SA_TOP
: p
.y
= r
.top
; break;
119 case SA_VERT_CENTER
: p
.y
= CenterBounds(r
.top
, r
.bottom
, d
.height
); break;
120 case SA_BOTTOM
: p
.y
= r
.bottom
+ 1 - d
.height
; break;
121 default: NOT_REACHED();
127 * Compute the vertical position of the draggable part of scrollbar
128 * @param sb Scrollbar list data
129 * @param top Top position of the scrollbar (top position of the up-button)
130 * @param bottom Bottom position of the scrollbar (bottom position of the down-button)
131 * @param horizontal Whether the scrollbar is horizontal or not
132 * @return A Point, with x containing the top coordinate of the draggable part, and
133 * y containing the bottom coordinate of the draggable part
135 static Point
HandleScrollbarHittest(const Scrollbar
*sb
, int top
, int bottom
, bool horizontal
)
137 /* Base for reversion */
138 int rev_base
= top
+ bottom
;
141 button_size
= NWidgetScrollbar::GetHorizontalDimension().width
;
143 button_size
= NWidgetScrollbar::GetVerticalDimension().height
;
145 top
+= button_size
; // top points to just below the up-button
146 bottom
-= button_size
; // bottom points to top of the down-button
148 int count
= sb
->GetCount();
149 int cap
= sb
->GetCapacity();
152 int height
= (bottom
- top
);
153 int slider_height
= std::max(button_size
, cap
* height
/ count
);
154 height
-= slider_height
;
156 top
+= height
* sb
->GetPosition() / (count
- cap
);
157 bottom
= top
+ slider_height
;
161 if (horizontal
&& _current_text_dir
== TD_RTL
) {
162 pt
.x
= rev_base
- bottom
;
163 pt
.y
= rev_base
- top
;
172 * Compute new position of the scrollbar after a click and updates the window flags.
173 * @param w Window on which a scroll was performed.
174 * @param sb Scrollbar
175 * @param mi Minimum coordinate of the scroll bar.
176 * @param ma Maximum coordinate of the scroll bar.
177 * @param x The X coordinate of the mouse click.
178 * @param y The Y coordinate of the mouse click.
180 static void ScrollbarClickPositioning(Window
*w
, NWidgetScrollbar
*sb
, int x
, int y
, int mi
, int ma
)
185 bool changed
= false;
187 if (sb
->type
== NWID_HSCROLLBAR
) {
189 rtl
= _current_text_dir
== TD_RTL
;
190 button_size
= NWidgetScrollbar::GetHorizontalDimension().width
;
193 button_size
= NWidgetScrollbar::GetVerticalDimension().height
;
195 if (pos
< mi
+ button_size
) {
196 /* Pressing the upper button? */
197 SetBit(sb
->disp_flags
, NDB_SCROLLBAR_UP
);
198 if (_scroller_click_timeout
<= 1) {
199 _scroller_click_timeout
= 3;
200 changed
= sb
->UpdatePosition(rtl
? 1 : -1);
202 w
->mouse_capture_widget
= sb
->index
;
203 } else if (pos
>= ma
- button_size
) {
204 /* Pressing the lower button? */
205 SetBit(sb
->disp_flags
, NDB_SCROLLBAR_DOWN
);
207 if (_scroller_click_timeout
<= 1) {
208 _scroller_click_timeout
= 3;
209 changed
= sb
->UpdatePosition(rtl
? -1 : 1);
211 w
->mouse_capture_widget
= sb
->index
;
213 Point pt
= HandleScrollbarHittest(sb
, mi
, ma
, sb
->type
== NWID_HSCROLLBAR
);
216 changed
= sb
->UpdatePosition(rtl
? 1 : -1, Scrollbar::SS_BIG
);
217 } else if (pos
> pt
.y
) {
218 changed
= sb
->UpdatePosition(rtl
? -1 : 1, Scrollbar::SS_BIG
);
220 _scrollbar_start_pos
= pt
.x
- mi
- button_size
;
221 _scrollbar_size
= ma
- mi
- button_size
* 2 - (pt
.y
- pt
.x
);
222 w
->mouse_capture_widget
= sb
->index
;
223 _cursorpos_drag_start
= _cursor
.pos
;
228 /* Position changed so refresh the window */
231 /* No change so only refresh this scrollbar */
237 * Special handling for the scrollbar widget type.
238 * Handles the special scrolling buttons and other scrolling.
239 * @param w Window on which a scroll was performed.
240 * @param nw Pointer to the scrollbar widget.
241 * @param x The X coordinate of the mouse click.
242 * @param y The Y coordinate of the mouse click.
244 void ScrollbarClickHandler(Window
*w
, NWidgetCore
*nw
, int x
, int y
)
248 if (nw
->type
== NWID_HSCROLLBAR
) {
250 ma
= nw
->pos_x
+ nw
->current_x
;
253 ma
= nw
->pos_y
+ nw
->current_y
;
255 NWidgetScrollbar
*scrollbar
= dynamic_cast<NWidgetScrollbar
*>(nw
);
256 assert(scrollbar
!= nullptr);
257 ScrollbarClickPositioning(w
, scrollbar
, x
, y
, mi
, ma
);
261 * Returns the index for the widget located at the given position
262 * relative to the window. It includes all widget-corner pixels as well.
263 * @param *w Window to look inside
264 * @param x The Window client X coordinate
265 * @param y The Window client y coordinate
266 * @return A widget index, or -1 if no widget was found.
268 WidgetID
GetWidgetFromPos(const Window
*w
, int x
, int y
)
270 NWidgetCore
*nw
= w
->nested_root
->GetWidgetFromPos(x
, y
);
271 return (nw
!= nullptr) ? nw
->index
: -1;
275 * Draw frame rectangle.
276 * @param left Left edge of the frame
277 * @param top Top edge of the frame
278 * @param right Right edge of the frame
279 * @param bottom Bottom edge of the frame
280 * @param colour Colour table to use. @see Colours
281 * @param flags Flags controlling how to draw the frame. @see FrameFlags
283 void DrawFrameRect(int left
, int top
, int right
, int bottom
, Colours colour
, FrameFlags flags
)
285 assert(colour
< COLOUR_END
);
287 uint dark
= GetColourGradient(colour
, SHADE_DARK
);
288 uint medium_dark
= GetColourGradient(colour
, SHADE_LIGHT
);
289 uint medium_light
= GetColourGradient(colour
, SHADE_LIGHTER
);
290 uint light
= GetColourGradient(colour
, SHADE_LIGHTEST
);
292 if (flags
& FR_TRANSPARENT
) {
293 GfxFillRect(left
, top
, right
, bottom
, PALETTE_TO_TRANSPARENT
, FILLRECT_RECOLOUR
);
297 Rect outer
= {left
, top
, right
, bottom
}; // Outside rectangle
298 Rect inner
= outer
.Shrink(WidgetDimensions::scaled
.bevel
); // Inside rectangle
300 if (flags
& FR_LOWERED
) {
301 GfxFillRect(outer
.left
, outer
.top
, inner
.left
- 1, outer
.bottom
, dark
); // Left
302 GfxFillRect(inner
.left
, outer
.top
, outer
.right
, inner
.top
- 1, dark
); // Top
303 GfxFillRect(inner
.right
+ 1, inner
.top
, outer
.right
, inner
.bottom
, light
); // Right
304 GfxFillRect(inner
.left
, inner
.bottom
+ 1, outer
.right
, outer
.bottom
, light
); // Bottom
305 interior
= (flags
& FR_DARKENED
? medium_dark
: medium_light
);
307 GfxFillRect(outer
.left
, outer
.top
, inner
.left
- 1, inner
.bottom
, light
); // Left
308 GfxFillRect(inner
.left
, outer
.top
, inner
.right
, inner
.top
- 1, light
); // Top
309 GfxFillRect(inner
.right
+ 1, outer
.top
, outer
.right
, inner
.bottom
, dark
); // Right
310 GfxFillRect(outer
.left
, inner
.bottom
+ 1, outer
.right
, outer
.bottom
, dark
); // Bottom
311 interior
= medium_dark
;
313 if (!(flags
& FR_BORDERONLY
)) {
314 GfxFillRect(inner
.left
, inner
.top
, inner
.right
, inner
.bottom
, interior
); // Inner
319 void DrawSpriteIgnorePadding(SpriteID img
, PaletteID pal
, const Rect
&r
, StringAlignment align
)
322 Dimension d
= GetSpriteSize(img
, &offset
);
324 d
.height
-= offset
.y
;
326 Point p
= GetAlignedPosition(r
, d
, align
);
327 DrawSprite(img
, pal
, p
.x
- offset
.x
, p
.y
- offset
.y
);
331 * Draw an image button.
332 * @param r Rectangle of the button.
333 * @param type Widget type (#WWT_IMGBTN or #WWT_IMGBTN_2).
334 * @param colour Colour of the button.
335 * @param clicked Button is clicked.
336 * @param img Sprite to draw.
337 * @param align Alignment of the sprite.
339 static inline void DrawImageButtons(const Rect
&r
, WidgetType type
, Colours colour
, bool clicked
, SpriteID img
, StringAlignment align
)
342 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
344 if ((type
& WWT_MASK
) == WWT_IMGBTN_2
&& clicked
) img
++; // Show different image when clicked for #WWT_IMGBTN_2.
345 DrawSpriteIgnorePadding(img
, PAL_NONE
, r
, align
);
349 * Draw the label-part of a widget.
350 * @param r Rectangle of the label background.
351 * @param type Widget type (#WWT_TEXTBTN, #WWT_TEXTBTN_2, or #WWT_LABEL).
352 * @param clicked Label is clicked.
353 * @param colour Colour of the text.
354 * @param str Text to draw.
355 * @param align Alignment of the text.
356 * @param fs Font size of the text.
358 static inline void DrawLabel(const Rect
&r
, WidgetType type
, bool clicked
, TextColour colour
, StringID str
, StringAlignment align
, FontSize fs
)
360 if (str
== STR_NULL
) return;
361 if ((type
& WWT_MASK
) == WWT_TEXTBTN_2
&& clicked
) str
++;
362 Dimension d
= GetStringBoundingBox(str
, fs
);
363 Point p
= GetAlignedPosition(r
, d
, align
);
364 DrawString(r
.left
, r
.right
, p
.y
, str
, colour
, align
, false, fs
);
369 * @param r Rectangle of the background.
370 * @param colour Colour of the text.
371 * @param str Text to draw.
372 * @param align Alignment of the text.
373 * @param fs Font size of the text.
375 static inline void DrawText(const Rect
&r
, TextColour colour
, StringID str
, StringAlignment align
, FontSize fs
)
377 Dimension d
= GetStringBoundingBox(str
, fs
);
378 Point p
= GetAlignedPosition(r
, d
, align
);
379 if (str
!= STR_NULL
) DrawString(r
.left
, r
.right
, p
.y
, str
, colour
, align
, false, fs
);
383 * Draw an inset widget.
384 * @param r Rectangle of the background.
385 * @param colour Colour of the inset.
386 * @param text_colour Colour of the text.
387 * @param str Text to draw.
388 * @param align Alignment of the text.
389 * @param fs Font size of the text.
391 static inline void DrawInset(const Rect
&r
, Colours colour
, TextColour text_colour
, StringID str
, StringAlignment align
, FontSize fs
)
393 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, FR_LOWERED
| FR_DARKENED
);
394 if (str
!= STR_NULL
) DrawString(r
.Shrink(WidgetDimensions::scaled
.inset
), str
, text_colour
, align
, false, fs
);
398 * Draw a matrix widget.
399 * @param r Rectangle of the matrix background.
400 * @param colour Colour of the background.
401 * @param clicked Matrix is rendered lowered.
402 * @param data Data of the widget, number of rows and columns of the widget.
403 * @param resize_x Matrix resize unit size.
404 * @param resize_y Matrix resize unit size.
406 static inline void DrawMatrix(const Rect
&r
, Colours colour
, bool clicked
, uint16_t data
, uint resize_x
, uint resize_y
)
408 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
410 int num_columns
= GB(data
, MAT_COL_START
, MAT_COL_BITS
); // Lower 8 bits of the widget data: Number of columns in the matrix.
411 int column_width
; // Width of a single column in the matrix.
412 if (num_columns
== 0) {
413 column_width
= resize_x
;
414 num_columns
= r
.Width() / column_width
;
416 column_width
= r
.Width() / num_columns
;
419 int num_rows
= GB(data
, MAT_ROW_START
, MAT_ROW_BITS
); // Upper 8 bits of the widget data: Number of rows in the matrix.
420 int row_height
; // Height of a single row in the matrix.
422 row_height
= resize_y
;
423 num_rows
= r
.Height() / row_height
;
425 row_height
= r
.Height() / num_rows
;
428 int col
= GetColourGradient(colour
, SHADE_LIGHTER
);
431 for (int ctr
= num_columns
; ctr
> 1; ctr
--) {
433 GfxFillRect(x
, r
.top
+ WidgetDimensions::scaled
.bevel
.top
, x
+ WidgetDimensions::scaled
.bevel
.left
- 1, r
.bottom
- WidgetDimensions::scaled
.bevel
.bottom
, col
);
437 for (int ctr
= num_rows
; ctr
> 1; ctr
--) {
439 GfxFillRect(r
.left
+ WidgetDimensions::scaled
.bevel
.left
, x
, r
.right
- WidgetDimensions::scaled
.bevel
.right
, x
+ WidgetDimensions::scaled
.bevel
.top
- 1, col
);
442 col
= GetColourGradient(colour
, SHADE_NORMAL
);
445 for (int ctr
= num_columns
; ctr
> 1; ctr
--) {
447 GfxFillRect(x
- WidgetDimensions::scaled
.bevel
.right
+ 1, r
.top
+ WidgetDimensions::scaled
.bevel
.top
, x
, r
.bottom
- WidgetDimensions::scaled
.bevel
.bottom
, col
);
451 for (int ctr
= num_rows
; ctr
> 1; ctr
--) {
453 GfxFillRect(r
.left
+ WidgetDimensions::scaled
.bevel
.left
, x
- WidgetDimensions::scaled
.bevel
.bottom
+ 1, r
.right
- WidgetDimensions::scaled
.bevel
.right
, x
, col
);
458 * Draw a vertical scrollbar.
459 * @param r Rectangle of the scrollbar widget.
460 * @param colour Colour of the scrollbar widget.
461 * @param up_clicked Up-arrow is clicked.
462 * @param bar_dragged Bar is dragged.
463 * @param down_clicked Down-arrow is clicked.
464 * @param scrollbar Scrollbar size, offset, and capacity information.
466 static inline void DrawVerticalScrollbar(const Rect
&r
, Colours colour
, bool up_clicked
, bool bar_dragged
, bool down_clicked
, const Scrollbar
*scrollbar
)
468 int height
= NWidgetScrollbar::GetVerticalDimension().height
;
470 /* draw up/down buttons */
471 DrawImageButtons(r
.WithHeight(height
, false), NWID_VSCROLLBAR
, colour
, up_clicked
, SPR_ARROW_UP
, SA_CENTER
);
472 DrawImageButtons(r
.WithHeight(height
, true), NWID_VSCROLLBAR
, colour
, down_clicked
, SPR_ARROW_DOWN
, SA_CENTER
);
474 int c1
= GetColourGradient(colour
, SHADE_DARK
);
475 int c2
= GetColourGradient(colour
, SHADE_LIGHTEST
);
477 /* draw "shaded" background */
478 GfxFillRect(r
.left
, r
.top
+ height
, r
.right
, r
.bottom
- height
, c2
);
479 GfxFillRect(r
.left
, r
.top
+ height
, r
.right
, r
.bottom
- height
, c1
, FILLRECT_CHECKER
);
481 /* track positions. These fractions are based on original 1x dimensions, but scale better. */
482 int left
= r
.left
+ r
.Width() * 3 / 11; /* left track is positioned 3/11ths from the left */
483 int right
= r
.left
+ r
.Width() * 8 / 11; /* right track is positioned 8/11ths from the left */
484 const uint8_t bl
= WidgetDimensions::scaled
.bevel
.left
;
485 const uint8_t br
= WidgetDimensions::scaled
.bevel
.right
;
487 /* draw shaded lines */
488 GfxFillRect(left
- bl
, r
.top
+ height
, left
- 1, r
.bottom
- height
, c1
);
489 GfxFillRect(left
, r
.top
+ height
, left
+ br
- 1, r
.bottom
- height
, c2
);
490 GfxFillRect(right
- bl
, r
.top
+ height
, right
- 1, r
.bottom
- height
, c1
);
491 GfxFillRect(right
, r
.top
+ height
, right
+ br
- 1, r
.bottom
- height
, c2
);
493 Point pt
= HandleScrollbarHittest(scrollbar
, r
.top
, r
.bottom
, false);
494 DrawFrameRect(r
.left
, pt
.x
, r
.right
, pt
.y
, colour
, bar_dragged
? FR_LOWERED
: FR_NONE
);
498 * Draw a horizontal scrollbar.
499 * @param r Rectangle of the scrollbar widget.
500 * @param colour Colour of the scrollbar widget.
501 * @param left_clicked Left-arrow is clicked.
502 * @param bar_dragged Bar is dragged.
503 * @param right_clicked Right-arrow is clicked.
504 * @param scrollbar Scrollbar size, offset, and capacity information.
506 static inline void DrawHorizontalScrollbar(const Rect
&r
, Colours colour
, bool left_clicked
, bool bar_dragged
, bool right_clicked
, const Scrollbar
*scrollbar
)
508 int width
= NWidgetScrollbar::GetHorizontalDimension().width
;
510 DrawImageButtons(r
.WithWidth(width
, false), NWID_HSCROLLBAR
, colour
, left_clicked
, SPR_ARROW_LEFT
, SA_CENTER
);
511 DrawImageButtons(r
.WithWidth(width
, true), NWID_HSCROLLBAR
, colour
, right_clicked
, SPR_ARROW_RIGHT
, SA_CENTER
);
513 int c1
= GetColourGradient(colour
, SHADE_DARK
);
514 int c2
= GetColourGradient(colour
, SHADE_LIGHTEST
);
516 /* draw "shaded" background */
517 GfxFillRect(r
.left
+ width
, r
.top
, r
.right
- width
, r
.bottom
, c2
);
518 GfxFillRect(r
.left
+ width
, r
.top
, r
.right
- width
, r
.bottom
, c1
, FILLRECT_CHECKER
);
520 /* track positions. These fractions are based on original 1x dimensions, but scale better. */
521 int top
= r
.top
+ r
.Height() * 3 / 11; /* top track is positioned 3/11ths from the top */
522 int bottom
= r
.top
+ r
.Height() * 8 / 11; /* bottom track is positioned 8/11ths from the top */
523 const uint8_t bt
= WidgetDimensions::scaled
.bevel
.top
;
524 const uint8_t bb
= WidgetDimensions::scaled
.bevel
.bottom
;
526 /* draw shaded lines */
527 GfxFillRect(r
.left
+ width
, top
- bt
, r
.right
- width
, top
- 1, c1
);
528 GfxFillRect(r
.left
+ width
, top
, r
.right
- width
, top
+ bb
- 1, c2
);
529 GfxFillRect(r
.left
+ width
, bottom
- bt
, r
.right
- width
, bottom
- 1, c1
);
530 GfxFillRect(r
.left
+ width
, bottom
, r
.right
- width
, bottom
+ bb
- 1, c2
);
532 /* draw actual scrollbar */
533 Point pt
= HandleScrollbarHittest(scrollbar
, r
.left
, r
.right
, true);
534 DrawFrameRect(pt
.x
, r
.top
, pt
.y
, r
.bottom
, colour
, bar_dragged
? FR_LOWERED
: FR_NONE
);
538 * Draw a frame widget.
539 * @param r Rectangle of the frame.
540 * @param colour Colour of the frame.
541 * @param text_colour Colour of the text.
542 * @param str Text of the frame.
543 * @param align Alignment of the text in the frame.
544 * @param fs Font size of the text.
546 static inline void DrawFrame(const Rect
&r
, Colours colour
, TextColour text_colour
, StringID str
, StringAlignment align
, FontSize fs
)
548 int x2
= r
.left
; // by default the left side is the left side of the widget
550 if (str
!= STR_NULL
) x2
= DrawString(r
.left
+ WidgetDimensions::scaled
.frametext
.left
, r
.right
- WidgetDimensions::scaled
.frametext
.right
, r
.top
, str
, text_colour
, align
, false, fs
);
552 int c1
= GetColourGradient(colour
, SHADE_DARK
);
553 int c2
= GetColourGradient(colour
, SHADE_LIGHTEST
);
555 /* If the frame has text, adjust the top bar to fit half-way through */
556 Rect inner
= r
.Shrink(ScaleGUITrad(1));
557 if (str
!= STR_NULL
) inner
.top
= r
.top
+ GetCharacterHeight(FS_NORMAL
) / 2;
559 Rect outer
= inner
.Expand(WidgetDimensions::scaled
.bevel
);
560 Rect inside
= inner
.Shrink(WidgetDimensions::scaled
.bevel
);
562 if (_current_text_dir
== TD_LTR
) {
563 /* Line from upper left corner to start of text */
564 GfxFillRect(outer
.left
, outer
.top
, r
.left
+ WidgetDimensions::scaled
.frametext
.left
- WidgetDimensions::scaled
.bevel
.left
- 1, inner
.top
- 1, c1
);
565 GfxFillRect(inner
.left
, inner
.top
, r
.left
+ WidgetDimensions::scaled
.frametext
.left
- WidgetDimensions::scaled
.bevel
.left
- 1, inside
.top
- 1, c2
);
567 /* Line from end of text to upper right corner */
568 GfxFillRect(x2
+ WidgetDimensions::scaled
.bevel
.right
, outer
.top
, inner
.right
, inner
.top
- 1, c1
);
569 GfxFillRect(x2
+ WidgetDimensions::scaled
.bevel
.right
, inner
.top
, inside
.right
, inside
.top
- 1, c2
);
571 /* Line from upper left corner to start of text */
572 GfxFillRect(outer
.left
, outer
.top
, x2
- WidgetDimensions::scaled
.bevel
.left
- 1, inner
.top
- 1, c1
);
573 GfxFillRect(inner
.left
, inner
.top
, x2
- WidgetDimensions::scaled
.bevel
.left
- 1, inside
.top
- 1, c2
);
575 /* Line from end of text to upper right corner */
576 GfxFillRect(r
.right
- WidgetDimensions::scaled
.frametext
.right
+ WidgetDimensions::scaled
.bevel
.right
, outer
.top
, inner
.right
, inner
.top
- 1, c1
);
577 GfxFillRect(r
.right
- WidgetDimensions::scaled
.frametext
.right
+ WidgetDimensions::scaled
.bevel
.right
, inner
.top
, inside
.right
, inside
.top
- 1, c2
);
580 /* Line from upper left corner to bottom left corner */
581 GfxFillRect(outer
.left
, inner
.top
, inner
.left
- 1, inner
.bottom
, c1
);
582 GfxFillRect(inner
.left
, inside
.top
, inside
.left
- 1, inside
.bottom
, c2
);
584 /* Line from upper right corner to bottom right corner */
585 GfxFillRect(inside
.right
+ 1, inner
.top
, inner
.right
, inside
.bottom
, c1
);
586 GfxFillRect(inner
.right
+ 1, outer
.top
, outer
.right
, inner
.bottom
, c2
);
588 /* Line from bottom left corner to bottom right corner */
589 GfxFillRect(inner
.left
, inside
.bottom
+ 1, inner
.right
, inner
.bottom
, c1
);
590 GfxFillRect(outer
.left
, inner
.bottom
+ 1, outer
.right
, outer
.bottom
, c2
);
595 * @param r Rectangle of the box.
596 * @param colour Colour of the shade box.
597 * @param clicked Box is lowered.
599 static inline void DrawShadeBox(const Rect
&r
, Colours colour
, bool clicked
)
601 DrawImageButtons(r
, WWT_SHADEBOX
, colour
, clicked
, clicked
? SPR_WINDOW_SHADE
: SPR_WINDOW_UNSHADE
, SA_CENTER
);
606 * @param r Rectangle of the box.
607 * @param colour Colour of the sticky box.
608 * @param clicked Box is lowered.
610 static inline void DrawStickyBox(const Rect
&r
, Colours colour
, bool clicked
)
612 DrawImageButtons(r
, WWT_STICKYBOX
, colour
, clicked
, clicked
? SPR_PIN_UP
: SPR_PIN_DOWN
, SA_CENTER
);
616 * Draw a defsize box.
617 * @param r Rectangle of the box.
618 * @param colour Colour of the defsize box.
619 * @param clicked Box is lowered.
621 static inline void DrawDefSizeBox(const Rect
&r
, Colours colour
, bool clicked
)
623 DrawImageButtons(r
, WWT_DEFSIZEBOX
, colour
, clicked
, SPR_WINDOW_DEFSIZE
, SA_CENTER
);
627 * Draw a NewGRF debug box.
628 * @param r Rectangle of the box.
629 * @param colour Colour of the debug box.
630 * @param clicked Box is lowered.
632 static inline void DrawDebugBox(const Rect
&r
, Colours colour
, bool clicked
)
634 DrawImageButtons(r
, WWT_DEBUGBOX
, colour
, clicked
, SPR_WINDOW_DEBUG
, SA_CENTER
);
639 * @param r Rectangle of the box.
640 * @param colour Colour of the resize box.
641 * @param at_left Resize box is at left-side of the window,
642 * @param clicked Box is lowered.
643 * @param bevel Draw bevel iff set.
645 static inline void DrawResizeBox(const Rect
&r
, Colours colour
, bool at_left
, bool clicked
, bool bevel
)
648 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
649 } else if (clicked
) {
650 GfxFillRect(r
.Shrink(WidgetDimensions::scaled
.bevel
), GetColourGradient(colour
, SHADE_LIGHTER
));
652 DrawSpriteIgnorePadding(at_left
? SPR_WINDOW_RESIZE_LEFT
: SPR_WINDOW_RESIZE_RIGHT
, PAL_NONE
, r
.Shrink(ScaleGUITrad(2)), at_left
? (SA_LEFT
| SA_BOTTOM
| SA_FORCE
) : (SA_RIGHT
| SA_BOTTOM
| SA_FORCE
));
657 * @param r Rectangle of the box.`
658 * @param colour Colour of the close box.
660 static inline void DrawCloseBox(const Rect
&r
, Colours colour
)
662 if (colour
!= COLOUR_WHITE
) DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, FR_NONE
);
664 Dimension d
= GetSpriteSize(SPR_CLOSEBOX
, &offset
);
666 d
.height
-= offset
.y
;
667 int s
= ScaleSpriteTrad(1); /* Offset to account for shadow of SPR_CLOSEBOX */
668 DrawSprite(SPR_CLOSEBOX
, (colour
!= COLOUR_WHITE
? TC_BLACK
: TC_SILVER
) | (1U << PALETTE_TEXT_RECOLOUR
), CenterBounds(r
.left
, r
.right
, d
.width
- s
) - offset
.x
, CenterBounds(r
.top
, r
.bottom
, d
.height
- s
) - offset
.y
);
672 * Draw a caption bar.
673 * @param r Rectangle of the bar.
674 * @param colour Colour of the window.
675 * @param owner 'Owner' of the window.
676 * @param text_colour Colour of the text.
677 * @param str Text to draw in the bar.
678 * @param align Alignment of the text.
679 * @param fs Font size of the text.
681 void DrawCaption(const Rect
&r
, Colours colour
, Owner owner
, TextColour text_colour
, StringID str
, StringAlignment align
, FontSize fs
)
683 bool company_owned
= owner
< MAX_COMPANIES
;
685 DrawFrameRect(r
, colour
, FR_BORDERONLY
);
686 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.bevel
);
687 DrawFrameRect(ir
, colour
, company_owned
? FR_LOWERED
| FR_DARKENED
| FR_BORDERONLY
: FR_LOWERED
| FR_DARKENED
);
690 GfxFillRect(ir
.Shrink(WidgetDimensions::scaled
.bevel
), GetColourGradient(_company_colours
[owner
], SHADE_NORMAL
));
693 if (str
!= STR_NULL
) {
694 Dimension d
= GetStringBoundingBox(str
);
695 Point p
= GetAlignedPosition(r
, d
, align
);
696 DrawString(r
.left
+ WidgetDimensions::scaled
.captiontext
.left
, r
.right
- WidgetDimensions::scaled
.captiontext
.left
, p
.y
, str
, text_colour
, align
, false, fs
);
701 * Draw a button with a dropdown (#WWT_DROPDOWN and #NWID_BUTTON_DROPDOWN).
702 * @param r Rectangle containing the widget.
703 * @param colour Background colour of the widget.
704 * @param clicked_button The button-part is clicked.
705 * @param clicked_dropdown The drop-down part is clicked.
706 * @param str Text of the button.
707 * @param align Alignment of the text within the dropdown.
709 * @note Magic constants are also used in #NWidgetLeaf::ButtonHit.
711 static inline void DrawButtonDropdown(const Rect
&r
, Colours colour
, bool clicked_button
, bool clicked_dropdown
, StringID str
, StringAlignment align
)
713 int dd_width
= NWidgetLeaf::dropdown_dimension
.width
;
715 if (_current_text_dir
== TD_LTR
) {
716 DrawFrameRect(r
.left
, r
.top
, r
.right
- dd_width
, r
.bottom
, colour
, clicked_button
? FR_LOWERED
: FR_NONE
);
717 DrawImageButtons(r
.WithWidth(dd_width
, true), WWT_DROPDOWN
, colour
, clicked_dropdown
, SPR_ARROW_DOWN
, SA_CENTER
);
718 if (str
!= STR_NULL
) {
719 DrawString(r
.left
+ WidgetDimensions::scaled
.dropdowntext
.left
, r
.right
- dd_width
- WidgetDimensions::scaled
.dropdowntext
.right
, CenterBounds(r
.top
, r
.bottom
, GetCharacterHeight(FS_NORMAL
)), str
, TC_BLACK
, align
);
722 DrawFrameRect(r
.left
+ dd_width
, r
.top
, r
.right
, r
.bottom
, colour
, clicked_button
? FR_LOWERED
: FR_NONE
);
723 DrawImageButtons(r
.WithWidth(dd_width
, false), WWT_DROPDOWN
, colour
, clicked_dropdown
, SPR_ARROW_DOWN
, SA_CENTER
);
724 if (str
!= STR_NULL
) {
725 DrawString(r
.left
+ dd_width
+ WidgetDimensions::scaled
.dropdowntext
.left
, r
.right
- WidgetDimensions::scaled
.dropdowntext
.right
, CenterBounds(r
.top
, r
.bottom
, GetCharacterHeight(FS_NORMAL
)), str
, TC_BLACK
, align
);
731 * Paint all widgets of a window.
733 void Window::DrawWidgets() const
735 this->nested_root
->Draw(this);
737 if (this->flags
& WF_WHITE_BORDER
) {
738 DrawFrameRect(0, 0, this->width
- 1, this->height
- 1, COLOUR_WHITE
, FR_BORDERONLY
);
741 if (this->flags
& WF_HIGHLIGHTED
) {
742 extern bool _window_highlight_colour
;
743 for (const auto &pair
: this->widget_lookup
) {
744 const NWidgetBase
*widget
= pair
.second
;
745 if (!widget
->IsHighlighted()) continue;
747 Rect outer
= widget
->GetCurrentRect();
748 Rect inner
= outer
.Shrink(WidgetDimensions::scaled
.bevel
).Expand(1);
750 int colour
= _string_colourmap
[_window_highlight_colour
? widget
->GetHighlightColour() : TC_WHITE
];
752 GfxFillRect(outer
.left
, outer
.top
, inner
.left
, inner
.bottom
, colour
);
753 GfxFillRect(inner
.left
+ 1, outer
.top
, inner
.right
- 1, inner
.top
, colour
);
754 GfxFillRect(inner
.right
, outer
.top
, outer
.right
, inner
.bottom
, colour
);
755 GfxFillRect(outer
.left
+ 1, inner
.bottom
, outer
.right
- 1, outer
.bottom
, colour
);
761 * Draw a sort button's up or down arrow symbol.
762 * @param widget Sort button widget
763 * @param state State of sort button
765 void Window::DrawSortButtonState(WidgetID widget
, SortButtonState state
) const
767 if (state
== SBS_OFF
) return;
769 assert(!this->widget_lookup
.empty());
770 Rect r
= this->GetWidget
<NWidgetBase
>(widget
)->GetCurrentRect();
772 /* Sort button uses the same sprites as vertical scrollbar */
773 Dimension dim
= NWidgetScrollbar::GetVerticalDimension();
775 DrawSpriteIgnorePadding(state
== SBS_DOWN
? SPR_ARROW_DOWN
: SPR_ARROW_UP
, PAL_NONE
, r
.WithWidth(dim
.width
, _current_text_dir
== TD_LTR
), SA_CENTER
);
779 * Get width of up/down arrow of sort button state.
780 * @return Width of space required by sort button arrow.
782 int Window::SortButtonWidth()
784 return NWidgetScrollbar::GetVerticalDimension().width
+ 1;
787 bool _draw_widget_outlines
;
789 static void DrawOutline(const Window
*, const NWidgetBase
*wid
)
791 if (!_draw_widget_outlines
|| wid
->current_x
== 0 || wid
->current_y
== 0) return;
793 DrawRectOutline(wid
->GetCurrentRect(), PC_WHITE
, 1, 4);
797 * @defgroup NestedWidgets Hierarchical widgets
798 * Hierarchical widgets, also known as nested widgets, are widgets stored in a tree. At the leafs of the tree are (mostly) the 'real' widgets
799 * visible to the user. At higher levels, widgets get organized in container widgets, until all widgets of the window are merged.
801 * \section nestedwidgetkinds Hierarchical widget kinds
802 * A leaf widget is one of
804 * <li> #NWidgetLeaf for widgets visible for the user, or
805 * <li> #NWidgetSpacer for creating (flexible) empty space between widgets.
807 * The purpose of a leaf widget is to provide interaction with the user by displaying settings, and/or allowing changing the settings.
809 * A container widget is one of
811 * <li> #NWidgetHorizontal for organizing child widgets in a (horizontal) row. The row switches order depending on the language setting (thus supporting
812 * right-to-left languages),
813 * <li> #NWidgetHorizontalLTR for organizing child widgets in a (horizontal) row, always in the same order. All children below this container will also
815 * <li> #NWidgetVertical for organizing child widgets underneath each other.
816 * <li> #NWidgetMatrix for organizing child widgets in a matrix form.
817 * <li> #NWidgetBackground for adding a background behind its child widget.
818 * <li> #NWidgetStacked for stacking child widgets on top of each other.
820 * The purpose of a container widget is to structure its leafs and sub-containers to allow proper resizing.
822 * \section nestedwidgetscomputations Hierarchical widget computations
823 * The first 'computation' is the creation of the nested widgets tree by calling the constructors of the widgets listed above and calling \c Add() for every child,
824 * or by means of specifying the tree as a collection of nested widgets parts and instantiating the tree from the array.
826 * After the creation step,
827 * - The leafs have their own minimal size (\e min_x, \e min_y), filling (\e fill_x, \e fill_y), and resize steps (\e resize_x, \e resize_y).
828 * - Containers only know what their children are, \e fill_x, \e fill_y, \e resize_x, and \e resize_y are not initialized.
830 * Computations in the nested widgets take place as follows:
832 * <li> A bottom-up sweep by recursively calling NWidgetBase::SetupSmallestSize() to initialize the smallest size (\e smallest_x, \e smallest_y) and
833 * to propagate filling and resize steps upwards to the root of the tree.
834 * <li> A top-down sweep by recursively calling NWidgetBase::AssignSizePosition() with #ST_SMALLEST to make the smallest sizes consistent over
835 * the entire tree, and to assign the top-left (\e pos_x, \e pos_y) position of each widget in the tree. This step uses \e fill_x and \e fill_y at each
836 * node in the tree to decide how to fill each widget towards consistent sizes. Also the current size (\e current_x and \e current_y) is set.
837 * <li> After initializing the smallest size in the widget tree with #ST_SMALLEST, the tree can be resized (the current size modified) by calling
838 * NWidgetBase::AssignSizePosition() at the root with #ST_RESIZE and the new size of the window. For proper functioning, the new size should be the smallest
839 * size + a whole number of resize steps in both directions (ie you can only resize in steps of length resize_{x,y} from smallest_{x,y}).
841 * After the second step, the current size of the widgets are set to the smallest size.
843 * To resize, perform the last step with the new window size. This can be done as often as desired.
844 * When the smallest size of at least one widget changes, the whole procedure has to be redone from the start.
846 * @see NestedWidgetParts
850 * Base class constructor.
851 * @param tp Nested widget type.
853 NWidgetBase::NWidgetBase(WidgetType tp
) : ZeroedMemoryAllocator()
858 /* ~NWidgetContainer() takes care of #next and #prev data members. */
861 * @fn void NWidgetBase::SetupSmallestSize(Window *w)
862 * Compute smallest size needed by the widget.
864 * The smallest size of a widget is the smallest size that a widget needs to
865 * display itself properly. In addition, filling and resizing of the widget are computed.
866 * The function calls #Window::UpdateWidgetSize for each leaf widget and
867 * background widget without child with a non-negative index.
869 * @param w Window owning the widget.
871 * @note After the computation, the results can be queried by accessing the #smallest_x and #smallest_y data members of the widget.
875 * @fn void NWidgetBase::AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl)
876 * Assign size and position to the widget.
877 * @param sizing Type of resizing to perform.
878 * @param x Horizontal offset of the widget relative to the left edge of the window.
879 * @param y Vertical offset of the widget relative to the top edge of the window.
880 * @param given_width Width allocated to the widget.
881 * @param given_height Height allocated to the widget.
882 * @param rtl Adapt for right-to-left languages (position contents of horizontal containers backwards).
884 * Afterwards, \e pos_x and \e pos_y contain the top-left position of the widget, \e smallest_x and \e smallest_y contain
885 * the smallest size such that all widgets of the window are consistent, and \e current_x and \e current_y contain the current size.
889 * @fn void NWidgetBase::FillWidgetLookup(WidgetLookup &widget_lookup)
890 * Fill the Window::widget_lookup with pointers to nested widgets in the tree.
891 * @param widget_lookup The WidgetLookup.
895 * @fn void NWidgetBase::Draw(const Window *w)
896 * Draw the widgets of the tree.
897 * The function calls #Window::DrawWidget for each widget with a non-negative index, after the widget itself is painted.
898 * @param w Window that owns the tree.
902 * Mark the widget as 'dirty' (in need of repaint).
903 * @param w Window owning the widget.
905 void NWidgetBase::SetDirty(const Window
*w
) const
907 int abs_left
= w
->left
+ this->pos_x
;
908 int abs_top
= w
->top
+ this->pos_y
;
909 AddDirtyBlock(abs_left
, abs_top
, abs_left
+ this->current_x
, abs_top
+ this->current_y
);
913 * @fn NWidgetCore *NWidgetBase::GetWidgetFromPos(int x, int y)
914 * Retrieve a widget by its position.
915 * @param x Horizontal position relative to the left edge of the window.
916 * @param y Vertical position relative to the top edge of the window.
917 * @return Returns the deepest nested widget that covers the given position, or \c nullptr if no widget can be found.
921 * Retrieve a widget by its type.
922 * @param tp Widget type to search for.
923 * @return Returns the first widget of the specified type, or \c nullptr if no widget can be found.
925 NWidgetBase
*NWidgetBase::GetWidgetOfType(WidgetType tp
)
927 return (this->type
== tp
) ? this : nullptr;
930 void NWidgetBase::ApplyAspectRatio()
932 if (this->aspect_ratio
== 0) return;
933 if (this->smallest_x
== 0 || this->smallest_y
== 0) return;
935 uint x
= this->smallest_x
;
936 uint y
= this->smallest_y
;
937 if (HasFlag(this->aspect_flags
, AspectFlags::ResizeX
)) x
= std::max(this->smallest_x
, static_cast<uint
>(this->smallest_y
* std::abs(this->aspect_ratio
)));
938 if (HasFlag(this->aspect_flags
, AspectFlags::ResizeY
)) y
= std::max(this->smallest_y
, static_cast<uint
>(this->smallest_x
/ std::abs(this->aspect_ratio
)));
940 this->smallest_x
= x
;
941 this->smallest_y
= y
;
944 void NWidgetBase::AdjustPaddingForZoom()
946 this->padding
= ScaleGUITrad(this->uz_padding
);
950 * Constructor for resizable nested widgets.
951 * @param tp Nested widget type.
952 * @param fill_x Horizontal fill step size, \c 0 means no filling is allowed.
953 * @param fill_y Vertical fill step size, \c 0 means no filling is allowed.
955 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp
, uint fill_x
, uint fill_y
) : NWidgetBase(tp
)
957 this->fill_x
= fill_x
;
958 this->fill_y
= fill_y
;
962 * Set desired aspect ratio of this widget.
963 * @param ratio Desired aspect ratio, or 0 for none.
964 * @param flags Dimensions which should be resized.
966 void NWidgetResizeBase::SetAspect(float ratio
, AspectFlags flags
)
968 this->aspect_ratio
= ratio
;
969 this->aspect_flags
= flags
;
973 * Set desired aspect ratio of this widget, in terms of horizontal and vertical dimensions.
974 * @param x_ratio Desired horizontal component of aspect ratio.
975 * @param y_ratio Desired vertical component of aspect ratio.
976 * @param flags Dimensions which should be resized.
978 void NWidgetResizeBase::SetAspect(int x_ratio
, int y_ratio
, AspectFlags flags
)
980 this->SetAspect(static_cast<float>(x_ratio
) / static_cast<float>(y_ratio
), flags
);
983 void NWidgetResizeBase::AdjustPaddingForZoom()
985 if (!this->absolute
) {
986 this->min_x
= ScaleGUITrad(this->uz_min_x
);
987 this->min_y
= std::max(ScaleGUITrad(this->uz_min_y
), this->uz_text_lines
* GetCharacterHeight(this->uz_text_size
) + ScaleGUITrad(this->uz_text_spacing
));
989 NWidgetBase::AdjustPaddingForZoom();
993 * Set minimal size of the widget.
994 * @param min_x Horizontal minimal size of the widget.
995 * @param min_y Vertical minimal size of the widget.
997 void NWidgetResizeBase::SetMinimalSize(uint min_x
, uint min_y
)
999 this->uz_min_x
= std::max(this->uz_min_x
, min_x
);
1000 this->uz_min_y
= std::max(this->uz_min_y
, min_y
);
1001 this->min_x
= ScaleGUITrad(this->uz_min_x
);
1002 this->min_y
= std::max(ScaleGUITrad(this->uz_min_y
), this->uz_text_lines
* GetCharacterHeight(this->uz_text_size
) + ScaleGUITrad(this->uz_text_spacing
));
1006 * Set absolute (post-scaling) minimal size of the widget.
1007 * @param min_x Horizontal minimal size of the widget.
1008 * @param min_y Vertical minimal size of the widget.
1010 void NWidgetResizeBase::SetMinimalSizeAbsolute(uint min_x
, uint min_y
)
1012 this->absolute
= true;
1013 this->min_x
= std::max(this->min_x
, min_x
);
1014 this->min_y
= std::max(this->min_y
, min_y
);
1018 * Set minimal text lines for the widget.
1019 * @param min_lines Number of text lines of the widget.
1020 * @param spacing Extra unscaled spacing (eg WidgetDimensions::unscaled.framerect.Vertical()) of the widget.
1021 * @param size Font size of text.
1023 void NWidgetResizeBase::SetMinimalTextLines(uint8_t min_lines
, uint8_t spacing
, FontSize size
)
1025 this->uz_text_lines
= min_lines
;
1026 this->uz_text_spacing
= spacing
;
1027 this->uz_text_size
= size
;
1028 this->min_y
= std::max(ScaleGUITrad(this->uz_min_y
), this->uz_text_lines
* GetCharacterHeight(this->uz_text_size
) + ScaleGUITrad(this->uz_text_spacing
));
1032 * Set the filling of the widget from initial size.
1033 * @param fill_x Horizontal fill step size, \c 0 means no filling is allowed.
1034 * @param fill_y Vertical fill step size, \c 0 means no filling is allowed.
1036 void NWidgetResizeBase::SetFill(uint fill_x
, uint fill_y
)
1038 this->fill_x
= fill_x
;
1039 this->fill_y
= fill_y
;
1043 * Set resize step of the widget.
1044 * @param resize_x Resize step in horizontal direction, value \c 0 means no resize, otherwise the step size in pixels.
1045 * @param resize_y Resize step in vertical direction, value \c 0 means no resize, otherwise the step size in pixels.
1047 void NWidgetResizeBase::SetResize(uint resize_x
, uint resize_y
)
1049 this->resize_x
= resize_x
;
1050 this->resize_y
= resize_y
;
1054 * Try to set optimum widget size for a multiline text widget.
1055 * The window will need to be reinited if the size is changed.
1056 * @param str Multiline string contents that will fill the widget.
1057 * @param max_line Maximum number of lines.
1058 * @return true iff the widget minimum size has changed.
1060 bool NWidgetResizeBase::UpdateMultilineWidgetSize(const std::string
&str
, int max_lines
)
1062 int y
= GetStringHeight(str
, this->current_x
);
1063 if (y
> max_lines
* GetCharacterHeight(FS_NORMAL
)) {
1064 /* Text at the current width is too tall, so try to guess a better width. */
1065 Dimension d
= GetStringBoundingBox(str
);
1066 d
.height
*= max_lines
;
1068 return this->UpdateSize(d
.width
, d
.height
);
1070 return this->UpdateVerticalSize(y
);
1074 * Set absolute (post-scaling) minimal size of the widget.
1075 * The window will need to be reinited if the size is changed.
1076 * @param min_x Horizontal minimal size of the widget.
1077 * @param min_y Vertical minimal size of the widget.
1078 * @return true iff the widget minimum size has changed.
1080 bool NWidgetResizeBase::UpdateSize(uint min_x
, uint min_y
)
1082 if (min_x
== this->min_x
&& min_y
== this->min_y
) return false;
1083 this->min_x
= min_x
;
1084 this->min_y
= min_y
;
1089 * Set absolute (post-scaling) minimal size of the widget.
1090 * The window will need to be reinited if the size is changed.
1091 * @param min_y Vertical minimal size of the widget.
1092 * @return true iff the widget minimum size has changed.
1094 bool NWidgetResizeBase::UpdateVerticalSize(uint min_y
)
1096 if (min_y
== this->min_y
) return false;
1097 this->min_y
= min_y
;
1101 void NWidgetResizeBase::AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool)
1103 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1107 * Initialization of a 'real' widget.
1108 * @param tp Type of the widget.
1109 * @param colour Colour of the widget.
1110 * @param index Index of the widget.
1111 * @param fill_x Default horizontal filling.
1112 * @param fill_y Default vertical filling.
1113 * @param widget_data Data component of the widget. @see Widget::data
1114 * @param tool_tip Tool tip of the widget. @see Widget::tooltips
1116 NWidgetCore::NWidgetCore(WidgetType tp
, Colours colour
, WidgetID index
, uint fill_x
, uint fill_y
, uint32_t widget_data
, StringID tool_tip
) : NWidgetResizeBase(tp
, fill_x
, fill_y
), index(index
)
1118 this->colour
= colour
;
1119 this->widget_data
= widget_data
;
1120 this->tool_tip
= tool_tip
;
1121 this->scrollbar_index
= -1;
1122 this->text_colour
= tp
== WWT_CAPTION
? TC_WHITE
: TC_BLACK
;
1123 this->text_size
= FS_NORMAL
;
1124 this->align
= SA_CENTER
;
1128 * Set data and tool tip of the nested widget.
1129 * @param widget_data Data to use.
1130 * @param tool_tip Tool tip string to use.
1132 void NWidgetCore::SetDataTip(uint32_t widget_data
, StringID tool_tip
)
1134 this->widget_data
= widget_data
;
1135 this->tool_tip
= tool_tip
;
1139 * Set the text style of the nested widget.
1140 * @param colour TextColour to use.
1141 * @param size Font size to use.
1143 void NWidgetCore::SetTextStyle(TextColour colour
, FontSize size
)
1145 this->text_colour
= colour
;
1146 this->text_size
= size
;
1150 * Set the tool tip of the nested widget.
1151 * @param tool_tip Tool tip string to use.
1153 void NWidgetCore::SetToolTip(StringID tool_tip
)
1155 this->tool_tip
= tool_tip
;
1159 * Set the text/image alignment of the nested widget.
1160 * @param align Alignment to use.
1162 void NWidgetCore::SetAlignment(StringAlignment align
)
1164 this->align
= align
;
1167 void NWidgetCore::FillWidgetLookup(WidgetLookup
&widget_lookup
)
1169 if (this->index
>= 0) widget_lookup
[this->index
] = this;
1172 NWidgetCore
*NWidgetCore::GetWidgetFromPos(int x
, int y
)
1174 return (IsInsideBS(x
, this->pos_x
, this->current_x
) && IsInsideBS(y
, this->pos_y
, this->current_y
)) ? this : nullptr;
1177 NWidgetBase
*NWidgetContainer::GetWidgetOfType(WidgetType tp
)
1179 if (this->type
== tp
) return this;
1180 for (const auto &child_wid
: this->children
) {
1181 NWidgetBase
*nwid
= child_wid
->GetWidgetOfType(tp
);
1182 if (nwid
!= nullptr) return nwid
;
1187 void NWidgetContainer::AdjustPaddingForZoom()
1189 for (const auto &child_wid
: this->children
) {
1190 child_wid
->AdjustPaddingForZoom();
1192 NWidgetBase::AdjustPaddingForZoom();
1196 * Append widget \a wid to container.
1197 * @param wid Widget to append.
1199 void NWidgetContainer::Add(std::unique_ptr
<NWidgetBase
> &&wid
)
1201 assert(wid
!= nullptr);
1203 this->children
.push_back(std::move(wid
));
1206 void NWidgetContainer::FillWidgetLookup(WidgetLookup
&widget_lookup
)
1208 for (const auto &child_wid
: this->children
) {
1209 child_wid
->FillWidgetLookup(widget_lookup
);
1213 void NWidgetContainer::Draw(const Window
*w
)
1215 for (const auto &child_wid
: this->children
) {
1219 DrawOutline(w
, this);
1222 NWidgetCore
*NWidgetContainer::GetWidgetFromPos(int x
, int y
)
1224 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return nullptr;
1226 for (const auto &child_wid
: this->children
) {
1227 NWidgetCore
*nwid
= child_wid
->GetWidgetFromPos(x
, y
);
1228 if (nwid
!= nullptr) return nwid
;
1234 * Widgets stacked on top of each other.
1236 NWidgetStacked::NWidgetStacked(WidgetID index
) : NWidgetContainer(NWID_SELECTION
), index(index
)
1240 void NWidgetStacked::SetupSmallestSize(Window
*w
)
1242 /* Zero size plane selected */
1243 if (this->shown_plane
>= SZSP_BEGIN
) {
1244 Dimension size
= {0, 0};
1245 Dimension padding
= {0, 0};
1246 Dimension fill
= {(this->shown_plane
== SZSP_HORIZONTAL
), (this->shown_plane
== SZSP_VERTICAL
)};
1247 Dimension resize
= {(this->shown_plane
== SZSP_HORIZONTAL
), (this->shown_plane
== SZSP_VERTICAL
)};
1248 /* Here we're primarily interested in the value of resize */
1249 if (this->index
>= 0) w
->UpdateWidgetSize(this->index
, size
, padding
, fill
, resize
);
1251 this->smallest_x
= size
.width
;
1252 this->smallest_y
= size
.height
;
1253 this->fill_x
= fill
.width
;
1254 this->fill_y
= fill
.height
;
1255 this->resize_x
= resize
.width
;
1256 this->resize_y
= resize
.height
;
1257 this->ApplyAspectRatio();
1261 /* First sweep, recurse down and compute minimal size and filling. */
1262 this->smallest_x
= 0;
1263 this->smallest_y
= 0;
1264 this->fill_x
= this->IsEmpty() ? 0 : 1;
1265 this->fill_y
= this->IsEmpty() ? 0 : 1;
1266 this->resize_x
= this->IsEmpty() ? 0 : 1;
1267 this->resize_y
= this->IsEmpty() ? 0 : 1;
1268 for (const auto &child_wid
: this->children
) {
1269 child_wid
->SetupSmallestSize(w
);
1271 this->smallest_x
= std::max(this->smallest_x
, child_wid
->smallest_x
+ child_wid
->padding
.Horizontal());
1272 this->smallest_y
= std::max(this->smallest_y
, child_wid
->smallest_y
+ child_wid
->padding
.Vertical());
1273 this->fill_x
= std::lcm(this->fill_x
, child_wid
->fill_x
);
1274 this->fill_y
= std::lcm(this->fill_y
, child_wid
->fill_y
);
1275 this->resize_x
= std::lcm(this->resize_x
, child_wid
->resize_x
);
1276 this->resize_y
= std::lcm(this->resize_y
, child_wid
->resize_y
);
1277 this->ApplyAspectRatio();
1281 void NWidgetStacked::AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
)
1283 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1284 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1286 if (this->shown_plane
>= SZSP_BEGIN
) return;
1288 for (const auto &child_wid
: this->children
) {
1289 uint hor_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetHorizontalStepSize(sizing
);
1290 uint child_width
= ComputeMaxSize(child_wid
->smallest_x
, given_width
- child_wid
->padding
.Horizontal(), hor_step
);
1291 uint child_pos_x
= (rtl
? child_wid
->padding
.right
: child_wid
->padding
.left
);
1293 uint vert_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetVerticalStepSize(sizing
);
1294 uint child_height
= ComputeMaxSize(child_wid
->smallest_y
, given_height
- child_wid
->padding
.Vertical(), vert_step
);
1295 uint child_pos_y
= child_wid
->padding
.top
;
1297 child_wid
->AssignSizePosition(sizing
, x
+ child_pos_x
, y
+ child_pos_y
, child_width
, child_height
, rtl
);
1301 void NWidgetStacked::FillWidgetLookup(WidgetLookup
&widget_lookup
)
1303 /* We need to update widget_lookup later. */
1304 this->widget_lookup
= &widget_lookup
;
1306 if (this->index
>= 0) widget_lookup
[this->index
] = this;
1307 NWidgetContainer::FillWidgetLookup(widget_lookup
);
1308 /* In case widget IDs are repeated, make sure Window::GetWidget works on displayed widgets. */
1309 if (static_cast<size_t>(this->shown_plane
) < this->children
.size()) this->children
[shown_plane
]->FillWidgetLookup(widget_lookup
);
1312 void NWidgetStacked::Draw(const Window
*w
)
1314 if (this->shown_plane
>= SZSP_BEGIN
) return;
1316 assert(static_cast<size_t>(this->shown_plane
) < this->children
.size());
1317 this->children
[shown_plane
]->Draw(w
);
1318 DrawOutline(w
, this);
1321 NWidgetCore
*NWidgetStacked::GetWidgetFromPos(int x
, int y
)
1323 if (this->shown_plane
>= SZSP_BEGIN
) return nullptr;
1325 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return nullptr;
1327 if (static_cast<size_t>(this->shown_plane
) >= this->children
.size()) return nullptr;
1328 return this->children
[shown_plane
]->GetWidgetFromPos(x
, y
);
1332 * Select which plane to show (for #NWID_SELECTION only).
1333 * @param plane Plane number to display.
1334 * @return true iff the shown plane changed.
1336 bool NWidgetStacked::SetDisplayedPlane(int plane
)
1338 if (this->shown_plane
== plane
) return false;
1339 this->shown_plane
= plane
;
1340 /* In case widget IDs are repeated, make sure Window::GetWidget works on displayed widgets. */
1341 if (static_cast<size_t>(this->shown_plane
) < this->children
.size()) this->children
[shown_plane
]->FillWidgetLookup(*this->widget_lookup
);
1345 class NWidgetLayer
: public NWidgetContainer
{
1347 NWidgetLayer(WidgetID index
);
1349 void SetupSmallestSize(Window
*w
) override
;
1350 void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) override
;
1352 void Draw(const Window
*w
) override
;
1354 const WidgetID index
; ///< If non-negative, index in the #Window::widget_lookup.
1357 NWidgetLayer::NWidgetLayer(WidgetID index
) : NWidgetContainer(NWID_LAYER
), index(index
) {}
1359 void NWidgetLayer::SetupSmallestSize(Window
*w
)
1361 /* First sweep, recurse down and compute minimal size and filling. */
1362 this->smallest_x
= 0;
1363 this->smallest_y
= 0;
1364 this->fill_x
= this->IsEmpty() ? 0 : 1;
1365 this->fill_y
= this->IsEmpty() ? 0 : 1;
1366 this->resize_x
= this->IsEmpty() ? 0 : 1;
1367 this->resize_y
= this->IsEmpty() ? 0 : 1;
1368 for (const auto &child_wid
: this->children
) {
1369 child_wid
->SetupSmallestSize(w
);
1371 this->smallest_x
= std::max(this->smallest_x
, child_wid
->smallest_x
+ child_wid
->padding
.Horizontal());
1372 this->smallest_y
= std::max(this->smallest_y
, child_wid
->smallest_y
+ child_wid
->padding
.Vertical());
1373 this->fill_x
= std::lcm(this->fill_x
, child_wid
->fill_x
);
1374 this->fill_y
= std::lcm(this->fill_y
, child_wid
->fill_y
);
1375 this->resize_x
= std::lcm(this->resize_x
, child_wid
->resize_x
);
1376 this->resize_y
= std::lcm(this->resize_y
, child_wid
->resize_y
);
1377 this->ApplyAspectRatio();
1381 void NWidgetLayer::AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
)
1383 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1384 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1386 for (const auto &child_wid
: this->children
) {
1387 uint hor_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetHorizontalStepSize(sizing
);
1388 uint child_width
= ComputeMaxSize(child_wid
->smallest_x
, given_width
- child_wid
->padding
.Horizontal(), hor_step
);
1389 uint child_pos_x
= (rtl
? child_wid
->padding
.right
: child_wid
->padding
.left
);
1391 uint vert_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetVerticalStepSize(sizing
);
1392 uint child_height
= ComputeMaxSize(child_wid
->smallest_y
, given_height
- child_wid
->padding
.Vertical(), vert_step
);
1393 uint child_pos_y
= child_wid
->padding
.top
;
1395 child_wid
->AssignSizePosition(sizing
, x
+ child_pos_x
, y
+ child_pos_y
, child_width
, child_height
, rtl
);
1399 void NWidgetLayer::Draw(const Window
*w
)
1401 /* Draw in reverse order, as layers are arranged top-down. */
1402 for (auto it
= std::rbegin(this->children
); it
!= std::rend(this->children
); ++it
) {
1406 DrawOutline(w
, this);
1409 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp
, NWidContainerFlags flags
) : NWidgetContainer(tp
)
1411 this->flags
= flags
;
1414 void NWidgetPIPContainer::AdjustPaddingForZoom()
1416 this->pip_pre
= ScaleGUITrad(this->uz_pip_pre
);
1417 this->pip_inter
= ScaleGUITrad(this->uz_pip_inter
);
1418 this->pip_post
= ScaleGUITrad(this->uz_pip_post
);
1419 NWidgetContainer::AdjustPaddingForZoom();
1423 * Set additional pre/inter/post space for the container.
1425 * @param pip_pre Additional space in front of the first child widget (above
1426 * for the vertical container, at the left for the horizontal container).
1427 * @param pip_inter Additional space between two child widgets.
1428 * @param pip_post Additional space after the last child widget (below for the
1429 * vertical container, at the right for the horizontal container).
1431 void NWidgetPIPContainer::SetPIP(uint8_t pip_pre
, uint8_t pip_inter
, uint8_t pip_post
)
1433 this->uz_pip_pre
= pip_pre
;
1434 this->uz_pip_inter
= pip_inter
;
1435 this->uz_pip_post
= pip_post
;
1437 this->pip_pre
= ScaleGUITrad(this->uz_pip_pre
);
1438 this->pip_inter
= ScaleGUITrad(this->uz_pip_inter
);
1439 this->pip_post
= ScaleGUITrad(this->uz_pip_post
);
1443 * Set additional pre/inter/post space for the container.
1445 * @param pip_ratio_pre Ratio of additional space in front of the first child widget (above
1446 * for the vertical container, at the left for the horizontal container).
1447 * @param pip_ratio_inter Ratio of additional space between two child widgets.
1448 * @param pip_ratio_post Ratio of additional space after the last child widget (below for the
1449 * vertical container, at the right for the horizontal container).
1451 void NWidgetPIPContainer::SetPIPRatio(uint8_t pip_ratio_pre
, uint8_t pip_ratio_inter
, uint8_t pip_ratio_post
)
1453 this->pip_ratio_pre
= pip_ratio_pre
;
1454 this->pip_ratio_inter
= pip_ratio_inter
;
1455 this->pip_ratio_post
= pip_ratio_post
;
1458 /** Horizontal container widget. */
1459 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags
) : NWidgetPIPContainer(NWID_HORIZONTAL
, flags
)
1463 void NWidgetHorizontal::SetupSmallestSize(Window
*w
)
1465 this->smallest_x
= 0; // Sum of minimal size of all children.
1466 this->smallest_y
= 0; // Biggest child.
1467 this->fill_x
= 0; // smallest non-zero child widget fill step.
1468 this->fill_y
= 1; // smallest common child fill step.
1469 this->resize_x
= 0; // smallest non-zero child widget resize step.
1470 this->resize_y
= 1; // smallest common child resize step.
1473 /* 1a. Forward call, collect longest/widest child length. */
1474 uint longest
= 0; // Longest child found.
1475 uint max_vert_fill
= 0; // Biggest vertical fill step.
1476 for (const auto &child_wid
: this->children
) {
1477 child_wid
->SetupSmallestSize(w
);
1478 longest
= std::max(longest
, child_wid
->smallest_x
);
1479 max_vert_fill
= std::max(max_vert_fill
, child_wid
->GetVerticalStepSize(ST_SMALLEST
));
1480 this->smallest_y
= std::max(this->smallest_y
, child_wid
->smallest_y
+ child_wid
->padding
.Vertical());
1481 if (child_wid
->smallest_x
!= 0 || child_wid
->fill_x
!= 0) this->gaps
++;
1483 if (this->gaps
> 0) this->gaps
--; // Number of gaps is number of widgets less one.
1484 /* 1b. Make the container higher if needed to accommodate all children nicely. */
1485 [[maybe_unused
]] uint max_smallest
= this->smallest_y
+ 3 * max_vert_fill
; // Upper limit to computing smallest height.
1486 uint cur_height
= this->smallest_y
;
1488 for (const auto &child_wid
: this->children
) {
1489 uint step_size
= child_wid
->GetVerticalStepSize(ST_SMALLEST
);
1490 uint child_height
= child_wid
->smallest_y
+ child_wid
->padding
.Vertical();
1491 if (step_size
> 1 && child_height
< cur_height
) { // Small step sizes or already fitting children are not interesting.
1492 uint remainder
= (cur_height
- child_height
) % step_size
;
1493 if (remainder
> 0) { // Child did not fit entirely, widen the container.
1494 cur_height
+= step_size
- remainder
;
1495 assert(cur_height
< max_smallest
); // Safeguard against infinite height expansion.
1496 /* Remaining children will adapt to the new cur_height, thus speeding up the computation. */
1500 if (this->smallest_y
== cur_height
) break;
1501 this->smallest_y
= cur_height
; // Smallest height got changed, try again.
1503 /* 2. For containers that must maintain equal width, extend child minimal size. */
1504 for (const auto &child_wid
: this->children
) {
1505 child_wid
->smallest_y
= this->smallest_y
- child_wid
->padding
.Vertical();
1506 child_wid
->ApplyAspectRatio();
1507 longest
= std::max(longest
, child_wid
->smallest_x
);
1509 if (this->flags
& NC_EQUALSIZE
) {
1510 for (const auto &child_wid
: this->children
) {
1511 if (child_wid
->fill_x
== 1) child_wid
->smallest_x
= longest
;
1514 /* 3. Compute smallest, fill, and resize values of the container. */
1515 for (const auto &child_wid
: this->children
) {
1516 this->smallest_x
+= child_wid
->smallest_x
+ child_wid
->padding
.Horizontal();
1517 if (child_wid
->fill_x
> 0) {
1518 if (this->fill_x
== 0 || this->fill_x
> child_wid
->fill_x
) this->fill_x
= child_wid
->fill_x
;
1520 this->fill_y
= std::lcm(this->fill_y
, child_wid
->fill_y
);
1522 if (child_wid
->resize_x
> 0) {
1523 if (this->resize_x
== 0 || this->resize_x
> child_wid
->resize_x
) this->resize_x
= child_wid
->resize_x
;
1525 this->resize_y
= std::lcm(this->resize_y
, child_wid
->resize_y
);
1527 if (this->fill_x
== 0 && this->pip_ratio_pre
+ this->pip_ratio_inter
+ this->pip_ratio_post
> 0) this->fill_x
= 1;
1528 /* 4. Increase by required PIP space. */
1529 this->smallest_x
+= this->pip_pre
+ this->gaps
* this->pip_inter
+ this->pip_post
;
1532 void NWidgetHorizontal::AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
)
1534 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1536 /* Compute additional width given to us. */
1537 uint additional_length
= given_width
- (this->pip_pre
+ this->gaps
* this->pip_inter
+ this->pip_post
);
1538 for (const auto &child_wid
: this->children
) {
1539 if (child_wid
->smallest_x
!= 0 || child_wid
->fill_x
!= 0) additional_length
-= child_wid
->smallest_x
+ child_wid
->padding
.Horizontal();
1542 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1544 /* In principle, the additional horizontal space is distributed evenly over the available resizable children. Due to step sizes, this may not always be feasible.
1545 * To make resizing work as good as possible, first children with biggest step sizes are done. These may get less due to rounding down.
1546 * This additional space is then given to children with smaller step sizes. This will give a good result when resize steps of each child is a multiple
1547 * of the child with the smallest non-zero stepsize.
1549 * Since child sizes are computed out of order, positions cannot be calculated until all sizes are known. That means it is not possible to compute the child
1550 * size and position, and directly call child->AssignSizePosition() with the computed values.
1551 * Instead, computed child widths and heights are stored in child->current_x and child->current_y values. That is allowed, since this method overwrites those values
1552 * then we call the child.
1555 /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle vertical size for all children,
1556 * handle horizontal size for non-resizing children.
1558 int num_changing_childs
= 0; // Number of children that can change size.
1559 uint biggest_stepsize
= 0;
1560 for (const auto &child_wid
: this->children
) {
1561 uint hor_step
= child_wid
->GetHorizontalStepSize(sizing
);
1563 if (!(flags
& NC_BIGFIRST
)) num_changing_childs
++;
1564 biggest_stepsize
= std::max(biggest_stepsize
, hor_step
);
1566 child_wid
->current_x
= child_wid
->smallest_x
;
1569 uint vert_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetVerticalStepSize(sizing
);
1570 child_wid
->current_y
= ComputeMaxSize(child_wid
->smallest_y
, given_height
- child_wid
->padding
.Vertical(), vert_step
);
1573 /* First.5 loop: count how many children are of the biggest step size. */
1574 if ((flags
& NC_BIGFIRST
) && biggest_stepsize
> 0) {
1575 for (const auto &child_wid
: this->children
) {
1576 uint hor_step
= child_wid
->GetHorizontalStepSize(sizing
);
1577 if (hor_step
== biggest_stepsize
) {
1578 num_changing_childs
++;
1583 /* Second loop: Allocate the additional horizontal space over the resizing children, starting with the biggest resize steps. */
1584 while (biggest_stepsize
> 0) {
1585 uint next_biggest_stepsize
= 0;
1586 for (const auto &child_wid
: this->children
) {
1587 uint hor_step
= child_wid
->GetHorizontalStepSize(sizing
);
1588 if (hor_step
> biggest_stepsize
) continue; // Already done
1589 if (hor_step
== biggest_stepsize
) {
1590 uint increment
= additional_length
/ num_changing_childs
;
1591 num_changing_childs
--;
1592 if (hor_step
> 1) increment
-= increment
% hor_step
;
1593 child_wid
->current_x
= child_wid
->smallest_x
+ increment
;
1594 additional_length
-= increment
;
1597 next_biggest_stepsize
= std::max(next_biggest_stepsize
, hor_step
);
1599 biggest_stepsize
= next_biggest_stepsize
;
1601 if (num_changing_childs
== 0 && (flags
& NC_BIGFIRST
) && biggest_stepsize
> 0) {
1602 /* Second.5 loop: count how many children are of the updated biggest step size. */
1603 for (const auto &child_wid
: this->children
) {
1604 uint hor_step
= child_wid
->GetHorizontalStepSize(sizing
);
1605 if (hor_step
== biggest_stepsize
) {
1606 num_changing_childs
++;
1611 assert(num_changing_childs
== 0);
1613 uint pre
= this->pip_pre
;
1614 uint inter
= this->pip_inter
;
1616 if (additional_length
> 0) {
1617 /* Allocate remaining space by pip ratios. If this doesn't round exactly, the unused space will fall into pip_post
1618 * which is never explicitly needed. */
1619 int r
= this->pip_ratio_pre
+ this->gaps
* this->pip_ratio_inter
+ this->pip_ratio_post
;
1621 pre
+= this->pip_ratio_pre
* additional_length
/ r
;
1622 if (this->gaps
> 0) inter
+= this->pip_ratio_inter
* additional_length
/ r
;
1626 /* Third loop: Compute position and call the child. */
1627 uint position
= rtl
? this->current_x
- pre
: pre
; // Place to put next child relative to origin of the container.
1628 for (const auto &child_wid
: this->children
) {
1629 uint child_width
= child_wid
->current_x
;
1630 uint child_x
= x
+ (rtl
? position
- child_width
- child_wid
->padding
.left
: position
+ child_wid
->padding
.left
);
1631 uint child_y
= y
+ child_wid
->padding
.top
;
1633 child_wid
->AssignSizePosition(sizing
, child_x
, child_y
, child_width
, child_wid
->current_y
, rtl
);
1634 if (child_wid
->current_x
!= 0) {
1635 uint padded_child_width
= child_width
+ child_wid
->padding
.Horizontal() + inter
;
1636 position
= rtl
? position
- padded_child_width
: position
+ padded_child_width
;
1641 /** Horizontal left-to-right container widget. */
1642 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags
) : NWidgetHorizontal(flags
)
1644 this->type
= NWID_HORIZONTAL_LTR
;
1647 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool)
1649 NWidgetHorizontal::AssignSizePosition(sizing
, x
, y
, given_width
, given_height
, false);
1652 /** Vertical container widget. */
1653 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags
) : NWidgetPIPContainer(NWID_VERTICAL
, flags
)
1657 void NWidgetVertical::SetupSmallestSize(Window
*w
)
1659 this->smallest_x
= 0; // Biggest child.
1660 this->smallest_y
= 0; // Sum of minimal size of all children.
1661 this->fill_x
= 1; // smallest common child fill step.
1662 this->fill_y
= 0; // smallest non-zero child widget fill step.
1663 this->resize_x
= 1; // smallest common child resize step.
1664 this->resize_y
= 0; // smallest non-zero child widget resize step.
1667 /* 1a. Forward call, collect longest/widest child length. */
1668 uint highest
= 0; // Highest child found.
1669 uint max_hor_fill
= 0; // Biggest horizontal fill step.
1670 for (const auto &child_wid
: this->children
) {
1671 child_wid
->SetupSmallestSize(w
);
1672 highest
= std::max(highest
, child_wid
->smallest_y
);
1673 max_hor_fill
= std::max(max_hor_fill
, child_wid
->GetHorizontalStepSize(ST_SMALLEST
));
1674 this->smallest_x
= std::max(this->smallest_x
, child_wid
->smallest_x
+ child_wid
->padding
.Horizontal());
1675 if (child_wid
->smallest_y
!= 0 || child_wid
->fill_y
!= 0) this->gaps
++;
1677 if (this->gaps
> 0) this->gaps
--; // Number of gaps is number of widgets less one.
1678 /* 1b. Make the container wider if needed to accommodate all children nicely. */
1679 [[maybe_unused
]] uint max_smallest
= this->smallest_x
+ 3 * max_hor_fill
; // Upper limit to computing smallest height.
1680 uint cur_width
= this->smallest_x
;
1682 for (const auto &child_wid
: this->children
) {
1683 uint step_size
= child_wid
->GetHorizontalStepSize(ST_SMALLEST
);
1684 uint child_width
= child_wid
->smallest_x
+ child_wid
->padding
.Horizontal();
1685 if (step_size
> 1 && child_width
< cur_width
) { // Small step sizes or already fitting children are not interesting.
1686 uint remainder
= (cur_width
- child_width
) % step_size
;
1687 if (remainder
> 0) { // Child did not fit entirely, widen the container.
1688 cur_width
+= step_size
- remainder
;
1689 assert(cur_width
< max_smallest
); // Safeguard against infinite width expansion.
1690 /* Remaining children will adapt to the new cur_width, thus speeding up the computation. */
1694 if (this->smallest_x
== cur_width
) break;
1695 this->smallest_x
= cur_width
; // Smallest width got changed, try again.
1697 /* 2. For containers that must maintain equal width, extend children minimal size. */
1698 for (const auto &child_wid
: this->children
) {
1699 child_wid
->smallest_x
= this->smallest_x
- child_wid
->padding
.Horizontal();
1700 child_wid
->ApplyAspectRatio();
1701 highest
= std::max(highest
, child_wid
->smallest_y
);
1703 if (this->flags
& NC_EQUALSIZE
) {
1704 for (const auto &child_wid
: this->children
) {
1705 if (child_wid
->fill_y
== 1) child_wid
->smallest_y
= highest
;
1708 /* 3. Compute smallest, fill, and resize values of the container. */
1709 for (const auto &child_wid
: this->children
) {
1710 this->smallest_y
+= child_wid
->smallest_y
+ child_wid
->padding
.Vertical();
1711 if (child_wid
->fill_y
> 0) {
1712 if (this->fill_y
== 0 || this->fill_y
> child_wid
->fill_y
) this->fill_y
= child_wid
->fill_y
;
1714 this->fill_x
= std::lcm(this->fill_x
, child_wid
->fill_x
);
1716 if (child_wid
->resize_y
> 0) {
1717 if (this->resize_y
== 0 || this->resize_y
> child_wid
->resize_y
) this->resize_y
= child_wid
->resize_y
;
1719 this->resize_x
= std::lcm(this->resize_x
, child_wid
->resize_x
);
1721 if (this->fill_y
== 0 && this->pip_ratio_pre
+ this->pip_ratio_inter
+ this->pip_ratio_post
> 0) this->fill_y
= 1;
1722 /* 4. Increase by required PIP space. */
1723 this->smallest_y
+= this->pip_pre
+ this->gaps
* this->pip_inter
+ this->pip_post
;
1726 void NWidgetVertical::AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
)
1728 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1730 /* Compute additional height given to us. */
1731 uint additional_length
= given_height
- (this->pip_pre
+ this->gaps
* this->pip_inter
+ this->pip_post
);
1732 for (const auto &child_wid
: this->children
) {
1733 if (child_wid
->smallest_y
!= 0 || child_wid
->fill_y
!= 0) additional_length
-= child_wid
->smallest_y
+ child_wid
->padding
.Vertical();
1736 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1738 /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the children with the biggest resize steps.
1739 * It also stores computed widths and heights into current_x and current_y values of the child.
1742 /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle horizontal size for all children, handle vertical size for non-resizing child. */
1743 int num_changing_childs
= 0; // Number of children that can change size.
1744 uint biggest_stepsize
= 0;
1745 for (const auto &child_wid
: this->children
) {
1746 uint vert_step
= child_wid
->GetVerticalStepSize(sizing
);
1747 if (vert_step
> 0) {
1748 if (!(flags
& NC_BIGFIRST
)) num_changing_childs
++;
1749 biggest_stepsize
= std::max(biggest_stepsize
, vert_step
);
1751 child_wid
->current_y
= child_wid
->smallest_y
;
1754 uint hor_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetHorizontalStepSize(sizing
);
1755 child_wid
->current_x
= ComputeMaxSize(child_wid
->smallest_x
, given_width
- child_wid
->padding
.Horizontal(), hor_step
);
1758 /* First.5 loop: count how many children are of the biggest step size. */
1759 if ((this->flags
& NC_BIGFIRST
) && biggest_stepsize
> 0) {
1760 for (const auto &child_wid
: this->children
) {
1761 uint vert_step
= child_wid
->GetVerticalStepSize(sizing
);
1762 if (vert_step
== biggest_stepsize
) {
1763 num_changing_childs
++;
1768 /* Second loop: Allocate the additional vertical space over the resizing children, starting with the biggest resize steps. */
1769 while (biggest_stepsize
> 0) {
1770 uint next_biggest_stepsize
= 0;
1771 for (const auto &child_wid
: this->children
) {
1772 uint vert_step
= child_wid
->GetVerticalStepSize(sizing
);
1773 if (vert_step
> biggest_stepsize
) continue; // Already done
1774 if (vert_step
== biggest_stepsize
) {
1775 uint increment
= additional_length
/ num_changing_childs
;
1776 num_changing_childs
--;
1777 if (vert_step
> 1) increment
-= increment
% vert_step
;
1778 child_wid
->current_y
= child_wid
->smallest_y
+ increment
;
1779 additional_length
-= increment
;
1782 next_biggest_stepsize
= std::max(next_biggest_stepsize
, vert_step
);
1784 biggest_stepsize
= next_biggest_stepsize
;
1786 if (num_changing_childs
== 0 && (flags
& NC_BIGFIRST
) && biggest_stepsize
> 0) {
1787 /* Second.5 loop: count how many children are of the updated biggest step size. */
1788 for (const auto &child_wid
: this->children
) {
1789 uint vert_step
= child_wid
->GetVerticalStepSize(sizing
);
1790 if (vert_step
== biggest_stepsize
) {
1791 num_changing_childs
++;
1796 assert(num_changing_childs
== 0);
1798 uint pre
= this->pip_pre
;
1799 uint inter
= this->pip_inter
;
1801 if (additional_length
> 0) {
1802 /* Allocate remaining space by pip ratios. If this doesn't round exactly, the unused space will fall into pip_post
1803 * which is never explicitly needed. */
1804 int r
= this->pip_ratio_pre
+ this->gaps
* this->pip_ratio_inter
+ this->pip_ratio_post
;
1806 pre
+= this->pip_ratio_pre
* additional_length
/ r
;
1807 if (this->gaps
> 0) inter
+= this->pip_ratio_inter
* additional_length
/ r
;
1811 /* Third loop: Compute position and call the child. */
1812 uint position
= pre
; // Place to put next child relative to origin of the container.
1813 for (const auto &child_wid
: this->children
) {
1814 uint child_x
= x
+ (rtl
? child_wid
->padding
.right
: child_wid
->padding
.left
);
1815 uint child_height
= child_wid
->current_y
;
1817 child_wid
->AssignSizePosition(sizing
, child_x
, y
+ position
+ child_wid
->padding
.top
, child_wid
->current_x
, child_height
, rtl
);
1818 if (child_wid
->current_y
!= 0) {
1819 position
+= child_height
+ child_wid
->padding
.Vertical() + inter
;
1825 * Generic spacer widget.
1826 * @param width Horizontal size of the spacer widget.
1827 * @param height Vertical size of the spacer widget.
1829 NWidgetSpacer::NWidgetSpacer(int width
, int height
) : NWidgetResizeBase(NWID_SPACER
, 0, 0)
1831 this->SetMinimalSize(width
, height
);
1832 this->SetResize(0, 0);
1835 void NWidgetSpacer::SetupSmallestSize(Window
*)
1837 this->smallest_x
= this->min_x
;
1838 this->smallest_y
= this->min_y
;
1839 this->ApplyAspectRatio();
1842 void NWidgetSpacer::FillWidgetLookup(WidgetLookup
&)
1846 void NWidgetSpacer::Draw(const Window
*w
)
1848 /* Spacer widget is never normally visible. */
1850 if (_draw_widget_outlines
&& this->current_x
!= 0 && this->current_y
!= 0) {
1851 /* Spacers indicate a potential design issue, so get extra highlighting. */
1852 GfxFillRect(this->GetCurrentRect(), PC_WHITE
, FILLRECT_CHECKER
);
1854 DrawOutline(w
, this);
1858 void NWidgetSpacer::SetDirty(const Window
*) const
1860 /* Spacer widget never need repainting. */
1863 NWidgetCore
*NWidgetSpacer::GetWidgetFromPos(int, int)
1868 NWidgetMatrix::NWidgetMatrix(Colours colour
, WidgetID index
) : NWidgetPIPContainer(NWID_MATRIX
, NC_EQUALSIZE
), index(index
), clicked(-1), count(-1)
1870 this->colour
= colour
;
1874 * Sets the clicked element in the matrix.
1875 * @param clicked The clicked element.
1877 void NWidgetMatrix::SetClicked(int clicked
)
1879 this->clicked
= clicked
;
1880 if (this->clicked
>= 0 && this->sb
!= nullptr && this->widgets_x
!= 0) {
1881 int vpos
= (this->clicked
/ this->widgets_x
) * this->widget_h
; // Vertical position of the top.
1882 /* Need to scroll down -> Scroll to the bottom.
1883 * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
1884 if (this->sb
->GetPosition() < vpos
) vpos
+= this->widget_h
- this->pip_inter
- 1;
1885 this->sb
->ScrollTowards(vpos
);
1890 * Set the number of elements in this matrix.
1891 * @note Updates the number of elements/capacity of the real scrollbar.
1892 * @param count The number of elements.
1894 void NWidgetMatrix::SetCount(int count
)
1896 this->count
= count
;
1898 if (this->sb
== nullptr || this->widgets_x
== 0) return;
1900 /* We need to get the number of pixels the matrix is high/wide.
1901 * So, determine the number of rows/columns based on the number of
1902 * columns/rows (one is constant/unscrollable).
1903 * Then multiply that by the height of a widget, and add the pre
1904 * and post spacing "offsets". */
1905 count
= CeilDiv(count
, this->sb
->IsVertical() ? this->widgets_x
: this->widgets_y
);
1906 count
*= (this->sb
->IsVertical() ? this->children
.front()->smallest_y
: this->children
.front()->smallest_x
) + this->pip_inter
;
1907 if (count
> 0) count
-= this->pip_inter
; // We counted an inter too much in the multiplication above
1908 count
+= this->pip_pre
+ this->pip_post
;
1909 this->sb
->SetCount(count
);
1910 this->sb
->SetCapacity(this->sb
->IsVertical() ? this->current_y
: this->current_x
);
1911 this->sb
->SetStepSize(this->sb
->IsVertical() ? this->widget_h
: this->widget_w
);
1915 * Assign a scrollbar to this matrix.
1916 * @param sb The scrollbar to assign to us.
1918 void NWidgetMatrix::SetScrollbar(Scrollbar
*sb
)
1924 * Get current element.
1925 * @returns index of current element.
1927 int NWidgetMatrix::GetCurrentElement() const
1929 return this->current_element
;
1932 void NWidgetMatrix::SetupSmallestSize(Window
*w
)
1934 assert(this->children
.size() == 1);
1936 this->children
.front()->SetupSmallestSize(w
);
1938 Dimension padding
= { (uint
)this->pip_pre
+ this->pip_post
, (uint
)this->pip_pre
+ this->pip_post
};
1939 Dimension size
= {this->children
.front()->smallest_x
+ padding
.width
, this->children
.front()->smallest_y
+ padding
.height
};
1940 Dimension fill
= {0, 0};
1941 Dimension resize
= {this->pip_inter
+ this->children
.front()->smallest_x
, this->pip_inter
+ this->children
.front()->smallest_y
};
1943 if (this->index
>= 0) w
->UpdateWidgetSize(this->index
, size
, padding
, fill
, resize
);
1945 this->smallest_x
= size
.width
;
1946 this->smallest_y
= size
.height
;
1947 this->fill_x
= fill
.width
;
1948 this->fill_y
= fill
.height
;
1949 this->resize_x
= resize
.width
;
1950 this->resize_y
= resize
.height
;
1951 this->ApplyAspectRatio();
1954 void NWidgetMatrix::AssignSizePosition(SizingType
, int x
, int y
, uint given_width
, uint given_height
, bool)
1956 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1960 this->current_x
= given_width
;
1961 this->current_y
= given_height
;
1963 /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
1964 this->widget_w
= this->children
.front()->smallest_x
+ this->pip_inter
;
1965 this->widget_h
= this->children
.front()->smallest_y
+ this->pip_inter
;
1967 /* Account for the pip_inter is between widgets, so we need to account for that when
1968 * the division assumes pip_inter is used for all widgets. */
1969 this->widgets_x
= CeilDiv(this->current_x
- this->pip_pre
- this->pip_post
+ this->pip_inter
, this->widget_w
);
1970 this->widgets_y
= CeilDiv(this->current_y
- this->pip_pre
- this->pip_post
+ this->pip_inter
, this->widget_h
);
1972 /* When resizing, update the scrollbar's count. E.g. with a vertical
1973 * scrollbar becoming wider or narrower means the amount of rows in
1974 * the scrollbar becomes respectively smaller or higher. */
1975 this->SetCount(this->count
);
1978 void NWidgetMatrix::FillWidgetLookup(WidgetLookup
&widget_lookup
)
1980 if (this->index
>= 0) widget_lookup
[this->index
] = this;
1981 NWidgetContainer::FillWidgetLookup(widget_lookup
);
1984 NWidgetCore
*NWidgetMatrix::GetWidgetFromPos(int x
, int y
)
1986 /* Falls outside of the matrix widget. */
1987 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return nullptr;
1989 int start_x
, start_y
, base_offs_x
, base_offs_y
;
1990 this->GetScrollOffsets(start_x
, start_y
, base_offs_x
, base_offs_y
);
1992 bool rtl
= _current_text_dir
== TD_RTL
;
1994 int widget_col
= (rtl
?
1995 -x
+ (int)this->pip_post
+ this->pos_x
+ base_offs_x
+ this->widget_w
- 1 - (int)this->pip_inter
:
1996 x
- (int)this->pip_pre
- this->pos_x
- base_offs_x
1999 int widget_row
= (y
- base_offs_y
- (int)this->pip_pre
- this->pos_y
) / this->widget_h
;
2001 this->current_element
= (widget_row
+ start_y
) * this->widgets_x
+ start_x
+ widget_col
;
2002 if (this->current_element
>= this->count
) return nullptr;
2004 NWidgetCore
*child
= dynamic_cast<NWidgetCore
*>(this->children
.front().get());
2005 assert(child
!= nullptr);
2006 child
->AssignSizePosition(ST_RESIZE
,
2007 this->pos_x
+ (rtl
? this->pip_post
- widget_col
* this->widget_w
: this->pip_pre
+ widget_col
* this->widget_w
) + base_offs_x
,
2008 this->pos_y
+ this->pip_pre
+ widget_row
* this->widget_h
+ base_offs_y
,
2009 child
->smallest_x
, child
->smallest_y
, rtl
);
2011 return child
->GetWidgetFromPos(x
, y
);
2014 /* virtual */ void NWidgetMatrix::Draw(const Window
*w
)
2016 /* Fill the background. */
2017 GfxFillRect(this->GetCurrentRect(), GetColourGradient(this->colour
, SHADE_LIGHT
));
2019 /* Set up a clipping area for the previews. */
2020 bool rtl
= _current_text_dir
== TD_RTL
;
2021 DrawPixelInfo tmp_dpi
;
2022 if (!FillDrawPixelInfo(&tmp_dpi
, this->pos_x
+ (rtl
? this->pip_post
: this->pip_pre
), this->pos_y
+ this->pip_pre
, this->current_x
- this->pip_pre
- this->pip_post
, this->current_y
- this->pip_pre
- this->pip_post
)) return;
2025 AutoRestoreBackup
dpi_backup(_cur_dpi
, &tmp_dpi
);
2027 /* Get the appropriate offsets so we can draw the right widgets. */
2028 NWidgetCore
*child
= dynamic_cast<NWidgetCore
*>(this->children
.front().get());
2029 assert(child
!= nullptr);
2030 int start_x
, start_y
, base_offs_x
, base_offs_y
;
2031 this->GetScrollOffsets(start_x
, start_y
, base_offs_x
, base_offs_y
);
2033 int offs_y
= base_offs_y
;
2034 for (int y
= start_y
; y
< start_y
+ this->widgets_y
+ 1; y
++, offs_y
+= this->widget_h
) {
2035 /* Are we within bounds? */
2036 if (offs_y
+ child
->smallest_y
<= 0) continue;
2037 if (offs_y
>= (int)this->current_y
) break;
2039 /* We've passed our amount of widgets. */
2040 if (y
* this->widgets_x
>= this->count
) break;
2042 int offs_x
= base_offs_x
;
2043 for (int x
= start_x
; x
< start_x
+ this->widgets_x
+ 1; x
++, offs_x
+= rtl
? -this->widget_w
: this->widget_w
) {
2044 /* Are we within bounds? */
2045 if (offs_x
+ child
->smallest_x
<= 0) continue;
2046 if (offs_x
>= (int)this->current_x
) continue;
2048 /* Do we have this many widgets? */
2049 this->current_element
= y
* this->widgets_x
+ x
;
2050 if (this->current_element
>= this->count
) break;
2052 child
->AssignSizePosition(ST_RESIZE
, offs_x
, offs_y
, child
->smallest_x
, child
->smallest_y
, rtl
);
2053 child
->SetLowered(this->clicked
== this->current_element
);
2059 DrawOutline(w
, this);
2063 * Get the different offsets that are influenced by scrolling.
2064 * @param[out] start_x The start position in columns (index of the left-most column, swapped in RTL).
2065 * @param[out] start_y The start position in rows.
2066 * @param[out] base_offs_x The base horizontal offset in pixels (X position of the column \a start_x).
2067 * @param[out] base_offs_y The base vertical offset in pixels (Y position of the column \a start_y).
2069 void NWidgetMatrix::GetScrollOffsets(int &start_x
, int &start_y
, int &base_offs_x
, int &base_offs_y
)
2071 base_offs_x
= _current_text_dir
== TD_RTL
? this->widget_w
* (this->widgets_x
- 1) : 0;
2075 if (this->sb
!= nullptr) {
2076 if (this->sb
->IsVertical()) {
2077 start_y
= this->sb
->GetPosition() / this->widget_h
;
2078 base_offs_y
+= -this->sb
->GetPosition() + start_y
* this->widget_h
;
2080 start_x
= this->sb
->GetPosition() / this->widget_w
;
2081 int sub_x
= this->sb
->GetPosition() - start_x
* this->widget_w
;
2082 if (_current_text_dir
== TD_RTL
) {
2083 base_offs_x
+= sub_x
;
2085 base_offs_x
-= sub_x
;
2092 * Constructor parent nested widgets.
2093 * @param tp Type of parent widget.
2094 * @param colour Colour of the parent widget.
2095 * @param index Index of the widget.
2096 * @param child Child container widget (if supplied). If not supplied, a
2097 * vertical container will be inserted while adding the first
2100 NWidgetBackground::NWidgetBackground(WidgetType tp
, Colours colour
, WidgetID index
, std::unique_ptr
<NWidgetPIPContainer
> &&child
) : NWidgetCore(tp
, colour
, index
, 1, 1, 0x0, STR_NULL
)
2102 assert(tp
== WWT_PANEL
|| tp
== WWT_INSET
|| tp
== WWT_FRAME
);
2103 this->child
= std::move(child
);
2104 if (this->child
!= nullptr) this->child
->parent
= this;
2105 this->SetAlignment(SA_TOP
| SA_LEFT
);
2109 * Add a child to the parent.
2110 * @param nwid Nested widget to add to the background widget.
2112 * Unless a child container has been given in the constructor, a parent behaves as a vertical container.
2113 * You can add several children to it, and they are put underneath each other.
2115 void NWidgetBackground::Add(std::unique_ptr
<NWidgetBase
> &&nwid
)
2117 if (this->child
== nullptr) {
2118 this->child
= std::make_unique
<NWidgetVertical
>();
2120 nwid
->parent
= this->child
.get();
2121 this->child
->Add(std::move(nwid
));
2125 * Set additional pre/inter/post space for the background widget.
2127 * @param pip_pre Additional space in front of the first child widget (above
2128 * for the vertical container, at the left for the horizontal container).
2129 * @param pip_inter Additional space between two child widgets.
2130 * @param pip_post Additional space after the last child widget (below for the
2131 * vertical container, at the right for the horizontal container).
2132 * @note Using this function implies that the widget has (or will have) child widgets.
2134 void NWidgetBackground::SetPIP(uint8_t pip_pre
, uint8_t pip_inter
, uint8_t pip_post
)
2136 if (this->child
== nullptr) {
2137 this->child
= std::make_unique
<NWidgetVertical
>();
2139 this->child
->parent
= this;
2140 this->child
->SetPIP(pip_pre
, pip_inter
, pip_post
);
2144 * Set additional pre/inter/post space ratios for the background widget.
2146 * @param pip_ratio_pre Ratio of additional space in front of the first child widget (above
2147 * for the vertical container, at the left for the horizontal container).
2148 * @param pip_ratio_inter Ratio of additional space between two child widgets.
2149 * @param pip_ratio_post Ratio of additional space after the last child widget (below for the
2150 * vertical container, at the right for the horizontal container).
2151 * @note Using this function implies that the widget has (or will have) child widgets.
2153 void NWidgetBackground::SetPIPRatio(uint8_t pip_ratio_pre
, uint8_t pip_ratio_inter
, uint8_t pip_ratio_post
)
2155 if (this->child
== nullptr) {
2156 this->child
= std::make_unique
<NWidgetVertical
>();
2158 this->child
->parent
= this;
2159 this->child
->SetPIPRatio(pip_ratio_pre
, pip_ratio_inter
, pip_ratio_post
);
2162 void NWidgetBackground::AdjustPaddingForZoom()
2164 if (child
!= nullptr) child
->AdjustPaddingForZoom();
2165 NWidgetCore::AdjustPaddingForZoom();
2168 void NWidgetBackground::SetupSmallestSize(Window
*w
)
2170 if (this->child
!= nullptr) {
2171 this->child
->SetupSmallestSize(w
);
2173 this->smallest_x
= this->child
->smallest_x
;
2174 this->smallest_y
= this->child
->smallest_y
;
2175 this->fill_x
= this->child
->fill_x
;
2176 this->fill_y
= this->child
->fill_y
;
2177 this->resize_x
= this->child
->resize_x
;
2178 this->resize_y
= this->child
->resize_y
;
2180 /* Don't apply automatic padding if there is no child widget. */
2181 if (w
== nullptr) return;
2183 if (this->type
== WWT_FRAME
) {
2184 /* Account for the size of the frame's text if that exists */
2185 this->child
->padding
= WidgetDimensions::scaled
.frametext
;
2186 this->child
->padding
.top
= std::max
<uint8_t>(WidgetDimensions::scaled
.frametext
.top
, this->widget_data
!= STR_NULL
? GetCharacterHeight(FS_NORMAL
) + WidgetDimensions::scaled
.frametext
.top
/ 2 : 0);
2188 this->smallest_x
+= this->child
->padding
.Horizontal();
2189 this->smallest_y
+= this->child
->padding
.Vertical();
2191 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2192 this->smallest_x
= std::max(this->smallest_x
, GetStringBoundingBox(this->widget_data
, this->text_size
).width
+ WidgetDimensions::scaled
.frametext
.Horizontal());
2193 } else if (this->type
== WWT_INSET
) {
2194 /* Apply automatic padding for bevel thickness. */
2195 this->child
->padding
= WidgetDimensions::scaled
.bevel
;
2197 this->smallest_x
+= this->child
->padding
.Horizontal();
2198 this->smallest_y
+= this->child
->padding
.Vertical();
2200 this->ApplyAspectRatio();
2202 Dimension d
= {this->min_x
, this->min_y
};
2203 Dimension fill
= {this->fill_x
, this->fill_y
};
2204 Dimension resize
= {this->resize_x
, this->resize_y
};
2205 if (w
!= nullptr) { // A non-nullptr window pointer acts as switch to turn dynamic widget size on.
2206 if (this->type
== WWT_FRAME
|| this->type
== WWT_INSET
) {
2207 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2208 Dimension background
= GetStringBoundingBox(this->widget_data
, this->text_size
);
2209 background
.width
+= (this->type
== WWT_FRAME
) ? (WidgetDimensions::scaled
.frametext
.Horizontal()) : (WidgetDimensions::scaled
.inset
.Horizontal());
2210 d
= maxdim(d
, background
);
2212 if (this->index
>= 0) {
2214 switch (this->type
) {
2215 default: NOT_REACHED();
2216 case WWT_PANEL
: padding
= {WidgetDimensions::scaled
.framerect
.Horizontal(), WidgetDimensions::scaled
.framerect
.Vertical()}; break;
2217 case WWT_FRAME
: padding
= {WidgetDimensions::scaled
.frametext
.Horizontal(), WidgetDimensions::scaled
.frametext
.Vertical()}; break;
2218 case WWT_INSET
: padding
= {WidgetDimensions::scaled
.inset
.Horizontal(), WidgetDimensions::scaled
.inset
.Vertical()}; break;
2220 w
->UpdateWidgetSize(this->index
, d
, padding
, fill
, resize
);
2223 this->smallest_x
= d
.width
;
2224 this->smallest_y
= d
.height
;
2225 this->fill_x
= fill
.width
;
2226 this->fill_y
= fill
.height
;
2227 this->resize_x
= resize
.width
;
2228 this->resize_y
= resize
.height
;
2229 this->ApplyAspectRatio();
2233 void NWidgetBackground::AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
)
2235 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
2237 if (this->child
!= nullptr) {
2238 uint x_offset
= (rtl
? this->child
->padding
.right
: this->child
->padding
.left
);
2239 uint width
= given_width
- this->child
->padding
.Horizontal();
2240 uint height
= given_height
- this->child
->padding
.Vertical();
2241 this->child
->AssignSizePosition(sizing
, x
+ x_offset
, y
+ this->child
->padding
.top
, width
, height
, rtl
);
2245 void NWidgetBackground::FillWidgetLookup(WidgetLookup
&widget_lookup
)
2247 if (this->index
>= 0) widget_lookup
[this->index
] = this;
2248 if (this->child
!= nullptr) this->child
->FillWidgetLookup(widget_lookup
);
2251 void NWidgetBackground::Draw(const Window
*w
)
2253 if (this->current_x
== 0 || this->current_y
== 0) return;
2255 Rect r
= this->GetCurrentRect();
2257 const DrawPixelInfo
*dpi
= _cur_dpi
;
2258 if (dpi
->left
> r
.right
|| dpi
->left
+ dpi
->width
<= r
.left
|| dpi
->top
> r
.bottom
|| dpi
->top
+ dpi
->height
<= r
.top
) return;
2260 switch (this->type
) {
2262 assert(this->widget_data
== 0);
2263 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, this->colour
, this->IsLowered() ? FR_LOWERED
: FR_NONE
);
2267 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2268 DrawFrame(r
, this->colour
, this->text_colour
, this->widget_data
, this->align
, this->text_size
);
2272 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2273 DrawInset(r
, this->colour
, this->text_colour
, this->widget_data
, this->align
, this->text_size
);
2280 if (this->index
>= 0) w
->DrawWidget(r
, this->index
);
2281 if (this->child
!= nullptr) this->child
->Draw(w
);
2283 if (this->IsDisabled()) {
2284 GfxFillRect(r
.Shrink(WidgetDimensions::scaled
.bevel
), GetColourGradient(this->colour
, SHADE_DARKER
), FILLRECT_CHECKER
);
2287 DrawOutline(w
, this);
2290 NWidgetCore
*NWidgetBackground::GetWidgetFromPos(int x
, int y
)
2292 NWidgetCore
*nwid
= nullptr;
2293 if (IsInsideBS(x
, this->pos_x
, this->current_x
) && IsInsideBS(y
, this->pos_y
, this->current_y
)) {
2294 if (this->child
!= nullptr) nwid
= this->child
->GetWidgetFromPos(x
, y
);
2295 if (nwid
== nullptr) nwid
= this;
2300 NWidgetBase
*NWidgetBackground::GetWidgetOfType(WidgetType tp
)
2302 NWidgetBase
*nwid
= nullptr;
2303 if (this->child
!= nullptr) nwid
= this->child
->GetWidgetOfType(tp
);
2304 if (nwid
== nullptr && this->type
== tp
) nwid
= this;
2308 NWidgetViewport::NWidgetViewport(WidgetID index
) : NWidgetCore(NWID_VIEWPORT
, INVALID_COLOUR
, index
, 1, 1, 0x0, STR_NULL
)
2312 void NWidgetViewport::SetupSmallestSize(Window
*)
2314 this->smallest_x
= this->min_x
;
2315 this->smallest_y
= this->min_y
;
2316 this->ApplyAspectRatio();
2319 void NWidgetViewport::Draw(const Window
*w
)
2321 if (this->current_x
== 0 || this->current_y
== 0) return;
2323 if (this->disp_flags
& ND_NO_TRANSPARENCY
) {
2324 TransparencyOptionBits to_backup
= _transparency_opt
;
2325 _transparency_opt
&= (1 << TO_SIGNS
) | (1 << TO_TEXT
); // Disable all transparency, except textual stuff
2327 _transparency_opt
= to_backup
;
2332 /* Optionally shade the viewport. */
2333 if (this->disp_flags
& (ND_SHADE_GREY
| ND_SHADE_DIMMED
)) {
2334 GfxFillRect(this->GetCurrentRect(), (this->disp_flags
& ND_SHADE_DIMMED
) ? PALETTE_TO_TRANSPARENT
: PALETTE_NEWSPAPER
, FILLRECT_RECOLOUR
);
2337 DrawOutline(w
, this);
2341 * Initialize the viewport of the window.
2342 * @param w Window owning the viewport.
2343 * @param focus Either the tile index or vehicle ID to focus.
2344 * @param zoom Zoom level.
2346 void NWidgetViewport::InitializeViewport(Window
*w
, std::variant
<TileIndex
, VehicleID
> focus
, ZoomLevel zoom
)
2348 InitializeWindowViewport(w
, this->pos_x
, this->pos_y
, this->current_x
, this->current_y
, focus
, zoom
);
2352 * Update the position and size of the viewport (after eg a resize).
2353 * @param w Window owning the viewport.
2355 void NWidgetViewport::UpdateViewportCoordinates(Window
*w
)
2357 Viewport
*vp
= w
->viewport
;
2358 if (vp
!= nullptr) {
2359 vp
->left
= w
->left
+ this->pos_x
;
2360 vp
->top
= w
->top
+ this->pos_y
;
2361 vp
->width
= this->current_x
;
2362 vp
->height
= this->current_y
;
2364 vp
->virtual_width
= ScaleByZoom(vp
->width
, vp
->zoom
);
2365 vp
->virtual_height
= ScaleByZoom(vp
->height
, vp
->zoom
);
2370 * Compute the row of a scrolled widget that a user clicked in.
2371 * @param clickpos Vertical position of the mouse click (without taking scrolling into account).
2372 * @param w The window the click was in.
2373 * @param widget Widget number of the widget clicked in.
2374 * @param padding Amount of empty space between the widget edge and the top of the first row. Default value is \c 0.
2375 * @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
2376 * @return Row number clicked at. If clicked at a wrong position, #Scrollbar::npos is returned.
2378 Scrollbar::size_type
Scrollbar::GetScrolledRowFromWidget(int clickpos
, const Window
* const w
, WidgetID widget
, int padding
, int line_height
) const
2380 int pos
= w
->GetRowFromWidget(clickpos
, widget
, padding
, line_height
);
2381 if (pos
!= INT_MAX
) pos
+= this->GetPosition();
2382 return (pos
< 0 || pos
>= this->GetCount()) ? Scrollbar::npos
: pos
;
2386 * Update the given list position as if it were on this scroll bar when the given keycode was pressed.
2387 * This does not update the actual position of this scroll bar, that is left to the caller. It does,
2388 * however use the capacity and count of the scroll bar for the bounds and amount to scroll.
2390 * When the count is 0 or the return is ES_NOT_HANDLED, then the position is not updated.
2391 * With WKC_UP and WKC_DOWN the position goes one up or down respectively.
2392 * With WKC_PAGEUP and WKC_PAGEDOWN the position goes one capacity up or down respectively.
2393 * With WKC_HOME the first position is selected and with WKC_END the last position is selected.
2394 * This function ensures that pos is in the range [0..count).
2395 * @param list_position The current position in the list.
2396 * @param key_code The pressed key code.
2397 * @return ES_NOT_HANDLED when another key than the 6 specific keys was pressed, otherwise ES_HANDLED.
2399 EventState
Scrollbar::UpdateListPositionOnKeyPress(int &list_position
, uint16_t keycode
) const
2401 int new_pos
= list_position
;
2404 /* scroll up by one */
2409 /* scroll down by one */
2414 /* scroll up a page */
2415 new_pos
-= this->GetCapacity();
2419 /* scroll down a page */
2420 new_pos
+= this->GetCapacity();
2424 /* jump to beginning */
2430 new_pos
= this->GetCount() - 1;
2434 return ES_NOT_HANDLED
;
2437 /* If there are no elements, there is nothing to scroll/update. */
2438 if (this->GetCount() != 0) {
2439 list_position
= Clamp(new_pos
, 0, this->GetCount() - 1);
2446 * Set capacity of visible elements from the size and resize properties of a widget.
2448 * @param widget Widget with size and resize properties.
2449 * @param padding Padding to subtract from the size.
2450 * @note Updates the position if needed.
2452 void Scrollbar::SetCapacityFromWidget(Window
*w
, WidgetID widget
, int padding
)
2454 NWidgetBase
*nwid
= w
->GetWidget
<NWidgetBase
>(widget
);
2455 if (this->IsVertical()) {
2456 this->SetCapacity(((int)nwid
->current_y
- padding
) / (int)nwid
->resize_y
);
2458 this->SetCapacity(((int)nwid
->current_x
- padding
) / (int)nwid
->resize_x
);
2463 * Apply 'scroll' to a rect to be drawn in.
2464 * @param r Rect to be 'scrolled'.
2465 * @param sb The scrollbar affecting the scroll.
2466 * @param resize_step Resize step of the widget/scrollbar (1 if the scrollbar is pixel-based.)
2467 * @returns Scrolled rect.
2469 Rect
ScrollRect(Rect r
, const Scrollbar
&sb
, int resize_step
)
2471 const int count
= sb
.GetCount() * resize_step
;
2472 const int position
= sb
.GetPosition() * resize_step
;
2474 if (sb
.IsVertical()) {
2476 r
.bottom
= r
.top
+ count
;
2478 bool rtl
= _current_text_dir
== TD_RTL
;
2480 r
.right
+= position
;
2481 r
.left
= r
.right
- count
;
2484 r
.right
= r
.left
+ count
;
2493 * @param tp Scrollbar type. (horizontal/vertical)
2494 * @param colour Colour of the scrollbar.
2495 * @param index Index of the widget.
2497 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp
, Colours colour
, WidgetID index
) : NWidgetCore(tp
, colour
, index
, 1, 1, 0x0, STR_NULL
), Scrollbar(tp
!= NWID_HSCROLLBAR
)
2499 assert(tp
== NWID_HSCROLLBAR
|| tp
== NWID_VSCROLLBAR
);
2501 switch (this->type
) {
2502 case NWID_HSCROLLBAR
:
2503 this->SetResize(1, 0);
2504 this->SetFill(1, 0);
2505 this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST
);
2508 case NWID_VSCROLLBAR
:
2509 this->SetResize(0, 1);
2510 this->SetFill(0, 1);
2511 this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST
);
2514 default: NOT_REACHED();
2518 void NWidgetScrollbar::SetupSmallestSize(Window
*)
2523 switch (this->type
) {
2524 case NWID_HSCROLLBAR
:
2525 this->SetMinimalSizeAbsolute(NWidgetScrollbar::GetHorizontalDimension().width
* 3, NWidgetScrollbar::GetHorizontalDimension().height
);
2528 case NWID_VSCROLLBAR
:
2529 this->SetMinimalSizeAbsolute(NWidgetScrollbar::GetVerticalDimension().width
, NWidgetScrollbar::GetVerticalDimension().height
* 3);
2532 default: NOT_REACHED();
2535 this->smallest_x
= this->min_x
;
2536 this->smallest_y
= this->min_y
;
2539 void NWidgetScrollbar::Draw(const Window
*w
)
2541 if (this->current_x
== 0 || this->current_y
== 0) return;
2543 Rect r
= this->GetCurrentRect();
2545 const DrawPixelInfo
*dpi
= _cur_dpi
;
2546 if (dpi
->left
> r
.right
|| dpi
->left
+ dpi
->width
<= r
.left
|| dpi
->top
> r
.bottom
|| dpi
->top
+ dpi
->height
<= r
.top
) return;
2548 bool up_lowered
= HasBit(this->disp_flags
, NDB_SCROLLBAR_UP
);
2549 bool down_lowered
= HasBit(this->disp_flags
, NDB_SCROLLBAR_DOWN
);
2550 bool middle_lowered
= !(this->disp_flags
& ND_SCROLLBAR_BTN
) && w
->mouse_capture_widget
== this->index
;
2552 if (this->type
== NWID_HSCROLLBAR
) {
2553 DrawHorizontalScrollbar(r
, this->colour
, up_lowered
, middle_lowered
, down_lowered
, this);
2555 DrawVerticalScrollbar(r
, this->colour
, up_lowered
, middle_lowered
, down_lowered
, this);
2558 if (this->IsDisabled()) {
2559 GfxFillRect(r
.Shrink(WidgetDimensions::scaled
.bevel
), GetColourGradient(this->colour
, SHADE_DARKER
), FILLRECT_CHECKER
);
2562 DrawOutline(w
, this);
2565 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
2567 vertical_dimension
.width
= vertical_dimension
.height
= 0;
2568 horizontal_dimension
.width
= horizontal_dimension
.height
= 0;
2571 /* static */ Dimension
NWidgetScrollbar::GetVerticalDimension()
2573 if (vertical_dimension
.width
== 0) {
2574 vertical_dimension
= maxdim(GetScaledSpriteSize(SPR_ARROW_UP
), GetScaledSpriteSize(SPR_ARROW_DOWN
));
2575 vertical_dimension
.width
+= WidgetDimensions::scaled
.vscrollbar
.Horizontal();
2576 vertical_dimension
.height
+= WidgetDimensions::scaled
.vscrollbar
.Vertical();
2578 return vertical_dimension
;
2581 /* static */ Dimension
NWidgetScrollbar::GetHorizontalDimension()
2583 if (horizontal_dimension
.width
== 0) {
2584 horizontal_dimension
= maxdim(GetScaledSpriteSize(SPR_ARROW_LEFT
), GetScaledSpriteSize(SPR_ARROW_RIGHT
));
2585 horizontal_dimension
.width
+= WidgetDimensions::scaled
.hscrollbar
.Horizontal();
2586 horizontal_dimension
.height
+= WidgetDimensions::scaled
.hscrollbar
.Vertical();
2588 return horizontal_dimension
;
2591 Dimension
NWidgetScrollbar::vertical_dimension
= {0, 0};
2592 Dimension
NWidgetScrollbar::horizontal_dimension
= {0, 0};
2594 /** Reset the cached dimensions. */
2595 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
2597 shadebox_dimension
.width
= shadebox_dimension
.height
= 0;
2598 debugbox_dimension
.width
= debugbox_dimension
.height
= 0;
2599 defsizebox_dimension
.width
= defsizebox_dimension
.height
= 0;
2600 stickybox_dimension
.width
= stickybox_dimension
.height
= 0;
2601 resizebox_dimension
.width
= resizebox_dimension
.height
= 0;
2602 closebox_dimension
.width
= closebox_dimension
.height
= 0;
2603 dropdown_dimension
.width
= dropdown_dimension
.height
= 0;
2606 Dimension
NWidgetLeaf::shadebox_dimension
= {0, 0};
2607 Dimension
NWidgetLeaf::debugbox_dimension
= {0, 0};
2608 Dimension
NWidgetLeaf::defsizebox_dimension
= {0, 0};
2609 Dimension
NWidgetLeaf::stickybox_dimension
= {0, 0};
2610 Dimension
NWidgetLeaf::resizebox_dimension
= {0, 0};
2611 Dimension
NWidgetLeaf::closebox_dimension
= {0, 0};
2612 Dimension
NWidgetLeaf::dropdown_dimension
= {0, 0};
2615 * Nested leaf widget.
2616 * @param tp Type of leaf widget.
2617 * @param colour Colour of the leaf widget.
2618 * @param index Index of the widget.
2619 * @param data Data of the widget.
2620 * @param tip Tooltip of the widget.
2622 NWidgetLeaf::NWidgetLeaf(WidgetType tp
, Colours colour
, WidgetID index
, uint32_t data
, StringID tip
) : NWidgetCore(tp
, colour
, index
, 1, 1, data
, tip
)
2624 assert(index
>= 0 || tp
== WWT_LABEL
|| tp
== WWT_TEXT
|| tp
== WWT_CAPTION
|| tp
== WWT_RESIZEBOX
|| tp
== WWT_SHADEBOX
|| tp
== WWT_DEFSIZEBOX
|| tp
== WWT_DEBUGBOX
|| tp
== WWT_STICKYBOX
|| tp
== WWT_CLOSEBOX
);
2627 this->SetResize(0, 0);
2634 this->SetFill(0, 0);
2635 this->SetAlignment(SA_LEFT
| SA_VERT_CENTER
);
2640 case WWT_PUSHIMGBTN
:
2643 case WWT_PUSHTXTBTN
:
2647 case NWID_BUTTON_DROPDOWN
:
2648 case NWID_PUSHBUTTON_DROPDOWN
:
2649 this->SetFill(0, 0);
2653 case WWT_PUSHARROWBTN
:
2654 this->SetFill(0, 0);
2655 this->SetAspect(WidgetDimensions::ASPECT_LEFT_RIGHT_BUTTON
);
2659 this->SetFill(0, 0);
2663 this->SetFill(1, 0);
2664 this->SetResize(1, 0);
2665 this->SetMinimalSize(0, WidgetDimensions::WD_CAPTION_HEIGHT
);
2666 this->SetMinimalTextLines(1, WidgetDimensions::unscaled
.captiontext
.Vertical(), FS_NORMAL
);
2667 this->SetDataTip(data
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
);
2671 this->SetFill(0, 0);
2672 this->SetMinimalSize(WidgetDimensions::WD_STICKYBOX_WIDTH
, WidgetDimensions::WD_CAPTION_HEIGHT
);
2673 this->SetDataTip(STR_NULL
, STR_TOOLTIP_STICKY
);
2674 this->SetAspect(this->min_x
, this->min_y
);
2678 this->SetFill(0, 0);
2679 this->SetMinimalSize(WidgetDimensions::WD_SHADEBOX_WIDTH
, WidgetDimensions::WD_CAPTION_HEIGHT
);
2680 this->SetDataTip(STR_NULL
, STR_TOOLTIP_SHADE
);
2681 this->SetAspect(this->min_x
, this->min_y
);
2685 this->SetFill(0, 0);
2686 this->SetMinimalSize(WidgetDimensions::WD_DEBUGBOX_WIDTH
, WidgetDimensions::WD_CAPTION_HEIGHT
);
2687 this->SetDataTip(STR_NULL
, STR_TOOLTIP_DEBUG
);
2688 this->SetAspect(this->min_x
, this->min_y
);
2691 case WWT_DEFSIZEBOX
:
2692 this->SetFill(0, 0);
2693 this->SetMinimalSize(WidgetDimensions::WD_DEFSIZEBOX_WIDTH
, WidgetDimensions::WD_CAPTION_HEIGHT
);
2694 this->SetDataTip(STR_NULL
, STR_TOOLTIP_DEFSIZE
);
2695 this->SetAspect(this->min_x
, this->min_y
);
2699 this->SetFill(0, 0);
2700 this->SetMinimalSize(WidgetDimensions::WD_RESIZEBOX_WIDTH
, 12);
2701 this->SetDataTip(RWV_SHOW_BEVEL
, STR_TOOLTIP_RESIZE
);
2705 this->SetFill(0, 0);
2706 this->SetMinimalSize(WidgetDimensions::WD_CLOSEBOX_WIDTH
, WidgetDimensions::WD_CAPTION_HEIGHT
);
2707 this->SetDataTip(STR_NULL
, STR_TOOLTIP_CLOSE_WINDOW
);
2708 this->SetAspect(this->min_x
, this->min_y
);
2712 this->SetFill(0, 0);
2713 this->SetMinimalSize(0, WidgetDimensions::WD_DROPDOWN_HEIGHT
);
2714 this->SetAlignment(SA_TOP
| SA_LEFT
);
2722 void NWidgetLeaf::SetupSmallestSize(Window
*w
)
2724 Dimension padding
= {0, 0};
2725 Dimension size
= {this->min_x
, this->min_y
};
2726 Dimension fill
= {this->fill_x
, this->fill_y
};
2727 Dimension resize
= {this->resize_x
, this->resize_y
};
2728 switch (this->type
) {
2733 padding
= {WidgetDimensions::scaled
.matrix
.Horizontal(), WidgetDimensions::scaled
.matrix
.Vertical()};
2736 case WWT_SHADEBOX
: {
2737 padding
= {WidgetDimensions::scaled
.shadebox
.Horizontal(), WidgetDimensions::scaled
.shadebox
.Vertical()};
2738 if (NWidgetLeaf::shadebox_dimension
.width
== 0) {
2739 NWidgetLeaf::shadebox_dimension
= maxdim(GetScaledSpriteSize(SPR_WINDOW_SHADE
), GetScaledSpriteSize(SPR_WINDOW_UNSHADE
));
2740 NWidgetLeaf::shadebox_dimension
.width
+= padding
.width
;
2741 NWidgetLeaf::shadebox_dimension
.height
+= padding
.height
;
2743 size
= maxdim(size
, NWidgetLeaf::shadebox_dimension
);
2747 if (_settings_client
.gui
.newgrf_developer_tools
&& w
->IsNewGRFInspectable()) {
2748 padding
= {WidgetDimensions::scaled
.debugbox
.Horizontal(), WidgetDimensions::scaled
.debugbox
.Vertical()};
2749 if (NWidgetLeaf::debugbox_dimension
.width
== 0) {
2750 NWidgetLeaf::debugbox_dimension
= GetScaledSpriteSize(SPR_WINDOW_DEBUG
);
2751 NWidgetLeaf::debugbox_dimension
.width
+= padding
.width
;
2752 NWidgetLeaf::debugbox_dimension
.height
+= padding
.height
;
2754 size
= maxdim(size
, NWidgetLeaf::debugbox_dimension
);
2756 /* If the setting is disabled we don't want to see it! */
2763 case WWT_STICKYBOX
: {
2764 padding
= {WidgetDimensions::scaled
.stickybox
.Horizontal(), WidgetDimensions::scaled
.stickybox
.Vertical()};
2765 if (NWidgetLeaf::stickybox_dimension
.width
== 0) {
2766 NWidgetLeaf::stickybox_dimension
= maxdim(GetScaledSpriteSize(SPR_PIN_UP
), GetScaledSpriteSize(SPR_PIN_DOWN
));
2767 NWidgetLeaf::stickybox_dimension
.width
+= padding
.width
;
2768 NWidgetLeaf::stickybox_dimension
.height
+= padding
.height
;
2770 size
= maxdim(size
, NWidgetLeaf::stickybox_dimension
);
2774 case WWT_DEFSIZEBOX
: {
2775 padding
= {WidgetDimensions::scaled
.defsizebox
.Horizontal(), WidgetDimensions::scaled
.defsizebox
.Vertical()};
2776 if (NWidgetLeaf::defsizebox_dimension
.width
== 0) {
2777 NWidgetLeaf::defsizebox_dimension
= GetScaledSpriteSize(SPR_WINDOW_DEFSIZE
);
2778 NWidgetLeaf::defsizebox_dimension
.width
+= padding
.width
;
2779 NWidgetLeaf::defsizebox_dimension
.height
+= padding
.height
;
2781 size
= maxdim(size
, NWidgetLeaf::defsizebox_dimension
);
2785 case WWT_RESIZEBOX
: {
2786 padding
= {WidgetDimensions::scaled
.resizebox
.Horizontal(), WidgetDimensions::scaled
.resizebox
.Vertical()};
2787 if (NWidgetLeaf::resizebox_dimension
.width
== 0) {
2788 NWidgetLeaf::resizebox_dimension
= maxdim(GetScaledSpriteSize(SPR_WINDOW_RESIZE_LEFT
), GetScaledSpriteSize(SPR_WINDOW_RESIZE_RIGHT
));
2789 NWidgetLeaf::resizebox_dimension
.width
+= padding
.width
;
2790 NWidgetLeaf::resizebox_dimension
.height
+= padding
.height
;
2792 size
= maxdim(size
, NWidgetLeaf::resizebox_dimension
);
2796 Dimension sprite_size
= GetScaledSpriteSize(_current_text_dir
== TD_RTL
? SPR_IMG_DELETE_RIGHT
: SPR_IMG_DELETE_LEFT
);
2797 size
.width
= std::max(size
.width
, ScaleGUITrad(30) + sprite_size
.width
);
2798 size
.height
= std::max(sprite_size
.height
, GetStringBoundingBox("_").height
+ WidgetDimensions::scaled
.framerect
.Vertical());
2802 padding
= {WidgetDimensions::scaled
.frametext
.Horizontal(), WidgetDimensions::scaled
.framerect
.Vertical()};
2807 case WWT_PUSHIMGBTN
: {
2808 padding
= {WidgetDimensions::scaled
.imgbtn
.Horizontal(), WidgetDimensions::scaled
.imgbtn
.Vertical()};
2809 Dimension d2
= GetScaledSpriteSize(this->widget_data
);
2810 if (this->type
== WWT_IMGBTN_2
) d2
= maxdim(d2
, GetScaledSpriteSize(this->widget_data
+ 1));
2811 d2
.width
+= padding
.width
;
2812 d2
.height
+= padding
.height
;
2813 size
= maxdim(size
, d2
);
2817 case WWT_PUSHARROWBTN
: {
2818 padding
= {WidgetDimensions::scaled
.imgbtn
.Horizontal(), WidgetDimensions::scaled
.imgbtn
.Vertical()};
2819 Dimension d2
= maxdim(GetScaledSpriteSize(SPR_ARROW_LEFT
), GetScaledSpriteSize(SPR_ARROW_RIGHT
));
2820 d2
.width
+= padding
.width
;
2821 d2
.height
+= padding
.height
;
2822 size
= maxdim(size
, d2
);
2826 case WWT_CLOSEBOX
: {
2827 padding
= {WidgetDimensions::scaled
.closebox
.Horizontal(), WidgetDimensions::scaled
.closebox
.Vertical()};
2828 if (NWidgetLeaf::closebox_dimension
.width
== 0) {
2829 NWidgetLeaf::closebox_dimension
= GetScaledSpriteSize(SPR_CLOSEBOX
);
2830 NWidgetLeaf::closebox_dimension
.width
+= padding
.width
;
2831 NWidgetLeaf::closebox_dimension
.height
+= padding
.height
;
2833 size
= maxdim(size
, NWidgetLeaf::closebox_dimension
);
2837 case WWT_PUSHTXTBTN
:
2838 case WWT_TEXTBTN_2
: {
2839 padding
= {WidgetDimensions::scaled
.framerect
.Horizontal(), WidgetDimensions::scaled
.framerect
.Vertical()};
2840 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2841 Dimension d2
= GetStringBoundingBox(this->widget_data
, this->text_size
);
2842 d2
.width
+= padding
.width
;
2843 d2
.height
+= padding
.height
;
2844 size
= maxdim(size
, d2
);
2849 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2850 size
= maxdim(size
, GetStringBoundingBox(this->widget_data
, this->text_size
));
2854 padding
= {WidgetDimensions::scaled
.captiontext
.Horizontal(), WidgetDimensions::scaled
.captiontext
.Vertical()};
2855 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2856 Dimension d2
= GetStringBoundingBox(this->widget_data
, this->text_size
);
2857 d2
.width
+= padding
.width
;
2858 d2
.height
+= padding
.height
;
2859 size
= maxdim(size
, d2
);
2863 case NWID_BUTTON_DROPDOWN
:
2864 case NWID_PUSHBUTTON_DROPDOWN
: {
2865 if (NWidgetLeaf::dropdown_dimension
.width
== 0) {
2866 NWidgetLeaf::dropdown_dimension
= GetScaledSpriteSize(SPR_ARROW_DOWN
);
2867 NWidgetLeaf::dropdown_dimension
.width
+= WidgetDimensions::scaled
.vscrollbar
.Horizontal();
2868 NWidgetLeaf::dropdown_dimension
.height
+= WidgetDimensions::scaled
.vscrollbar
.Vertical();
2870 padding
= {WidgetDimensions::scaled
.dropdowntext
.Horizontal() + NWidgetLeaf::dropdown_dimension
.width
+ WidgetDimensions::scaled
.fullbevel
.Horizontal(), WidgetDimensions::scaled
.dropdowntext
.Vertical()};
2871 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2872 Dimension d2
= GetStringBoundingBox(this->widget_data
, this->text_size
);
2873 d2
.width
+= padding
.width
;
2874 d2
.height
= std::max(d2
.height
+ padding
.height
, NWidgetLeaf::dropdown_dimension
.height
);
2875 size
= maxdim(size
, d2
);
2882 if (this->index
>= 0) w
->UpdateWidgetSize(this->index
, size
, padding
, fill
, resize
);
2884 this->smallest_x
= size
.width
;
2885 this->smallest_y
= size
.height
;
2886 this->fill_x
= fill
.width
;
2887 this->fill_y
= fill
.height
;
2888 this->resize_x
= resize
.width
;
2889 this->resize_y
= resize
.height
;
2890 this->ApplyAspectRatio();
2893 void NWidgetLeaf::Draw(const Window
*w
)
2895 if (this->current_x
== 0 || this->current_y
== 0) return;
2897 /* Setup a clipping rectangle... for WWT_EMPTY or WWT_TEXT, an extra scaled pixel is allowed in case text shadow encroaches. */
2898 int extra
= (this->type
== WWT_EMPTY
|| this->type
== WWT_TEXT
) ? ScaleGUITrad(1) : 0;
2899 DrawPixelInfo new_dpi
;
2900 if (!FillDrawPixelInfo(&new_dpi
, this->pos_x
, this->pos_y
, this->current_x
+ extra
, this->current_y
+ extra
)) return;
2901 /* ...but keep coordinates relative to the window. */
2902 new_dpi
.left
+= this->pos_x
;
2903 new_dpi
.top
+= this->pos_y
;
2905 AutoRestoreBackup
dpi_backup(_cur_dpi
, &new_dpi
);
2907 Rect r
= this->GetCurrentRect();
2909 bool clicked
= this->IsLowered();
2910 switch (this->type
) {
2912 /* WWT_EMPTY used as a spacer indicates a potential design issue. */
2913 if (this->index
== -1 && _draw_widget_outlines
) {
2914 GfxFillRect(r
, PC_BLACK
, FILLRECT_CHECKER
);
2919 assert(this->widget_data
== 0);
2920 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, this->colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
2924 case WWT_PUSHIMGBTN
:
2926 DrawImageButtons(r
, this->type
, this->colour
, clicked
, this->widget_data
, this->align
);
2930 case WWT_PUSHTXTBTN
:
2932 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2933 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, this->colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
2934 DrawLabel(r
, this->type
, clicked
, this->text_colour
, this->widget_data
, this->align
, this->text_size
);
2938 case WWT_PUSHARROWBTN
: {
2940 switch (this->widget_data
) {
2941 case AWV_DECREASE
: sprite
= _current_text_dir
!= TD_RTL
? SPR_ARROW_LEFT
: SPR_ARROW_RIGHT
; break;
2942 case AWV_INCREASE
: sprite
= _current_text_dir
== TD_RTL
? SPR_ARROW_LEFT
: SPR_ARROW_RIGHT
; break;
2943 case AWV_LEFT
: sprite
= SPR_ARROW_LEFT
; break;
2944 case AWV_RIGHT
: sprite
= SPR_ARROW_RIGHT
; break;
2945 default: NOT_REACHED();
2947 DrawImageButtons(r
, WWT_PUSHIMGBTN
, this->colour
, clicked
, sprite
, this->align
);
2952 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2953 DrawLabel(r
, this->type
, clicked
, this->text_colour
, this->widget_data
, this->align
, this->text_size
);
2957 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2958 DrawText(r
, this->text_colour
, this->widget_data
, this->align
, this->text_size
);
2962 DrawMatrix(r
, this->colour
, clicked
, this->widget_data
, this->resize_x
, this->resize_y
);
2966 const QueryString
*query
= w
->GetQueryString(this->index
);
2967 if (query
!= nullptr) query
->DrawEditBox(w
, this->index
);
2972 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2973 DrawCaption(r
, this->colour
, w
->owner
, this->text_colour
, this->widget_data
, this->align
, this->text_size
);
2977 assert(this->widget_data
== 0);
2978 DrawShadeBox(r
, this->colour
, w
->IsShaded());
2982 DrawDebugBox(r
, this->colour
, clicked
);
2986 assert(this->widget_data
== 0);
2987 DrawStickyBox(r
, this->colour
, !!(w
->flags
& WF_STICKY
));
2990 case WWT_DEFSIZEBOX
:
2991 assert(this->widget_data
== 0);
2992 DrawDefSizeBox(r
, this->colour
, clicked
);
2996 DrawResizeBox(r
, this->colour
, this->pos_x
< (w
->width
/ 2), !!(w
->flags
& WF_SIZING
), this->widget_data
== 0);
3000 DrawCloseBox(r
, this->colour
);
3004 if (this->index
>= 0) w
->SetStringParameters(this->index
);
3005 DrawButtonDropdown(r
, this->colour
, false, clicked
, this->widget_data
, this->align
);
3008 case NWID_BUTTON_DROPDOWN
:
3009 case NWID_PUSHBUTTON_DROPDOWN
:
3010 if (this->index
>= 0) w
->SetStringParameters(this->index
);
3011 DrawButtonDropdown(r
, this->colour
, clicked
, (this->disp_flags
& ND_DROPDOWN_ACTIVE
) != 0, this->widget_data
, this->align
);
3017 if (this->index
>= 0) w
->DrawWidget(r
, this->index
);
3019 if (this->IsDisabled()) {
3020 GfxFillRect(r
.Shrink(WidgetDimensions::scaled
.bevel
), GetColourGradient(this->colour
, SHADE_DARKER
), FILLRECT_CHECKER
);
3023 DrawOutline(w
, this);
3027 * For a #NWID_BUTTON_DROPDOWN, test whether \a pt refers to the button or to the drop-down.
3028 * @param pt Point in the widget.
3029 * @return The point refers to the button.
3031 * @note The magic constants are also used at #DrawButtonDropdown.
3033 bool NWidgetLeaf::ButtonHit(const Point
&pt
)
3035 if (_current_text_dir
== TD_LTR
) {
3036 int button_width
= this->pos_x
+ this->current_x
- NWidgetLeaf::dropdown_dimension
.width
;
3037 return pt
.x
< button_width
;
3039 int button_left
= this->pos_x
+ NWidgetLeaf::dropdown_dimension
.width
;
3040 return pt
.x
>= button_left
;
3044 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
3047 * Test if (an NWidgetPart) WidgetType is an attribute widget part type.
3048 * @param tp WidgetType to test.
3049 * @return True iff WidgetType is an attribute widget.
3051 static bool IsAttributeWidgetPartType(WidgetType tp
)
3053 return tp
> WPT_ATTRIBUTE_BEGIN
&& tp
< WPT_ATTRIBUTE_END
;
3057 * Apply an attribute NWidgetPart to an NWidget.
3058 * @param nwid Attribute NWidgetPart
3059 * @param dest NWidget to apply attribute to.
3060 * @pre NWidgetPart must be an attribute NWidgetPart.
3062 static void ApplyNWidgetPartAttribute(const NWidgetPart
&nwid
, NWidgetBase
*dest
)
3064 switch (nwid
.type
) {
3066 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(dest
);
3067 if (nwrb
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_RESIZE requires NWidgetResizeBase");
3068 assert(nwid
.u
.xy
.x
>= 0 && nwid
.u
.xy
.y
>= 0);
3069 nwrb
->SetResize(nwid
.u
.xy
.x
, nwid
.u
.xy
.y
);
3074 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(dest
);
3075 if (nwrb
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_MINSIZE requires NWidgetResizeBase");
3076 assert(nwid
.u
.xy
.x
>= 0 && nwid
.u
.xy
.y
>= 0);
3077 nwrb
->SetMinimalSize(nwid
.u
.xy
.x
, nwid
.u
.xy
.y
);
3081 case WPT_MINTEXTLINES
: {
3082 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(dest
);
3083 if (nwrb
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_MINTEXTLINES requires NWidgetResizeBase");
3084 assert(nwid
.u
.text_lines
.size
>= FS_BEGIN
&& nwid
.u
.text_lines
.size
< FS_END
);
3085 nwrb
->SetMinimalTextLines(nwid
.u
.text_lines
.lines
, nwid
.u
.text_lines
.spacing
, nwid
.u
.text_lines
.size
);
3089 case WPT_TEXTSTYLE
: {
3090 NWidgetCore
*nwc
= dynamic_cast<NWidgetCore
*>(dest
);
3091 if (nwc
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_TEXTSTYLE requires NWidgetCore");
3092 nwc
->SetTextStyle(nwid
.u
.text_style
.colour
, nwid
.u
.text_style
.size
);
3096 case WPT_ALIGNMENT
: {
3097 NWidgetCore
*nwc
= dynamic_cast<NWidgetCore
*>(dest
);
3098 if (nwc
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_ALIGNMENT requires NWidgetCore");
3099 nwc
->SetAlignment(nwid
.u
.align
.align
);
3104 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(dest
);
3105 if (nwrb
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_FILL requires NWidgetResizeBase");
3106 nwrb
->SetFill(nwid
.u
.xy
.x
, nwid
.u
.xy
.y
);
3111 NWidgetCore
*nwc
= dynamic_cast<NWidgetCore
*>(dest
);
3112 if (nwc
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_DATATIP requires NWidgetCore");
3113 nwc
->widget_data
= nwid
.u
.data_tip
.data
;
3114 nwc
->tool_tip
= nwid
.u
.data_tip
.tooltip
;
3119 if (dest
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_PADDING requires NWidgetBase");
3120 dest
->SetPadding(nwid
.u
.padding
);
3123 case WPT_PIPSPACE
: {
3124 NWidgetPIPContainer
*nwc
= dynamic_cast<NWidgetPIPContainer
*>(dest
);
3125 if (nwc
!= nullptr) nwc
->SetPIP(nwid
.u
.pip
.pre
, nwid
.u
.pip
.inter
, nwid
.u
.pip
.post
);
3127 NWidgetBackground
*nwb
= dynamic_cast<NWidgetBackground
*>(dest
);
3128 if (nwb
!= nullptr) nwb
->SetPIP(nwid
.u
.pip
.pre
, nwid
.u
.pip
.inter
, nwid
.u
.pip
.post
);
3130 if (nwc
== nullptr && nwb
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_PIPSPACE requires NWidgetPIPContainer or NWidgetBackground");
3134 case WPT_PIPRATIO
: {
3135 NWidgetPIPContainer
*nwc
= dynamic_cast<NWidgetPIPContainer
*>(dest
);
3136 if (nwc
!= nullptr) nwc
->SetPIPRatio(nwid
.u
.pip
.pre
, nwid
.u
.pip
.inter
, nwid
.u
.pip
.post
);
3138 NWidgetBackground
*nwb
= dynamic_cast<NWidgetBackground
*>(dest
);
3139 if (nwb
!= nullptr) nwb
->SetPIPRatio(nwid
.u
.pip
.pre
, nwid
.u
.pip
.inter
, nwid
.u
.pip
.post
);
3141 if (nwc
== nullptr && nwb
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_PIPRATIO requires NWidgetPIPContainer or NWidgetBackground");
3145 case WPT_SCROLLBAR
: {
3146 NWidgetCore
*nwc
= dynamic_cast<NWidgetCore
*>(dest
);
3147 if (nwc
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_SCROLLBAR requires NWidgetCore");
3148 nwc
->scrollbar_index
= nwid
.u
.widget
.index
;
3153 if (dest
== nullptr) [[unlikely
]] throw std::runtime_error("WPT_ASPECT requires NWidgetBase");
3154 dest
->aspect_ratio
= nwid
.u
.aspect
.ratio
;
3155 dest
->aspect_flags
= nwid
.u
.aspect
.flags
;
3165 * Make NWidget from an NWidgetPart.
3166 * @param nwid NWidgetPart.
3167 * @pre NWidgetPart must not be an attribute NWidgetPart nor WPT_ENDCONTAINER.
3168 * @return Pointer to created NWidget.
3170 static std::unique_ptr
<NWidgetBase
> MakeNWidget(const NWidgetPart
&nwid
)
3172 assert(!IsAttributeWidgetPartType(nwid
.type
));
3173 assert(nwid
.type
!= WPT_ENDCONTAINER
);
3175 switch (nwid
.type
) {
3176 case NWID_SPACER
: return std::make_unique
<NWidgetSpacer
>(0, 0);
3178 case WWT_PANEL
: [[fallthrough
]];
3179 case WWT_INSET
: [[fallthrough
]];
3180 case WWT_FRAME
: return std::make_unique
<NWidgetBackground
>(nwid
.type
, nwid
.u
.widget
.colour
, nwid
.u
.widget
.index
);
3182 case NWID_HORIZONTAL
: return std::make_unique
<NWidgetHorizontal
>(nwid
.u
.cont_flags
);
3183 case NWID_HORIZONTAL_LTR
: return std::make_unique
<NWidgetHorizontalLTR
>(nwid
.u
.cont_flags
);
3184 case NWID_VERTICAL
: return std::make_unique
<NWidgetVertical
>(nwid
.u
.cont_flags
);
3185 case NWID_SELECTION
: return std::make_unique
<NWidgetStacked
>(nwid
.u
.widget
.index
);
3186 case NWID_MATRIX
: return std::make_unique
<NWidgetMatrix
>(nwid
.u
.widget
.colour
, nwid
.u
.widget
.index
);
3187 case NWID_VIEWPORT
: return std::make_unique
<NWidgetViewport
>(nwid
.u
.widget
.index
);
3188 case NWID_LAYER
: return std::make_unique
<NWidgetLayer
>(nwid
.u
.widget
.index
);
3190 case NWID_HSCROLLBAR
: [[fallthrough
]];
3191 case NWID_VSCROLLBAR
: return std::make_unique
<NWidgetScrollbar
>(nwid
.type
, nwid
.u
.widget
.colour
, nwid
.u
.widget
.index
);
3193 case WPT_FUNCTION
: return nwid
.u
.func_ptr();
3196 assert((nwid
.type
& WWT_MASK
) < WWT_LAST
|| (nwid
.type
& WWT_MASK
) == NWID_BUTTON_DROPDOWN
);
3197 return std::make_unique
<NWidgetLeaf
>(nwid
.type
, nwid
.u
.widget
.colour
, nwid
.u
.widget
.index
, 0x0, STR_NULL
);
3202 * Construct a single nested widget in \a *dest from its parts.
3204 * Construct a NWidgetBase object from a #NWidget function, and apply all
3205 * attributes that follow it, until encountering a #EndContainer, another
3206 * #NWidget, or the end of the parts array.
3208 * @param nwid_begin Iterator to beginning of nested widget parts.
3209 * @param nwid_end Iterator to ending of nested widget parts.
3210 * @param[out] dest Address of pointer to use for returning the composed widget.
3211 * @param[out] fill_dest Fill the composed widget with child widgets.
3212 * @return Iterator to remaining nested widget parts.
3214 static std::span
<const NWidgetPart
>::iterator
MakeNWidget(std::span
<const NWidgetPart
>::iterator nwid_begin
, std::span
<const NWidgetPart
>::iterator nwid_end
, std::unique_ptr
<NWidgetBase
> &dest
, bool &fill_dest
)
3218 if (IsAttributeWidgetPartType(nwid_begin
->type
)) [[unlikely
]] throw std::runtime_error("Expected non-attribute NWidgetPart type");
3219 if (nwid_begin
->type
== WPT_ENDCONTAINER
) return nwid_begin
;
3221 fill_dest
= IsContainerWidgetType(nwid_begin
->type
);
3222 dest
= MakeNWidget(*nwid_begin
);
3223 if (dest
== nullptr) return nwid_begin
;
3227 /* Once a widget is created, we're now looking for attributes. */
3228 while (nwid_begin
!= nwid_end
&& IsAttributeWidgetPartType(nwid_begin
->type
)) {
3229 ApplyNWidgetPartAttribute(*nwid_begin
, dest
.get());
3237 * Test if WidgetType is a container widget.
3238 * @param tp WidgetType to test.
3239 * @return True iff WidgetType is a container widget.
3241 bool IsContainerWidgetType(WidgetType tp
)
3243 return tp
== NWID_HORIZONTAL
|| tp
== NWID_HORIZONTAL_LTR
|| tp
== NWID_VERTICAL
|| tp
== NWID_MATRIX
3244 || tp
== WWT_PANEL
|| tp
== WWT_FRAME
|| tp
== WWT_INSET
|| tp
== NWID_SELECTION
|| tp
== NWID_LAYER
;
3248 * Build a nested widget tree by recursively filling containers with nested widgets read from their parts.
3249 * @param nwid_begin Iterator to beginning of nested widget parts.
3250 * @param nwid_end Iterator to ending of nested widget parts.
3251 * @param parent Pointer or container to use for storing the child widgets (*parent == nullptr or *parent == container or background widget).
3252 * @return Iterator to remaining nested widget parts.
3254 static std::span
<const NWidgetPart
>::iterator
MakeWidgetTree(std::span
<const NWidgetPart
>::iterator nwid_begin
, std::span
<const NWidgetPart
>::iterator nwid_end
, std::unique_ptr
<NWidgetBase
> &parent
)
3256 /* If *parent == nullptr, only the first widget is read and returned. Otherwise, *parent must point to either
3257 * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
3258 NWidgetContainer
*nwid_cont
= dynamic_cast<NWidgetContainer
*>(parent
.get());
3259 NWidgetBackground
*nwid_parent
= dynamic_cast<NWidgetBackground
*>(parent
.get());
3260 assert(parent
== nullptr || (nwid_cont
!= nullptr && nwid_parent
== nullptr) || (nwid_cont
== nullptr && nwid_parent
!= nullptr));
3262 while (nwid_begin
!= nwid_end
) {
3263 std::unique_ptr
<NWidgetBase
> sub_widget
= nullptr;
3264 bool fill_sub
= false;
3265 nwid_begin
= MakeNWidget(nwid_begin
, nwid_end
, sub_widget
, fill_sub
);
3267 /* Break out of loop when end reached */
3268 if (sub_widget
== nullptr) break;
3270 /* If sub-widget is a container, recursively fill that container. */
3271 if (fill_sub
&& IsContainerWidgetType(sub_widget
->type
)) {
3272 nwid_begin
= MakeWidgetTree(nwid_begin
, nwid_end
, sub_widget
);
3275 /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
3276 if (nwid_cont
!= nullptr) nwid_cont
->Add(std::move(sub_widget
));
3277 if (nwid_parent
!= nullptr) nwid_parent
->Add(std::move(sub_widget
));
3278 if (nwid_cont
== nullptr && nwid_parent
== nullptr) {
3279 parent
= std::move(sub_widget
);
3284 if (nwid_begin
== nwid_end
) return nwid_begin
; // Reached the end of the array of parts?
3286 assert(nwid_begin
< nwid_end
);
3287 assert(nwid_begin
->type
== WPT_ENDCONTAINER
);
3288 return std::next(nwid_begin
); // *nwid_begin is also 'used'
3292 * Construct a nested widget tree from an array of parts.
3293 * @param nwid_parts Span of nested widget parts.
3294 * @param container Container to add the nested widgets to. In case it is nullptr a vertical container is used.
3295 * @return Root of the nested widget tree, a vertical container containing the entire GUI.
3296 * @ingroup NestedWidgetParts
3298 std::unique_ptr
<NWidgetBase
> MakeNWidgets(std::span
<const NWidgetPart
> nwid_parts
, std::unique_ptr
<NWidgetBase
> &&container
)
3300 if (container
== nullptr) container
= std::make_unique
<NWidgetVertical
>();
3301 [[maybe_unused
]] auto nwid_part
= MakeWidgetTree(std::begin(nwid_parts
), std::end(nwid_parts
), container
);
3303 if (nwid_part
!= std::end(nwid_parts
)) [[unlikely
]] throw std::runtime_error("Did not consume all NWidgetParts");
3305 return std::move(container
);
3309 * Make a nested widget tree for a window from a parts array. Besides loading, it inserts a shading selection widget
3310 * between the title bar and the window body if the first widget in the parts array looks like a title bar (it is a horizontal
3311 * container with a caption widget) and has a shade box widget.
3312 * @param nwid_parts Span of nested widget parts.
3313 * @param[out] shade_select Pointer to the inserted shade selection widget (\c nullptr if not unserted).
3314 * @return Root of the nested widget tree, a vertical container containing the entire GUI.
3315 * @ingroup NestedWidgetParts
3317 std::unique_ptr
<NWidgetBase
> MakeWindowNWidgetTree(std::span
<const NWidgetPart
> nwid_parts
, NWidgetStacked
**shade_select
)
3319 auto nwid_begin
= std::begin(nwid_parts
);
3320 auto nwid_end
= std::end(nwid_parts
);
3322 *shade_select
= nullptr;
3324 /* Read the first widget recursively from the array. */
3325 std::unique_ptr
<NWidgetBase
> nwid
= nullptr;
3326 nwid_begin
= MakeWidgetTree(nwid_begin
, nwid_end
, nwid
);
3327 assert(nwid
!= nullptr);
3329 NWidgetHorizontal
*hor_cont
= dynamic_cast<NWidgetHorizontal
*>(nwid
.get());
3331 auto root
= std::make_unique
<NWidgetVertical
>();
3332 root
->Add(std::move(nwid
));
3333 if (nwid_begin
== nwid_end
) return root
; // There is no body at all.
3335 if (hor_cont
!= nullptr && hor_cont
->GetWidgetOfType(WWT_CAPTION
) != nullptr && hor_cont
->GetWidgetOfType(WWT_SHADEBOX
) != nullptr) {
3336 /* If the first widget has a title bar and a shade box, silently add a shade selection widget in the tree. */
3337 auto shade_stack
= std::make_unique
<NWidgetStacked
>(-1);
3338 *shade_select
= shade_stack
.get();
3339 /* Load the remaining parts into the shade stack. */
3340 shade_stack
->Add(MakeNWidgets({nwid_begin
, nwid_end
}, std::make_unique
<NWidgetVertical
>()));
3341 root
->Add(std::move(shade_stack
));
3345 /* Load the remaining parts into 'root'. */
3346 return MakeNWidgets({nwid_begin
, nwid_end
}, std::move(root
));
3350 * Make a number of rows with button-like graphics, for enabling/disabling each company.
3351 * @param widget_first The first widget index to use.
3352 * @param widget_last The last widget index to use.
3353 * @param colour The colour in which to draw the button.
3354 * @param max_length Maximal number of company buttons in one row.
3355 * @param button_tooltip The tooltip-string of every button.
3356 * @param resizable Whether the rows are resizable.
3357 * @return Panel with rows of company buttons.
3359 std::unique_ptr
<NWidgetBase
> MakeCompanyButtonRows(WidgetID widget_first
, WidgetID widget_last
, Colours button_colour
, int max_length
, StringID button_tooltip
, bool resizable
)
3361 assert(max_length
>= 1);
3362 std::unique_ptr
<NWidgetVertical
> vert
= nullptr; // Storage for all rows.
3363 std::unique_ptr
<NWidgetHorizontal
> hor
= nullptr; // Storage for buttons in one row.
3366 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
, nullptr, ZOOM_LVL_NORMAL
);
3367 sprite_size
.width
+= WidgetDimensions::unscaled
.matrix
.Horizontal();
3368 sprite_size
.height
+= WidgetDimensions::unscaled
.matrix
.Vertical();
3370 for (WidgetID widnum
= widget_first
; widnum
<= widget_last
; widnum
++) {
3371 /* Ensure there is room in 'hor' for another button. */
3372 if (hor_length
== max_length
) {
3373 if (vert
== nullptr) vert
= std::make_unique
<NWidgetVertical
>();
3374 vert
->Add(std::move(hor
));
3378 if (hor
== nullptr) {
3379 hor
= std::make_unique
<NWidgetHorizontal
>();
3383 auto panel
= std::make_unique
<NWidgetBackground
>(WWT_PANEL
, button_colour
, widnum
);
3384 panel
->SetMinimalSize(sprite_size
.width
, sprite_size
.height
);
3385 panel
->SetFill(1, 1);
3386 if (resizable
) panel
->SetResize(1, 0);
3387 panel
->SetDataTip(0x0, button_tooltip
);
3388 hor
->Add(std::move(panel
));
3391 if (vert
== nullptr) return hor
; // All buttons fit in a single row.
3393 if (hor_length
> 0 && hor_length
< max_length
) {
3394 /* Last row is partial, add a spacer at the end to force all buttons to the left. */
3395 auto spc
= std::make_unique
<NWidgetSpacer
>(sprite_size
.width
, sprite_size
.height
);
3397 if (resizable
) spc
->SetResize(1, 0);
3398 hor
->Add(std::move(spc
));
3400 if (hor
!= nullptr) vert
->Add(std::move(hor
));