OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

widget.cpp
Go to the documentation of this file.
1 /* $Id: widget.cpp 27255 2015-04-26 11:07:41Z frosch $ */
2
3 /*
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/>.
8 */
9
12 #include "stdafx.h"
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"
22
23 #include "table/sprites.h"
24 #include "table/strings.h"
25 #include "table/palettes.h"
26
27 #include "safeguards.h"
28
38 static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bool horizontal)
39 {
40 /* Base for reversion */
41 int rev_base = top + bottom;
42 int button_size;
43 if (horizontal) {
44 button_size = NWidgetScrollbar::GetHorizontalDimension().width;
45 } else {
46 button_size = NWidgetScrollbar::GetVerticalDimension().height;
47 }
48 top += button_size; // top points to just below the up-button
49 bottom -= button_size; // bottom points to top of the down-button
50
51 int height = (bottom - top);
52 int pos = sb->GetPosition();
53 int count = sb->GetCount();
54 int cap = sb->GetCapacity();
55
56 if (count != 0) top += height * pos / count;
57
58 if (cap > count) cap = count;
59 if (count != 0) bottom -= (count - pos - cap) * height / count;
60
61 Point pt;
62 if (horizontal && _current_text_dir == TD_RTL) {
63 pt.x = rev_base - bottom;
64 pt.y = rev_base - top;
65 } else {
66 pt.x = top;
67 pt.y = bottom;
68 }
69 return pt;
70 }
71
81 static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma)
82 {
83 int pos;
84 int button_size;
85 bool rtl = false;
86
87 if (sb->type == NWID_HSCROLLBAR) {
88 pos = x;
89 rtl = _current_text_dir == TD_RTL;
90 button_size = NWidgetScrollbar::GetHorizontalDimension().width;
91 } else {
92 pos = y;
93 button_size = NWidgetScrollbar::GetVerticalDimension().height;
94 }
95 if (pos < mi + button_size) {
96 /* Pressing the upper button? */
97 SetBit(sb->disp_flags, NDB_SCROLLBAR_UP);
98 if (_scroller_click_timeout <= 1) {
99 _scroller_click_timeout = 3;
100 sb->UpdatePosition(rtl ? 1 : -1);
101 }
102 w->scrolling_scrollbar = sb->index;
103 } else if (pos >= ma - button_size) {
104 /* Pressing the lower button? */
105 SetBit(sb->disp_flags, NDB_SCROLLBAR_DOWN);
106
107 if (_scroller_click_timeout <= 1) {
108 _scroller_click_timeout = 3;
109 sb->UpdatePosition(rtl ? -1 : 1);
110 }
111 w->scrolling_scrollbar = sb->index;
112 } else {
113 Point pt = HandleScrollbarHittest(sb, mi, ma, sb->type == NWID_HSCROLLBAR);
114
115 if (pos < pt.x) {
116 sb->UpdatePosition(rtl ? 1 : -1, Scrollbar::SS_BIG);
117 } else if (pos > pt.y) {
118 sb->UpdatePosition(rtl ? -1 : 1, Scrollbar::SS_BIG);
119 } else {
120 _scrollbar_start_pos = pt.x - mi - button_size;
121 _scrollbar_size = ma - mi - button_size * 2;
122 w->scrolling_scrollbar = sb->index;
123 _cursorpos_drag_start = _cursor.pos;
124 }
125 }
126
127 w->SetDirty();
128 }
129
138 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
139 {
140 int mi, ma;
141
142 if (nw->type == NWID_HSCROLLBAR) {
143 mi = nw->pos_x;
144 ma = nw->pos_x + nw->current_x;
145 } else {
146 mi = nw->pos_y;
147 ma = nw->pos_y + nw->current_y;
148 }
149 NWidgetScrollbar *scrollbar = dynamic_cast<NWidgetScrollbar*>(nw);
150 assert(scrollbar != NULL);
151 ScrollbarClickPositioning(w, scrollbar, x, y, mi, ma);
152 }
153
162 int GetWidgetFromPos(const Window *w, int x, int y)
163 {
164 NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
165 return (nw != NULL) ? nw->index : -1;
166 }
167
177 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
178 {
179 assert(colour < COLOUR_END);
180
181 uint dark = _colour_gradient[colour][3];
182 uint medium_dark = _colour_gradient[colour][5];
183 uint medium_light = _colour_gradient[colour][6];
184 uint light = _colour_gradient[colour][7];
185
186 if (flags & FR_TRANSPARENT) {
187 GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR);
188 } else {
189 uint interior;
190
191 if (flags & FR_LOWERED) {
192 GfxFillRect(left, top, left, bottom, dark);
193 GfxFillRect(left + WD_BEVEL_LEFT, top, right, top, dark);
194 GfxFillRect(right, top + WD_BEVEL_TOP, right, bottom - WD_BEVEL_BOTTOM, light);
195 GfxFillRect(left + WD_BEVEL_LEFT, bottom, right, bottom, light);
196 interior = (flags & FR_DARKENED ? medium_dark : medium_light);
197 } else {
198 GfxFillRect(left, top, left, bottom - WD_BEVEL_BOTTOM, light);
199 GfxFillRect(left + WD_BEVEL_LEFT, top, right - WD_BEVEL_RIGHT, top, light);
200 GfxFillRect(right, top, right, bottom - WD_BEVEL_BOTTOM, dark);
201 GfxFillRect(left, bottom, right, bottom, dark);
202 interior = medium_dark;
203 }
204 if (!(flags & FR_BORDERONLY)) {
205 GfxFillRect(left + WD_BEVEL_LEFT, top + WD_BEVEL_TOP, right - WD_BEVEL_RIGHT, bottom - WD_BEVEL_BOTTOM, interior);
206 }
207 }
208 }
209
218 static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
219 {
220 assert(img != 0);
221 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
222
223 if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2.
224 DrawSprite(img, PAL_NONE, r.left + WD_IMGBTN_LEFT + clicked, r.top + WD_IMGBTN_TOP + clicked);
225 }
226
234 static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
235 {
236 if (str == STR_NULL) return;
237 if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
238 Dimension d = GetStringBoundingBox(str);
239 int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
240 DrawString(r.left + clicked, r.right + clicked, r.top + offset + clicked, str, TC_FROMSTRING, SA_HOR_CENTER);
241 }
242
249 static inline void DrawText(const Rect &r, TextColour colour, StringID str)
250 {
251 Dimension d = GetStringBoundingBox(str);
252 int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
253 if (str != STR_NULL) DrawString(r.left, r.right, r.top + offset, str, colour);
254 }
255
262 static inline void DrawInset(const Rect &r, Colours colour, StringID str)
263 {
264 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
265 if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str);
266 }
267
277 static inline void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint16 data, uint resize_x, uint resize_y)
278 {
279 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
280
281 int num_columns = GB(data, MAT_COL_START, MAT_COL_BITS); // Lower 8 bits of the widget data: Number of columns in the matrix.
282 int column_width; // Width of a single column in the matrix.
283 if (num_columns == 0) {
284 column_width = resize_x;
285 num_columns = (r.right - r.left + 1) / column_width;
286 } else {
287 column_width = (r.right - r.left + 1) / num_columns;
288 }
289
290 int num_rows = GB(data, MAT_ROW_START, MAT_ROW_BITS); // Upper 8 bits of the widget data: Number of rows in the matrix.
291 int row_height; // Height of a single row in the matrix.
292 if (num_rows == 0) {
293 row_height = resize_y;
294 num_rows = (r.bottom - r.top + 1) / row_height;
295 } else {
296 row_height = (r.bottom - r.top + 1) / num_rows;
297 }
298
299 int col = _colour_gradient[colour & 0xF][6];
300
301 int x = r.left;
302 for (int ctr = num_columns; ctr > 1; ctr--) {
303 x += column_width;
304 GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
305 }
306
307 x = r.top;
308 for (int ctr = num_rows; ctr > 1; ctr--) {
309 x += row_height;
310 GfxFillRect(r.left + 1, x, r.right - 1, x, col);
311 }
312
313 col = _colour_gradient[colour & 0xF][4];
314
315 x = r.left - 1;
316 for (int ctr = num_columns; ctr > 1; ctr--) {
317 x += column_width;
318 GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
319 }
320
321 x = r.top - 1;
322 for (int ctr = num_rows; ctr > 1; ctr--) {
323 x += row_height;
324 GfxFillRect(r.left + 1, x, r.right - 1, x, col);
325 }
326 }
327
337 static inline void DrawVerticalScrollbar(const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
338 {
339 int centre = (r.right - r.left) / 2;
340 int height = NWidgetScrollbar::GetVerticalDimension().height;
341
342 /* draw up/down buttons */
343 DrawFrameRect(r.left, r.top, r.right, r.top + height - 1, colour, (up_clicked) ? FR_LOWERED : FR_NONE);
344 DrawSprite(SPR_ARROW_UP, PAL_NONE, r.left + 1 + up_clicked, r.top + 1 + up_clicked);
345
346 DrawFrameRect(r.left, r.bottom - (height - 1), r.right, r.bottom, colour, (down_clicked) ? FR_LOWERED : FR_NONE);
347 DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.left + 1 + down_clicked, r.bottom - (height - 2) + down_clicked);
348
349 int c1 = _colour_gradient[colour & 0xF][3];
350 int c2 = _colour_gradient[colour & 0xF][7];
351
352 /* draw "shaded" background */
353 GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c2);
354 GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c1, FILLRECT_CHECKER);
355
356 /* draw shaded lines */
357 GfxFillRect(r.left + centre - 3, r.top + height, r.left + centre - 3, r.bottom - height, c1);
358 GfxFillRect(r.left + centre - 2, r.top + height, r.left + centre - 2, r.bottom - height, c2);
359 GfxFillRect(r.left + centre + 2, r.top + height, r.left + centre + 2, r.bottom - height, c1);
360 GfxFillRect(r.left + centre + 3, r.top + height, r.left + centre + 3, r.bottom - height, c2);
361
362 Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom, false);
363 DrawFrameRect(r.left, pt.x, r.right, pt.y, colour, bar_dragged ? FR_LOWERED : FR_NONE);
364 }
365
375 static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
376 {
377 int centre = (r.bottom - r.top) / 2;
378 int width = NWidgetScrollbar::GetHorizontalDimension().width;
379
380 DrawFrameRect(r.left, r.top, r.left + width - 1, r.bottom, colour, left_clicked ? FR_LOWERED : FR_NONE);
381 DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + left_clicked, r.top + 1 + left_clicked);
382
383 DrawFrameRect(r.right - (width - 1), r.top, r.right, r.bottom, colour, right_clicked ? FR_LOWERED : FR_NONE);
384 DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - (width - 2) + right_clicked, r.top + 1 + right_clicked);
385
386 int c1 = _colour_gradient[colour & 0xF][3];
387 int c2 = _colour_gradient[colour & 0xF][7];
388
389 /* draw "shaded" background */
390 GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c2);
391 GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c1, FILLRECT_CHECKER);
392
393 /* draw shaded lines */
394 GfxFillRect(r.left + width, r.top + centre - 3, r.right - width, r.top + centre - 3, c1);
395 GfxFillRect(r.left + width, r.top + centre - 2, r.right - width, r.top + centre - 2, c2);
396 GfxFillRect(r.left + width, r.top + centre + 2, r.right - width, r.top + centre + 2, c1);
397 GfxFillRect(r.left + width, r.top + centre + 3, r.right - width, r.top + centre + 3, c2);
398
399 /* draw actual scrollbar */
400 Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right, true);
401 DrawFrameRect(pt.x, r.top, pt.y, r.bottom, colour, bar_dragged ? FR_LOWERED : FR_NONE);
402 }
403
410 static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
411 {
412 int x2 = r.left; // by default the left side is the left side of the widget
413
414 if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str);
415
416 int c1 = _colour_gradient[colour][3];
417 int c2 = _colour_gradient[colour][7];
418
419 /* If the frame has text, adjust the top bar to fit half-way through */
420 int dy1 = 4;
421 if (str != STR_NULL) dy1 = FONT_HEIGHT_NORMAL / 2 - 1;
422 int dy2 = dy1 + 1;
423
424 if (_current_text_dir == TD_LTR) {
425 /* Line from upper left corner to start of text */
426 GfxFillRect(r.left, r.top + dy1, r.left + 4, r.top + dy1, c1);
427 GfxFillRect(r.left + 1, r.top + dy2, r.left + 4, r.top + dy2, c2);
428
429 /* Line from end of text to upper right corner */
430 GfxFillRect(x2, r.top + dy1, r.right - 1, r.top + dy1, c1);
431 GfxFillRect(x2, r.top + dy2, r.right - 2, r.top + dy2, c2);
432 } else {
433 /* Line from upper left corner to start of text */
434 GfxFillRect(r.left, r.top + dy1, x2 - 2, r.top + dy1, c1);
435 GfxFillRect(r.left + 1, r.top + dy2, x2 - 2, r.top + dy2, c2);
436
437 /* Line from end of text to upper right corner */
438 GfxFillRect(r.right - 5, r.top + dy1, r.right - 1, r.top + dy1, c1);
439 GfxFillRect(r.right - 5, r.top + dy2, r.right - 2, r.top + dy2, c2);
440 }
441
442 /* Line from upper left corner to bottom left corner */
443 GfxFillRect(r.left, r.top + dy2, r.left, r.bottom - 1, c1);
444 GfxFillRect(r.left + 1, r.top + dy2 + 1, r.left + 1, r.bottom - 2, c2);
445
446 /* Line from upper right corner to bottom right corner */
447 GfxFillRect(r.right - 1, r.top + dy2, r.right - 1, r.bottom - 2, c1);
448 GfxFillRect(r.right, r.top + dy1, r.right, r.bottom - 1, c2);
449
450 GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
451 GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
452 }
453
460 static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
461 {
462 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
463 DrawSprite((clicked) ? SPR_WINDOW_SHADE : SPR_WINDOW_UNSHADE, PAL_NONE, r.left + WD_SHADEBOX_LEFT + clicked, r.top + WD_SHADEBOX_TOP + clicked);
464 }
465
472 static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
473 {
474 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
475 DrawSprite((clicked) ? SPR_PIN_UP : SPR_PIN_DOWN, PAL_NONE, r.left + WD_STICKYBOX_LEFT + clicked, r.top + WD_STICKYBOX_TOP + clicked);
476 }
477
484 static inline void DrawDefSizeBox(const Rect &r, Colours colour, bool clicked)
485 {
486 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
487 DrawSprite(SPR_WINDOW_DEFSIZE, PAL_NONE, r.left + WD_DEFSIZEBOX_LEFT + clicked, r.top + WD_DEFSIZEBOX_TOP + clicked);
488 }
489
496 static inline void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
497 {
498 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
499 DrawSprite(SPR_WINDOW_DEBUG, PAL_NONE, r.left + WD_DEBUGBOX_LEFT + clicked, r.top + WD_DEBUGBOX_TOP + clicked);
500 }
501
509 static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
510 {
511 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
512 if (at_left) {
513 DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked,
514 r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_LEFT).height + clicked);
515 } else {
516 DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.left + WD_RESIZEBOX_LEFT + clicked,
517 r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT).height + clicked);
518 }
519 }
520
526 static inline void DrawCloseBox(const Rect &r, Colours colour)
527 {
528 if (colour != COLOUR_WHITE) DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE);
529 DrawSprite(SPR_CLOSEBOX, (colour != COLOUR_WHITE ? TC_BLACK : TC_SILVER) | (1 << PALETTE_TEXT_RECOLOUR), r.left + WD_CLOSEBOX_LEFT, r.top + WD_CLOSEBOX_TOP);
530 }
531
539 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
540 {
541 bool company_owned = owner < MAX_COMPANIES;
542
543 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_BORDERONLY);
544 DrawFrameRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, colour, company_owned ? FR_LOWERED | FR_DARKENED | FR_BORDERONLY : FR_LOWERED | FR_DARKENED);
545
546 if (company_owned) {
547 GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]);
548 }
549
550 if (str != STR_NULL) {
551 Dimension d = GetStringBoundingBox(str);
552 int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
553 DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + offset, str, TC_FROMSTRING, SA_HOR_CENTER);
554 }
555 }
556
567 static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
568 {
569 int text_offset = max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered
570
571 int dd_width = NWidgetLeaf::dropdown_dimension.width;
572 int dd_height = NWidgetLeaf::dropdown_dimension.height;
573 int image_offset = max(0, ((int)(r.bottom - r.top + 1) - dd_height) / 2);
574
575 if (_current_text_dir == TD_LTR) {
576 DrawFrameRect(r.left, r.top, r.right - dd_width, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
577 DrawFrameRect(r.right + 1 - dd_width, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
578 DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.right - (dd_width - 2) + clicked_dropdown, r.top + image_offset + clicked_dropdown);
579 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 } else {
581 DrawFrameRect(r.left + dd_width, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
582 DrawFrameRect(r.left, r.top, r.left + dd_width - 1, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
583 DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.left + 1 + clicked_dropdown, r.top + image_offset + clicked_dropdown);
584 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);
585 }
586 }
587
595 static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
596 {
597 DrawButtonDropdown(r, colour, false, clicked, str);
598 }
599
603 void Window::DrawWidgets() const
604 {
605 this->nested_root->Draw(this);
606
607 if (this->flags & WF_WHITE_BORDER) {
608 DrawFrameRect(0, 0, this->width - 1, this->height - 1, COLOUR_WHITE, FR_BORDERONLY);
609 }
610
611 if (this->flags & WF_HIGHLIGHTED) {
612 extern bool _window_highlight_colour;
613 for (uint i = 0; i < this->nested_array_size; i++) {
614 const NWidgetBase *widget = this->GetWidget<NWidgetBase>(i);
615 if (widget == NULL || !widget->IsHighlighted()) continue;
616
617 int left = widget->pos_x;
618 int top = widget->pos_y;
619 int right = left + widget->current_x - 1;
620 int bottom = top + widget->current_y - 1;
621
622 int colour = _string_colourmap[_window_highlight_colour ? widget->GetHighlightColour() : TC_WHITE];
623
624 GfxFillRect(left, top, left, bottom - WD_BEVEL_BOTTOM, colour);
625 GfxFillRect(left + WD_BEVEL_LEFT, top, right - WD_BEVEL_RIGHT, top, colour);
626 GfxFillRect(right, top, right, bottom - WD_BEVEL_BOTTOM, colour);
627 GfxFillRect(left, bottom, right, bottom, colour);
628 }
629 }
630 }
631
637 void Window::DrawSortButtonState(int widget, SortButtonState state) const
638 {
639 if (state == SBS_OFF) return;
640
641 assert(this->nested_array != NULL);
642 const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
643
644 /* Sort button uses the same sprites as vertical scrollbar */
645 Dimension dim = NWidgetScrollbar::GetVerticalDimension();
646 int offset = this->IsWidgetLowered(widget) ? 1 : 0;
647 int x = offset + nwid->pos_x + (_current_text_dir == TD_LTR ? nwid->current_x - dim.width : 0);
648 int y = offset + nwid->pos_y + (nwid->current_y - dim.height) / 2;
649
650 DrawSprite(state == SBS_DOWN ? SPR_ARROW_DOWN : SPR_ARROW_UP, PAL_NONE, x, y);
651 }
652
657 int Window::SortButtonWidth()
658 {
659 return NWidgetScrollbar::GetVerticalDimension().width + 1;
660 }
661
662
720 NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator()
721 {
722 this->type = tp;
723 }
724
725 /* ~NWidgetContainer() takes care of #next and #prev data members. */
726
774 void NWidgetBase::SetDirty(const Window *w) const
775 {
776 int abs_left = w->left + this->pos_x;
777 int abs_top = w->top + this->pos_y;
778 SetDirtyBlocks(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
779 }
780
794 NWidgetBase *NWidgetBase::GetWidgetOfType(WidgetType tp)
795 {
796 return (this->type == tp) ? this : NULL;
797 }
798
805 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y) : NWidgetBase(tp)
806 {
807 this->fill_x = fill_x;
808 this->fill_y = fill_y;
809 }
810
816 void NWidgetResizeBase::SetMinimalSize(uint min_x, uint min_y)
817 {
818 this->min_x = max(this->min_x, min_x);
819 this->min_y = max(this->min_y, min_y);
820 }
821
828 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
829 {
830 this->min_y = min_lines * GetCharacterHeight(size) + spacing;
831 }
832
838 void NWidgetResizeBase::SetFill(uint fill_x, uint fill_y)
839 {
840 this->fill_x = fill_x;
841 this->fill_y = fill_y;
842 }
843
849 void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y)
850 {
851 this->resize_x = resize_x;
852 this->resize_y = resize_y;
853 }
854
855 void NWidgetResizeBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
856 {
857 this->StoreSizePosition(sizing, x, y, given_width, given_height);
858 }
859
869 NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint32 widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y)
870 {
871 this->colour = colour;
872 this->index = -1;
873 this->widget_data = widget_data;
874 this->tool_tip = tool_tip;
875 this->scrollbar_index = -1;
876 }
877
882 void NWidgetCore::SetIndex(int index)
883 {
884 assert(index >= 0);
885 this->index = index;
886 }
887
893 void NWidgetCore::SetDataTip(uint32 widget_data, StringID tool_tip)
894 {
895 this->widget_data = widget_data;
896 this->tool_tip = tool_tip;
897 }
898
899 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
900 {
901 if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
902 }
903
904 NWidgetCore *NWidgetCore::GetWidgetFromPos(int x, int y)
905 {
906 return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : NULL;
907 }
908
913 NWidgetContainer::NWidgetContainer(WidgetType tp) : NWidgetBase(tp)
914 {
915 this->head = NULL;
916 this->tail = NULL;
917 }
918
919 NWidgetContainer::~NWidgetContainer()
920 {
921 while (this->head != NULL) {
922 NWidgetBase *wid = this->head->next;
923 delete this->head;
924 this->head = wid;
925 }
926 this->tail = NULL;
927 }
928
929 NWidgetBase *NWidgetContainer::GetWidgetOfType(WidgetType tp)
930 {
931 if (this->type == tp) return this;
932 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
933 NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
934 if (nwid != NULL) return nwid;
935 }
936 return NULL;
937 }
938
943 void NWidgetContainer::Add(NWidgetBase *wid)
944 {
945 assert(wid->next == NULL && wid->prev == NULL);
946
947 if (this->head == NULL) {
948 this->head = wid;
949 this->tail = wid;
950 } else {
951 assert(this->tail != NULL);
952 assert(this->tail->next == NULL);
953
954 this->tail->next = wid;
955 wid->prev = this->tail;
956 this->tail = wid;
957 }
958 }
959
960 void NWidgetContainer::FillNestedArray(NWidgetBase **array, uint length)
961 {
962 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
963 child_wid->FillNestedArray(array, length);
964 }
965 }
966
970 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION)
971 {
972 this->index = -1;
973 }
974
975 void NWidgetStacked::SetIndex(int index)
976 {
977 this->index = index;
978 }
979
980 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
981 {
982 if (this->index >= 0 && init_array) { // Fill w->nested_array[]
983 assert(w->nested_array_size > (uint)this->index);
984 w->nested_array[this->index] = this;
985 }
986
987 /* Zero size plane selected */
988 if (this->shown_plane >= SZSP_BEGIN) {
989 Dimension size = {0, 0};
990 Dimension padding = {0, 0};
991 Dimension fill = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
992 Dimension resize = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
993 /* Here we're primarily interested in the value of resize */
994 if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
995
996 this->smallest_x = size.width;
997 this->smallest_y = size.height;
998 this->fill_x = fill.width;
999 this->fill_y = fill.height;
1000 this->resize_x = resize.width;
1001 this->resize_y = resize.height;
1002 return;
1003 }
1004
1005 /* First sweep, recurse down and compute minimal size and filling. */
1006 this->smallest_x = 0;
1007 this->smallest_y = 0;
1008 this->fill_x = (this->head != NULL) ? 1 : 0;
1009 this->fill_y = (this->head != NULL) ? 1 : 0;
1010 this->resize_x = (this->head != NULL) ? 1 : 0;
1011 this->resize_y = (this->head != NULL) ? 1 : 0;
1012 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1013 child_wid->SetupSmallestSize(w, init_array);
1014
1015 this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
1016 this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
1017 this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
1018 this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
1019 this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
1020 this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
1021 }
1022 }
1023
1024 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1025 {
1026 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1027 this->StoreSizePosition(sizing, x, y, given_width, given_height);
1028
1029 if (this->shown_plane >= SZSP_BEGIN) return;
1030
1031 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1032 uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
1033 uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
1034 uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
1035
1036 uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
1037 uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
1038 uint child_pos_y = child_wid->padding_top;
1039
1040 child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
1041 }
1042 }
1043
1044 void NWidgetStacked::FillNestedArray(NWidgetBase **array, uint length)
1045 {
1046 if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
1047 NWidgetContainer::FillNestedArray(array, length);
1048 }
1049
1050 void NWidgetStacked::Draw(const Window *w)
1051 {
1052 if (this->shown_plane >= SZSP_BEGIN) return;
1053
1054 int plane = 0;
1055 for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
1056 if (plane == this->shown_plane) {
1057 child_wid->Draw(w);
1058 return;
1059 }
1060 }
1061
1062 NOT_REACHED();
1063 }
1064
1065 NWidgetCore *NWidgetStacked::GetWidgetFromPos(int x, int y)
1066 {
1067 if (this->shown_plane >= SZSP_BEGIN) return NULL;
1068
1069 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1070 int plane = 0;
1071 for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
1072 if (plane == this->shown_plane) {
1073 return child_wid->GetWidgetFromPos(x, y);
1074 }
1075 }
1076 return NULL;
1077 }
1078
1083 void NWidgetStacked::SetDisplayedPlane(int plane)
1084 {
1085 this->shown_plane = plane;
1086 }
1087
1088 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
1089 {
1090 this->flags = flags;
1091 }
1092
1102 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
1103 {
1104 this->pip_pre = pip_pre;
1105 this->pip_inter = pip_inter;
1106 this->pip_post = pip_post;
1107 }
1108
1109 void NWidgetPIPContainer::Draw(const Window *w)
1110 {
1111 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1112 child_wid->Draw(w);
1113 }
1114 }
1115
1116 NWidgetCore *NWidgetPIPContainer::GetWidgetFromPos(int x, int y)
1117 {
1118 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1119
1120 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1121 NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
1122 if (nwid != NULL) return nwid;
1123 }
1124 return NULL;
1125 }
1126
1128 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
1129 {
1130 }
1131
1132 void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array)
1133 {
1134 this->smallest_x = 0; // Sum of minimal size of all children.
1135 this->smallest_y = 0; // Biggest child.
1136 this->fill_x = 0; // smallest non-zero child widget fill step.
1137 this->fill_y = 1; // smallest common child fill step.
1138 this->resize_x = 0; // smallest non-zero child widget resize step.
1139 this->resize_y = 1; // smallest common child resize step.
1140
1141 /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1142 uint longest = 0; // Longest child found.
1143 uint max_vert_fill = 0; // Biggest vertical fill step.
1144 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1145 child_wid->SetupSmallestSize(w, init_array);
1146 longest = max(longest, child_wid->smallest_x);
1147 max_vert_fill = max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
1148 this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
1149 }
1150 /* 1b. Make the container higher if needed to accommodate all children nicely. */
1151 uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height.
1152 uint cur_height = this->smallest_y;
1153 for (;;) {
1154 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1155 uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
1156 uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
1157 if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting children are not interesting.
1158 uint remainder = (cur_height - child_height) % step_size;
1159 if (remainder > 0) { // Child did not fit entirely, widen the container.
1160 cur_height += step_size - remainder;
1161 assert(cur_height < max_smallest); // Safeguard against infinite height expansion.
1162 /* Remaining children will adapt to the new cur_height, thus speeding up the computation. */
1163 }
1164 }
1165 }
1166 if (this->smallest_y == cur_height) break;
1167 this->smallest_y = cur_height; // Smallest height got changed, try again.
1168 }
1169 /* 2. For containers that must maintain equal width, extend child minimal size. */
1170 if (this->flags & NC_EQUALSIZE) {
1171 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1172 if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
1173 }
1174 }
1175 /* 3. Move PIP space to the children, compute smallest, fill, and resize values of the container. */
1176 if (this->head != NULL) this->head->padding_left += this->pip_pre;
1177 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1178 if (child_wid->next != NULL) {
1179 child_wid->padding_right += this->pip_inter;
1180 } else {
1181 child_wid->padding_right += this->pip_post;
1182 }
1183
1184 this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
1185 if (child_wid->fill_x > 0) {
1186 if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
1187 }
1188 this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
1189
1190 if (child_wid->resize_x > 0) {
1191 if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
1192 }
1193 this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
1194 }
1195 /* We need to zero the PIP settings so we can re-initialize the tree. */
1196 this->pip_pre = this->pip_inter = this->pip_post = 0;
1197 }
1198
1199 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1200 {
1201 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1202
1203 /* Compute additional width given to us. */
1204 uint additional_length = given_width;
1205 if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
1206 /* For EQUALSIZE containers this does not sum to smallest_x during initialisation */
1207 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1208 additional_length -= child_wid->smallest_x + child_wid->padding_right + child_wid->padding_left;
1209 }
1210 } else {
1211 additional_length -= this->smallest_x;
1212 }
1213
1214 this->StoreSizePosition(sizing, x, y, given_width, given_height);
1215
1216 /* In principle, the additional horizontal space is distributed evenly over the available resizable children. Due to step sizes, this may not always be feasible.
1217 * To make resizing work as good as possible, first children with biggest step sizes are done. These may get less due to rounding down.
1218 * 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
1219 * of the child with the smallest non-zero stepsize.
1220 *
1221 * 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
1222 * size and position, and directly call child->AssignSizePosition() with the computed values.
1223 * 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
1224 * then we call the child.
1225 */
1226
1227 /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle vertical size for all children,
1228 * handle horizontal size for non-resizing children.
1229 */
1230 int num_changing_childs = 0; // Number of children that can change size.
1231 uint biggest_stepsize = 0;
1232 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1233 uint hor_step = child_wid->GetHorizontalStepSize(sizing);
1234 if (hor_step > 0) {
1235 num_changing_childs++;
1236 biggest_stepsize = max(biggest_stepsize, hor_step);
1237 } else {
1238 child_wid->current_x = child_wid->smallest_x;
1239 }
1240
1241 uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
1242 child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
1243 }
1244
1245 /* Second loop: Allocate the additional horizontal space over the resizing children, starting with the biggest resize steps. */
1246 while (biggest_stepsize > 0) {
1247 uint next_biggest_stepsize = 0;
1248 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1249 uint hor_step = child_wid->GetHorizontalStepSize(sizing);
1250 if (hor_step > biggest_stepsize) continue; // Already done
1251 if (hor_step == biggest_stepsize) {
1252 uint increment = additional_length / num_changing_childs;
1253 num_changing_childs--;
1254 if (hor_step > 1) increment -= increment % hor_step;
1255 child_wid->current_x = child_wid->smallest_x + increment;
1256 additional_length -= increment;
1257 continue;
1258 }
1259 next_biggest_stepsize = max(next_biggest_stepsize, hor_step);
1260 }
1261 biggest_stepsize = next_biggest_stepsize;
1262 }
1263 assert(num_changing_childs == 0);
1264
1265 /* Third loop: Compute position and call the child. */
1266 uint position = rtl ? this->current_x : 0; // Place to put next child relative to origin of the container.
1267 NWidgetBase *child_wid = this->head;
1268 while (child_wid != NULL) {
1269 uint child_width = child_wid->current_x;
1270 uint child_x = x + (rtl ? position - child_width - child_wid->padding_left : position + child_wid->padding_left);
1271 uint child_y = y + child_wid->padding_top;
1272
1273 child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
1274 uint padded_child_width = child_width + child_wid->padding_right + child_wid->padding_left;
1275 position = rtl ? position - padded_child_width : position + padded_child_width;
1276
1277 child_wid = child_wid->next;
1278 }
1279 }
1280
1282 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
1283 {
1284 this->type = NWID_HORIZONTAL_LTR;
1285 }
1286
1287 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1288 {
1289 NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
1290 }
1291
1293 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
1294 {
1295 }
1296
1297 void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array)
1298 {
1299 this->smallest_x = 0; // Biggest child.
1300 this->smallest_y = 0; // Sum of minimal size of all children.
1301 this->fill_x = 1; // smallest common child fill step.
1302 this->fill_y = 0; // smallest non-zero child widget fill step.
1303 this->resize_x = 1; // smallest common child resize step.
1304 this->resize_y = 0; // smallest non-zero child widget resize step.
1305
1306 /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1307 uint highest = 0; // Highest child found.
1308 uint max_hor_fill = 0; // Biggest horizontal fill step.
1309 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1310 child_wid->SetupSmallestSize(w, init_array);
1311 highest = max(highest, child_wid->smallest_y);
1312 max_hor_fill = max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
1313 this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
1314 }
1315 /* 1b. Make the container wider if needed to accommodate all children nicely. */
1316 uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height.
1317 uint cur_width = this->smallest_x;
1318 for (;;) {
1319 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1320 uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
1321 uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
1322 if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting children are not interesting.
1323 uint remainder = (cur_width - child_width) % step_size;
1324 if (remainder > 0) { // Child did not fit entirely, widen the container.
1325 cur_width += step_size - remainder;
1326 assert(cur_width < max_smallest); // Safeguard against infinite width expansion.
1327 /* Remaining children will adapt to the new cur_width, thus speeding up the computation. */
1328 }
1329 }
1330 }
1331 if (this->smallest_x == cur_width) break;
1332 this->smallest_x = cur_width; // Smallest width got changed, try again.
1333 }
1334 /* 2. For containers that must maintain equal width, extend children minimal size. */
1335 if (this->flags & NC_EQUALSIZE) {
1336 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1337 if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
1338 }
1339 }
1340 /* 3. Move PIP space to the child, compute smallest, fill, and resize values of the container. */
1341 if (this->head != NULL) this->head->padding_top += this->pip_pre;
1342 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1343 if (child_wid->next != NULL) {
1344 child_wid->padding_bottom += this->pip_inter;
1345 } else {
1346 child_wid->padding_bottom += this->pip_post;
1347 }
1348
1349 this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
1350 if (child_wid->fill_y > 0) {
1351 if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
1352 }
1353 this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
1354
1355 if (child_wid->resize_y > 0) {
1356 if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
1357 }
1358 this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
1359 }
1360 /* We need to zero the PIP settings so we can re-initialize the tree. */
1361 this->pip_pre = this->pip_inter = this->pip_post = 0;
1362 }
1363
1364 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1365 {
1366 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1367
1368 /* Compute additional height given to us. */
1369 uint additional_length = given_height;
1370 if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
1371 /* For EQUALSIZE containers this does not sum to smallest_y during initialisation */
1372 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1373 additional_length -= child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
1374 }
1375 } else {
1376 additional_length -= this->smallest_y;
1377 }
1378
1379 this->StoreSizePosition(sizing, x, y, given_width, given_height);
1380
1381 /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the children with the biggest resize steps.
1382 * It also stores computed widths and heights into current_x and current_y values of the child.
1383 */
1384
1385 /* 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. */
1386 int num_changing_childs = 0; // Number of children that can change size.
1387 uint biggest_stepsize = 0;
1388 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1389 uint vert_step = child_wid->GetVerticalStepSize(sizing);
1390 if (vert_step > 0) {
1391 num_changing_childs++;
1392 biggest_stepsize = max(biggest_stepsize, vert_step);
1393 } else {
1394 child_wid->current_y = child_wid->smallest_y;
1395 }
1396
1397 uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
1398 child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
1399 }
1400
1401 /* Second loop: Allocate the additional vertical space over the resizing children, starting with the biggest resize steps. */
1402 while (biggest_stepsize > 0) {
1403 uint next_biggest_stepsize = 0;
1404 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1405 uint vert_step = child_wid->GetVerticalStepSize(sizing);
1406 if (vert_step > biggest_stepsize) continue; // Already done
1407 if (vert_step == biggest_stepsize) {
1408 uint increment = additional_length / num_changing_childs;
1409 num_changing_childs--;
1410 if (vert_step > 1) increment -= increment % vert_step;
1411 child_wid->current_y = child_wid->smallest_y + increment;
1412 additional_length -= increment;
1413 continue;
1414 }
1415 next_biggest_stepsize = max(next_biggest_stepsize, vert_step);
1416 }
1417 biggest_stepsize = next_biggest_stepsize;
1418 }
1419 assert(num_changing_childs == 0);
1420
1421 /* Third loop: Compute position and call the child. */
1422 uint position = 0; // Place to put next child relative to origin of the container.
1423 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1424 uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
1425 uint child_height = child_wid->current_y;
1426
1427 child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
1428 position += child_height + child_wid->padding_top + child_wid->padding_bottom;
1429 }
1430 }
1431
1437 NWidgetSpacer::NWidgetSpacer(int length, int height) : NWidgetResizeBase(NWID_SPACER, 0, 0)
1438 {
1439 this->SetMinimalSize(length, height);
1440 this->SetResize(0, 0);
1441 }
1442
1443 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
1444 {
1445 this->smallest_x = this->min_x;
1446 this->smallest_y = this->min_y;
1447 }
1448
1449 void NWidgetSpacer::FillNestedArray(NWidgetBase **array, uint length)
1450 {
1451 }
1452
1453 void NWidgetSpacer::Draw(const Window *w)
1454 {
1455 /* Spacer widget is never visible. */
1456 }
1457
1458 void NWidgetSpacer::SetDirty(const Window *w) const
1459 {
1460 /* Spacer widget never need repainting. */
1461 }
1462
1463 NWidgetCore *NWidgetSpacer::GetWidgetFromPos(int x, int y)
1464 {
1465 return NULL;
1466 }
1467
1468 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX, NC_EQUALSIZE), index(-1), clicked(-1), count(-1)
1469 {
1470 }
1471
1472 void NWidgetMatrix::SetIndex(int index)
1473 {
1474 this->index = index;
1475 }
1476
1477 void NWidgetMatrix::SetColour(Colours colour)
1478 {
1479 this->colour = colour;
1480 }
1481
1486 void NWidgetMatrix::SetClicked(int clicked)
1487 {
1488 this->clicked = clicked;
1489 if (this->clicked >= 0 && this->sb != NULL && this->widgets_x != 0) {
1490 int vpos = (this->clicked / this->widgets_x) * this->widget_h; // Vertical position of the top.
1491 /* Need to scroll down -> Scroll to the bottom.
1492 * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
1493 if (this->sb->GetPosition() < vpos) vpos += this->widget_h - this->pip_inter - 1;
1494 this->sb->ScrollTowards(vpos);
1495 }
1496 }
1497
1503 void NWidgetMatrix::SetCount(int count)
1504 {
1505 this->count = count;
1506
1507 if (this->sb == NULL || this->widgets_x == 0) return;
1508
1509 /* We need to get the number of pixels the matrix is high/wide.
1510 * So, determine the number of rows/columns based on the number of
1511 * columns/rows (one is constant/unscrollable).
1512 * Then multiply that by the height of a widget, and add the pre
1513 * and post spacing "offsets". */
1514 count = CeilDiv(count, this->sb->IsVertical() ? this->widgets_x : this->widgets_y);
1515 count *= (this->sb->IsVertical() ? this->head->smallest_y : this->head->smallest_x) + this->pip_inter;
1516 if (count > 0) count -= this->pip_inter; // We counted an inter too much in the multiplication above
1517 count += this->pip_pre + this->pip_post;
1518 this->sb->SetCount(count);
1519 this->sb->SetCapacity(this->sb->IsVertical() ? this->current_y : this->current_x);
1520 this->sb->SetStepSize(this->sb->IsVertical() ? this->widget_h : this->widget_w);
1521 }
1522
1527 void NWidgetMatrix::SetScrollbar(Scrollbar *sb)
1528 {
1529 this->sb = sb;
1530 }
1531
1532 void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array)
1533 {
1534 assert(this->head != NULL);
1535 assert(this->head->next == NULL);
1536
1537 if (this->index >= 0 && init_array) { // Fill w->nested_array[]
1538 assert(w->nested_array_size > (uint)this->index);
1539 w->nested_array[this->index] = this;
1540 }
1541
1542 /* Reset the widget number. */
1543 NWidgetCore *nw = dynamic_cast<NWidgetCore *>(this->head);
1544 assert(nw != NULL);
1545 SB(nw->index, 16, 16, 0);
1546 this->head->SetupSmallestSize(w, init_array);
1547
1548 Dimension padding = {this->pip_pre + this->pip_post, this->pip_pre + this->pip_post};
1549 Dimension size = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height};
1550 Dimension fill = {0, 0};
1551 Dimension resize = {this->pip_inter + this->head->smallest_x, this->pip_inter + this->head->smallest_y};
1552
1553 if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
1554
1555 this->smallest_x = size.width;
1556 this->smallest_y = size.height;
1557 this->fill_x = fill.width;
1558 this->fill_y = fill.height;
1559 this->resize_x = resize.width;
1560 this->resize_y = resize.height;
1561 }
1562
1563 void NWidgetMatrix::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1564 {
1565 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1566
1567 this->pos_x = x;
1568 this->pos_y = y;
1569 this->current_x = given_width;
1570 this->current_y = given_height;
1571
1572 /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
1573 this->widget_w = this->head->smallest_x + this->pip_inter;
1574 this->widget_h = this->head->smallest_y + this->pip_inter;
1575
1576 /* Account for the pip_inter is between widgets, so we need to account for that when
1577 * the division assumes pip_inter is used for all widgets. */
1578 this->widgets_x = CeilDiv(this->current_x - this->pip_pre - this->pip_post + this->pip_inter, this->widget_w);
1579 this->widgets_y = CeilDiv(this->current_y - this->pip_pre - this->pip_post + this->pip_inter, this->widget_h);
1580
1581 /* When resizing, update the scrollbar's count. E.g. with a vertical
1582 * scrollbar becoming wider or narrower means the amount of rows in
1583 * the scrollbar becomes respectively smaller or higher. */
1584 this->SetCount(this->count);
1585 }
1586
1587 void NWidgetMatrix::FillNestedArray(NWidgetBase **array, uint length)
1588 {
1589 if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
1590 NWidgetContainer::FillNestedArray(array, length);
1591 }
1592
1593 NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y)
1594 {
1595 /* Falls outside of the matrix widget. */
1596 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1597
1598 int start_x, start_y, base_offs_x, base_offs_y;
1599 this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
1600
1601 bool rtl = _current_text_dir == TD_RTL;
1602
1603 int widget_col = (rtl ?
1604 -x + (int)this->pip_post + (int)this->pos_x + base_offs_x + (int)this->widget_w - 1 - (int)this->pip_inter :
1605 x - (int)this->pip_pre - (int)this->pos_x - base_offs_x
1606 ) / this->widget_w;
1607
1608 int widget_row = (y - base_offs_y - (int)this->pip_pre - (int)this->pos_y) / this->widget_h;
1609
1610 int sub_wid = (widget_row + start_y) * this->widgets_x + start_x + widget_col;
1611 if (sub_wid >= this->count) return NULL;
1612
1613 NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
1614 assert(child != NULL);
1615 child->AssignSizePosition(ST_RESIZE,
1616 this->pos_x + (rtl ? this->pip_post - widget_col * this->widget_w : this->pip_pre + widget_col * this->widget_w) + base_offs_x,
1617 this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y,
1618 child->smallest_x, child->smallest_y, rtl);
1619
1620 SB(child->index, 16, 16, sub_wid);
1621
1622 return child->GetWidgetFromPos(x, y);
1623 }
1624
1625 /* virtual */ void NWidgetMatrix::Draw(const Window *w)
1626 {
1627 /* Fill the background. */
1628 GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1, _colour_gradient[this->colour & 0xF][5]);
1629
1630 /* Set up a clipping area for the previews. */
1631 bool rtl = _current_text_dir == TD_RTL;
1632 DrawPixelInfo tmp_dpi;
1633 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;
1634 DrawPixelInfo *old_dpi = _cur_dpi;
1635 _cur_dpi = &tmp_dpi;
1636
1637 /* Get the appropriate offsets so we can draw the right widgets. */
1638 NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
1639 assert(child != NULL);
1640 int start_x, start_y, base_offs_x, base_offs_y;
1641 this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
1642
1643 int offs_y = base_offs_y;
1644 for (int y = start_y; y < start_y + this->widgets_y + 1; y++, offs_y += this->widget_h) {
1645 /* Are we within bounds? */
1646 if (offs_y + child->smallest_y <= 0) continue;
1647 if (offs_y >= (int)this->current_y) break;
1648
1649 /* We've passed our amount of widgets. */
1650 if (y * this->widgets_x >= this->count) break;
1651
1652 int offs_x = base_offs_x;
1653 for (int x = start_x; x < start_x + this->widgets_x + 1; x++, offs_x += rtl ? -this->widget_w : this->widget_w) {
1654 /* Are we within bounds? */
1655 if (offs_x + child->smallest_x <= 0) continue;
1656 if (offs_x >= (int)this->current_x) continue;
1657
1658 /* Do we have this many widgets? */
1659 int sub_wid = y * this->widgets_x + x;
1660 if (sub_wid >= this->count) break;
1661
1662 child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl);
1663 child->SetLowered(this->clicked == sub_wid);
1664 SB(child->index, 16, 16, sub_wid);
1665 child->Draw(w);
1666 }
1667 }
1668
1669 /* Restore the clipping area. */
1670 _cur_dpi = old_dpi;
1671 }
1672
1680 void NWidgetMatrix::GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
1681 {
1682 base_offs_x = _current_text_dir == TD_RTL ? this->widget_w * (this->widgets_x - 1) : 0;
1683 base_offs_y = 0;
1684 start_x = 0;
1685 start_y = 0;
1686 if (this->sb != NULL) {
1687 if (this->sb->IsVertical()) {
1688 start_y = this->sb->GetPosition() / this->widget_h;
1689 base_offs_y += -this->sb->GetPosition() + start_y * this->widget_h;
1690 } else {
1691 start_x = this->sb->GetPosition() / this->widget_w;
1692 int sub_x = this->sb->GetPosition() - start_x * this->widget_w;
1693 if (_current_text_dir == TD_RTL) {
1694 base_offs_x += sub_x;
1695 } else {
1696 base_offs_x -= sub_x;
1697 }
1698 }
1699 }
1700 }
1701
1711 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
1712 {
1713 assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
1714 if (index >= 0) this->SetIndex(index);
1715 this->child = child;
1716 }
1717
1718 NWidgetBackground::~NWidgetBackground()
1719 {
1720 if (this->child != NULL) delete this->child;
1721 }
1722
1730 void NWidgetBackground::Add(NWidgetBase *nwid)
1731 {
1732 if (this->child == NULL) {
1733 this->child = new NWidgetVertical();
1734 }
1735 this->child->Add(nwid);
1736 }
1737
1748 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
1749 {
1750 if (this->child == NULL) {
1751 this->child = new NWidgetVertical();
1752 }
1753 this->child->SetPIP(pip_pre, pip_inter, pip_post);
1754 }
1755
1756 void NWidgetBackground::SetupSmallestSize(Window *w, bool init_array)
1757 {
1758 if (init_array && this->index >= 0) {
1759 assert(w->nested_array_size > (uint)this->index);
1760 w->nested_array[this->index] = this;
1761 }
1762 if (this->child != NULL) {
1763 this->child->SetupSmallestSize(w, init_array);
1764
1765 this->smallest_x = this->child->smallest_x;
1766 this->smallest_y = this->child->smallest_y;
1767 this->fill_x = this->child->fill_x;
1768 this->fill_y = this->child->fill_y;
1769 this->resize_x = this->child->resize_x;
1770 this->resize_y = this->child->resize_y;
1771
1772 /* Account for the size of the frame's text if that exists */
1773 if (w != NULL && this->type == WWT_FRAME) {
1774 this->child->padding_left = WD_FRAMETEXT_LEFT;
1775 this->child->padding_right = WD_FRAMETEXT_RIGHT;
1776 this->child->padding_top = max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
1777 this->child->padding_bottom = WD_FRAMETEXT_BOTTOM;
1778
1779 this->smallest_x += this->child->padding_left + this->child->padding_right;
1780 this->smallest_y += this->child->padding_top + this->child->padding_bottom;
1781
1782 if (this->index >= 0) w->SetStringParameters(this->index);
1783 this->smallest_x = max(this->smallest_x, GetStringBoundingBox(this->widget_data).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
1784 }
1785 } else {
1786 Dimension d = {this->min_x, this->min_y};
1787 Dimension fill = {this->fill_x, this->fill_y};
1788 Dimension resize = {this->resize_x, this->resize_y};
1789 if (w != NULL) { // A non-NULL window pointer acts as switch to turn dynamic widget size on.
1790 if (this->type == WWT_FRAME || this->type == WWT_INSET) {
1791 if (this->index >= 0) w->SetStringParameters(this->index);
1792 Dimension background = GetStringBoundingBox(this->widget_data);
1793 background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
1794 d = maxdim(d, background);
1795 }
1796 if (this->index >= 0) {
1797 static const Dimension padding = {0, 0};
1798 w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
1799 }
1800 }
1801 this->smallest_x = d.width;
1802 this->smallest_y = d.height;
1803 this->fill_x = fill.width;
1804 this->fill_y = fill.height;
1805 this->resize_x = resize.width;
1806 this->resize_y = resize.height;
1807 }
1808 }
1809
1810 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1811 {
1812 this->StoreSizePosition(sizing, x, y, given_width, given_height);
1813
1814 if (this->child != NULL) {
1815 uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
1816 uint width = given_width - this->child->padding_right - this->child->padding_left;
1817 uint height = given_height - this->child->padding_top - this->child->padding_bottom;
1818 this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
1819 }
1820 }
1821
1822 void NWidgetBackground::FillNestedArray(NWidgetBase **array, uint length)
1823 {
1824 if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
1825 if (this->child != NULL) this->child->FillNestedArray(array, length);
1826 }
1827
1828 void NWidgetBackground::Draw(const Window *w)
1829 {
1830 if (this->current_x == 0 || this->current_y == 0) return;
1831
1832 Rect r;
1833 r.left = this->pos_x;
1834 r.right = this->pos_x + this->current_x - 1;
1835 r.top = this->pos_y;
1836 r.bottom = this->pos_y + this->current_y - 1;
1837
1838 const DrawPixelInfo *dpi = _cur_dpi;
1839 if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
1840
1841 switch (this->type) {
1842 case WWT_PANEL:
1843 assert(this->widget_data == 0);
1844 DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
1845 break;
1846
1847 case WWT_FRAME:
1848 if (this->index >= 0) w->SetStringParameters(this->index);
1849 DrawFrame(r, this->colour, this->widget_data);
1850 break;
1851
1852 case WWT_INSET:
1853 if (this->index >= 0) w->SetStringParameters(this->index);
1854 DrawInset(r, this->colour, this->widget_data);
1855 break;
1856
1857 default:
1858 NOT_REACHED();
1859 }
1860
1861 if (this->index >= 0) w->DrawWidget(r, this->index);
1862 if (this->child != NULL) this->child->Draw(w);
1863
1864 if (this->IsDisabled()) {
1865 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
1866 }
1867 }
1868
1869 NWidgetCore *NWidgetBackground::GetWidgetFromPos(int x, int y)
1870 {
1871 NWidgetCore *nwid = NULL;
1872 if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
1873 if (this->child != NULL) nwid = this->child->GetWidgetFromPos(x, y);
1874 if (nwid == NULL) nwid = this;
1875 }
1876 return nwid;
1877 }
1878
1879 NWidgetBase *NWidgetBackground::GetWidgetOfType(WidgetType tp)
1880 {
1881 NWidgetBase *nwid = NULL;
1882 if (this->child != NULL) nwid = this->child->GetWidgetOfType(tp);
1883 if (nwid == NULL && this->type == tp) nwid = this;
1884 return nwid;
1885 }
1886
1887 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
1888 {
1889 this->SetIndex(index);
1890 }
1891
1892 void NWidgetViewport::SetupSmallestSize(Window *w, bool init_array)
1893 {
1894 if (init_array && this->index >= 0) {
1895 assert(w->nested_array_size > (uint)this->index);
1896 w->nested_array[this->index] = this;
1897 }
1898 this->smallest_x = this->min_x;
1899 this->smallest_y = this->min_y;
1900 }
1901
1902 void NWidgetViewport::Draw(const Window *w)
1903 {
1904 if (this->disp_flags & ND_NO_TRANSPARENCY) {
1905 TransparencyOptionBits to_backup = _transparency_opt;
1906 _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_LOADING); // Disable all transparency, except textual stuff
1907 w->DrawViewport();
1908 _transparency_opt = to_backup;
1909 } else {
1910 w->DrawViewport();
1911 }
1912
1913 /* Optionally shade the viewport. */
1914 if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
1915 GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
1916 (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
1917 }
1918 }
1919
1926 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
1927 {
1928 InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
1929 }
1930
1935 void NWidgetViewport::UpdateViewportCoordinates(Window *w)
1936 {
1937 ViewPort *vp = w->viewport;
1938 if (vp != NULL) {
1939 vp->left = w->left + this->pos_x;
1940 vp->top = w->top + this->pos_y;
1941 vp->width = this->current_x;
1942 vp->height = this->current_y;
1943
1944 vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
1945 vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
1946 }
1947 }
1948
1958 int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
1959 {
1960 uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
1961 if (pos != INT_MAX) pos += this->GetPosition();
1962 return (pos >= this->GetCount()) ? INT_MAX : pos;
1963 }
1964
1972 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
1973 {
1974 NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
1975 if (this->IsVertical()) {
1976 this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
1977 } else {
1978 this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
1979 }
1980 }
1981
1988 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL), Scrollbar(tp != NWID_HSCROLLBAR)
1989 {
1990 assert(tp == NWID_HSCROLLBAR || tp == NWID_VSCROLLBAR);
1991 this->SetIndex(index);
1992 }
1993
1994 void NWidgetScrollbar::SetupSmallestSize(Window *w, bool init_array)
1995 {
1996 if (init_array && this->index >= 0) {
1997 assert(w->nested_array_size > (uint)this->index);
1998 w->nested_array[this->index] = this;
1999 }
2000 this->min_x = 0;
2001 this->min_y = 0;
2002
2003 switch (this->type) {
2004 case NWID_HSCROLLBAR:
2005 this->SetMinimalSize(NWidgetScrollbar::GetHorizontalDimension().width * 3, NWidgetScrollbar::GetHorizontalDimension().height);
2006 this->SetResize(1, 0);
2007 this->SetFill(1, 0);
2008 this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
2009 break;
2010
2011 case NWID_VSCROLLBAR:
2012 this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width, NWidgetScrollbar::GetVerticalDimension().height * 3);
2013 this->SetResize(0, 1);
2014 this->SetFill(0, 1);
2015 this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
2016 break;
2017
2018 default: NOT_REACHED();
2019 }
2020
2021 this->smallest_x = this->min_x;
2022 this->smallest_y = this->min_y;
2023 }
2024
2025 void NWidgetScrollbar::Draw(const Window *w)
2026 {
2027 if (this->current_x == 0 || this->current_y == 0) return;
2028
2029 Rect r;
2030 r.left = this->pos_x;
2031 r.right = this->pos_x + this->current_x - 1;
2032 r.top = this->pos_y;
2033 r.bottom = this->pos_y + this->current_y - 1;
2034
2035 const DrawPixelInfo *dpi = _cur_dpi;
2036 if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
2037
2038 bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
2039 bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
2040 bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->scrolling_scrollbar == this->index;
2041
2042 if (this->type == NWID_HSCROLLBAR) {
2043 DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
2044 } else {
2045 DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
2046 }
2047
2048 if (this->IsDisabled()) {
2049 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
2050 }
2051 }
2052
2053 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
2054 {
2055 vertical_dimension.width = vertical_dimension.height = 0;
2056 horizontal_dimension.width = horizontal_dimension.height = 0;
2057 }
2058
2059 /* static */ Dimension NWidgetScrollbar::GetVerticalDimension()
2060 {
2061 static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
2062 if (vertical_dimension.width == 0) {
2063 vertical_dimension = maxdim(GetSpriteSize(SPR_ARROW_UP), GetSpriteSize(SPR_ARROW_DOWN));
2064 vertical_dimension.width += extra.width;
2065 vertical_dimension.height += extra.height;
2066 }
2067 return vertical_dimension;
2068 }
2069
2070 /* static */ Dimension NWidgetScrollbar::GetHorizontalDimension()
2071 {
2072 static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
2073 if (horizontal_dimension.width == 0) {
2074 horizontal_dimension = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
2075 horizontal_dimension.width += extra.width;
2076 horizontal_dimension.height += extra.height;
2077 }
2078 return horizontal_dimension;
2079 }
2080
2081 Dimension NWidgetScrollbar::vertical_dimension = {0, 0};
2082 Dimension NWidgetScrollbar::horizontal_dimension = {0, 0};
2083
2085 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
2086 {
2087 shadebox_dimension.width = shadebox_dimension.height = 0;
2088 debugbox_dimension.width = debugbox_dimension.height = 0;
2089 defsizebox_dimension.width = defsizebox_dimension.height = 0;
2090 stickybox_dimension.width = stickybox_dimension.height = 0;
2091 resizebox_dimension.width = resizebox_dimension.height = 0;
2092 closebox_dimension.width = closebox_dimension.height = 0;
2093 dropdown_dimension.width = dropdown_dimension.height = 0;
2094 }
2095
2096 Dimension NWidgetLeaf::shadebox_dimension = {0, 0};
2097 Dimension NWidgetLeaf::debugbox_dimension = {0, 0};
2098 Dimension NWidgetLeaf::defsizebox_dimension = {0, 0};
2099 Dimension NWidgetLeaf::stickybox_dimension = {0, 0};
2100 Dimension NWidgetLeaf::resizebox_dimension = {0, 0};
2101 Dimension NWidgetLeaf::closebox_dimension = {0, 0};
2102 Dimension NWidgetLeaf::dropdown_dimension = {0, 0};
2103
2112 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
2113 {
2114 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);
2115 if (index >= 0) this->SetIndex(index);
2116 this->min_x = 0;
2117 this->min_y = 0;
2118 this->SetResize(0, 0);
2119
2120 switch (tp) {
2121 case WWT_EMPTY:
2122 break;
2123
2124 case WWT_PUSHBTN:
2125 case WWT_IMGBTN:
2126 case WWT_PUSHIMGBTN:
2127 case WWT_IMGBTN_2:
2128 case WWT_TEXTBTN:
2129 case WWT_PUSHTXTBTN:
2130 case WWT_TEXTBTN_2:
2131 case WWT_LABEL:
2132 case WWT_TEXT:
2133 case WWT_MATRIX:
2134 case NWID_BUTTON_DROPDOWN:
2135 case NWID_PUSHBUTTON_DROPDOWN:
2136 case WWT_ARROWBTN:
2137 case WWT_PUSHARROWBTN:
2138 this->SetFill(0, 0);
2139 break;
2140
2141 case WWT_EDITBOX:
2142 this->SetFill(0, 0);
2143 break;
2144
2145 case WWT_CAPTION:
2146 this->SetFill(1, 0);
2147 this->SetResize(1, 0);
2148 this->min_y = WD_CAPTION_HEIGHT;
2149 this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
2150 break;
2151
2152 case WWT_STICKYBOX:
2153 this->SetFill(0, 0);
2154 this->SetMinimalSize(WD_STICKYBOX_WIDTH, WD_CAPTION_HEIGHT);
2155 this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
2156 break;
2157
2158 case WWT_SHADEBOX:
2159 this->SetFill(0, 0);
2160 this->SetMinimalSize(WD_SHADEBOX_TOP, WD_CAPTION_HEIGHT);
2161 this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
2162 break;
2163
2164 case WWT_DEBUGBOX:
2165 this->SetFill(0, 0);
2166 this->SetMinimalSize(WD_DEBUGBOX_TOP, WD_CAPTION_HEIGHT);
2167 this->SetDataTip(STR_NULL, STR_TOOLTIP_DEBUG);
2168 break;
2169
2170 case WWT_DEFSIZEBOX:
2171 this->SetFill(0, 0);
2172 this->SetMinimalSize(WD_DEFSIZEBOX_TOP, WD_CAPTION_HEIGHT);
2173 this->SetDataTip(STR_NULL, STR_TOOLTIP_DEFSIZE);
2174 break;
2175
2176 case WWT_RESIZEBOX:
2177 this->SetFill(0, 0);
2178 this->SetMinimalSize(WD_RESIZEBOX_WIDTH, 12);
2179 this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
2180 break;
2181
2182 case WWT_CLOSEBOX:
2183 this->SetFill(0, 0);
2184 this->SetMinimalSize(WD_CLOSEBOX_WIDTH, WD_CAPTION_HEIGHT);
2185 this->SetDataTip(STR_NULL, STR_TOOLTIP_CLOSE_WINDOW);
2186 break;
2187
2188 case WWT_DROPDOWN:
2189 this->SetFill(0, 0);
2190 this->min_y = WD_DROPDOWN_HEIGHT;
2191 break;
2192
2193 default:
2194 NOT_REACHED();
2195 }
2196 }
2197
2198 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
2199 {
2200 if (this->index >= 0 && init_array) { // Fill w->nested_array[]
2201 assert(w->nested_array_size > (uint)this->index);
2202 w->nested_array[this->index] = this;
2203 }
2204
2205 Dimension size = {this->min_x, this->min_y};
2206 Dimension fill = {this->fill_x, this->fill_y};
2207 Dimension resize = {this->resize_x, this->resize_y};
2208 /* Get padding, and update size with the real content size if appropriate. */
2209 const Dimension *padding = NULL;
2210 switch (this->type) {
2211 case WWT_EMPTY: {
2212 static const Dimension extra = {0, 0};
2213 padding = &extra;
2214 break;
2215 }
2216 case WWT_MATRIX: {
2217 static const Dimension extra = {WD_MATRIX_LEFT + WD_MATRIX_RIGHT, WD_MATRIX_TOP + WD_MATRIX_BOTTOM};
2218 padding = &extra;
2219 break;
2220 }
2221 case WWT_SHADEBOX: {
2222 static const Dimension extra = {WD_SHADEBOX_LEFT + WD_SHADEBOX_RIGHT, WD_SHADEBOX_TOP + WD_SHADEBOX_BOTTOM};
2223 padding = &extra;
2224 if (NWidgetLeaf::shadebox_dimension.width == 0) {
2225 NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
2226 NWidgetLeaf::shadebox_dimension.width += extra.width;
2227 NWidgetLeaf::shadebox_dimension.height += extra.height;
2228 }
2229 size = maxdim(size, NWidgetLeaf::shadebox_dimension);
2230 break;
2231 }
2232 case WWT_DEBUGBOX:
2233 if (_settings_client.gui.newgrf_developer_tools && w->IsNewGRFInspectable()) {
2234 static const Dimension extra = {WD_DEBUGBOX_LEFT + WD_DEBUGBOX_RIGHT, WD_DEBUGBOX_TOP + WD_DEBUGBOX_BOTTOM};
2235 padding = &extra;
2236 if (NWidgetLeaf::debugbox_dimension.width == 0) {
2237 NWidgetLeaf::debugbox_dimension = GetSpriteSize(SPR_WINDOW_DEBUG);
2238 NWidgetLeaf::debugbox_dimension.width += extra.width;
2239 NWidgetLeaf::debugbox_dimension.height += extra.height;
2240 }
2241 size = maxdim(size, NWidgetLeaf::debugbox_dimension);
2242 } else {
2243 /* If the setting is disabled we don't want to see it! */
2244 size.width = 0;
2245 fill.width = 0;
2246 resize.width = 0;
2247 }
2248 break;
2249
2250 case WWT_STICKYBOX: {
2251 static const Dimension extra = {WD_STICKYBOX_LEFT + WD_STICKYBOX_RIGHT, WD_STICKYBOX_TOP + WD_STICKYBOX_BOTTOM};
2252 padding = &extra;
2253 if (NWidgetLeaf::stickybox_dimension.width == 0) {
2254 NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
2255 NWidgetLeaf::stickybox_dimension.width += extra.width;
2256 NWidgetLeaf::stickybox_dimension.height += extra.height;
2257 }
2258 size = maxdim(size, NWidgetLeaf::stickybox_dimension);
2259 break;
2260 }
2261
2262 case WWT_DEFSIZEBOX: {
2263 static const Dimension extra = {WD_DEFSIZEBOX_LEFT + WD_DEFSIZEBOX_RIGHT, WD_DEFSIZEBOX_TOP + WD_DEFSIZEBOX_BOTTOM};
2264 padding = &extra;
2265 if (NWidgetLeaf::defsizebox_dimension.width == 0) {
2266 NWidgetLeaf::defsizebox_dimension = GetSpriteSize(SPR_WINDOW_DEFSIZE);
2267 NWidgetLeaf::defsizebox_dimension.width += extra.width;
2268 NWidgetLeaf::defsizebox_dimension.height += extra.height;
2269 }
2270 size = maxdim(size, NWidgetLeaf::defsizebox_dimension);
2271 break;
2272 }
2273
2274 case WWT_RESIZEBOX: {
2275 static const Dimension extra = {WD_RESIZEBOX_LEFT + WD_RESIZEBOX_RIGHT, WD_RESIZEBOX_TOP + WD_RESIZEBOX_BOTTOM};
2276 padding = &extra;
2277 if (NWidgetLeaf::resizebox_dimension.width == 0) {
2278 NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
2279 NWidgetLeaf::resizebox_dimension.width += extra.width;
2280 NWidgetLeaf::resizebox_dimension.height += extra.height;
2281 }
2282 size = maxdim(size, NWidgetLeaf::resizebox_dimension);
2283 break;
2284 }
2285 case WWT_EDITBOX: {
2286 Dimension sprite_size = GetSpriteSize(_current_text_dir == TD_RTL ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
2287 size.width = 30 + sprite_size.width;
2288 size.height = max(sprite_size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
2289 /* FALL THROUGH */
2290 }
2291 case WWT_PUSHBTN: {
2292 static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
2293 padding = &extra;
2294 break;
2295 }
2296 case WWT_IMGBTN:
2297 case WWT_IMGBTN_2:
2298 case WWT_PUSHIMGBTN: {
2299 static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
2300 padding = &extra;
2301 Dimension d2 = GetSpriteSize(this->widget_data);
2302 if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
2303 d2.width += extra.width;
2304 d2.height += extra.height;
2305 size = maxdim(size, d2);
2306 break;
2307 }
2308 case WWT_ARROWBTN:
2309 case WWT_PUSHARROWBTN: {
2310 static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
2311 padding = &extra;
2312 Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
2313 d2.width += extra.width;
2314 d2.height += extra.height;
2315 size = maxdim(size, d2);
2316 break;
2317 }
2318
2319 case WWT_CLOSEBOX: {
2320 static const Dimension extra = {WD_CLOSEBOX_LEFT + WD_CLOSEBOX_RIGHT, WD_CLOSEBOX_TOP + WD_CLOSEBOX_BOTTOM};
2321 padding = &extra;
2322 if (NWidgetLeaf::closebox_dimension.width == 0) {
2323 NWidgetLeaf::closebox_dimension = GetSpriteSize(SPR_CLOSEBOX);
2324 NWidgetLeaf::closebox_dimension.width += extra.width;
2325 NWidgetLeaf::closebox_dimension.height += extra.height;
2326 }
2327 size = maxdim(size, NWidgetLeaf::closebox_dimension);
2328 break;
2329 }
2330 case WWT_TEXTBTN:
2331 case WWT_PUSHTXTBTN:
2332 case WWT_TEXTBTN_2: {
2333 static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
2334 padding = &extra;
2335 if (this->index >= 0) w->SetStringParameters(this->index);
2336 Dimension d2 = GetStringBoundingBox(this->widget_data);
2337 d2.width += extra.width;
2338 d2.height += extra.height;
2339 size = maxdim(size, d2);
2340 break;
2341 }
2342 case WWT_LABEL:
2343 case WWT_TEXT: {
2344 static const Dimension extra = {0, 0};
2345 padding = &extra;
2346 if (this->index >= 0) w->SetStringParameters(this->index);
2347 size = maxdim(size, GetStringBoundingBox(this->widget_data));
2348 break;
2349 }
2350 case WWT_CAPTION: {
2351 static const Dimension extra = {WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT, WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM};
2352 padding = &extra;
2353 if (this->index >= 0) w->SetStringParameters(this->index);
2354 Dimension d2 = GetStringBoundingBox(this->widget_data);
2355 d2.width += extra.width;
2356 d2.height += extra.height;
2357 size = maxdim(size, d2);
2358 break;
2359 }
2360 case WWT_DROPDOWN:
2361 case NWID_BUTTON_DROPDOWN:
2362 case NWID_PUSHBUTTON_DROPDOWN: {
2363 static Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
2364 padding = &extra;
2365 if (NWidgetLeaf::dropdown_dimension.width == 0) {
2366 NWidgetLeaf::dropdown_dimension = GetSpriteSize(SPR_ARROW_DOWN);
2367 NWidgetLeaf::dropdown_dimension.width += WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT;
2368 NWidgetLeaf::dropdown_dimension.height += WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM;
2369 extra.width = WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT + NWidgetLeaf::dropdown_dimension.width;
2370 }
2371 if (this->index >= 0) w->SetStringParameters(this->index);
2372 Dimension d2 = GetStringBoundingBox(this->widget_data);
2373 d2.width += extra.width;
2374 d2.height = max(d2.height, NWidgetLeaf::dropdown_dimension.height) + extra.height;
2375 size = maxdim(size, d2);
2376 break;
2377 }
2378 default:
2379 NOT_REACHED();
2380 }
2381
2382 if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
2383
2384 this->smallest_x = size.width;
2385 this->smallest_y = size.height;
2386 this->fill_x = fill.width;
2387 this->fill_y = fill.height;
2388 this->resize_x = resize.width;
2389 this->resize_y = resize.height;
2390 }
2391
2392 void NWidgetLeaf::Draw(const Window *w)
2393 {
2394 if (this->current_x == 0 || this->current_y == 0) return;
2395
2396 Rect r;
2397 r.left = this->pos_x;
2398 r.right = this->pos_x + this->current_x - 1;
2399 r.top = this->pos_y;
2400 r.bottom = this->pos_y + this->current_y - 1;
2401
2402 const DrawPixelInfo *dpi = _cur_dpi;
2403 if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
2404
2405 bool clicked = this->IsLowered();
2406 switch (this->type) {
2407 case WWT_EMPTY:
2408 break;
2409
2410 case WWT_PUSHBTN:
2411 assert(this->widget_data == 0);
2412 DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
2413 break;
2414
2415 case WWT_IMGBTN:
2416 case WWT_PUSHIMGBTN:
2417 case WWT_IMGBTN_2:
2418 DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
2419 break;
2420
2421 case WWT_TEXTBTN:
2422 case WWT_PUSHTXTBTN:
2423 case WWT_TEXTBTN_2:
2424 if (this->index >= 0) w->SetStringParameters(this->index);
2425 DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
2426 DrawLabel(r, this->type, clicked, this->widget_data);
2427 break;
2428
2429 case WWT_ARROWBTN:
2430 case WWT_PUSHARROWBTN: {
2431 SpriteID sprite;
2432 switch (this->widget_data) {
2433 case AWV_DECREASE: sprite = _current_text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
2434 case AWV_INCREASE: sprite = _current_text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
2435 case AWV_LEFT: sprite = SPR_ARROW_LEFT; break;
2436 case AWV_RIGHT: sprite = SPR_ARROW_RIGHT; break;
2437 default: NOT_REACHED();
2438 }
2439 DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
2440 break;
2441 }
2442
2443 case WWT_LABEL:
2444 if (this->index >= 0) w->SetStringParameters(this->index);
2445 DrawLabel(r, this->type, clicked, this->widget_data);
2446 break;
2447
2448 case WWT_TEXT:
2449 if (this->index >= 0) w->SetStringParameters(this->index);
2450 DrawText(r, (TextColour)this->colour, this->widget_data);
2451 break;
2452
2453 case WWT_MATRIX:
2454 DrawMatrix(r, this->colour, clicked, this->widget_data, this->resize_x, this->resize_y);
2455 break;
2456
2457 case WWT_EDITBOX: {
2458 const QueryString *query = w->GetQueryString(this->index);
2459 if (query != NULL) query->DrawEditBox(w, this->index);
2460 break;
2461 }
2462
2463 case WWT_CAPTION:
2464 if (this->index >= 0) w->SetStringParameters(this->index);
2465 DrawCaption(r, this->colour, w->owner, this->widget_data);
2466 break;
2467
2468 case WWT_SHADEBOX:
2469 assert(this->widget_data == 0);
2470 DrawShadeBox(r, this->colour, w->IsShaded());
2471 break;
2472
2473 case WWT_DEBUGBOX:
2474 DrawDebugBox(r, this->colour, clicked);
2475 break;
2476
2477 case WWT_STICKYBOX:
2478 assert(this->widget_data == 0);
2479 DrawStickyBox(r, this->colour, !!(w->flags & WF_STICKY));
2480 break;
2481
2482 case WWT_DEFSIZEBOX:
2483 assert(this->widget_data == 0);
2484 DrawDefSizeBox(r, this->colour, clicked);
2485 break;
2486
2487 case WWT_RESIZEBOX:
2488 assert(this->widget_data == 0);
2489 DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags & WF_SIZING));
2490 break;
2491
2492 case WWT_CLOSEBOX:
2493 DrawCloseBox(r, this->colour);
2494 break;
2495
2496 case WWT_DROPDOWN:
2497 if (this->index >= 0) w->SetStringParameters(this->index);
2498 DrawDropdown(r, this->colour, clicked, this->widget_data);
2499 break;
2500
2501 case NWID_BUTTON_DROPDOWN:
2502 case NWID_PUSHBUTTON_DROPDOWN:
2503 if (this->index >= 0) w->SetStringParameters(this->index);
2504 DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
2505 break;
2506
2507 default:
2508 NOT_REACHED();
2509 }
2510 if (this->index >= 0) w->DrawWidget(r, this->index);
2511
2512 if (this->IsDisabled()) {
2513 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
2514 }
2515 }
2516
2524 bool NWidgetLeaf::ButtonHit(const Point &pt)
2525 {
2526 if (_current_text_dir == TD_LTR) {
2527 int button_width = this->pos_x + this->current_x - NWidgetLeaf::dropdown_dimension.width;
2528 return pt.x < button_width;
2529 } else {
2530 int button_left = this->pos_x + NWidgetLeaf::dropdown_dimension.width;
2531 return pt.x >= button_left;
2532 }
2533 }
2534
2535 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
2536
2552 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
2553 {
2554 int num_used = 0;
2555
2556 *dest = NULL;
2557 *fill_dest = false;
2558
2559 while (count > num_used) {
2560 switch (parts->type) {
2561 case NWID_SPACER:
2562 if (*dest != NULL) return num_used;
2563 *dest = new NWidgetSpacer(0, 0);
2564 break;
2565
2566 case NWID_HORIZONTAL:
2567 if (*dest != NULL) return num_used;
2568 *dest = new NWidgetHorizontal(parts->u.cont_flags);
2569 *fill_dest = true;
2570 break;
2571
2572 case NWID_HORIZONTAL_LTR:
2573 if (*dest != NULL) return num_used;
2574 *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
2575 *fill_dest = true;
2576 break;
2577
2578 case WWT_PANEL:
2579 case WWT_INSET:
2580 case WWT_FRAME:
2581 if (*dest != NULL) return num_used;
2582 *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
2583 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2584 *fill_dest = true;
2585 break;
2586
2587 case NWID_VERTICAL:
2588 if (*dest != NULL) return num_used;
2589 *dest = new NWidgetVertical(parts->u.cont_flags);
2590 *fill_dest = true;
2591 break;
2592
2593 case NWID_MATRIX: {
2594 if (*dest != NULL) return num_used;
2595 NWidgetMatrix *nwm = new NWidgetMatrix();
2596 *dest = nwm;
2597 *fill_dest = true;
2598 nwm->SetIndex(parts->u.widget.index);
2599 nwm->SetColour(parts->u.widget.colour);
2600 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2601 break;
2602 }
2603
2604 case WPT_FUNCTION: {
2605 if (*dest != NULL) return num_used;
2606 /* Ensure proper functioning even when the called code simply writes its largest index. */
2607 int biggest = -1;
2608 *dest = parts->u.func_ptr(&biggest);
2609 *biggest_index = max(*biggest_index, biggest);
2610 *fill_dest = false;
2611 break;
2612 }
2613
2614 case WPT_RESIZE: {
2615 NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2616 if (nwrb != NULL) {
2617 assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
2618 nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
2619 }
2620 break;
2621 }
2622
2623 case WPT_MINSIZE: {
2624 NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2625 if (nwrb != NULL) {
2626 assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
2627 nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
2628 }
2629 break;
2630 }
2631
2632 case WPT_MINTEXTLINES: {
2633 NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2634 if (nwrb != NULL) {
2635 assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
2636 nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
2637 }
2638 break;
2639 }
2640
2641 case WPT_FILL: {
2642 NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2643 if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
2644 break;
2645 }
2646
2647 case WPT_DATATIP: {
2648 NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
2649 if (nwc != NULL) {
2650 nwc->widget_data = parts->u.data_tip.data;
2651 nwc->tool_tip = parts->u.data_tip.tooltip;
2652 }
2653 break;
2654 }
2655
2656 case WPT_PADDING:
2657 if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
2658 break;
2659
2660 case WPT_PIPSPACE: {
2661 NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
2662 if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre, parts->u.pip.inter, parts->u.pip.post);
2663
2664 NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
2665 if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre, parts->u.pip.inter, parts->u.pip.post);
2666 break;
2667 }
2668
2669 case WPT_SCROLLBAR: {
2670 NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
2671 if (nwc != NULL) {
2672 nwc->scrollbar_index = parts->u.widget.index;
2673 }
2674 break;
2675 }
2676
2677 case WPT_ENDCONTAINER:
2678 return num_used;
2679
2680 case NWID_VIEWPORT:
2681 if (*dest != NULL) return num_used;
2682 *dest = new NWidgetViewport(parts->u.widget.index);
2683 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2684 break;
2685
2686 case NWID_HSCROLLBAR:
2687 case NWID_VSCROLLBAR:
2688 if (*dest != NULL) return num_used;
2689 *dest = new NWidgetScrollbar(parts->type, parts->u.widget.colour, parts->u.widget.index);
2690 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2691 break;
2692
2693 case NWID_SELECTION: {
2694 if (*dest != NULL) return num_used;
2695 NWidgetStacked *nws = new NWidgetStacked();
2696 *dest = nws;
2697 *fill_dest = true;
2698 nws->SetIndex(parts->u.widget.index);
2699 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2700 break;
2701 }
2702
2703 default:
2704 if (*dest != NULL) return num_used;
2705 assert((parts->type & WWT_MASK) < WWT_LAST || (parts->type & WWT_MASK) == NWID_BUTTON_DROPDOWN);
2706 *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
2707 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2708 break;
2709 }
2710 num_used++;
2711 parts++;
2712 }
2713
2714 return num_used;
2715 }
2716
2726 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
2727 {
2728 /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
2729 * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
2730 NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
2731 NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
2732 assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
2733
2734 int total_used = 0;
2735 for (;;) {
2736 NWidgetBase *sub_widget = NULL;
2737 bool fill_sub = false;
2738 int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
2739 parts += num_used;
2740 total_used += num_used;
2741
2742 /* Break out of loop when end reached */
2743 if (sub_widget == NULL) break;
2744
2745 /* If sub-widget is a container, recursively fill that container. */
2746 WidgetType tp = sub_widget->type;
2747 if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
2748 || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
2749 NWidgetBase *sub_ptr = sub_widget;
2750 int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
2751 parts += num_used;
2752 total_used += num_used;
2753 }
2754
2755 /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
2756 if (nwid_cont != NULL) nwid_cont->Add(sub_widget);
2757 if (nwid_parent != NULL) nwid_parent->Add(sub_widget);
2758 if (nwid_cont == NULL && nwid_parent == NULL) {
2759 *parent = sub_widget;
2760 return total_used;
2761 }
2762 }
2763
2764 if (count == total_used) return total_used; // Reached the end of the array of parts?
2765
2766 assert(total_used < count);
2767 assert(parts->type == WPT_ENDCONTAINER);
2768 return total_used + 1; // *parts is also 'used'
2769 }
2770
2782 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
2783 {
2784 *biggest_index = -1;
2785 if (container == NULL) container = new NWidgetVertical();
2786 NWidgetBase *cont_ptr = container;
2787 MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
2788 return container;
2789 }
2790
2804 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
2805 {
2806 *biggest_index = -1;
2807
2808 /* Read the first widget recursively from the array. */
2809 NWidgetBase *nwid = NULL;
2810 int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
2811 assert(nwid != NULL);
2812 parts += num_used;
2813 count -= num_used;
2814
2815 NWidgetContainer *root = new NWidgetVertical;
2816 root->Add(nwid);
2817 if (count == 0) { // There is no body at all.
2818 *shade_select = NULL;
2819 return root;
2820 }
2821
2822 /* If the first widget looks like a titlebar, treat it as such.
2823 * If it has a shading box, silently add a shade selection widget in the tree. */
2824 NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
2825 NWidgetContainer *body;
2826 if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
2827 *shade_select = new NWidgetStacked;
2828 root->Add(*shade_select);
2829 body = new NWidgetVertical;
2830 (*shade_select)->Add(body);
2831 } else {
2832 *shade_select = NULL;
2833 body = root;
2834 }
2835
2836 /* Load the remaining parts into 'body'. */
2837 int biggest2 = -1;
2838 MakeNWidgets(parts, count, &biggest2, body);
2839
2840 *biggest_index = max(*biggest_index, biggest2);
2841 return root;
2842 }
2843
2854 NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
2855 {
2856 assert(max_length >= 1);
2857 NWidgetVertical *vert = NULL; // Storage for all rows.
2858 NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
2859 int hor_length = 0;
2860
2861 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
2862 sprite_size.width += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
2863 sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
2864
2865 for (int widnum = widget_first; widnum <= widget_last; widnum++) {
2866 /* Ensure there is room in 'hor' for another button. */
2867 if (hor_length == max_length) {
2868 if (vert == NULL) vert = new NWidgetVertical();
2869 vert->Add(hor);
2870 hor = NULL;
2871 hor_length = 0;
2872 }
2873 if (hor == NULL) {
2874 hor = new NWidgetHorizontal();
2875 hor_length = 0;
2876 }
2877
2878 NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
2879 panel->SetMinimalSize(sprite_size.width, sprite_size.height);
2880 panel->SetFill(1, 1);
2881 panel->SetResize(1, 0);
2882 panel->SetDataTip(0x0, button_tooltip);
2883 hor->Add(panel);
2884 hor_length++;
2885 }
2886 *biggest_index = widget_last;
2887 if (vert == NULL) return hor; // All buttons fit in a single row.
2888
2889 if (hor_length > 0 && hor_length < max_length) {
2890 /* Last row is partial, add a spacer at the end to force all buttons to the left. */
2891 NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
2892 spc->SetFill(1, 1);
2893 spc->SetResize(1, 0);
2894 hor->Add(spc);
2895 }
2896 if (hor != NULL) vert->Add(hor);
2897 return vert;
2898 }