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 "company_func.h"
12 #include "window_gui.h"
13 #include "viewport_func.h"
14 #include "zoom_func.h"
15 #include "strings_func.h"
16 #include "transparency.h"
17 #include "core/geometry_func.hpp"
18 #include "settings_type.h"
19 #include "querystring_gui.h"
21 #include "table/sprites.h"
22 #include "table/strings.h"
23 #include "table/string_colours.h"
25 #include "safeguards.h"
28 * Compute the vertical position of the draggable part of scrollbar
29 * @param sb Scrollbar list data
30 * @param top Top position of the scrollbar (top position of the up-button)
31 * @param bottom Bottom position of the scrollbar (bottom position of the down-button)
32 * @param horizontal Whether the scrollbar is horizontal or not
33 * @return A Point, with x containing the top coordinate of the draggable part, and
34 * y containing the bottom coordinate of the draggable part
36 static Point
HandleScrollbarHittest(const Scrollbar
*sb
, int top
, int bottom
, bool horizontal
)
38 /* Base for reversion */
39 int rev_base
= top
+ bottom
;
42 button_size
= NWidgetScrollbar::GetHorizontalDimension().width
;
44 button_size
= NWidgetScrollbar::GetVerticalDimension().height
;
46 top
+= button_size
; // top points to just below the up-button
47 bottom
-= button_size
; // bottom points to top of the down-button
49 int height
= (bottom
- top
);
50 int pos
= sb
->GetPosition();
51 int count
= sb
->GetCount();
52 int cap
= sb
->GetCapacity();
54 if (count
!= 0) top
+= height
* pos
/ count
;
56 if (cap
> count
) cap
= count
;
57 if (count
!= 0) bottom
-= (count
- pos
- cap
) * height
/ count
;
60 if (horizontal
&& _current_text_dir
== TD_RTL
) {
61 pt
.x
= rev_base
- bottom
;
62 pt
.y
= rev_base
- top
;
71 * Compute new position of the scrollbar after a click and updates the window flags.
72 * @param w Window on which a scroll was performed.
74 * @param mi Minimum coordinate of the scroll bar.
75 * @param ma Maximum coordinate of the scroll bar.
76 * @param x The X coordinate of the mouse click.
77 * @param y The Y coordinate of the mouse click.
79 static void ScrollbarClickPositioning(Window
*w
, NWidgetScrollbar
*sb
, int x
, int y
, int mi
, int ma
)
85 if (sb
->type
== NWID_HSCROLLBAR
) {
87 rtl
= _current_text_dir
== TD_RTL
;
88 button_size
= NWidgetScrollbar::GetHorizontalDimension().width
;
91 button_size
= NWidgetScrollbar::GetVerticalDimension().height
;
93 if (pos
< mi
+ button_size
) {
94 /* Pressing the upper button? */
95 SetBit(sb
->disp_flags
, NDB_SCROLLBAR_UP
);
96 if (_scroller_click_timeout
<= 1) {
97 _scroller_click_timeout
= 3;
98 sb
->UpdatePosition(rtl
? 1 : -1);
100 w
->mouse_capture_widget
= sb
->index
;
101 } else if (pos
>= ma
- button_size
) {
102 /* Pressing the lower button? */
103 SetBit(sb
->disp_flags
, NDB_SCROLLBAR_DOWN
);
105 if (_scroller_click_timeout
<= 1) {
106 _scroller_click_timeout
= 3;
107 sb
->UpdatePosition(rtl
? -1 : 1);
109 w
->mouse_capture_widget
= sb
->index
;
111 Point pt
= HandleScrollbarHittest(sb
, mi
, ma
, sb
->type
== NWID_HSCROLLBAR
);
114 sb
->UpdatePosition(rtl
? 1 : -1, Scrollbar::SS_BIG
);
115 } else if (pos
> pt
.y
) {
116 sb
->UpdatePosition(rtl
? -1 : 1, Scrollbar::SS_BIG
);
118 _scrollbar_start_pos
= pt
.x
- mi
- button_size
;
119 _scrollbar_size
= ma
- mi
- button_size
* 2;
120 w
->mouse_capture_widget
= sb
->index
;
121 _cursorpos_drag_start
= _cursor
.pos
;
129 * Special handling for the scrollbar widget type.
130 * Handles the special scrolling buttons and other scrolling.
131 * @param w Window on which a scroll was performed.
132 * @param nw Pointer to the scrollbar widget.
133 * @param x The X coordinate of the mouse click.
134 * @param y The Y coordinate of the mouse click.
136 void ScrollbarClickHandler(Window
*w
, NWidgetCore
*nw
, int x
, int y
)
140 if (nw
->type
== NWID_HSCROLLBAR
) {
142 ma
= nw
->pos_x
+ nw
->current_x
;
145 ma
= nw
->pos_y
+ nw
->current_y
;
147 NWidgetScrollbar
*scrollbar
= dynamic_cast<NWidgetScrollbar
*>(nw
);
148 assert(scrollbar
!= nullptr);
149 ScrollbarClickPositioning(w
, scrollbar
, x
, y
, mi
, ma
);
153 * Returns the index for the widget located at the given position
154 * relative to the window. It includes all widget-corner pixels as well.
155 * @param *w Window to look inside
156 * @param x The Window client X coordinate
157 * @param y The Window client y coordinate
158 * @return A widget index, or -1 if no widget was found.
160 int GetWidgetFromPos(const Window
*w
, int x
, int y
)
162 NWidgetCore
*nw
= w
->nested_root
->GetWidgetFromPos(x
, y
);
163 return (nw
!= nullptr) ? nw
->index
: -1;
167 * Draw frame rectangle.
168 * @param left Left edge of the frame
169 * @param top Top edge of the frame
170 * @param right Right edge of the frame
171 * @param bottom Bottom edge of the frame
172 * @param colour Colour table to use. @see _colour_gradient
173 * @param flags Flags controlling how to draw the frame. @see FrameFlags
175 void DrawFrameRect(int left
, int top
, int right
, int bottom
, Colours colour
, FrameFlags flags
)
177 assert(colour
< COLOUR_END
);
179 uint dark
= _colour_gradient
[colour
][3];
180 uint medium_dark
= _colour_gradient
[colour
][5];
181 uint medium_light
= _colour_gradient
[colour
][6];
182 uint light
= _colour_gradient
[colour
][7];
184 if (flags
& FR_TRANSPARENT
) {
185 GfxFillRect(left
, top
, right
, bottom
, PALETTE_TO_TRANSPARENT
, FILLRECT_RECOLOUR
);
189 if (flags
& FR_LOWERED
) {
190 GfxFillRect(left
, top
, left
, bottom
, dark
);
191 GfxFillRect(left
+ WD_BEVEL_LEFT
, top
, right
, top
, dark
);
192 GfxFillRect(right
, top
+ WD_BEVEL_TOP
, right
, bottom
- WD_BEVEL_BOTTOM
, light
);
193 GfxFillRect(left
+ WD_BEVEL_LEFT
, bottom
, right
, bottom
, light
);
194 interior
= (flags
& FR_DARKENED
? medium_dark
: medium_light
);
196 GfxFillRect(left
, top
, left
, bottom
- WD_BEVEL_BOTTOM
, light
);
197 GfxFillRect(left
+ WD_BEVEL_LEFT
, top
, right
- WD_BEVEL_RIGHT
, top
, light
);
198 GfxFillRect(right
, top
, right
, bottom
- WD_BEVEL_BOTTOM
, dark
);
199 GfxFillRect(left
, bottom
, right
, bottom
, dark
);
200 interior
= medium_dark
;
202 if (!(flags
& FR_BORDERONLY
)) {
203 GfxFillRect(left
+ WD_BEVEL_LEFT
, top
+ WD_BEVEL_TOP
, right
- WD_BEVEL_RIGHT
, bottom
- WD_BEVEL_BOTTOM
, interior
);
209 * Draw an image button.
210 * @param r Rectangle of the button.
211 * @param type Widget type (#WWT_IMGBTN or #WWT_IMGBTN_2).
212 * @param colour Colour of the button.
213 * @param clicked Button is lowered.
214 * @param img Sprite to draw.
216 static inline void DrawImageButtons(const Rect
&r
, WidgetType type
, Colours colour
, bool clicked
, SpriteID img
)
219 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
221 if ((type
& WWT_MASK
) == WWT_IMGBTN_2
&& clicked
) img
++; // Show different image when clicked for #WWT_IMGBTN_2.
222 Dimension d
= GetSpriteSize(img
);
223 DrawSprite(img
, PAL_NONE
, CenterBounds(r
.left
, r
.right
, d
.width
) + clicked
, CenterBounds(r
.top
, r
.bottom
, d
.height
) + clicked
);
227 * Draw the label-part of a widget.
228 * @param r Rectangle of the label background.
229 * @param type Widget type (#WWT_TEXTBTN, #WWT_TEXTBTN_2, or #WWT_LABEL).
230 * @param clicked Label is rendered lowered.
231 * @param str Text to draw.
233 static inline void DrawLabel(const Rect
&r
, WidgetType type
, bool clicked
, StringID str
)
235 if (str
== STR_NULL
) return;
236 if ((type
& WWT_MASK
) == WWT_TEXTBTN_2
&& clicked
) str
++;
237 Dimension d
= GetStringBoundingBox(str
);
238 int offset
= max(0, ((int)(r
.bottom
- r
.top
+ 1) - (int)d
.height
) / 2); // Offset for rendering the text vertically centered
239 DrawString(r
.left
+ clicked
, r
.right
+ clicked
, r
.top
+ offset
+ clicked
, str
, TC_FROMSTRING
, SA_HOR_CENTER
);
244 * @param r Rectangle of the background.
245 * @param colour Colour of the text.
246 * @param str Text to draw.
248 static inline void DrawText(const Rect
&r
, TextColour colour
, StringID str
)
250 Dimension d
= GetStringBoundingBox(str
);
251 int offset
= max(0, ((int)(r
.bottom
- r
.top
+ 1) - (int)d
.height
) / 2); // Offset for rendering the text vertically centered
252 if (str
!= STR_NULL
) DrawString(r
.left
, r
.right
, r
.top
+ offset
, str
, colour
);
256 * Draw an inset widget.
257 * @param r Rectangle of the background.
258 * @param colour Colour of the inset.
259 * @param str Text to draw.
261 static inline void DrawInset(const Rect
&r
, Colours colour
, StringID str
)
263 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, FR_LOWERED
| FR_DARKENED
);
264 if (str
!= STR_NULL
) DrawString(r
.left
+ WD_INSET_LEFT
, r
.right
- WD_INSET_RIGHT
, r
.top
+ WD_INSET_TOP
, str
);
268 * Draw a matrix widget.
269 * @param r Rectangle of the matrix background.
270 * @param colour Colour of the background.
271 * @param clicked Matrix is rendered lowered.
272 * @param data Data of the widget, number of rows and columns of the widget.
273 * @param resize_x Matrix resize unit size.
274 * @param resize_y Matrix resize unit size.
276 static inline void DrawMatrix(const Rect
&r
, Colours colour
, bool clicked
, uint16 data
, uint resize_x
, uint resize_y
)
278 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
280 int num_columns
= GB(data
, MAT_COL_START
, MAT_COL_BITS
); // Lower 8 bits of the widget data: Number of columns in the matrix.
281 int column_width
; // Width of a single column in the matrix.
282 if (num_columns
== 0) {
283 column_width
= resize_x
;
284 num_columns
= (r
.right
- r
.left
+ 1) / column_width
;
286 column_width
= (r
.right
- r
.left
+ 1) / num_columns
;
289 int num_rows
= GB(data
, MAT_ROW_START
, MAT_ROW_BITS
); // Upper 8 bits of the widget data: Number of rows in the matrix.
290 int row_height
; // Height of a single row in the matrix.
292 row_height
= resize_y
;
293 num_rows
= (r
.bottom
- r
.top
+ 1) / row_height
;
295 row_height
= (r
.bottom
- r
.top
+ 1) / num_rows
;
298 int col
= _colour_gradient
[colour
& 0xF][6];
301 for (int ctr
= num_columns
; ctr
> 1; ctr
--) {
303 GfxFillRect(x
, r
.top
+ 1, x
, r
.bottom
- 1, col
);
307 for (int ctr
= num_rows
; ctr
> 1; ctr
--) {
309 GfxFillRect(r
.left
+ 1, x
, r
.right
- 1, x
, col
);
312 col
= _colour_gradient
[colour
& 0xF][4];
315 for (int ctr
= num_columns
; ctr
> 1; ctr
--) {
317 GfxFillRect(x
, r
.top
+ 1, x
, r
.bottom
- 1, col
);
321 for (int ctr
= num_rows
; ctr
> 1; ctr
--) {
323 GfxFillRect(r
.left
+ 1, x
, r
.right
- 1, x
, col
);
328 * Draw a vertical scrollbar.
329 * @param r Rectangle of the scrollbar widget.
330 * @param colour Colour of the scrollbar widget.
331 * @param up_clicked Up-arrow is clicked.
332 * @param bar_dragged Bar is dragged.
333 * @param down_clicked Down-arrow is clicked.
334 * @param scrollbar Scrollbar size, offset, and capacity information.
336 static inline void DrawVerticalScrollbar(const Rect
&r
, Colours colour
, bool up_clicked
, bool bar_dragged
, bool down_clicked
, const Scrollbar
*scrollbar
)
338 int centre
= (r
.right
- r
.left
) / 2;
339 int height
= NWidgetScrollbar::GetVerticalDimension().height
;
341 /* draw up/down buttons */
342 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.top
+ height
- 1, colour
, (up_clicked
) ? FR_LOWERED
: FR_NONE
);
343 DrawSprite(SPR_ARROW_UP
, PAL_NONE
, r
.left
+ 1 + up_clicked
, r
.top
+ 1 + up_clicked
);
345 DrawFrameRect(r
.left
, r
.bottom
- (height
- 1), r
.right
, r
.bottom
, colour
, (down_clicked
) ? FR_LOWERED
: FR_NONE
);
346 DrawSprite(SPR_ARROW_DOWN
, PAL_NONE
, r
.left
+ 1 + down_clicked
, r
.bottom
- (height
- 2) + down_clicked
);
348 int c1
= _colour_gradient
[colour
& 0xF][3];
349 int c2
= _colour_gradient
[colour
& 0xF][7];
351 /* draw "shaded" background */
352 GfxFillRect(r
.left
, r
.top
+ height
, r
.right
, r
.bottom
- height
, c2
);
353 GfxFillRect(r
.left
, r
.top
+ height
, r
.right
, r
.bottom
- height
, c1
, FILLRECT_CHECKER
);
355 /* draw shaded lines */
356 GfxFillRect(r
.left
+ centre
- 3, r
.top
+ height
, r
.left
+ centre
- 3, r
.bottom
- height
, c1
);
357 GfxFillRect(r
.left
+ centre
- 2, r
.top
+ height
, r
.left
+ centre
- 2, r
.bottom
- height
, c2
);
358 GfxFillRect(r
.left
+ centre
+ 2, r
.top
+ height
, r
.left
+ centre
+ 2, r
.bottom
- height
, c1
);
359 GfxFillRect(r
.left
+ centre
+ 3, r
.top
+ height
, r
.left
+ centre
+ 3, r
.bottom
- height
, c2
);
361 Point pt
= HandleScrollbarHittest(scrollbar
, r
.top
, r
.bottom
, false);
362 DrawFrameRect(r
.left
, pt
.x
, r
.right
, pt
.y
, colour
, bar_dragged
? FR_LOWERED
: FR_NONE
);
366 * Draw a horizontal scrollbar.
367 * @param r Rectangle of the scrollbar widget.
368 * @param colour Colour of the scrollbar widget.
369 * @param left_clicked Left-arrow is clicked.
370 * @param bar_dragged Bar is dragged.
371 * @param right_clicked Right-arrow is clicked.
372 * @param scrollbar Scrollbar size, offset, and capacity information.
374 static inline void DrawHorizontalScrollbar(const Rect
&r
, Colours colour
, bool left_clicked
, bool bar_dragged
, bool right_clicked
, const Scrollbar
*scrollbar
)
376 int centre
= (r
.bottom
- r
.top
) / 2;
377 int width
= NWidgetScrollbar::GetHorizontalDimension().width
;
379 DrawFrameRect(r
.left
, r
.top
, r
.left
+ width
- 1, r
.bottom
, colour
, left_clicked
? FR_LOWERED
: FR_NONE
);
380 DrawSprite(SPR_ARROW_LEFT
, PAL_NONE
, r
.left
+ 1 + left_clicked
, r
.top
+ 1 + left_clicked
);
382 DrawFrameRect(r
.right
- (width
- 1), r
.top
, r
.right
, r
.bottom
, colour
, right_clicked
? FR_LOWERED
: FR_NONE
);
383 DrawSprite(SPR_ARROW_RIGHT
, PAL_NONE
, r
.right
- (width
- 2) + right_clicked
, r
.top
+ 1 + right_clicked
);
385 int c1
= _colour_gradient
[colour
& 0xF][3];
386 int c2
= _colour_gradient
[colour
& 0xF][7];
388 /* draw "shaded" background */
389 GfxFillRect(r
.left
+ width
, r
.top
, r
.right
- width
, r
.bottom
, c2
);
390 GfxFillRect(r
.left
+ width
, r
.top
, r
.right
- width
, r
.bottom
, c1
, FILLRECT_CHECKER
);
392 /* draw shaded lines */
393 GfxFillRect(r
.left
+ width
, r
.top
+ centre
- 3, r
.right
- width
, r
.top
+ centre
- 3, c1
);
394 GfxFillRect(r
.left
+ width
, r
.top
+ centre
- 2, r
.right
- width
, r
.top
+ centre
- 2, c2
);
395 GfxFillRect(r
.left
+ width
, r
.top
+ centre
+ 2, r
.right
- width
, r
.top
+ centre
+ 2, c1
);
396 GfxFillRect(r
.left
+ width
, r
.top
+ centre
+ 3, r
.right
- width
, r
.top
+ centre
+ 3, c2
);
398 /* draw actual scrollbar */
399 Point pt
= HandleScrollbarHittest(scrollbar
, r
.left
, r
.right
, true);
400 DrawFrameRect(pt
.x
, r
.top
, pt
.y
, r
.bottom
, colour
, bar_dragged
? FR_LOWERED
: FR_NONE
);
404 * Draw a frame widget.
405 * @param r Rectangle of the frame.
406 * @param colour Colour of the frame.
407 * @param str Text of the frame.
409 static inline void DrawFrame(const Rect
&r
, Colours colour
, StringID str
)
411 int x2
= r
.left
; // by default the left side is the left side of the widget
413 if (str
!= STR_NULL
) x2
= DrawString(r
.left
+ WD_FRAMETEXT_LEFT
, r
.right
- WD_FRAMETEXT_RIGHT
, r
.top
, str
);
415 int c1
= _colour_gradient
[colour
][3];
416 int c2
= _colour_gradient
[colour
][7];
418 /* If the frame has text, adjust the top bar to fit half-way through */
420 if (str
!= STR_NULL
) dy1
= FONT_HEIGHT_NORMAL
/ 2 - 1;
423 if (_current_text_dir
== TD_LTR
) {
424 /* Line from upper left corner to start of text */
425 GfxFillRect(r
.left
, r
.top
+ dy1
, r
.left
+ 4, r
.top
+ dy1
, c1
);
426 GfxFillRect(r
.left
+ 1, r
.top
+ dy2
, r
.left
+ 4, r
.top
+ dy2
, c2
);
428 /* Line from end of text to upper right corner */
429 GfxFillRect(x2
, r
.top
+ dy1
, r
.right
- 1, r
.top
+ dy1
, c1
);
430 GfxFillRect(x2
, r
.top
+ dy2
, r
.right
- 2, r
.top
+ dy2
, c2
);
432 /* Line from upper left corner to start of text */
433 GfxFillRect(r
.left
, r
.top
+ dy1
, x2
- 2, r
.top
+ dy1
, c1
);
434 GfxFillRect(r
.left
+ 1, r
.top
+ dy2
, x2
- 2, r
.top
+ dy2
, c2
);
436 /* Line from end of text to upper right corner */
437 GfxFillRect(r
.right
- 5, r
.top
+ dy1
, r
.right
- 1, r
.top
+ dy1
, c1
);
438 GfxFillRect(r
.right
- 5, r
.top
+ dy2
, r
.right
- 2, r
.top
+ dy2
, c2
);
441 /* Line from upper left corner to bottom left corner */
442 GfxFillRect(r
.left
, r
.top
+ dy2
, r
.left
, r
.bottom
- 1, c1
);
443 GfxFillRect(r
.left
+ 1, r
.top
+ dy2
+ 1, r
.left
+ 1, r
.bottom
- 2, c2
);
445 /* Line from upper right corner to bottom right corner */
446 GfxFillRect(r
.right
- 1, r
.top
+ dy2
, r
.right
- 1, r
.bottom
- 2, c1
);
447 GfxFillRect(r
.right
, r
.top
+ dy1
, r
.right
, r
.bottom
- 1, c2
);
449 GfxFillRect(r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1, c1
);
450 GfxFillRect(r
.left
, r
.bottom
, r
.right
, r
.bottom
, c2
);
455 * @param r Rectangle of the box.
456 * @param colour Colour of the shade box.
457 * @param clicked Box is lowered.
459 static inline void DrawShadeBox(const Rect
&r
, Colours colour
, bool clicked
)
461 DrawImageButtons(r
, WWT_SHADEBOX
, colour
, clicked
, clicked
? SPR_WINDOW_SHADE
: SPR_WINDOW_UNSHADE
);
466 * @param r Rectangle of the box.
467 * @param colour Colour of the sticky box.
468 * @param clicked Box is lowered.
470 static inline void DrawStickyBox(const Rect
&r
, Colours colour
, bool clicked
)
472 DrawImageButtons(r
, WWT_STICKYBOX
, colour
, clicked
, clicked
? SPR_PIN_UP
: SPR_PIN_DOWN
);
476 * Draw a defsize box.
477 * @param r Rectangle of the box.
478 * @param colour Colour of the defsize box.
479 * @param clicked Box is lowered.
481 static inline void DrawDefSizeBox(const Rect
&r
, Colours colour
, bool clicked
)
483 DrawImageButtons(r
, WWT_DEFSIZEBOX
, colour
, clicked
, SPR_WINDOW_DEFSIZE
);
487 * Draw a NewGRF debug box.
488 * @param r Rectangle of the box.
489 * @param colour Colour of the debug box.
490 * @param clicked Box is lowered.
492 static inline void DrawDebugBox(const Rect
&r
, Colours colour
, bool clicked
)
494 DrawImageButtons(r
, WWT_DEBUGBOX
, colour
, clicked
, SPR_WINDOW_DEBUG
);
499 * @param r Rectangle of the box.
500 * @param colour Colour of the resize box.
501 * @param at_left Resize box is at left-side of the window,
502 * @param clicked Box is lowered.
504 static inline void DrawResizeBox(const Rect
&r
, Colours colour
, bool at_left
, bool clicked
)
506 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
508 Dimension d
= GetSpriteSize(SPR_WINDOW_RESIZE_LEFT
);
509 DrawSprite(SPR_WINDOW_RESIZE_LEFT
, PAL_NONE
, r
.left
+ WD_RESIZEBOX_RIGHT
+ clicked
,
510 r
.bottom
+ 1 - WD_RESIZEBOX_BOTTOM
- d
.height
+ clicked
);
512 Dimension d
= GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT
);
513 DrawSprite(SPR_WINDOW_RESIZE_RIGHT
, PAL_NONE
, r
.right
+ 1 - WD_RESIZEBOX_RIGHT
- d
.width
+ clicked
,
514 r
.bottom
+ 1 - WD_RESIZEBOX_BOTTOM
- d
.height
+ clicked
);
520 * @param r Rectangle of the box.
521 * @param colour Colour of the close box.
523 static inline void DrawCloseBox(const Rect
&r
, Colours colour
)
525 if (colour
!= COLOUR_WHITE
) DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, FR_NONE
);
526 Dimension d
= GetSpriteSize(SPR_CLOSEBOX
);
527 int s
= UnScaleGUI(1); /* Offset to account for shadow of SPR_CLOSEBOX */
528 DrawSprite(SPR_CLOSEBOX
, (colour
!= COLOUR_WHITE
? TC_BLACK
: TC_SILVER
) | (1 << PALETTE_TEXT_RECOLOUR
), CenterBounds(r
.left
, r
.right
, d
.width
- s
), CenterBounds(r
.top
, r
.bottom
, d
.height
- s
));
532 * Draw a caption bar.
533 * @param r Rectangle of the bar.
534 * @param colour Colour of the window.
535 * @param owner 'Owner' of the window.
536 * @param str Text to draw in the bar.
538 void DrawCaption(const Rect
&r
, Colours colour
, Owner owner
, StringID str
)
540 bool company_owned
= owner
< MAX_COMPANIES
;
542 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, FR_BORDERONLY
);
543 DrawFrameRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, colour
, company_owned
? FR_LOWERED
| FR_DARKENED
| FR_BORDERONLY
: FR_LOWERED
| FR_DARKENED
);
546 GfxFillRect(r
.left
+ 2, r
.top
+ 2, r
.right
- 2, r
.bottom
- 2, _colour_gradient
[_company_colours
[owner
]][4]);
549 if (str
!= STR_NULL
) {
550 Dimension d
= GetStringBoundingBox(str
);
551 int offset
= max(0, ((int)(r
.bottom
- r
.top
+ 1) - (int)d
.height
) / 2); // Offset for rendering the text vertically centered
552 DrawString(r
.left
+ WD_CAPTIONTEXT_LEFT
, r
.right
- WD_CAPTIONTEXT_RIGHT
, r
.top
+ offset
, str
, TC_FROMSTRING
, SA_HOR_CENTER
);
557 * Draw a button with a dropdown (#WWT_DROPDOWN and #NWID_BUTTON_DROPDOWN).
558 * @param r Rectangle containing the widget.
559 * @param colour Background colour of the widget.
560 * @param clicked_button The button-part is lowered.
561 * @param clicked_dropdown The drop-down part is lowered.
562 * @param str Text of the button.
564 * @note Magic constants are also used in #NWidgetLeaf::ButtonHit.
566 static inline void DrawButtonDropdown(const Rect
&r
, Colours colour
, bool clicked_button
, bool clicked_dropdown
, StringID str
)
568 int text_offset
= max(0, ((int)(r
.bottom
- r
.top
+ 1) - FONT_HEIGHT_NORMAL
) / 2); // Offset for rendering the text vertically centered
570 int dd_width
= NWidgetLeaf::dropdown_dimension
.width
;
571 int dd_height
= NWidgetLeaf::dropdown_dimension
.height
;
572 int image_offset
= max(0, ((int)(r
.bottom
- r
.top
+ 1) - dd_height
) / 2);
574 if (_current_text_dir
== TD_LTR
) {
575 DrawFrameRect(r
.left
, r
.top
, r
.right
- dd_width
, r
.bottom
, colour
, clicked_button
? FR_LOWERED
: FR_NONE
);
576 DrawFrameRect(r
.right
+ 1 - dd_width
, r
.top
, r
.right
, r
.bottom
, colour
, clicked_dropdown
? FR_LOWERED
: FR_NONE
);
577 DrawSprite(SPR_ARROW_DOWN
, PAL_NONE
, r
.right
- (dd_width
- 2) + clicked_dropdown
, r
.top
+ image_offset
+ clicked_dropdown
);
578 if (str
!= STR_NULL
) DrawString(r
.left
+ WD_DROPDOWNTEXT_LEFT
+ clicked_button
, r
.right
- dd_width
- WD_DROPDOWNTEXT_RIGHT
+ clicked_button
, r
.top
+ text_offset
+ clicked_button
, str
, TC_BLACK
);
580 DrawFrameRect(r
.left
+ dd_width
, r
.top
, r
.right
, r
.bottom
, colour
, clicked_button
? FR_LOWERED
: FR_NONE
);
581 DrawFrameRect(r
.left
, r
.top
, r
.left
+ dd_width
- 1, r
.bottom
, colour
, clicked_dropdown
? FR_LOWERED
: FR_NONE
);
582 DrawSprite(SPR_ARROW_DOWN
, PAL_NONE
, r
.left
+ 1 + clicked_dropdown
, r
.top
+ image_offset
+ clicked_dropdown
);
583 if (str
!= STR_NULL
) DrawString(r
.left
+ dd_width
+ WD_DROPDOWNTEXT_LEFT
+ clicked_button
, r
.right
- WD_DROPDOWNTEXT_RIGHT
+ clicked_button
, r
.top
+ text_offset
+ clicked_button
, str
, TC_BLACK
);
588 * Draw a dropdown #WWT_DROPDOWN widget.
589 * @param r Rectangle containing the widget.
590 * @param colour Background colour of the widget.
591 * @param clicked The widget is lowered.
592 * @param str Text of the button.
594 static inline void DrawDropdown(const Rect
&r
, Colours colour
, bool clicked
, StringID str
)
596 DrawButtonDropdown(r
, colour
, false, clicked
, str
);
600 * Paint all widgets of a window.
602 void Window::DrawWidgets() const
604 this->nested_root
->Draw(this);
606 if (this->flags
& WF_WHITE_BORDER
) {
607 DrawFrameRect(0, 0, this->width
- 1, this->height
- 1, COLOUR_WHITE
, FR_BORDERONLY
);
610 if (this->flags
& WF_HIGHLIGHTED
) {
611 extern bool _window_highlight_colour
;
612 for (uint i
= 0; i
< this->nested_array_size
; i
++) {
613 const NWidgetBase
*widget
= this->GetWidget
<NWidgetBase
>(i
);
614 if (widget
== nullptr || !widget
->IsHighlighted()) continue;
616 int left
= widget
->pos_x
;
617 int top
= widget
->pos_y
;
618 int right
= left
+ widget
->current_x
- 1;
619 int bottom
= top
+ widget
->current_y
- 1;
621 int colour
= _string_colourmap
[_window_highlight_colour
? widget
->GetHighlightColour() : TC_WHITE
];
623 GfxFillRect(left
, top
, left
, bottom
- WD_BEVEL_BOTTOM
, colour
);
624 GfxFillRect(left
+ WD_BEVEL_LEFT
, top
, right
- WD_BEVEL_RIGHT
, top
, colour
);
625 GfxFillRect(right
, top
, right
, bottom
- WD_BEVEL_BOTTOM
, colour
);
626 GfxFillRect(left
, bottom
, right
, bottom
, colour
);
632 * Draw a sort button's up or down arrow symbol.
633 * @param widget Sort button widget
634 * @param state State of sort button
636 void Window::DrawSortButtonState(int widget
, SortButtonState state
) const
638 if (state
== SBS_OFF
) return;
640 assert(this->nested_array
!= nullptr);
641 const NWidgetBase
*nwid
= this->GetWidget
<NWidgetBase
>(widget
);
643 /* Sort button uses the same sprites as vertical scrollbar */
644 Dimension dim
= NWidgetScrollbar::GetVerticalDimension();
645 int offset
= this->IsWidgetLowered(widget
) ? 1 : 0;
646 int x
= offset
+ nwid
->pos_x
+ (_current_text_dir
== TD_LTR
? nwid
->current_x
- dim
.width
: 0);
647 int y
= offset
+ nwid
->pos_y
+ (nwid
->current_y
- dim
.height
) / 2;
649 DrawSprite(state
== SBS_DOWN
? SPR_ARROW_DOWN
: SPR_ARROW_UP
, PAL_NONE
, x
, y
);
653 * Get width of up/down arrow of sort button state.
654 * @return Width of space required by sort button arrow.
656 int Window::SortButtonWidth()
658 return NWidgetScrollbar::GetVerticalDimension().width
+ 1;
663 * @defgroup NestedWidgets Hierarchical widgets
664 * Hierarchical widgets, also known as nested widgets, are widgets stored in a tree. At the leafs of the tree are (mostly) the 'real' widgets
665 * visible to the user. At higher levels, widgets get organized in container widgets, until all widgets of the window are merged.
667 * \section nestedwidgetkinds Hierarchical widget kinds
668 * A leaf widget is one of
670 * <li> #NWidgetLeaf for widgets visible for the user, or
671 * <li> #NWidgetSpacer for creating (flexible) empty space between widgets.
673 * The purpose of a leaf widget is to provide interaction with the user by displaying settings, and/or allowing changing the settings.
675 * A container widget is one of
677 * <li> #NWidgetHorizontal for organizing child widgets in a (horizontal) row. The row switches order depending on the language setting (thus supporting
678 * right-to-left languages),
679 * <li> #NWidgetHorizontalLTR for organizing child widgets in a (horizontal) row, always in the same order. All children below this container will also
681 * <li> #NWidgetVertical for organizing child widgets underneath each other.
682 * <li> #NWidgetMatrix for organizing child widgets in a matrix form.
683 * <li> #NWidgetBackground for adding a background behind its child widget.
684 * <li> #NWidgetStacked for stacking child widgets on top of each other.
686 * The purpose of a container widget is to structure its leafs and sub-containers to allow proper resizing.
688 * \section nestedwidgetscomputations Hierarchical widget computations
689 * 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,
690 * or by means of specifying the tree as a collection of nested widgets parts and instantiating the tree from the array.
692 * After the creation step,
693 * - 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).
694 * - Containers only know what their children are, \e fill_x, \e fill_y, \e resize_x, and \e resize_y are not initialized.
696 * Computations in the nested widgets take place as follows:
698 * <li> A bottom-up sweep by recursively calling NWidgetBase::SetupSmallestSize() to initialize the smallest size (\e smallest_x, \e smallest_y) and
699 * to propagate filling and resize steps upwards to the root of the tree.
700 * <li> A top-down sweep by recursively calling NWidgetBase::AssignSizePosition() with #ST_SMALLEST to make the smallest sizes consistent over
701 * 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
702 * 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.
703 * <li> After initializing the smallest size in the widget tree with #ST_SMALLEST, the tree can be resized (the current size modified) by calling
704 * 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
705 * 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}).
707 * After the second step, the current size of the widgets are set to the smallest size.
709 * To resize, perform the last step with the new window size. This can be done as often as desired.
710 * When the smallest size of at least one widget changes, the whole procedure has to be redone from the start.
712 * @see NestedWidgetParts
716 * Base class constructor.
717 * @param tp Nested widget type.
719 NWidgetBase::NWidgetBase(WidgetType tp
) : ZeroedMemoryAllocator()
724 /* ~NWidgetContainer() takes care of #next and #prev data members. */
727 * @fn void NWidgetBase::SetupSmallestSize(Window *w, bool init_array)
728 * Compute smallest size needed by the widget.
730 * The smallest size of a widget is the smallest size that a widget needs to
731 * display itself properly. In addition, filling and resizing of the widget are computed.
732 * The function calls #Window::UpdateWidgetSize for each leaf widget and
733 * background widget without child with a non-negative index.
735 * @param w Window owning the widget.
736 * @param init_array Initialize the \c w->nested_array.
738 * @note After the computation, the results can be queried by accessing the #smallest_x and #smallest_y data members of the widget.
742 * @fn void NWidgetBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
743 * Assign size and position to the widget.
744 * @param sizing Type of resizing to perform.
745 * @param x Horizontal offset of the widget relative to the left edge of the window.
746 * @param y Vertical offset of the widget relative to the top edge of the window.
747 * @param given_width Width allocated to the widget.
748 * @param given_height Height allocated to the widget.
749 * @param rtl Adapt for right-to-left languages (position contents of horizontal containers backwards).
751 * Afterwards, \e pos_x and \e pos_y contain the top-left position of the widget, \e smallest_x and \e smallest_y contain
752 * the smallest size such that all widgets of the window are consistent, and \e current_x and \e current_y contain the current size.
756 * @fn void NWidgetBase::FillNestedArray(NWidgetBase **array, uint length)
757 * Fill the Window::nested_array array with pointers to nested widgets in the tree.
758 * @param array Base pointer of the array.
759 * @param length Length of the array.
763 * @fn void NWidgetBase::Draw(const Window *w)
764 * Draw the widgets of the tree.
765 * The function calls #Window::DrawWidget for each widget with a non-negative index, after the widget itself is painted.
766 * @param w Window that owns the tree.
770 * Mark the widget as 'dirty' (in need of repaint).
771 * @param w Window owning the widget.
773 void NWidgetBase::SetDirty(const Window
*w
) const
775 int abs_left
= w
->left
+ this->pos_x
;
776 int abs_top
= w
->top
+ this->pos_y
;
777 SetDirtyBlocks(abs_left
, abs_top
, abs_left
+ this->current_x
, abs_top
+ this->current_y
);
781 * @fn NWidgetCore *NWidgetBase::GetWidgetFromPos(int x, int y)
782 * Retrieve a widget by its position.
783 * @param x Horizontal position relative to the left edge of the window.
784 * @param y Vertical position relative to the top edge of the window.
785 * @return Returns the deepest nested widget that covers the given position, or \c nullptr if no widget can be found.
789 * Retrieve a widget by its type.
790 * @param tp Widget type to search for.
791 * @return Returns the first widget of the specified type, or \c nullptr if no widget can be found.
793 NWidgetBase
*NWidgetBase::GetWidgetOfType(WidgetType tp
)
795 return (this->type
== tp
) ? this : nullptr;
799 * Constructor for resizable nested widgets.
800 * @param tp Nested widget type.
801 * @param fill_x Horizontal fill step size, \c 0 means no filling is allowed.
802 * @param fill_y Vertical fill step size, \c 0 means no filling is allowed.
804 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp
, uint fill_x
, uint fill_y
) : NWidgetBase(tp
)
806 this->fill_x
= fill_x
;
807 this->fill_y
= fill_y
;
811 * Set minimal size of the widget.
812 * @param min_x Horizontal minimal size of the widget.
813 * @param min_y Vertical minimal size of the widget.
815 void NWidgetResizeBase::SetMinimalSize(uint min_x
, uint min_y
)
817 this->min_x
= max(this->min_x
, min_x
);
818 this->min_y
= max(this->min_y
, min_y
);
822 * Set minimal text lines for the widget.
823 * @param min_lines Number of text lines of the widget.
824 * @param spacing Extra spacing (eg WD_FRAMERECT_TOP + _BOTTOM) of the widget.
825 * @param size Font size of text.
827 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines
, uint8 spacing
, FontSize size
)
829 this->min_y
= min_lines
* GetCharacterHeight(size
) + spacing
;
833 * Set the filling of the widget from initial size.
834 * @param fill_x Horizontal fill step size, \c 0 means no filling is allowed.
835 * @param fill_y Vertical fill step size, \c 0 means no filling is allowed.
837 void NWidgetResizeBase::SetFill(uint fill_x
, uint fill_y
)
839 this->fill_x
= fill_x
;
840 this->fill_y
= fill_y
;
844 * Set resize step of the widget.
845 * @param resize_x Resize step in horizontal direction, value \c 0 means no resize, otherwise the step size in pixels.
846 * @param resize_y Resize step in vertical direction, value \c 0 means no resize, otherwise the step size in pixels.
848 void NWidgetResizeBase::SetResize(uint resize_x
, uint resize_y
)
850 this->resize_x
= resize_x
;
851 this->resize_y
= resize_y
;
854 void NWidgetResizeBase::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
856 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
860 * Initialization of a 'real' widget.
861 * @param tp Type of the widget.
862 * @param colour Colour of the widget.
863 * @param fill_x Default horizontal filling.
864 * @param fill_y Default vertical filling.
865 * @param widget_data Data component of the widget. @see Widget::data
866 * @param tool_tip Tool tip of the widget. @see Widget::tooltips
868 NWidgetCore::NWidgetCore(WidgetType tp
, Colours colour
, uint fill_x
, uint fill_y
, uint32 widget_data
, StringID tool_tip
) : NWidgetResizeBase(tp
, fill_x
, fill_y
)
870 this->colour
= colour
;
872 this->widget_data
= widget_data
;
873 this->tool_tip
= tool_tip
;
874 this->scrollbar_index
= -1;
878 * Set index of the nested widget in the widget array.
879 * @param index Index to use.
881 void NWidgetCore::SetIndex(int index
)
888 * Set data and tool tip of the nested widget.
889 * @param widget_data Data to use.
890 * @param tool_tip Tool tip string to use.
892 void NWidgetCore::SetDataTip(uint32 widget_data
, StringID tool_tip
)
894 this->widget_data
= widget_data
;
895 this->tool_tip
= tool_tip
;
898 void NWidgetCore::FillNestedArray(NWidgetBase
**array
, uint length
)
900 if (this->index
>= 0 && (uint
)(this->index
) < length
) array
[this->index
] = this;
903 NWidgetCore
*NWidgetCore::GetWidgetFromPos(int x
, int y
)
905 return (IsInsideBS(x
, this->pos_x
, this->current_x
) && IsInsideBS(y
, this->pos_y
, this->current_y
)) ? this : nullptr;
909 * Constructor container baseclass.
910 * @param tp Type of the container.
912 NWidgetContainer::NWidgetContainer(WidgetType tp
) : NWidgetBase(tp
)
914 this->head
= nullptr;
915 this->tail
= nullptr;
918 NWidgetContainer::~NWidgetContainer()
920 while (this->head
!= nullptr) {
921 NWidgetBase
*wid
= this->head
->next
;
925 this->tail
= nullptr;
928 NWidgetBase
*NWidgetContainer::GetWidgetOfType(WidgetType tp
)
930 if (this->type
== tp
) return this;
931 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
932 NWidgetBase
*nwid
= child_wid
->GetWidgetOfType(tp
);
933 if (nwid
!= nullptr) return nwid
;
939 * Append widget \a wid to container.
940 * @param wid Widget to append.
942 void NWidgetContainer::Add(NWidgetBase
*wid
)
944 assert(wid
->next
== nullptr && wid
->prev
== nullptr);
946 if (this->head
== nullptr) {
950 assert(this->tail
!= nullptr);
951 assert(this->tail
->next
== nullptr);
953 this->tail
->next
= wid
;
954 wid
->prev
= this->tail
;
959 void NWidgetContainer::FillNestedArray(NWidgetBase
**array
, uint length
)
961 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
962 child_wid
->FillNestedArray(array
, length
);
967 * Widgets stacked on top of each other.
969 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION
)
974 void NWidgetStacked::SetIndex(int index
)
979 void NWidgetStacked::SetupSmallestSize(Window
*w
, bool init_array
)
981 if (this->index
>= 0 && init_array
) { // Fill w->nested_array[]
982 assert(w
->nested_array_size
> (uint
)this->index
);
983 w
->nested_array
[this->index
] = this;
986 /* Zero size plane selected */
987 if (this->shown_plane
>= SZSP_BEGIN
) {
988 Dimension size
= {0, 0};
989 Dimension padding
= {0, 0};
990 Dimension fill
= {(this->shown_plane
== SZSP_HORIZONTAL
), (this->shown_plane
== SZSP_VERTICAL
)};
991 Dimension resize
= {(this->shown_plane
== SZSP_HORIZONTAL
), (this->shown_plane
== SZSP_VERTICAL
)};
992 /* Here we're primarily interested in the value of resize */
993 if (this->index
>= 0) w
->UpdateWidgetSize(this->index
, &size
, padding
, &fill
, &resize
);
995 this->smallest_x
= size
.width
;
996 this->smallest_y
= size
.height
;
997 this->fill_x
= fill
.width
;
998 this->fill_y
= fill
.height
;
999 this->resize_x
= resize
.width
;
1000 this->resize_y
= resize
.height
;
1004 /* First sweep, recurse down and compute minimal size and filling. */
1005 this->smallest_x
= 0;
1006 this->smallest_y
= 0;
1007 this->fill_x
= (this->head
!= nullptr) ? 1 : 0;
1008 this->fill_y
= (this->head
!= nullptr) ? 1 : 0;
1009 this->resize_x
= (this->head
!= nullptr) ? 1 : 0;
1010 this->resize_y
= (this->head
!= nullptr) ? 1 : 0;
1011 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1012 child_wid
->SetupSmallestSize(w
, init_array
);
1014 this->smallest_x
= max(this->smallest_x
, child_wid
->smallest_x
+ child_wid
->padding_left
+ child_wid
->padding_right
);
1015 this->smallest_y
= max(this->smallest_y
, child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
);
1016 this->fill_x
= LeastCommonMultiple(this->fill_x
, child_wid
->fill_x
);
1017 this->fill_y
= LeastCommonMultiple(this->fill_y
, child_wid
->fill_y
);
1018 this->resize_x
= LeastCommonMultiple(this->resize_x
, child_wid
->resize_x
);
1019 this->resize_y
= LeastCommonMultiple(this->resize_y
, child_wid
->resize_y
);
1023 void NWidgetStacked::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1025 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1026 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1028 if (this->shown_plane
>= SZSP_BEGIN
) return;
1030 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1031 uint hor_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetHorizontalStepSize(sizing
);
1032 uint child_width
= ComputeMaxSize(child_wid
->smallest_x
, given_width
- child_wid
->padding_left
- child_wid
->padding_right
, hor_step
);
1033 uint child_pos_x
= (rtl
? child_wid
->padding_right
: child_wid
->padding_left
);
1035 uint vert_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetVerticalStepSize(sizing
);
1036 uint child_height
= ComputeMaxSize(child_wid
->smallest_y
, given_height
- child_wid
->padding_top
- child_wid
->padding_bottom
, vert_step
);
1037 uint child_pos_y
= child_wid
->padding_top
;
1039 child_wid
->AssignSizePosition(sizing
, x
+ child_pos_x
, y
+ child_pos_y
, child_width
, child_height
, rtl
);
1043 void NWidgetStacked::FillNestedArray(NWidgetBase
**array
, uint length
)
1045 if (this->index
>= 0 && (uint
)(this->index
) < length
) array
[this->index
] = this;
1046 NWidgetContainer::FillNestedArray(array
, length
);
1049 void NWidgetStacked::Draw(const Window
*w
)
1051 if (this->shown_plane
>= SZSP_BEGIN
) return;
1054 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; plane
++, child_wid
= child_wid
->next
) {
1055 if (plane
== this->shown_plane
) {
1064 NWidgetCore
*NWidgetStacked::GetWidgetFromPos(int x
, int y
)
1066 if (this->shown_plane
>= SZSP_BEGIN
) return nullptr;
1068 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return nullptr;
1070 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; plane
++, child_wid
= child_wid
->next
) {
1071 if (plane
== this->shown_plane
) {
1072 return child_wid
->GetWidgetFromPos(x
, y
);
1079 * Select which plane to show (for #NWID_SELECTION only).
1080 * @param plane Plane number to display.
1082 void NWidgetStacked::SetDisplayedPlane(int plane
)
1084 this->shown_plane
= plane
;
1087 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp
, NWidContainerFlags flags
) : NWidgetContainer(tp
)
1089 this->flags
= flags
;
1093 * Set additional pre/inter/post space for the container.
1095 * @param pip_pre Additional space in front of the first child widget (above
1096 * for the vertical container, at the left for the horizontal container).
1097 * @param pip_inter Additional space between two child widgets.
1098 * @param pip_post Additional space after the last child widget (below for the
1099 * vertical container, at the right for the horizontal container).
1101 void NWidgetPIPContainer::SetPIP(uint8 pip_pre
, uint8 pip_inter
, uint8 pip_post
)
1103 this->pip_pre
= pip_pre
;
1104 this->pip_inter
= pip_inter
;
1105 this->pip_post
= pip_post
;
1108 void NWidgetPIPContainer::Draw(const Window
*w
)
1110 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1115 NWidgetCore
*NWidgetPIPContainer::GetWidgetFromPos(int x
, int y
)
1117 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return nullptr;
1119 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1120 NWidgetCore
*nwid
= child_wid
->GetWidgetFromPos(x
, y
);
1121 if (nwid
!= nullptr) return nwid
;
1126 /** Horizontal container widget. */
1127 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags
) : NWidgetPIPContainer(NWID_HORIZONTAL
, flags
)
1131 void NWidgetHorizontal::SetupSmallestSize(Window
*w
, bool init_array
)
1133 this->smallest_x
= 0; // Sum of minimal size of all children.
1134 this->smallest_y
= 0; // Biggest child.
1135 this->fill_x
= 0; // smallest non-zero child widget fill step.
1136 this->fill_y
= 1; // smallest common child fill step.
1137 this->resize_x
= 0; // smallest non-zero child widget resize step.
1138 this->resize_y
= 1; // smallest common child resize step.
1140 /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1141 uint longest
= 0; // Longest child found.
1142 uint max_vert_fill
= 0; // Biggest vertical fill step.
1143 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1144 child_wid
->SetupSmallestSize(w
, init_array
);
1145 longest
= max(longest
, child_wid
->smallest_x
);
1146 max_vert_fill
= max(max_vert_fill
, child_wid
->GetVerticalStepSize(ST_SMALLEST
));
1147 this->smallest_y
= max(this->smallest_y
, child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
);
1149 /* 1b. Make the container higher if needed to accommodate all children nicely. */
1150 uint max_smallest
= this->smallest_y
+ 3 * max_vert_fill
; // Upper limit to computing smallest height.
1151 uint cur_height
= this->smallest_y
;
1153 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1154 uint step_size
= child_wid
->GetVerticalStepSize(ST_SMALLEST
);
1155 uint child_height
= child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
;
1156 if (step_size
> 1 && child_height
< cur_height
) { // Small step sizes or already fitting children are not interesting.
1157 uint remainder
= (cur_height
- child_height
) % step_size
;
1158 if (remainder
> 0) { // Child did not fit entirely, widen the container.
1159 cur_height
+= step_size
- remainder
;
1160 assert(cur_height
< max_smallest
); // Safeguard against infinite height expansion.
1161 /* Remaining children will adapt to the new cur_height, thus speeding up the computation. */
1165 if (this->smallest_y
== cur_height
) break;
1166 this->smallest_y
= cur_height
; // Smallest height got changed, try again.
1168 /* 2. For containers that must maintain equal width, extend child minimal size. */
1169 if (this->flags
& NC_EQUALSIZE
) {
1170 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1171 if (child_wid
->fill_x
== 1) child_wid
->smallest_x
= longest
;
1174 /* 3. Move PIP space to the children, compute smallest, fill, and resize values of the container. */
1175 if (this->head
!= nullptr) this->head
->padding_left
+= this->pip_pre
;
1176 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1177 if (child_wid
->next
!= nullptr) {
1178 child_wid
->padding_right
+= this->pip_inter
;
1180 child_wid
->padding_right
+= this->pip_post
;
1183 this->smallest_x
+= child_wid
->smallest_x
+ child_wid
->padding_left
+ child_wid
->padding_right
;
1184 if (child_wid
->fill_x
> 0) {
1185 if (this->fill_x
== 0 || this->fill_x
> child_wid
->fill_x
) this->fill_x
= child_wid
->fill_x
;
1187 this->fill_y
= LeastCommonMultiple(this->fill_y
, child_wid
->fill_y
);
1189 if (child_wid
->resize_x
> 0) {
1190 if (this->resize_x
== 0 || this->resize_x
> child_wid
->resize_x
) this->resize_x
= child_wid
->resize_x
;
1192 this->resize_y
= LeastCommonMultiple(this->resize_y
, child_wid
->resize_y
);
1194 /* We need to zero the PIP settings so we can re-initialize the tree. */
1195 this->pip_pre
= this->pip_inter
= this->pip_post
= 0;
1198 void NWidgetHorizontal::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1200 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1202 /* Compute additional width given to us. */
1203 uint additional_length
= given_width
;
1204 if (sizing
== ST_SMALLEST
&& (this->flags
& NC_EQUALSIZE
)) {
1205 /* For EQUALSIZE containers this does not sum to smallest_x during initialisation */
1206 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1207 additional_length
-= child_wid
->smallest_x
+ child_wid
->padding_right
+ child_wid
->padding_left
;
1210 additional_length
-= this->smallest_x
;
1213 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1215 /* In principle, the additional horizontal space is distributed evenly over the available resizable children. Due to step sizes, this may not always be feasible.
1216 * To make resizing work as good as possible, first children with biggest step sizes are done. These may get less due to rounding down.
1217 * 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
1218 * of the child with the smallest non-zero stepsize.
1220 * 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
1221 * size and position, and directly call child->AssignSizePosition() with the computed values.
1222 * 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
1223 * then we call the child.
1226 /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle vertical size for all children,
1227 * handle horizontal size for non-resizing children.
1229 int num_changing_childs
= 0; // Number of children that can change size.
1230 uint biggest_stepsize
= 0;
1231 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1232 uint hor_step
= child_wid
->GetHorizontalStepSize(sizing
);
1234 num_changing_childs
++;
1235 biggest_stepsize
= max(biggest_stepsize
, hor_step
);
1237 child_wid
->current_x
= child_wid
->smallest_x
;
1240 uint vert_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetVerticalStepSize(sizing
);
1241 child_wid
->current_y
= ComputeMaxSize(child_wid
->smallest_y
, given_height
- child_wid
->padding_top
- child_wid
->padding_bottom
, vert_step
);
1244 /* Second loop: Allocate the additional horizontal space over the resizing children, starting with the biggest resize steps. */
1245 while (biggest_stepsize
> 0) {
1246 uint next_biggest_stepsize
= 0;
1247 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1248 uint hor_step
= child_wid
->GetHorizontalStepSize(sizing
);
1249 if (hor_step
> biggest_stepsize
) continue; // Already done
1250 if (hor_step
== biggest_stepsize
) {
1251 uint increment
= additional_length
/ num_changing_childs
;
1252 num_changing_childs
--;
1253 if (hor_step
> 1) increment
-= increment
% hor_step
;
1254 child_wid
->current_x
= child_wid
->smallest_x
+ increment
;
1255 additional_length
-= increment
;
1258 next_biggest_stepsize
= max(next_biggest_stepsize
, hor_step
);
1260 biggest_stepsize
= next_biggest_stepsize
;
1262 assert(num_changing_childs
== 0);
1264 /* Third loop: Compute position and call the child. */
1265 uint position
= rtl
? this->current_x
: 0; // Place to put next child relative to origin of the container.
1266 NWidgetBase
*child_wid
= this->head
;
1267 while (child_wid
!= nullptr) {
1268 uint child_width
= child_wid
->current_x
;
1269 uint child_x
= x
+ (rtl
? position
- child_width
- child_wid
->padding_left
: position
+ child_wid
->padding_left
);
1270 uint child_y
= y
+ child_wid
->padding_top
;
1272 child_wid
->AssignSizePosition(sizing
, child_x
, child_y
, child_width
, child_wid
->current_y
, rtl
);
1273 uint padded_child_width
= child_width
+ child_wid
->padding_right
+ child_wid
->padding_left
;
1274 position
= rtl
? position
- padded_child_width
: position
+ padded_child_width
;
1276 child_wid
= child_wid
->next
;
1280 /** Horizontal left-to-right container widget. */
1281 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags
) : NWidgetHorizontal(flags
)
1283 this->type
= NWID_HORIZONTAL_LTR
;
1286 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1288 NWidgetHorizontal::AssignSizePosition(sizing
, x
, y
, given_width
, given_height
, false);
1291 /** Vertical container widget. */
1292 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags
) : NWidgetPIPContainer(NWID_VERTICAL
, flags
)
1296 void NWidgetVertical::SetupSmallestSize(Window
*w
, bool init_array
)
1298 this->smallest_x
= 0; // Biggest child.
1299 this->smallest_y
= 0; // Sum of minimal size of all children.
1300 this->fill_x
= 1; // smallest common child fill step.
1301 this->fill_y
= 0; // smallest non-zero child widget fill step.
1302 this->resize_x
= 1; // smallest common child resize step.
1303 this->resize_y
= 0; // smallest non-zero child widget resize step.
1305 /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1306 uint highest
= 0; // Highest child found.
1307 uint max_hor_fill
= 0; // Biggest horizontal fill step.
1308 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1309 child_wid
->SetupSmallestSize(w
, init_array
);
1310 highest
= max(highest
, child_wid
->smallest_y
);
1311 max_hor_fill
= max(max_hor_fill
, child_wid
->GetHorizontalStepSize(ST_SMALLEST
));
1312 this->smallest_x
= max(this->smallest_x
, child_wid
->smallest_x
+ child_wid
->padding_left
+ child_wid
->padding_right
);
1314 /* 1b. Make the container wider if needed to accommodate all children nicely. */
1315 uint max_smallest
= this->smallest_x
+ 3 * max_hor_fill
; // Upper limit to computing smallest height.
1316 uint cur_width
= this->smallest_x
;
1318 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1319 uint step_size
= child_wid
->GetHorizontalStepSize(ST_SMALLEST
);
1320 uint child_width
= child_wid
->smallest_x
+ child_wid
->padding_left
+ child_wid
->padding_right
;
1321 if (step_size
> 1 && child_width
< cur_width
) { // Small step sizes or already fitting children are not interesting.
1322 uint remainder
= (cur_width
- child_width
) % step_size
;
1323 if (remainder
> 0) { // Child did not fit entirely, widen the container.
1324 cur_width
+= step_size
- remainder
;
1325 assert(cur_width
< max_smallest
); // Safeguard against infinite width expansion.
1326 /* Remaining children will adapt to the new cur_width, thus speeding up the computation. */
1330 if (this->smallest_x
== cur_width
) break;
1331 this->smallest_x
= cur_width
; // Smallest width got changed, try again.
1333 /* 2. For containers that must maintain equal width, extend children minimal size. */
1334 if (this->flags
& NC_EQUALSIZE
) {
1335 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1336 if (child_wid
->fill_y
== 1) child_wid
->smallest_y
= highest
;
1339 /* 3. Move PIP space to the child, compute smallest, fill, and resize values of the container. */
1340 if (this->head
!= nullptr) this->head
->padding_top
+= this->pip_pre
;
1341 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1342 if (child_wid
->next
!= nullptr) {
1343 child_wid
->padding_bottom
+= this->pip_inter
;
1345 child_wid
->padding_bottom
+= this->pip_post
;
1348 this->smallest_y
+= child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
;
1349 if (child_wid
->fill_y
> 0) {
1350 if (this->fill_y
== 0 || this->fill_y
> child_wid
->fill_y
) this->fill_y
= child_wid
->fill_y
;
1352 this->fill_x
= LeastCommonMultiple(this->fill_x
, child_wid
->fill_x
);
1354 if (child_wid
->resize_y
> 0) {
1355 if (this->resize_y
== 0 || this->resize_y
> child_wid
->resize_y
) this->resize_y
= child_wid
->resize_y
;
1357 this->resize_x
= LeastCommonMultiple(this->resize_x
, child_wid
->resize_x
);
1359 /* We need to zero the PIP settings so we can re-initialize the tree. */
1360 this->pip_pre
= this->pip_inter
= this->pip_post
= 0;
1363 void NWidgetVertical::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1365 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1367 /* Compute additional height given to us. */
1368 uint additional_length
= given_height
;
1369 if (sizing
== ST_SMALLEST
&& (this->flags
& NC_EQUALSIZE
)) {
1370 /* For EQUALSIZE containers this does not sum to smallest_y during initialisation */
1371 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1372 additional_length
-= child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
;
1375 additional_length
-= this->smallest_y
;
1378 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1380 /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the children with the biggest resize steps.
1381 * It also stores computed widths and heights into current_x and current_y values of the child.
1384 /* 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. */
1385 int num_changing_childs
= 0; // Number of children that can change size.
1386 uint biggest_stepsize
= 0;
1387 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1388 uint vert_step
= child_wid
->GetVerticalStepSize(sizing
);
1389 if (vert_step
> 0) {
1390 num_changing_childs
++;
1391 biggest_stepsize
= max(biggest_stepsize
, vert_step
);
1393 child_wid
->current_y
= child_wid
->smallest_y
;
1396 uint hor_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetHorizontalStepSize(sizing
);
1397 child_wid
->current_x
= ComputeMaxSize(child_wid
->smallest_x
, given_width
- child_wid
->padding_left
- child_wid
->padding_right
, hor_step
);
1400 /* Second loop: Allocate the additional vertical space over the resizing children, starting with the biggest resize steps. */
1401 while (biggest_stepsize
> 0) {
1402 uint next_biggest_stepsize
= 0;
1403 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1404 uint vert_step
= child_wid
->GetVerticalStepSize(sizing
);
1405 if (vert_step
> biggest_stepsize
) continue; // Already done
1406 if (vert_step
== biggest_stepsize
) {
1407 uint increment
= additional_length
/ num_changing_childs
;
1408 num_changing_childs
--;
1409 if (vert_step
> 1) increment
-= increment
% vert_step
;
1410 child_wid
->current_y
= child_wid
->smallest_y
+ increment
;
1411 additional_length
-= increment
;
1414 next_biggest_stepsize
= max(next_biggest_stepsize
, vert_step
);
1416 biggest_stepsize
= next_biggest_stepsize
;
1418 assert(num_changing_childs
== 0);
1420 /* Third loop: Compute position and call the child. */
1421 uint position
= 0; // Place to put next child relative to origin of the container.
1422 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1423 uint child_x
= x
+ (rtl
? child_wid
->padding_right
: child_wid
->padding_left
);
1424 uint child_height
= child_wid
->current_y
;
1426 child_wid
->AssignSizePosition(sizing
, child_x
, y
+ position
+ child_wid
->padding_top
, child_wid
->current_x
, child_height
, rtl
);
1427 position
+= child_height
+ child_wid
->padding_top
+ child_wid
->padding_bottom
;
1432 * Generic spacer widget.
1433 * @param length Horizontal size of the spacer widget.
1434 * @param height Vertical size of the spacer widget.
1436 NWidgetSpacer::NWidgetSpacer(int length
, int height
) : NWidgetResizeBase(NWID_SPACER
, 0, 0)
1438 this->SetMinimalSize(length
, height
);
1439 this->SetResize(0, 0);
1442 void NWidgetSpacer::SetupSmallestSize(Window
*w
, bool init_array
)
1444 this->smallest_x
= this->min_x
;
1445 this->smallest_y
= this->min_y
;
1448 void NWidgetSpacer::FillNestedArray(NWidgetBase
**array
, uint length
)
1452 void NWidgetSpacer::Draw(const Window
*w
)
1454 /* Spacer widget is never visible. */
1457 void NWidgetSpacer::SetDirty(const Window
*w
) const
1459 /* Spacer widget never need repainting. */
1462 NWidgetCore
*NWidgetSpacer::GetWidgetFromPos(int x
, int y
)
1467 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX
, NC_EQUALSIZE
), index(-1), clicked(-1), count(-1)
1471 void NWidgetMatrix::SetIndex(int index
)
1473 this->index
= index
;
1476 void NWidgetMatrix::SetColour(Colours colour
)
1478 this->colour
= colour
;
1482 * Sets the clicked widget in the matrix.
1483 * @param clicked The clicked widget.
1485 void NWidgetMatrix::SetClicked(int clicked
)
1487 this->clicked
= clicked
;
1488 if (this->clicked
>= 0 && this->sb
!= nullptr && this->widgets_x
!= 0) {
1489 int vpos
= (this->clicked
/ this->widgets_x
) * this->widget_h
; // Vertical position of the top.
1490 /* Need to scroll down -> Scroll to the bottom.
1491 * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
1492 if (this->sb
->GetPosition() < vpos
) vpos
+= this->widget_h
- this->pip_inter
- 1;
1493 this->sb
->ScrollTowards(vpos
);
1498 * Set the number of elements in this matrix.
1499 * @note Updates the number of elements/capacity of the real scrollbar.
1500 * @param count The number of elements.
1502 void NWidgetMatrix::SetCount(int count
)
1504 this->count
= count
;
1506 if (this->sb
== nullptr || this->widgets_x
== 0) return;
1508 /* We need to get the number of pixels the matrix is high/wide.
1509 * So, determine the number of rows/columns based on the number of
1510 * columns/rows (one is constant/unscrollable).
1511 * Then multiply that by the height of a widget, and add the pre
1512 * and post spacing "offsets". */
1513 count
= CeilDiv(count
, this->sb
->IsVertical() ? this->widgets_x
: this->widgets_y
);
1514 count
*= (this->sb
->IsVertical() ? this->head
->smallest_y
: this->head
->smallest_x
) + this->pip_inter
;
1515 if (count
> 0) count
-= this->pip_inter
; // We counted an inter too much in the multiplication above
1516 count
+= this->pip_pre
+ this->pip_post
;
1517 this->sb
->SetCount(count
);
1518 this->sb
->SetCapacity(this->sb
->IsVertical() ? this->current_y
: this->current_x
);
1519 this->sb
->SetStepSize(this->sb
->IsVertical() ? this->widget_h
: this->widget_w
);
1523 * Assign a scrollbar to this matrix.
1524 * @param sb The scrollbar to assign to us.
1526 void NWidgetMatrix::SetScrollbar(Scrollbar
*sb
)
1531 void NWidgetMatrix::SetupSmallestSize(Window
*w
, bool init_array
)
1533 assert(this->head
!= nullptr);
1534 assert(this->head
->next
== nullptr);
1536 if (this->index
>= 0 && init_array
) { // Fill w->nested_array[]
1537 assert(w
->nested_array_size
> (uint
)this->index
);
1538 w
->nested_array
[this->index
] = this;
1541 /* Reset the widget number. */
1542 NWidgetCore
*nw
= dynamic_cast<NWidgetCore
*>(this->head
);
1543 assert(nw
!= nullptr);
1544 SB(nw
->index
, 16, 16, 0);
1545 this->head
->SetupSmallestSize(w
, init_array
);
1547 Dimension padding
= { (uint
)this->pip_pre
+ this->pip_post
, (uint
)this->pip_pre
+ this->pip_post
};
1548 Dimension size
= {this->head
->smallest_x
+ padding
.width
, this->head
->smallest_y
+ padding
.height
};
1549 Dimension fill
= {0, 0};
1550 Dimension resize
= {this->pip_inter
+ this->head
->smallest_x
, this->pip_inter
+ this->head
->smallest_y
};
1552 if (this->index
>= 0) w
->UpdateWidgetSize(this->index
, &size
, padding
, &fill
, &resize
);
1554 this->smallest_x
= size
.width
;
1555 this->smallest_y
= size
.height
;
1556 this->fill_x
= fill
.width
;
1557 this->fill_y
= fill
.height
;
1558 this->resize_x
= resize
.width
;
1559 this->resize_y
= resize
.height
;
1562 void NWidgetMatrix::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1564 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1568 this->current_x
= given_width
;
1569 this->current_y
= given_height
;
1571 /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
1572 this->widget_w
= this->head
->smallest_x
+ this->pip_inter
;
1573 this->widget_h
= this->head
->smallest_y
+ this->pip_inter
;
1575 /* Account for the pip_inter is between widgets, so we need to account for that when
1576 * the division assumes pip_inter is used for all widgets. */
1577 this->widgets_x
= CeilDiv(this->current_x
- this->pip_pre
- this->pip_post
+ this->pip_inter
, this->widget_w
);
1578 this->widgets_y
= CeilDiv(this->current_y
- this->pip_pre
- this->pip_post
+ this->pip_inter
, this->widget_h
);
1580 /* When resizing, update the scrollbar's count. E.g. with a vertical
1581 * scrollbar becoming wider or narrower means the amount of rows in
1582 * the scrollbar becomes respectively smaller or higher. */
1583 this->SetCount(this->count
);
1586 void NWidgetMatrix::FillNestedArray(NWidgetBase
**array
, uint length
)
1588 if (this->index
>= 0 && (uint
)(this->index
) < length
) array
[this->index
] = this;
1589 NWidgetContainer::FillNestedArray(array
, length
);
1592 NWidgetCore
*NWidgetMatrix::GetWidgetFromPos(int x
, int y
)
1594 /* Falls outside of the matrix widget. */
1595 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return nullptr;
1597 int start_x
, start_y
, base_offs_x
, base_offs_y
;
1598 this->GetScrollOffsets(start_x
, start_y
, base_offs_x
, base_offs_y
);
1600 bool rtl
= _current_text_dir
== TD_RTL
;
1602 int widget_col
= (rtl
?
1603 -x
+ (int)this->pip_post
+ (int)this->pos_x
+ base_offs_x
+ (int)this->widget_w
- 1 - (int)this->pip_inter
:
1604 x
- (int)this->pip_pre
- (int)this->pos_x
- base_offs_x
1607 int widget_row
= (y
- base_offs_y
- (int)this->pip_pre
- (int)this->pos_y
) / this->widget_h
;
1609 int sub_wid
= (widget_row
+ start_y
) * this->widgets_x
+ start_x
+ widget_col
;
1610 if (sub_wid
>= this->count
) return nullptr;
1612 NWidgetCore
*child
= dynamic_cast<NWidgetCore
*>(this->head
);
1613 assert(child
!= nullptr);
1614 child
->AssignSizePosition(ST_RESIZE
,
1615 this->pos_x
+ (rtl
? this->pip_post
- widget_col
* this->widget_w
: this->pip_pre
+ widget_col
* this->widget_w
) + base_offs_x
,
1616 this->pos_y
+ this->pip_pre
+ widget_row
* this->widget_h
+ base_offs_y
,
1617 child
->smallest_x
, child
->smallest_y
, rtl
);
1619 SB(child
->index
, 16, 16, sub_wid
);
1621 return child
->GetWidgetFromPos(x
, y
);
1624 /* virtual */ void NWidgetMatrix::Draw(const Window
*w
)
1626 /* Fill the background. */
1627 GfxFillRect(this->pos_x
, this->pos_y
, this->pos_x
+ this->current_x
- 1, this->pos_y
+ this->current_y
- 1, _colour_gradient
[this->colour
& 0xF][5]);
1629 /* Set up a clipping area for the previews. */
1630 bool rtl
= _current_text_dir
== TD_RTL
;
1631 DrawPixelInfo tmp_dpi
;
1632 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;
1633 DrawPixelInfo
*old_dpi
= _cur_dpi
;
1634 _cur_dpi
= &tmp_dpi
;
1636 /* Get the appropriate offsets so we can draw the right widgets. */
1637 NWidgetCore
*child
= dynamic_cast<NWidgetCore
*>(this->head
);
1638 assert(child
!= nullptr);
1639 int start_x
, start_y
, base_offs_x
, base_offs_y
;
1640 this->GetScrollOffsets(start_x
, start_y
, base_offs_x
, base_offs_y
);
1642 int offs_y
= base_offs_y
;
1643 for (int y
= start_y
; y
< start_y
+ this->widgets_y
+ 1; y
++, offs_y
+= this->widget_h
) {
1644 /* Are we within bounds? */
1645 if (offs_y
+ child
->smallest_y
<= 0) continue;
1646 if (offs_y
>= (int)this->current_y
) break;
1648 /* We've passed our amount of widgets. */
1649 if (y
* this->widgets_x
>= this->count
) break;
1651 int offs_x
= base_offs_x
;
1652 for (int x
= start_x
; x
< start_x
+ this->widgets_x
+ 1; x
++, offs_x
+= rtl
? -this->widget_w
: this->widget_w
) {
1653 /* Are we within bounds? */
1654 if (offs_x
+ child
->smallest_x
<= 0) continue;
1655 if (offs_x
>= (int)this->current_x
) continue;
1657 /* Do we have this many widgets? */
1658 int sub_wid
= y
* this->widgets_x
+ x
;
1659 if (sub_wid
>= this->count
) break;
1661 child
->AssignSizePosition(ST_RESIZE
, offs_x
, offs_y
, child
->smallest_x
, child
->smallest_y
, rtl
);
1662 child
->SetLowered(this->clicked
== sub_wid
);
1663 SB(child
->index
, 16, 16, sub_wid
);
1668 /* Restore the clipping area. */
1673 * Get the different offsets that are influenced by scrolling.
1674 * @param[out] start_x The start position in columns (index of the left-most column, swapped in RTL).
1675 * @param[out] start_y The start position in rows.
1676 * @param[out] base_offs_x The base horizontal offset in pixels (X position of the column \a start_x).
1677 * @param[out] base_offs_y The base vertical offset in pixels (Y position of the column \a start_y).
1679 void NWidgetMatrix::GetScrollOffsets(int &start_x
, int &start_y
, int &base_offs_x
, int &base_offs_y
)
1681 base_offs_x
= _current_text_dir
== TD_RTL
? this->widget_w
* (this->widgets_x
- 1) : 0;
1685 if (this->sb
!= nullptr) {
1686 if (this->sb
->IsVertical()) {
1687 start_y
= this->sb
->GetPosition() / this->widget_h
;
1688 base_offs_y
+= -this->sb
->GetPosition() + start_y
* this->widget_h
;
1690 start_x
= this->sb
->GetPosition() / this->widget_w
;
1691 int sub_x
= this->sb
->GetPosition() - start_x
* this->widget_w
;
1692 if (_current_text_dir
== TD_RTL
) {
1693 base_offs_x
+= sub_x
;
1695 base_offs_x
-= sub_x
;
1702 * Constructor parent nested widgets.
1703 * @param tp Type of parent widget.
1704 * @param colour Colour of the parent widget.
1705 * @param index Index in the widget array used by the window system.
1706 * @param child Child container widget (if supplied). If not supplied, a
1707 * vertical container will be inserted while adding the first
1710 NWidgetBackground::NWidgetBackground(WidgetType tp
, Colours colour
, int index
, NWidgetPIPContainer
*child
) : NWidgetCore(tp
, colour
, 1, 1, 0x0, STR_NULL
)
1712 assert(tp
== WWT_PANEL
|| tp
== WWT_INSET
|| tp
== WWT_FRAME
);
1713 if (index
>= 0) this->SetIndex(index
);
1714 this->child
= child
;
1717 NWidgetBackground::~NWidgetBackground()
1719 if (this->child
!= nullptr) delete this->child
;
1723 * Add a child to the parent.
1724 * @param nwid Nested widget to add to the background widget.
1726 * Unless a child container has been given in the constructor, a parent behaves as a vertical container.
1727 * You can add several children to it, and they are put underneath each other.
1729 void NWidgetBackground::Add(NWidgetBase
*nwid
)
1731 if (this->child
== nullptr) {
1732 this->child
= new NWidgetVertical();
1734 this->child
->Add(nwid
);
1738 * Set additional pre/inter/post space for the background widget.
1740 * @param pip_pre Additional space in front of the first child widget (above
1741 * for the vertical container, at the left for the horizontal container).
1742 * @param pip_inter Additional space between two child widgets.
1743 * @param pip_post Additional space after the last child widget (below for the
1744 * vertical container, at the right for the horizontal container).
1745 * @note Using this function implies that the widget has (or will have) child widgets.
1747 void NWidgetBackground::SetPIP(uint8 pip_pre
, uint8 pip_inter
, uint8 pip_post
)
1749 if (this->child
== nullptr) {
1750 this->child
= new NWidgetVertical();
1752 this->child
->SetPIP(pip_pre
, pip_inter
, pip_post
);
1755 void NWidgetBackground::SetupSmallestSize(Window
*w
, bool init_array
)
1757 if (init_array
&& this->index
>= 0) {
1758 assert(w
->nested_array_size
> (uint
)this->index
);
1759 w
->nested_array
[this->index
] = this;
1761 if (this->child
!= nullptr) {
1762 this->child
->SetupSmallestSize(w
, init_array
);
1764 this->smallest_x
= this->child
->smallest_x
;
1765 this->smallest_y
= this->child
->smallest_y
;
1766 this->fill_x
= this->child
->fill_x
;
1767 this->fill_y
= this->child
->fill_y
;
1768 this->resize_x
= this->child
->resize_x
;
1769 this->resize_y
= this->child
->resize_y
;
1771 /* Account for the size of the frame's text if that exists */
1772 if (w
!= nullptr && this->type
== WWT_FRAME
) {
1773 this->child
->padding_left
= WD_FRAMETEXT_LEFT
;
1774 this->child
->padding_right
= WD_FRAMETEXT_RIGHT
;
1775 this->child
->padding_top
= max((int)WD_FRAMETEXT_TOP
, this->widget_data
!= STR_NULL
? FONT_HEIGHT_NORMAL
+ WD_FRAMETEXT_TOP
/ 2 : 0);
1776 this->child
->padding_bottom
= WD_FRAMETEXT_BOTTOM
;
1778 this->smallest_x
+= this->child
->padding_left
+ this->child
->padding_right
;
1779 this->smallest_y
+= this->child
->padding_top
+ this->child
->padding_bottom
;
1781 if (this->index
>= 0) w
->SetStringParameters(this->index
);
1782 this->smallest_x
= max(this->smallest_x
, GetStringBoundingBox(this->widget_data
).width
+ WD_FRAMETEXT_LEFT
+ WD_FRAMETEXT_RIGHT
);
1785 Dimension d
= {this->min_x
, this->min_y
};
1786 Dimension fill
= {this->fill_x
, this->fill_y
};
1787 Dimension resize
= {this->resize_x
, this->resize_y
};
1788 if (w
!= nullptr) { // A non-nullptr window pointer acts as switch to turn dynamic widget size on.
1789 if (this->type
== WWT_FRAME
|| this->type
== WWT_INSET
) {
1790 if (this->index
>= 0) w
->SetStringParameters(this->index
);
1791 Dimension background
= GetStringBoundingBox(this->widget_data
);
1792 background
.width
+= (this->type
== WWT_FRAME
) ? (WD_FRAMETEXT_LEFT
+ WD_FRAMERECT_RIGHT
) : (WD_INSET_LEFT
+ WD_INSET_RIGHT
);
1793 d
= maxdim(d
, background
);
1795 if (this->index
>= 0) {
1796 static const Dimension padding
= {0, 0};
1797 w
->UpdateWidgetSize(this->index
, &d
, padding
, &fill
, &resize
);
1800 this->smallest_x
= d
.width
;
1801 this->smallest_y
= d
.height
;
1802 this->fill_x
= fill
.width
;
1803 this->fill_y
= fill
.height
;
1804 this->resize_x
= resize
.width
;
1805 this->resize_y
= resize
.height
;
1809 void NWidgetBackground::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1811 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1813 if (this->child
!= nullptr) {
1814 uint x_offset
= (rtl
? this->child
->padding_right
: this->child
->padding_left
);
1815 uint width
= given_width
- this->child
->padding_right
- this->child
->padding_left
;
1816 uint height
= given_height
- this->child
->padding_top
- this->child
->padding_bottom
;
1817 this->child
->AssignSizePosition(sizing
, x
+ x_offset
, y
+ this->child
->padding_top
, width
, height
, rtl
);
1821 void NWidgetBackground::FillNestedArray(NWidgetBase
**array
, uint length
)
1823 if (this->index
>= 0 && (uint
)(this->index
) < length
) array
[this->index
] = this;
1824 if (this->child
!= nullptr) this->child
->FillNestedArray(array
, length
);
1827 void NWidgetBackground::Draw(const Window
*w
)
1829 if (this->current_x
== 0 || this->current_y
== 0) return;
1832 r
.left
= this->pos_x
;
1833 r
.right
= this->pos_x
+ this->current_x
- 1;
1834 r
.top
= this->pos_y
;
1835 r
.bottom
= this->pos_y
+ this->current_y
- 1;
1837 const DrawPixelInfo
*dpi
= _cur_dpi
;
1838 if (dpi
->left
> r
.right
|| dpi
->left
+ dpi
->width
<= r
.left
|| dpi
->top
> r
.bottom
|| dpi
->top
+ dpi
->height
<= r
.top
) return;
1840 switch (this->type
) {
1842 assert(this->widget_data
== 0);
1843 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, this->colour
, this->IsLowered() ? FR_LOWERED
: FR_NONE
);
1847 if (this->index
>= 0) w
->SetStringParameters(this->index
);
1848 DrawFrame(r
, this->colour
, this->widget_data
);
1852 if (this->index
>= 0) w
->SetStringParameters(this->index
);
1853 DrawInset(r
, this->colour
, this->widget_data
);
1860 if (this->index
>= 0) w
->DrawWidget(r
, this->index
);
1861 if (this->child
!= nullptr) this->child
->Draw(w
);
1863 if (this->IsDisabled()) {
1864 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, _colour_gradient
[this->colour
& 0xF][2], FILLRECT_CHECKER
);
1868 NWidgetCore
*NWidgetBackground::GetWidgetFromPos(int x
, int y
)
1870 NWidgetCore
*nwid
= nullptr;
1871 if (IsInsideBS(x
, this->pos_x
, this->current_x
) && IsInsideBS(y
, this->pos_y
, this->current_y
)) {
1872 if (this->child
!= nullptr) nwid
= this->child
->GetWidgetFromPos(x
, y
);
1873 if (nwid
== nullptr) nwid
= this;
1878 NWidgetBase
*NWidgetBackground::GetWidgetOfType(WidgetType tp
)
1880 NWidgetBase
*nwid
= nullptr;
1881 if (this->child
!= nullptr) nwid
= this->child
->GetWidgetOfType(tp
);
1882 if (nwid
== nullptr && this->type
== tp
) nwid
= this;
1886 NWidgetViewport::NWidgetViewport(int index
) : NWidgetCore(NWID_VIEWPORT
, INVALID_COLOUR
, 1, 1, 0x0, STR_NULL
)
1888 this->SetIndex(index
);
1891 void NWidgetViewport::SetupSmallestSize(Window
*w
, bool init_array
)
1893 if (init_array
&& this->index
>= 0) {
1894 assert(w
->nested_array_size
> (uint
)this->index
);
1895 w
->nested_array
[this->index
] = this;
1897 this->smallest_x
= this->min_x
;
1898 this->smallest_y
= this->min_y
;
1901 void NWidgetViewport::Draw(const Window
*w
)
1903 if (this->disp_flags
& ND_NO_TRANSPARENCY
) {
1904 TransparencyOptionBits to_backup
= _transparency_opt
;
1905 _transparency_opt
&= (1 << TO_SIGNS
) | (1 << TO_LOADING
); // Disable all transparency, except textual stuff
1907 _transparency_opt
= to_backup
;
1912 /* Optionally shade the viewport. */
1913 if (this->disp_flags
& (ND_SHADE_GREY
| ND_SHADE_DIMMED
)) {
1914 GfxFillRect(this->pos_x
, this->pos_y
, this->pos_x
+ this->current_x
- 1, this->pos_y
+ this->current_y
- 1,
1915 (this->disp_flags
& ND_SHADE_DIMMED
) ? PALETTE_TO_TRANSPARENT
: PALETTE_NEWSPAPER
, FILLRECT_RECOLOUR
);
1920 * Initialize the viewport of the window.
1921 * @param w Window owning the viewport.
1922 * @param follow_flags Type of viewport, see #InitializeWindowViewport().
1923 * @param zoom Zoom level.
1925 void NWidgetViewport::InitializeViewport(Window
*w
, uint32 follow_flags
, ZoomLevel zoom
)
1927 InitializeWindowViewport(w
, this->pos_x
, this->pos_y
, this->current_x
, this->current_y
, follow_flags
, zoom
);
1931 * Update the position and size of the viewport (after eg a resize).
1932 * @param w Window owning the viewport.
1934 void NWidgetViewport::UpdateViewportCoordinates(Window
*w
)
1936 ViewPort
*vp
= w
->viewport
;
1937 if (vp
!= nullptr) {
1938 vp
->left
= w
->left
+ this->pos_x
;
1939 vp
->top
= w
->top
+ this->pos_y
;
1940 vp
->width
= this->current_x
;
1941 vp
->height
= this->current_y
;
1943 vp
->virtual_width
= ScaleByZoom(vp
->width
, vp
->zoom
);
1944 vp
->virtual_height
= ScaleByZoom(vp
->height
, vp
->zoom
);
1949 * Compute the row of a scrolled widget that a user clicked in.
1950 * @param clickpos Vertical position of the mouse click (without taking scrolling into account).
1951 * @param w The window the click was in.
1952 * @param widget Widget number of the widget clicked in.
1953 * @param padding Amount of empty space between the widget edge and the top of the first row. Default value is \c 0.
1954 * @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
1955 * @return Row number clicked at. If clicked at a wrong position, #INT_MAX is returned.
1957 int Scrollbar::GetScrolledRowFromWidget(int clickpos
, const Window
* const w
, int widget
, int padding
, int line_height
) const
1959 uint pos
= w
->GetRowFromWidget(clickpos
, widget
, padding
, line_height
);
1960 if (pos
!= INT_MAX
) pos
+= this->GetPosition();
1961 return (pos
>= this->GetCount()) ? INT_MAX
: pos
;
1965 * Set capacity of visible elements from the size and resize properties of a widget.
1967 * @param widget Widget with size and resize properties.
1968 * @param padding Padding to subtract from the size.
1969 * @note Updates the position if needed.
1971 void Scrollbar::SetCapacityFromWidget(Window
*w
, int widget
, int padding
)
1973 NWidgetBase
*nwid
= w
->GetWidget
<NWidgetBase
>(widget
);
1974 if (this->IsVertical()) {
1975 this->SetCapacity(((int)nwid
->current_y
- padding
) / (int)nwid
->resize_y
);
1977 this->SetCapacity(((int)nwid
->current_x
- padding
) / (int)nwid
->resize_x
);
1983 * @param tp Scrollbar type. (horizontal/vertical)
1984 * @param colour Colour of the scrollbar.
1985 * @param index Index in the widget array used by the window system.
1987 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp
, Colours colour
, int index
) : NWidgetCore(tp
, colour
, 1, 1, 0x0, STR_NULL
), Scrollbar(tp
!= NWID_HSCROLLBAR
)
1989 assert(tp
== NWID_HSCROLLBAR
|| tp
== NWID_VSCROLLBAR
);
1990 this->SetIndex(index
);
1993 void NWidgetScrollbar::SetupSmallestSize(Window
*w
, bool init_array
)
1995 if (init_array
&& this->index
>= 0) {
1996 assert(w
->nested_array_size
> (uint
)this->index
);
1997 w
->nested_array
[this->index
] = this;
2002 switch (this->type
) {
2003 case NWID_HSCROLLBAR
:
2004 this->SetMinimalSize(NWidgetScrollbar::GetHorizontalDimension().width
* 3, NWidgetScrollbar::GetHorizontalDimension().height
);
2005 this->SetResize(1, 0);
2006 this->SetFill(1, 0);
2007 this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST
);
2010 case NWID_VSCROLLBAR
:
2011 this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width
, NWidgetScrollbar::GetVerticalDimension().height
* 3);
2012 this->SetResize(0, 1);
2013 this->SetFill(0, 1);
2014 this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST
);
2017 default: NOT_REACHED();
2020 this->smallest_x
= this->min_x
;
2021 this->smallest_y
= this->min_y
;
2024 void NWidgetScrollbar::Draw(const Window
*w
)
2026 if (this->current_x
== 0 || this->current_y
== 0) return;
2029 r
.left
= this->pos_x
;
2030 r
.right
= this->pos_x
+ this->current_x
- 1;
2031 r
.top
= this->pos_y
;
2032 r
.bottom
= this->pos_y
+ this->current_y
- 1;
2034 const DrawPixelInfo
*dpi
= _cur_dpi
;
2035 if (dpi
->left
> r
.right
|| dpi
->left
+ dpi
->width
<= r
.left
|| dpi
->top
> r
.bottom
|| dpi
->top
+ dpi
->height
<= r
.top
) return;
2037 bool up_lowered
= HasBit(this->disp_flags
, NDB_SCROLLBAR_UP
);
2038 bool down_lowered
= HasBit(this->disp_flags
, NDB_SCROLLBAR_DOWN
);
2039 bool middle_lowered
= !(this->disp_flags
& ND_SCROLLBAR_BTN
) && w
->mouse_capture_widget
== this->index
;
2041 if (this->type
== NWID_HSCROLLBAR
) {
2042 DrawHorizontalScrollbar(r
, this->colour
, up_lowered
, middle_lowered
, down_lowered
, this);
2044 DrawVerticalScrollbar(r
, this->colour
, up_lowered
, middle_lowered
, down_lowered
, this);
2047 if (this->IsDisabled()) {
2048 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, _colour_gradient
[this->colour
& 0xF][2], FILLRECT_CHECKER
);
2052 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
2054 vertical_dimension
.width
= vertical_dimension
.height
= 0;
2055 horizontal_dimension
.width
= horizontal_dimension
.height
= 0;
2058 /* static */ Dimension
NWidgetScrollbar::GetVerticalDimension()
2060 static const Dimension extra
= {WD_SCROLLBAR_LEFT
+ WD_SCROLLBAR_RIGHT
, WD_SCROLLBAR_TOP
+ WD_SCROLLBAR_BOTTOM
};
2061 if (vertical_dimension
.width
== 0) {
2062 vertical_dimension
= maxdim(GetSpriteSize(SPR_ARROW_UP
), GetSpriteSize(SPR_ARROW_DOWN
));
2063 vertical_dimension
.width
+= extra
.width
;
2064 vertical_dimension
.height
+= extra
.height
;
2066 return vertical_dimension
;
2069 /* static */ Dimension
NWidgetScrollbar::GetHorizontalDimension()
2071 static const Dimension extra
= {WD_SCROLLBAR_LEFT
+ WD_SCROLLBAR_RIGHT
, WD_SCROLLBAR_TOP
+ WD_SCROLLBAR_BOTTOM
};
2072 if (horizontal_dimension
.width
== 0) {
2073 horizontal_dimension
= maxdim(GetSpriteSize(SPR_ARROW_LEFT
), GetSpriteSize(SPR_ARROW_RIGHT
));
2074 horizontal_dimension
.width
+= extra
.width
;
2075 horizontal_dimension
.height
+= extra
.height
;
2077 return horizontal_dimension
;
2080 Dimension
NWidgetScrollbar::vertical_dimension
= {0, 0};
2081 Dimension
NWidgetScrollbar::horizontal_dimension
= {0, 0};
2083 /** Reset the cached dimensions. */
2084 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
2086 shadebox_dimension
.width
= shadebox_dimension
.height
= 0;
2087 debugbox_dimension
.width
= debugbox_dimension
.height
= 0;
2088 defsizebox_dimension
.width
= defsizebox_dimension
.height
= 0;
2089 stickybox_dimension
.width
= stickybox_dimension
.height
= 0;
2090 resizebox_dimension
.width
= resizebox_dimension
.height
= 0;
2091 closebox_dimension
.width
= closebox_dimension
.height
= 0;
2092 dropdown_dimension
.width
= dropdown_dimension
.height
= 0;
2095 Dimension
NWidgetLeaf::shadebox_dimension
= {0, 0};
2096 Dimension
NWidgetLeaf::debugbox_dimension
= {0, 0};
2097 Dimension
NWidgetLeaf::defsizebox_dimension
= {0, 0};
2098 Dimension
NWidgetLeaf::stickybox_dimension
= {0, 0};
2099 Dimension
NWidgetLeaf::resizebox_dimension
= {0, 0};
2100 Dimension
NWidgetLeaf::closebox_dimension
= {0, 0};
2101 Dimension
NWidgetLeaf::dropdown_dimension
= {0, 0};
2104 * Nested leaf widget.
2105 * @param tp Type of leaf widget.
2106 * @param colour Colour of the leaf widget.
2107 * @param index Index in the widget array used by the window system.
2108 * @param data Data of the widget.
2109 * @param tip Tooltip of the widget.
2111 NWidgetLeaf::NWidgetLeaf(WidgetType tp
, Colours colour
, int index
, uint32 data
, StringID tip
) : NWidgetCore(tp
, colour
, 1, 1, data
, tip
)
2113 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
);
2114 if (index
>= 0) this->SetIndex(index
);
2117 this->SetResize(0, 0);
2125 case WWT_PUSHIMGBTN
:
2128 case WWT_PUSHTXTBTN
:
2133 case NWID_BUTTON_DROPDOWN
:
2134 case NWID_PUSHBUTTON_DROPDOWN
:
2136 case WWT_PUSHARROWBTN
:
2137 this->SetFill(0, 0);
2141 this->SetFill(0, 0);
2145 this->SetFill(1, 0);
2146 this->SetResize(1, 0);
2147 this->min_y
= WD_CAPTION_HEIGHT
;
2148 this->SetDataTip(data
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
);
2152 this->SetFill(0, 0);
2153 this->SetMinimalSize(WD_STICKYBOX_WIDTH
, WD_CAPTION_HEIGHT
);
2154 this->SetDataTip(STR_NULL
, STR_TOOLTIP_STICKY
);
2158 this->SetFill(0, 0);
2159 this->SetMinimalSize(WD_SHADEBOX_TOP
, WD_CAPTION_HEIGHT
);
2160 this->SetDataTip(STR_NULL
, STR_TOOLTIP_SHADE
);
2164 this->SetFill(0, 0);
2165 this->SetMinimalSize(WD_DEBUGBOX_TOP
, WD_CAPTION_HEIGHT
);
2166 this->SetDataTip(STR_NULL
, STR_TOOLTIP_DEBUG
);
2169 case WWT_DEFSIZEBOX
:
2170 this->SetFill(0, 0);
2171 this->SetMinimalSize(WD_DEFSIZEBOX_TOP
, WD_CAPTION_HEIGHT
);
2172 this->SetDataTip(STR_NULL
, STR_TOOLTIP_DEFSIZE
);
2176 this->SetFill(0, 0);
2177 this->SetMinimalSize(WD_RESIZEBOX_WIDTH
, 12);
2178 this->SetDataTip(STR_NULL
, STR_TOOLTIP_RESIZE
);
2182 this->SetFill(0, 0);
2183 this->SetMinimalSize(WD_CLOSEBOX_WIDTH
, WD_CAPTION_HEIGHT
);
2184 this->SetDataTip(STR_NULL
, STR_TOOLTIP_CLOSE_WINDOW
);
2188 this->SetFill(0, 0);
2189 this->min_y
= WD_DROPDOWN_HEIGHT
;
2197 void NWidgetLeaf::SetupSmallestSize(Window
*w
, bool init_array
)
2199 if (this->index
>= 0 && init_array
) { // Fill w->nested_array[]
2200 assert(w
->nested_array_size
> (uint
)this->index
);
2201 w
->nested_array
[this->index
] = this;
2204 Dimension size
= {this->min_x
, this->min_y
};
2205 Dimension fill
= {this->fill_x
, this->fill_y
};
2206 Dimension resize
= {this->resize_x
, this->resize_y
};
2207 /* Get padding, and update size with the real content size if appropriate. */
2208 const Dimension
*padding
= nullptr;
2209 switch (this->type
) {
2211 static const Dimension extra
= {0, 0};
2216 static const Dimension extra
= {WD_MATRIX_LEFT
+ WD_MATRIX_RIGHT
, WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
};
2220 case WWT_SHADEBOX
: {
2221 static const Dimension extra
= {WD_SHADEBOX_LEFT
+ WD_SHADEBOX_RIGHT
, WD_SHADEBOX_TOP
+ WD_SHADEBOX_BOTTOM
};
2223 if (NWidgetLeaf::shadebox_dimension
.width
== 0) {
2224 NWidgetLeaf::shadebox_dimension
= maxdim(GetSpriteSize(SPR_WINDOW_SHADE
), GetSpriteSize(SPR_WINDOW_UNSHADE
));
2225 NWidgetLeaf::shadebox_dimension
.width
+= extra
.width
;
2226 NWidgetLeaf::shadebox_dimension
.height
+= extra
.height
;
2228 size
= maxdim(size
, NWidgetLeaf::shadebox_dimension
);
2232 if (_settings_client
.gui
.newgrf_developer_tools
&& w
->IsNewGRFInspectable()) {
2233 static const Dimension extra
= {WD_DEBUGBOX_LEFT
+ WD_DEBUGBOX_RIGHT
, WD_DEBUGBOX_TOP
+ WD_DEBUGBOX_BOTTOM
};
2235 if (NWidgetLeaf::debugbox_dimension
.width
== 0) {
2236 NWidgetLeaf::debugbox_dimension
= GetSpriteSize(SPR_WINDOW_DEBUG
);
2237 NWidgetLeaf::debugbox_dimension
.width
+= extra
.width
;
2238 NWidgetLeaf::debugbox_dimension
.height
+= extra
.height
;
2240 size
= maxdim(size
, NWidgetLeaf::debugbox_dimension
);
2242 /* If the setting is disabled we don't want to see it! */
2249 case WWT_STICKYBOX
: {
2250 static const Dimension extra
= {WD_STICKYBOX_LEFT
+ WD_STICKYBOX_RIGHT
, WD_STICKYBOX_TOP
+ WD_STICKYBOX_BOTTOM
};
2252 if (NWidgetLeaf::stickybox_dimension
.width
== 0) {
2253 NWidgetLeaf::stickybox_dimension
= maxdim(GetSpriteSize(SPR_PIN_UP
), GetSpriteSize(SPR_PIN_DOWN
));
2254 NWidgetLeaf::stickybox_dimension
.width
+= extra
.width
;
2255 NWidgetLeaf::stickybox_dimension
.height
+= extra
.height
;
2257 size
= maxdim(size
, NWidgetLeaf::stickybox_dimension
);
2261 case WWT_DEFSIZEBOX
: {
2262 static const Dimension extra
= {WD_DEFSIZEBOX_LEFT
+ WD_DEFSIZEBOX_RIGHT
, WD_DEFSIZEBOX_TOP
+ WD_DEFSIZEBOX_BOTTOM
};
2264 if (NWidgetLeaf::defsizebox_dimension
.width
== 0) {
2265 NWidgetLeaf::defsizebox_dimension
= GetSpriteSize(SPR_WINDOW_DEFSIZE
);
2266 NWidgetLeaf::defsizebox_dimension
.width
+= extra
.width
;
2267 NWidgetLeaf::defsizebox_dimension
.height
+= extra
.height
;
2269 size
= maxdim(size
, NWidgetLeaf::defsizebox_dimension
);
2273 case WWT_RESIZEBOX
: {
2274 static const Dimension extra
= {WD_RESIZEBOX_LEFT
+ WD_RESIZEBOX_RIGHT
, WD_RESIZEBOX_TOP
+ WD_RESIZEBOX_BOTTOM
};
2276 if (NWidgetLeaf::resizebox_dimension
.width
== 0) {
2277 NWidgetLeaf::resizebox_dimension
= maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT
), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT
));
2278 NWidgetLeaf::resizebox_dimension
.width
+= extra
.width
;
2279 NWidgetLeaf::resizebox_dimension
.height
+= extra
.height
;
2281 size
= maxdim(size
, NWidgetLeaf::resizebox_dimension
);
2285 Dimension sprite_size
= GetSpriteSize(_current_text_dir
== TD_RTL
? SPR_IMG_DELETE_RIGHT
: SPR_IMG_DELETE_LEFT
);
2286 size
.width
= max(size
.width
, 30 + sprite_size
.width
);
2287 size
.height
= max(sprite_size
.height
, GetStringBoundingBox("_").height
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
2291 static const Dimension extra
= {WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
};
2297 case WWT_PUSHIMGBTN
: {
2298 static const Dimension extra
= {WD_IMGBTN_LEFT
+ WD_IMGBTN_RIGHT
, WD_IMGBTN_TOP
+ WD_IMGBTN_BOTTOM
};
2300 Dimension d2
= GetSpriteSize(this->widget_data
);
2301 if (this->type
== WWT_IMGBTN_2
) d2
= maxdim(d2
, GetSpriteSize(this->widget_data
+ 1));
2302 d2
.width
+= extra
.width
;
2303 d2
.height
+= extra
.height
;
2304 size
= maxdim(size
, d2
);
2308 case WWT_PUSHARROWBTN
: {
2309 static const Dimension extra
= {WD_IMGBTN_LEFT
+ WD_IMGBTN_RIGHT
, WD_IMGBTN_TOP
+ WD_IMGBTN_BOTTOM
};
2311 Dimension d2
= maxdim(GetSpriteSize(SPR_ARROW_LEFT
), GetSpriteSize(SPR_ARROW_RIGHT
));
2312 d2
.width
+= extra
.width
;
2313 d2
.height
+= extra
.height
;
2314 size
= maxdim(size
, d2
);
2318 case WWT_CLOSEBOX
: {
2319 static const Dimension extra
= {WD_CLOSEBOX_LEFT
+ WD_CLOSEBOX_RIGHT
, WD_CLOSEBOX_TOP
+ WD_CLOSEBOX_BOTTOM
};
2321 if (NWidgetLeaf::closebox_dimension
.width
== 0) {
2322 NWidgetLeaf::closebox_dimension
= GetSpriteSize(SPR_CLOSEBOX
);
2323 NWidgetLeaf::closebox_dimension
.width
+= extra
.width
;
2324 NWidgetLeaf::closebox_dimension
.height
+= extra
.height
;
2326 size
= maxdim(size
, NWidgetLeaf::closebox_dimension
);
2330 case WWT_PUSHTXTBTN
:
2331 case WWT_TEXTBTN_2
: {
2332 static const Dimension extra
= {WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
};
2334 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2335 Dimension d2
= GetStringBoundingBox(this->widget_data
);
2336 d2
.width
+= extra
.width
;
2337 d2
.height
+= extra
.height
;
2338 size
= maxdim(size
, d2
);
2343 static const Dimension extra
= {0, 0};
2345 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2346 size
= maxdim(size
, GetStringBoundingBox(this->widget_data
));
2350 static const Dimension extra
= {WD_CAPTIONTEXT_LEFT
+ WD_CAPTIONTEXT_RIGHT
, WD_CAPTIONTEXT_TOP
+ WD_CAPTIONTEXT_BOTTOM
};
2352 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2353 Dimension d2
= GetStringBoundingBox(this->widget_data
);
2354 d2
.width
+= extra
.width
;
2355 d2
.height
+= extra
.height
;
2356 size
= maxdim(size
, d2
);
2360 case NWID_BUTTON_DROPDOWN
:
2361 case NWID_PUSHBUTTON_DROPDOWN
: {
2362 static Dimension extra
= {WD_DROPDOWNTEXT_LEFT
+ WD_DROPDOWNTEXT_RIGHT
, WD_DROPDOWNTEXT_TOP
+ WD_DROPDOWNTEXT_BOTTOM
};
2364 if (NWidgetLeaf::dropdown_dimension
.width
== 0) {
2365 NWidgetLeaf::dropdown_dimension
= GetSpriteSize(SPR_ARROW_DOWN
);
2366 NWidgetLeaf::dropdown_dimension
.width
+= WD_DROPDOWNTEXT_LEFT
+ WD_DROPDOWNTEXT_RIGHT
;
2367 NWidgetLeaf::dropdown_dimension
.height
+= WD_DROPDOWNTEXT_TOP
+ WD_DROPDOWNTEXT_BOTTOM
;
2368 extra
.width
= WD_DROPDOWNTEXT_LEFT
+ WD_DROPDOWNTEXT_RIGHT
+ NWidgetLeaf::dropdown_dimension
.width
;
2370 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2371 Dimension d2
= GetStringBoundingBox(this->widget_data
);
2372 d2
.width
+= extra
.width
;
2373 d2
.height
= max(d2
.height
, NWidgetLeaf::dropdown_dimension
.height
) + extra
.height
;
2374 size
= maxdim(size
, d2
);
2381 if (this->index
>= 0) w
->UpdateWidgetSize(this->index
, &size
, *padding
, &fill
, &resize
);
2383 this->smallest_x
= size
.width
;
2384 this->smallest_y
= size
.height
;
2385 this->fill_x
= fill
.width
;
2386 this->fill_y
= fill
.height
;
2387 this->resize_x
= resize
.width
;
2388 this->resize_y
= resize
.height
;
2391 void NWidgetLeaf::Draw(const Window
*w
)
2393 if (this->current_x
== 0 || this->current_y
== 0) return;
2395 /* Setup a clipping rectangle... */
2396 DrawPixelInfo new_dpi
;
2397 if (!FillDrawPixelInfo(&new_dpi
, this->pos_x
, this->pos_y
, this->current_x
, this->current_y
)) return;
2398 /* ...but keep coordinates relative to the window. */
2399 new_dpi
.left
+= this->pos_x
;
2400 new_dpi
.top
+= this->pos_y
;
2402 DrawPixelInfo
*old_dpi
= _cur_dpi
;
2403 _cur_dpi
= &new_dpi
;
2406 r
.left
= this->pos_x
;
2407 r
.right
= this->pos_x
+ this->current_x
- 1;
2408 r
.top
= this->pos_y
;
2409 r
.bottom
= this->pos_y
+ this->current_y
- 1;
2411 bool clicked
= this->IsLowered();
2412 switch (this->type
) {
2417 assert(this->widget_data
== 0);
2418 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, this->colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
2422 case WWT_PUSHIMGBTN
:
2424 DrawImageButtons(r
, this->type
, this->colour
, clicked
, this->widget_data
);
2428 case WWT_PUSHTXTBTN
:
2430 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2431 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, this->colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
2432 DrawLabel(r
, this->type
, clicked
, this->widget_data
);
2436 case WWT_PUSHARROWBTN
: {
2438 switch (this->widget_data
) {
2439 case AWV_DECREASE
: sprite
= _current_text_dir
!= TD_RTL
? SPR_ARROW_LEFT
: SPR_ARROW_RIGHT
; break;
2440 case AWV_INCREASE
: sprite
= _current_text_dir
== TD_RTL
? SPR_ARROW_LEFT
: SPR_ARROW_RIGHT
; break;
2441 case AWV_LEFT
: sprite
= SPR_ARROW_LEFT
; break;
2442 case AWV_RIGHT
: sprite
= SPR_ARROW_RIGHT
; break;
2443 default: NOT_REACHED();
2445 DrawImageButtons(r
, WWT_PUSHIMGBTN
, this->colour
, clicked
, sprite
);
2450 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2451 DrawLabel(r
, this->type
, clicked
, this->widget_data
);
2455 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2456 DrawText(r
, (TextColour
)this->colour
, this->widget_data
);
2460 DrawMatrix(r
, this->colour
, clicked
, this->widget_data
, this->resize_x
, this->resize_y
);
2464 const QueryString
*query
= w
->GetQueryString(this->index
);
2465 if (query
!= nullptr) query
->DrawEditBox(w
, this->index
);
2470 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2471 DrawCaption(r
, this->colour
, w
->owner
, this->widget_data
);
2475 assert(this->widget_data
== 0);
2476 DrawShadeBox(r
, this->colour
, w
->IsShaded());
2480 DrawDebugBox(r
, this->colour
, clicked
);
2484 assert(this->widget_data
== 0);
2485 DrawStickyBox(r
, this->colour
, !!(w
->flags
& WF_STICKY
));
2488 case WWT_DEFSIZEBOX
:
2489 assert(this->widget_data
== 0);
2490 DrawDefSizeBox(r
, this->colour
, clicked
);
2494 assert(this->widget_data
== 0);
2495 DrawResizeBox(r
, this->colour
, this->pos_x
< (w
->width
/ 2), !!(w
->flags
& WF_SIZING
));
2499 DrawCloseBox(r
, this->colour
);
2503 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2504 DrawDropdown(r
, this->colour
, clicked
, this->widget_data
);
2507 case NWID_BUTTON_DROPDOWN
:
2508 case NWID_PUSHBUTTON_DROPDOWN
:
2509 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2510 DrawButtonDropdown(r
, this->colour
, clicked
, (this->disp_flags
& ND_DROPDOWN_ACTIVE
) != 0, this->widget_data
);
2516 if (this->index
>= 0) w
->DrawWidget(r
, this->index
);
2518 if (this->IsDisabled()) {
2519 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, _colour_gradient
[this->colour
& 0xF][2], FILLRECT_CHECKER
);
2526 * For a #NWID_BUTTON_DROPDOWN, test whether \a pt refers to the button or to the drop-down.
2527 * @param pt Point in the widget.
2528 * @return The point refers to the button.
2530 * @note The magic constants are also used at #DrawButtonDropdown.
2532 bool NWidgetLeaf::ButtonHit(const Point
&pt
)
2534 if (_current_text_dir
== TD_LTR
) {
2535 int button_width
= this->pos_x
+ this->current_x
- NWidgetLeaf::dropdown_dimension
.width
;
2536 return pt
.x
< button_width
;
2538 int button_left
= this->pos_x
+ NWidgetLeaf::dropdown_dimension
.width
;
2539 return pt
.x
>= button_left
;
2543 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
2546 * Construct a single nested widget in \a *dest from its parts.
2548 * Construct a NWidgetBase object from a #NWidget function, and apply all
2549 * settings that follow it, until encountering a #EndContainer, another
2550 * #NWidget, or the end of the parts array.
2552 * @param parts Array with parts of the nested widget.
2553 * @param count Length of the \a parts array.
2554 * @param dest Address of pointer to use for returning the composed widget.
2555 * @param fill_dest Fill the composed widget with child widgets.
2556 * @param biggest_index Pointer to biggest nested widget index in the tree encountered so far.
2557 * @return Number of widget part elements used to compose the widget.
2558 * @pre \c biggest_index != nullptr.
2560 static int MakeNWidget(const NWidgetPart
*parts
, int count
, NWidgetBase
**dest
, bool *fill_dest
, int *biggest_index
)
2567 while (count
> num_used
) {
2568 switch (parts
->type
) {
2570 if (*dest
!= nullptr) return num_used
;
2571 *dest
= new NWidgetSpacer(0, 0);
2574 case NWID_HORIZONTAL
:
2575 if (*dest
!= nullptr) return num_used
;
2576 *dest
= new NWidgetHorizontal(parts
->u
.cont_flags
);
2580 case NWID_HORIZONTAL_LTR
:
2581 if (*dest
!= nullptr) return num_used
;
2582 *dest
= new NWidgetHorizontalLTR(parts
->u
.cont_flags
);
2589 if (*dest
!= nullptr) return num_used
;
2590 *dest
= new NWidgetBackground(parts
->type
, parts
->u
.widget
.colour
, parts
->u
.widget
.index
);
2591 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2596 if (*dest
!= nullptr) return num_used
;
2597 *dest
= new NWidgetVertical(parts
->u
.cont_flags
);
2602 if (*dest
!= nullptr) return num_used
;
2603 NWidgetMatrix
*nwm
= new NWidgetMatrix();
2606 nwm
->SetIndex(parts
->u
.widget
.index
);
2607 nwm
->SetColour(parts
->u
.widget
.colour
);
2608 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2612 case WPT_FUNCTION
: {
2613 if (*dest
!= nullptr) return num_used
;
2614 /* Ensure proper functioning even when the called code simply writes its largest index. */
2616 *dest
= parts
->u
.func_ptr(&biggest
);
2617 *biggest_index
= max(*biggest_index
, biggest
);
2623 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(*dest
);
2624 if (nwrb
!= nullptr) {
2625 assert(parts
->u
.xy
.x
>= 0 && parts
->u
.xy
.y
>= 0);
2626 nwrb
->SetResize(parts
->u
.xy
.x
, parts
->u
.xy
.y
);
2632 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(*dest
);
2633 if (nwrb
!= nullptr) {
2634 assert(parts
->u
.xy
.x
>= 0 && parts
->u
.xy
.y
>= 0);
2635 nwrb
->SetMinimalSize(ScaleGUITrad(parts
->u
.xy
.x
), ScaleGUITrad(parts
->u
.xy
.y
));
2640 case WPT_MINTEXTLINES
: {
2641 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(*dest
);
2642 if (nwrb
!= nullptr) {
2643 assert(parts
->u
.text_lines
.size
>= FS_BEGIN
&& parts
->u
.text_lines
.size
< FS_END
);
2644 nwrb
->SetMinimalTextLines(parts
->u
.text_lines
.lines
, parts
->u
.text_lines
.spacing
, parts
->u
.text_lines
.size
);
2650 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(*dest
);
2651 if (nwrb
!= nullptr) nwrb
->SetFill(parts
->u
.xy
.x
, parts
->u
.xy
.y
);
2656 NWidgetCore
*nwc
= dynamic_cast<NWidgetCore
*>(*dest
);
2657 if (nwc
!= nullptr) {
2658 nwc
->widget_data
= parts
->u
.data_tip
.data
;
2659 nwc
->tool_tip
= parts
->u
.data_tip
.tooltip
;
2665 if (*dest
!= nullptr) (*dest
)->SetPadding(ScaleGUITrad(parts
->u
.padding
.top
), ScaleGUITrad(parts
->u
.padding
.right
), ScaleGUITrad(parts
->u
.padding
.bottom
), ScaleGUITrad(parts
->u
.padding
.left
));
2668 case WPT_PIPSPACE
: {
2669 NWidgetPIPContainer
*nwc
= dynamic_cast<NWidgetPIPContainer
*>(*dest
);
2670 if (nwc
!= nullptr) nwc
->SetPIP(parts
->u
.pip
.pre
, parts
->u
.pip
.inter
, parts
->u
.pip
.post
);
2672 NWidgetBackground
*nwb
= dynamic_cast<NWidgetBackground
*>(*dest
);
2673 if (nwb
!= nullptr) nwb
->SetPIP(parts
->u
.pip
.pre
, parts
->u
.pip
.inter
, parts
->u
.pip
.post
);
2677 case WPT_SCROLLBAR
: {
2678 NWidgetCore
*nwc
= dynamic_cast<NWidgetCore
*>(*dest
);
2679 if (nwc
!= nullptr) {
2680 nwc
->scrollbar_index
= parts
->u
.widget
.index
;
2685 case WPT_ENDCONTAINER
:
2689 if (*dest
!= nullptr) return num_used
;
2690 *dest
= new NWidgetViewport(parts
->u
.widget
.index
);
2691 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2694 case NWID_HSCROLLBAR
:
2695 case NWID_VSCROLLBAR
:
2696 if (*dest
!= nullptr) return num_used
;
2697 *dest
= new NWidgetScrollbar(parts
->type
, parts
->u
.widget
.colour
, parts
->u
.widget
.index
);
2698 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2701 case NWID_SELECTION
: {
2702 if (*dest
!= nullptr) return num_used
;
2703 NWidgetStacked
*nws
= new NWidgetStacked();
2706 nws
->SetIndex(parts
->u
.widget
.index
);
2707 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2712 if (*dest
!= nullptr) return num_used
;
2713 assert((parts
->type
& WWT_MASK
) < WWT_LAST
|| (parts
->type
& WWT_MASK
) == NWID_BUTTON_DROPDOWN
);
2714 *dest
= new NWidgetLeaf(parts
->type
, parts
->u
.widget
.colour
, parts
->u
.widget
.index
, 0x0, STR_NULL
);
2715 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2726 * Build a nested widget tree by recursively filling containers with nested widgets read from their parts.
2727 * @param parts Array with parts of the nested widgets.
2728 * @param count Length of the \a parts array.
2729 * @param parent Pointer or container to use for storing the child widgets (*parent == nullptr or *parent == container or background widget).
2730 * @param biggest_index Pointer to biggest nested widget index in the tree.
2731 * @return Number of widget part elements used to fill the container.
2732 * @post \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used.
2734 static int MakeWidgetTree(const NWidgetPart
*parts
, int count
, NWidgetBase
**parent
, int *biggest_index
)
2736 /* If *parent == nullptr, only the first widget is read and returned. Otherwise, *parent must point to either
2737 * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
2738 NWidgetContainer
*nwid_cont
= dynamic_cast<NWidgetContainer
*>(*parent
);
2739 NWidgetBackground
*nwid_parent
= dynamic_cast<NWidgetBackground
*>(*parent
);
2740 assert(*parent
== nullptr || (nwid_cont
!= nullptr && nwid_parent
== nullptr) || (nwid_cont
== nullptr && nwid_parent
!= nullptr));
2744 NWidgetBase
*sub_widget
= nullptr;
2745 bool fill_sub
= false;
2746 int num_used
= MakeNWidget(parts
, count
- total_used
, &sub_widget
, &fill_sub
, biggest_index
);
2748 total_used
+= num_used
;
2750 /* Break out of loop when end reached */
2751 if (sub_widget
== nullptr) break;
2753 /* If sub-widget is a container, recursively fill that container. */
2754 WidgetType tp
= sub_widget
->type
;
2755 if (fill_sub
&& (tp
== NWID_HORIZONTAL
|| tp
== NWID_HORIZONTAL_LTR
|| tp
== NWID_VERTICAL
|| tp
== NWID_MATRIX
2756 || tp
== WWT_PANEL
|| tp
== WWT_FRAME
|| tp
== WWT_INSET
|| tp
== NWID_SELECTION
)) {
2757 NWidgetBase
*sub_ptr
= sub_widget
;
2758 int num_used
= MakeWidgetTree(parts
, count
- total_used
, &sub_ptr
, biggest_index
);
2760 total_used
+= num_used
;
2763 /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
2764 if (nwid_cont
!= nullptr) nwid_cont
->Add(sub_widget
);
2765 if (nwid_parent
!= nullptr) nwid_parent
->Add(sub_widget
);
2766 if (nwid_cont
== nullptr && nwid_parent
== nullptr) {
2767 *parent
= sub_widget
;
2772 if (count
== total_used
) return total_used
; // Reached the end of the array of parts?
2774 assert(total_used
< count
);
2775 assert(parts
->type
== WPT_ENDCONTAINER
);
2776 return total_used
+ 1; // *parts is also 'used'
2780 * Construct a nested widget tree from an array of parts.
2781 * @param parts Array with parts of the widgets.
2782 * @param count Length of the \a parts array.
2783 * @param biggest_index Pointer to biggest nested widget index collected in the tree.
2784 * @param container Container to add the nested widgets to. In case it is nullptr a vertical container is used.
2785 * @return Root of the nested widget tree, a vertical container containing the entire GUI.
2786 * @ingroup NestedWidgetParts
2787 * @pre \c biggest_index != nullptr
2788 * @post \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used.
2790 NWidgetContainer
*MakeNWidgets(const NWidgetPart
*parts
, int count
, int *biggest_index
, NWidgetContainer
*container
)
2792 *biggest_index
= -1;
2793 if (container
== nullptr) container
= new NWidgetVertical();
2794 NWidgetBase
*cont_ptr
= container
;
2795 MakeWidgetTree(parts
, count
, &cont_ptr
, biggest_index
);
2800 * Make a nested widget tree for a window from a parts array. Besides loading, it inserts a shading selection widget
2801 * 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
2802 * container with a caption widget) and has a shade box widget.
2803 * @param parts Array with parts of the widgets.
2804 * @param count Length of the \a parts array.
2805 * @param biggest_index Pointer to biggest nested widget index collected in the tree.
2806 * @param[out] shade_select Pointer to the inserted shade selection widget (\c nullptr if not unserted).
2807 * @return Root of the nested widget tree, a vertical container containing the entire GUI.
2808 * @ingroup NestedWidgetParts
2809 * @pre \c biggest_index != nullptr
2810 * @post \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used.
2812 NWidgetContainer
*MakeWindowNWidgetTree(const NWidgetPart
*parts
, int count
, int *biggest_index
, NWidgetStacked
**shade_select
)
2814 *biggest_index
= -1;
2816 /* Read the first widget recursively from the array. */
2817 NWidgetBase
*nwid
= nullptr;
2818 int num_used
= MakeWidgetTree(parts
, count
, &nwid
, biggest_index
);
2819 assert(nwid
!= nullptr);
2823 NWidgetContainer
*root
= new NWidgetVertical
;
2825 if (count
== 0) { // There is no body at all.
2826 *shade_select
= nullptr;
2830 /* If the first widget looks like a titlebar, treat it as such.
2831 * If it has a shading box, silently add a shade selection widget in the tree. */
2832 NWidgetHorizontal
*hor_cont
= dynamic_cast<NWidgetHorizontal
*>(nwid
);
2833 NWidgetContainer
*body
;
2834 if (hor_cont
!= nullptr && hor_cont
->GetWidgetOfType(WWT_CAPTION
) != nullptr && hor_cont
->GetWidgetOfType(WWT_SHADEBOX
) != nullptr) {
2835 *shade_select
= new NWidgetStacked
;
2836 root
->Add(*shade_select
);
2837 body
= new NWidgetVertical
;
2838 (*shade_select
)->Add(body
);
2840 *shade_select
= nullptr;
2844 /* Load the remaining parts into 'body'. */
2846 MakeNWidgets(parts
, count
, &biggest2
, body
);
2848 *biggest_index
= max(*biggest_index
, biggest2
);
2853 * Make a number of rows with button-like graphics, for enabling/disabling each company.
2854 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
2855 * @param widget_first The first widget index to use.
2856 * @param widget_last The last widget index to use.
2857 * @param max_length Maximal number of company buttons in one row.
2858 * @param button_tooltip The tooltip-string of every button.
2859 * @return Panel with rows of company buttons.
2860 * @post \c *biggest_index contains the largest used index in the tree.
2862 NWidgetBase
*MakeCompanyButtonRows(int *biggest_index
, int widget_first
, int widget_last
, int max_length
, StringID button_tooltip
)
2864 assert(max_length
>= 1);
2865 NWidgetVertical
*vert
= nullptr; // Storage for all rows.
2866 NWidgetHorizontal
*hor
= nullptr; // Storage for buttons in one row.
2869 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
);
2870 sprite_size
.width
+= WD_MATRIX_LEFT
+ WD_MATRIX_RIGHT
;
2871 sprite_size
.height
+= WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
+ 1; // 1 for the 'offset' of being pressed
2873 for (int widnum
= widget_first
; widnum
<= widget_last
; widnum
++) {
2874 /* Ensure there is room in 'hor' for another button. */
2875 if (hor_length
== max_length
) {
2876 if (vert
== nullptr) vert
= new NWidgetVertical();
2881 if (hor
== nullptr) {
2882 hor
= new NWidgetHorizontal();
2886 NWidgetBackground
*panel
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, widnum
);
2887 panel
->SetMinimalSize(sprite_size
.width
, sprite_size
.height
);
2888 panel
->SetFill(1, 1);
2889 panel
->SetResize(1, 0);
2890 panel
->SetDataTip(0x0, button_tooltip
);
2894 *biggest_index
= widget_last
;
2895 if (vert
== nullptr) return hor
; // All buttons fit in a single row.
2897 if (hor_length
> 0 && hor_length
< max_length
) {
2898 /* Last row is partial, add a spacer at the end to force all buttons to the left. */
2899 NWidgetSpacer
*spc
= new NWidgetSpacer(sprite_size
.width
, sprite_size
.height
);
2901 spc
->SetResize(1, 0);
2904 if (hor
!= nullptr) vert
->Add(hor
);