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