4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file widget.cpp Handling of the default/simple widgets. */
13 #include "company_func.h"
14 #include "window_gui.h"
15 #include "viewport_func.h"
16 #include "zoom_func.h"
17 #include "strings_func.h"
18 #include "transparency.h"
19 #include "core/geometry_func.hpp"
20 #include "settings_type.h"
21 #include "querystring_gui.h"
23 #include "table/sprites.h"
24 #include "table/strings.h"
25 #include "table/palettes.h"
27 static const char *UPARROW
= "\xEE\x8A\xA0"; ///< String containing an upwards pointing arrow.
28 static const char *DOWNARROW
= "\xEE\x8A\xAA"; ///< String containing a downwards pointing arrow.
31 * Compute the vertical position of the draggable part of scrollbar
32 * @param sb Scrollbar list data
33 * @param top Top position of the scrollbar (top position of the up-button)
34 * @param bottom Bottom position of the scrollbar (bottom position of the down-button)
35 * @param horizontal Whether the scrollbar is horizontal or not
36 * @return A Point, with x containing the top coordinate of the draggable part, and
37 * y containing the bottom coordinate of the draggable part
39 static Point
HandleScrollbarHittest(const Scrollbar
*sb
, int top
, int bottom
, bool horizontal
)
41 /* Base for reversion */
42 int rev_base
= top
+ bottom
;
45 button_size
= NWidgetScrollbar::GetHorizontalDimension().width
;
47 button_size
= NWidgetScrollbar::GetVerticalDimension().height
;
49 top
+= button_size
; // top points to just below the up-button
50 bottom
-= button_size
; // bottom points to top of the down-button
52 int height
= (bottom
- top
);
53 int pos
= sb
->GetPosition();
54 int count
= sb
->GetCount();
55 int cap
= sb
->GetCapacity();
57 if (count
!= 0) top
+= height
* pos
/ count
;
59 if (cap
> count
) cap
= count
;
60 if (count
!= 0) bottom
-= (count
- pos
- cap
) * height
/ count
;
63 if (horizontal
&& _current_text_dir
== TD_RTL
) {
64 pt
.x
= rev_base
- bottom
;
65 pt
.y
= rev_base
- top
;
74 * Compute new position of the scrollbar after a click and updates the window flags.
75 * @param w Window on which a scroll was performed.
77 * @param mi Minimum coordinate of the scroll bar.
78 * @param ma Maximum coordinate of the scroll bar.
79 * @param x The X coordinate of the mouse click.
80 * @param y The Y coordinate of the mouse click.
82 static void ScrollbarClickPositioning(Window
*w
, NWidgetScrollbar
*sb
, int x
, int y
, int mi
, int ma
)
88 if (sb
->type
== NWID_HSCROLLBAR
) {
90 rtl
= _current_text_dir
== TD_RTL
;
91 button_size
= NWidgetScrollbar::GetHorizontalDimension().width
;
94 button_size
= NWidgetScrollbar::GetVerticalDimension().height
;
96 if (pos
< mi
+ button_size
) {
97 /* Pressing the upper button? */
98 SetBit(sb
->disp_flags
, NDB_SCROLLBAR_UP
);
99 if (_scroller_click_timeout
<= 1) {
100 _scroller_click_timeout
= 3;
101 sb
->UpdatePosition(rtl
? 1 : -1);
103 w
->scrolling_scrollbar
= sb
->index
;
104 } else if (pos
>= ma
- button_size
) {
105 /* Pressing the lower button? */
106 SetBit(sb
->disp_flags
, NDB_SCROLLBAR_DOWN
);
108 if (_scroller_click_timeout
<= 1) {
109 _scroller_click_timeout
= 3;
110 sb
->UpdatePosition(rtl
? -1 : 1);
112 w
->scrolling_scrollbar
= sb
->index
;
114 Point pt
= HandleScrollbarHittest(sb
, mi
, ma
, sb
->type
== NWID_HSCROLLBAR
);
117 sb
->UpdatePosition(rtl
? 1 : -1, Scrollbar::SS_BIG
);
118 } else if (pos
> pt
.y
) {
119 sb
->UpdatePosition(rtl
? -1 : 1, Scrollbar::SS_BIG
);
121 _scrollbar_start_pos
= pt
.x
- mi
- button_size
;
122 _scrollbar_size
= ma
- mi
- button_size
* 2;
123 w
->scrolling_scrollbar
= sb
->index
;
124 _cursorpos_drag_start
= _cursor
.pos
;
132 * Special handling for the scrollbar widget type.
133 * Handles the special scrolling buttons and other scrolling.
134 * @param w Window on which a scroll was performed.
135 * @param nw Pointer to the scrollbar widget.
136 * @param x The X coordinate of the mouse click.
137 * @param y The Y coordinate of the mouse click.
139 void ScrollbarClickHandler(Window
*w
, NWidgetCore
*nw
, int x
, int y
)
143 if (nw
->type
== NWID_HSCROLLBAR
) {
145 ma
= nw
->pos_x
+ nw
->current_x
;
148 ma
= nw
->pos_y
+ nw
->current_y
;
150 NWidgetScrollbar
*scrollbar
= dynamic_cast<NWidgetScrollbar
*>(nw
);
151 assert(scrollbar
!= NULL
);
152 ScrollbarClickPositioning(w
, scrollbar
, x
, y
, mi
, ma
);
156 * Returns the index for the widget located at the given position
157 * relative to the window. It includes all widget-corner pixels as well.
158 * @param *w Window to look inside
159 * @param x The Window client X coordinate
160 * @param y The Window client y coordinate
161 * @return A widget index, or -1 if no widget was found.
163 int GetWidgetFromPos(const Window
*w
, int x
, int y
)
165 NWidgetCore
*nw
= w
->nested_root
->GetWidgetFromPos(x
, y
);
166 return (nw
!= NULL
) ? nw
->index
: -1;
170 * Draw frame rectangle.
171 * @param left Left edge of the frame
172 * @param top Top edge of the frame
173 * @param right Right edge of the frame
174 * @param bottom Bottom edge of the frame
175 * @param colour Colour table to use. @see _colour_gradient
176 * @param flags Flags controlling how to draw the frame. @see FrameFlags
178 void DrawFrameRect(int left
, int top
, int right
, int bottom
, Colours colour
, FrameFlags flags
)
180 assert(colour
< COLOUR_END
);
182 uint dark
= _colour_gradient
[colour
][3];
183 uint medium_dark
= _colour_gradient
[colour
][5];
184 uint medium_light
= _colour_gradient
[colour
][6];
185 uint light
= _colour_gradient
[colour
][7];
187 if (flags
& FR_TRANSPARENT
) {
188 GfxFillRect(left
, top
, right
, bottom
, PALETTE_TO_TRANSPARENT
, FILLRECT_RECOLOUR
);
192 if (flags
& FR_LOWERED
) {
193 GfxFillRect(left
, top
, left
, bottom
, dark
);
194 GfxFillRect(left
+ WD_BEVEL_LEFT
, top
, right
, top
, dark
);
195 GfxFillRect(right
, top
+ WD_BEVEL_TOP
, right
, bottom
- WD_BEVEL_BOTTOM
, light
);
196 GfxFillRect(left
+ WD_BEVEL_LEFT
, bottom
, right
, bottom
, light
);
197 interior
= (flags
& FR_DARKENED
? medium_dark
: medium_light
);
199 GfxFillRect(left
, top
, left
, bottom
- WD_BEVEL_BOTTOM
, light
);
200 GfxFillRect(left
+ WD_BEVEL_LEFT
, top
, right
- WD_BEVEL_RIGHT
, top
, light
);
201 GfxFillRect(right
, top
, right
, bottom
- WD_BEVEL_BOTTOM
, dark
);
202 GfxFillRect(left
, bottom
, right
, bottom
, dark
);
203 interior
= medium_dark
;
205 if (!(flags
& FR_BORDERONLY
)) {
206 GfxFillRect(left
+ WD_BEVEL_LEFT
, top
+ WD_BEVEL_TOP
, right
- WD_BEVEL_RIGHT
, bottom
- WD_BEVEL_BOTTOM
, interior
);
212 * Draw an image button.
213 * @param r Rectangle of the button.
214 * @param type Widget type (#WWT_IMGBTN or #WWT_IMGBTN_2).
215 * @param colour Colour of the button.
216 * @param clicked Button is lowered.
217 * @param img Sprite to draw.
219 static inline void DrawImageButtons(const Rect
&r
, WidgetType type
, Colours colour
, bool clicked
, SpriteID img
)
222 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
224 if ((type
& WWT_MASK
) == WWT_IMGBTN_2
&& clicked
) img
++; // Show different image when clicked for #WWT_IMGBTN_2.
225 DrawSprite(img
, PAL_NONE
, r
.left
+ WD_IMGBTN_LEFT
+ clicked
, r
.top
+ WD_IMGBTN_TOP
+ clicked
);
229 * Draw the label-part of a widget.
230 * @param r Rectangle of the label background.
231 * @param type Widget type (#WWT_TEXTBTN, #WWT_TEXTBTN_2, or #WWT_LABEL).
232 * @param clicked Label is rendered lowered.
233 * @param str Text to draw.
235 static inline void DrawLabel(const Rect
&r
, WidgetType type
, bool clicked
, StringID str
)
237 if (str
== STR_NULL
) return;
238 if ((type
& WWT_MASK
) == WWT_TEXTBTN_2
&& clicked
) str
++;
239 Dimension d
= GetStringBoundingBox(str
);
240 int offset
= max(0, ((int)(r
.bottom
- r
.top
+ 1) - (int)d
.height
) / 2); // Offset for rendering the text vertically centered
241 DrawString(r
.left
+ clicked
, r
.right
+ clicked
, r
.top
+ offset
+ clicked
, str
, TC_FROMSTRING
, SA_HOR_CENTER
);
246 * @param r Rectangle of the background.
247 * @param colour Colour of the text.
248 * @param str Text to draw.
250 static inline void DrawText(const Rect
&r
, TextColour colour
, StringID str
)
252 Dimension d
= GetStringBoundingBox(str
);
253 int offset
= max(0, ((int)(r
.bottom
- r
.top
+ 1) - (int)d
.height
) / 2); // Offset for rendering the text vertically centered
254 if (str
!= STR_NULL
) DrawString(r
.left
, r
.right
, r
.top
+ offset
, str
, colour
);
258 * Draw an inset widget.
259 * @param r Rectangle of the background.
260 * @param colour Colour of the inset.
261 * @param str Text to draw.
263 static inline void DrawInset(const Rect
&r
, Colours colour
, StringID str
)
265 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, FR_LOWERED
| FR_DARKENED
);
266 if (str
!= STR_NULL
) DrawString(r
.left
+ WD_INSET_LEFT
, r
.right
- WD_INSET_RIGHT
, r
.top
+ WD_INSET_TOP
, str
);
270 * Draw a matrix widget.
271 * @param r Rectangle of the matrix background.
272 * @param colour Colour of the background.
273 * @param clicked Matrix is rendered lowered.
274 * @param data Data of the widget, number of rows and columns of the widget.
275 * @param resize_x Matrix resize unit size.
276 * @param resize_y Matrix resize unit size.
278 static inline void DrawMatrix(const Rect
&r
, Colours colour
, bool clicked
, uint16 data
, uint resize_x
, uint resize_y
)
280 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
282 int num_columns
= GB(data
, MAT_COL_START
, MAT_COL_BITS
); // Lower 8 bits of the widget data: Number of columns in the matrix.
283 int column_width
; // Width of a single column in the matrix.
284 if (num_columns
== 0) {
285 column_width
= resize_x
;
286 num_columns
= (r
.right
- r
.left
+ 1) / column_width
;
288 column_width
= (r
.right
- r
.left
+ 1) / num_columns
;
291 int num_rows
= GB(data
, MAT_ROW_START
, MAT_ROW_BITS
); // Upper 8 bits of the widget data: Number of rows in the matrix.
292 int row_height
; // Height of a single row in the matrix.
294 row_height
= resize_y
;
295 num_rows
= (r
.bottom
- r
.top
+ 1) / row_height
;
297 row_height
= (r
.bottom
- r
.top
+ 1) / num_rows
;
300 int col
= _colour_gradient
[colour
& 0xF][6];
303 for (int ctr
= num_columns
; ctr
> 1; ctr
--) {
305 GfxFillRect(x
, r
.top
+ 1, x
, r
.bottom
- 1, col
);
309 for (int ctr
= num_rows
; ctr
> 1; ctr
--) {
311 GfxFillRect(r
.left
+ 1, x
, r
.right
- 1, x
, col
);
314 col
= _colour_gradient
[colour
& 0xF][4];
317 for (int ctr
= num_columns
; ctr
> 1; ctr
--) {
319 GfxFillRect(x
, r
.top
+ 1, x
, r
.bottom
- 1, col
);
323 for (int ctr
= num_rows
; ctr
> 1; ctr
--) {
325 GfxFillRect(r
.left
+ 1, x
, r
.right
- 1, x
, col
);
330 * Draw a vertical scrollbar.
331 * @param r Rectangle of the scrollbar widget.
332 * @param colour Colour of the scrollbar widget.
333 * @param up_clicked Up-arrow is clicked.
334 * @param bar_dragged Bar is dragged.
335 * @param down_clicked Down-arrow is clicked.
336 * @param scrollbar Scrollbar size, offset, and capacity information.
338 static inline void DrawVerticalScrollbar(const Rect
&r
, Colours colour
, bool up_clicked
, bool bar_dragged
, bool down_clicked
, const Scrollbar
*scrollbar
)
340 int centre
= (r
.right
- r
.left
) / 2;
341 int height
= NWidgetScrollbar::GetVerticalDimension().height
;
343 /* draw up/down buttons */
344 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.top
+ height
- 1, colour
, (up_clicked
) ? FR_LOWERED
: FR_NONE
);
345 DrawString(r
.left
+ up_clicked
, r
.right
+ up_clicked
, r
.top
+ up_clicked
, UPARROW
, TC_BLACK
, SA_HOR_CENTER
);
347 DrawFrameRect(r
.left
, r
.bottom
- (height
- 1), r
.right
, r
.bottom
, colour
, (down_clicked
) ? FR_LOWERED
: FR_NONE
);
348 DrawString(r
.left
+ down_clicked
, r
.right
+ down_clicked
, r
.bottom
- (height
- 1) + down_clicked
, DOWNARROW
, TC_BLACK
, SA_HOR_CENTER
);
350 int c1
= _colour_gradient
[colour
& 0xF][3];
351 int c2
= _colour_gradient
[colour
& 0xF][7];
353 /* draw "shaded" background */
354 GfxFillRect(r
.left
, r
.top
+ height
, r
.right
, r
.bottom
- height
, c2
);
355 GfxFillRect(r
.left
, r
.top
+ height
, r
.right
, r
.bottom
- height
, c1
, FILLRECT_CHECKER
);
357 /* draw shaded lines */
358 GfxFillRect(r
.left
+ centre
- 3, r
.top
+ height
, r
.left
+ centre
- 3, r
.bottom
- height
, c1
);
359 GfxFillRect(r
.left
+ centre
- 2, r
.top
+ height
, r
.left
+ centre
- 2, r
.bottom
- height
, c2
);
360 GfxFillRect(r
.left
+ centre
+ 2, r
.top
+ height
, r
.left
+ centre
+ 2, r
.bottom
- height
, c1
);
361 GfxFillRect(r
.left
+ centre
+ 3, r
.top
+ height
, r
.left
+ centre
+ 3, r
.bottom
- height
, c2
);
363 Point pt
= HandleScrollbarHittest(scrollbar
, r
.top
, r
.bottom
, false);
364 DrawFrameRect(r
.left
, pt
.x
, r
.right
, pt
.y
, colour
, bar_dragged
? FR_LOWERED
: FR_NONE
);
368 * Draw a horizontal scrollbar.
369 * @param r Rectangle of the scrollbar widget.
370 * @param colour Colour of the scrollbar widget.
371 * @param left_clicked Left-arrow is clicked.
372 * @param bar_dragged Bar is dragged.
373 * @param right_clicked Right-arrow is clicked.
374 * @param scrollbar Scrollbar size, offset, and capacity information.
376 static inline void DrawHorizontalScrollbar(const Rect
&r
, Colours colour
, bool left_clicked
, bool bar_dragged
, bool right_clicked
, const Scrollbar
*scrollbar
)
378 int centre
= (r
.bottom
- r
.top
) / 2;
379 int width
= NWidgetScrollbar::GetHorizontalDimension().width
;
381 DrawFrameRect(r
.left
, r
.top
, r
.left
+ width
- 1, r
.bottom
, colour
, left_clicked
? FR_LOWERED
: FR_NONE
);
382 DrawSprite(SPR_ARROW_LEFT
, PAL_NONE
, r
.left
+ 1 + left_clicked
, r
.top
+ 1 + left_clicked
);
384 DrawFrameRect(r
.right
- (width
- 1), r
.top
, r
.right
, r
.bottom
, colour
, right_clicked
? FR_LOWERED
: FR_NONE
);
385 DrawSprite(SPR_ARROW_RIGHT
, PAL_NONE
, r
.right
- (width
- 2) + right_clicked
, r
.top
+ 1 + right_clicked
);
387 int c1
= _colour_gradient
[colour
& 0xF][3];
388 int c2
= _colour_gradient
[colour
& 0xF][7];
390 /* draw "shaded" background */
391 GfxFillRect(r
.left
+ width
, r
.top
, r
.right
- width
, r
.bottom
, c2
);
392 GfxFillRect(r
.left
+ width
, r
.top
, r
.right
- width
, r
.bottom
, c1
, FILLRECT_CHECKER
);
394 /* draw shaded lines */
395 GfxFillRect(r
.left
+ width
, r
.top
+ centre
- 3, r
.right
- width
, r
.top
+ centre
- 3, c1
);
396 GfxFillRect(r
.left
+ width
, r
.top
+ centre
- 2, r
.right
- width
, r
.top
+ centre
- 2, c2
);
397 GfxFillRect(r
.left
+ width
, r
.top
+ centre
+ 2, r
.right
- width
, r
.top
+ centre
+ 2, c1
);
398 GfxFillRect(r
.left
+ width
, r
.top
+ centre
+ 3, r
.right
- width
, r
.top
+ centre
+ 3, c2
);
400 /* draw actual scrollbar */
401 Point pt
= HandleScrollbarHittest(scrollbar
, r
.left
, r
.right
, true);
402 DrawFrameRect(pt
.x
, r
.top
, pt
.y
, r
.bottom
, colour
, bar_dragged
? FR_LOWERED
: FR_NONE
);
406 * Draw a frame widget.
407 * @param r Rectangle of the frame.
408 * @param colour Colour of the frame.
409 * @param str Text of the frame.
411 static inline void DrawFrame(const Rect
&r
, Colours colour
, StringID str
)
413 int x2
= r
.left
; // by default the left side is the left side of the widget
415 if (str
!= STR_NULL
) x2
= DrawString(r
.left
+ WD_FRAMETEXT_LEFT
, r
.right
- WD_FRAMETEXT_RIGHT
, r
.top
, str
);
417 int c1
= _colour_gradient
[colour
][3];
418 int c2
= _colour_gradient
[colour
][7];
420 /* If the frame has text, adjust the top bar to fit half-way through */
422 if (str
!= STR_NULL
) dy1
= FONT_HEIGHT_NORMAL
/ 2 - 1;
425 if (_current_text_dir
== TD_LTR
) {
426 /* Line from upper left corner to start of text */
427 GfxFillRect(r
.left
, r
.top
+ dy1
, r
.left
+ 4, r
.top
+ dy1
, c1
);
428 GfxFillRect(r
.left
+ 1, r
.top
+ dy2
, r
.left
+ 4, r
.top
+ dy2
, c2
);
430 /* Line from end of text to upper right corner */
431 GfxFillRect(x2
, r
.top
+ dy1
, r
.right
- 1, r
.top
+ dy1
, c1
);
432 GfxFillRect(x2
, r
.top
+ dy2
, r
.right
- 2, r
.top
+ dy2
, c2
);
434 /* Line from upper left corner to start of text */
435 GfxFillRect(r
.left
, r
.top
+ dy1
, x2
- 2, r
.top
+ dy1
, c1
);
436 GfxFillRect(r
.left
+ 1, r
.top
+ dy2
, x2
- 2, r
.top
+ dy2
, c2
);
438 /* Line from end of text to upper right corner */
439 GfxFillRect(r
.right
- 5, r
.top
+ dy1
, r
.right
- 1, r
.top
+ dy1
, c1
);
440 GfxFillRect(r
.right
- 5, r
.top
+ dy2
, r
.right
- 2, r
.top
+ dy2
, c2
);
443 /* Line from upper left corner to bottom left corner */
444 GfxFillRect(r
.left
, r
.top
+ dy2
, r
.left
, r
.bottom
- 1, c1
);
445 GfxFillRect(r
.left
+ 1, r
.top
+ dy2
+ 1, r
.left
+ 1, r
.bottom
- 2, c2
);
447 /* Line from upper right corner to bottom right corner */
448 GfxFillRect(r
.right
- 1, r
.top
+ dy2
, r
.right
- 1, r
.bottom
- 2, c1
);
449 GfxFillRect(r
.right
, r
.top
+ dy1
, r
.right
, r
.bottom
- 1, c2
);
451 GfxFillRect(r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1, c1
);
452 GfxFillRect(r
.left
, r
.bottom
, r
.right
, r
.bottom
, c2
);
457 * @param r Rectangle of the box.
458 * @param colour Colour of the shade box.
459 * @param clicked Box is lowered.
461 static inline void DrawShadeBox(const Rect
&r
, Colours colour
, bool clicked
)
463 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
464 DrawSprite((clicked
) ? SPR_WINDOW_SHADE
: SPR_WINDOW_UNSHADE
, PAL_NONE
, r
.left
+ WD_SHADEBOX_LEFT
+ clicked
, r
.top
+ WD_SHADEBOX_TOP
+ clicked
);
469 * @param r Rectangle of the box.
470 * @param colour Colour of the sticky box.
471 * @param clicked Box is lowered.
473 static inline void DrawStickyBox(const Rect
&r
, Colours colour
, bool clicked
)
475 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
476 DrawSprite((clicked
) ? SPR_PIN_UP
: SPR_PIN_DOWN
, PAL_NONE
, r
.left
+ WD_STICKYBOX_LEFT
+ clicked
, r
.top
+ WD_STICKYBOX_TOP
+ clicked
);
480 * Draw a defsize box.
481 * @param r Rectangle of the box.
482 * @param colour Colour of the defsize box.
483 * @param clicked Box is lowered.
485 static inline void DrawDefSizeBox(const Rect
&r
, Colours colour
, bool clicked
)
487 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
488 DrawSprite(SPR_WINDOW_DEFSIZE
, PAL_NONE
, r
.left
+ WD_DEFSIZEBOX_LEFT
+ clicked
, r
.top
+ WD_DEFSIZEBOX_TOP
+ clicked
);
492 * Draw a NewGRF debug box.
493 * @param r Rectangle of the box.
494 * @param colour Colour of the debug box.
495 * @param clicked Box is lowered.
497 static inline void DrawDebugBox(const Rect
&r
, Colours colour
, bool clicked
)
499 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
500 DrawSprite(SPR_WINDOW_DEBUG
, PAL_NONE
, r
.left
+ WD_DEBUGBOX_LEFT
+ clicked
, r
.top
+ WD_DEBUGBOX_TOP
+ clicked
);
505 * @param r Rectangle of the box.
506 * @param colour Colour of the resize box.
507 * @param at_left Resize box is at left-side of the window,
508 * @param clicked Box is lowered.
510 static inline void DrawResizeBox(const Rect
&r
, Colours colour
, bool at_left
, bool clicked
)
512 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
514 DrawSprite(SPR_WINDOW_RESIZE_LEFT
, PAL_NONE
, r
.left
+ WD_RESIZEBOX_RIGHT
+ clicked
,
515 r
.bottom
- WD_RESIZEBOX_BOTTOM
- GetSpriteSize(SPR_WINDOW_RESIZE_LEFT
).height
+ clicked
);
517 DrawSprite(SPR_WINDOW_RESIZE_RIGHT
, PAL_NONE
, r
.left
+ WD_RESIZEBOX_LEFT
+ clicked
,
518 r
.bottom
- WD_RESIZEBOX_BOTTOM
- GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT
).height
+ clicked
);
524 * @param r Rectangle of the box.
525 * @param colour Colour of the close box.
526 * @param str Cross to draw (#STR_BLACK_CROSS or #STR_SILVER_CROSS).
528 static inline void DrawCloseBox(const Rect
&r
, Colours colour
, StringID str
)
530 assert(str
== STR_BLACK_CROSS
|| str
== STR_SILVER_CROSS
); // black or silver cross
531 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, FR_NONE
);
532 DrawString(r
.left
, r
.right
, r
.top
+ WD_CLOSEBOX_TOP
, str
, TC_FROMSTRING
, SA_HOR_CENTER
);
536 * Draw a caption bar.
537 * @param r Rectangle of the bar.
538 * @param colour Colour of the window.
539 * @param owner 'Owner' of the window.
540 * @param str Text to draw in the bar.
542 void DrawCaption(const Rect
&r
, Colours colour
, Owner owner
, StringID str
)
544 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, colour
, FR_BORDERONLY
);
545 DrawFrameRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, colour
, (owner
== INVALID_OWNER
) ? FR_LOWERED
| FR_DARKENED
: FR_LOWERED
| FR_DARKENED
| FR_BORDERONLY
);
547 if (owner
!= INVALID_OWNER
) {
548 GfxFillRect(r
.left
+ 2, r
.top
+ 2, r
.right
- 2, r
.bottom
- 2, _colour_gradient
[_company_colours
[owner
]][4]);
551 if (str
!= STR_NULL
) {
552 Dimension d
= GetStringBoundingBox(str
);
553 int offset
= max(0, ((int)(r
.bottom
- r
.top
+ 1) - (int)d
.height
) / 2); // Offset for rendering the text vertically centered
554 DrawString(r
.left
+ WD_CAPTIONTEXT_LEFT
, r
.right
- WD_CAPTIONTEXT_RIGHT
, r
.top
+ offset
, str
, TC_FROMSTRING
, SA_HOR_CENTER
);
559 * Draw a button with a dropdown (#WWT_DROPDOWN and #NWID_BUTTON_DROPDOWN).
560 * @param r Rectangle containing the widget.
561 * @param colour Background colour of the widget.
562 * @param clicked_button The button-part is lowered.
563 * @param clicked_dropdown The drop-down part is lowered.
564 * @param str Text of the button.
566 * @note Magic constants are also used in #NWidgetLeaf::ButtonHit.
568 static inline void DrawButtonDropdown(const Rect
&r
, Colours colour
, bool clicked_button
, bool clicked_dropdown
, StringID str
)
570 int text_offset
= max(0, ((int)(r
.bottom
- r
.top
+ 1) - FONT_HEIGHT_NORMAL
) / 2); // Offset for rendering the text vertically centered
572 int dd_width
= NWidgetLeaf::dropdown_dimension
.width
;
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 DrawString(r
.right
- dd_width
+ (clicked_dropdown
? 2 : 1), r
.right
, r
.top
+ (clicked_dropdown
? 2 : 1), DOWNARROW
, TC_BLACK
, SA_HOR_CENTER
);
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 DrawString(r
.left
+ (clicked_dropdown
? 2 : 1), r
.left
+ dd_width
, r
.top
+ (clicked_dropdown
? 2 : 1), DOWNARROW
, TC_BLACK
, SA_HOR_CENTER
);
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
== NULL
|| !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
!= NULL
);
641 const NWidgetBase
*nwid
= this->GetWidget
<NWidgetBase
>(widget
);
643 int offset
= this->IsWidgetLowered(widget
) ? 1 : 0;
644 int base
= offset
+ nwid
->pos_x
+ (_current_text_dir
== TD_LTR
? nwid
->current_x
- WD_SORTBUTTON_ARROW_WIDTH
: 0);
645 int top
= nwid
->pos_y
;
647 DrawString(base
, base
+ WD_SORTBUTTON_ARROW_WIDTH
, top
+ 1 + offset
, state
== SBS_DOWN
? DOWNARROW
: UPARROW
, TC_BLACK
, SA_HOR_CENTER
);
652 * @defgroup NestedWidgets Hierarchical widgets
653 * Hierarchical widgets, also known as nested widgets, are widgets stored in a tree. At the leafs of the tree are (mostly) the 'real' widgets
654 * visible to the user. At higher levels, widgets get organized in container widgets, until all widgets of the window are merged.
656 * \section nestedwidgetkinds Hierarchical widget kinds
657 * A leaf widget is one of
659 * <li> #NWidgetLeaf for widgets visible for the user, or
660 * <li> #NWidgetSpacer for creating (flexible) empty space between widgets.
662 * The purpose of a leaf widget is to provide interaction with the user by displaying settings, and/or allowing changing the settings.
664 * A container widget is one of
666 * <li> #NWidgetHorizontal for organizing child widgets in a (horizontal) row. The row switches order depending on the language setting (thus supporting
667 * right-to-left languages),
668 * <li> #NWidgetHorizontalLTR for organizing child widgets in a (horizontal) row, always in the same order. All children below this container will also
670 * <li> #NWidgetVertical for organizing child widgets underneath each other.
671 * <li> #NWidgetMatrix for organizing child widgets in a matrix form.
672 * <li> #NWidgetBackground for adding a background behind its child widget.
673 * <li> #NWidgetStacked for stacking child widgets on top of each other.
675 * The purpose of a container widget is to structure its leafs and sub-containers to allow proper resizing.
677 * \section nestedwidgetscomputations Hierarchical widget computations
678 * 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,
679 * or by means of specifying the tree as a collection of nested widgets parts and instantiating the tree from the array.
681 * After the creation step,
682 * - 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).
683 * - Containers only know what their children are, \e fill_x, \e fill_y, \e resize_x, and \e resize_y are not initialized.
685 * Computations in the nested widgets take place as follows:
687 * <li> A bottom-up sweep by recursively calling NWidgetBase::SetupSmallestSize() to initialize the smallest size (\e smallest_x, \e smallest_y) and
688 * to propagate filling and resize steps upwards to the root of the tree.
689 * <li> A top-down sweep by recursively calling NWidgetBase::AssignSizePosition() with #ST_SMALLEST to make the smallest sizes consistent over
690 * 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
691 * 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.
692 * <li> After initializing the smallest size in the widget tree with #ST_SMALLEST, the tree can be resized (the current size modified) by calling
693 * 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
694 * 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}).
696 * After the second step, the current size of the widgets are set to the smallest size.
698 * To resize, perform the last step with the new window size. This can be done as often as desired.
699 * When the smallest size of at least one widget changes, the whole procedure has to be redone from the start.
701 * @see NestedWidgetParts
705 * Base class constructor.
706 * @param tp Nested widget type.
708 NWidgetBase::NWidgetBase(WidgetType tp
) : ZeroedMemoryAllocator()
713 /* ~NWidgetContainer() takes care of #next and #prev data members. */
716 * @fn void NWidgetBase::SetupSmallestSize(Window *w, bool init_array)
717 * Compute smallest size needed by the widget.
719 * The smallest size of a widget is the smallest size that a widget needs to
720 * display itself properly. In addition, filling and resizing of the widget are computed.
721 * The function calls #Window::UpdateWidgetSize for each leaf widget and
722 * background widget without child with a non-negative index.
724 * @param w Window owning the widget.
725 * @param init_array Initialize the \c w->nested_array.
727 * @note After the computation, the results can be queried by accessing the #smallest_x and #smallest_y data members of the widget.
731 * @fn void NWidgetBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
732 * Assign size and position to the widget.
733 * @param sizing Type of resizing to perform.
734 * @param x Horizontal offset of the widget relative to the left edge of the window.
735 * @param y Vertical offset of the widget relative to the top edge of the window.
736 * @param given_width Width allocated to the widget.
737 * @param given_height Height allocated to the widget.
738 * @param rtl Adapt for right-to-left languages (position contents of horizontal containers backwards).
740 * Afterwards, \e pos_x and \e pos_y contain the top-left position of the widget, \e smallest_x and \e smallest_y contain
741 * the smallest size such that all widgets of the window are consistent, and \e current_x and \e current_y contain the current size.
745 * @fn void FillNestedArray(NWidgetBase **array, uint length)
746 * Fill the Window::nested_array array with pointers to nested widgets in the tree.
747 * @param array Base pointer of the array.
748 * @param length Length of the array.
752 * @fn void NWidgetBase::Draw(const Window *w)
753 * Draw the widgets of the tree.
754 * The function calls #Window::DrawWidget for each widget with a non-negative index, after the widget itself is painted.
755 * @param w Window that owns the tree.
759 * Mark the widget as 'dirty' (in need of repaint).
760 * @param w Window owning the widget.
762 void NWidgetBase::SetDirty(const Window
*w
) const
764 int abs_left
= w
->left
+ this->pos_x
;
765 int abs_top
= w
->top
+ this->pos_y
;
766 SetDirtyBlocks(abs_left
, abs_top
, abs_left
+ this->current_x
, abs_top
+ this->current_y
);
770 * @fn NWidgetCore *NWidgetBase::GetWidgetFromPos(int x, int y)
771 * Retrieve a widget by its position.
772 * @param x Horizontal position relative to the left edge of the window.
773 * @param y Vertical position relative to the top edge of the window.
774 * @return Returns the deepest nested widget that covers the given position, or \c NULL if no widget can be found.
778 * Retrieve a widget by its type.
779 * @param tp Widget type to search for.
780 * @return Returns the first widget of the specified type, or \c NULL if no widget can be found.
782 NWidgetBase
*NWidgetBase::GetWidgetOfType(WidgetType tp
)
784 return (this->type
== tp
) ? this : NULL
;
788 * Constructor for resizable nested widgets.
789 * @param tp Nested widget type.
790 * @param fill_x Horizontal fill step size, \c 0 means no filling is allowed.
791 * @param fill_y Vertical fill step size, \c 0 means no filling is allowed.
793 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp
, uint fill_x
, uint fill_y
) : NWidgetBase(tp
)
795 this->fill_x
= fill_x
;
796 this->fill_y
= fill_y
;
800 * Set minimal size of the widget.
801 * @param min_x Horizontal minimal size of the widget.
802 * @param min_y Vertical minimal size of the widget.
804 void NWidgetResizeBase::SetMinimalSize(uint min_x
, uint min_y
)
811 * Set minimal text lines for the widget.
812 * @param min_lines Number of text lines of the widget.
813 * @param spacing Extra spacing (eg WD_FRAMERECT_TOP + _BOTTOM) of the widget.
814 * @param size Font size of text.
816 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines
, uint8 spacing
, FontSize size
)
818 this->min_y
= min_lines
* GetCharacterHeight(size
) + spacing
;
822 * Set the filling of the widget from initial size.
823 * @param fill_x Horizontal fill step size, \c 0 means no filling is allowed.
824 * @param fill_y Vertical fill step size, \c 0 means no filling is allowed.
826 void NWidgetResizeBase::SetFill(uint fill_x
, uint fill_y
)
828 this->fill_x
= fill_x
;
829 this->fill_y
= fill_y
;
833 * Set resize step of the widget.
834 * @param resize_x Resize step in horizontal direction, value \c 0 means no resize, otherwise the step size in pixels.
835 * @param resize_y Resize step in vertical direction, value \c 0 means no resize, otherwise the step size in pixels.
837 void NWidgetResizeBase::SetResize(uint resize_x
, uint resize_y
)
839 this->resize_x
= resize_x
;
840 this->resize_y
= resize_y
;
843 void NWidgetResizeBase::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
845 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
849 * Initialization of a 'real' widget.
850 * @param tp Type of the widget.
851 * @param colour Colour of the widget.
852 * @param fill_x Default horizontal filling.
853 * @param fill_y Default vertical filling.
854 * @param widget_data Data component of the widget. @see Widget::data
855 * @param tool_tip Tool tip of the widget. @see Widget::tooltips
857 NWidgetCore::NWidgetCore(WidgetType tp
, Colours colour
, uint fill_x
, uint fill_y
, uint32 widget_data
, StringID tool_tip
) : NWidgetResizeBase(tp
, fill_x
, fill_y
)
859 this->colour
= colour
;
861 this->widget_data
= widget_data
;
862 this->tool_tip
= tool_tip
;
863 this->scrollbar_index
= -1;
867 * Set index of the nested widget in the widget array.
868 * @param index Index to use.
870 void NWidgetCore::SetIndex(int index
)
877 * Set data and tool tip of the nested widget.
878 * @param widget_data Data to use.
879 * @param tool_tip Tool tip string to use.
881 void NWidgetCore::SetDataTip(uint32 widget_data
, StringID tool_tip
)
883 this->widget_data
= widget_data
;
884 this->tool_tip
= tool_tip
;
887 void NWidgetCore::FillNestedArray(NWidgetBase
**array
, uint length
)
889 if (this->index
>= 0 && (uint
)(this->index
) < length
) array
[this->index
] = this;
892 NWidgetCore
*NWidgetCore::GetWidgetFromPos(int x
, int y
)
894 return (IsInsideBS(x
, this->pos_x
, this->current_x
) && IsInsideBS(y
, this->pos_y
, this->current_y
)) ? this : NULL
;
898 * Constructor container baseclass.
899 * @param tp Type of the container.
901 NWidgetContainer::NWidgetContainer(WidgetType tp
) : NWidgetBase(tp
)
907 NWidgetContainer::~NWidgetContainer()
909 while (this->head
!= NULL
) {
910 NWidgetBase
*wid
= this->head
->next
;
917 NWidgetBase
*NWidgetContainer::GetWidgetOfType(WidgetType tp
)
919 if (this->type
== tp
) return this;
920 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
921 NWidgetBase
*nwid
= child_wid
->GetWidgetOfType(tp
);
922 if (nwid
!= NULL
) return nwid
;
928 * Append widget \a wid to container.
929 * @param wid Widget to append.
931 void NWidgetContainer::Add(NWidgetBase
*wid
)
933 assert(wid
->next
== NULL
&& wid
->prev
== NULL
);
935 if (this->head
== NULL
) {
939 assert(this->tail
!= NULL
);
940 assert(this->tail
->next
== NULL
);
942 this->tail
->next
= wid
;
943 wid
->prev
= this->tail
;
948 void NWidgetContainer::FillNestedArray(NWidgetBase
**array
, uint length
)
950 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
951 child_wid
->FillNestedArray(array
, length
);
956 * Widgets stacked on top of each other.
958 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION
)
963 void NWidgetStacked::SetIndex(int index
)
968 void NWidgetStacked::SetupSmallestSize(Window
*w
, bool init_array
)
970 if (this->index
>= 0 && init_array
) { // Fill w->nested_array[]
971 assert(w
->nested_array_size
> (uint
)this->index
);
972 w
->nested_array
[this->index
] = this;
975 /* Zero size plane selected */
976 if (this->shown_plane
>= SZSP_BEGIN
) {
977 Dimension size
= {0, 0};
978 Dimension padding
= {0, 0};
979 Dimension fill
= {(this->shown_plane
== SZSP_HORIZONTAL
), (this->shown_plane
== SZSP_VERTICAL
)};
980 Dimension resize
= {(this->shown_plane
== SZSP_HORIZONTAL
), (this->shown_plane
== SZSP_VERTICAL
)};
981 /* Here we're primarily interested in the value of resize */
982 if (this->index
>= 0) w
->UpdateWidgetSize(this->index
, &size
, padding
, &fill
, &resize
);
984 this->smallest_x
= size
.width
;
985 this->smallest_y
= size
.height
;
986 this->fill_x
= fill
.width
;
987 this->fill_y
= fill
.height
;
988 this->resize_x
= resize
.width
;
989 this->resize_y
= resize
.height
;
993 /* First sweep, recurse down and compute minimal size and filling. */
994 this->smallest_x
= 0;
995 this->smallest_y
= 0;
996 this->fill_x
= (this->head
!= NULL
) ? 1 : 0;
997 this->fill_y
= (this->head
!= NULL
) ? 1 : 0;
998 this->resize_x
= (this->head
!= NULL
) ? 1 : 0;
999 this->resize_y
= (this->head
!= NULL
) ? 1 : 0;
1000 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1001 child_wid
->SetupSmallestSize(w
, init_array
);
1003 this->smallest_x
= max(this->smallest_x
, child_wid
->smallest_x
+ child_wid
->padding_left
+ child_wid
->padding_right
);
1004 this->smallest_y
= max(this->smallest_y
, child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
);
1005 this->fill_x
= LeastCommonMultiple(this->fill_x
, child_wid
->fill_x
);
1006 this->fill_y
= LeastCommonMultiple(this->fill_y
, child_wid
->fill_y
);
1007 this->resize_x
= LeastCommonMultiple(this->resize_x
, child_wid
->resize_x
);
1008 this->resize_y
= LeastCommonMultiple(this->resize_y
, child_wid
->resize_y
);
1012 void NWidgetStacked::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1014 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1015 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1017 if (this->shown_plane
>= SZSP_BEGIN
) return;
1019 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1020 uint hor_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetHorizontalStepSize(sizing
);
1021 uint child_width
= ComputeMaxSize(child_wid
->smallest_x
, given_width
- child_wid
->padding_left
- child_wid
->padding_right
, hor_step
);
1022 uint child_pos_x
= (rtl
? child_wid
->padding_right
: child_wid
->padding_left
);
1024 uint vert_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetVerticalStepSize(sizing
);
1025 uint child_height
= ComputeMaxSize(child_wid
->smallest_y
, given_height
- child_wid
->padding_top
- child_wid
->padding_bottom
, vert_step
);
1026 uint child_pos_y
= child_wid
->padding_top
;
1028 child_wid
->AssignSizePosition(sizing
, x
+ child_pos_x
, y
+ child_pos_y
, child_width
, child_height
, rtl
);
1032 void NWidgetStacked::FillNestedArray(NWidgetBase
**array
, uint length
)
1034 if (this->index
>= 0 && (uint
)(this->index
) < length
) array
[this->index
] = this;
1035 NWidgetContainer::FillNestedArray(array
, length
);
1038 void NWidgetStacked::Draw(const Window
*w
)
1040 if (this->shown_plane
>= SZSP_BEGIN
) return;
1043 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; plane
++, child_wid
= child_wid
->next
) {
1044 if (plane
== this->shown_plane
) {
1053 NWidgetCore
*NWidgetStacked::GetWidgetFromPos(int x
, int y
)
1055 if (this->shown_plane
>= SZSP_BEGIN
) return NULL
;
1057 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return NULL
;
1059 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; plane
++, child_wid
= child_wid
->next
) {
1060 if (plane
== this->shown_plane
) {
1061 return child_wid
->GetWidgetFromPos(x
, y
);
1068 * Select which plane to show (for #NWID_SELECTION only).
1069 * @param plane Plane number to display.
1071 void NWidgetStacked::SetDisplayedPlane(int plane
)
1073 this->shown_plane
= plane
;
1076 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp
, NWidContainerFlags flags
) : NWidgetContainer(tp
)
1078 this->flags
= flags
;
1082 * Set additional pre/inter/post space for the container.
1084 * @param pip_pre Additional space in front of the first child widget (above
1085 * for the vertical container, at the left for the horizontal container).
1086 * @param pip_inter Additional space between two child widgets.
1087 * @param pip_post Additional space after the last child widget (below for the
1088 * vertical container, at the right for the horizontal container).
1090 void NWidgetPIPContainer::SetPIP(uint8 pip_pre
, uint8 pip_inter
, uint8 pip_post
)
1092 this->pip_pre
= pip_pre
;
1093 this->pip_inter
= pip_inter
;
1094 this->pip_post
= pip_post
;
1097 void NWidgetPIPContainer::Draw(const Window
*w
)
1099 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1104 NWidgetCore
*NWidgetPIPContainer::GetWidgetFromPos(int x
, int y
)
1106 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return NULL
;
1108 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1109 NWidgetCore
*nwid
= child_wid
->GetWidgetFromPos(x
, y
);
1110 if (nwid
!= NULL
) return nwid
;
1115 /** Horizontal container widget. */
1116 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags
) : NWidgetPIPContainer(NWID_HORIZONTAL
, flags
)
1120 void NWidgetHorizontal::SetupSmallestSize(Window
*w
, bool init_array
)
1122 this->smallest_x
= 0; // Sum of minimal size of all children.
1123 this->smallest_y
= 0; // Biggest child.
1124 this->fill_x
= 0; // smallest non-zero child widget fill step.
1125 this->fill_y
= 1; // smallest common child fill step.
1126 this->resize_x
= 0; // smallest non-zero child widget resize step.
1127 this->resize_y
= 1; // smallest common child resize step.
1129 /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1130 uint longest
= 0; // Longest child found.
1131 uint max_vert_fill
= 0; // Biggest vertical fill step.
1132 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1133 child_wid
->SetupSmallestSize(w
, init_array
);
1134 longest
= max(longest
, child_wid
->smallest_x
);
1135 max_vert_fill
= max(max_vert_fill
, child_wid
->GetVerticalStepSize(ST_SMALLEST
));
1136 this->smallest_y
= max(this->smallest_y
, child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
);
1138 /* 1b. Make the container higher if needed to accommodate all children nicely. */
1139 uint max_smallest
= this->smallest_y
+ 3 * max_vert_fill
; // Upper limit to computing smallest height.
1140 uint cur_height
= this->smallest_y
;
1142 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1143 uint step_size
= child_wid
->GetVerticalStepSize(ST_SMALLEST
);
1144 uint child_height
= child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
;
1145 if (step_size
> 1 && child_height
< cur_height
) { // Small step sizes or already fitting children are not interesting.
1146 uint remainder
= (cur_height
- child_height
) % step_size
;
1147 if (remainder
> 0) { // Child did not fit entirely, widen the container.
1148 cur_height
+= step_size
- remainder
;
1149 assert(cur_height
< max_smallest
); // Safeguard against infinite height expansion.
1150 /* Remaining children will adapt to the new cur_height, thus speeding up the computation. */
1154 if (this->smallest_y
== cur_height
) break;
1155 this->smallest_y
= cur_height
; // Smallest height got changed, try again.
1157 /* 2. For containers that must maintain equal width, extend child minimal size. */
1158 if (this->flags
& NC_EQUALSIZE
) {
1159 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1160 if (child_wid
->fill_x
== 1) child_wid
->smallest_x
= longest
;
1163 /* 3. Move PIP space to the children, compute smallest, fill, and resize values of the container. */
1164 if (this->head
!= NULL
) this->head
->padding_left
+= this->pip_pre
;
1165 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1166 if (child_wid
->next
!= NULL
) {
1167 child_wid
->padding_right
+= this->pip_inter
;
1169 child_wid
->padding_right
+= this->pip_post
;
1172 this->smallest_x
+= child_wid
->smallest_x
+ child_wid
->padding_left
+ child_wid
->padding_right
;
1173 if (child_wid
->fill_x
> 0) {
1174 if (this->fill_x
== 0 || this->fill_x
> child_wid
->fill_x
) this->fill_x
= child_wid
->fill_x
;
1176 this->fill_y
= LeastCommonMultiple(this->fill_y
, child_wid
->fill_y
);
1178 if (child_wid
->resize_x
> 0) {
1179 if (this->resize_x
== 0 || this->resize_x
> child_wid
->resize_x
) this->resize_x
= child_wid
->resize_x
;
1181 this->resize_y
= LeastCommonMultiple(this->resize_y
, child_wid
->resize_y
);
1183 /* We need to zero the PIP settings so we can re-initialize the tree. */
1184 this->pip_pre
= this->pip_inter
= this->pip_post
= 0;
1187 void NWidgetHorizontal::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1189 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1191 /* Compute additional width given to us. */
1192 uint additional_length
= given_width
;
1193 if (sizing
== ST_SMALLEST
&& (this->flags
& NC_EQUALSIZE
)) {
1194 /* For EQUALSIZE containers this does not sum to smallest_x during initialisation */
1195 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1196 additional_length
-= child_wid
->smallest_x
+ child_wid
->padding_right
+ child_wid
->padding_left
;
1199 additional_length
-= this->smallest_x
;
1202 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1204 /* In principle, the additional horizontal space is distributed evenly over the available resizable children. Due to step sizes, this may not always be feasible.
1205 * To make resizing work as good as possible, first children with biggest step sizes are done. These may get less due to rounding down.
1206 * 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
1207 * of the child with the smallest non-zero stepsize.
1209 * 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
1210 * size and position, and directly call child->AssignSizePosition() with the computed values.
1211 * 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
1212 * then we call the child.
1215 /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle vertical size for all children,
1216 * handle horizontal size for non-resizing children.
1218 int num_changing_childs
= 0; // Number of children that can change size.
1219 uint biggest_stepsize
= 0;
1220 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1221 uint hor_step
= child_wid
->GetHorizontalStepSize(sizing
);
1223 num_changing_childs
++;
1224 biggest_stepsize
= max(biggest_stepsize
, hor_step
);
1226 child_wid
->current_x
= child_wid
->smallest_x
;
1229 uint vert_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetVerticalStepSize(sizing
);
1230 child_wid
->current_y
= ComputeMaxSize(child_wid
->smallest_y
, given_height
- child_wid
->padding_top
- child_wid
->padding_bottom
, vert_step
);
1233 /* Second loop: Allocate the additional horizontal space over the resizing children, starting with the biggest resize steps. */
1234 while (biggest_stepsize
> 0) {
1235 uint next_biggest_stepsize
= 0;
1236 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1237 uint hor_step
= child_wid
->GetHorizontalStepSize(sizing
);
1238 if (hor_step
> biggest_stepsize
) continue; // Already done
1239 if (hor_step
== biggest_stepsize
) {
1240 uint increment
= additional_length
/ num_changing_childs
;
1241 num_changing_childs
--;
1242 if (hor_step
> 1) increment
-= increment
% hor_step
;
1243 child_wid
->current_x
= child_wid
->smallest_x
+ increment
;
1244 additional_length
-= increment
;
1247 next_biggest_stepsize
= max(next_biggest_stepsize
, hor_step
);
1249 biggest_stepsize
= next_biggest_stepsize
;
1251 assert(num_changing_childs
== 0);
1253 /* Third loop: Compute position and call the child. */
1254 uint position
= rtl
? this->current_x
: 0; // Place to put next child relative to origin of the container.
1255 NWidgetBase
*child_wid
= this->head
;
1256 while (child_wid
!= NULL
) {
1257 uint child_width
= child_wid
->current_x
;
1258 uint child_x
= x
+ position
+ (rtl
? -child_width
- child_wid
->padding_left
: child_wid
->padding_left
);
1259 uint child_y
= y
+ child_wid
->padding_top
;
1261 child_wid
->AssignSizePosition(sizing
, child_x
, child_y
, child_width
, child_wid
->current_y
, rtl
);
1262 uint padded_child_width
= child_width
+ child_wid
->padding_right
+ child_wid
->padding_left
;
1263 position
+= rtl
? -padded_child_width
: padded_child_width
;
1265 child_wid
= child_wid
->next
;
1269 /** Horizontal left-to-right container widget. */
1270 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags
) : NWidgetHorizontal(flags
)
1272 this->type
= NWID_HORIZONTAL_LTR
;
1275 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1277 NWidgetHorizontal::AssignSizePosition(sizing
, x
, y
, given_width
, given_height
, false);
1280 /** Vertical container widget. */
1281 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags
) : NWidgetPIPContainer(NWID_VERTICAL
, flags
)
1285 void NWidgetVertical::SetupSmallestSize(Window
*w
, bool init_array
)
1287 this->smallest_x
= 0; // Biggest child.
1288 this->smallest_y
= 0; // Sum of minimal size of all children.
1289 this->fill_x
= 1; // smallest common child fill step.
1290 this->fill_y
= 0; // smallest non-zero child widget fill step.
1291 this->resize_x
= 1; // smallest common child resize step.
1292 this->resize_y
= 0; // smallest non-zero child widget resize step.
1294 /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1295 uint highest
= 0; // Highest child found.
1296 uint max_hor_fill
= 0; // Biggest horizontal fill step.
1297 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1298 child_wid
->SetupSmallestSize(w
, init_array
);
1299 highest
= max(highest
, child_wid
->smallest_y
);
1300 max_hor_fill
= max(max_hor_fill
, child_wid
->GetHorizontalStepSize(ST_SMALLEST
));
1301 this->smallest_x
= max(this->smallest_x
, child_wid
->smallest_x
+ child_wid
->padding_left
+ child_wid
->padding_right
);
1303 /* 1b. Make the container wider if needed to accommodate all children nicely. */
1304 uint max_smallest
= this->smallest_x
+ 3 * max_hor_fill
; // Upper limit to computing smallest height.
1305 uint cur_width
= this->smallest_x
;
1307 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1308 uint step_size
= child_wid
->GetHorizontalStepSize(ST_SMALLEST
);
1309 uint child_width
= child_wid
->smallest_x
+ child_wid
->padding_left
+ child_wid
->padding_right
;
1310 if (step_size
> 1 && child_width
< cur_width
) { // Small step sizes or already fitting children are not interesting.
1311 uint remainder
= (cur_width
- child_width
) % step_size
;
1312 if (remainder
> 0) { // Child did not fit entirely, widen the container.
1313 cur_width
+= step_size
- remainder
;
1314 assert(cur_width
< max_smallest
); // Safeguard against infinite width expansion.
1315 /* Remaining children will adapt to the new cur_width, thus speeding up the computation. */
1319 if (this->smallest_x
== cur_width
) break;
1320 this->smallest_x
= cur_width
; // Smallest width got changed, try again.
1322 /* 2. For containers that must maintain equal width, extend children minimal size. */
1323 if (this->flags
& NC_EQUALSIZE
) {
1324 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1325 if (child_wid
->fill_y
== 1) child_wid
->smallest_y
= highest
;
1328 /* 3. Move PIP space to the child, compute smallest, fill, and resize values of the container. */
1329 if (this->head
!= NULL
) this->head
->padding_top
+= this->pip_pre
;
1330 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1331 if (child_wid
->next
!= NULL
) {
1332 child_wid
->padding_bottom
+= this->pip_inter
;
1334 child_wid
->padding_bottom
+= this->pip_post
;
1337 this->smallest_y
+= child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
;
1338 if (child_wid
->fill_y
> 0) {
1339 if (this->fill_y
== 0 || this->fill_y
> child_wid
->fill_y
) this->fill_y
= child_wid
->fill_y
;
1341 this->fill_x
= LeastCommonMultiple(this->fill_x
, child_wid
->fill_x
);
1343 if (child_wid
->resize_y
> 0) {
1344 if (this->resize_y
== 0 || this->resize_y
> child_wid
->resize_y
) this->resize_y
= child_wid
->resize_y
;
1346 this->resize_x
= LeastCommonMultiple(this->resize_x
, child_wid
->resize_x
);
1348 /* We need to zero the PIP settings so we can re-initialize the tree. */
1349 this->pip_pre
= this->pip_inter
= this->pip_post
= 0;
1352 void NWidgetVertical::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1354 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1356 /* Compute additional height given to us. */
1357 uint additional_length
= given_height
;
1358 if (sizing
== ST_SMALLEST
&& (this->flags
& NC_EQUALSIZE
)) {
1359 /* For EQUALSIZE containers this does not sum to smallest_y during initialisation */
1360 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1361 additional_length
-= child_wid
->smallest_y
+ child_wid
->padding_top
+ child_wid
->padding_bottom
;
1364 additional_length
-= this->smallest_y
;
1367 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1369 /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the children with the biggest resize steps.
1370 * It also stores computed widths and heights into current_x and current_y values of the child.
1373 /* 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. */
1374 int num_changing_childs
= 0; // Number of children that can change size.
1375 uint biggest_stepsize
= 0;
1376 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1377 uint vert_step
= child_wid
->GetVerticalStepSize(sizing
);
1378 if (vert_step
> 0) {
1379 num_changing_childs
++;
1380 biggest_stepsize
= max(biggest_stepsize
, vert_step
);
1382 child_wid
->current_y
= child_wid
->smallest_y
;
1385 uint hor_step
= (sizing
== ST_SMALLEST
) ? 1 : child_wid
->GetHorizontalStepSize(sizing
);
1386 child_wid
->current_x
= ComputeMaxSize(child_wid
->smallest_x
, given_width
- child_wid
->padding_left
- child_wid
->padding_right
, hor_step
);
1389 /* Second loop: Allocate the additional vertical space over the resizing children, starting with the biggest resize steps. */
1390 while (biggest_stepsize
> 0) {
1391 uint next_biggest_stepsize
= 0;
1392 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1393 uint vert_step
= child_wid
->GetVerticalStepSize(sizing
);
1394 if (vert_step
> biggest_stepsize
) continue; // Already done
1395 if (vert_step
== biggest_stepsize
) {
1396 uint increment
= additional_length
/ num_changing_childs
;
1397 num_changing_childs
--;
1398 if (vert_step
> 1) increment
-= increment
% vert_step
;
1399 child_wid
->current_y
= child_wid
->smallest_y
+ increment
;
1400 additional_length
-= increment
;
1403 next_biggest_stepsize
= max(next_biggest_stepsize
, vert_step
);
1405 biggest_stepsize
= next_biggest_stepsize
;
1407 assert(num_changing_childs
== 0);
1409 /* Third loop: Compute position and call the child. */
1410 uint position
= 0; // Place to put next child relative to origin of the container.
1411 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= NULL
; child_wid
= child_wid
->next
) {
1412 uint child_x
= x
+ (rtl
? child_wid
->padding_right
: child_wid
->padding_left
);
1413 uint child_height
= child_wid
->current_y
;
1415 child_wid
->AssignSizePosition(sizing
, child_x
, y
+ position
+ child_wid
->padding_top
, child_wid
->current_x
, child_height
, rtl
);
1416 position
+= child_height
+ child_wid
->padding_top
+ child_wid
->padding_bottom
;
1421 * Generic spacer widget.
1422 * @param length Horizontal size of the spacer widget.
1423 * @param height Vertical size of the spacer widget.
1425 NWidgetSpacer::NWidgetSpacer(int length
, int height
) : NWidgetResizeBase(NWID_SPACER
, 0, 0)
1427 this->SetMinimalSize(length
, height
);
1428 this->SetResize(0, 0);
1431 void NWidgetSpacer::SetupSmallestSize(Window
*w
, bool init_array
)
1433 this->smallest_x
= this->min_x
;
1434 this->smallest_y
= this->min_y
;
1437 void NWidgetSpacer::FillNestedArray(NWidgetBase
**array
, uint length
)
1441 void NWidgetSpacer::Draw(const Window
*w
)
1443 /* Spacer widget is never visible. */
1446 void NWidgetSpacer::SetDirty(const Window
*w
) const
1448 /* Spacer widget never need repainting. */
1451 NWidgetCore
*NWidgetSpacer::GetWidgetFromPos(int x
, int y
)
1456 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX
, NC_EQUALSIZE
), index(-1), clicked(-1), count(-1)
1460 void NWidgetMatrix::SetIndex(int index
)
1462 this->index
= index
;
1465 void NWidgetMatrix::SetColour(Colours colour
)
1467 this->colour
= colour
;
1471 * Sets the clicked widget in the matrix.
1472 * @param clicked The clicked widget.
1474 void NWidgetMatrix::SetClicked(int clicked
)
1476 this->clicked
= clicked
;
1477 if (this->clicked
>= 0 && this->sb
!= NULL
&& this->widgets_x
!= 0) {
1478 int vpos
= (this->clicked
/ this->widgets_x
) * this->widget_h
; // Vertical position of the top.
1479 /* Need to scroll down -> Scroll to the bottom.
1480 * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
1481 if (this->sb
->GetPosition() < vpos
) vpos
+= this->widget_h
- this->pip_inter
- 1;
1482 this->sb
->ScrollTowards(vpos
);
1487 * Set the number of elements in this matrix.
1488 * @note Updates the number of elements/capacity of the real scrollbar.
1489 * @param count The number of elements.
1491 void NWidgetMatrix::SetCount(int count
)
1493 this->count
= count
;
1495 if (this->sb
== NULL
|| this->widgets_x
== 0) return;
1497 /* We need to get the number of pixels the matrix is high/wide.
1498 * So, determine the number of rows/columns based on the number of
1499 * columns/rows (one is constant/unscrollable).
1500 * Then multiply that by the height of a widget, and add the pre
1501 * and post spacing "offsets". */
1502 count
= CeilDiv(count
, this->sb
->IsVertical() ? this->widgets_x
: this->widgets_y
);
1503 count
*= (this->sb
->IsVertical() ? this->head
->smallest_y
: this->head
->smallest_x
) + this->pip_inter
;
1504 if (count
> 0) count
-= this->pip_inter
; // We counted an inter too much in the multiplication above
1505 count
+= this->pip_pre
+ this->pip_post
;
1506 this->sb
->SetCount(count
);
1507 this->sb
->SetCapacity(this->sb
->IsVertical() ? this->current_y
: this->current_x
);
1508 this->sb
->SetStepSize(this->sb
->IsVertical() ? this->widget_h
: this->widget_w
);
1512 * Assign a scrollbar to this matrix.
1513 * @param sb The scrollbar to assign to us.
1515 void NWidgetMatrix::SetScrollbar(Scrollbar
*sb
)
1520 void NWidgetMatrix::SetupSmallestSize(Window
*w
, bool init_array
)
1522 assert(this->head
!= NULL
);
1523 assert(this->head
->next
== NULL
);
1525 if (this->index
>= 0 && init_array
) { // Fill w->nested_array[]
1526 assert(w
->nested_array_size
> (uint
)this->index
);
1527 w
->nested_array
[this->index
] = this;
1530 /* Reset the widget number. */
1531 NWidgetCore
*nw
= dynamic_cast<NWidgetCore
*>(this->head
);
1533 SB(nw
->index
, 16, 16, 0);
1534 this->head
->SetupSmallestSize(w
, init_array
);
1536 Dimension padding
= {this->pip_pre
+ this->pip_post
, this->pip_pre
+ this->pip_post
};
1537 Dimension size
= {this->head
->smallest_x
+ padding
.width
, this->head
->smallest_y
+ padding
.height
};
1538 Dimension fill
= {0, 0};
1539 Dimension resize
= {this->pip_inter
+ this->head
->smallest_x
, this->pip_inter
+ this->head
->smallest_y
};
1541 if (this->index
>= 0) w
->UpdateWidgetSize(this->index
, &size
, padding
, &fill
, &resize
);
1543 this->smallest_x
= size
.width
;
1544 this->smallest_y
= size
.height
;
1545 this->fill_x
= fill
.width
;
1546 this->fill_y
= fill
.height
;
1547 this->resize_x
= resize
.width
;
1548 this->resize_y
= resize
.height
;
1551 void NWidgetMatrix::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1553 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
1557 this->current_x
= given_width
;
1558 this->current_y
= given_height
;
1560 /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
1561 this->widget_w
= this->head
->smallest_x
+ this->pip_inter
;
1562 this->widget_h
= this->head
->smallest_y
+ this->pip_inter
;
1564 /* Account for the pip_inter is between widgets, so we need to account for that when
1565 * the division assumes pip_inter is used for all widgets. */
1566 this->widgets_x
= CeilDiv(this->current_x
- this->pip_pre
- this->pip_post
+ this->pip_inter
, this->widget_w
);
1567 this->widgets_y
= CeilDiv(this->current_y
- this->pip_pre
- this->pip_post
+ this->pip_inter
, this->widget_h
);
1569 /* When resizing, update the scrollbar's count. E.g. with a vertical
1570 * scrollbar becoming wider or narrower means the amount of rows in
1571 * the scrollbar becomes respectively smaller or higher. */
1572 this->SetCount(this->count
);
1575 void NWidgetMatrix::FillNestedArray(NWidgetBase
**array
, uint length
)
1577 if (this->index
>= 0 && (uint
)(this->index
) < length
) array
[this->index
] = this;
1578 NWidgetContainer::FillNestedArray(array
, length
);
1581 NWidgetCore
*NWidgetMatrix::GetWidgetFromPos(int x
, int y
)
1583 /* Falls outside of the matrix widget. */
1584 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return NULL
;
1586 int start_x
, start_y
, base_offs_x
, base_offs_y
;
1587 this->GetScrollOffsets(start_x
, start_y
, base_offs_x
, base_offs_y
);
1589 bool rtl
= _current_text_dir
== TD_RTL
;
1591 int widget_col
= (rtl
?
1592 -x
+ (int)this->pip_post
+ (int)this->pos_x
+ base_offs_x
+ (int)this->widget_w
- 1 - (int)this->pip_inter
:
1593 x
- (int)this->pip_pre
- (int)this->pos_x
- base_offs_x
1596 int widget_row
= (y
- base_offs_y
- (int)this->pip_pre
- (int)this->pos_y
) / this->widget_h
;
1598 int sub_wid
= (widget_row
+ start_y
) * this->widgets_x
+ start_x
+ widget_col
;
1599 if (sub_wid
>= this->count
) return NULL
;
1601 NWidgetCore
*child
= dynamic_cast<NWidgetCore
*>(this->head
);
1602 assert(child
!= NULL
);
1603 child
->AssignSizePosition(ST_RESIZE
,
1604 this->pos_x
+ (rtl
? this->pip_post
- widget_col
* this->widget_w
: this->pip_pre
+ widget_col
* this->widget_w
) + base_offs_x
,
1605 this->pos_y
+ this->pip_pre
+ widget_row
* this->widget_h
+ base_offs_y
,
1606 child
->smallest_x
, child
->smallest_y
, rtl
);
1608 SB(child
->index
, 16, 16, sub_wid
);
1610 return child
->GetWidgetFromPos(x
, y
);
1613 /* virtual */ void NWidgetMatrix::Draw(const Window
*w
)
1615 /* Fill the background. */
1616 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]);
1618 /* Set up a clipping area for the previews. */
1619 bool rtl
= _current_text_dir
== TD_RTL
;
1620 DrawPixelInfo tmp_dpi
;
1621 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;
1622 DrawPixelInfo
*old_dpi
= _cur_dpi
;
1623 _cur_dpi
= &tmp_dpi
;
1625 /* Get the appropriate offsets so we can draw the right widgets. */
1626 NWidgetCore
*child
= dynamic_cast<NWidgetCore
*>(this->head
);
1627 assert(child
!= NULL
);
1628 int start_x
, start_y
, base_offs_x
, base_offs_y
;
1629 this->GetScrollOffsets(start_x
, start_y
, base_offs_x
, base_offs_y
);
1631 int offs_y
= base_offs_y
;
1632 for (int y
= start_y
; y
< start_y
+ this->widgets_y
+ 1; y
++, offs_y
+= this->widget_h
) {
1633 /* Are we within bounds? */
1634 if (offs_y
+ child
->smallest_y
<= 0) continue;
1635 if (offs_y
>= (int)this->current_y
) break;
1637 /* We've passed our amount of widgets. */
1638 if (y
* this->widgets_x
>= this->count
) break;
1640 int offs_x
= base_offs_x
;
1641 for (int x
= start_x
; x
< start_x
+ this->widgets_x
+ 1; x
++, offs_x
+= rtl
? -this->widget_w
: this->widget_w
) {
1642 /* Are we within bounds? */
1643 if (offs_x
+ child
->smallest_x
<= 0) continue;
1644 if (offs_x
>= (int)this->current_x
) continue;
1646 /* Do we have this many widgets? */
1647 int sub_wid
= y
* this->widgets_x
+ x
;
1648 if (sub_wid
>= this->count
) break;
1650 child
->AssignSizePosition(ST_RESIZE
, offs_x
, offs_y
, child
->smallest_x
, child
->smallest_y
, rtl
);
1651 child
->SetLowered(this->clicked
== sub_wid
);
1652 SB(child
->index
, 16, 16, sub_wid
);
1657 /* Restore the clipping area. */
1662 * Get the different offsets that are influenced by scrolling.
1663 * @param [out] start_x The start position in columns (index of the left-most column, swapped in RTL).
1664 * @param [out] start_y The start position in rows.
1665 * @param [out] base_offs_x The base horizontal offset in pixels (X position of the column \a start_x).
1666 * @param [out] base_offs_y The base vertical offset in pixels (Y position of the column \a start_y).
1668 void NWidgetMatrix::GetScrollOffsets(int &start_x
, int &start_y
, int &base_offs_x
, int &base_offs_y
)
1670 base_offs_x
= _current_text_dir
== TD_RTL
? this->widget_w
* (this->widgets_x
- 1) : 0;
1674 if (this->sb
!= NULL
) {
1675 if (this->sb
->IsVertical()) {
1676 start_y
= this->sb
->GetPosition() / this->widget_h
;
1677 base_offs_y
+= -this->sb
->GetPosition() + start_y
* this->widget_h
;
1679 start_x
= this->sb
->GetPosition() / this->widget_w
;
1680 int sub_x
= this->sb
->GetPosition() - start_x
* this->widget_w
;
1681 if (_current_text_dir
== TD_RTL
) {
1682 base_offs_x
+= sub_x
;
1684 base_offs_x
-= sub_x
;
1691 * Constructor parent nested widgets.
1692 * @param tp Type of parent widget.
1693 * @param colour Colour of the parent widget.
1694 * @param index Index in the widget array used by the window system.
1695 * @param child Child container widget (if supplied). If not supplied, a
1696 * vertical container will be inserted while adding the first
1699 NWidgetBackground::NWidgetBackground(WidgetType tp
, Colours colour
, int index
, NWidgetPIPContainer
*child
) : NWidgetCore(tp
, colour
, 1, 1, 0x0, STR_NULL
)
1701 assert(tp
== WWT_PANEL
|| tp
== WWT_INSET
|| tp
== WWT_FRAME
);
1702 if (index
>= 0) this->SetIndex(index
);
1703 this->child
= child
;
1706 NWidgetBackground::~NWidgetBackground()
1708 if (this->child
!= NULL
) delete this->child
;
1712 * Add a child to the parent.
1713 * @param nwid Nested widget to add to the background widget.
1715 * Unless a child container has been given in the constructor, a parent behaves as a vertical container.
1716 * You can add several children to it, and they are put underneath each other.
1718 void NWidgetBackground::Add(NWidgetBase
*nwid
)
1720 if (this->child
== NULL
) {
1721 this->child
= new NWidgetVertical();
1723 this->child
->Add(nwid
);
1727 * Set additional pre/inter/post space for the background widget.
1729 * @param pip_pre Additional space in front of the first child widget (above
1730 * for the vertical container, at the left for the horizontal container).
1731 * @param pip_inter Additional space between two child widgets.
1732 * @param pip_post Additional space after the last child widget (below for the
1733 * vertical container, at the right for the horizontal container).
1734 * @note Using this function implies that the widget has (or will have) child widgets.
1736 void NWidgetBackground::SetPIP(uint8 pip_pre
, uint8 pip_inter
, uint8 pip_post
)
1738 if (this->child
== NULL
) {
1739 this->child
= new NWidgetVertical();
1741 this->child
->SetPIP(pip_pre
, pip_inter
, pip_post
);
1744 void NWidgetBackground::SetupSmallestSize(Window
*w
, bool init_array
)
1746 if (init_array
&& this->index
>= 0) {
1747 assert(w
->nested_array_size
> (uint
)this->index
);
1748 w
->nested_array
[this->index
] = this;
1750 if (this->child
!= NULL
) {
1751 this->child
->SetupSmallestSize(w
, init_array
);
1753 this->smallest_x
= this->child
->smallest_x
;
1754 this->smallest_y
= this->child
->smallest_y
;
1755 this->fill_x
= this->child
->fill_x
;
1756 this->fill_y
= this->child
->fill_y
;
1757 this->resize_x
= this->child
->resize_x
;
1758 this->resize_y
= this->child
->resize_y
;
1760 /* Account for the size of the frame's text if that exists */
1761 if (w
!= NULL
&& this->type
== WWT_FRAME
) {
1762 this->child
->padding_left
= WD_FRAMETEXT_LEFT
;
1763 this->child
->padding_right
= WD_FRAMETEXT_RIGHT
;
1764 this->child
->padding_top
= max((int)WD_FRAMETEXT_TOP
, this->widget_data
!= STR_NULL
? FONT_HEIGHT_NORMAL
+ WD_FRAMETEXT_TOP
/ 2 : 0);
1765 this->child
->padding_bottom
= WD_FRAMETEXT_BOTTOM
;
1767 this->smallest_x
+= this->child
->padding_left
+ this->child
->padding_right
;
1768 this->smallest_y
+= this->child
->padding_top
+ this->child
->padding_bottom
;
1770 if (this->index
>= 0) w
->SetStringParameters(this->index
);
1771 this->smallest_x
= max(this->smallest_x
, GetStringBoundingBox(this->widget_data
).width
+ WD_FRAMETEXT_LEFT
+ WD_FRAMETEXT_RIGHT
);
1774 Dimension d
= {this->min_x
, this->min_y
};
1775 Dimension fill
= {this->fill_x
, this->fill_y
};
1776 Dimension resize
= {this->resize_x
, this->resize_y
};
1777 if (w
!= NULL
) { // A non-NULL window pointer acts as switch to turn dynamic widget size on.
1778 if (this->type
== WWT_FRAME
|| this->type
== WWT_INSET
) {
1779 if (this->index
>= 0) w
->SetStringParameters(this->index
);
1780 Dimension background
= GetStringBoundingBox(this->widget_data
);
1781 background
.width
+= (this->type
== WWT_FRAME
) ? (WD_FRAMETEXT_LEFT
+ WD_FRAMERECT_RIGHT
) : (WD_INSET_LEFT
+ WD_INSET_RIGHT
);
1782 d
= maxdim(d
, background
);
1784 if (this->index
>= 0) {
1785 static const Dimension padding
= {0, 0};
1786 w
->UpdateWidgetSize(this->index
, &d
, padding
, &fill
, &resize
);
1789 this->smallest_x
= d
.width
;
1790 this->smallest_y
= d
.height
;
1791 this->fill_x
= fill
.width
;
1792 this->fill_y
= fill
.height
;
1793 this->resize_x
= resize
.width
;
1794 this->resize_y
= resize
.height
;
1798 void NWidgetBackground::AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1800 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1802 if (this->child
!= NULL
) {
1803 uint x_offset
= (rtl
? this->child
->padding_right
: this->child
->padding_left
);
1804 uint width
= given_width
- this->child
->padding_right
- this->child
->padding_left
;
1805 uint height
= given_height
- this->child
->padding_top
- this->child
->padding_bottom
;
1806 this->child
->AssignSizePosition(sizing
, x
+ x_offset
, y
+ this->child
->padding_top
, width
, height
, rtl
);
1810 void NWidgetBackground::FillNestedArray(NWidgetBase
**array
, uint length
)
1812 if (this->index
>= 0 && (uint
)(this->index
) < length
) array
[this->index
] = this;
1813 if (this->child
!= NULL
) this->child
->FillNestedArray(array
, length
);
1816 void NWidgetBackground::Draw(const Window
*w
)
1818 if (this->current_x
== 0 || this->current_y
== 0) return;
1821 r
.left
= this->pos_x
;
1822 r
.right
= this->pos_x
+ this->current_x
- 1;
1823 r
.top
= this->pos_y
;
1824 r
.bottom
= this->pos_y
+ this->current_y
- 1;
1826 const DrawPixelInfo
*dpi
= _cur_dpi
;
1827 if (dpi
->left
> r
.right
|| dpi
->left
+ dpi
->width
<= r
.left
|| dpi
->top
> r
.bottom
|| dpi
->top
+ dpi
->height
<= r
.top
) return;
1829 switch (this->type
) {
1831 assert(this->widget_data
== 0);
1832 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, this->colour
, this->IsLowered() ? FR_LOWERED
: FR_NONE
);
1836 if (this->index
>= 0) w
->SetStringParameters(this->index
);
1837 DrawFrame(r
, this->colour
, this->widget_data
);
1841 if (this->index
>= 0) w
->SetStringParameters(this->index
);
1842 DrawInset(r
, this->colour
, this->widget_data
);
1849 if (this->index
>= 0) w
->DrawWidget(r
, this->index
);
1850 if (this->child
!= NULL
) this->child
->Draw(w
);
1852 if (this->IsDisabled()) {
1853 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, _colour_gradient
[this->colour
& 0xF][2], FILLRECT_CHECKER
);
1857 NWidgetCore
*NWidgetBackground::GetWidgetFromPos(int x
, int y
)
1859 NWidgetCore
*nwid
= NULL
;
1860 if (IsInsideBS(x
, this->pos_x
, this->current_x
) && IsInsideBS(y
, this->pos_y
, this->current_y
)) {
1861 if (this->child
!= NULL
) nwid
= this->child
->GetWidgetFromPos(x
, y
);
1862 if (nwid
== NULL
) nwid
= this;
1867 NWidgetBase
*NWidgetBackground::GetWidgetOfType(WidgetType tp
)
1869 NWidgetBase
*nwid
= NULL
;
1870 if (this->child
!= NULL
) nwid
= this->child
->GetWidgetOfType(tp
);
1871 if (nwid
== NULL
&& this->type
== tp
) nwid
= this;
1875 NWidgetViewport::NWidgetViewport(int index
) : NWidgetCore(NWID_VIEWPORT
, INVALID_COLOUR
, 1, 1, 0x0, STR_NULL
)
1877 this->SetIndex(index
);
1880 void NWidgetViewport::SetupSmallestSize(Window
*w
, bool init_array
)
1882 if (init_array
&& this->index
>= 0) {
1883 assert(w
->nested_array_size
> (uint
)this->index
);
1884 w
->nested_array
[this->index
] = this;
1886 this->smallest_x
= this->min_x
;
1887 this->smallest_y
= this->min_y
;
1890 void NWidgetViewport::Draw(const Window
*w
)
1892 if (this->disp_flags
& ND_NO_TRANSPARENCY
) {
1893 TransparencyOptionBits to_backup
= _transparency_opt
;
1894 _transparency_opt
&= (1 << TO_SIGNS
) | (1 << TO_LOADING
); // Disable all transparency, except textual stuff
1896 _transparency_opt
= to_backup
;
1901 /* Optionally shade the viewport. */
1902 if (this->disp_flags
& (ND_SHADE_GREY
| ND_SHADE_DIMMED
)) {
1903 GfxFillRect(this->pos_x
, this->pos_y
, this->pos_x
+ this->current_x
- 1, this->pos_y
+ this->current_y
- 1,
1904 (this->disp_flags
& ND_SHADE_DIMMED
) ? PALETTE_TO_TRANSPARENT
: PALETTE_NEWSPAPER
, FILLRECT_RECOLOUR
);
1909 * Initialize the viewport of the window.
1910 * @param w Window owning the viewport.
1911 * @param follow_flags Type of viewport, see #InitializeWindowViewport().
1912 * @param zoom Zoom level.
1914 void NWidgetViewport::InitializeViewport(Window
*w
, uint32 follow_flags
, ZoomLevel zoom
)
1916 InitializeWindowViewport(w
, this->pos_x
, this->pos_y
, this->current_x
, this->current_y
, follow_flags
, zoom
);
1920 * Update the position and size of the viewport (after eg a resize).
1921 * @param w Window owning the viewport.
1923 void NWidgetViewport::UpdateViewportCoordinates(Window
*w
)
1925 ViewPort
*vp
= w
->viewport
;
1927 vp
->left
= w
->left
+ this->pos_x
;
1928 vp
->top
= w
->top
+ this->pos_y
;
1929 vp
->width
= this->current_x
;
1930 vp
->height
= this->current_y
;
1932 vp
->virtual_width
= ScaleByZoom(vp
->width
, vp
->zoom
);
1933 vp
->virtual_height
= ScaleByZoom(vp
->height
, vp
->zoom
);
1938 * Compute the row of a scrolled widget that a user clicked in.
1939 * @param clickpos Vertical position of the mouse click (without taking scrolling into account).
1940 * @param w The window the click was in.
1941 * @param widget Widget number of the widget clicked in.
1942 * @param padding Amount of empty space between the widget edge and the top of the first row. Default value is \c 0.
1943 * @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
1944 * @return Row number clicked at. If clicked at a wrong position, #INT_MAX is returned.
1946 int Scrollbar::GetScrolledRowFromWidget(int clickpos
, const Window
* const w
, int widget
, int padding
, int line_height
) const
1948 uint pos
= w
->GetRowFromWidget(clickpos
, widget
, padding
, line_height
);
1949 if (pos
!= INT_MAX
) pos
+= this->GetPosition();
1950 return (pos
>= this->GetCount()) ? INT_MAX
: pos
;
1954 * Set capacity of visible elements from the size and resize properties of a widget.
1956 * @param widget Widget with size and resize properties.
1957 * @param padding Padding to subtract from the size.
1958 * @note Updates the position if needed.
1960 void Scrollbar::SetCapacityFromWidget(Window
*w
, int widget
, int padding
)
1962 NWidgetBase
*nwid
= w
->GetWidget
<NWidgetBase
>(widget
);
1963 if (this->IsVertical()) {
1964 this->SetCapacity(((int)nwid
->current_y
- padding
) / (int)nwid
->resize_y
);
1966 this->SetCapacity(((int)nwid
->current_x
- padding
) / (int)nwid
->resize_x
);
1972 * @param tp Scrollbar type. (horizontal/vertical)
1973 * @param colour Colour of the scrollbar.
1974 * @param index Index in the widget array used by the window system.
1976 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp
, Colours colour
, int index
) : NWidgetCore(tp
, colour
, 1, 1, 0x0, STR_NULL
), Scrollbar(tp
!= NWID_HSCROLLBAR
)
1978 assert(tp
== NWID_HSCROLLBAR
|| tp
== NWID_VSCROLLBAR
);
1979 this->SetIndex(index
);
1981 switch (this->type
) {
1982 case NWID_HSCROLLBAR
:
1983 this->SetMinimalSize(0, NWidgetScrollbar::GetHorizontalDimension().height
);
1984 this->SetResize(1, 0);
1985 this->SetFill(1, 0);
1986 this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST
);
1989 case NWID_VSCROLLBAR
:
1990 this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width
, 0);
1991 this->SetResize(0, 1);
1992 this->SetFill(0, 1);
1993 this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST
);
1996 default: NOT_REACHED();
2000 void NWidgetScrollbar::SetupSmallestSize(Window
*w
, bool init_array
)
2002 if (init_array
&& this->index
>= 0) {
2003 assert(w
->nested_array_size
> (uint
)this->index
);
2004 w
->nested_array
[this->index
] = this;
2006 this->smallest_x
= this->min_x
;
2007 this->smallest_y
= this->min_y
;
2010 void NWidgetScrollbar::Draw(const Window
*w
)
2012 if (this->current_x
== 0 || this->current_y
== 0) return;
2015 r
.left
= this->pos_x
;
2016 r
.right
= this->pos_x
+ this->current_x
- 1;
2017 r
.top
= this->pos_y
;
2018 r
.bottom
= this->pos_y
+ this->current_y
- 1;
2020 const DrawPixelInfo
*dpi
= _cur_dpi
;
2021 if (dpi
->left
> r
.right
|| dpi
->left
+ dpi
->width
<= r
.left
|| dpi
->top
> r
.bottom
|| dpi
->top
+ dpi
->height
<= r
.top
) return;
2023 bool up_lowered
= HasBit(this->disp_flags
, NDB_SCROLLBAR_UP
);
2024 bool down_lowered
= HasBit(this->disp_flags
, NDB_SCROLLBAR_DOWN
);
2025 bool middle_lowered
= !(this->disp_flags
& ND_SCROLLBAR_BTN
) && w
->scrolling_scrollbar
== this->index
;
2027 if (this->type
== NWID_HSCROLLBAR
) {
2028 DrawHorizontalScrollbar(r
, this->colour
, up_lowered
, middle_lowered
, down_lowered
, this);
2030 DrawVerticalScrollbar(r
, this->colour
, up_lowered
, middle_lowered
, down_lowered
, this);
2033 if (this->IsDisabled()) {
2034 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, _colour_gradient
[this->colour
& 0xF][2], FILLRECT_CHECKER
);
2038 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
2040 vertical_dimension
.width
= vertical_dimension
.height
= 0;
2041 horizontal_dimension
.width
= horizontal_dimension
.height
= 0;
2044 /* static */ Dimension
NWidgetScrollbar::GetVerticalDimension()
2046 static const Dimension extra
= {WD_SCROLLBAR_LEFT
+ WD_SCROLLBAR_RIGHT
, WD_SCROLLBAR_TOP
+ WD_SCROLLBAR_BOTTOM
};
2047 if (vertical_dimension
.width
== 0) {
2048 vertical_dimension
= maxdim(GetSpriteSize(SPR_ARROW_UP
), GetSpriteSize(SPR_ARROW_DOWN
));
2049 vertical_dimension
.width
+= extra
.width
;
2050 vertical_dimension
.height
+= extra
.height
;
2052 return vertical_dimension
;
2055 /* static */ Dimension
NWidgetScrollbar::GetHorizontalDimension()
2057 static const Dimension extra
= {WD_SCROLLBAR_LEFT
+ WD_SCROLLBAR_RIGHT
, WD_SCROLLBAR_TOP
+ WD_SCROLLBAR_BOTTOM
};
2058 if (horizontal_dimension
.width
== 0) {
2059 horizontal_dimension
= maxdim(GetSpriteSize(SPR_ARROW_LEFT
), GetSpriteSize(SPR_ARROW_RIGHT
));
2060 horizontal_dimension
.width
+= extra
.width
;
2061 horizontal_dimension
.height
+= extra
.height
;
2063 return horizontal_dimension
;
2066 Dimension
NWidgetScrollbar::vertical_dimension
= {0, 0};
2067 Dimension
NWidgetScrollbar::horizontal_dimension
= {0, 0};
2069 /** Reset the cached dimensions. */
2070 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
2072 shadebox_dimension
.width
= shadebox_dimension
.height
= 0;
2073 debugbox_dimension
.width
= debugbox_dimension
.height
= 0;
2074 stickybox_dimension
.width
= stickybox_dimension
.height
= 0;
2075 resizebox_dimension
.width
= resizebox_dimension
.height
= 0;
2076 closebox_dimension
.width
= closebox_dimension
.height
= 0;
2077 dropdown_dimension
.width
= dropdown_dimension
.height
= 0;
2080 Dimension
NWidgetLeaf::shadebox_dimension
= {0, 0};
2081 Dimension
NWidgetLeaf::debugbox_dimension
= {0, 0};
2082 Dimension
NWidgetLeaf::defsizebox_dimension
= {0, 0};
2083 Dimension
NWidgetLeaf::stickybox_dimension
= {0, 0};
2084 Dimension
NWidgetLeaf::resizebox_dimension
= {0, 0};
2085 Dimension
NWidgetLeaf::closebox_dimension
= {0, 0};
2086 Dimension
NWidgetLeaf::dropdown_dimension
= {0, 0};
2089 * Nested leaf widget.
2090 * @param tp Type of leaf widget.
2091 * @param colour Colour of the leaf widget.
2092 * @param index Index in the widget array used by the window system.
2093 * @param data Data of the widget.
2094 * @param tip Tooltip of the widget.
2096 NWidgetLeaf::NWidgetLeaf(WidgetType tp
, Colours colour
, int index
, uint16 data
, StringID tip
) : NWidgetCore(tp
, colour
, 1, 1, data
, tip
)
2098 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
);
2099 if (index
>= 0) this->SetIndex(index
);
2100 this->SetMinimalSize(0, 0);
2101 this->SetResize(0, 0);
2109 case WWT_PUSHIMGBTN
:
2112 case WWT_PUSHTXTBTN
:
2117 case NWID_BUTTON_DROPDOWN
:
2118 case NWID_PUSHBUTTON_DROPDOWN
:
2120 case WWT_PUSHARROWBTN
:
2121 this->SetFill(0, 0);
2125 Dimension sprite_size
= GetSpriteSize(_current_text_dir
== TD_RTL
? SPR_IMG_DELETE_RIGHT
: SPR_IMG_DELETE_LEFT
);
2126 this->SetMinimalSize(30 + sprite_size
.width
, sprite_size
.height
);
2127 this->SetFill(0, 0);
2132 this->SetFill(1, 0);
2133 this->SetResize(1, 0);
2134 this->min_y
= WD_CAPTION_HEIGHT
;
2135 this->SetDataTip(data
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
);
2139 this->SetFill(0, 0);
2140 this->SetMinimalSize(WD_STICKYBOX_WIDTH
, WD_CAPTION_HEIGHT
);
2141 this->SetDataTip(STR_NULL
, STR_TOOLTIP_STICKY
);
2145 this->SetFill(0, 0);
2146 this->SetMinimalSize(WD_SHADEBOX_TOP
, WD_CAPTION_HEIGHT
);
2147 this->SetDataTip(STR_NULL
, STR_TOOLTIP_SHADE
);
2151 this->SetFill(0, 0);
2152 this->SetMinimalSize(WD_DEBUGBOX_TOP
, WD_CAPTION_HEIGHT
);
2153 this->SetDataTip(STR_NULL
, STR_TOOLTIP_DEBUG
);
2156 case WWT_DEFSIZEBOX
:
2157 this->SetFill(0, 0);
2158 this->SetMinimalSize(WD_DEFSIZEBOX_TOP
, WD_CAPTION_HEIGHT
);
2159 this->SetDataTip(STR_NULL
, STR_TOOLTIP_DEFSIZE
);
2163 this->SetFill(0, 0);
2164 this->SetMinimalSize(WD_RESIZEBOX_WIDTH
, 12);
2165 this->SetDataTip(STR_NULL
, STR_TOOLTIP_RESIZE
);
2169 this->SetFill(0, 0);
2170 this->SetMinimalSize(WD_CLOSEBOX_WIDTH
, WD_CAPTION_HEIGHT
);
2171 this->SetDataTip(STR_BLACK_CROSS
, STR_TOOLTIP_CLOSE_WINDOW
);
2175 this->SetFill(0, 0);
2176 this->min_y
= WD_DROPDOWN_HEIGHT
;
2184 void NWidgetLeaf::SetupSmallestSize(Window
*w
, bool init_array
)
2186 if (this->index
>= 0 && init_array
) { // Fill w->nested_array[]
2187 assert(w
->nested_array_size
> (uint
)this->index
);
2188 w
->nested_array
[this->index
] = this;
2191 Dimension size
= {this->min_x
, this->min_y
};
2192 Dimension fill
= {this->fill_x
, this->fill_y
};
2193 Dimension resize
= {this->resize_x
, this->resize_y
};
2194 /* Get padding, and update size with the real content size if appropriate. */
2195 const Dimension
*padding
= NULL
;
2196 switch (this->type
) {
2198 static const Dimension extra
= {0, 0};
2203 static const Dimension extra
= {WD_MATRIX_LEFT
+ WD_MATRIX_RIGHT
, WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
};
2207 case WWT_SHADEBOX
: {
2208 static const Dimension extra
= {WD_SHADEBOX_LEFT
+ WD_SHADEBOX_RIGHT
, WD_SHADEBOX_TOP
+ WD_SHADEBOX_BOTTOM
};
2210 if (NWidgetLeaf::shadebox_dimension
.width
== 0) {
2211 NWidgetLeaf::shadebox_dimension
= maxdim(GetSpriteSize(SPR_WINDOW_SHADE
), GetSpriteSize(SPR_WINDOW_UNSHADE
));
2212 NWidgetLeaf::shadebox_dimension
.width
+= extra
.width
;
2213 NWidgetLeaf::shadebox_dimension
.height
+= extra
.height
;
2215 size
= maxdim(size
, NWidgetLeaf::shadebox_dimension
);
2219 if (_settings_client
.gui
.newgrf_developer_tools
&& w
->IsNewGRFInspectable()) {
2220 static const Dimension extra
= {WD_DEBUGBOX_LEFT
+ WD_DEBUGBOX_RIGHT
, WD_DEBUGBOX_TOP
+ WD_DEBUGBOX_BOTTOM
};
2222 if (NWidgetLeaf::debugbox_dimension
.width
== 0) {
2223 NWidgetLeaf::debugbox_dimension
= GetSpriteSize(SPR_WINDOW_DEBUG
);
2224 NWidgetLeaf::debugbox_dimension
.width
+= extra
.width
;
2225 NWidgetLeaf::debugbox_dimension
.height
+= extra
.height
;
2227 size
= maxdim(size
, NWidgetLeaf::debugbox_dimension
);
2229 /* If the setting is disabled we don't want to see it! */
2236 case WWT_STICKYBOX
: {
2237 static const Dimension extra
= {WD_STICKYBOX_LEFT
+ WD_STICKYBOX_RIGHT
, WD_STICKYBOX_TOP
+ WD_STICKYBOX_BOTTOM
};
2239 if (NWidgetLeaf::stickybox_dimension
.width
== 0) {
2240 NWidgetLeaf::stickybox_dimension
= maxdim(GetSpriteSize(SPR_PIN_UP
), GetSpriteSize(SPR_PIN_DOWN
));
2241 NWidgetLeaf::stickybox_dimension
.width
+= extra
.width
;
2242 NWidgetLeaf::stickybox_dimension
.height
+= extra
.height
;
2244 size
= maxdim(size
, NWidgetLeaf::stickybox_dimension
);
2248 case WWT_DEFSIZEBOX
: {
2249 static const Dimension extra
= {WD_DEFSIZEBOX_LEFT
+ WD_DEFSIZEBOX_RIGHT
, WD_DEFSIZEBOX_TOP
+ WD_DEFSIZEBOX_BOTTOM
};
2251 if (NWidgetLeaf::defsizebox_dimension
.width
== 0) {
2252 NWidgetLeaf::defsizebox_dimension
= GetSpriteSize(SPR_WINDOW_DEFSIZE
);
2253 NWidgetLeaf::defsizebox_dimension
.width
+= extra
.width
;
2254 NWidgetLeaf::defsizebox_dimension
.height
+= extra
.height
;
2256 size
= maxdim(size
, NWidgetLeaf::defsizebox_dimension
);
2260 case WWT_RESIZEBOX
: {
2261 static const Dimension extra
= {WD_RESIZEBOX_LEFT
+ WD_RESIZEBOX_RIGHT
, WD_RESIZEBOX_TOP
+ WD_RESIZEBOX_BOTTOM
};
2263 if (NWidgetLeaf::resizebox_dimension
.width
== 0) {
2264 NWidgetLeaf::resizebox_dimension
= maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT
), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT
));
2265 NWidgetLeaf::resizebox_dimension
.width
+= extra
.width
;
2266 NWidgetLeaf::resizebox_dimension
.height
+= extra
.height
;
2268 size
= maxdim(size
, NWidgetLeaf::resizebox_dimension
);
2272 size
.height
= max(size
.height
, GetStringBoundingBox("_").height
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
2275 static const Dimension extra
= {WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
};
2281 case WWT_PUSHIMGBTN
: {
2282 static const Dimension extra
= {WD_IMGBTN_LEFT
+ WD_IMGBTN_RIGHT
, WD_IMGBTN_TOP
+ WD_IMGBTN_BOTTOM
};
2284 Dimension d2
= GetSpriteSize(this->widget_data
);
2285 if (this->type
== WWT_IMGBTN_2
) d2
= maxdim(d2
, GetSpriteSize(this->widget_data
+ 1));
2286 d2
.width
+= extra
.width
;
2287 d2
.height
+= extra
.height
;
2288 size
= maxdim(size
, d2
);
2292 case WWT_PUSHARROWBTN
: {
2293 static const Dimension extra
= {WD_IMGBTN_LEFT
+ WD_IMGBTN_RIGHT
, WD_IMGBTN_TOP
+ WD_IMGBTN_BOTTOM
};
2295 Dimension d2
= maxdim(GetSpriteSize(SPR_ARROW_LEFT
), GetSpriteSize(SPR_ARROW_RIGHT
));
2296 d2
.width
+= extra
.width
;
2297 d2
.height
+= extra
.height
;
2298 size
= maxdim(size
, d2
);
2302 case WWT_CLOSEBOX
: {
2303 static const Dimension extra
= {WD_CLOSEBOX_LEFT
+ WD_CLOSEBOX_RIGHT
, WD_CLOSEBOX_TOP
+ WD_CLOSEBOX_BOTTOM
};
2305 if (NWidgetLeaf::closebox_dimension
.width
== 0) {
2306 NWidgetLeaf::closebox_dimension
= maxdim(GetStringBoundingBox(STR_BLACK_CROSS
), GetStringBoundingBox(STR_SILVER_CROSS
));
2307 NWidgetLeaf::closebox_dimension
.width
+= extra
.width
;
2308 NWidgetLeaf::closebox_dimension
.height
+= extra
.height
;
2310 size
= maxdim(size
, NWidgetLeaf::closebox_dimension
);
2314 case WWT_PUSHTXTBTN
:
2315 case WWT_TEXTBTN_2
: {
2316 static const Dimension extra
= {WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
};
2318 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2319 Dimension d2
= GetStringBoundingBox(this->widget_data
);
2320 d2
.width
+= extra
.width
;
2321 d2
.height
+= extra
.height
;
2322 size
= maxdim(size
, d2
);
2327 static const Dimension extra
= {0, 0};
2329 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2330 size
= maxdim(size
, GetStringBoundingBox(this->widget_data
));
2334 static const Dimension extra
= {WD_CAPTIONTEXT_LEFT
+ WD_CAPTIONTEXT_RIGHT
, WD_CAPTIONTEXT_TOP
+ WD_CAPTIONTEXT_BOTTOM
};
2336 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2337 Dimension d2
= GetStringBoundingBox(this->widget_data
);
2338 d2
.width
+= extra
.width
;
2339 d2
.height
+= extra
.height
;
2340 size
= maxdim(size
, d2
);
2344 case NWID_BUTTON_DROPDOWN
:
2345 case NWID_PUSHBUTTON_DROPDOWN
: {
2346 static Dimension extra
= {WD_DROPDOWNTEXT_LEFT
+ WD_DROPDOWNTEXT_RIGHT
, WD_DROPDOWNTEXT_TOP
+ WD_DROPDOWNTEXT_BOTTOM
};
2348 if (NWidgetLeaf::dropdown_dimension
.width
== 0) {
2349 NWidgetLeaf::dropdown_dimension
= GetSpriteSize(SPR_ARROW_DOWN
);
2350 NWidgetLeaf::dropdown_dimension
.width
+= WD_DROPDOWNTEXT_LEFT
+ WD_DROPDOWNTEXT_RIGHT
;
2351 NWidgetLeaf::dropdown_dimension
.height
+= WD_DROPDOWNTEXT_TOP
+ WD_DROPDOWNTEXT_BOTTOM
;
2352 extra
.width
= WD_DROPDOWNTEXT_LEFT
+ WD_DROPDOWNTEXT_RIGHT
+ NWidgetLeaf::dropdown_dimension
.width
;
2354 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2355 Dimension d2
= GetStringBoundingBox(this->widget_data
);
2356 d2
.width
+= extra
.width
;
2357 d2
.height
+= extra
.height
;
2358 size
= maxdim(size
, d2
);
2365 if (this->index
>= 0) w
->UpdateWidgetSize(this->index
, &size
, *padding
, &fill
, &resize
);
2367 this->smallest_x
= size
.width
;
2368 this->smallest_y
= size
.height
;
2369 this->fill_x
= fill
.width
;
2370 this->fill_y
= fill
.height
;
2371 this->resize_x
= resize
.width
;
2372 this->resize_y
= resize
.height
;
2375 void NWidgetLeaf::Draw(const Window
*w
)
2377 if (this->current_x
== 0 || this->current_y
== 0) return;
2380 r
.left
= this->pos_x
;
2381 r
.right
= this->pos_x
+ this->current_x
- 1;
2382 r
.top
= this->pos_y
;
2383 r
.bottom
= this->pos_y
+ this->current_y
- 1;
2385 const DrawPixelInfo
*dpi
= _cur_dpi
;
2386 if (dpi
->left
> r
.right
|| dpi
->left
+ dpi
->width
<= r
.left
|| dpi
->top
> r
.bottom
|| dpi
->top
+ dpi
->height
<= r
.top
) return;
2388 bool clicked
= this->IsLowered();
2389 switch (this->type
) {
2394 assert(this->widget_data
== 0);
2395 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, this->colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
2399 case WWT_PUSHIMGBTN
:
2401 DrawImageButtons(r
, this->type
, this->colour
, clicked
, this->widget_data
);
2405 case WWT_PUSHTXTBTN
:
2407 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2408 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, this->colour
, (clicked
) ? FR_LOWERED
: FR_NONE
);
2409 DrawLabel(r
, this->type
, clicked
, this->widget_data
);
2413 case WWT_PUSHARROWBTN
: {
2415 switch (this->widget_data
) {
2416 case AWV_DECREASE
: sprite
= _current_text_dir
!= TD_RTL
? SPR_ARROW_LEFT
: SPR_ARROW_RIGHT
; break;
2417 case AWV_INCREASE
: sprite
= _current_text_dir
== TD_RTL
? SPR_ARROW_LEFT
: SPR_ARROW_RIGHT
; break;
2418 case AWV_LEFT
: sprite
= SPR_ARROW_LEFT
; break;
2419 case AWV_RIGHT
: sprite
= SPR_ARROW_RIGHT
; break;
2420 default: NOT_REACHED();
2422 DrawImageButtons(r
, WWT_PUSHIMGBTN
, this->colour
, clicked
, sprite
);
2427 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2428 DrawLabel(r
, this->type
, clicked
, this->widget_data
);
2432 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2433 DrawText(r
, (TextColour
)this->colour
, this->widget_data
);
2437 DrawMatrix(r
, this->colour
, clicked
, this->widget_data
, this->resize_x
, this->resize_y
);
2441 const QueryString
*query
= w
->GetQueryString(this->index
);
2442 if (query
!= NULL
) query
->DrawEditBox(w
, this->index
);
2447 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2448 DrawCaption(r
, this->colour
, w
->owner
, this->widget_data
);
2452 assert(this->widget_data
== 0);
2453 DrawShadeBox(r
, this->colour
, w
->IsShaded());
2457 DrawDebugBox(r
, this->colour
, clicked
);
2461 assert(this->widget_data
== 0);
2462 DrawStickyBox(r
, this->colour
, !!(w
->flags
& WF_STICKY
));
2465 case WWT_DEFSIZEBOX
:
2466 assert(this->widget_data
== 0);
2467 DrawDefSizeBox(r
, this->colour
, clicked
);
2471 assert(this->widget_data
== 0);
2472 DrawResizeBox(r
, this->colour
, this->pos_x
< (uint
)(w
->width
/ 2), !!(w
->flags
& WF_SIZING
));
2476 DrawCloseBox(r
, this->colour
, this->widget_data
);
2480 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2481 DrawDropdown(r
, this->colour
, clicked
, this->widget_data
);
2484 case NWID_BUTTON_DROPDOWN
:
2485 case NWID_PUSHBUTTON_DROPDOWN
:
2486 if (this->index
>= 0) w
->SetStringParameters(this->index
);
2487 DrawButtonDropdown(r
, this->colour
, clicked
, (this->disp_flags
& ND_DROPDOWN_ACTIVE
) != 0, this->widget_data
);
2493 if (this->index
>= 0) w
->DrawWidget(r
, this->index
);
2495 if (this->IsDisabled()) {
2496 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, _colour_gradient
[this->colour
& 0xF][2], FILLRECT_CHECKER
);
2501 * For a #NWID_BUTTON_DROPDOWN, test whether \a pt refers to the button or to the drop-down.
2502 * @param pt Point in the widget.
2503 * @return The point refers to the button.
2505 * @note The magic constants are also used at #DrawButtonDropdown.
2507 bool NWidgetLeaf::ButtonHit(const Point
&pt
)
2509 if (_current_text_dir
== TD_LTR
) {
2510 int button_width
= this->pos_x
+ this->current_x
- 12;
2511 return pt
.x
< button_width
;
2513 int button_left
= this->pos_x
+ 12;
2514 return pt
.x
>= button_left
;
2518 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
2521 * Construct a single nested widget in \a *dest from its parts.
2523 * Construct a NWidgetBase object from a #NWidget function, and apply all
2524 * settings that follow it, until encountering a #EndContainer, another
2525 * #NWidget, or the end of the parts array.
2527 * @param parts Array with parts of the nested widget.
2528 * @param count Length of the \a parts array.
2529 * @param dest Address of pointer to use for returning the composed widget.
2530 * @param fill_dest Fill the composed widget with child widgets.
2531 * @param biggest_index Pointer to biggest nested widget index in the tree encountered so far.
2532 * @return Number of widget part elements used to compose the widget.
2533 * @pre \c biggest_index != NULL.
2535 static int MakeNWidget(const NWidgetPart
*parts
, int count
, NWidgetBase
**dest
, bool *fill_dest
, int *biggest_index
)
2542 while (count
> num_used
) {
2543 switch (parts
->type
) {
2545 if (*dest
!= NULL
) return num_used
;
2546 *dest
= new NWidgetSpacer(0, 0);
2549 case NWID_HORIZONTAL
:
2550 if (*dest
!= NULL
) return num_used
;
2551 *dest
= new NWidgetHorizontal(parts
->u
.cont_flags
);
2555 case NWID_HORIZONTAL_LTR
:
2556 if (*dest
!= NULL
) return num_used
;
2557 *dest
= new NWidgetHorizontalLTR(parts
->u
.cont_flags
);
2564 if (*dest
!= NULL
) return num_used
;
2565 *dest
= new NWidgetBackground(parts
->type
, parts
->u
.widget
.colour
, parts
->u
.widget
.index
);
2566 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2571 if (*dest
!= NULL
) return num_used
;
2572 *dest
= new NWidgetVertical(parts
->u
.cont_flags
);
2577 if (*dest
!= NULL
) return num_used
;
2578 NWidgetMatrix
*nwm
= new NWidgetMatrix();
2581 nwm
->SetIndex(parts
->u
.widget
.index
);
2582 nwm
->SetColour(parts
->u
.widget
.colour
);
2583 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2587 case WPT_FUNCTION
: {
2588 if (*dest
!= NULL
) return num_used
;
2589 /* Ensure proper functioning even when the called code simply writes its largest index. */
2591 *dest
= parts
->u
.func_ptr(&biggest
);
2592 *biggest_index
= max(*biggest_index
, biggest
);
2598 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(*dest
);
2600 assert(parts
->u
.xy
.x
>= 0 && parts
->u
.xy
.y
>= 0);
2601 nwrb
->SetResize(parts
->u
.xy
.x
, parts
->u
.xy
.y
);
2607 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(*dest
);
2609 assert(parts
->u
.xy
.x
>= 0 && parts
->u
.xy
.y
>= 0);
2610 nwrb
->SetMinimalSize(parts
->u
.xy
.x
, parts
->u
.xy
.y
);
2615 case WPT_MINTEXTLINES
: {
2616 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(*dest
);
2618 assert(parts
->u
.text_lines
.size
>= FS_BEGIN
&& parts
->u
.text_lines
.size
< FS_END
);
2619 nwrb
->SetMinimalTextLines(parts
->u
.text_lines
.lines
, parts
->u
.text_lines
.spacing
, parts
->u
.text_lines
.size
);
2625 NWidgetResizeBase
*nwrb
= dynamic_cast<NWidgetResizeBase
*>(*dest
);
2626 if (nwrb
!= NULL
) nwrb
->SetFill(parts
->u
.xy
.x
, parts
->u
.xy
.y
);
2631 NWidgetCore
*nwc
= dynamic_cast<NWidgetCore
*>(*dest
);
2633 nwc
->widget_data
= parts
->u
.data_tip
.data
;
2634 nwc
->tool_tip
= parts
->u
.data_tip
.tooltip
;
2640 if (*dest
!= NULL
) (*dest
)->SetPadding(parts
->u
.padding
.top
, parts
->u
.padding
.right
, parts
->u
.padding
.bottom
, parts
->u
.padding
.left
);
2643 case WPT_PIPSPACE
: {
2644 NWidgetPIPContainer
*nwc
= dynamic_cast<NWidgetPIPContainer
*>(*dest
);
2645 if (nwc
!= NULL
) nwc
->SetPIP(parts
->u
.pip
.pre
, parts
->u
.pip
.inter
, parts
->u
.pip
.post
);
2647 NWidgetBackground
*nwb
= dynamic_cast<NWidgetBackground
*>(*dest
);
2648 if (nwb
!= NULL
) nwb
->SetPIP(parts
->u
.pip
.pre
, parts
->u
.pip
.inter
, parts
->u
.pip
.post
);
2652 case WPT_SCROLLBAR
: {
2653 NWidgetCore
*nwc
= dynamic_cast<NWidgetCore
*>(*dest
);
2655 nwc
->scrollbar_index
= parts
->u
.widget
.index
;
2660 case WPT_ENDCONTAINER
:
2664 if (*dest
!= NULL
) return num_used
;
2665 *dest
= new NWidgetViewport(parts
->u
.widget
.index
);
2666 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2669 case NWID_HSCROLLBAR
:
2670 case NWID_VSCROLLBAR
:
2671 if (*dest
!= NULL
) return num_used
;
2672 *dest
= new NWidgetScrollbar(parts
->type
, parts
->u
.widget
.colour
, parts
->u
.widget
.index
);
2673 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2676 case NWID_SELECTION
: {
2677 if (*dest
!= NULL
) return num_used
;
2678 NWidgetStacked
*nws
= new NWidgetStacked();
2681 nws
->SetIndex(parts
->u
.widget
.index
);
2682 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2687 if (*dest
!= NULL
) return num_used
;
2688 assert((parts
->type
& WWT_MASK
) < WWT_LAST
|| (parts
->type
& WWT_MASK
) == NWID_BUTTON_DROPDOWN
);
2689 *dest
= new NWidgetLeaf(parts
->type
, parts
->u
.widget
.colour
, parts
->u
.widget
.index
, 0x0, STR_NULL
);
2690 *biggest_index
= max(*biggest_index
, (int)parts
->u
.widget
.index
);
2701 * Build a nested widget tree by recursively filling containers with nested widgets read from their parts.
2702 * @param parts Array with parts of the nested widgets.
2703 * @param count Length of the \a parts array.
2704 * @param parent Pointer or container to use for storing the child widgets (*parent == NULL or *parent == container or background widget).
2705 * @param biggest_index Pointer to biggest nested widget index in the tree.
2706 * @return Number of widget part elements used to fill the container.
2707 * @post \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used.
2709 static int MakeWidgetTree(const NWidgetPart
*parts
, int count
, NWidgetBase
**parent
, int *biggest_index
)
2711 /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
2712 * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
2713 NWidgetContainer
*nwid_cont
= dynamic_cast<NWidgetContainer
*>(*parent
);
2714 NWidgetBackground
*nwid_parent
= dynamic_cast<NWidgetBackground
*>(*parent
);
2715 assert(*parent
== NULL
|| (nwid_cont
!= NULL
&& nwid_parent
== NULL
) || (nwid_cont
== NULL
&& nwid_parent
!= NULL
));
2719 NWidgetBase
*sub_widget
= NULL
;
2720 bool fill_sub
= false;
2721 int num_used
= MakeNWidget(parts
, count
- total_used
, &sub_widget
, &fill_sub
, biggest_index
);
2723 total_used
+= num_used
;
2725 /* Break out of loop when end reached */
2726 if (sub_widget
== NULL
) break;
2728 /* If sub-widget is a container, recursively fill that container. */
2729 WidgetType tp
= sub_widget
->type
;
2730 if (fill_sub
&& (tp
== NWID_HORIZONTAL
|| tp
== NWID_HORIZONTAL_LTR
|| tp
== NWID_VERTICAL
|| tp
== NWID_MATRIX
2731 || tp
== WWT_PANEL
|| tp
== WWT_FRAME
|| tp
== WWT_INSET
|| tp
== NWID_SELECTION
)) {
2732 NWidgetBase
*sub_ptr
= sub_widget
;
2733 int num_used
= MakeWidgetTree(parts
, count
- total_used
, &sub_ptr
, biggest_index
);
2735 total_used
+= num_used
;
2738 /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
2739 if (nwid_cont
!= NULL
) nwid_cont
->Add(sub_widget
);
2740 if (nwid_parent
!= NULL
) nwid_parent
->Add(sub_widget
);
2741 if (nwid_cont
== NULL
&& nwid_parent
== NULL
) {
2742 *parent
= sub_widget
;
2747 if (count
== total_used
) return total_used
; // Reached the end of the array of parts?
2749 assert(total_used
< count
);
2750 assert(parts
->type
== WPT_ENDCONTAINER
);
2751 return total_used
+ 1; // *parts is also 'used'
2755 * Construct a nested widget tree from an array of parts.
2756 * @param parts Array with parts of the widgets.
2757 * @param count Length of the \a parts array.
2758 * @param biggest_index Pointer to biggest nested widget index collected in the tree.
2759 * @param container Container to add the nested widgets to. In case it is NULL a vertical container is used.
2760 * @return Root of the nested widget tree, a vertical container containing the entire GUI.
2761 * @ingroup NestedWidgetParts
2762 * @pre \c biggest_index != NULL
2763 * @post \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used.
2765 NWidgetContainer
*MakeNWidgets(const NWidgetPart
*parts
, int count
, int *biggest_index
, NWidgetContainer
*container
)
2767 *biggest_index
= -1;
2768 if (container
== NULL
) container
= new NWidgetVertical();
2769 NWidgetBase
*cont_ptr
= container
;
2770 MakeWidgetTree(parts
, count
, &cont_ptr
, biggest_index
);
2775 * Make a nested widget tree for a window from a parts array. Besides loading, it inserts a shading selection widget
2776 * 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
2777 * container with a caption widget) and has a shade box widget.
2778 * @param parts Array with parts of the widgets.
2779 * @param count Length of the \a parts array.
2780 * @param biggest_index Pointer to biggest nested widget index collected in the tree.
2781 * @param [out] shade_select Pointer to the inserted shade selection widget (\c NULL if not unserted).
2782 * @return Root of the nested widget tree, a vertical container containing the entire GUI.
2783 * @ingroup NestedWidgetParts
2784 * @pre \c biggest_index != NULL
2785 * @post \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used.
2787 NWidgetContainer
*MakeWindowNWidgetTree(const NWidgetPart
*parts
, int count
, int *biggest_index
, NWidgetStacked
**shade_select
)
2789 *biggest_index
= -1;
2791 /* Read the first widget recursively from the array. */
2792 NWidgetBase
*nwid
= NULL
;
2793 int num_used
= MakeWidgetTree(parts
, count
, &nwid
, biggest_index
);
2794 assert(nwid
!= NULL
);
2798 NWidgetContainer
*root
= new NWidgetVertical
;
2800 if (count
== 0) { // There is no body at all.
2801 *shade_select
= NULL
;
2805 /* If the first widget looks like a titlebar, treat it as such.
2806 * If it has a shading box, silently add a shade selection widget in the tree. */
2807 NWidgetHorizontal
*hor_cont
= dynamic_cast<NWidgetHorizontal
*>(nwid
);
2808 NWidgetContainer
*body
;
2809 if (hor_cont
!= NULL
&& hor_cont
->GetWidgetOfType(WWT_CAPTION
) != NULL
&& hor_cont
->GetWidgetOfType(WWT_SHADEBOX
) != NULL
) {
2810 *shade_select
= new NWidgetStacked
;
2811 root
->Add(*shade_select
);
2812 body
= new NWidgetVertical
;
2813 (*shade_select
)->Add(body
);
2815 *shade_select
= NULL
;
2819 /* Load the remaining parts into 'body'. */
2821 MakeNWidgets(parts
, count
, &biggest2
, body
);
2823 *biggest_index
= max(*biggest_index
, biggest2
);
2828 * Make a number of rows with button-like graphics, for enabling/disabling each company.
2829 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
2830 * @param widget_first The first widget index to use.
2831 * @param widget_last The last widget index to use.
2832 * @param max_length Maximal number of company buttons in one row.
2833 * @param button_tooltip The tooltip-string of every button.
2834 * @return Panel with rows of company buttons.
2835 * @post \c *biggest_index contains the largest used index in the tree.
2837 NWidgetBase
*MakeCompanyButtonRows(int *biggest_index
, int widget_first
, int widget_last
, int max_length
, StringID button_tooltip
)
2839 assert(max_length
>= 1);
2840 NWidgetVertical
*vert
= NULL
; // Storage for all rows.
2841 NWidgetHorizontal
*hor
= NULL
; // Storage for buttons in one row.
2844 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
);
2845 sprite_size
.width
+= WD_MATRIX_LEFT
+ WD_MATRIX_RIGHT
;
2846 sprite_size
.height
+= WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
+ 1; // 1 for the 'offset' of being pressed
2848 for (int widnum
= widget_first
; widnum
<= widget_last
; widnum
++) {
2849 /* Ensure there is room in 'hor' for another button. */
2850 if (hor_length
== max_length
) {
2851 if (vert
== NULL
) vert
= new NWidgetVertical();
2857 hor
= new NWidgetHorizontal();
2861 NWidgetBackground
*panel
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, widnum
);
2862 panel
->SetMinimalSize(sprite_size
.width
, sprite_size
.height
);
2863 panel
->SetFill(1, 1);
2864 panel
->SetResize(1, 0);
2865 panel
->SetDataTip(0x0, button_tooltip
);
2869 *biggest_index
= widget_last
;
2870 if (vert
== NULL
) return hor
; // All buttons fit in a single row.
2872 if (hor_length
> 0 && hor_length
< max_length
) {
2873 /* Last row is partial, add a spacer at the end to force all buttons to the left. */
2874 NWidgetSpacer
*spc
= new NWidgetSpacer(sprite_size
.width
, sprite_size
.height
);
2876 spc
->SetResize(1, 0);
2879 if (hor
!= NULL
) vert
->Add(hor
);