OpenTTD Source  1.11.2
widget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include "company_func.h"
12 #include "window_gui.h"
13 #include "viewport_func.h"
14 #include "zoom_func.h"
15 #include "strings_func.h"
16 #include "transparency.h"
17 #include "core/geometry_func.hpp"
18 #include "settings_type.h"
19 #include "querystring_gui.h"
20 
21 #include "table/sprites.h"
22 #include "table/strings.h"
23 #include "table/string_colours.h"
24 
25 #include "safeguards.h"
26 
36 static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bool horizontal)
37 {
38  /* Base for reversion */
39  int rev_base = top + bottom;
40  int button_size;
41  if (horizontal) {
42  button_size = NWidgetScrollbar::GetHorizontalDimension().width;
43  } else {
44  button_size = NWidgetScrollbar::GetVerticalDimension().height;
45  }
46  top += button_size; // top points to just below the up-button
47  bottom -= button_size; // bottom points to top of the down-button
48 
49  int height = (bottom - top);
50  int pos = sb->GetPosition();
51  int count = sb->GetCount();
52  int cap = sb->GetCapacity();
53 
54  if (count != 0) top += height * pos / count;
55 
56  if (cap > count) cap = count;
57  if (count != 0) bottom -= (count - pos - cap) * height / count;
58 
59  Point pt;
60  if (horizontal && _current_text_dir == TD_RTL) {
61  pt.x = rev_base - bottom;
62  pt.y = rev_base - top;
63  } else {
64  pt.x = top;
65  pt.y = bottom;
66  }
67  return pt;
68 }
69 
79 static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma)
80 {
81  int pos;
82  int button_size;
83  bool rtl = false;
84 
85  if (sb->type == NWID_HSCROLLBAR) {
86  pos = x;
87  rtl = _current_text_dir == TD_RTL;
88  button_size = NWidgetScrollbar::GetHorizontalDimension().width;
89  } else {
90  pos = y;
91  button_size = NWidgetScrollbar::GetVerticalDimension().height;
92  }
93  if (pos < mi + button_size) {
94  /* Pressing the upper button? */
96  if (_scroller_click_timeout <= 1) {
97  _scroller_click_timeout = 3;
98  sb->UpdatePosition(rtl ? 1 : -1);
99  }
100  w->mouse_capture_widget = sb->index;
101  } else if (pos >= ma - button_size) {
102  /* Pressing the lower button? */
104 
105  if (_scroller_click_timeout <= 1) {
106  _scroller_click_timeout = 3;
107  sb->UpdatePosition(rtl ? -1 : 1);
108  }
109  w->mouse_capture_widget = sb->index;
110  } else {
111  Point pt = HandleScrollbarHittest(sb, mi, ma, sb->type == NWID_HSCROLLBAR);
112 
113  if (pos < pt.x) {
114  sb->UpdatePosition(rtl ? 1 : -1, Scrollbar::SS_BIG);
115  } else if (pos > pt.y) {
116  sb->UpdatePosition(rtl ? -1 : 1, Scrollbar::SS_BIG);
117  } else {
118  _scrollbar_start_pos = pt.x - mi - button_size;
119  _scrollbar_size = ma - mi - button_size * 2;
120  w->mouse_capture_widget = sb->index;
121  _cursorpos_drag_start = _cursor.pos;
122  }
123  }
124 
125  w->SetDirty();
126 }
127 
136 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
137 {
138  int mi, ma;
139 
140  if (nw->type == NWID_HSCROLLBAR) {
141  mi = nw->pos_x;
142  ma = nw->pos_x + nw->current_x;
143  } else {
144  mi = nw->pos_y;
145  ma = nw->pos_y + nw->current_y;
146  }
147  NWidgetScrollbar *scrollbar = dynamic_cast<NWidgetScrollbar*>(nw);
148  assert(scrollbar != nullptr);
149  ScrollbarClickPositioning(w, scrollbar, x, y, mi, ma);
150 }
151 
160 int GetWidgetFromPos(const Window *w, int x, int y)
161 {
162  NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
163  return (nw != nullptr) ? nw->index : -1;
164 }
165 
175 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
176 {
177  assert(colour < COLOUR_END);
178 
179  uint dark = _colour_gradient[colour][3];
180  uint medium_dark = _colour_gradient[colour][5];
181  uint medium_light = _colour_gradient[colour][6];
182  uint light = _colour_gradient[colour][7];
183 
184  if (flags & FR_TRANSPARENT) {
185  GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR);
186  } else {
187  uint interior;
188 
189  if (flags & FR_LOWERED) {
190  GfxFillRect(left, top, left, bottom, dark);
191  GfxFillRect(left + WD_BEVEL_LEFT, top, right, top, dark);
192  GfxFillRect(right, top + WD_BEVEL_TOP, right, bottom - WD_BEVEL_BOTTOM, light);
193  GfxFillRect(left + WD_BEVEL_LEFT, bottom, right, bottom, light);
194  interior = (flags & FR_DARKENED ? medium_dark : medium_light);
195  } else {
196  GfxFillRect(left, top, left, bottom - WD_BEVEL_BOTTOM, light);
197  GfxFillRect(left + WD_BEVEL_LEFT, top, right - WD_BEVEL_RIGHT, top, light);
198  GfxFillRect(right, top, right, bottom - WD_BEVEL_BOTTOM, dark);
199  GfxFillRect(left, bottom, right, bottom, dark);
200  interior = medium_dark;
201  }
202  if (!(flags & FR_BORDERONLY)) {
203  GfxFillRect(left + WD_BEVEL_LEFT, top + WD_BEVEL_TOP, right - WD_BEVEL_RIGHT, bottom - WD_BEVEL_BOTTOM, interior);
204  }
205  }
206 }
207 
216 static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
217 {
218  assert(img != 0);
219  DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
220 
221  if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2.
222  Dimension d = GetSpriteSize(img);
223  DrawSprite(img, PAL_NONE, CenterBounds(r.left, r.right, d.width) + clicked, CenterBounds(r.top, r.bottom, d.height) + clicked);
224 }
225 
233 static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
234 {
235  if (str == STR_NULL) return;
236  if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
238  int offset = std::max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
239  DrawString(r.left + clicked, r.right + clicked, r.top + offset + clicked, str, TC_FROMSTRING, SA_HOR_CENTER);
240 }
241 
248 static inline void DrawText(const Rect &r, TextColour colour, StringID str)
249 {
251  int offset = std::max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
252  if (str != STR_NULL) DrawString(r.left, r.right, r.top + offset, str, colour);
253 }
254 
261 static inline void DrawInset(const Rect &r, Colours colour, StringID str)
262 {
263  DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
264  if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str);
265 }
266 
276 static inline void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint16 data, uint resize_x, uint resize_y)
277 {
278  DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
279 
280  int num_columns = GB(data, MAT_COL_START, MAT_COL_BITS); // Lower 8 bits of the widget data: Number of columns in the matrix.
281  int column_width; // Width of a single column in the matrix.
282  if (num_columns == 0) {
283  column_width = resize_x;
284  num_columns = (r.right - r.left + 1) / column_width;
285  } else {
286  column_width = (r.right - r.left + 1) / num_columns;
287  }
288 
289  int num_rows = GB(data, MAT_ROW_START, MAT_ROW_BITS); // Upper 8 bits of the widget data: Number of rows in the matrix.
290  int row_height; // Height of a single row in the matrix.
291  if (num_rows == 0) {
292  row_height = resize_y;
293  num_rows = (r.bottom - r.top + 1) / row_height;
294  } else {
295  row_height = (r.bottom - r.top + 1) / num_rows;
296  }
297 
298  int col = _colour_gradient[colour & 0xF][6];
299 
300  int x = r.left;
301  for (int ctr = num_columns; ctr > 1; ctr--) {
302  x += column_width;
303  GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
304  }
305 
306  x = r.top;
307  for (int ctr = num_rows; ctr > 1; ctr--) {
308  x += row_height;
309  GfxFillRect(r.left + 1, x, r.right - 1, x, col);
310  }
311 
312  col = _colour_gradient[colour & 0xF][4];
313 
314  x = r.left - 1;
315  for (int ctr = num_columns; ctr > 1; ctr--) {
316  x += column_width;
317  GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
318  }
319 
320  x = r.top - 1;
321  for (int ctr = num_rows; ctr > 1; ctr--) {
322  x += row_height;
323  GfxFillRect(r.left + 1, x, r.right - 1, x, col);
324  }
325 }
326 
336 static inline void DrawVerticalScrollbar(const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
337 {
338  int centre = (r.right - r.left) / 2;
339  int height = NWidgetScrollbar::GetVerticalDimension().height;
340 
341  /* draw up/down buttons */
342  DrawFrameRect(r.left, r.top, r.right, r.top + height - 1, colour, (up_clicked) ? FR_LOWERED : FR_NONE);
343  DrawSprite(SPR_ARROW_UP, PAL_NONE, r.left + 1 + up_clicked, r.top + 1 + up_clicked);
344 
345  DrawFrameRect(r.left, r.bottom - (height - 1), r.right, r.bottom, colour, (down_clicked) ? FR_LOWERED : FR_NONE);
346  DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.left + 1 + down_clicked, r.bottom - (height - 2) + down_clicked);
347 
348  int c1 = _colour_gradient[colour & 0xF][3];
349  int c2 = _colour_gradient[colour & 0xF][7];
350 
351  /* draw "shaded" background */
352  GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c2);
353  GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c1, FILLRECT_CHECKER);
354 
355  /* draw shaded lines */
356  GfxFillRect(r.left + centre - 3, r.top + height, r.left + centre - 3, r.bottom - height, c1);
357  GfxFillRect(r.left + centre - 2, r.top + height, r.left + centre - 2, r.bottom - height, c2);
358  GfxFillRect(r.left + centre + 2, r.top + height, r.left + centre + 2, r.bottom - height, c1);
359  GfxFillRect(r.left + centre + 3, r.top + height, r.left + centre + 3, r.bottom - height, c2);
360 
361  Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom, false);
362  DrawFrameRect(r.left, pt.x, r.right, pt.y, colour, bar_dragged ? FR_LOWERED : FR_NONE);
363 }
364 
374 static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
375 {
376  int centre = (r.bottom - r.top) / 2;
377  int width = NWidgetScrollbar::GetHorizontalDimension().width;
378 
379  DrawFrameRect(r.left, r.top, r.left + width - 1, r.bottom, colour, left_clicked ? FR_LOWERED : FR_NONE);
380  DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + left_clicked, r.top + 1 + left_clicked);
381 
382  DrawFrameRect(r.right - (width - 1), r.top, r.right, r.bottom, colour, right_clicked ? FR_LOWERED : FR_NONE);
383  DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - (width - 2) + right_clicked, r.top + 1 + right_clicked);
384 
385  int c1 = _colour_gradient[colour & 0xF][3];
386  int c2 = _colour_gradient[colour & 0xF][7];
387 
388  /* draw "shaded" background */
389  GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c2);
390  GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c1, FILLRECT_CHECKER);
391 
392  /* draw shaded lines */
393  GfxFillRect(r.left + width, r.top + centre - 3, r.right - width, r.top + centre - 3, c1);
394  GfxFillRect(r.left + width, r.top + centre - 2, r.right - width, r.top + centre - 2, c2);
395  GfxFillRect(r.left + width, r.top + centre + 2, r.right - width, r.top + centre + 2, c1);
396  GfxFillRect(r.left + width, r.top + centre + 3, r.right - width, r.top + centre + 3, c2);
397 
398  /* draw actual scrollbar */
399  Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right, true);
400  DrawFrameRect(pt.x, r.top, pt.y, r.bottom, colour, bar_dragged ? FR_LOWERED : FR_NONE);
401 }
402 
409 static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
410 {
411  int x2 = r.left; // by default the left side is the left side of the widget
412 
413  if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str);
414 
415  int c1 = _colour_gradient[colour][3];
416  int c2 = _colour_gradient[colour][7];
417 
418  /* If the frame has text, adjust the top bar to fit half-way through */
419  int dy1 = 4;
420  if (str != STR_NULL) dy1 = FONT_HEIGHT_NORMAL / 2 - 1;
421  int dy2 = dy1 + 1;
422 
423  if (_current_text_dir == TD_LTR) {
424  /* Line from upper left corner to start of text */
425  GfxFillRect(r.left, r.top + dy1, r.left + 4, r.top + dy1, c1);
426  GfxFillRect(r.left + 1, r.top + dy2, r.left + 4, r.top + dy2, c2);
427 
428  /* Line from end of text to upper right corner */
429  GfxFillRect(x2, r.top + dy1, r.right - 1, r.top + dy1, c1);
430  GfxFillRect(x2, r.top + dy2, r.right - 2, r.top + dy2, c2);
431  } else {
432  /* Line from upper left corner to start of text */
433  GfxFillRect(r.left, r.top + dy1, x2 - 2, r.top + dy1, c1);
434  GfxFillRect(r.left + 1, r.top + dy2, x2 - 2, r.top + dy2, c2);
435 
436  /* Line from end of text to upper right corner */
437  GfxFillRect(r.right - 5, r.top + dy1, r.right - 1, r.top + dy1, c1);
438  GfxFillRect(r.right - 5, r.top + dy2, r.right - 2, r.top + dy2, c2);
439  }
440 
441  /* Line from upper left corner to bottom left corner */
442  GfxFillRect(r.left, r.top + dy2, r.left, r.bottom - 1, c1);
443  GfxFillRect(r.left + 1, r.top + dy2 + 1, r.left + 1, r.bottom - 2, c2);
444 
445  /* Line from upper right corner to bottom right corner */
446  GfxFillRect(r.right - 1, r.top + dy2, r.right - 1, r.bottom - 2, c1);
447  GfxFillRect(r.right, r.top + dy1, r.right, r.bottom - 1, c2);
448 
449  GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
450  GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
451 }
452 
459 static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
460 {
461  DrawImageButtons(r, WWT_SHADEBOX, colour, clicked, clicked ? SPR_WINDOW_SHADE: SPR_WINDOW_UNSHADE);
462 }
463 
470 static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
471 {
472  DrawImageButtons(r, WWT_STICKYBOX, colour, clicked, clicked ? SPR_PIN_UP : SPR_PIN_DOWN);
473 }
474 
481 static inline void DrawDefSizeBox(const Rect &r, Colours colour, bool clicked)
482 {
483  DrawImageButtons(r, WWT_DEFSIZEBOX, colour, clicked, SPR_WINDOW_DEFSIZE);
484 }
485 
492 static inline void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
493 {
494  DrawImageButtons(r, WWT_DEBUGBOX, colour, clicked, SPR_WINDOW_DEBUG);
495 }
496 
504 static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
505 {
506  DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
507  if (at_left) {
508  Dimension d = GetSpriteSize(SPR_WINDOW_RESIZE_LEFT);
509  DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked,
510  r.bottom + 1 - WD_RESIZEBOX_BOTTOM - d.height + clicked);
511  } else {
512  Dimension d = GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT);
513  DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.right + 1 - WD_RESIZEBOX_RIGHT - d.width + clicked,
514  r.bottom + 1 - WD_RESIZEBOX_BOTTOM - d.height + clicked);
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 = std::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 = std::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 = std::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 == nullptr || !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 != nullptr);
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  AddDirtyBlock(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
778 }
779 
794 {
795  return (this->type == tp) ? this : nullptr;
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 = std::max(this->min_x, min_x);
818  this->min_y = std::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 
903 {
904  this->tool_tip = tool_tip;
905 }
906 
907 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
908 {
909  if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
910 }
911 
913 {
914  return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : nullptr;
915 }
916 
922 {
923  this->head = nullptr;
924  this->tail = nullptr;
925 }
926 
927 NWidgetContainer::~NWidgetContainer()
928 {
929  while (this->head != nullptr) {
930  NWidgetBase *wid = this->head->next;
931  delete this->head;
932  this->head = wid;
933  }
934  this->tail = nullptr;
935 }
936 
938 {
939  if (this->type == tp) return this;
940  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
941  NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
942  if (nwid != nullptr) return nwid;
943  }
944  return nullptr;
945 }
946 
952 {
953  assert(wid->next == nullptr && wid->prev == nullptr);
954 
955  if (this->head == nullptr) {
956  this->head = wid;
957  this->tail = wid;
958  } else {
959  assert(this->tail != nullptr);
960  assert(this->tail->next == nullptr);
961 
962  this->tail->next = wid;
963  wid->prev = this->tail;
964  this->tail = wid;
965  }
966 }
967 
969 {
970  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
971  child_wid->FillNestedArray(array, length);
972  }
973 }
974 
979 {
980  this->index = -1;
981 }
982 
983 void NWidgetStacked::SetIndex(int index)
984 {
985  this->index = index;
986 }
987 
988 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
989 {
990  if (this->index >= 0 && init_array) { // Fill w->nested_array[]
991  assert(w->nested_array_size > (uint)this->index);
992  w->nested_array[this->index] = this;
993  }
994 
995  /* Zero size plane selected */
996  if (this->shown_plane >= SZSP_BEGIN) {
997  Dimension size = {0, 0};
998  Dimension padding = {0, 0};
999  Dimension fill = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
1000  Dimension resize = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
1001  /* Here we're primarily interested in the value of resize */
1002  if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
1003 
1004  this->smallest_x = size.width;
1005  this->smallest_y = size.height;
1006  this->fill_x = fill.width;
1007  this->fill_y = fill.height;
1008  this->resize_x = resize.width;
1009  this->resize_y = resize.height;
1010  return;
1011  }
1012 
1013  /* First sweep, recurse down and compute minimal size and filling. */
1014  this->smallest_x = 0;
1015  this->smallest_y = 0;
1016  this->fill_x = (this->head != nullptr) ? 1 : 0;
1017  this->fill_y = (this->head != nullptr) ? 1 : 0;
1018  this->resize_x = (this->head != nullptr) ? 1 : 0;
1019  this->resize_y = (this->head != nullptr) ? 1 : 0;
1020  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1021  child_wid->SetupSmallestSize(w, init_array);
1022 
1023  this->smallest_x = std::max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
1024  this->smallest_y = std::max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
1025  this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
1026  this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
1027  this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
1028  this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
1029  }
1030 }
1031 
1032 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1033 {
1034  assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1035  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1036 
1037  if (this->shown_plane >= SZSP_BEGIN) return;
1038 
1039  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1040  uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
1041  uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
1042  uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
1043 
1044  uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
1045  uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
1046  uint child_pos_y = child_wid->padding_top;
1047 
1048  child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
1049  }
1050 }
1051 
1053 {
1054  if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
1055  NWidgetContainer::FillNestedArray(array, length);
1056 }
1057 
1059 {
1060  if (this->shown_plane >= SZSP_BEGIN) return;
1061 
1062  int plane = 0;
1063  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; plane++, child_wid = child_wid->next) {
1064  if (plane == this->shown_plane) {
1065  child_wid->Draw(w);
1066  return;
1067  }
1068  }
1069 
1070  NOT_REACHED();
1071 }
1072 
1074 {
1075  if (this->shown_plane >= SZSP_BEGIN) return nullptr;
1076 
1077  if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return nullptr;
1078  int plane = 0;
1079  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; plane++, child_wid = child_wid->next) {
1080  if (plane == this->shown_plane) {
1081  return child_wid->GetWidgetFromPos(x, y);
1082  }
1083  }
1084  return nullptr;
1085 }
1086 
1092 {
1093  this->shown_plane = plane;
1094 }
1095 
1096 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
1097 {
1098  this->flags = flags;
1099 }
1100 
1110 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
1111 {
1112  this->pip_pre = pip_pre;
1113  this->pip_inter = pip_inter;
1114  this->pip_post = pip_post;
1115 }
1116 
1118 {
1119  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1120  child_wid->Draw(w);
1121  }
1122 }
1123 
1125 {
1126  if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return nullptr;
1127 
1128  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1129  NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
1130  if (nwid != nullptr) return nwid;
1131  }
1132  return nullptr;
1133 }
1134 
1137 {
1138 }
1139 
1141 {
1142  this->smallest_x = 0; // Sum of minimal size of all children.
1143  this->smallest_y = 0; // Biggest child.
1144  this->fill_x = 0; // smallest non-zero child widget fill step.
1145  this->fill_y = 1; // smallest common child fill step.
1146  this->resize_x = 0; // smallest non-zero child widget resize step.
1147  this->resize_y = 1; // smallest common child resize step.
1148 
1149  /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1150  uint longest = 0; // Longest child found.
1151  uint max_vert_fill = 0; // Biggest vertical fill step.
1152  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1153  child_wid->SetupSmallestSize(w, init_array);
1154  longest = std::max(longest, child_wid->smallest_x);
1155  max_vert_fill = std::max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
1156  this->smallest_y = std::max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
1157  }
1158  /* 1b. Make the container higher if needed to accommodate all children nicely. */
1159 #ifdef WITH_ASSERT
1160  uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height.
1161 #endif
1162  uint cur_height = this->smallest_y;
1163  for (;;) {
1164  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1165  uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
1166  uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
1167  if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting children are not interesting.
1168  uint remainder = (cur_height - child_height) % step_size;
1169  if (remainder > 0) { // Child did not fit entirely, widen the container.
1170  cur_height += step_size - remainder;
1171  assert(cur_height < max_smallest); // Safeguard against infinite height expansion.
1172  /* Remaining children will adapt to the new cur_height, thus speeding up the computation. */
1173  }
1174  }
1175  }
1176  if (this->smallest_y == cur_height) break;
1177  this->smallest_y = cur_height; // Smallest height got changed, try again.
1178  }
1179  /* 2. For containers that must maintain equal width, extend child minimal size. */
1180  if (this->flags & NC_EQUALSIZE) {
1181  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1182  if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
1183  }
1184  }
1185  /* 3. Move PIP space to the children, compute smallest, fill, and resize values of the container. */
1186  if (this->head != nullptr) this->head->padding_left += this->pip_pre;
1187  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1188  if (child_wid->next != nullptr) {
1189  child_wid->padding_right += this->pip_inter;
1190  } else {
1191  child_wid->padding_right += this->pip_post;
1192  }
1193 
1194  this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
1195  if (child_wid->fill_x > 0) {
1196  if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
1197  }
1198  this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
1199 
1200  if (child_wid->resize_x > 0) {
1201  if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
1202  }
1203  this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
1204  }
1205  /* We need to zero the PIP settings so we can re-initialize the tree. */
1206  this->pip_pre = this->pip_inter = this->pip_post = 0;
1207 }
1208 
1209 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1210 {
1211  assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1212 
1213  /* Compute additional width given to us. */
1214  uint additional_length = given_width;
1215  if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
1216  /* For EQUALSIZE containers this does not sum to smallest_x during initialisation */
1217  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1218  additional_length -= child_wid->smallest_x + child_wid->padding_right + child_wid->padding_left;
1219  }
1220  } else {
1221  additional_length -= this->smallest_x;
1222  }
1223 
1224  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1225 
1226  /* In principle, the additional horizontal space is distributed evenly over the available resizable children. Due to step sizes, this may not always be feasible.
1227  * To make resizing work as good as possible, first children with biggest step sizes are done. These may get less due to rounding down.
1228  * 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
1229  * of the child with the smallest non-zero stepsize.
1230  *
1231  * 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
1232  * size and position, and directly call child->AssignSizePosition() with the computed values.
1233  * 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
1234  * then we call the child.
1235  */
1236 
1237  /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle vertical size for all children,
1238  * handle horizontal size for non-resizing children.
1239  */
1240  int num_changing_childs = 0; // Number of children that can change size.
1241  uint biggest_stepsize = 0;
1242  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1243  uint hor_step = child_wid->GetHorizontalStepSize(sizing);
1244  if (hor_step > 0) {
1245  num_changing_childs++;
1246  biggest_stepsize = std::max(biggest_stepsize, hor_step);
1247  } else {
1248  child_wid->current_x = child_wid->smallest_x;
1249  }
1250 
1251  uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
1252  child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
1253  }
1254 
1255  /* Second loop: Allocate the additional horizontal space over the resizing children, starting with the biggest resize steps. */
1256  while (biggest_stepsize > 0) {
1257  uint next_biggest_stepsize = 0;
1258  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1259  uint hor_step = child_wid->GetHorizontalStepSize(sizing);
1260  if (hor_step > biggest_stepsize) continue; // Already done
1261  if (hor_step == biggest_stepsize) {
1262  uint increment = additional_length / num_changing_childs;
1263  num_changing_childs--;
1264  if (hor_step > 1) increment -= increment % hor_step;
1265  child_wid->current_x = child_wid->smallest_x + increment;
1266  additional_length -= increment;
1267  continue;
1268  }
1269  next_biggest_stepsize = std::max(next_biggest_stepsize, hor_step);
1270  }
1271  biggest_stepsize = next_biggest_stepsize;
1272  }
1273  assert(num_changing_childs == 0);
1274 
1275  /* Third loop: Compute position and call the child. */
1276  uint position = rtl ? this->current_x : 0; // Place to put next child relative to origin of the container.
1277  NWidgetBase *child_wid = this->head;
1278  while (child_wid != nullptr) {
1279  uint child_width = child_wid->current_x;
1280  uint child_x = x + (rtl ? position - child_width - child_wid->padding_left : position + child_wid->padding_left);
1281  uint child_y = y + child_wid->padding_top;
1282 
1283  child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
1284  uint padded_child_width = child_width + child_wid->padding_right + child_wid->padding_left;
1285  position = rtl ? position - padded_child_width : position + padded_child_width;
1286 
1287  child_wid = child_wid->next;
1288  }
1289 }
1290 
1293 {
1294  this->type = NWID_HORIZONTAL_LTR;
1295 }
1296 
1297 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1298 {
1299  NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
1300 }
1301 
1304 {
1305 }
1306 
1308 {
1309  this->smallest_x = 0; // Biggest child.
1310  this->smallest_y = 0; // Sum of minimal size of all children.
1311  this->fill_x = 1; // smallest common child fill step.
1312  this->fill_y = 0; // smallest non-zero child widget fill step.
1313  this->resize_x = 1; // smallest common child resize step.
1314  this->resize_y = 0; // smallest non-zero child widget resize step.
1315 
1316  /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
1317  uint highest = 0; // Highest child found.
1318  uint max_hor_fill = 0; // Biggest horizontal fill step.
1319  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1320  child_wid->SetupSmallestSize(w, init_array);
1321  highest = std::max(highest, child_wid->smallest_y);
1322  max_hor_fill = std::max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
1323  this->smallest_x = std::max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
1324  }
1325  /* 1b. Make the container wider if needed to accommodate all children nicely. */
1326 #ifdef WITH_ASSERT
1327  uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height.
1328 #endif
1329  uint cur_width = this->smallest_x;
1330  for (;;) {
1331  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1332  uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
1333  uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
1334  if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting children are not interesting.
1335  uint remainder = (cur_width - child_width) % step_size;
1336  if (remainder > 0) { // Child did not fit entirely, widen the container.
1337  cur_width += step_size - remainder;
1338  assert(cur_width < max_smallest); // Safeguard against infinite width expansion.
1339  /* Remaining children will adapt to the new cur_width, thus speeding up the computation. */
1340  }
1341  }
1342  }
1343  if (this->smallest_x == cur_width) break;
1344  this->smallest_x = cur_width; // Smallest width got changed, try again.
1345  }
1346  /* 2. For containers that must maintain equal width, extend children minimal size. */
1347  if (this->flags & NC_EQUALSIZE) {
1348  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1349  if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
1350  }
1351  }
1352  /* 3. Move PIP space to the child, compute smallest, fill, and resize values of the container. */
1353  if (this->head != nullptr) this->head->padding_top += this->pip_pre;
1354  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1355  if (child_wid->next != nullptr) {
1356  child_wid->padding_bottom += this->pip_inter;
1357  } else {
1358  child_wid->padding_bottom += this->pip_post;
1359  }
1360 
1361  this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
1362  if (child_wid->fill_y > 0) {
1363  if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
1364  }
1365  this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
1366 
1367  if (child_wid->resize_y > 0) {
1368  if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
1369  }
1370  this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
1371  }
1372  /* We need to zero the PIP settings so we can re-initialize the tree. */
1373  this->pip_pre = this->pip_inter = this->pip_post = 0;
1374 }
1375 
1376 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1377 {
1378  assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1379 
1380  /* Compute additional height given to us. */
1381  uint additional_length = given_height;
1382  if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
1383  /* For EQUALSIZE containers this does not sum to smallest_y during initialisation */
1384  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1385  additional_length -= child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
1386  }
1387  } else {
1388  additional_length -= this->smallest_y;
1389  }
1390 
1391  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1392 
1393  /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the children with the biggest resize steps.
1394  * It also stores computed widths and heights into current_x and current_y values of the child.
1395  */
1396 
1397  /* 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. */
1398  int num_changing_childs = 0; // Number of children that can change size.
1399  uint biggest_stepsize = 0;
1400  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1401  uint vert_step = child_wid->GetVerticalStepSize(sizing);
1402  if (vert_step > 0) {
1403  num_changing_childs++;
1404  biggest_stepsize = std::max(biggest_stepsize, vert_step);
1405  } else {
1406  child_wid->current_y = child_wid->smallest_y;
1407  }
1408 
1409  uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
1410  child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
1411  }
1412 
1413  /* Second loop: Allocate the additional vertical space over the resizing children, starting with the biggest resize steps. */
1414  while (biggest_stepsize > 0) {
1415  uint next_biggest_stepsize = 0;
1416  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1417  uint vert_step = child_wid->GetVerticalStepSize(sizing);
1418  if (vert_step > biggest_stepsize) continue; // Already done
1419  if (vert_step == biggest_stepsize) {
1420  uint increment = additional_length / num_changing_childs;
1421  num_changing_childs--;
1422  if (vert_step > 1) increment -= increment % vert_step;
1423  child_wid->current_y = child_wid->smallest_y + increment;
1424  additional_length -= increment;
1425  continue;
1426  }
1427  next_biggest_stepsize = std::max(next_biggest_stepsize, vert_step);
1428  }
1429  biggest_stepsize = next_biggest_stepsize;
1430  }
1431  assert(num_changing_childs == 0);
1432 
1433  /* Third loop: Compute position and call the child. */
1434  uint position = 0; // Place to put next child relative to origin of the container.
1435  for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1436  uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
1437  uint child_height = child_wid->current_y;
1438 
1439  child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
1440  position += child_height + child_wid->padding_top + child_wid->padding_bottom;
1441  }
1442 }
1443 
1450 {
1451  this->SetMinimalSize(length, height);
1452  this->SetResize(0, 0);
1453 }
1454 
1455 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
1456 {
1457  this->smallest_x = this->min_x;
1458  this->smallest_y = this->min_y;
1459 }
1460 
1462 {
1463 }
1464 
1466 {
1467  /* Spacer widget is never visible. */
1468 }
1469 
1470 void NWidgetSpacer::SetDirty(const Window *w) const
1471 {
1472  /* Spacer widget never need repainting. */
1473 }
1474 
1476 {
1477  return nullptr;
1478 }
1479 
1480 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX, NC_EQUALSIZE), index(-1), clicked(-1), count(-1)
1481 {
1482 }
1483 
1484 void NWidgetMatrix::SetIndex(int index)
1485 {
1486  this->index = index;
1487 }
1488 
1489 void NWidgetMatrix::SetColour(Colours colour)
1490 {
1491  this->colour = colour;
1492 }
1493 
1498 void NWidgetMatrix::SetClicked(int clicked)
1499 {
1500  this->clicked = clicked;
1501  if (this->clicked >= 0 && this->sb != nullptr && this->widgets_x != 0) {
1502  int vpos = (this->clicked / this->widgets_x) * this->widget_h; // Vertical position of the top.
1503  /* Need to scroll down -> Scroll to the bottom.
1504  * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
1505  if (this->sb->GetPosition() < vpos) vpos += this->widget_h - this->pip_inter - 1;
1506  this->sb->ScrollTowards(vpos);
1507  }
1508 }
1509 
1516 {
1517  this->count = count;
1518 
1519  if (this->sb == nullptr || this->widgets_x == 0) return;
1520 
1521  /* We need to get the number of pixels the matrix is high/wide.
1522  * So, determine the number of rows/columns based on the number of
1523  * columns/rows (one is constant/unscrollable).
1524  * Then multiply that by the height of a widget, and add the pre
1525  * and post spacing "offsets". */
1526  count = CeilDiv(count, this->sb->IsVertical() ? this->widgets_x : this->widgets_y);
1527  count *= (this->sb->IsVertical() ? this->head->smallest_y : this->head->smallest_x) + this->pip_inter;
1528  if (count > 0) count -= this->pip_inter; // We counted an inter too much in the multiplication above
1529  count += this->pip_pre + this->pip_post;
1530  this->sb->SetCount(count);
1531  this->sb->SetCapacity(this->sb->IsVertical() ? this->current_y : this->current_x);
1532  this->sb->SetStepSize(this->sb->IsVertical() ? this->widget_h : this->widget_w);
1533 }
1534 
1540 {
1541  this->sb = sb;
1542 }
1543 
1544 void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array)
1545 {
1546  assert(this->head != nullptr);
1547  assert(this->head->next == nullptr);
1548 
1549  if (this->index >= 0 && init_array) { // Fill w->nested_array[]
1550  assert(w->nested_array_size > (uint)this->index);
1551  w->nested_array[this->index] = this;
1552  }
1553 
1554  /* Reset the widget number. */
1555  NWidgetCore *nw = dynamic_cast<NWidgetCore *>(this->head);
1556  assert(nw != nullptr);
1557  SB(nw->index, 16, 16, 0);
1558  this->head->SetupSmallestSize(w, init_array);
1559 
1560  Dimension padding = { (uint)this->pip_pre + this->pip_post, (uint)this->pip_pre + this->pip_post};
1561  Dimension size = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height};
1562  Dimension fill = {0, 0};
1563  Dimension resize = {this->pip_inter + this->head->smallest_x, this->pip_inter + this->head->smallest_y};
1564 
1565  if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
1566 
1567  this->smallest_x = size.width;
1568  this->smallest_y = size.height;
1569  this->fill_x = fill.width;
1570  this->fill_y = fill.height;
1571  this->resize_x = resize.width;
1572  this->resize_y = resize.height;
1573 }
1574 
1575 void NWidgetMatrix::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1576 {
1577  assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
1578 
1579  this->pos_x = x;
1580  this->pos_y = y;
1581  this->current_x = given_width;
1582  this->current_y = given_height;
1583 
1584  /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
1585  this->widget_w = this->head->smallest_x + this->pip_inter;
1586  this->widget_h = this->head->smallest_y + this->pip_inter;
1587 
1588  /* Account for the pip_inter is between widgets, so we need to account for that when
1589  * the division assumes pip_inter is used for all widgets. */
1590  this->widgets_x = CeilDiv(this->current_x - this->pip_pre - this->pip_post + this->pip_inter, this->widget_w);
1591  this->widgets_y = CeilDiv(this->current_y - this->pip_pre - this->pip_post + this->pip_inter, this->widget_h);
1592 
1593  /* When resizing, update the scrollbar's count. E.g. with a vertical
1594  * scrollbar becoming wider or narrower means the amount of rows in
1595  * the scrollbar becomes respectively smaller or higher. */
1596  this->SetCount(this->count);
1597 }
1598 
1600 {
1601  if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
1602  NWidgetContainer::FillNestedArray(array, length);
1603 }
1604 
1606 {
1607  /* Falls outside of the matrix widget. */
1608  if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return nullptr;
1609 
1610  int start_x, start_y, base_offs_x, base_offs_y;
1611  this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
1612 
1613  bool rtl = _current_text_dir == TD_RTL;
1614 
1615  int widget_col = (rtl ?
1616  -x + (int)this->pip_post + (int)this->pos_x + base_offs_x + (int)this->widget_w - 1 - (int)this->pip_inter :
1617  x - (int)this->pip_pre - (int)this->pos_x - base_offs_x
1618  ) / this->widget_w;
1619 
1620  int widget_row = (y - base_offs_y - (int)this->pip_pre - (int)this->pos_y) / this->widget_h;
1621 
1622  int sub_wid = (widget_row + start_y) * this->widgets_x + start_x + widget_col;
1623  if (sub_wid >= this->count) return nullptr;
1624 
1625  NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
1626  assert(child != nullptr);
1628  this->pos_x + (rtl ? this->pip_post - widget_col * this->widget_w : this->pip_pre + widget_col * this->widget_w) + base_offs_x,
1629  this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y,
1630  child->smallest_x, child->smallest_y, rtl);
1631 
1632  SB(child->index, 16, 16, sub_wid);
1633 
1634  return child->GetWidgetFromPos(x, y);
1635 }
1636 
1637 /* virtual */ void NWidgetMatrix::Draw(const Window *w)
1638 {
1639  /* Fill the background. */
1640  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]);
1641 
1642  /* Set up a clipping area for the previews. */
1643  bool rtl = _current_text_dir == TD_RTL;
1644  DrawPixelInfo tmp_dpi;
1645  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;
1646  DrawPixelInfo *old_dpi = _cur_dpi;
1647  _cur_dpi = &tmp_dpi;
1648 
1649  /* Get the appropriate offsets so we can draw the right widgets. */
1650  NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
1651  assert(child != nullptr);
1652  int start_x, start_y, base_offs_x, base_offs_y;
1653  this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
1654 
1655  int offs_y = base_offs_y;
1656  for (int y = start_y; y < start_y + this->widgets_y + 1; y++, offs_y += this->widget_h) {
1657  /* Are we within bounds? */
1658  if (offs_y + child->smallest_y <= 0) continue;
1659  if (offs_y >= (int)this->current_y) break;
1660 
1661  /* We've passed our amount of widgets. */
1662  if (y * this->widgets_x >= this->count) break;
1663 
1664  int offs_x = base_offs_x;
1665  for (int x = start_x; x < start_x + this->widgets_x + 1; x++, offs_x += rtl ? -this->widget_w : this->widget_w) {
1666  /* Are we within bounds? */
1667  if (offs_x + child->smallest_x <= 0) continue;
1668  if (offs_x >= (int)this->current_x) continue;
1669 
1670  /* Do we have this many widgets? */
1671  int sub_wid = y * this->widgets_x + x;
1672  if (sub_wid >= this->count) break;
1673 
1674  child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl);
1675  child->SetLowered(this->clicked == sub_wid);
1676  SB(child->index, 16, 16, sub_wid);
1677  child->Draw(w);
1678  }
1679  }
1680 
1681  /* Restore the clipping area. */
1682  _cur_dpi = old_dpi;
1683 }
1684 
1692 void NWidgetMatrix::GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
1693 {
1694  base_offs_x = _current_text_dir == TD_RTL ? this->widget_w * (this->widgets_x - 1) : 0;
1695  base_offs_y = 0;
1696  start_x = 0;
1697  start_y = 0;
1698  if (this->sb != nullptr) {
1699  if (this->sb->IsVertical()) {
1700  start_y = this->sb->GetPosition() / this->widget_h;
1701  base_offs_y += -this->sb->GetPosition() + start_y * this->widget_h;
1702  } else {
1703  start_x = this->sb->GetPosition() / this->widget_w;
1704  int sub_x = this->sb->GetPosition() - start_x * this->widget_w;
1705  if (_current_text_dir == TD_RTL) {
1706  base_offs_x += sub_x;
1707  } else {
1708  base_offs_x -= sub_x;
1709  }
1710  }
1711  }
1712 }
1713 
1723 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
1724 {
1725  assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
1726  if (index >= 0) this->SetIndex(index);
1727  this->child = child;
1728 }
1729 
1730 NWidgetBackground::~NWidgetBackground()
1731 {
1732  if (this->child != nullptr) delete this->child;
1733 }
1734 
1743 {
1744  if (this->child == nullptr) {
1745  this->child = new NWidgetVertical();
1746  }
1747  this->child->Add(nwid);
1748 }
1749 
1760 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
1761 {
1762  if (this->child == nullptr) {
1763  this->child = new NWidgetVertical();
1764  }
1765  this->child->SetPIP(pip_pre, pip_inter, pip_post);
1766 }
1767 
1769 {
1770  if (init_array && this->index >= 0) {
1771  assert(w->nested_array_size > (uint)this->index);
1772  w->nested_array[this->index] = this;
1773  }
1774  if (this->child != nullptr) {
1775  this->child->SetupSmallestSize(w, init_array);
1776 
1777  this->smallest_x = this->child->smallest_x;
1778  this->smallest_y = this->child->smallest_y;
1779  this->fill_x = this->child->fill_x;
1780  this->fill_y = this->child->fill_y;
1781  this->resize_x = this->child->resize_x;
1782  this->resize_y = this->child->resize_y;
1783 
1784  /* Account for the size of the frame's text if that exists */
1785  if (w != nullptr && this->type == WWT_FRAME) {
1788  this->child->padding_top = std::max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
1790 
1791  this->smallest_x += this->child->padding_left + this->child->padding_right;
1792  this->smallest_y += this->child->padding_top + this->child->padding_bottom;
1793 
1794  if (this->index >= 0) w->SetStringParameters(this->index);
1796  }
1797  } else {
1798  Dimension d = {this->min_x, this->min_y};
1799  Dimension fill = {this->fill_x, this->fill_y};
1800  Dimension resize = {this->resize_x, this->resize_y};
1801  if (w != nullptr) { // A non-nullptr window pointer acts as switch to turn dynamic widget size on.
1802  if (this->type == WWT_FRAME || this->type == WWT_INSET) {
1803  if (this->index >= 0) w->SetStringParameters(this->index);
1804  Dimension background = GetStringBoundingBox(this->widget_data);
1805  background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
1806  d = maxdim(d, background);
1807  }
1808  if (this->index >= 0) {
1809  static const Dimension padding = {0, 0};
1810  w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
1811  }
1812  }
1813  this->smallest_x = d.width;
1814  this->smallest_y = d.height;
1815  this->fill_x = fill.width;
1816  this->fill_y = fill.height;
1817  this->resize_x = resize.width;
1818  this->resize_y = resize.height;
1819  }
1820 }
1821 
1822 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1823 {
1824  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1825 
1826  if (this->child != nullptr) {
1827  uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
1828  uint width = given_width - this->child->padding_right - this->child->padding_left;
1829  uint height = given_height - this->child->padding_top - this->child->padding_bottom;
1830  this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
1831  }
1832 }
1833 
1835 {
1836  if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
1837  if (this->child != nullptr) this->child->FillNestedArray(array, length);
1838 }
1839 
1841 {
1842  if (this->current_x == 0 || this->current_y == 0) return;
1843 
1844  Rect r;
1845  r.left = this->pos_x;
1846  r.right = this->pos_x + this->current_x - 1;
1847  r.top = this->pos_y;
1848  r.bottom = this->pos_y + this->current_y - 1;
1849 
1850  const DrawPixelInfo *dpi = _cur_dpi;
1851  if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
1852 
1853  switch (this->type) {
1854  case WWT_PANEL:
1855  assert(this->widget_data == 0);
1856  DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
1857  break;
1858 
1859  case WWT_FRAME:
1860  if (this->index >= 0) w->SetStringParameters(this->index);
1861  DrawFrame(r, this->colour, this->widget_data);
1862  break;
1863 
1864  case WWT_INSET:
1865  if (this->index >= 0) w->SetStringParameters(this->index);
1866  DrawInset(r, this->colour, this->widget_data);
1867  break;
1868 
1869  default:
1870  NOT_REACHED();
1871  }
1872 
1873  if (this->index >= 0) w->DrawWidget(r, this->index);
1874  if (this->child != nullptr) this->child->Draw(w);
1875 
1876  if (this->IsDisabled()) {
1877  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
1878  }
1879 }
1880 
1882 {
1883  NWidgetCore *nwid = nullptr;
1884  if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
1885  if (this->child != nullptr) nwid = this->child->GetWidgetFromPos(x, y);
1886  if (nwid == nullptr) nwid = this;
1887  }
1888  return nwid;
1889 }
1890 
1892 {
1893  NWidgetBase *nwid = nullptr;
1894  if (this->child != nullptr) nwid = this->child->GetWidgetOfType(tp);
1895  if (nwid == nullptr && this->type == tp) nwid = this;
1896  return nwid;
1897 }
1898 
1899 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
1900 {
1901  this->SetIndex(index);
1902 }
1903 
1905 {
1906  if (init_array && this->index >= 0) {
1907  assert(w->nested_array_size > (uint)this->index);
1908  w->nested_array[this->index] = this;
1909  }
1910  this->smallest_x = this->min_x;
1911  this->smallest_y = this->min_y;
1912 }
1913 
1915 {
1916  if (this->current_x == 0 || this->current_y == 0) return;
1917 
1918  if (this->disp_flags & ND_NO_TRANSPARENCY) {
1920  _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_LOADING); // Disable all transparency, except textual stuff
1921  w->DrawViewport();
1922  _transparency_opt = to_backup;
1923  } else {
1924  w->DrawViewport();
1925  }
1926 
1927  /* Optionally shade the viewport. */
1928  if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
1929  GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
1931  }
1932 }
1933 
1940 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
1941 {
1942  InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
1943 }
1944 
1950 {
1951  Viewport *vp = w->viewport;
1952  if (vp != nullptr) {
1953  vp->left = w->left + this->pos_x;
1954  vp->top = w->top + this->pos_y;
1955  vp->width = this->current_x;
1956  vp->height = this->current_y;
1957 
1958  vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
1959  vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
1960  }
1961 }
1962 
1972 int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
1973 {
1974  uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
1975  if (pos != INT_MAX) pos += this->GetPosition();
1976  return (pos >= this->GetCount()) ? INT_MAX : pos;
1977 }
1978 
1986 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
1987 {
1988  NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
1989  if (this->IsVertical()) {
1990  this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
1991  } else {
1992  this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
1993  }
1994 }
1995 
2002 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL), Scrollbar(tp != NWID_HSCROLLBAR)
2003 {
2004  assert(tp == NWID_HSCROLLBAR || tp == NWID_VSCROLLBAR);
2005  this->SetIndex(index);
2006 }
2007 
2009 {
2010  if (init_array && this->index >= 0) {
2011  assert(w->nested_array_size > (uint)this->index);
2012  w->nested_array[this->index] = this;
2013  }
2014  this->min_x = 0;
2015  this->min_y = 0;
2016 
2017  switch (this->type) {
2018  case NWID_HSCROLLBAR:
2019  this->SetMinimalSize(NWidgetScrollbar::GetHorizontalDimension().width * 3, NWidgetScrollbar::GetHorizontalDimension().height);
2020  this->SetResize(1, 0);
2021  this->SetFill(1, 0);
2022  this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
2023  break;
2024 
2025  case NWID_VSCROLLBAR:
2026  this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width, NWidgetScrollbar::GetVerticalDimension().height * 3);
2027  this->SetResize(0, 1);
2028  this->SetFill(0, 1);
2029  this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
2030  break;
2031 
2032  default: NOT_REACHED();
2033  }
2034 
2035  this->smallest_x = this->min_x;
2036  this->smallest_y = this->min_y;
2037 }
2038 
2040 {
2041  if (this->current_x == 0 || this->current_y == 0) return;
2042 
2043  Rect r;
2044  r.left = this->pos_x;
2045  r.right = this->pos_x + this->current_x - 1;
2046  r.top = this->pos_y;
2047  r.bottom = this->pos_y + this->current_y - 1;
2048 
2049  const DrawPixelInfo *dpi = _cur_dpi;
2050  if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
2051 
2052  bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
2053  bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
2054  bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->mouse_capture_widget == this->index;
2055 
2056  if (this->type == NWID_HSCROLLBAR) {
2057  DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
2058  } else {
2059  DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
2060  }
2061 
2062  if (this->IsDisabled()) {
2063  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
2064  }
2065 }
2066 
2067 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
2068 {
2069  vertical_dimension.width = vertical_dimension.height = 0;
2070  horizontal_dimension.width = horizontal_dimension.height = 0;
2071 }
2072 
2073 /* static */ Dimension NWidgetScrollbar::GetVerticalDimension()
2074 {
2076  if (vertical_dimension.width == 0) {
2077  vertical_dimension = maxdim(GetSpriteSize(SPR_ARROW_UP), GetSpriteSize(SPR_ARROW_DOWN));
2078  vertical_dimension.width += extra.width;
2079  vertical_dimension.height += extra.height;
2080  }
2081  return vertical_dimension;
2082 }
2083 
2084 /* static */ Dimension NWidgetScrollbar::GetHorizontalDimension()
2085 {
2087  if (horizontal_dimension.width == 0) {
2088  horizontal_dimension = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
2089  horizontal_dimension.width += extra.width;
2090  horizontal_dimension.height += extra.height;
2091  }
2092  return horizontal_dimension;
2093 }
2094 
2097 
2100 {
2101  shadebox_dimension.width = shadebox_dimension.height = 0;
2102  debugbox_dimension.width = debugbox_dimension.height = 0;
2103  defsizebox_dimension.width = defsizebox_dimension.height = 0;
2104  stickybox_dimension.width = stickybox_dimension.height = 0;
2105  resizebox_dimension.width = resizebox_dimension.height = 0;
2106  closebox_dimension.width = closebox_dimension.height = 0;
2107  dropdown_dimension.width = dropdown_dimension.height = 0;
2108 }
2109 
2117 
2126 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
2127 {
2128  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);
2129  if (index >= 0) this->SetIndex(index);
2130  this->min_x = 0;
2131  this->min_y = 0;
2132  this->SetResize(0, 0);
2133 
2134  switch (tp) {
2135  case WWT_EMPTY:
2136  break;
2137 
2138  case WWT_PUSHBTN:
2139  case WWT_IMGBTN:
2140  case WWT_PUSHIMGBTN:
2141  case WWT_IMGBTN_2:
2142  case WWT_TEXTBTN:
2143  case WWT_PUSHTXTBTN:
2144  case WWT_TEXTBTN_2:
2145  case WWT_LABEL:
2146  case WWT_TEXT:
2147  case WWT_MATRIX:
2148  case NWID_BUTTON_DROPDOWN:
2149  case NWID_PUSHBUTTON_DROPDOWN:
2150  case WWT_ARROWBTN:
2151  case WWT_PUSHARROWBTN:
2152  this->SetFill(0, 0);
2153  break;
2154 
2155  case WWT_EDITBOX:
2156  this->SetFill(0, 0);
2157  break;
2158 
2159  case WWT_CAPTION:
2160  this->SetFill(1, 0);
2161  this->SetResize(1, 0);
2162  this->min_y = WD_CAPTION_HEIGHT;
2163  this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
2164  break;
2165 
2166  case WWT_STICKYBOX:
2167  this->SetFill(0, 0);
2169  this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
2170  break;
2171 
2172  case WWT_SHADEBOX:
2173  this->SetFill(0, 0);
2175  this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
2176  break;
2177 
2178  case WWT_DEBUGBOX:
2179  this->SetFill(0, 0);
2181  this->SetDataTip(STR_NULL, STR_TOOLTIP_DEBUG);
2182  break;
2183 
2184  case WWT_DEFSIZEBOX:
2185  this->SetFill(0, 0);
2187  this->SetDataTip(STR_NULL, STR_TOOLTIP_DEFSIZE);
2188  break;
2189 
2190  case WWT_RESIZEBOX:
2191  this->SetFill(0, 0);
2193  this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
2194  break;
2195 
2196  case WWT_CLOSEBOX:
2197  this->SetFill(0, 0);
2199  this->SetDataTip(STR_NULL, STR_TOOLTIP_CLOSE_WINDOW);
2200  break;
2201 
2202  case WWT_DROPDOWN:
2203  this->SetFill(0, 0);
2204  this->min_y = WD_DROPDOWN_HEIGHT;
2205  break;
2206 
2207  default:
2208  NOT_REACHED();
2209  }
2210 }
2211 
2212 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
2213 {
2214  if (this->index >= 0 && init_array) { // Fill w->nested_array[]
2215  assert(w->nested_array_size > (uint)this->index);
2216  w->nested_array[this->index] = this;
2217  }
2218 
2219  Dimension size = {this->min_x, this->min_y};
2220  Dimension fill = {this->fill_x, this->fill_y};
2221  Dimension resize = {this->resize_x, this->resize_y};
2222  /* Get padding, and update size with the real content size if appropriate. */
2223  const Dimension *padding = nullptr;
2224  switch (this->type) {
2225  case WWT_EMPTY: {
2226  static const Dimension extra = {0, 0};
2227  padding = &extra;
2228  break;
2229  }
2230  case WWT_MATRIX: {
2232  padding = &extra;
2233  break;
2234  }
2235  case WWT_SHADEBOX: {
2237  padding = &extra;
2238  if (NWidgetLeaf::shadebox_dimension.width == 0) {
2239  NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
2240  NWidgetLeaf::shadebox_dimension.width += extra.width;
2241  NWidgetLeaf::shadebox_dimension.height += extra.height;
2242  }
2244  break;
2245  }
2246  case WWT_DEBUGBOX:
2249  padding = &extra;
2250  if (NWidgetLeaf::debugbox_dimension.width == 0) {
2251  NWidgetLeaf::debugbox_dimension = GetSpriteSize(SPR_WINDOW_DEBUG);
2252  NWidgetLeaf::debugbox_dimension.width += extra.width;
2253  NWidgetLeaf::debugbox_dimension.height += extra.height;
2254  }
2256  } else {
2257  /* If the setting is disabled we don't want to see it! */
2258  size.width = 0;
2259  fill.width = 0;
2260  resize.width = 0;
2261  }
2262  break;
2263 
2264  case WWT_STICKYBOX: {
2266  padding = &extra;
2267  if (NWidgetLeaf::stickybox_dimension.width == 0) {
2269  NWidgetLeaf::stickybox_dimension.width += extra.width;
2270  NWidgetLeaf::stickybox_dimension.height += extra.height;
2271  }
2273  break;
2274  }
2275 
2276  case WWT_DEFSIZEBOX: {
2278  padding = &extra;
2279  if (NWidgetLeaf::defsizebox_dimension.width == 0) {
2280  NWidgetLeaf::defsizebox_dimension = GetSpriteSize(SPR_WINDOW_DEFSIZE);
2281  NWidgetLeaf::defsizebox_dimension.width += extra.width;
2282  NWidgetLeaf::defsizebox_dimension.height += extra.height;
2283  }
2285  break;
2286  }
2287 
2288  case WWT_RESIZEBOX: {
2290  padding = &extra;
2291  if (NWidgetLeaf::resizebox_dimension.width == 0) {
2292  NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
2293  NWidgetLeaf::resizebox_dimension.width += extra.width;
2294  NWidgetLeaf::resizebox_dimension.height += extra.height;
2295  }
2297  break;
2298  }
2299  case WWT_EDITBOX: {
2300  Dimension sprite_size = GetSpriteSize(_current_text_dir == TD_RTL ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
2301  size.width = std::max(size.width, 30 + sprite_size.width);
2302  size.height = std::max(sprite_size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
2303  }
2304  FALLTHROUGH;
2305  case WWT_PUSHBTN: {
2307  padding = &extra;
2308  break;
2309  }
2310  case WWT_IMGBTN:
2311  case WWT_IMGBTN_2:
2312  case WWT_PUSHIMGBTN: {
2314  padding = &extra;
2315  Dimension d2 = GetSpriteSize(this->widget_data);
2316  if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
2317  d2.width += extra.width;
2318  d2.height += extra.height;
2319  size = maxdim(size, d2);
2320  break;
2321  }
2322  case WWT_ARROWBTN:
2323  case WWT_PUSHARROWBTN: {
2325  padding = &extra;
2326  Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
2327  d2.width += extra.width;
2328  d2.height += extra.height;
2329  size = maxdim(size, d2);
2330  break;
2331  }
2332 
2333  case WWT_CLOSEBOX: {
2335  padding = &extra;
2336  if (NWidgetLeaf::closebox_dimension.width == 0) {
2338  NWidgetLeaf::closebox_dimension.width += extra.width;
2339  NWidgetLeaf::closebox_dimension.height += extra.height;
2340  }
2342  break;
2343  }
2344  case WWT_TEXTBTN:
2345  case WWT_PUSHTXTBTN:
2346  case WWT_TEXTBTN_2: {
2348  padding = &extra;
2349  if (this->index >= 0) w->SetStringParameters(this->index);
2351  d2.width += extra.width;
2352  d2.height += extra.height;
2353  size = maxdim(size, d2);
2354  break;
2355  }
2356  case WWT_LABEL:
2357  case WWT_TEXT: {
2358  static const Dimension extra = {0, 0};
2359  padding = &extra;
2360  if (this->index >= 0) w->SetStringParameters(this->index);
2361  size = maxdim(size, GetStringBoundingBox(this->widget_data));
2362  break;
2363  }
2364  case WWT_CAPTION: {
2366  padding = &extra;
2367  if (this->index >= 0) w->SetStringParameters(this->index);
2369  d2.width += extra.width;
2370  d2.height += extra.height;
2371  size = maxdim(size, d2);
2372  break;
2373  }
2374  case WWT_DROPDOWN:
2375  case NWID_BUTTON_DROPDOWN:
2376  case NWID_PUSHBUTTON_DROPDOWN: {
2378  padding = &extra;
2379  if (NWidgetLeaf::dropdown_dimension.width == 0) {
2384  }
2385  if (this->index >= 0) w->SetStringParameters(this->index);
2387  d2.width += extra.width;
2388  d2.height = std::max(d2.height, NWidgetLeaf::dropdown_dimension.height) + extra.height;
2389  size = maxdim(size, d2);
2390  break;
2391  }
2392  default:
2393  NOT_REACHED();
2394  }
2395 
2396  if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
2397 
2398  this->smallest_x = size.width;
2399  this->smallest_y = size.height;
2400  this->fill_x = fill.width;
2401  this->fill_y = fill.height;
2402  this->resize_x = resize.width;
2403  this->resize_y = resize.height;
2404 }
2405 
2407 {
2408  if (this->current_x == 0 || this->current_y == 0) return;
2409 
2410  /* Setup a clipping rectangle... */
2411  DrawPixelInfo new_dpi;
2412  if (!FillDrawPixelInfo(&new_dpi, this->pos_x, this->pos_y, this->current_x, this->current_y)) return;
2413  /* ...but keep coordinates relative to the window. */
2414  new_dpi.left += this->pos_x;
2415  new_dpi.top += this->pos_y;
2416 
2417  DrawPixelInfo *old_dpi = _cur_dpi;
2418  _cur_dpi = &new_dpi;
2419 
2420  Rect r;
2421  r.left = this->pos_x;
2422  r.right = this->pos_x + this->current_x - 1;
2423  r.top = this->pos_y;
2424  r.bottom = this->pos_y + this->current_y - 1;
2425 
2426  bool clicked = this->IsLowered();
2427  switch (this->type) {
2428  case WWT_EMPTY:
2429  break;
2430 
2431  case WWT_PUSHBTN:
2432  assert(this->widget_data == 0);
2433  DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
2434  break;
2435 
2436  case WWT_IMGBTN:
2437  case WWT_PUSHIMGBTN:
2438  case WWT_IMGBTN_2:
2439  DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
2440  break;
2441 
2442  case WWT_TEXTBTN:
2443  case WWT_PUSHTXTBTN:
2444  case WWT_TEXTBTN_2:
2445  if (this->index >= 0) w->SetStringParameters(this->index);
2446  DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
2447  DrawLabel(r, this->type, clicked, this->widget_data);
2448  break;
2449 
2450  case WWT_ARROWBTN:
2451  case WWT_PUSHARROWBTN: {
2452  SpriteID sprite;
2453  switch (this->widget_data) {
2454  case AWV_DECREASE: sprite = _current_text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
2455  case AWV_INCREASE: sprite = _current_text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
2456  case AWV_LEFT: sprite = SPR_ARROW_LEFT; break;
2457  case AWV_RIGHT: sprite = SPR_ARROW_RIGHT; break;
2458  default: NOT_REACHED();
2459  }
2460  DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
2461  break;
2462  }
2463 
2464  case WWT_LABEL:
2465  if (this->index >= 0) w->SetStringParameters(this->index);
2466  DrawLabel(r, this->type, clicked, this->widget_data);
2467  break;
2468 
2469  case WWT_TEXT:
2470  if (this->index >= 0) w->SetStringParameters(this->index);
2471  DrawText(r, (TextColour)this->colour, this->widget_data);
2472  break;
2473 
2474  case WWT_MATRIX:
2475  DrawMatrix(r, this->colour, clicked, this->widget_data, this->resize_x, this->resize_y);
2476  break;
2477 
2478  case WWT_EDITBOX: {
2479  const QueryString *query = w->GetQueryString(this->index);
2480  if (query != nullptr) query->DrawEditBox(w, this->index);
2481  break;
2482  }
2483 
2484  case WWT_CAPTION:
2485  if (this->index >= 0) w->SetStringParameters(this->index);
2486  DrawCaption(r, this->colour, w->owner, this->widget_data);
2487  break;
2488 
2489  case WWT_SHADEBOX:
2490  assert(this->widget_data == 0);
2491  DrawShadeBox(r, this->colour, w->IsShaded());
2492  break;
2493 
2494  case WWT_DEBUGBOX:
2495  DrawDebugBox(r, this->colour, clicked);
2496  break;
2497 
2498  case WWT_STICKYBOX:
2499  assert(this->widget_data == 0);
2500  DrawStickyBox(r, this->colour, !!(w->flags & WF_STICKY));
2501  break;
2502 
2503  case WWT_DEFSIZEBOX:
2504  assert(this->widget_data == 0);
2505  DrawDefSizeBox(r, this->colour, clicked);
2506  break;
2507 
2508  case WWT_RESIZEBOX:
2509  assert(this->widget_data == 0);
2510  DrawResizeBox(r, this->colour, this->pos_x < (w->width / 2), !!(w->flags & WF_SIZING));
2511  break;
2512 
2513  case WWT_CLOSEBOX:
2514  DrawCloseBox(r, this->colour);
2515  break;
2516 
2517  case WWT_DROPDOWN:
2518  if (this->index >= 0) w->SetStringParameters(this->index);
2519  DrawDropdown(r, this->colour, clicked, this->widget_data);
2520  break;
2521 
2522  case NWID_BUTTON_DROPDOWN:
2523  case NWID_PUSHBUTTON_DROPDOWN:
2524  if (this->index >= 0) w->SetStringParameters(this->index);
2525  DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
2526  break;
2527 
2528  default:
2529  NOT_REACHED();
2530  }
2531  if (this->index >= 0) w->DrawWidget(r, this->index);
2532 
2533  if (this->IsDisabled()) {
2534  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
2535  }
2536 
2537  _cur_dpi = old_dpi;
2538 }
2539 
2548 {
2549  if (_current_text_dir == TD_LTR) {
2550  int button_width = this->pos_x + this->current_x - NWidgetLeaf::dropdown_dimension.width;
2551  return pt.x < button_width;
2552  } else {
2553  int button_left = this->pos_x + NWidgetLeaf::dropdown_dimension.width;
2554  return pt.x >= button_left;
2555  }
2556 }
2557 
2558 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
2559 
2575 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
2576 {
2577  int num_used = 0;
2578 
2579  *dest = nullptr;
2580  *fill_dest = false;
2581 
2582  while (count > num_used) {
2583  switch (parts->type) {
2584  case NWID_SPACER:
2585  if (*dest != nullptr) return num_used;
2586  *dest = new NWidgetSpacer(0, 0);
2587  break;
2588 
2589  case NWID_HORIZONTAL:
2590  if (*dest != nullptr) return num_used;
2591  *dest = new NWidgetHorizontal(parts->u.cont_flags);
2592  *fill_dest = true;
2593  break;
2594 
2595  case NWID_HORIZONTAL_LTR:
2596  if (*dest != nullptr) return num_used;
2597  *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
2598  *fill_dest = true;
2599  break;
2600 
2601  case WWT_PANEL:
2602  case WWT_INSET:
2603  case WWT_FRAME:
2604  if (*dest != nullptr) return num_used;
2605  *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
2606  *biggest_index = std::max(*biggest_index, (int)parts->u.widget.index);
2607  *fill_dest = true;
2608  break;
2609 
2610  case NWID_VERTICAL:
2611  if (*dest != nullptr) return num_used;
2612  *dest = new NWidgetVertical(parts->u.cont_flags);
2613  *fill_dest = true;
2614  break;
2615 
2616  case NWID_MATRIX: {
2617  if (*dest != nullptr) return num_used;
2618  NWidgetMatrix *nwm = new NWidgetMatrix();
2619  *dest = nwm;
2620  *fill_dest = true;
2621  nwm->SetIndex(parts->u.widget.index);
2622  nwm->SetColour(parts->u.widget.colour);
2623  *biggest_index = std::max(*biggest_index, (int)parts->u.widget.index);
2624  break;
2625  }
2626 
2627  case WPT_FUNCTION: {
2628  if (*dest != nullptr) return num_used;
2629  /* Ensure proper functioning even when the called code simply writes its largest index. */
2630  int biggest = -1;
2631  *dest = parts->u.func_ptr(&biggest);
2632  *biggest_index = std::max(*biggest_index, biggest);
2633  *fill_dest = false;
2634  break;
2635  }
2636 
2637  case WPT_RESIZE: {
2638  NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2639  if (nwrb != nullptr) {
2640  assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
2641  nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
2642  }
2643  break;
2644  }
2645 
2646  case WPT_MINSIZE: {
2647  NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2648  if (nwrb != nullptr) {
2649  assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
2650  nwrb->SetMinimalSize(ScaleGUITrad(parts->u.xy.x), ScaleGUITrad(parts->u.xy.y));
2651  }
2652  break;
2653  }
2654 
2655  case WPT_MINTEXTLINES: {
2656  NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2657  if (nwrb != nullptr) {
2658  assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
2659  nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
2660  }
2661  break;
2662  }
2663 
2664  case WPT_FILL: {
2665  NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
2666  if (nwrb != nullptr) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
2667  break;
2668  }
2669 
2670  case WPT_DATATIP: {
2671  NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
2672  if (nwc != nullptr) {
2673  nwc->widget_data = parts->u.data_tip.data;
2674  nwc->tool_tip = parts->u.data_tip.tooltip;
2675  }
2676  break;
2677  }
2678 
2679  case WPT_PADDING:
2680  if (*dest != nullptr) (*dest)->SetPadding(ScaleGUITrad(parts->u.padding.top), ScaleGUITrad(parts->u.padding.right), ScaleGUITrad(parts->u.padding.bottom), ScaleGUITrad(parts->u.padding.left));
2681  break;
2682 
2683  case WPT_PIPSPACE: {
2684  NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
2685  if (nwc != nullptr) nwc->SetPIP(ScaleGUITrad(parts->u.pip.pre), ScaleGUITrad(parts->u.pip.inter), ScaleGUITrad(parts->u.pip.post));
2686 
2687  NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
2688  if (nwb != nullptr) nwb->SetPIP(ScaleGUITrad(parts->u.pip.pre), ScaleGUITrad(parts->u.pip.inter), ScaleGUITrad(parts->u.pip.post));
2689  break;
2690  }
2691 
2692  case WPT_SCROLLBAR: {
2693  NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
2694  if (nwc != nullptr) {
2695  nwc->scrollbar_index = parts->u.widget.index;
2696  }
2697  break;
2698  }
2699 
2700  case WPT_ENDCONTAINER:
2701  return num_used;
2702 
2703  case NWID_VIEWPORT:
2704  if (*dest != nullptr) return num_used;
2705  *dest = new NWidgetViewport(parts->u.widget.index);
2706  *biggest_index = std::max(*biggest_index, (int)parts->u.widget.index);
2707  break;
2708 
2709  case NWID_HSCROLLBAR:
2710  case NWID_VSCROLLBAR:
2711  if (*dest != nullptr) return num_used;
2712  *dest = new NWidgetScrollbar(parts->type, parts->u.widget.colour, parts->u.widget.index);
2713  *biggest_index = std::max(*biggest_index, (int)parts->u.widget.index);
2714  break;
2715 
2716  case NWID_SELECTION: {
2717  if (*dest != nullptr) return num_used;
2718  NWidgetStacked *nws = new NWidgetStacked();
2719  *dest = nws;
2720  *fill_dest = true;
2721  nws->SetIndex(parts->u.widget.index);
2722  *biggest_index = std::max(*biggest_index, (int)parts->u.widget.index);
2723  break;
2724  }
2725 
2726  default:
2727  if (*dest != nullptr) return num_used;
2728  assert((parts->type & WWT_MASK) < WWT_LAST || (parts->type & WWT_MASK) == NWID_BUTTON_DROPDOWN);
2729  *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
2730  *biggest_index = std::max(*biggest_index, (int)parts->u.widget.index);
2731  break;
2732  }
2733  num_used++;
2734  parts++;
2735  }
2736 
2737  return num_used;
2738 }
2739 
2749 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
2750 {
2751  /* If *parent == nullptr, only the first widget is read and returned. Otherwise, *parent must point to either
2752  * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
2753  NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
2754  NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
2755  assert(*parent == nullptr || (nwid_cont != nullptr && nwid_parent == nullptr) || (nwid_cont == nullptr && nwid_parent != nullptr));
2756 
2757  int total_used = 0;
2758  for (;;) {
2759  NWidgetBase *sub_widget = nullptr;
2760  bool fill_sub = false;
2761  int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
2762  parts += num_used;
2763  total_used += num_used;
2764 
2765  /* Break out of loop when end reached */
2766  if (sub_widget == nullptr) break;
2767 
2768  /* If sub-widget is a container, recursively fill that container. */
2769  WidgetType tp = sub_widget->type;
2770  if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
2771  || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
2772  NWidgetBase *sub_ptr = sub_widget;
2773  int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
2774  parts += num_used;
2775  total_used += num_used;
2776  }
2777 
2778  /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
2779  if (nwid_cont != nullptr) nwid_cont->Add(sub_widget);
2780  if (nwid_parent != nullptr) nwid_parent->Add(sub_widget);
2781  if (nwid_cont == nullptr && nwid_parent == nullptr) {
2782  *parent = sub_widget;
2783  return total_used;
2784  }
2785  }
2786 
2787  if (count == total_used) return total_used; // Reached the end of the array of parts?
2788 
2789  assert(total_used < count);
2790  assert(parts->type == WPT_ENDCONTAINER);
2791  return total_used + 1; // *parts is also 'used'
2792 }
2793 
2805 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
2806 {
2807  *biggest_index = -1;
2808  if (container == nullptr) container = new NWidgetVertical();
2809  NWidgetBase *cont_ptr = container;
2810  MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
2811  return container;
2812 }
2813 
2827 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
2828 {
2829  *biggest_index = -1;
2830 
2831  /* Read the first widget recursively from the array. */
2832  NWidgetBase *nwid = nullptr;
2833  int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
2834  assert(nwid != nullptr);
2835  parts += num_used;
2836  count -= num_used;
2837 
2838  NWidgetContainer *root = new NWidgetVertical;
2839  root->Add(nwid);
2840  if (count == 0) { // There is no body at all.
2841  *shade_select = nullptr;
2842  return root;
2843  }
2844 
2845  /* If the first widget looks like a titlebar, treat it as such.
2846  * If it has a shading box, silently add a shade selection widget in the tree. */
2847  NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
2848  NWidgetContainer *body;
2849  if (hor_cont != nullptr && hor_cont->GetWidgetOfType(WWT_CAPTION) != nullptr && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != nullptr) {
2850  *shade_select = new NWidgetStacked;
2851  root->Add(*shade_select);
2852  body = new NWidgetVertical;
2853  (*shade_select)->Add(body);
2854  } else {
2855  *shade_select = nullptr;
2856  body = root;
2857  }
2858 
2859  /* Load the remaining parts into 'body'. */
2860  int biggest2 = -1;
2861  MakeNWidgets(parts, count, &biggest2, body);
2862 
2863  *biggest_index = std::max(*biggest_index, biggest2);
2864  return root;
2865 }
2866 
2878 NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, Colours button_colour, int max_length, StringID button_tooltip)
2879 {
2880  assert(max_length >= 1);
2881  NWidgetVertical *vert = nullptr; // Storage for all rows.
2882  NWidgetHorizontal *hor = nullptr; // Storage for buttons in one row.
2883  int hor_length = 0;
2884 
2885  Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
2886  sprite_size.width += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
2887  sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
2888 
2889  for (int widnum = widget_first; widnum <= widget_last; widnum++) {
2890  /* Ensure there is room in 'hor' for another button. */
2891  if (hor_length == max_length) {
2892  if (vert == nullptr) vert = new NWidgetVertical();
2893  vert->Add(hor);
2894  hor = nullptr;
2895  hor_length = 0;
2896  }
2897  if (hor == nullptr) {
2898  hor = new NWidgetHorizontal();
2899  hor_length = 0;
2900  }
2901 
2902  NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, button_colour, widnum);
2903  panel->SetMinimalSize(sprite_size.width, sprite_size.height);
2904  panel->SetFill(1, 1);
2905  panel->SetResize(1, 0);
2906  panel->SetDataTip(0x0, button_tooltip);
2907  hor->Add(panel);
2908  hor_length++;
2909  }
2910  *biggest_index = widget_last;
2911  if (vert == nullptr) return hor; // All buttons fit in a single row.
2912 
2913  if (hor_length > 0 && hor_length < max_length) {
2914  /* Last row is partial, add a spacer at the end to force all buttons to the left. */
2915  NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
2916  spc->SetFill(1, 1);
2917  spc->SetResize(1, 0);
2918  hor->Add(spc);
2919  }
2920  if (hor != nullptr) vert->Add(hor);
2921  return vert;
2922 }
NWidgetBackground::SetupSmallestSize
void SetupSmallestSize(Window *w, bool init_array) override
Definition: widget.cpp:1768
MakeCompanyButtonRows
NWidgetBase * MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, Colours button_colour, int max_length, StringID button_tooltip)
Make a number of rows with button-like graphics, for enabling/disabling each company.
Definition: widget.cpp:2878
NWidgetResizeBase
Base class for a resizable nested widget.
Definition: widget_type.h:243
NWidgetPartTextLines::lines
uint8 lines
Number of text lines.
Definition: widget_type.h:902
NWidgetMatrix::clicked
int clicked
The currently clicked widget.
Definition: widget_type.h:521
ScrollbarClickHandler
void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
Special handling for the scrollbar widget type.
Definition: widget.cpp:136
NWidgetScrollbar::SetupSmallestSize
void SetupSmallestSize(Window *w, bool init_array) override
Definition: widget.cpp:2008
NWidgetStacked::AssignSizePosition
void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl) override
Definition: widget.cpp:1032
NWidgetMatrix::widgets_y
int widgets_y
The number of visible widgets in vertical direction.
Definition: widget_type.h:528
WD_FRAMERECT_TOP
@ WD_FRAMERECT_TOP
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:62
WD_DEFSIZEBOX_LEFT
@ WD_DEFSIZEBOX_LEFT
Left offset of defsize sprite.
Definition: window_gui.h:104
NWidgetStacked::NWidgetStacked
NWidgetStacked()
Widgets stacked on top of each other.
Definition: widget.cpp:978
NWidgetCore::IsDisabled
bool IsDisabled() const
Return whether the widget is disabled.
Definition: widget_type.h:367
WWT_IMGBTN_2
@ WWT_IMGBTN_2
(Toggle) Button with diff image when clicked
Definition: widget_type.h:51
WD_RESIZEBOX_BOTTOM
@ WD_RESIZEBOX_BOTTOM
Bottom offset of resize sprite.
Definition: window_gui.h:114
WPT_DATATIP
@ WPT_DATATIP
Widget part for specifying data and tooltip.
Definition: widget_type.h:89
ComputeMaxSize
static uint ComputeMaxSize(uint base, uint max_space, uint step)
Return the biggest possible size of a nested widget.
Definition: widget_type.h:807
NWidgetPart::pip
NWidgetPartPIP pip
Part with pre/inter/post spaces.
Definition: widget_type.h:926
WD_MATRIX_RIGHT
@ WD_MATRIX_RIGHT
Offset at right of a matrix cell.
Definition: window_gui.h:77
AddDirtyBlock
void AddDirtyBlock(int left, int top, int right, int bottom)
Extend the internal _invalid_rect rectangle to contain the rectangle defined by the given parameters.
Definition: gfx.cpp:1554
WF_SIZING
@ WF_SIZING
Window is being resized.
Definition: window_gui.h:237
NWidgetSpacer::GetWidgetFromPos
NWidgetCore * GetWidgetFromPos(int x, int y) override
Definition: widget.cpp:1475
querystring_gui.h
NWidgetPartPIP::post
uint8 post
Amount of space before/between/after child widgets.
Definition: widget_type.h:894
SortButtonState
SortButtonState
State of a sort direction button.
Definition: window_gui.h:222
NWidgetCore::NWidgetCore
NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint32 widget_data, StringID tool_tip)
Initialization of a 'real' widget.
Definition: widget.cpp:868
WD_STICKYBOX_WIDTH
@ WD_STICKYBOX_WIDTH
Width of a standard sticky box widget.
Definition: window_gui.h:89
GUISettings::newgrf_developer_tools
bool newgrf_developer_tools
activate NewGRF developer tools and allow modifying NewGRFs in an existing game
Definition: settings_type.h:175
ScrollbarClickPositioning
static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma)
Compute new position of the scrollbar after a click and updates the window flags.
Definition: widget.cpp:79
PALETTE_TEXT_RECOLOUR
@ PALETTE_TEXT_RECOLOUR
Set if palette is actually a magic text recolour.
Definition: sprites.h:1516
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
NWidgetPIPContainer::pip_inter
uint8 pip_inter
Amount of space between widgets.
Definition: widget_type.h:455
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
Scrollbar::GetCapacity
uint16 GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:631
NWidgetStacked::index
int index
If non-negative, index in the Window::nested_array.
Definition: widget_type.h:430
Window::DrawSortButtonState
void DrawSortButtonState(int widget, SortButtonState state) const
Draw a sort button's up or down arrow symbol.
Definition: widget.cpp:636
Window::nested_root
NWidgetBase * nested_root
Root of the nested tree.
Definition: window_gui.h:330
NWidgetLeaf::resizebox_dimension
static Dimension resizebox_dimension
Cached size of a resizebox widget.
Definition: widget_type.h:791
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:64
WD_SHADEBOX_BOTTOM
@ WD_SHADEBOX_BOTTOM
Bottom offset of shade sprite.
Definition: window_gui.h:86
DrawImageButtons
static void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
Draw an image button.
Definition: widget.cpp:216
DrawCloseBox
static void DrawCloseBox(const Rect &r, Colours colour)
Draw a close box.
Definition: widget.cpp:523
_transparency_opt
TransparencyOptionBits _transparency_opt
The bits that should be transparent.
Definition: transparency_gui.cpp:23
SZSP_BEGIN
@ SZSP_BEGIN
First zero-size plane.
Definition: widget_type.h:401
NWidgetPIPContainer::SetPIP
void SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
Set additional pre/inter/post space for the container.
Definition: widget.cpp:1110
Window::DrawWidget
virtual void DrawWidget(const Rect &r, int widget) const
Draw the contents of a nested widget.
Definition: window_gui.h:570
NWidgetContainer::Add
void Add(NWidgetBase *wid)
Append widget wid to container.
Definition: widget.cpp:951
DrawDropdown
static void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
Draw a dropdown WWT_DROPDOWN widget.
Definition: widget.cpp:594
NWID_HSCROLLBAR
@ NWID_HSCROLLBAR
Horizontal scrollbar.
Definition: widget_type.h:81
FS_BEGIN
@ FS_BEGIN
First font.
Definition: gfx_type.h:213
WD_MATRIX_TOP
@ WD_MATRIX_TOP
Offset at top of a matrix cell.
Definition: window_gui.h:78
TD_LTR
@ TD_LTR
Text is written left-to-right by default.
Definition: strings_type.h:23
NWidgetViewport
Nested widget to display a viewport in a window.
Definition: widget_type.h:584
TO_LOADING
@ TO_LOADING
loading indicators
Definition: transparency.h:31
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
Scrollbar::ScrollTowards
void ScrollTowards(int position)
Scroll towards the given position; if the item is visible nothing happens, otherwise it will be shown...
Definition: widget_type.h:740
Scrollbar::pos
uint16 pos
Index of first visible item of the list.
Definition: widget_type.h:603
Viewport::width
int width
Screen width of the viewport.
Definition: viewport_type.h:25
DrawStickyBox
static void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
Draw a sticky box.
Definition: widget.cpp:470
NWidgetMatrix
Matrix container with implicitly equal sized (virtual) sub-widgets.
Definition: widget_type.h:502
Scrollbar::GetScrolledRowFromWidget
int GetScrolledRowFromWidget(int clickpos, const Window *const w, int widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition: widget.cpp:1972
WWT_IMGBTN
@ WWT_IMGBTN
(Toggle) Button with image
Definition: widget_type.h:50
Window::viewport
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:327
ND_SHADE_DIMMED
@ ND_SHADE_DIMMED
Bit value of the 'dimmed colours' flag.
Definition: widget_type.h:280
WD_STICKYBOX_TOP
@ WD_STICKYBOX_TOP
Top offset of sticky sprite.
Definition: window_gui.h:92
WWT_LABEL
@ WWT_LABEL
Centered label.
Definition: widget_type.h:55
Viewport::height
int height
Screen height of the viewport.
Definition: viewport_type.h:26
NWidgetCore::SetIndex
void SetIndex(int index)
Set index of the nested widget in the widget array.
Definition: widget.cpp:881
FILLRECT_RECOLOUR
@ FILLRECT_RECOLOUR
Apply a recolour sprite to the screen content.
Definition: gfx_type.h:289
WWT_DEFSIZEBOX
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition: widget_type.h:63
NWidgetResizeBase::SetMinimalTextLines
void SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
Set minimal text lines for the widget.
Definition: widget.cpp:827
FrameFlags
FrameFlags
Flags to describe the look of the frame.
Definition: window_gui.h:25
NWidgetStacked::Draw
void Draw(const Window *w) override
Definition: widget.cpp:1058
DrawButtonDropdown
static void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
Draw a button with a dropdown (WWT_DROPDOWN and NWID_BUTTON_DROPDOWN).
Definition: widget.cpp:566
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:73
NWidgetMatrix::Draw
void Draw(const Window *w) override
Definition: widget.cpp:1637
NWidgetLeaf::stickybox_dimension
static Dimension stickybox_dimension
Cached size of a stickybox widget.
Definition: widget_type.h:797
NDB_SCROLLBAR_DOWN
@ NDB_SCROLLBAR_DOWN
Down-button is lowered bit.
Definition: widget_type.h:271
WD_CLOSEBOX_RIGHT
@ WD_CLOSEBOX_RIGHT
Right offset of closebox string.
Definition: window_gui.h:119
Viewport::top
int top
Screen coordinate top edge of the viewport.
Definition: viewport_type.h:24
Window::IsNewGRFInspectable
virtual bool IsNewGRFInspectable() const
Is the data related to this window NewGRF inspectable?
Definition: window_gui.h:804
WF_STICKY
@ WF_STICKY
Window is made sticky by user.
Definition: window_gui.h:238
maxdim
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Definition: geometry_func.cpp:22
WWT_MATRIX
@ WWT_MATRIX
Grid of rows and columns.
Definition: widget_type.h:57
NWID_HORIZONTAL_LTR
@ NWID_HORIZONTAL_LTR
Horizontal container that doesn't change the order of the widgets for RTL languages.
Definition: widget_type.h:74
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
NWidgetSpacer
Spacer widget.
Definition: widget_type.h:538
UnScaleGUI
static int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition: zoom_func.h:66
PALETTE_TO_TRANSPARENT
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition: sprites.h:1591
WWT_ARROWBTN
@ WWT_ARROWBTN
(Toggle) Button with an arrow
Definition: widget_type.h:52
NWidgetBase::NWidgetBase
NWidgetBase(WidgetType tp)
Base class constructor.
Definition: widget.cpp:719
Scrollbar::SetCount
void SetCount(int num)
Sets the number of elements in the list.
Definition: widget_type.h:679
FILLRECT_CHECKER
@ FILLRECT_CHECKER
Draw only every second pixel, used for greying-out.
Definition: gfx_type.h:288
MAT_ROW_START
@ MAT_ROW_START
Lowest bit of the number of rows.
Definition: widget_type.h:29
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
zoom_func.h
NWidgetLeaf
Leaf widget.
Definition: widget_type.h:779
WD_FRAMETEXT_TOP
@ WD_FRAMETEXT_TOP
Top offset of the text of the frame.
Definition: window_gui.h:72
WD_SCROLLBAR_RIGHT
@ WD_SCROLLBAR_RIGHT
Right offset of scrollbar.
Definition: window_gui.h:49
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
NWidgetMatrix::SetScrollbar
void SetScrollbar(Scrollbar *sb)
Assign a scrollbar to this matrix.
Definition: widget.cpp:1539
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
WD_DEFSIZEBOX_BOTTOM
@ WD_DEFSIZEBOX_BOTTOM
Bottom offset of defsize sprite.
Definition: window_gui.h:107
NWidgetMatrix::count
int count
Amount of valid widgets.
Definition: widget_type.h:522
DrawString
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:640
NWidgetPart::xy
Point xy
Part with an x/y size.
Definition: widget_type.h:922
NWidgetSpacer::FillNestedArray
void FillNestedArray(NWidgetBase **array, uint length) override
Definition: widget.cpp:1461
SZSP_HORIZONTAL
@ SZSP_HORIZONTAL
Display plane with zero size vertically, and filling and resizing horizontally.
Definition: widget_type.h:398
WWT_EMPTY
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget array.
Definition: widget_type.h:46
Window::owner
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition: window_gui.h:325
WWT_PUSHARROWBTN
@ WWT_PUSHARROWBTN
Normal push-button (no toggle button) with arrow caption.
Definition: widget_type.h:104
FR_LOWERED
@ FR_LOWERED
If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
Definition: window_gui.h:29
NWidgetCore::tool_tip
StringID tool_tip
Tooltip of the widget.
Definition: widget_type.h:315
NWidgetContainer::GetWidgetOfType
NWidgetBase * GetWidgetOfType(WidgetType tp) override
Retrieve a widget by its type.
Definition: widget.cpp:937
NWidgetCore::SetLowered
void SetLowered(bool lowered)
Lower or raise the widget.
Definition: widget_type.h:346
SA_HOR_CENTER
@ SA_HOR_CENTER
Horizontally center the text.
Definition: gfx_func.h:97
WD_DEBUGBOX_LEFT
@ WD_DEBUGBOX_LEFT
Left offset of debug sprite.
Definition: window_gui.h:97
NWidgetLeaf::shadebox_dimension
static Dimension shadebox_dimension
Cached size of a shadebox widget.
Definition: widget_type.h:794
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
NWID_MATRIX
@ NWID_MATRIX
Matrix container.
Definition: widget_type.h:76
NWidgetBase::smallest_y
uint smallest_y
Smallest vertical size of the widget in a filled window.
Definition: widget_type.h:180
Scrollbar
Scrollbar data structure.
Definition: widget_type.h:598
MakeNWidgets
NWidgetContainer * MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
Construct a nested widget tree from an array of parts.
Definition: widget.cpp:2805
NWidgetBase::padding_top
uint8 padding_top
Paddings added to the top of the widget. Managed by parent container widget.
Definition: widget_type.h:191
NWidgetMatrix::SetupSmallestSize
void SetupSmallestSize(Window *w, bool init_array) override
Definition: widget.cpp:1544
WD_FRAMETEXT_LEFT
@ WD_FRAMETEXT_LEFT
Left offset of the text of the frame.
Definition: window_gui.h:70
FR_TRANSPARENT
@ FR_TRANSPARENT
Makes the background transparent if set.
Definition: window_gui.h:27
_colour_gradient
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:52
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:919
DrawDefSizeBox
static void DrawDefSizeBox(const Rect &r, Colours colour, bool clicked)
Draw a defsize box.
Definition: widget.cpp:481
NWidgetMatrix::sb
Scrollbar * sb
The scrollbar we're associated with.
Definition: widget_type.h:523
QueryString
Data stored about a string that can be modified in the GUI.
Definition: querystring_gui.h:20
MAT_COL_START
@ MAT_COL_START
Lowest bit of the number of columns.
Definition: widget_type.h:25
NWidgetCore::widget_data
uint32 widget_data
Data of the widget.
Definition: widget_type.h:314
WD_IMGBTN_BOTTOM
@ WD_IMGBTN_BOTTOM
Bottom offset of image in the button.
Definition: window_gui.h:41
GetStringBoundingBox
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:842
WD_INSET_TOP
@ WD_INSET_TOP
Top offset of string.
Definition: window_gui.h:46
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
NWidgetBase::Draw
virtual void Draw(const Window *w)=0
NWidgetMatrix::SetCount
void SetCount(int count)
Set the number of elements in this matrix.
Definition: widget.cpp:1515
NWidgetBase::prev
NWidgetBase * prev
Pointer to previous widget in container. Managed by parent container widget.
Definition: widget_type.h:189
Window::SetStringParameters
virtual void SetStringParameters(int widget) const
Initialize string parameters for a widget.
Definition: window_gui.h:592
window_gui.h
NWidgetStacked::FillNestedArray
void FillNestedArray(NWidgetBase **array, uint length) override
Definition: widget.cpp:1052
NWidgetLeaf::InvalidateDimensionCache
static void InvalidateDimensionCache()
Reset the cached dimensions.
Definition: widget.cpp:2099
NWidgetResizeBase::NWidgetResizeBase
NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y)
Constructor for resizable nested widgets.
Definition: widget.cpp:804
NC_EQUALSIZE
@ NC_EQUALSIZE
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:438
Window::GetWidget
const NWID * GetWidget(uint widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition: window_gui.h:845
ND_DROPDOWN_ACTIVE
@ ND_DROPDOWN_ACTIVE
Bit value of the 'dropdown active' flag.
Definition: widget_type.h:281
_company_colours
Colours _company_colours[MAX_COMPANIES]
NOSAVE: can be determined from company structs.
Definition: company_cmd.cpp:48
AWV_LEFT
@ AWV_LEFT
Force the arrow to the left.
Definition: widget_type.h:37
Viewport
Data structure for viewport, display of a part of the world.
Definition: viewport_type.h:22
WPT_FILL
@ WPT_FILL
Widget part for specifying fill.
Definition: widget_type.h:88
NWidgetBase::padding_left
uint8 padding_left
Paddings added to the left of the widget. Managed by parent container widget. (parent container may s...
Definition: widget_type.h:194
NWidgetLeaf::NWidgetLeaf
NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32 data, StringID tip)
Nested leaf widget.
Definition: widget.cpp:2126
NWidgetBackground::AssignSizePosition
void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl) override
Definition: widget.cpp:1822
WD_RESIZEBOX_RIGHT
@ WD_RESIZEBOX_RIGHT
Right offset of resize sprite.
Definition: window_gui.h:112
WWT_PUSHBTN
@ WWT_PUSHBTN
Normal push-button (no toggle button) with custom drawing.
Definition: widget_type.h:101
DrawVerticalScrollbar
static void DrawVerticalScrollbar(const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
Draw a vertical scrollbar.
Definition: widget.cpp:336
NWidgetLeaf::dropdown_dimension
static Dimension dropdown_dimension
Cached size of a dropdown widget.
Definition: widget_type.h:790
ST_SMALLEST
@ ST_SMALLEST
Initialize nested widget tree to smallest size. Also updates current_x and current_y.
Definition: widget_type.h:110
DrawShadeBox
static void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
Draw a shade box.
Definition: widget.cpp:459
NWidgetViewport::UpdateViewportCoordinates
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition: widget.cpp:1949
WD_SHADEBOX_RIGHT
@ WD_SHADEBOX_RIGHT
Right offset of shade sprite.
Definition: window_gui.h:84
Scrollbar::GetCount
uint16 GetCount() const
Gets the number of elements in the list.
Definition: widget_type.h:622
NWidgetContainer::tail
NWidgetBase * tail
Pointer to last widget in container.
Definition: widget_type.h:392
NWidgetPIPContainer::Draw
void Draw(const Window *w) override
Definition: widget.cpp:1117
NWID_VIEWPORT
@ NWID_VIEWPORT
Nested widget containing a viewport.
Definition: widget_type.h:79
WD_CLOSEBOX_TOP
@ WD_CLOSEBOX_TOP
Top offset of closebox string.
Definition: window_gui.h:120
NWidgetViewport::SetupSmallestSize
void SetupSmallestSize(Window *w, bool init_array) override
Definition: widget.cpp:1904
WWT_EDITBOX
@ WWT_EDITBOX
a textbox for typing
Definition: widget_type.h:69
NWidgetBase::type
WidgetType type
Type of the widget / nested widget.
Definition: widget_type.h:171
NWidgetPIPContainer::GetWidgetFromPos
NWidgetCore * GetWidgetFromPos(int x, int y) override
Definition: widget.cpp:1124
Window::height
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:321
DrawCaption
void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
Draw a caption bar.
Definition: widget.cpp:538
WF_WHITE_BORDER
@ WF_WHITE_BORDER
Window white border counter bit mask.
Definition: window_gui.h:240
WD_INSET_RIGHT
@ WD_INSET_RIGHT
Right offset of string.
Definition: window_gui.h:45
NWidgetResizeBase::SetResize
void SetResize(uint resize_x, uint resize_y)
Set resize step of the widget.
Definition: widget.cpp:848
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:984
Viewport::left
int left
Screen coordinate left edge of the viewport.
Definition: viewport_type.h:23
NWidgetBase::smallest_x
uint smallest_x
Smallest horizontal size of the widget in a filled window.
Definition: widget_type.h:179
WD_FRAMERECT_LEFT
@ WD_FRAMERECT_LEFT
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:60
NWidgetHorizontal::AssignSizePosition
void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
Definition: widget.cpp:1209
NWidgetVertical::SetupSmallestSize
void SetupSmallestSize(Window *w, bool init_array)
Definition: widget.cpp:1307
NWidgetBase::AssignSizePosition
virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)=0
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
NWidgetLeaf::debugbox_dimension
static Dimension debugbox_dimension
Cached size of a debugbox widget.
Definition: widget_type.h:795
IsInsideBS
static bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:188
NWidgetBase::GetWidgetOfType
virtual NWidgetBase * GetWidgetOfType(WidgetType tp)
Retrieve a widget by its type.
Definition: widget.cpp:793
WPT_PIPSPACE
@ WPT_PIPSPACE
Widget part for specifying pre/inter/post space for containers.
Definition: widget_type.h:91
WD_FRAMERECT_RIGHT
@ WD_FRAMERECT_RIGHT
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:61
ZeroedMemoryAllocator
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:85
WD_FRAMERECT_BOTTOM
@ WD_FRAMERECT_BOTTOM
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:63
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:102
WD_DEFSIZEBOX_RIGHT
@ WD_DEFSIZEBOX_RIGHT
Right offset of defsize sprite.
Definition: window_gui.h:105
NWidgetCore::SetToolTip
void SetToolTip(StringID tool_tip)
Set the tool tip of the nested widget.
Definition: widget.cpp:902
NWidgetBase
Baseclass for nested widgets.
Definition: widget_type.h:124
NWidgetBackground::GetWidgetOfType
NWidgetBase * GetWidgetOfType(WidgetType tp) override
Retrieve a widget by its type.
Definition: widget.cpp:1891
WD_FRAMETEXT_BOTTOM
@ WD_FRAMETEXT_BOTTOM
Bottom offset of the text of the frame.
Definition: window_gui.h:73
Scrollbar::IsVertical
bool IsVertical() const
Is the scrollbar vertical or not?
Definition: widget_type.h:659
NWidgetMatrix::GetWidgetFromPos
NWidgetCore * GetWidgetFromPos(int x, int y) override
Definition: widget.cpp:1605
PALETTE_NEWSPAPER
static const PaletteID PALETTE_NEWSPAPER
Recolour sprite for newspaper-greying.
Definition: sprites.h:1593
NWidgetVertical::AssignSizePosition
void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
Definition: widget.cpp:1376
GetWidgetFromPos
int GetWidgetFromPos(const Window *w, int x, int y)
Returns the index for the widget located at the given position relative to the window.
Definition: widget.cpp:160
DrawDebugBox
static void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
Draw a NewGRF debug box.
Definition: widget.cpp:492
_string_colourmap
static const byte _string_colourmap[17]
Colour mapping for TextColour.
Definition: string_colours.h:11
Window::GetQueryString
const QueryString * GetQueryString(uint widnum) const
Return the querystring associated to a editbox.
Definition: window.cpp:329
NWidgetPart::widget
NWidgetPartWidget widget
Part with a start of a widget.
Definition: widget_type.h:924
NWidgetPartWidget::index
int16 index
Widget index in the widget array.
Definition: widget_type.h:878
WPT_SCROLLBAR
@ WPT_SCROLLBAR
Widget part for attaching a scrollbar.
Definition: widget_type.h:94
MAT_COL_BITS
@ MAT_COL_BITS
Number of bits for the number of columns in the matrix.
Definition: widget_type.h:26
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
NWidgetPIPContainer::pip_pre
uint8 pip_pre
Amount of space before first widget.
Definition: widget_type.h:454
TransparencyOptionBits
uint TransparencyOptionBits
transparency option bits
Definition: transparency.h:36
WD_STICKYBOX_RIGHT
@ WD_STICKYBOX_RIGHT
Right offset of sticky sprite.
Definition: window_gui.h:91
NWidgetBackground::SetPIP
void SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
Set additional pre/inter/post space for the background widget.
Definition: widget.cpp:1760
safeguards.h
DrawMatrix
static void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint16 data, uint resize_x, uint resize_y)
Draw a matrix widget.
Definition: widget.cpp:276
Window::left
int left
x position of left edge of the window
Definition: window_gui.h:318
NWidgetLeaf::defsizebox_dimension
static Dimension defsizebox_dimension
Cached size of a defsizebox widget.
Definition: widget_type.h:796
Window::flags
WindowFlags flags
Window flags.
Definition: window_gui.h:311
WD_INSET_LEFT
@ WD_INSET_LEFT
Left offset of string.
Definition: window_gui.h:44
WWT_LAST
@ WWT_LAST
Last Item. use WIDGETS_END to fill up padding!!
Definition: widget_type.h:70
NWidgetContainer::NWidgetContainer
NWidgetContainer(WidgetType tp)
Constructor container baseclass.
Definition: widget.cpp:921
WD_DEBUGBOX_RIGHT
@ WD_DEBUGBOX_RIGHT
Right offset of debug sprite.
Definition: window_gui.h:98
NWidgetPart::func_ptr
NWidgetFunctionType * func_ptr
Part with a function call.
Definition: widget_type.h:928
NDB_SCROLLBAR_UP
@ NDB_SCROLLBAR_UP
Up-button is lowered bit.
Definition: widget_type.h:270
DrawSprite
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition: gfx.cpp:978
NWidgetPart::padding
NWidgetPartPaddings padding
Part with paddings.
Definition: widget_type.h:925
settings_type.h
sprites.h
Viewport::virtual_width
int virtual_width
width << zoom
Definition: viewport_type.h:30
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
WF_HIGHLIGHTED
@ WF_HIGHLIGHTED
Window has a widget that has a highlight.
Definition: window_gui.h:241
WPT_MINSIZE
@ WPT_MINSIZE
Widget part for specifying minimal size.
Definition: widget_type.h:86
NWidgetCore::IsLowered
bool IsLowered() const
Return whether the widget is lowered.
Definition: widget_type.h:352
DrawHorizontalScrollbar
static void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
Draw a horizontal scrollbar.
Definition: widget.cpp:374
NWidgetPartTextLines::spacing
uint8 spacing
Extra spacing around lines.
Definition: widget_type.h:903
WWT_FRAME
@ WWT_FRAME
Frame.
Definition: widget_type.h:58
NWidgetStacked::GetWidgetFromPos
NWidgetCore * GetWidgetFromPos(int x, int y) override
Definition: widget.cpp:1073
NWidgetContainer::head
NWidgetBase * head
Pointer to first widget in container.
Definition: widget_type.h:391
NWidgetMatrix::index
int index
If non-negative, index in the Window::nested_array.
Definition: widget_type.h:519
WD_FRAMETEXT_RIGHT
@ WD_FRAMETEXT_RIGHT
Right offset of the text of the frame.
Definition: window_gui.h:71
stdafx.h
GfxFillRect
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition: gfx.cpp:114
WD_DROPDOWNTEXT_RIGHT
@ WD_DROPDOWNTEXT_RIGHT
Right offset of the dropdown widget string.
Definition: window_gui.h:133
WD_SCROLLBAR_LEFT
@ WD_SCROLLBAR_LEFT
Left offset of scrollbar.
Definition: window_gui.h:48
NWidgetStacked::SetDisplayedPlane
void SetDisplayedPlane(int plane)
Select which plane to show (for NWID_SELECTION only).
Definition: widget.cpp:1091
viewport_func.h
Window::mouse_capture_widget
int mouse_capture_widget
Widgetindex of current mouse capture widget (e.g. dragged scrollbar). -1 if no widget has mouse captu...
Definition: window_gui.h:336
NWidgetBase::current_y
uint current_y
Current vertical size (after resizing).
Definition: widget_type.h:183
NWidgetStacked
Stacked widgets, widgets all occupying the same space in the window.
Definition: widget_type.h:414
NWidgetScrollbar::vertical_dimension
static Dimension vertical_dimension
Cached size of vertical scrollbar button.
Definition: widget_type.h:771
NWidgetBase::next
NWidgetBase * next
Pointer to next widget in container. Managed by parent container widget.
Definition: widget_type.h:188
NWID_VERTICAL
@ NWID_VERTICAL
Vertical container.
Definition: widget_type.h:75
Window::nested_array
NWidgetBase ** nested_array
Array of pointers into the tree. Do not access directly, use Window::GetWidget() instead.
Definition: window_gui.h:331
WidgetType
WidgetType
Window widget types, nested widget types, and nested widget part types.
Definition: widget_type.h:44
NWidgetResizeBase::SetFill
void SetFill(uint fill_x, uint fill_y)
Set the filling of the widget from initial size.
Definition: widget.cpp:837
string_colours.h
FillDrawPixelInfo
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
Definition: gfx.cpp:1616
WWT_TEXTBTN_2
@ WWT_TEXTBTN_2
(Toggle) Button with diff text when clicked
Definition: widget_type.h:54
WD_SHADEBOX_TOP
@ WD_SHADEBOX_TOP
Top offset of shade sprite.
Definition: window_gui.h:85
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:913
DrawResizeBox
static void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
Draw a resize box.
Definition: widget.cpp:504
NWidgetSpacer::SetDirty
void SetDirty(const Window *w) const override
Mark the widget as 'dirty' (in need of repaint).
Definition: widget.cpp:1470
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:67
WWT_RESIZEBOX
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:66
NWidgetHorizontal::SetupSmallestSize
void SetupSmallestSize(Window *w, bool init_array)
Definition: widget.cpp:1140
WD_SCROLLBAR_BOTTOM
@ WD_SCROLLBAR_BOTTOM
Bottom offset of scrollbar.
Definition: window_gui.h:51
WPT_MINTEXTLINES
@ WPT_MINTEXTLINES
Widget part for specifying minimal number of lines of text.
Definition: widget_type.h:87
NWidgetBase::GetWidgetFromPos
virtual NWidgetCore * GetWidgetFromPos(int x, int y)=0
WD_DROPDOWNTEXT_TOP
@ WD_DROPDOWNTEXT_TOP
Top offset of the dropdown widget string.
Definition: window_gui.h:134
Scrollbar::SetCapacity
void SetCapacity(int capacity)
Set the capacity of visible elements.
Definition: widget_type.h:695
NWidgetSpacer::SetupSmallestSize
void SetupSmallestSize(Window *w, bool init_array) override
Definition: widget.cpp:1455
WD_SHADEBOX_LEFT
@ WD_SHADEBOX_LEFT
Left offset of shade sprite.
Definition: window_gui.h:83
NWidgetBackground::GetWidgetFromPos
NWidgetCore * GetWidgetFromPos(int x, int y) override
Definition: widget.cpp:1881
NWidgetMatrix::GetScrollOffsets
void GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
Get the different offsets that are influenced by scrolling.
Definition: widget.cpp:1692
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
NWidgetSpacer::Draw
void Draw(const Window *w) override
Definition: widget.cpp:1465
WWT_PUSHIMGBTN
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
Definition: widget_type.h:103
NWidgetBase::fill_y
uint fill_y
Vertical fill stepsize (from initial size, 0 means not resizable).
Definition: widget_type.h:173
NWidgetPart::cont_flags
NWidContainerFlags cont_flags
Part with container flags.
Definition: widget_type.h:929
SBS_DOWN
@ SBS_DOWN
Sort ascending.
Definition: window_gui.h:224
strings_func.h
WD_DROPDOWN_HEIGHT
@ WD_DROPDOWN_HEIGHT
Height of a drop down widget.
Definition: window_gui.h:131
NWID_VSCROLLBAR
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition: widget_type.h:82
ScaleByZoom
static int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) When shifting right,...
Definition: zoom_func.h:22
NWidgetCore::disp_flags
NWidgetDisplay disp_flags
Flags that affect display and interaction with the widget.
Definition: widget_type.h:311
NWidgetCore::SetDataTip
void SetDataTip(uint32 widget_data, StringID tool_tip)
Set data and tool tip of the nested widget.
Definition: widget.cpp:892
Window::IsShaded
bool IsShaded() const
Is window shaded currently?
Definition: window_gui.h:525
HandleScrollbarHittest
static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bool horizontal)
Compute the vertical position of the draggable part of scrollbar.
Definition: widget.cpp:36
NWidgetBase::pos_x
int pos_x
Horizontal position of top-left corner of the widget in the window.
Definition: widget_type.h:185
NWidgetHorizontal
Horizontal container.
Definition: widget_type.h:463
ST_RESIZE
@ ST_RESIZE
Resize the nested widget tree.
Definition: widget_type.h:111
NWidgetBackground::NWidgetBackground
NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child=nullptr)
Constructor parent nested widgets.
Definition: widget.cpp:1723
WD_CLOSEBOX_WIDTH
@ WD_CLOSEBOX_WIDTH
Width of a close box widget.
Definition: window_gui.h:117
NWidgetMatrix::FillNestedArray
void FillNestedArray(NWidgetBase **array, uint length) override
Definition: widget.cpp:1599
WWT_TEXT
@ WWT_TEXT
Pure simple text.
Definition: widget_type.h:56
WD_BEVEL_RIGHT
@ WD_BEVEL_RIGHT
Width of right bevel border.
Definition: window_gui.h:55
NWidgetPIPContainer
Container with pre/inter/post child space.
Definition: widget_type.h:443
WD_CAPTIONTEXT_TOP
@ WD_CAPTIONTEXT_TOP
Offset of the caption text at the top.
Definition: window_gui.h:127
FONT_HEIGHT_NORMAL
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:179
ScaleGUITrad
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:76
NWidgetBase::fill_x
uint fill_x
Horizontal fill stepsize (from initial size, 0 means not resizable).
Definition: widget_type.h:172
MakeWidgetTree
static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
Build a nested widget tree by recursively filling containers with nested widgets read from their part...
Definition: widget.cpp:2749
NWidgetScrollbar
Nested widget to display and control a scrollbar in a window.
Definition: widget_type.h:759
geometry_func.hpp
NWidgetBackground::Draw
void Draw(const Window *w) override
Definition: widget.cpp:1840
WPT_RESIZE
@ WPT_RESIZE
Widget part for specifying resizing.
Definition: widget_type.h:85
NWidgetViewport::InitializeViewport
void InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
Initialize the viewport of the window.
Definition: widget.cpp:1940
NWidgetPIPContainer::flags
NWidContainerFlags flags
Flags of the container.
Definition: widget_type.h:453
transparency.h
WD_DEBUGBOX_BOTTOM
@ WD_DEBUGBOX_BOTTOM
Bottom offset of debug sprite.
Definition: window_gui.h:100
WD_SCROLLBAR_TOP
@ WD_SCROLLBAR_TOP
Top offset of scrollbar.
Definition: window_gui.h:50
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:48
_window_highlight_colour
bool _window_highlight_colour
If false, highlight is white, otherwise the by the widget defined colour.
Definition: window.cpp:62
NWidgetPIPContainer::pip_post
uint8 pip_post
Amount of space after last widget.
Definition: widget_type.h:456
NWidgetStacked::SetupSmallestSize
void SetupSmallestSize(Window *w, bool init_array) override
Definition: widget.cpp:988
NWidgetBase::resize_y
uint resize_y
Vertical resize step (0 means not resizable).
Definition: widget_type.h:175
NWidgetBase::StoreSizePosition
void StoreSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height)
Store size and position.
Definition: widget_type.h:226
Window::IsWidgetLowered
bool IsWidgetLowered(byte widget_index) const
Gets the lowered state of a widget.
Definition: window_gui.h:494
NWidgetVertical
Vertical container.
Definition: widget_type.h:486
NWidgetBackground::child
NWidgetPIPContainer * child
Child widget.
Definition: widget_type.h:572
Scrollbar::GetPosition
uint16 GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:640
NWidgetPart::data_tip
NWidgetPartDataTip data_tip
Part with a data/tooltip.
Definition: widget_type.h:923
NWidgetContainer::FillNestedArray
void FillNestedArray(NWidgetBase **array, uint length) override
Definition: widget.cpp:968
Window::UpdateWidgetSize
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
Update size and resize step of a widget in the window.
Definition: window_gui.h:584
WD_IMGBTN_TOP
@ WD_IMGBTN_TOP
Top offset of image in the button.
Definition: window_gui.h:40
WD_RESIZEBOX_WIDTH
@ WD_RESIZEBOX_WIDTH
Width of a resize box widget.
Definition: window_gui.h:110
NWidgetHorizontalLTR::AssignSizePosition
void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
Definition: widget.cpp:1297
NWidgetCore::colour
Colours colour
Colour of this widget.
Definition: widget_type.h:312
NWidgetMatrix::widget_h
int widget_h
The height of the child widget including inter spacing.
Definition: widget_type.h:526
WD_STICKYBOX_BOTTOM
@ WD_STICKYBOX_BOTTOM
Bottom offset of sticky sprite.
Definition: window_gui.h:93
NWID_SPACER
@ NWID_SPACER
Invisible widget that takes some space.
Definition: widget_type.h:77
company_func.h
WWT_INSET
@ WWT_INSET
Pressed (inset) panel, most commonly used as combo box text area.
Definition: widget_type.h:49
NWidgetContainer
Baseclass for container widgets.
Definition: widget_type.h:377
TO_SIGNS
@ TO_SIGNS
signs
Definition: transparency.h:23
Window::top
int top
y position of top edge of the window
Definition: window_gui.h:319
NWidgetBase::padding_right
uint8 padding_right
Paddings added to the right of the widget. Managed by parent container widget. (parent container may ...
Definition: widget_type.h:192
Window::DrawViewport
void DrawViewport() const
Draw the viewport of this window.
Definition: viewport.cpp:1810
NWidgetCore::scrollbar_index
int scrollbar_index
Index of an attached scrollbar.
Definition: widget_type.h:316
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:175
NWidgetBase::SetDirty
virtual void SetDirty(const Window *w) const
Mark the widget as 'dirty' (in need of repaint).
Definition: widget.cpp:773
WD_BEVEL_TOP
@ WD_BEVEL_TOP
Height of top bevel border.
Definition: window_gui.h:56
WD_BEVEL_LEFT
@ WD_BEVEL_LEFT
Width of left bevel border.
Definition: window_gui.h:54
WD_MATRIX_BOTTOM
@ WD_MATRIX_BOTTOM
Offset at bottom of a matrix cell.
Definition: window_gui.h:79
NWidgetSpacer::NWidgetSpacer
NWidgetSpacer(int length, int height)
Generic spacer widget.
Definition: widget.cpp:1449
CenterBounds
static int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition: gfx_func.h:151
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:69
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
Window::width
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:320
Viewport::zoom
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:33
NWidgetResizeBase::AssignSizePosition
void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
Definition: widget.cpp:854
Scrollbar::SetCapacityFromWidget
void SetCapacityFromWidget(Window *w, int widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget.
Definition: widget.cpp:1986
WD_CAPTIONTEXT_BOTTOM
@ WD_CAPTIONTEXT_BOTTOM
Offset of the caption text at the bottom.
Definition: window_gui.h:128
Window::SortButtonWidth
static int SortButtonWidth()
Get width of up/down arrow of sort button state.
Definition: widget.cpp:656
NWidgetHorizontalLTR::NWidgetHorizontalLTR
NWidgetHorizontalLTR(NWidContainerFlags flags=NC_NONE)
Horizontal left-to-right container widget.
Definition: widget.cpp:1292
DrawLabel
static void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
Draw the label-part of a widget.
Definition: widget.cpp:233
FR_DARKENED
@ FR_DARKENED
If set the background is darker, allows for lowered frames with normal background colour when used wi...
Definition: window_gui.h:30
NWidgetResizeBase::min_x
uint min_x
Minimal horizontal size of only this widget.
Definition: widget_type.h:254
NWidgetBase::pos_y
int pos_y
Vertical position of top-left corner of the widget in the window.
Definition: widget_type.h:186
WD_RESIZEBOX_TOP
@ WD_RESIZEBOX_TOP
Top offset of resize sprite.
Definition: window_gui.h:113
DrawText
static void DrawText(const Rect &r, TextColour colour, StringID str)
Draw text.
Definition: widget.cpp:248
Scrollbar::SS_BIG
@ SS_BIG
Step in cap units.
Definition: widget_type.h:611
Scrollbar::SetStepSize
void SetStepSize(uint16 stepsize)
Set the distance to scroll when using the buttons or the wheel.
Definition: widget_type.h:668
NWidgetScrollbar::NWidgetScrollbar
NWidgetScrollbar(WidgetType tp, Colours colour, int index)
Scrollbar widget.
Definition: widget.cpp:2002
ND_SHADE_GREY
@ ND_SHADE_GREY
Bit value of the 'shade to grey' flag.
Definition: widget_type.h:279
DrawInset
static void DrawInset(const Rect &r, Colours colour, StringID str)
Draw an inset widget.
Definition: widget.cpp:261
WPT_PADDING
@ WPT_PADDING
Widget part for specifying a padding.
Definition: widget_type.h:90
Scrollbar::UpdatePosition
void UpdatePosition(int difference, ScrollbarStepping unit=SS_SMALL)
Updates the position of the first visible element by the given amount.
Definition: widget_type.h:723
NWidgetPart::type
WidgetType type
Type of the part.
Definition: widget_type.h:920
NWidgetResizeBase::min_y
uint min_y
Minimal vertical size of only this widget.
Definition: widget_type.h:255
NWidgetHorizontalLTR
Horizontal container that doesn't change the direction of the widgets for RTL languages.
Definition: widget_type.h:475
WD_DEBUGBOX_TOP
@ WD_DEBUGBOX_TOP
Top offset of debug sprite.
Definition: window_gui.h:99
WD_CAPTION_HEIGHT
@ WD_CAPTION_HEIGHT
Height of a title bar.
Definition: window_gui.h:124
CeilDiv
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:254
WD_DROPDOWNTEXT_BOTTOM
@ WD_DROPDOWNTEXT_BOTTOM
Bottom offset of the dropdown widget string.
Definition: window_gui.h:135
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
NWidgetMatrix::widget_w
int widget_w
The width of the child widget including inter spacing.
Definition: widget_type.h:525
NWidgetBase::padding_bottom
uint8 padding_bottom
Paddings added to the bottom of the widget. Managed by parent container widget.
Definition: widget_type.h:193
NWidgetBackground::Add
void Add(NWidgetBase *nwid)
Add a child to the parent.
Definition: widget.cpp:1742
MAT_ROW_BITS
@ MAT_ROW_BITS
Number of bits for the number of rows in the matrix.
Definition: widget_type.h:30
Window
Data structure for an opened window.
Definition: window_gui.h:277
SizingType
SizingType
Different forms of sizing nested widgets, using NWidgetBase::AssignSizePosition()
Definition: widget_type.h:109
WD_CAPTIONTEXT_LEFT
@ WD_CAPTIONTEXT_LEFT
Offset of the caption text at the left.
Definition: window_gui.h:125
NWidContainerFlags
NWidContainerFlags
Nested widget container flags,.
Definition: widget_type.h:434
SZSP_VERTICAL
@ SZSP_VERTICAL
Display plane with zero size horizontally, and filling and resizing vertically.
Definition: widget_type.h:397
WD_MATRIX_LEFT
@ WD_MATRIX_LEFT
Offset at left of a matrix cell.
Definition: window_gui.h:76
Window::DrawWidgets
void DrawWidgets() const
Paint all widgets of a window.
Definition: widget.cpp:602
DrawFrame
static void DrawFrame(const Rect &r, Colours colour, StringID str)
Draw a frame widget.
Definition: widget.cpp:409
NWidgetBackground
Nested widget with a child.
Definition: widget_type.h:554
Viewport::virtual_height
int virtual_height
height << zoom
Definition: viewport_type.h:31
NWidgetVertical::NWidgetVertical
NWidgetVertical(NWidContainerFlags flags=NC_NONE)
Vertical container widget.
Definition: widget.cpp:1303
WD_DROPDOWNTEXT_LEFT
@ WD_DROPDOWNTEXT_LEFT
Left offset of the dropdown widget string.
Definition: window_gui.h:132
NWidgetViewport::Draw
void Draw(const Window *w) override
Definition: widget.cpp:1914
WD_IMGBTN_LEFT
@ WD_IMGBTN_LEFT
Left offset of the image in the button.
Definition: window_gui.h:38
SBS_OFF
@ SBS_OFF
Do not sort (with this button).
Definition: window_gui.h:223
NWID_SELECTION
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition: widget_type.h:78
Window::nested_array_size
uint nested_array_size
Size of the nested array.
Definition: window_gui.h:332
NWidgetCore::index
int index
Index of the nested widget in the widget array of the window (-1 means 'not used').
Definition: widget_type.h:313
NWidgetMatrix::colour
Colours colour
Colour of this widget.
Definition: widget_type.h:520
NWidgetCore
Base class for a 'real' widget.
Definition: widget_type.h:292
NWidgetLeaf::closebox_dimension
static Dimension closebox_dimension
Cached size of a closebox widget.
Definition: widget_type.h:792
AWV_RIGHT
@ AWV_RIGHT
Force the arrow to the right.
Definition: widget_type.h:38
WWT_DEBUGBOX
@ WWT_DEBUGBOX
NewGRF debug box (at top-right of a window, between WWT_CAPTION and WWT_SHADEBOX)
Definition: widget_type.h:61
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
CursorVars::pos
Point pos
logical mouse position
Definition: gfx_type.h:117
NWidgetPartDataTip::tooltip
StringID tooltip
Tooltip of the widget.
Definition: widget_type.h:869
WD_CLOSEBOX_BOTTOM
@ WD_CLOSEBOX_BOTTOM
Bottom offset of closebox string.
Definition: window_gui.h:121
WPT_FUNCTION
@ WPT_FUNCTION
Widget part for calling a user function.
Definition: widget_type.h:93
NWidgetLeaf::Draw
void Draw(const Window *w) override
Definition: widget.cpp:2406
NWidgetBase::resize_x
uint resize_x
Horizontal resize step (0 means not resizable).
Definition: widget_type.h:174
AWV_INCREASE
@ AWV_INCREASE
Arrow to the right or in case of RTL to the left.
Definition: widget_type.h:36
NWidgetPartWidget::colour
Colours colour
Widget colour.
Definition: widget_type.h:877
NWidgetPartPaddings::left
uint8 left
Paddings for all directions.
Definition: widget_type.h:886
Window::GetRowFromWidget
int GetRowFromWidget(int clickpos, int widget, int padding, int line_height=-1) const
Compute the row of a widget that a user clicked in.
Definition: window.cpp:201
InitializeWindowViewport
void InitializeWindowViewport(Window *w, int x, int y, int width, int height, uint32 follow_flags, ZoomLevel zoom)
Initialize viewport of the window for use.
Definition: viewport.cpp:222
NWidgetPartTextLines::size
FontSize size
Font size of text lines.
Definition: widget_type.h:904
NWidgetBase::current_x
uint current_x
Current horizontal size (after resizing).
Definition: widget_type.h:182
WD_BEVEL_BOTTOM
@ WD_BEVEL_BOTTOM
Height of bottom bevel border.
Definition: window_gui.h:57
NWidgetStacked::shown_plane
int shown_plane
Plane being displayed (for NWID_SELECTION only).
Definition: widget_type.h:429
NWidgetMatrix::AssignSizePosition
void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl) override
Definition: widget.cpp:1575
NWidgetResizeBase::SetMinimalSize
void SetMinimalSize(uint min_x, uint min_y)
Set minimal size of the widget.
Definition: widget.cpp:815
NWidgetScrollbar::horizontal_dimension
static Dimension horizontal_dimension
Cached size of horizontal scrollbar button.
Definition: widget_type.h:772
WD_IMGBTN_RIGHT
@ WD_IMGBTN_RIGHT
Right offset of the image in the button.
Definition: window_gui.h:39
NWidgetLeaf::SetupSmallestSize
void SetupSmallestSize(Window *w, bool init_array) override
Definition: widget.cpp:2212
MakeNWidget
static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
Construct a single nested widget in *dest from its parts.
Definition: widget.cpp:2575
NWidgetScrollbar::Draw
void Draw(const Window *w) override
Definition: widget.cpp:2039
NWidgetCore::GetWidgetFromPos
NWidgetCore * GetWidgetFromPos(int x, int y) override
Definition: widget.cpp:912
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition: strings_type.h:24
WD_CLOSEBOX_LEFT
@ WD_CLOSEBOX_LEFT
Left offset of closebox string.
Definition: window_gui.h:118
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
NWidgetLeaf::ButtonHit
bool ButtonHit(const Point &pt)
For a NWID_BUTTON_DROPDOWN, test whether pt refers to the button or to the drop-down.
Definition: widget.cpp:2547
MakeWindowNWidgetTree
NWidgetContainer * MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
Make a nested widget tree for a window from a parts array.
Definition: widget.cpp:2827
NWidgetCore::FillNestedArray
void FillNestedArray(NWidgetBase **array, uint length) override
Definition: widget.cpp:907
LeastCommonMultiple
int LeastCommonMultiple(int a, int b)
Compute least common multiple (lcm) of arguments a and b, the smallest integer value that is a multip...
Definition: math_func.cpp:24
NWidgetHorizontal::NWidgetHorizontal
NWidgetHorizontal(NWidContainerFlags flags=NC_NONE)
Horizontal container widget.
Definition: widget.cpp:1136
NWidgetBackground::FillNestedArray
void FillNestedArray(NWidgetBase **array, uint length) override
Definition: widget.cpp:1834
WD_RESIZEBOX_LEFT
@ WD_RESIZEBOX_LEFT
Left offset of resize sprite.
Definition: window_gui.h:111
ND_SCROLLBAR_BTN
@ ND_SCROLLBAR_BTN
Bit value of the 'scrollbar up' or 'scrollbar down' flag.
Definition: widget_type.h:284
WD_STICKYBOX_LEFT
@ WD_STICKYBOX_LEFT
Left offset of sticky sprite.
Definition: window_gui.h:90
WWT_TEXTBTN
@ WWT_TEXTBTN
(Toggle) Button with text
Definition: widget_type.h:53
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:581
NWID_BUTTON_DROPDOWN
@ NWID_BUTTON_DROPDOWN
Button with a drop-down.
Definition: widget_type.h:80
FR_BORDERONLY
@ FR_BORDERONLY
Draw border only, no background.
Definition: window_gui.h:28
NWidgetPartDataTip::data
uint32 data
Data value of the widget.
Definition: widget_type.h:868
NWidgetMatrix::widgets_x
int widgets_x
The number of visible widgets in horizontal direction.
Definition: widget_type.h:527
AWV_DECREASE
@ AWV_DECREASE
Arrow to the left or in case of RTL to the right.
Definition: widget_type.h:35
WWT_DROPDOWN
@ WWT_DROPDOWN
Drop down list.
Definition: widget_type.h:68
ND_NO_TRANSPARENCY
@ ND_NO_TRANSPARENCY
Bit value of the 'no transparency' flag.
Definition: widget_type.h:278
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:155
NWidgetPart::text_lines
NWidgetPartTextLines text_lines
Part with text line data.
Definition: widget_type.h:927
WD_CAPTIONTEXT_RIGHT
@ WD_CAPTIONTEXT_RIGHT
Offset of the caption text at the right.
Definition: window_gui.h:126
WD_DEFSIZEBOX_TOP
@ WD_DEFSIZEBOX_TOP
Top offset of defsize sprite.
Definition: window_gui.h:106
NWidgetBase::SetupSmallestSize
virtual void SetupSmallestSize(Window *w, bool init_array)=0
WWT_SHADEBOX
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:62
NWidgetMatrix::SetClicked
void SetClicked(int clicked)
Sets the clicked widget in the matrix.
Definition: widget.cpp:1498
WPT_ENDCONTAINER
@ WPT_ENDCONTAINER
Widget part to denote end of a container.
Definition: widget_type.h:92