OpenTTD
widget.cpp
Go to the documentation of this file.
1 /* $Id: widget.cpp 27863 2017-05-03 20:09:51Z 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/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  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++;
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 {
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  Dimension d = GetSpriteSize(SPR_WINDOW_RESIZE_LEFT);
514  DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked,
515  r.bottom + 1 - WD_RESIZEBOX_BOTTOM - d.height + clicked);
516  } else {
517  Dimension d = GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT);
518  DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.right + 1 - WD_RESIZEBOX_RIGHT - d.width + clicked,
519  r.bottom + 1 - WD_RESIZEBOX_BOTTOM - d.height + clicked);
520  }
521 }
522 
528 static inline void DrawCloseBox(const Rect &r, Colours colour)
529 {
530  if (colour != COLOUR_WHITE) DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE);
531  DrawSprite(SPR_CLOSEBOX, (colour != COLOUR_WHITE ? TC_BLACK : TC_SILVER) | (1 << PALETTE_TEXT_RECOLOUR), r.left + WD_CLOSEBOX_LEFT, r.top + WD_CLOSEBOX_TOP);
532 }
533 
541 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
542 {
543  bool company_owned = owner < MAX_COMPANIES;
544 
545  DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_BORDERONLY);
546  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);
547 
548  if (company_owned) {
549  GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]);
550  }
551 
552  if (str != STR_NULL) {
554  int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
555  DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + offset, str, TC_FROMSTRING, SA_HOR_CENTER);
556  }
557 }
558 
569 static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
570 {
571  int text_offset = max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered
572 
573  int dd_width = NWidgetLeaf::dropdown_dimension.width;
574  int dd_height = NWidgetLeaf::dropdown_dimension.height;
575  int image_offset = max(0, ((int)(r.bottom - r.top + 1) - dd_height) / 2);
576 
577  if (_current_text_dir == TD_LTR) {
578  DrawFrameRect(r.left, r.top, r.right - dd_width, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
579  DrawFrameRect(r.right + 1 - dd_width, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
580  DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.right - (dd_width - 2) + clicked_dropdown, r.top + image_offset + clicked_dropdown);
581  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);
582  } else {
583  DrawFrameRect(r.left + dd_width, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
584  DrawFrameRect(r.left, r.top, r.left + dd_width - 1, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
585  DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.left + 1 + clicked_dropdown, r.top + image_offset + clicked_dropdown);
586  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);
587  }
588 }
589 
597 static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
598 {
599  DrawButtonDropdown(r, colour, false, clicked, str);
600 }
601 
606 {
607  this->nested_root->Draw(this);
608 
609  if (this->flags & WF_WHITE_BORDER) {
610  DrawFrameRect(0, 0, this->width - 1, this->height - 1, COLOUR_WHITE, FR_BORDERONLY);
611  }
612 
613  if (this->flags & WF_HIGHLIGHTED) {
614  extern bool _window_highlight_colour;
615  for (uint i = 0; i < this->nested_array_size; i++) {
616  const NWidgetBase *widget = this->GetWidget<NWidgetBase>(i);
617  if (widget == NULL || !widget->IsHighlighted()) continue;
618 
619  int left = widget->pos_x;
620  int top = widget->pos_y;
621  int right = left + widget->current_x - 1;
622  int bottom = top + widget->current_y - 1;
623 
624  int colour = _string_colourmap[_window_highlight_colour ? widget->GetHighlightColour() : TC_WHITE];
625 
626  GfxFillRect(left, top, left, bottom - WD_BEVEL_BOTTOM, colour);
627  GfxFillRect(left + WD_BEVEL_LEFT, top, right - WD_BEVEL_RIGHT, top, colour);
628  GfxFillRect(right, top, right, bottom - WD_BEVEL_BOTTOM, colour);
629  GfxFillRect(left, bottom, right, bottom, colour);
630  }
631  }
632 }
633 
639 void Window::DrawSortButtonState(int widget, SortButtonState state) const
640 {
641  if (state == SBS_OFF) return;
642 
643  assert(this->nested_array != NULL);
644  const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
645 
646  /* Sort button uses the same sprites as vertical scrollbar */
647  Dimension dim = NWidgetScrollbar::GetVerticalDimension();
648  int offset = this->IsWidgetLowered(widget) ? 1 : 0;
649  int x = offset + nwid->pos_x + (_current_text_dir == TD_LTR ? nwid->current_x - dim.width : 0);
650  int y = offset + nwid->pos_y + (nwid->current_y - dim.height) / 2;
651 
652  DrawSprite(state == SBS_DOWN ? SPR_ARROW_DOWN : SPR_ARROW_UP, PAL_NONE, x, y);
653 }
654 
660 {
661  return NWidgetScrollbar::GetVerticalDimension().width + 1;
662 }
663 
664 
723 {
724  this->type = tp;
725 }
726 
727 /* ~NWidgetContainer() takes care of #next and #prev data members. */
728 
776 void NWidgetBase::SetDirty(const Window *w) const
777 {
778  int abs_left = w->left + this->pos_x;
779  int abs_top = w->top + this->pos_y;
780  SetDirtyBlocks(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
781 }
782 
797 {
798  return (this->type == tp) ? this : NULL;
799 }
800 
808 {
809  this->fill_x = fill_x;
810  this->fill_y = fill_y;
811 }
812 
818 void NWidgetResizeBase::SetMinimalSize(uint min_x, uint min_y)
819 {
820  this->min_x = max(this->min_x, min_x);
821  this->min_y = max(this->min_y, min_y);
822 }
823 
830 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
831 {
832  this->min_y = min_lines * GetCharacterHeight(size) + spacing;
833 }
834 
840 void NWidgetResizeBase::SetFill(uint fill_x, uint fill_y)
841 {
842  this->fill_x = fill_x;
843  this->fill_y = fill_y;
844 }
845 
851 void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y)
852 {
853  this->resize_x = resize_x;
854  this->resize_y = resize_y;
855 }
856 
857 void NWidgetResizeBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
858 {
859  this->StoreSizePosition(sizing, x, y, given_width, given_height);
860 }
861 
871 NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint32 widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y)
872 {
873  this->colour = colour;
874  this->index = -1;
875  this->widget_data = widget_data;
876  this->tool_tip = tool_tip;
877  this->scrollbar_index = -1;
878 }
879 
884 void NWidgetCore::SetIndex(int index)
885 {
886  assert(index >= 0);
887  this->index = index;
888 }
889 
895 void NWidgetCore::SetDataTip(uint32 widget_data, StringID tool_tip)
896 {
897  this->widget_data = widget_data;
898  this->tool_tip = tool_tip;
899 }
900 
901 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
902 {
903  if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
904 }
905 
907 {
908  return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : NULL;
909 }
910 
916 {
917  this->head = NULL;
918  this->tail = NULL;
919 }
920 
921 NWidgetContainer::~NWidgetContainer()
922 {
923  while (this->head != NULL) {
924  NWidgetBase *wid = this->head->next;
925  delete this->head;
926  this->head = wid;
927  }
928  this->tail = NULL;
929 }
930 
932 {
933  if (this->type == tp) return this;
934  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
935  NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
936  if (nwid != NULL) return nwid;
937  }
938  return NULL;
939 }
940 
946 {
947  assert(wid->next == NULL && wid->prev == NULL);
948 
949  if (this->head == NULL) {
950  this->head = wid;
951  this->tail = wid;
952  } else {
953  assert(this->tail != NULL);
954  assert(this->tail->next == NULL);
955 
956  this->tail->next = wid;
957  wid->prev = this->tail;
958  this->tail = wid;
959  }
960 }
961 
962 void NWidgetContainer::FillNestedArray(NWidgetBase **array, uint length)
963 {
964  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
965  child_wid->FillNestedArray(array, length);
966  }
967 }
968 
973 {
974  this->index = -1;
975 }
976 
977 void NWidgetStacked::SetIndex(int index)
978 {
979  this->index = index;
980 }
981 
982 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
983 {
984  if (this->index >= 0 && init_array) { // Fill w->nested_array[]
985  assert(w->nested_array_size > (uint)this->index);
986  w->nested_array[this->index] = this;
987  }
988 
989  /* Zero size plane selected */
990  if (this->shown_plane >= SZSP_BEGIN) {
991  Dimension size = {0, 0};
992  Dimension padding = {0, 0};
993  Dimension fill = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
994  Dimension resize = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
995  /* Here we're primarily interested in the value of resize */
996  if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
997 
998  this->smallest_x = size.width;
999  this->smallest_y = size.height;
1000  this->fill_x = fill.width;
1001  this->fill_y = fill.height;
1002  this->resize_x = resize.width;
1003  this->resize_y = resize.height;
1004  return;
1005  }
1006 
1007  /* First sweep, recurse down and compute minimal size and filling. */
1008  this->smallest_x = 0;
1009  this->smallest_y = 0;
1010  this->fill_x = (this->head != NULL) ? 1 : 0;
1011  this->fill_y = (this->head != NULL) ? 1 : 0;
1012  this->resize_x = (this->head != NULL) ? 1 : 0;
1013  this->resize_y = (this->head != NULL) ? 1 : 0;
1014  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1015  child_wid->SetupSmallestSize(w, init_array);
1016 
1017  this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
1018  this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
1019  this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
1020  this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
1021  this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
1022  this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
1023  }
1024 }
1025 
1026 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1027 {
1028  assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1029  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1030 
1031  if (this->shown_plane >= SZSP_BEGIN) return;
1032 
1033  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1034  uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
1035  uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
1036  uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
1037 
1038  uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
1039  uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
1040  uint child_pos_y = child_wid->padding_top;
1041 
1042  child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
1043  }
1044 }
1045 
1046 void NWidgetStacked::FillNestedArray(NWidgetBase **array, uint length)
1047 {
1048  if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
1049  NWidgetContainer::FillNestedArray(array, length);
1050 }
1051 
1053 {
1054  if (this->shown_plane >= SZSP_BEGIN) return;
1055 
1056  int plane = 0;
1057  for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
1058  if (plane == this->shown_plane) {
1059  child_wid->Draw(w);
1060  return;
1061  }
1062  }
1063 
1064  NOT_REACHED();
1065 }
1066 
1068 {
1069  if (this->shown_plane >= SZSP_BEGIN) return NULL;
1070 
1071  if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1072  int plane = 0;
1073  for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
1074  if (plane == this->shown_plane) {
1075  return child_wid->GetWidgetFromPos(x, y);
1076  }
1077  }
1078  return NULL;
1079 }
1080 
1086 {
1087  this->shown_plane = plane;
1088 }
1089 
1090 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
1091 {
1092  this->flags = flags;
1093 }
1094 
1104 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
1105 {
1106  this->pip_pre = pip_pre;
1107  this->pip_inter = pip_inter;
1108  this->pip_post = pip_post;
1109 }
1110 
1112 {
1113  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1114  child_wid->Draw(w);
1115  }
1116 }
1117 
1119 {
1120  if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1121 
1122  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1123  NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
1124  if (nwid != NULL) return nwid;
1125  }
1126  return NULL;
1127 }
1128 
1131 {
1132 }
1133 
1135 {
1136  this->smallest_x = 0; // Sum of minimal size of all children.
1137  this->smallest_y = 0; // Biggest child.
1138  this->fill_x = 0; // smallest non-zero child widget fill step.
1139  this->fill_y = 1; // smallest common child fill step.
1140  this->resize_x = 0; // smallest non-zero child widget resize step.
1141  this->resize_y = 1; // smallest common child resize step.
1142 
1143  /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1144  uint longest = 0; // Longest child found.
1145  uint max_vert_fill = 0; // Biggest vertical fill step.
1146  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1147  child_wid->SetupSmallestSize(w, init_array);
1148  longest = max(longest, child_wid->smallest_x);
1149  max_vert_fill = max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
1150  this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
1151  }
1152  /* 1b. Make the container higher if needed to accommodate all children nicely. */
1153  uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height.
1154  uint cur_height = this->smallest_y;
1155  for (;;) {
1156  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1157  uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
1158  uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
1159  if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting children are not interesting.
1160  uint remainder = (cur_height - child_height) % step_size;
1161  if (remainder > 0) { // Child did not fit entirely, widen the container.
1162  cur_height += step_size - remainder;
1163  assert(cur_height < max_smallest); // Safeguard against infinite height expansion.
1164  /* Remaining children will adapt to the new cur_height, thus speeding up the computation. */
1165  }
1166  }
1167  }
1168  if (this->smallest_y == cur_height) break;
1169  this->smallest_y = cur_height; // Smallest height got changed, try again.
1170  }
1171  /* 2. For containers that must maintain equal width, extend child minimal size. */
1172  if (this->flags & NC_EQUALSIZE) {
1173  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1174  if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
1175  }
1176  }
1177  /* 3. Move PIP space to the children, compute smallest, fill, and resize values of the container. */
1178  if (this->head != NULL) this->head->padding_left += this->pip_pre;
1179  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1180  if (child_wid->next != NULL) {
1181  child_wid->padding_right += this->pip_inter;
1182  } else {
1183  child_wid->padding_right += this->pip_post;
1184  }
1185 
1186  this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
1187  if (child_wid->fill_x > 0) {
1188  if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
1189  }
1190  this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
1191 
1192  if (child_wid->resize_x > 0) {
1193  if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
1194  }
1195  this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
1196  }
1197  /* We need to zero the PIP settings so we can re-initialize the tree. */
1198  this->pip_pre = this->pip_inter = this->pip_post = 0;
1199 }
1200 
1201 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1202 {
1203  assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1204 
1205  /* Compute additional width given to us. */
1206  uint additional_length = given_width;
1207  if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
1208  /* For EQUALSIZE containers this does not sum to smallest_x during initialisation */
1209  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1210  additional_length -= child_wid->smallest_x + child_wid->padding_right + child_wid->padding_left;
1211  }
1212  } else {
1213  additional_length -= this->smallest_x;
1214  }
1215 
1216  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1217 
1218  /* In principle, the additional horizontal space is distributed evenly over the available resizable children. Due to step sizes, this may not always be feasible.
1219  * To make resizing work as good as possible, first children with biggest step sizes are done. These may get less due to rounding down.
1220  * 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
1221  * of the child with the smallest non-zero stepsize.
1222  *
1223  * 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
1224  * size and position, and directly call child->AssignSizePosition() with the computed values.
1225  * 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
1226  * then we call the child.
1227  */
1228 
1229  /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle vertical size for all children,
1230  * handle horizontal size for non-resizing children.
1231  */
1232  int num_changing_childs = 0; // Number of children that can change size.
1233  uint biggest_stepsize = 0;
1234  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1235  uint hor_step = child_wid->GetHorizontalStepSize(sizing);
1236  if (hor_step > 0) {
1237  num_changing_childs++;
1238  biggest_stepsize = max(biggest_stepsize, hor_step);
1239  } else {
1240  child_wid->current_x = child_wid->smallest_x;
1241  }
1242 
1243  uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
1244  child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
1245  }
1246 
1247  /* Second loop: Allocate the additional horizontal space over the resizing children, starting with the biggest resize steps. */
1248  while (biggest_stepsize > 0) {
1249  uint next_biggest_stepsize = 0;
1250  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1251  uint hor_step = child_wid->GetHorizontalStepSize(sizing);
1252  if (hor_step > biggest_stepsize) continue; // Already done
1253  if (hor_step == biggest_stepsize) {
1254  uint increment = additional_length / num_changing_childs;
1255  num_changing_childs--;
1256  if (hor_step > 1) increment -= increment % hor_step;
1257  child_wid->current_x = child_wid->smallest_x + increment;
1258  additional_length -= increment;
1259  continue;
1260  }
1261  next_biggest_stepsize = max(next_biggest_stepsize, hor_step);
1262  }
1263  biggest_stepsize = next_biggest_stepsize;
1264  }
1265  assert(num_changing_childs == 0);
1266 
1267  /* Third loop: Compute position and call the child. */
1268  uint position = rtl ? this->current_x : 0; // Place to put next child relative to origin of the container.
1269  NWidgetBase *child_wid = this->head;
1270  while (child_wid != NULL) {
1271  uint child_width = child_wid->current_x;
1272  uint child_x = x + (rtl ? position - child_width - child_wid->padding_left : position + child_wid->padding_left);
1273  uint child_y = y + child_wid->padding_top;
1274 
1275  child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
1276  uint padded_child_width = child_width + child_wid->padding_right + child_wid->padding_left;
1277  position = rtl ? position - padded_child_width : position + padded_child_width;
1278 
1279  child_wid = child_wid->next;
1280  }
1281 }
1282 
1285 {
1286  this->type = NWID_HORIZONTAL_LTR;
1287 }
1288 
1289 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1290 {
1291  NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
1292 }
1293 
1296 {
1297 }
1298 
1300 {
1301  this->smallest_x = 0; // Biggest child.
1302  this->smallest_y = 0; // Sum of minimal size of all children.
1303  this->fill_x = 1; // smallest common child fill step.
1304  this->fill_y = 0; // smallest non-zero child widget fill step.
1305  this->resize_x = 1; // smallest common child resize step.
1306  this->resize_y = 0; // smallest non-zero child widget resize step.
1307 
1308  /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1309  uint highest = 0; // Highest child found.
1310  uint max_hor_fill = 0; // Biggest horizontal fill step.
1311  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1312  child_wid->SetupSmallestSize(w, init_array);
1313  highest = max(highest, child_wid->smallest_y);
1314  max_hor_fill = max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
1315  this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
1316  }
1317  /* 1b. Make the container wider if needed to accommodate all children nicely. */
1318  uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height.
1319  uint cur_width = this->smallest_x;
1320  for (;;) {
1321  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1322  uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
1323  uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
1324  if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting children are not interesting.
1325  uint remainder = (cur_width - child_width) % step_size;
1326  if (remainder > 0) { // Child did not fit entirely, widen the container.
1327  cur_width += step_size - remainder;
1328  assert(cur_width < max_smallest); // Safeguard against infinite width expansion.
1329  /* Remaining children will adapt to the new cur_width, thus speeding up the computation. */
1330  }
1331  }
1332  }
1333  if (this->smallest_x == cur_width) break;
1334  this->smallest_x = cur_width; // Smallest width got changed, try again.
1335  }
1336  /* 2. For containers that must maintain equal width, extend children minimal size. */
1337  if (this->flags & NC_EQUALSIZE) {
1338  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1339  if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
1340  }
1341  }
1342  /* 3. Move PIP space to the child, compute smallest, fill, and resize values of the container. */
1343  if (this->head != NULL) this->head->padding_top += this->pip_pre;
1344  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1345  if (child_wid->next != NULL) {
1346  child_wid->padding_bottom += this->pip_inter;
1347  } else {
1348  child_wid->padding_bottom += this->pip_post;
1349  }
1350 
1351  this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
1352  if (child_wid->fill_y > 0) {
1353  if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
1354  }
1355  this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
1356 
1357  if (child_wid->resize_y > 0) {
1358  if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
1359  }
1360  this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
1361  }
1362  /* We need to zero the PIP settings so we can re-initialize the tree. */
1363  this->pip_pre = this->pip_inter = this->pip_post = 0;
1364 }
1365 
1366 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1367 {
1368  assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1369 
1370  /* Compute additional height given to us. */
1371  uint additional_length = given_height;
1372  if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
1373  /* For EQUALSIZE containers this does not sum to smallest_y during initialisation */
1374  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1375  additional_length -= child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
1376  }
1377  } else {
1378  additional_length -= this->smallest_y;
1379  }
1380 
1381  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1382 
1383  /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the children with the biggest resize steps.
1384  * It also stores computed widths and heights into current_x and current_y values of the child.
1385  */
1386 
1387  /* 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. */
1388  int num_changing_childs = 0; // Number of children that can change size.
1389  uint biggest_stepsize = 0;
1390  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1391  uint vert_step = child_wid->GetVerticalStepSize(sizing);
1392  if (vert_step > 0) {
1393  num_changing_childs++;
1394  biggest_stepsize = max(biggest_stepsize, vert_step);
1395  } else {
1396  child_wid->current_y = child_wid->smallest_y;
1397  }
1398 
1399  uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
1400  child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
1401  }
1402 
1403  /* Second loop: Allocate the additional vertical space over the resizing children, starting with the biggest resize steps. */
1404  while (biggest_stepsize > 0) {
1405  uint next_biggest_stepsize = 0;
1406  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1407  uint vert_step = child_wid->GetVerticalStepSize(sizing);
1408  if (vert_step > biggest_stepsize) continue; // Already done
1409  if (vert_step == biggest_stepsize) {
1410  uint increment = additional_length / num_changing_childs;
1411  num_changing_childs--;
1412  if (vert_step > 1) increment -= increment % vert_step;
1413  child_wid->current_y = child_wid->smallest_y + increment;
1414  additional_length -= increment;
1415  continue;
1416  }
1417  next_biggest_stepsize = max(next_biggest_stepsize, vert_step);
1418  }
1419  biggest_stepsize = next_biggest_stepsize;
1420  }
1421  assert(num_changing_childs == 0);
1422 
1423  /* Third loop: Compute position and call the child. */
1424  uint position = 0; // Place to put next child relative to origin of the container.
1425  for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1426  uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
1427  uint child_height = child_wid->current_y;
1428 
1429  child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
1430  position += child_height + child_wid->padding_top + child_wid->padding_bottom;
1431  }
1432 }
1433 
1440 {
1441  this->SetMinimalSize(length, height);
1442  this->SetResize(0, 0);
1443 }
1444 
1445 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
1446 {
1447  this->smallest_x = this->min_x;
1448  this->smallest_y = this->min_y;
1449 }
1450 
1451 void NWidgetSpacer::FillNestedArray(NWidgetBase **array, uint length)
1452 {
1453 }
1454 
1456 {
1457  /* Spacer widget is never visible. */
1458 }
1459 
1460 void NWidgetSpacer::SetDirty(const Window *w) const
1461 {
1462  /* Spacer widget never need repainting. */
1463 }
1464 
1466 {
1467  return NULL;
1468 }
1469 
1470 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX, NC_EQUALSIZE), index(-1), clicked(-1), count(-1)
1471 {
1472 }
1473 
1474 void NWidgetMatrix::SetIndex(int index)
1475 {
1476  this->index = index;
1477 }
1478 
1479 void NWidgetMatrix::SetColour(Colours colour)
1480 {
1481  this->colour = colour;
1482 }
1483 
1488 void NWidgetMatrix::SetClicked(int clicked)
1489 {
1490  this->clicked = clicked;
1491  if (this->clicked >= 0 && this->sb != NULL && this->widgets_x != 0) {
1492  int vpos = (this->clicked / this->widgets_x) * this->widget_h; // Vertical position of the top.
1493  /* Need to scroll down -> Scroll to the bottom.
1494  * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
1495  if (this->sb->GetPosition() < vpos) vpos += this->widget_h - this->pip_inter - 1;
1496  this->sb->ScrollTowards(vpos);
1497  }
1498 }
1499 
1506 {
1507  this->count = count;
1508 
1509  if (this->sb == NULL || this->widgets_x == 0) return;
1510 
1511  /* We need to get the number of pixels the matrix is high/wide.
1512  * So, determine the number of rows/columns based on the number of
1513  * columns/rows (one is constant/unscrollable).
1514  * Then multiply that by the height of a widget, and add the pre
1515  * and post spacing "offsets". */
1516  count = CeilDiv(count, this->sb->IsVertical() ? this->widgets_x : this->widgets_y);
1517  count *= (this->sb->IsVertical() ? this->head->smallest_y : this->head->smallest_x) + this->pip_inter;
1518  if (count > 0) count -= this->pip_inter; // We counted an inter too much in the multiplication above
1519  count += this->pip_pre + this->pip_post;
1520  this->sb->SetCount(count);
1521  this->sb->SetCapacity(this->sb->IsVertical() ? this->current_y : this->current_x);
1522  this->sb->SetStepSize(this->sb->IsVertical() ? this->widget_h : this->widget_w);
1523 }
1524 
1530 {
1531  this->sb = sb;
1532 }
1533 
1534 void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array)
1535 {
1536  assert(this->head != NULL);
1537  assert(this->head->next == NULL);
1538 
1539  if (this->index >= 0 && init_array) { // Fill w->nested_array[]
1540  assert(w->nested_array_size > (uint)this->index);
1541  w->nested_array[this->index] = this;
1542  }
1543 
1544  /* Reset the widget number. */
1545  NWidgetCore *nw = dynamic_cast<NWidgetCore *>(this->head);
1546  assert(nw != NULL);
1547  SB(nw->index, 16, 16, 0);
1548  this->head->SetupSmallestSize(w, init_array);
1549 
1550  Dimension padding = { (uint)this->pip_pre + this->pip_post, (uint)this->pip_pre + this->pip_post};
1551  Dimension size = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height};
1552  Dimension fill = {0, 0};
1553  Dimension resize = {this->pip_inter + this->head->smallest_x, this->pip_inter + this->head->smallest_y};
1554 
1555  if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
1556 
1557  this->smallest_x = size.width;
1558  this->smallest_y = size.height;
1559  this->fill_x = fill.width;
1560  this->fill_y = fill.height;
1561  this->resize_x = resize.width;
1562  this->resize_y = resize.height;
1563 }
1564 
1565 void NWidgetMatrix::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1566 {
1567  assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1568 
1569  this->pos_x = x;
1570  this->pos_y = y;
1571  this->current_x = given_width;
1572  this->current_y = given_height;
1573 
1574  /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
1575  this->widget_w = this->head->smallest_x + this->pip_inter;
1576  this->widget_h = this->head->smallest_y + this->pip_inter;
1577 
1578  /* Account for the pip_inter is between widgets, so we need to account for that when
1579  * the division assumes pip_inter is used for all widgets. */
1580  this->widgets_x = CeilDiv(this->current_x - this->pip_pre - this->pip_post + this->pip_inter, this->widget_w);
1581  this->widgets_y = CeilDiv(this->current_y - this->pip_pre - this->pip_post + this->pip_inter, this->widget_h);
1582 
1583  /* When resizing, update the scrollbar's count. E.g. with a vertical
1584  * scrollbar becoming wider or narrower means the amount of rows in
1585  * the scrollbar becomes respectively smaller or higher. */
1586  this->SetCount(this->count);
1587 }
1588 
1589 void NWidgetMatrix::FillNestedArray(NWidgetBase **array, uint length)
1590 {
1591  if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
1592  NWidgetContainer::FillNestedArray(array, length);
1593 }
1594 
1596 {
1597  /* Falls outside of the matrix widget. */
1598  if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1599 
1600  int start_x, start_y, base_offs_x, base_offs_y;
1601  this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
1602 
1603  bool rtl = _current_text_dir == TD_RTL;
1604 
1605  int widget_col = (rtl ?
1606  -x + (int)this->pip_post + (int)this->pos_x + base_offs_x + (int)this->widget_w - 1 - (int)this->pip_inter :
1607  x - (int)this->pip_pre - (int)this->pos_x - base_offs_x
1608  ) / this->widget_w;
1609 
1610  int widget_row = (y - base_offs_y - (int)this->pip_pre - (int)this->pos_y) / this->widget_h;
1611 
1612  int sub_wid = (widget_row + start_y) * this->widgets_x + start_x + widget_col;
1613  if (sub_wid >= this->count) return NULL;
1614 
1615  NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
1616  assert(child != NULL);
1618  this->pos_x + (rtl ? this->pip_post - widget_col * this->widget_w : this->pip_pre + widget_col * this->widget_w) + base_offs_x,
1619  this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y,
1620  child->smallest_x, child->smallest_y, rtl);
1621 
1622  SB(child->index, 16, 16, sub_wid);
1623 
1624  return child->GetWidgetFromPos(x, y);
1625 }
1626 
1627 /* virtual */ void NWidgetMatrix::Draw(const Window *w)
1628 {
1629  /* Fill the background. */
1630  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]);
1631 
1632  /* Set up a clipping area for the previews. */
1633  bool rtl = _current_text_dir == TD_RTL;
1634  DrawPixelInfo tmp_dpi;
1635  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;
1636  DrawPixelInfo *old_dpi = _cur_dpi;
1637  _cur_dpi = &tmp_dpi;
1638 
1639  /* Get the appropriate offsets so we can draw the right widgets. */
1640  NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
1641  assert(child != NULL);
1642  int start_x, start_y, base_offs_x, base_offs_y;
1643  this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
1644 
1645  int offs_y = base_offs_y;
1646  for (int y = start_y; y < start_y + this->widgets_y + 1; y++, offs_y += this->widget_h) {
1647  /* Are we within bounds? */
1648  if (offs_y + child->smallest_y <= 0) continue;
1649  if (offs_y >= (int)this->current_y) break;
1650 
1651  /* We've passed our amount of widgets. */
1652  if (y * this->widgets_x >= this->count) break;
1653 
1654  int offs_x = base_offs_x;
1655  for (int x = start_x; x < start_x + this->widgets_x + 1; x++, offs_x += rtl ? -this->widget_w : this->widget_w) {
1656  /* Are we within bounds? */
1657  if (offs_x + child->smallest_x <= 0) continue;
1658  if (offs_x >= (int)this->current_x) continue;
1659 
1660  /* Do we have this many widgets? */
1661  int sub_wid = y * this->widgets_x + x;
1662  if (sub_wid >= this->count) break;
1663 
1664  child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl);
1665  child->SetLowered(this->clicked == sub_wid);
1666  SB(child->index, 16, 16, sub_wid);
1667  child->Draw(w);
1668  }
1669  }
1670 
1671  /* Restore the clipping area. */
1672  _cur_dpi = old_dpi;
1673 }
1674 
1682 void NWidgetMatrix::GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
1683 {
1684  base_offs_x = _current_text_dir == TD_RTL ? this->widget_w * (this->widgets_x - 1) : 0;
1685  base_offs_y = 0;
1686  start_x = 0;
1687  start_y = 0;
1688  if (this->sb != NULL) {
1689  if (this->sb->IsVertical()) {
1690  start_y = this->sb->GetPosition() / this->widget_h;
1691  base_offs_y += -this->sb->GetPosition() + start_y * this->widget_h;
1692  } else {
1693  start_x = this->sb->GetPosition() / this->widget_w;
1694  int sub_x = this->sb->GetPosition() - start_x * this->widget_w;
1695  if (_current_text_dir == TD_RTL) {
1696  base_offs_x += sub_x;
1697  } else {
1698  base_offs_x -= sub_x;
1699  }
1700  }
1701  }
1702 }
1703 
1713 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
1714 {
1715  assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
1716  if (index >= 0) this->SetIndex(index);
1717  this->child = child;
1718 }
1719 
1720 NWidgetBackground::~NWidgetBackground()
1721 {
1722  if (this->child != NULL) delete this->child;
1723 }
1724 
1733 {
1734  if (this->child == NULL) {
1735  this->child = new NWidgetVertical();
1736  }
1737  this->child->Add(nwid);
1738 }
1739 
1750 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
1751 {
1752  if (this->child == NULL) {
1753  this->child = new NWidgetVertical();
1754  }
1755  this->child->SetPIP(pip_pre, pip_inter, pip_post);
1756 }
1757 
1759 {
1760  if (init_array && this->index >= 0) {
1761  assert(w->nested_array_size > (uint)this->index);
1762  w->nested_array[this->index] = this;
1763  }
1764  if (this->child != NULL) {
1765  this->child->SetupSmallestSize(w, init_array);
1766 
1767  this->smallest_x = this->child->smallest_x;
1768  this->smallest_y = this->child->smallest_y;
1769  this->fill_x = this->child->fill_x;
1770  this->fill_y = this->child->fill_y;
1771  this->resize_x = this->child->resize_x;
1772  this->resize_y = this->child->resize_y;
1773 
1774  /* Account for the size of the frame's text if that exists */
1775  if (w != NULL && this->type == WWT_FRAME) {
1778  this->child->padding_top = max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
1780 
1781  this->smallest_x += this->child->padding_left + this->child->padding_right;
1782  this->smallest_y += this->child->padding_top + this->child->padding_bottom;
1783 
1784  if (this->index >= 0) w->SetStringParameters(this->index);
1786  }
1787  } else {
1788  Dimension d = {this->min_x, this->min_y};
1789  Dimension fill = {this->fill_x, this->fill_y};
1790  Dimension resize = {this->resize_x, this->resize_y};
1791  if (w != NULL) { // A non-NULL window pointer acts as switch to turn dynamic widget size on.
1792  if (this->type == WWT_FRAME || this->type == WWT_INSET) {
1793  if (this->index >= 0) w->SetStringParameters(this->index);
1794  Dimension background = GetStringBoundingBox(this->widget_data);
1795  background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
1796  d = maxdim(d, background);
1797  }
1798  if (this->index >= 0) {
1799  static const Dimension padding = {0, 0};
1800  w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
1801  }
1802  }
1803  this->smallest_x = d.width;
1804  this->smallest_y = d.height;
1805  this->fill_x = fill.width;
1806  this->fill_y = fill.height;
1807  this->resize_x = resize.width;
1808  this->resize_y = resize.height;
1809  }
1810 }
1811 
1812 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1813 {
1814  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1815 
1816  if (this->child != NULL) {
1817  uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
1818  uint width = given_width - this->child->padding_right - this->child->padding_left;
1819  uint height = given_height - this->child->padding_top - this->child->padding_bottom;
1820  this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
1821  }
1822 }
1823 
1824 void NWidgetBackground::FillNestedArray(NWidgetBase **array, uint length)
1825 {
1826  if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
1827  if (this->child != NULL) this->child->FillNestedArray(array, length);
1828 }
1829 
1831 {
1832  if (this->current_x == 0 || this->current_y == 0) return;
1833 
1834  Rect r;
1835  r.left = this->pos_x;
1836  r.right = this->pos_x + this->current_x - 1;
1837  r.top = this->pos_y;
1838  r.bottom = this->pos_y + this->current_y - 1;
1839 
1840  const DrawPixelInfo *dpi = _cur_dpi;
1841  if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
1842 
1843  switch (this->type) {
1844  case WWT_PANEL:
1845  assert(this->widget_data == 0);
1846  DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
1847  break;
1848 
1849  case WWT_FRAME:
1850  if (this->index >= 0) w->SetStringParameters(this->index);
1851  DrawFrame(r, this->colour, this->widget_data);
1852  break;
1853 
1854  case WWT_INSET:
1855  if (this->index >= 0) w->SetStringParameters(this->index);
1856  DrawInset(r, this->colour, this->widget_data);
1857  break;
1858 
1859  default:
1860  NOT_REACHED();
1861  }
1862 
1863  if (this->index >= 0) w->DrawWidget(r, this->index);
1864  if (this->child != NULL) this->child->Draw(w);
1865 
1866  if (this->IsDisabled()) {
1867  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
1868  }
1869 }
1870 
1872 {
1873  NWidgetCore *nwid = NULL;
1874  if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
1875  if (this->child != NULL) nwid = this->child->GetWidgetFromPos(x, y);
1876  if (nwid == NULL) nwid = this;
1877  }
1878  return nwid;
1879 }
1880 
1882 {
1883  NWidgetBase *nwid = NULL;
1884  if (this->child != NULL) nwid = this->child->GetWidgetOfType(tp);
1885  if (nwid == NULL && this->type == tp) nwid = this;
1886  return nwid;
1887 }
1888 
1889 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
1890 {
1891  this->SetIndex(index);
1892 }
1893 
1895 {
1896  if (init_array && this->index >= 0) {
1897  assert(w->nested_array_size > (uint)this->index);
1898  w->nested_array[this->index] = this;
1899  }
1900  this->smallest_x = this->min_x;
1901  this->smallest_y = this->min_y;
1902 }
1903 
1905 {
1906  if (this->disp_flags & ND_NO_TRANSPARENCY) {
1908  _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_LOADING); // Disable all transparency, except textual stuff
1909  w->DrawViewport();
1910  _transparency_opt = to_backup;
1911  } else {
1912  w->DrawViewport();
1913  }
1914 
1915  /* Optionally shade the viewport. */
1916  if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
1917  GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
1919  }
1920 }
1921 
1928 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
1929 {
1930  InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
1931 }
1932 
1938 {
1939  ViewPort *vp = w->viewport;
1940  if (vp != NULL) {
1941  vp->left = w->left + this->pos_x;
1942  vp->top = w->top + this->pos_y;
1943  vp->width = this->current_x;
1944  vp->height = this->current_y;
1945 
1946  vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
1947  vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
1948  }
1949 }
1950 
1960 int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
1961 {
1962  uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
1963  if (pos != INT_MAX) pos += this->GetPosition();
1964  return (pos >= this->GetCount()) ? INT_MAX : pos;
1965 }
1966 
1974 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
1975 {
1976  NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
1977  if (this->IsVertical()) {
1978  this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
1979  } else {
1980  this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
1981  }
1982 }
1983 
1990 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL), Scrollbar(tp != NWID_HSCROLLBAR)
1991 {
1992  assert(tp == NWID_HSCROLLBAR || tp == NWID_VSCROLLBAR);
1993  this->SetIndex(index);
1994 }
1995 
1997 {
1998  if (init_array && this->index >= 0) {
1999  assert(w->nested_array_size > (uint)this->index);
2000  w->nested_array[this->index] = this;
2001  }
2002  this->min_x = 0;
2003  this->min_y = 0;
2004 
2005  switch (this->type) {
2006  case NWID_HSCROLLBAR:
2007  this->SetMinimalSize(NWidgetScrollbar::GetHorizontalDimension().width * 3, NWidgetScrollbar::GetHorizontalDimension().height);
2008  this->SetResize(1, 0);
2009  this->SetFill(1, 0);
2010  this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
2011  break;
2012 
2013  case NWID_VSCROLLBAR:
2014  this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width, NWidgetScrollbar::GetVerticalDimension().height * 3);
2015  this->SetResize(0, 1);
2016  this->SetFill(0, 1);
2017  this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
2018  break;
2019 
2020  default: NOT_REACHED();
2021  }
2022 
2023  this->smallest_x = this->min_x;
2024  this->smallest_y = this->min_y;
2025 }
2026 
2028 {
2029  if (this->current_x == 0 || this->current_y == 0) return;
2030 
2031  Rect r;
2032  r.left = this->pos_x;
2033  r.right = this->pos_x + this->current_x - 1;
2034  r.top = this->pos_y;
2035  r.bottom = this->pos_y + this->current_y - 1;
2036 
2037  const DrawPixelInfo *dpi = _cur_dpi;
2038  if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
2039 
2040  bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
2041  bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
2042  bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->scrolling_scrollbar == this->index;
2043 
2044  if (this->type == NWID_HSCROLLBAR) {
2045  DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
2046  } else {
2047  DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
2048  }
2049 
2050  if (this->IsDisabled()) {
2051  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
2052  }
2053 }
2054 
2055 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
2056 {
2057  vertical_dimension.width = vertical_dimension.height = 0;
2058  horizontal_dimension.width = horizontal_dimension.height = 0;
2059 }
2060 
2061 /* static */ Dimension NWidgetScrollbar::GetVerticalDimension()
2062 {
2064  if (vertical_dimension.width == 0) {
2065  vertical_dimension = maxdim(GetSpriteSize(SPR_ARROW_UP), GetSpriteSize(SPR_ARROW_DOWN));
2066  vertical_dimension.width += extra.width;
2067  vertical_dimension.height += extra.height;
2068  }
2069  return vertical_dimension;
2070 }
2071 
2072 /* static */ Dimension NWidgetScrollbar::GetHorizontalDimension()
2073 {
2075  if (horizontal_dimension.width == 0) {
2076  horizontal_dimension = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
2077  horizontal_dimension.width += extra.width;
2078  horizontal_dimension.height += extra.height;
2079  }
2080  return horizontal_dimension;
2081 }
2082 
2085 
2088 {
2089  shadebox_dimension.width = shadebox_dimension.height = 0;
2090  debugbox_dimension.width = debugbox_dimension.height = 0;
2091  defsizebox_dimension.width = defsizebox_dimension.height = 0;
2092  stickybox_dimension.width = stickybox_dimension.height = 0;
2093  resizebox_dimension.width = resizebox_dimension.height = 0;
2094  closebox_dimension.width = closebox_dimension.height = 0;
2095  dropdown_dimension.width = dropdown_dimension.height = 0;
2096 }
2097 
2105 
2114 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
2115 {
2116  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);
2117  if (index >= 0) this->SetIndex(index);
2118  this->min_x = 0;
2119  this->min_y = 0;
2120  this->SetResize(0, 0);
2121 
2122  switch (tp) {
2123  case WWT_EMPTY:
2124  break;
2125 
2126  case WWT_PUSHBTN:
2127  case WWT_IMGBTN:
2128  case WWT_PUSHIMGBTN:
2129  case WWT_IMGBTN_2:
2130  case WWT_TEXTBTN:
2131  case WWT_PUSHTXTBTN:
2132  case WWT_TEXTBTN_2:
2133  case WWT_LABEL:
2134  case WWT_TEXT:
2135  case WWT_MATRIX:
2136  case NWID_BUTTON_DROPDOWN:
2137  case NWID_PUSHBUTTON_DROPDOWN:
2138  case WWT_ARROWBTN:
2139  case WWT_PUSHARROWBTN:
2140  this->SetFill(0, 0);
2141  break;
2142 
2143  case WWT_EDITBOX:
2144  this->SetFill(0, 0);
2145  break;
2146 
2147  case WWT_CAPTION:
2148  this->SetFill(1, 0);
2149  this->SetResize(1, 0);
2150  this->min_y = WD_CAPTION_HEIGHT;
2151  this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
2152  break;
2153 
2154  case WWT_STICKYBOX:
2155  this->SetFill(0, 0);
2157  this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
2158  break;
2159 
2160  case WWT_SHADEBOX:
2161  this->SetFill(0, 0);
2163  this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
2164  break;
2165 
2166  case WWT_DEBUGBOX:
2167  this->SetFill(0, 0);
2169  this->SetDataTip(STR_NULL, STR_TOOLTIP_DEBUG);
2170  break;
2171 
2172  case WWT_DEFSIZEBOX:
2173  this->SetFill(0, 0);
2175  this->SetDataTip(STR_NULL, STR_TOOLTIP_DEFSIZE);
2176  break;
2177 
2178  case WWT_RESIZEBOX:
2179  this->SetFill(0, 0);
2181  this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
2182  break;
2183 
2184  case WWT_CLOSEBOX:
2185  this->SetFill(0, 0);
2187  this->SetDataTip(STR_NULL, STR_TOOLTIP_CLOSE_WINDOW);
2188  break;
2189 
2190  case WWT_DROPDOWN:
2191  this->SetFill(0, 0);
2192  this->min_y = WD_DROPDOWN_HEIGHT;
2193  break;
2194 
2195  default:
2196  NOT_REACHED();
2197  }
2198 }
2199 
2200 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
2201 {
2202  if (this->index >= 0 && init_array) { // Fill w->nested_array[]
2203  assert(w->nested_array_size > (uint)this->index);
2204  w->nested_array[this->index] = this;
2205  }
2206 
2207  Dimension size = {this->min_x, this->min_y};
2208  Dimension fill = {this->fill_x, this->fill_y};
2209  Dimension resize = {this->resize_x, this->resize_y};
2210  /* Get padding, and update size with the real content size if appropriate. */
2211  const Dimension *padding = NULL;
2212  switch (this->type) {
2213  case WWT_EMPTY: {
2214  static const Dimension extra = {0, 0};
2215  padding = &extra;
2216  break;
2217  }
2218  case WWT_MATRIX: {
2220  padding = &extra;
2221  break;
2222  }
2223  case WWT_SHADEBOX: {
2225  padding = &extra;
2226  if (NWidgetLeaf::shadebox_dimension.width == 0) {
2227  NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
2228  NWidgetLeaf::shadebox_dimension.width += extra.width;
2229  NWidgetLeaf::shadebox_dimension.height += extra.height;
2230  }
2231  size = maxdim(size, NWidgetLeaf::shadebox_dimension);
2232  break;
2233  }
2234  case WWT_DEBUGBOX:
2237  padding = &extra;
2238  if (NWidgetLeaf::debugbox_dimension.width == 0) {
2239  NWidgetLeaf::debugbox_dimension = GetSpriteSize(SPR_WINDOW_DEBUG);
2240  NWidgetLeaf::debugbox_dimension.width += extra.width;
2241  NWidgetLeaf::debugbox_dimension.height += extra.height;
2242  }
2243  size = maxdim(size, NWidgetLeaf::debugbox_dimension);
2244  } else {
2245  /* If the setting is disabled we don't want to see it! */
2246  size.width = 0;
2247  fill.width = 0;
2248  resize.width = 0;
2249  }
2250  break;
2251 
2252  case WWT_STICKYBOX: {
2254  padding = &extra;
2255  if (NWidgetLeaf::stickybox_dimension.width == 0) {
2256  NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
2257  NWidgetLeaf::stickybox_dimension.width += extra.width;
2258  NWidgetLeaf::stickybox_dimension.height += extra.height;
2259  }
2260  size = maxdim(size, NWidgetLeaf::stickybox_dimension);
2261  break;
2262  }
2263 
2264  case WWT_DEFSIZEBOX: {
2266  padding = &extra;
2267  if (NWidgetLeaf::defsizebox_dimension.width == 0) {
2268  NWidgetLeaf::defsizebox_dimension = GetSpriteSize(SPR_WINDOW_DEFSIZE);
2269  NWidgetLeaf::defsizebox_dimension.width += extra.width;
2270  NWidgetLeaf::defsizebox_dimension.height += extra.height;
2271  }
2272  size = maxdim(size, NWidgetLeaf::defsizebox_dimension);
2273  break;
2274  }
2275 
2276  case WWT_RESIZEBOX: {
2278  padding = &extra;
2279  if (NWidgetLeaf::resizebox_dimension.width == 0) {
2280  NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
2281  NWidgetLeaf::resizebox_dimension.width += extra.width;
2282  NWidgetLeaf::resizebox_dimension.height += extra.height;
2283  }
2284  size = maxdim(size, NWidgetLeaf::resizebox_dimension);
2285  break;
2286  }
2287  case WWT_EDITBOX: {
2288  Dimension sprite_size = GetSpriteSize(_current_text_dir == TD_RTL ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
2289  size.width = max(size.width, 30 + sprite_size.width);
2290  size.height = max(sprite_size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
2291  /* FALL THROUGH */
2292  }
2293  case WWT_PUSHBTN: {
2295  padding = &extra;
2296  break;
2297  }
2298  case WWT_IMGBTN:
2299  case WWT_IMGBTN_2:
2300  case WWT_PUSHIMGBTN: {
2302  padding = &extra;
2303  Dimension d2 = GetSpriteSize(this->widget_data);
2304  if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
2305  d2.width += extra.width;
2306  d2.height += extra.height;
2307  size = maxdim(size, d2);
2308  break;
2309  }
2310  case WWT_ARROWBTN:
2311  case WWT_PUSHARROWBTN: {
2313  padding = &extra;
2314  Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
2315  d2.width += extra.width;
2316  d2.height += extra.height;
2317  size = maxdim(size, d2);
2318  break;
2319  }
2320 
2321  case WWT_CLOSEBOX: {
2323  padding = &extra;
2324  if (NWidgetLeaf::closebox_dimension.width == 0) {
2325  NWidgetLeaf::closebox_dimension = GetSpriteSize(SPR_CLOSEBOX);
2326  NWidgetLeaf::closebox_dimension.width += extra.width;
2327  NWidgetLeaf::closebox_dimension.height += extra.height;
2328  }
2329  size = maxdim(size, NWidgetLeaf::closebox_dimension);
2330  break;
2331  }
2332  case WWT_TEXTBTN:
2333  case WWT_PUSHTXTBTN:
2334  case WWT_TEXTBTN_2: {
2336  padding = &extra;
2337  if (this->index >= 0) w->SetStringParameters(this->index);
2339  d2.width += extra.width;
2340  d2.height += extra.height;
2341  size = maxdim(size, d2);
2342  break;
2343  }
2344  case WWT_LABEL:
2345  case WWT_TEXT: {
2346  static const Dimension extra = {0, 0};
2347  padding = &extra;
2348  if (this->index >= 0) w->SetStringParameters(this->index);
2349  size = maxdim(size, GetStringBoundingBox(this->widget_data));
2350  break;
2351  }
2352  case WWT_CAPTION: {
2354  padding = &extra;
2355  if (this->index >= 0) w->SetStringParameters(this->index);
2357  d2.width += extra.width;
2358  d2.height += extra.height;
2359  size = maxdim(size, d2);
2360  break;
2361  }
2362  case WWT_DROPDOWN:
2363  case NWID_BUTTON_DROPDOWN:
2364  case NWID_PUSHBUTTON_DROPDOWN: {
2366  padding = &extra;
2367  if (NWidgetLeaf::dropdown_dimension.width == 0) {
2368  NWidgetLeaf::dropdown_dimension = GetSpriteSize(SPR_ARROW_DOWN);
2369  NWidgetLeaf::dropdown_dimension.width += WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT;
2370  NWidgetLeaf::dropdown_dimension.height += WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM;
2371  extra.width = WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT + NWidgetLeaf::dropdown_dimension.width;
2372  }
2373  if (this->index >= 0) w->SetStringParameters(this->index);
2375  d2.width += extra.width;
2376  d2.height = max(d2.height, NWidgetLeaf::dropdown_dimension.height) + extra.height;
2377  size = maxdim(size, d2);
2378  break;
2379  }
2380  default:
2381  NOT_REACHED();
2382  }
2383 
2384  if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
2385 
2386  this->smallest_x = size.width;
2387  this->smallest_y = size.height;
2388  this->fill_x = fill.width;
2389  this->fill_y = fill.height;
2390  this->resize_x = resize.width;
2391  this->resize_y = resize.height;
2392 }
2393 
2395 {
2396  if (this->current_x == 0 || this->current_y == 0) return;
2397 
2398  /* Setup a clipping rectangle... */
2399  DrawPixelInfo new_dpi;
2400  if (!FillDrawPixelInfo(&new_dpi, this->pos_x, this->pos_y, this->current_x, this->current_y)) return;
2401  /* ...but keep coordinates relative to the window. */
2402  new_dpi.left += this->pos_x;
2403  new_dpi.top += this->pos_y;
2404 
2405  DrawPixelInfo *old_dpi = _cur_dpi;
2406  _cur_dpi = &new_dpi;
2407 
2408  Rect r;
2409  r.left = this->pos_x;
2410  r.right = this->pos_x + this->current_x - 1;
2411  r.top = this->pos_y;
2412  r.bottom = this->pos_y + this->current_y - 1;
2413 
2414  bool clicked = this->IsLowered();
2415  switch (this->type) {
2416  case WWT_EMPTY:
2417  break;
2418 
2419  case WWT_PUSHBTN:
2420  assert(this->widget_data == 0);
2421  DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
2422  break;
2423 
2424  case WWT_IMGBTN:
2425  case WWT_PUSHIMGBTN:
2426  case WWT_IMGBTN_2:
2427  DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
2428  break;
2429 
2430  case WWT_TEXTBTN:
2431  case WWT_PUSHTXTBTN:
2432  case WWT_TEXTBTN_2:
2433  if (this->index >= 0) w->SetStringParameters(this->index);
2434  DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
2435  DrawLabel(r, this->type, clicked, this->widget_data);
2436  break;
2437 
2438  case WWT_ARROWBTN:
2439  case WWT_PUSHARROWBTN: {
2440  SpriteID sprite;
2441  switch (this->widget_data) {
2442  case AWV_DECREASE: sprite = _current_text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
2443  case AWV_INCREASE: sprite = _current_text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
2444  case AWV_LEFT: sprite = SPR_ARROW_LEFT; break;
2445  case AWV_RIGHT: sprite = SPR_ARROW_RIGHT; break;
2446  default: NOT_REACHED();
2447  }
2448  DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
2449  break;
2450  }
2451 
2452  case WWT_LABEL:
2453  if (this->index >= 0) w->SetStringParameters(this->index);
2454  DrawLabel(r, this->type, clicked, this->widget_data);
2455  break;
2456 
2457  case WWT_TEXT:
2458  if (this->index >= 0) w->SetStringParameters(this->index);
2459  DrawText(r, (TextColour)this->colour, this->widget_data);
2460  break;
2461 
2462  case WWT_MATRIX:
2463  DrawMatrix(r, this->colour, clicked, this->widget_data, this->resize_x, this->resize_y);
2464  break;
2465 
2466  case WWT_EDITBOX: {
2467  const QueryString *query = w->GetQueryString(this->index);
2468  if (query != NULL) query->DrawEditBox(w, this->index);
2469  break;
2470  }
2471 
2472  case WWT_CAPTION:
2473  if (this->index >= 0) w->SetStringParameters(this->index);
2474  DrawCaption(r, this->colour, w->owner, this->widget_data);
2475  break;
2476 
2477  case WWT_SHADEBOX:
2478  assert(this->widget_data == 0);
2479  DrawShadeBox(r, this->colour, w->IsShaded());
2480  break;
2481 
2482  case WWT_DEBUGBOX:
2483  DrawDebugBox(r, this->colour, clicked);
2484  break;
2485 
2486  case WWT_STICKYBOX:
2487  assert(this->widget_data == 0);
2488  DrawStickyBox(r, this->colour, !!(w->flags & WF_STICKY));
2489  break;
2490 
2491  case WWT_DEFSIZEBOX:
2492  assert(this->widget_data == 0);
2493  DrawDefSizeBox(r, this->colour, clicked);
2494  break;
2495 
2496  case WWT_RESIZEBOX:
2497  assert(this->widget_data == 0);
2498  DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags & WF_SIZING));
2499  break;
2500 
2501  case WWT_CLOSEBOX:
2502  DrawCloseBox(r, this->colour);
2503  break;
2504 
2505  case WWT_DROPDOWN:
2506  if (this->index >= 0) w->SetStringParameters(this->index);
2507  DrawDropdown(r, this->colour, clicked, this->widget_data);
2508  break;
2509 
2510  case NWID_BUTTON_DROPDOWN:
2511  case NWID_PUSHBUTTON_DROPDOWN:
2512  if (this->index >= 0) w->SetStringParameters(this->index);
2513  DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
2514  break;
2515 
2516  default:
2517  NOT_REACHED();
2518  }
2519  if (this->index >= 0) w->DrawWidget(r, this->index);
2520 
2521  if (this->IsDisabled()) {
2522  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
2523  }
2524 
2525  _cur_dpi = old_dpi;
2526 }
2527 
2536 {
2537  if (_current_text_dir == TD_LTR) {
2538  int button_width = this->pos_x + this->current_x - NWidgetLeaf::dropdown_dimension.width;
2539  return pt.x < button_width;
2540  } else {
2541  int button_left = this->pos_x + NWidgetLeaf::dropdown_dimension.width;
2542  return pt.x >= button_left;
2543  }
2544 }
2545 
2546 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
2547 
2563 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
2564 {
2565  int num_used = 0;
2566 
2567  *dest = NULL;
2568  *fill_dest = false;
2569 
2570  while (count > num_used) {
2571  switch (parts->type) {
2572  case NWID_SPACER:
2573  if (*dest != NULL) return num_used;
2574  *dest = new NWidgetSpacer(0, 0);
2575  break;
2576 
2577  case NWID_HORIZONTAL:
2578  if (*dest != NULL) return num_used;
2579  *dest = new NWidgetHorizontal(parts->u.cont_flags);
2580  *fill_dest = true;
2581  break;
2582 
2583  case NWID_HORIZONTAL_LTR:
2584  if (*dest != NULL) return num_used;
2585  *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
2586  *fill_dest = true;
2587  break;
2588 
2589  case WWT_PANEL:
2590  case WWT_INSET:
2591  case WWT_FRAME:
2592  if (*dest != NULL) return num_used;
2593  *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
2594  *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2595  *fill_dest = true;
2596  break;
2597 
2598  case NWID_VERTICAL:
2599  if (*dest != NULL) return num_used;
2600  *dest = new NWidgetVertical(parts->u.cont_flags);
2601  *fill_dest = true;
2602  break;
2603 
2604  case NWID_MATRIX: {
2605  if (*dest != NULL) return num_used;
2606  NWidgetMatrix *nwm = new NWidgetMatrix();
2607  *dest = nwm;
2608  *fill_dest = true;
2609  nwm->SetIndex(parts->u.widget.index);
2610  nwm->SetColour(parts->u.widget.colour);
2611  *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2612  break;
2613  }
2614 
2615  case WPT_FUNCTION: {
2616  if (*dest != NULL) return num_used;
2617  /* Ensure proper functioning even when the called code simply writes its largest index. */
2618  int biggest = -1;
2619  *dest = parts->u.func_ptr(&biggest);
2620  *biggest_index = max(*biggest_index, biggest);
2621  *fill_dest = false;
2622  break;
2623  }
2624 
2625  case WPT_RESIZE: {
2626  NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2627  if (nwrb != NULL) {
2628  assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
2629  nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
2630  }
2631  break;
2632  }
2633 
2634  case WPT_MINSIZE: {
2635  NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2636  if (nwrb != NULL) {
2637  assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
2638  nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
2639  }
2640  break;
2641  }
2642 
2643  case WPT_MINTEXTLINES: {
2644  NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2645  if (nwrb != NULL) {
2646  assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
2647  nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
2648  }
2649  break;
2650  }
2651 
2652  case WPT_FILL: {
2653  NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2654  if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
2655  break;
2656  }
2657 
2658  case WPT_DATATIP: {
2659  NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
2660  if (nwc != NULL) {
2661  nwc->widget_data = parts->u.data_tip.data;
2662  nwc->tool_tip = parts->u.data_tip.tooltip;
2663  }
2664  break;
2665  }
2666 
2667  case WPT_PADDING:
2668  if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
2669  break;
2670 
2671  case WPT_PIPSPACE: {
2672  NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
2673  if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre, parts->u.pip.inter, parts->u.pip.post);
2674 
2675  NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
2676  if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre, parts->u.pip.inter, parts->u.pip.post);
2677  break;
2678  }
2679 
2680  case WPT_SCROLLBAR: {
2681  NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
2682  if (nwc != NULL) {
2683  nwc->scrollbar_index = parts->u.widget.index;
2684  }
2685  break;
2686  }
2687 
2688  case WPT_ENDCONTAINER:
2689  return num_used;
2690 
2691  case NWID_VIEWPORT:
2692  if (*dest != NULL) return num_used;
2693  *dest = new NWidgetViewport(parts->u.widget.index);
2694  *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2695  break;
2696 
2697  case NWID_HSCROLLBAR:
2698  case NWID_VSCROLLBAR:
2699  if (*dest != NULL) return num_used;
2700  *dest = new NWidgetScrollbar(parts->type, parts->u.widget.colour, parts->u.widget.index);
2701  *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2702  break;
2703 
2704  case NWID_SELECTION: {
2705  if (*dest != NULL) return num_used;
2706  NWidgetStacked *nws = new NWidgetStacked();
2707  *dest = nws;
2708  *fill_dest = true;
2709  nws->SetIndex(parts->u.widget.index);
2710  *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2711  break;
2712  }
2713 
2714  default:
2715  if (*dest != NULL) return num_used;
2716  assert((parts->type & WWT_MASK) < WWT_LAST || (parts->type & WWT_MASK) == NWID_BUTTON_DROPDOWN);
2717  *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
2718  *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
2719  break;
2720  }
2721  num_used++;
2722  parts++;
2723  }
2724 
2725  return num_used;
2726 }
2727 
2737 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
2738 {
2739  /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
2740  * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
2741  NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
2742  NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
2743  assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
2744 
2745  int total_used = 0;
2746  for (;;) {
2747  NWidgetBase *sub_widget = NULL;
2748  bool fill_sub = false;
2749  int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
2750  parts += num_used;
2751  total_used += num_used;
2752 
2753  /* Break out of loop when end reached */
2754  if (sub_widget == NULL) break;
2755 
2756  /* If sub-widget is a container, recursively fill that container. */
2757  WidgetType tp = sub_widget->type;
2758  if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
2759  || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
2760  NWidgetBase *sub_ptr = sub_widget;
2761  int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
2762  parts += num_used;
2763  total_used += num_used;
2764  }
2765 
2766  /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
2767  if (nwid_cont != NULL) nwid_cont->Add(sub_widget);
2768  if (nwid_parent != NULL) nwid_parent->Add(sub_widget);
2769  if (nwid_cont == NULL && nwid_parent == NULL) {
2770  *parent = sub_widget;
2771  return total_used;
2772  }
2773  }
2774 
2775  if (count == total_used) return total_used; // Reached the end of the array of parts?
2776 
2777  assert(total_used < count);
2778  assert(parts->type == WPT_ENDCONTAINER);
2779  return total_used + 1; // *parts is also 'used'
2780 }
2781 
2793 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
2794 {
2795  *biggest_index = -1;
2796  if (container == NULL) container = new NWidgetVertical();
2797  NWidgetBase *cont_ptr = container;
2798  MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
2799  return container;
2800 }
2801 
2815 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
2816 {
2817  *biggest_index = -1;
2818 
2819  /* Read the first widget recursively from the array. */
2820  NWidgetBase *nwid = NULL;
2821  int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
2822  assert(nwid != NULL);
2823  parts += num_used;
2824  count -= num_used;
2825 
2826  NWidgetContainer *root = new NWidgetVertical;
2827  root->Add(nwid);
2828  if (count == 0) { // There is no body at all.
2829  *shade_select = NULL;
2830  return root;
2831  }
2832 
2833  /* If the first widget looks like a titlebar, treat it as such.
2834  * If it has a shading box, silently add a shade selection widget in the tree. */
2835  NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
2836  NWidgetContainer *body;
2837  if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
2838  *shade_select = new NWidgetStacked;
2839  root->Add(*shade_select);
2840  body = new NWidgetVertical;
2841  (*shade_select)->Add(body);
2842  } else {
2843  *shade_select = NULL;
2844  body = root;
2845  }
2846 
2847  /* Load the remaining parts into 'body'. */
2848  int biggest2 = -1;
2849  MakeNWidgets(parts, count, &biggest2, body);
2850 
2851  *biggest_index = max(*biggest_index, biggest2);
2852  return root;
2853 }
2854 
2865 NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
2866 {
2867  assert(max_length >= 1);
2868  NWidgetVertical *vert = NULL; // Storage for all rows.
2869  NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
2870  int hor_length = 0;
2871 
2872  Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
2873  sprite_size.width += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
2874  sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
2875 
2876  for (int widnum = widget_first; widnum <= widget_last; widnum++) {
2877  /* Ensure there is room in 'hor' for another button. */
2878  if (hor_length == max_length) {
2879  if (vert == NULL) vert = new NWidgetVertical();
2880  vert->Add(hor);
2881  hor = NULL;
2882  hor_length = 0;
2883  }
2884  if (hor == NULL) {
2885  hor = new NWidgetHorizontal();
2886  hor_length = 0;
2887  }
2888 
2889  NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
2890  panel->SetMinimalSize(sprite_size.width, sprite_size.height);
2891  panel->SetFill(1, 1);
2892  panel->SetResize(1, 0);
2893  panel->SetDataTip(0x0, button_tooltip);
2894  hor->Add(panel);
2895  hor_length++;
2896  }
2897  *biggest_index = widget_last;
2898  if (vert == NULL) return hor; // All buttons fit in a single row.
2899 
2900  if (hor_length > 0 && hor_length < max_length) {
2901  /* Last row is partial, add a spacer at the end to force all buttons to the left. */
2902  NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
2903  spc->SetFill(1, 1);
2904  spc->SetResize(1, 0);
2905  hor->Add(spc);
2906  }
2907  if (hor != NULL) vert->Add(hor);
2908  return vert;
2909 }