# widget.cpp

Go to the documentation of this file.

    00001 /* $Id: widget.cpp 26362 2014-02-22 19:34:57Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "company_func.h"
    00014 #include "window_gui.h"
    00015 #include "viewport_func.h"
    00016 #include "zoom_func.h"
    00017 #include "strings_func.h"
    00018 #include "transparency.h"
    00019 #include "core/geometry_func.hpp"
    00020 #include "settings_type.h"
    00021 #include "querystring_gui.h"
    00022
    00023 #include "table/sprites.h"
    00024 #include "table/strings.h"
    00025 #include "table/palettes.h"
    00026
    00027 static const char *UPARROW   = "\xEE\x8A\xA0";
    00028 static const char *DOWNARROW = "\xEE\x8A\xAA";
    00029
    00039 static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bool horizontal)
    00040 {
    00041   /* Base for reversion */
    00042   int rev_base = top + bottom;
    00043   int button_size;
    00044   if (horizontal) {
    00045     button_size = NWidgetScrollbar::GetHorizontalDimension().width;
    00046   } else {
    00047     button_size = NWidgetScrollbar::GetVerticalDimension().height;
    00048   }
    00049   top += button_size;    // top    points to just below the up-button
    00050   bottom -= button_size; // bottom points to top of the down-button
    00051
    00052   int height = (bottom - top);
    00053   int pos = sb->GetPosition();
    00054   int count = sb->GetCount();
    00055   int cap = sb->GetCapacity();
    00056
    00057   if (count != 0) top += height * pos / count;
    00058
    00059   if (cap > count) cap = count;
    00060   if (count != 0) bottom -= (count - pos - cap) * height / count;
    00061
    00062   Point pt;
    00063   if (horizontal && _current_text_dir == TD_RTL) {
    00064     pt.x = rev_base - bottom;
    00065     pt.y = rev_base - top;
    00066   } else {
    00067     pt.x = top;
    00068     pt.y = bottom;
    00069   }
    00070   return pt;
    00071 }
    00072
    00082 static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma)
    00083 {
    00084   int pos;
    00085   int button_size;
    00086   bool rtl = false;
    00087
    00088   if (sb->type == NWID_HSCROLLBAR) {
    00089     pos = x;
    00090     rtl = _current_text_dir == TD_RTL;
    00091     button_size = NWidgetScrollbar::GetHorizontalDimension().width;
    00092   } else {
    00093     pos = y;
    00094     button_size = NWidgetScrollbar::GetVerticalDimension().height;
    00095   }
    00096   if (pos < mi + button_size) {
    00097     /* Pressing the upper button? */
    00098     SetBit(sb->disp_flags, NDB_SCROLLBAR_UP);
    00099     if (_scroller_click_timeout <= 1) {
    00100       _scroller_click_timeout = 3;
    00101       sb->UpdatePosition(rtl ? 1 : -1);
    00102     }
    00103     w->scrolling_scrollbar = sb->index;
    00104   } else if (pos >= ma - button_size) {
    00105     /* Pressing the lower button? */
    00106     SetBit(sb->disp_flags, NDB_SCROLLBAR_DOWN);
    00107
    00108     if (_scroller_click_timeout <= 1) {
    00109       _scroller_click_timeout = 3;
    00110       sb->UpdatePosition(rtl ? -1 : 1);
    00111     }
    00112     w->scrolling_scrollbar = sb->index;
    00113   } else {
    00114     Point pt = HandleScrollbarHittest(sb, mi, ma, sb->type == NWID_HSCROLLBAR);
    00115
    00116     if (pos < pt.x) {
    00117       sb->UpdatePosition(rtl ? 1 : -1, Scrollbar::SS_BIG);
    00118     } else if (pos > pt.y) {
    00119       sb->UpdatePosition(rtl ? -1 : 1, Scrollbar::SS_BIG);
    00120     } else {
    00121       _scrollbar_start_pos = pt.x - mi - button_size;
    00122       _scrollbar_size = ma - mi - button_size * 2;
    00123       w->scrolling_scrollbar = sb->index;
    00124       _cursorpos_drag_start = _cursor.pos;
    00125     }
    00126   }
    00127
    00128   w->SetDirty();
    00129 }
    00130
    00139 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
    00140 {
    00141   int mi, ma;
    00142
    00143   if (nw->type == NWID_HSCROLLBAR) {
    00144     mi = nw->pos_x;
    00145     ma = nw->pos_x + nw->current_x;
    00146   } else {
    00147     mi = nw->pos_y;
    00148     ma = nw->pos_y + nw->current_y;
    00149   }
    00150   NWidgetScrollbar *scrollbar = dynamic_cast<NWidgetScrollbar*>(nw);
    00151   assert(scrollbar != NULL);
    00152   ScrollbarClickPositioning(w, scrollbar, x, y, mi, ma);
    00153 }
    00154
    00163 int GetWidgetFromPos(const Window *w, int x, int y)
    00164 {
    00165   NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
    00166   return (nw != NULL) ? nw->index : -1;
    00167 }
    00168
    00178 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
    00179 {
    00180   assert(colour < COLOUR_END);
    00181
    00182   uint dark         = _colour_gradient[colour][3];
    00183   uint medium_dark  = _colour_gradient[colour][5];
    00184   uint medium_light = _colour_gradient[colour][6];
    00185   uint light        = _colour_gradient[colour][7];
    00186
    00187   if (flags & FR_TRANSPARENT) {
    00188     GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR);
    00189   } else {
    00190     uint interior;
    00191
    00192     if (flags & FR_LOWERED) {
    00193       GfxFillRect(left,                 top,                left,                   bottom,                   dark);
    00194       GfxFillRect(left + WD_BEVEL_LEFT, top,                right,                  top,                      dark);
    00195       GfxFillRect(right,                top + WD_BEVEL_TOP, right,                  bottom - WD_BEVEL_BOTTOM, light);
    00196       GfxFillRect(left + WD_BEVEL_LEFT, bottom,             right,                  bottom,                   light);
    00197       interior = (flags & FR_DARKENED ? medium_dark : medium_light);
    00198     } else {
    00199       GfxFillRect(left,                 top,                left,                   bottom - WD_BEVEL_BOTTOM, light);
    00200       GfxFillRect(left + WD_BEVEL_LEFT, top,                right - WD_BEVEL_RIGHT, top,                      light);
    00201       GfxFillRect(right,                top,                right,                  bottom - WD_BEVEL_BOTTOM, dark);
    00202       GfxFillRect(left,                 bottom,             right,                  bottom,                   dark);
    00203       interior = medium_dark;
    00204     }
    00205     if (!(flags & FR_BORDERONLY)) {
    00206       GfxFillRect(left + WD_BEVEL_LEFT, top + WD_BEVEL_TOP, right - WD_BEVEL_RIGHT, bottom - WD_BEVEL_BOTTOM, interior);
    00207     }
    00208   }
    00209 }
    00210
    00219 static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
    00220 {
    00221   assert(img != 0);
    00222   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00223
    00224   if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2.
    00225   DrawSprite(img, PAL_NONE, r.left + WD_IMGBTN_LEFT + clicked, r.top + WD_IMGBTN_TOP + clicked);
    00226 }
    00227
    00235 static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
    00236 {
    00237   if (str == STR_NULL) return;
    00238   if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
    00239   Dimension d = GetStringBoundingBox(str);
    00240   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00241   DrawString(r.left + clicked, r.right + clicked, r.top + offset + clicked, str, TC_FROMSTRING, SA_HOR_CENTER);
    00242 }
    00243
    00250 static inline void DrawText(const Rect &r, TextColour colour, StringID str)
    00251 {
    00252   Dimension d = GetStringBoundingBox(str);
    00253   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00254   if (str != STR_NULL) DrawString(r.left, r.right, r.top + offset, str, colour);
    00255 }
    00256
    00263 static inline void DrawInset(const Rect &r, Colours colour, StringID str)
    00264 {
    00265   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
    00266   if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str);
    00267 }
    00268
    00278 static inline void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint16 data, uint resize_x, uint resize_y)
    00279 {
    00280   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00281
    00282   int num_columns = GB(data, MAT_COL_START, MAT_COL_BITS);  // Lower 8 bits of the widget data: Number of columns in the matrix.
    00283   int column_width; // Width of a single column in the matrix.
    00284   if (num_columns == 0) {
    00285     column_width = resize_x;
    00286     num_columns = (r.right - r.left + 1) / column_width;
    00287   } else {
    00288     column_width = (r.right - r.left + 1) / num_columns;
    00289   }
    00290
    00291   int num_rows = GB(data, MAT_ROW_START, MAT_ROW_BITS); // Upper 8 bits of the widget data: Number of rows in the matrix.
    00292   int row_height; // Height of a single row in the matrix.
    00293   if (num_rows == 0) {
    00294     row_height = resize_y;
    00295     num_rows = (r.bottom - r.top + 1) / row_height;
    00296   } else {
    00297     row_height = (r.bottom - r.top + 1) / num_rows;
    00298   }
    00299
    00300   int col = _colour_gradient[colour & 0xF][6];
    00301
    00302   int x = r.left;
    00303   for (int ctr = num_columns; ctr > 1; ctr--) {
    00304     x += column_width;
    00305     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00306   }
    00307
    00308   x = r.top;
    00309   for (int ctr = num_rows; ctr > 1; ctr--) {
    00310     x += row_height;
    00311     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00312   }
    00313
    00314   col = _colour_gradient[colour & 0xF][4];
    00315
    00316   x = r.left - 1;
    00317   for (int ctr = num_columns; ctr > 1; ctr--) {
    00318     x += column_width;
    00319     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00320   }
    00321
    00322   x = r.top - 1;
    00323   for (int ctr = num_rows; ctr > 1; ctr--) {
    00324     x += row_height;
    00325     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00326   }
    00327 }
    00328
    00338 static inline void DrawVerticalScrollbar(const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
    00339 {
    00340   int centre = (r.right - r.left) / 2;
    00341   int height = NWidgetScrollbar::GetVerticalDimension().height;
    00342
    00343   /* draw up/down buttons */
    00344   DrawFrameRect(r.left, r.top, r.right, r.top + height - 1, colour, (up_clicked) ? FR_LOWERED : FR_NONE);
    00345   DrawString(r.left + up_clicked, r.right + up_clicked, r.top + up_clicked, UPARROW, TC_BLACK, SA_HOR_CENTER);
    00346
    00347   DrawFrameRect(r.left, r.bottom - (height - 1), r.right, r.bottom, colour, (down_clicked) ? FR_LOWERED : FR_NONE);
    00348   DrawString(r.left + down_clicked, r.right + down_clicked, r.bottom - (height - 1) + down_clicked, DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00349
    00350   int c1 = _colour_gradient[colour & 0xF][3];
    00351   int c2 = _colour_gradient[colour & 0xF][7];
    00352
    00353   /* draw "shaded" background */
    00354   GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c2);
    00355   GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c1, FILLRECT_CHECKER);
    00356
    00357   /* draw shaded lines */
    00358   GfxFillRect(r.left + centre - 3, r.top + height, r.left + centre - 3, r.bottom - height, c1);
    00359   GfxFillRect(r.left + centre - 2, r.top + height, r.left + centre - 2, r.bottom - height, c2);
    00360   GfxFillRect(r.left + centre + 2, r.top + height, r.left + centre + 2, r.bottom - height, c1);
    00361   GfxFillRect(r.left + centre + 3, r.top + height, r.left + centre + 3, r.bottom - height, c2);
    00362
    00363   Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom, false);
    00364   DrawFrameRect(r.left, pt.x, r.right, pt.y, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00365 }
    00366
    00376 static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
    00377 {
    00378   int centre = (r.bottom - r.top) / 2;
    00379   int width = NWidgetScrollbar::GetHorizontalDimension().width;
    00380
    00381   DrawFrameRect(r.left, r.top, r.left + width - 1, r.bottom, colour, left_clicked ? FR_LOWERED : FR_NONE);
    00382   DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + left_clicked, r.top + 1 + left_clicked);
    00383
    00384   DrawFrameRect(r.right - (width - 1), r.top, r.right, r.bottom, colour, right_clicked ? FR_LOWERED : FR_NONE);
    00385   DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - (width - 2) + right_clicked, r.top + 1 + right_clicked);
    00386
    00387   int c1 = _colour_gradient[colour & 0xF][3];
    00388   int c2 = _colour_gradient[colour & 0xF][7];
    00389
    00390   /* draw "shaded" background */
    00391   GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c2);
    00392   GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c1, FILLRECT_CHECKER);
    00393
    00394   /* draw shaded lines */
    00395   GfxFillRect(r.left + width, r.top + centre - 3, r.right - width, r.top + centre - 3, c1);
    00396   GfxFillRect(r.left + width, r.top + centre - 2, r.right - width, r.top + centre - 2, c2);
    00397   GfxFillRect(r.left + width, r.top + centre + 2, r.right - width, r.top + centre + 2, c1);
    00398   GfxFillRect(r.left + width, r.top + centre + 3, r.right - width, r.top + centre + 3, c2);
    00399
    00400   /* draw actual scrollbar */
    00401   Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right, true);
    00402   DrawFrameRect(pt.x, r.top, pt.y, r.bottom, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00403 }
    00404
    00411 static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
    00412 {
    00413   int x2 = r.left; // by default the left side is the left side of the widget
    00414
    00415   if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str);
    00416
    00417   int c1 = _colour_gradient[colour][3];
    00418   int c2 = _colour_gradient[colour][7];
    00419
    00420   /* If the frame has text, adjust the top bar to fit half-way through */
    00421   int dy1 = 4;
    00422   if (str != STR_NULL) dy1 = FONT_HEIGHT_NORMAL / 2 - 1;
    00423   int dy2 = dy1 + 1;
    00424
    00425   if (_current_text_dir == TD_LTR) {
    00426     /* Line from upper left corner to start of text */
    00427     GfxFillRect(r.left, r.top + dy1, r.left + 4, r.top + dy1, c1);
    00428     GfxFillRect(r.left + 1, r.top + dy2, r.left + 4, r.top + dy2, c2);
    00429
    00430     /* Line from end of text to upper right corner */
    00431     GfxFillRect(x2, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00432     GfxFillRect(x2, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00433   } else {
    00434     /* Line from upper left corner to start of text */
    00435     GfxFillRect(r.left, r.top + dy1, x2 - 2, r.top + dy1, c1);
    00436     GfxFillRect(r.left + 1, r.top + dy2, x2 - 2, r.top + dy2, c2);
    00437
    00438     /* Line from end of text to upper right corner */
    00439     GfxFillRect(r.right - 5, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00440     GfxFillRect(r.right - 5, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00441   }
    00442
    00443   /* Line from upper left corner to bottom left corner */
    00444   GfxFillRect(r.left, r.top + dy2, r.left, r.bottom - 1, c1);
    00445   GfxFillRect(r.left + 1, r.top + dy2 + 1, r.left + 1, r.bottom - 2, c2);
    00446
    00447   /* Line from upper right corner to bottom right corner */
    00448   GfxFillRect(r.right - 1, r.top + dy2, r.right - 1, r.bottom - 2, c1);
    00449   GfxFillRect(r.right, r.top + dy1, r.right, r.bottom - 1, c2);
    00450
    00451   GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
    00452   GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
    00453 }
    00454
    00461 static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
    00462 {
    00463   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00464   DrawSprite((clicked) ? SPR_WINDOW_SHADE : SPR_WINDOW_UNSHADE, PAL_NONE, r.left + WD_SHADEBOX_LEFT + clicked, r.top + WD_SHADEBOX_TOP + clicked);
    00465 }
    00466
    00473 static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
    00474 {
    00475   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00476   DrawSprite((clicked) ? SPR_PIN_UP : SPR_PIN_DOWN, PAL_NONE, r.left + WD_STICKYBOX_LEFT + clicked, r.top + WD_STICKYBOX_TOP + clicked);
    00477 }
    00478
    00485 static inline void DrawDefSizeBox(const Rect &r, Colours colour, bool clicked)
    00486 {
    00487   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00488   DrawSprite(SPR_WINDOW_DEFSIZE, PAL_NONE, r.left + WD_DEFSIZEBOX_LEFT + clicked, r.top + WD_DEFSIZEBOX_TOP + clicked);
    00489 }
    00490
    00497 static inline void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
    00498 {
    00499   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00500   DrawSprite(SPR_WINDOW_DEBUG, PAL_NONE, r.left + WD_DEBUGBOX_LEFT + clicked, r.top + WD_DEBUGBOX_TOP + clicked);
    00501 }
    00502
    00510 static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
    00511 {
    00512   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00513   if (at_left) {
    00514     DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked,
    00515          r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_LEFT).height + clicked);
    00516   } else {
    00517     DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.left + WD_RESIZEBOX_LEFT + clicked,
    00518          r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT).height + clicked);
    00519   }
    00520 }
    00521
    00528 static inline void DrawCloseBox(const Rect &r, Colours colour, StringID str)
    00529 {
    00530   assert(str == STR_BLACK_CROSS || str == STR_SILVER_CROSS); // black or silver cross
    00531   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE);
    00532   DrawString(r.left, r.right, r.top + WD_CLOSEBOX_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
    00533 }
    00534
    00542 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
    00543 {
    00544   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_BORDERONLY);
    00545   DrawFrameRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, colour, (owner == INVALID_OWNER) ? FR_LOWERED | FR_DARKENED : FR_LOWERED | FR_DARKENED | FR_BORDERONLY);
    00546
    00547   if (owner != INVALID_OWNER) {
    00548     GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]);
    00549   }
    00550
    00551   if (str != STR_NULL) {
    00552     Dimension d = GetStringBoundingBox(str);
    00553     int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00554     DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + offset, str, TC_FROMSTRING, SA_HOR_CENTER);
    00555   }
    00556 }
    00557
    00568 static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
    00569 {
    00570   int text_offset = max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered
    00571
    00572   int dd_width = NWidgetLeaf::dropdown_dimension.width;
    00573
    00574   if (_current_text_dir == TD_LTR) {
    00575     DrawFrameRect(r.left, r.top, r.right - dd_width, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00576     DrawFrameRect(r.right + 1 - dd_width, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00577     DrawString(r.right - dd_width + (clicked_dropdown ? 2 : 1), r.right, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00578     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);
    00579   } else {
    00580     DrawFrameRect(r.left + dd_width, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00581     DrawFrameRect(r.left, r.top, r.left + dd_width - 1, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00582     DrawString(r.left + (clicked_dropdown ? 2 : 1), r.left + dd_width, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00583     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);
    00584   }
    00585 }
    00586
    00594 static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
    00595 {
    00596   DrawButtonDropdown(r, colour, false, clicked, str);
    00597 }
    00598
    00602 void Window::DrawWidgets() const
    00603 {
    00604   this->nested_root->Draw(this);
    00605
    00606   if (this->flags & WF_WHITE_BORDER) {
    00607     DrawFrameRect(0, 0, this->width - 1, this->height - 1, COLOUR_WHITE, FR_BORDERONLY);
    00608   }
    00609
    00610   if (this->flags & WF_HIGHLIGHTED) {
    00611     extern bool _window_highlight_colour;
    00612     for (uint i = 0; i < this->nested_array_size; i++) {
    00613       const NWidgetBase *widget = this->GetWidget<NWidgetBase>(i);
    00614       if (widget == NULL || !widget->IsHighlighted()) continue;
    00615
    00616       int left = widget->pos_x;
    00617       int top  = widget->pos_y;
    00618       int right  = left + widget->current_x - 1;
    00619       int bottom = top  + widget->current_y - 1;
    00620
    00621       int colour = _string_colourmap[_window_highlight_colour ? widget->GetHighlightColour() : TC_WHITE];
    00622
    00623       GfxFillRect(left,                 top,    left,                   bottom - WD_BEVEL_BOTTOM, colour);
    00624       GfxFillRect(left + WD_BEVEL_LEFT, top,    right - WD_BEVEL_RIGHT, top,                      colour);
    00625       GfxFillRect(right,                top,    right,                  bottom - WD_BEVEL_BOTTOM, colour);
    00626       GfxFillRect(left,                 bottom, right,                  bottom,                   colour);
    00627     }
    00628   }
    00629 }
    00630
    00636 void Window::DrawSortButtonState(int widget, SortButtonState state) const
    00637 {
    00638   if (state == SBS_OFF) return;
    00639
    00640   assert(this->nested_array != NULL);
    00641   const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
    00642
    00643   int offset = this->IsWidgetLowered(widget) ? 1 : 0;
    00644   int base = offset + nwid->pos_x + (_current_text_dir == TD_LTR ? nwid->current_x - WD_SORTBUTTON_ARROW_WIDTH : 0);
    00645   int top = nwid->pos_y;
    00646
    00647   DrawString(base, base + WD_SORTBUTTON_ARROW_WIDTH, top + 1 + offset, state == SBS_DOWN ? DOWNARROW : UPARROW, TC_BLACK, SA_HOR_CENTER);
    00648 }
    00649
    00650
    00708 NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator()
    00709 {
    00710   this->type = tp;
    00711 }
    00712
    00713 /* ~NWidgetContainer() takes care of #next and #prev data members. */
    00714
    00762 void NWidgetBase::SetDirty(const Window *w) const
    00763 {
    00764   int abs_left = w->left + this->pos_x;
    00765   int abs_top = w->top + this->pos_y;
    00766   SetDirtyBlocks(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
    00767 }
    00768
    00782 NWidgetBase *NWidgetBase::GetWidgetOfType(WidgetType tp)
    00783 {
    00784   return (this->type == tp) ? this : NULL;
    00785 }
    00786
    00793 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y) : NWidgetBase(tp)
    00794 {
    00795   this->fill_x = fill_x;
    00796   this->fill_y = fill_y;
    00797 }
    00798
    00804 void NWidgetResizeBase::SetMinimalSize(uint min_x, uint min_y)
    00805 {
    00806   this->min_x = min_x;
    00807   this->min_y = min_y;
    00808 }
    00809
    00816 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
    00817 {
    00818   this->min_y = min_lines * GetCharacterHeight(size) + spacing;
    00819 }
    00820
    00826 void NWidgetResizeBase::SetFill(uint fill_x, uint fill_y)
    00827 {
    00828   this->fill_x = fill_x;
    00829   this->fill_y = fill_y;
    00830 }
    00831
    00837 void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y)
    00838 {
    00839   this->resize_x = resize_x;
    00840   this->resize_y = resize_y;
    00841 }
    00842
    00843 void NWidgetResizeBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00844 {
    00845   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    00846 }
    00847
    00857 NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint32 widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y)
    00858 {
    00859   this->colour = colour;
    00860   this->index = -1;
    00861   this->widget_data = widget_data;
    00862   this->tool_tip = tool_tip;
    00863   this->scrollbar_index = -1;
    00864 }
    00865
    00870 void NWidgetCore::SetIndex(int index)
    00871 {
    00872   assert(index >= 0);
    00873   this->index = index;
    00874 }
    00875
    00881 void NWidgetCore::SetDataTip(uint32 widget_data, StringID tool_tip)
    00882 {
    00883   this->widget_data = widget_data;
    00884   this->tool_tip = tool_tip;
    00885 }
    00886
    00887 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
    00888 {
    00889   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    00890 }
    00891
    00892 NWidgetCore *NWidgetCore::GetWidgetFromPos(int x, int y)
    00893 {
    00894   return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : NULL;
    00895 }
    00896
    00901 NWidgetContainer::NWidgetContainer(WidgetType tp) : NWidgetBase(tp)
    00902 {
    00903   this->head = NULL;
    00904   this->tail = NULL;
    00905 }
    00906
    00907 NWidgetContainer::~NWidgetContainer()
    00908 {
    00909   while (this->head != NULL) {
    00910     NWidgetBase *wid = this->head->next;
    00911     delete this->head;
    00912     this->head = wid;
    00913   }
    00914   this->tail = NULL;
    00915 }
    00916
    00917 NWidgetBase *NWidgetContainer::GetWidgetOfType(WidgetType tp)
    00918 {
    00919   if (this->type == tp) return this;
    00920   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00921     NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
    00922     if (nwid != NULL) return nwid;
    00923   }
    00924   return NULL;
    00925 }
    00926
    00931 void NWidgetContainer::Add(NWidgetBase *wid)
    00932 {
    00933   assert(wid->next == NULL && wid->prev == NULL);
    00934
    00935   if (this->head == NULL) {
    00936     this->head = wid;
    00937     this->tail = wid;
    00938   } else {
    00939     assert(this->tail != NULL);
    00940     assert(this->tail->next == NULL);
    00941
    00942     this->tail->next = wid;
    00943     wid->prev = this->tail;
    00944     this->tail = wid;
    00945   }
    00946 }
    00947
    00948 void NWidgetContainer::FillNestedArray(NWidgetBase **array, uint length)
    00949 {
    00950   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00951     child_wid->FillNestedArray(array, length);
    00952   }
    00953 }
    00954
    00958 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION)
    00959 {
    00960   this->index = -1;
    00961 }
    00962
    00963 void NWidgetStacked::SetIndex(int index)
    00964 {
    00965   this->index = index;
    00966 }
    00967
    00968 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
    00969 {
    00970   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    00971     assert(w->nested_array_size > (uint)this->index);
    00972     w->nested_array[this->index] = this;
    00973   }
    00974
    00975   /* Zero size plane selected */
    00976   if (this->shown_plane >= SZSP_BEGIN) {
    00977     Dimension size    = {0, 0};
    00978     Dimension padding = {0, 0};
    00979     Dimension fill    = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00980     Dimension resize  = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00981     /* Here we're primarily interested in the value of resize */
    00982     if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    00983
    00984     this->smallest_x = size.width;
    00985     this->smallest_y = size.height;
    00986     this->fill_x = fill.width;
    00987     this->fill_y = fill.height;
    00988     this->resize_x = resize.width;
    00989     this->resize_y = resize.height;
    00990     return;
    00991   }
    00992
    00993   /* First sweep, recurse down and compute minimal size and filling. */
    00994   this->smallest_x = 0;
    00995   this->smallest_y = 0;
    00996   this->fill_x = (this->head != NULL) ? 1 : 0;
    00997   this->fill_y = (this->head != NULL) ? 1 : 0;
    00998   this->resize_x = (this->head != NULL) ? 1 : 0;
    00999   this->resize_y = (this->head != NULL) ? 1 : 0;
    01000   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01001     child_wid->SetupSmallestSize(w, init_array);
    01002
    01003     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    01004     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    01005     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    01006     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    01007     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    01008     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    01009   }
    01010 }
    01011
    01012 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01013 {
    01014   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01015   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01016
    01017   if (this->shown_plane >= SZSP_BEGIN) return;
    01018
    01019   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01020     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    01021     uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    01022     uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
    01023
    01024     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    01025     uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    01026     uint child_pos_y = child_wid->padding_top;
    01027
    01028     child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
    01029   }
    01030 }
    01031
    01032 void NWidgetStacked::FillNestedArray(NWidgetBase **array, uint length)
    01033 {
    01034   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01035   NWidgetContainer::FillNestedArray(array, length);
    01036 }
    01037
    01038 void NWidgetStacked::Draw(const Window *w)
    01039 {
    01040   if (this->shown_plane >= SZSP_BEGIN) return;
    01041
    01042   int plane = 0;
    01043   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01044     if (plane == this->shown_plane) {
    01045       child_wid->Draw(w);
    01046       return;
    01047     }
    01048   }
    01049
    01050   NOT_REACHED();
    01051 }
    01052
    01053 NWidgetCore *NWidgetStacked::GetWidgetFromPos(int x, int y)
    01054 {
    01055   if (this->shown_plane >= SZSP_BEGIN) return NULL;
    01056
    01057   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01058   int plane = 0;
    01059   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01060     if (plane == this->shown_plane) {
    01061       return child_wid->GetWidgetFromPos(x, y);
    01062     }
    01063   }
    01064   return NULL;
    01065 }
    01066
    01071 void NWidgetStacked::SetDisplayedPlane(int plane)
    01072 {
    01073   this->shown_plane = plane;
    01074 }
    01075
    01076 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
    01077 {
    01078   this->flags = flags;
    01079 }
    01080
    01090 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01091 {
    01092   this->pip_pre = pip_pre;
    01093   this->pip_inter = pip_inter;
    01094   this->pip_post = pip_post;
    01095 }
    01096
    01097 void NWidgetPIPContainer::Draw(const Window *w)
    01098 {
    01099   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01100     child_wid->Draw(w);
    01101   }
    01102 }
    01103
    01104 NWidgetCore *NWidgetPIPContainer::GetWidgetFromPos(int x, int y)
    01105 {
    01106   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01107
    01108   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01109     NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
    01110     if (nwid != NULL) return nwid;
    01111   }
    01112   return NULL;
    01113 }
    01114
    01116 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
    01117 {
    01118 }
    01119
    01120 void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array)
    01121 {
    01122   this->smallest_x = 0; // Sum of minimal size of all children.
    01123   this->smallest_y = 0; // Biggest child.
    01124   this->fill_x = 0;     // smallest non-zero child widget fill step.
    01125   this->fill_y = 1;     // smallest common child fill step.
    01126   this->resize_x = 0;   // smallest non-zero child widget resize step.
    01127   this->resize_y = 1;   // smallest common child resize step.
    01128
    01129   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01130   uint longest = 0; // Longest child found.
    01131   uint max_vert_fill = 0; // Biggest vertical fill step.
    01132   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01133     child_wid->SetupSmallestSize(w, init_array);
    01134     longest = max(longest, child_wid->smallest_x);
    01135     max_vert_fill = max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
    01136     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    01137   }
    01138   /* 1b. Make the container higher if needed to accommodate all children nicely. */
    01139   uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height.
    01140   uint cur_height = this->smallest_y;
    01141   for (;;) {
    01142     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01143       uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
    01144       uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01145       if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting children are not interesting.
    01146         uint remainder = (cur_height - child_height) % step_size;
    01147         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01148           cur_height += step_size - remainder;
    01149           assert(cur_height < max_smallest); // Safeguard against infinite height expansion.
    01150           /* Remaining children will adapt to the new cur_height, thus speeding up the computation. */
    01151         }
    01152       }
    01153     }
    01154     if (this->smallest_y == cur_height) break;
    01155     this->smallest_y = cur_height; // Smallest height got changed, try again.
    01156   }
    01157   /* 2. For containers that must maintain equal width, extend child minimal size. */
    01158   if (this->flags & NC_EQUALSIZE) {
    01159     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01160       if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
    01161     }
    01162   }
    01163   /* 3. Move PIP space to the children, compute smallest, fill, and resize values of the container. */
    01164   if (this->head != NULL) this->head->padding_left += this->pip_pre;
    01165   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01166     if (child_wid->next != NULL) {
    01167       child_wid->padding_right += this->pip_inter;
    01168     } else {
    01169       child_wid->padding_right += this->pip_post;
    01170     }
    01171
    01172     this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01173     if (child_wid->fill_x > 0) {
    01174       if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
    01175     }
    01176     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    01177
    01178     if (child_wid->resize_x > 0) {
    01179       if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
    01180     }
    01181     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    01182   }
    01183   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01184   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01185 }
    01186
    01187 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01188 {
    01189   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01190
    01191   /* Compute additional width given to us. */
    01192   uint additional_length = given_width;
    01193   if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
    01194     /* For EQUALSIZE containers this does not sum to smallest_x during initialisation */
    01195     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01196       additional_length -= child_wid->smallest_x + child_wid->padding_right + child_wid->padding_left;
    01197     }
    01198   } else {
    01199     additional_length -= this->smallest_x;
    01200   }
    01201
    01202   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01203
    01204   /* In principle, the additional horizontal space is distributed evenly over the available resizable children. Due to step sizes, this may not always be feasible.
    01205    * To make resizing work as good as possible, first children with biggest step sizes are done. These may get less due to rounding down.
    01206    * 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
    01207    * of the child with the smallest non-zero stepsize.
    01208    *
    01209    * 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
    01210    * size and position, and directly call child->AssignSizePosition() with the computed values.
    01211    * 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
    01212    * then we call the child.
    01213    */
    01214
    01215   /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle vertical size for all children,
    01216    * handle horizontal size for non-resizing children.
    01217    */
    01218   int num_changing_childs = 0; // Number of children that can change size.
    01219   uint biggest_stepsize = 0;
    01220   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01221     uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01222     if (hor_step > 0) {
    01223       num_changing_childs++;
    01224       biggest_stepsize = max(biggest_stepsize, hor_step);
    01225     } else {
    01226       child_wid->current_x = child_wid->smallest_x;
    01227     }
    01228
    01229     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    01230     child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    01231   }
    01232
    01233   /* Second loop: Allocate the additional horizontal space over the resizing children, starting with the biggest resize steps. */
    01234   while (biggest_stepsize > 0) {
    01235     uint next_biggest_stepsize = 0;
    01236     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01237       uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01238       if (hor_step > biggest_stepsize) continue; // Already done
    01239       if (hor_step == biggest_stepsize) {
    01240         uint increment = additional_length / num_changing_childs;
    01241         num_changing_childs--;
    01242         if (hor_step > 1) increment -= increment % hor_step;
    01243         child_wid->current_x = child_wid->smallest_x + increment;
    01244         additional_length -= increment;
    01245         continue;
    01246       }
    01247       next_biggest_stepsize = max(next_biggest_stepsize, hor_step);
    01248     }
    01249     biggest_stepsize = next_biggest_stepsize;
    01250   }
    01251   assert(num_changing_childs == 0);
    01252
    01253   /* Third loop: Compute position and call the child. */
    01254   uint position = rtl ? this->current_x : 0; // Place to put next child relative to origin of the container.
    01255   NWidgetBase *child_wid = this->head;
    01256   while (child_wid != NULL) {
    01257     uint child_width = child_wid->current_x;
    01258     uint child_x = x + (rtl ? position - child_width - child_wid->padding_left : position + child_wid->padding_left);
    01259     uint child_y = y + child_wid->padding_top;
    01260
    01261     child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
    01262     uint padded_child_width = child_width + child_wid->padding_right + child_wid->padding_left;
    01263     position = rtl ? position - padded_child_width : position + padded_child_width;
    01264
    01265     child_wid = child_wid->next;
    01266   }
    01267 }
    01268
    01270 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
    01271 {
    01272   this->type = NWID_HORIZONTAL_LTR;
    01273 }
    01274
    01275 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01276 {
    01277   NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
    01278 }
    01279
    01281 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
    01282 {
    01283 }
    01284
    01285 void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array)
    01286 {
    01287   this->smallest_x = 0; // Biggest child.
    01288   this->smallest_y = 0; // Sum of minimal size of all children.
    01289   this->fill_x = 1;     // smallest common child fill step.
    01290   this->fill_y = 0;     // smallest non-zero child widget fill step.
    01291   this->resize_x = 1;   // smallest common child resize step.
    01292   this->resize_y = 0;   // smallest non-zero child widget resize step.
    01293
    01294   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01295   uint highest = 0; // Highest child found.
    01296   uint max_hor_fill = 0; // Biggest horizontal fill step.
    01297   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01298     child_wid->SetupSmallestSize(w, init_array);
    01299     highest = max(highest, child_wid->smallest_y);
    01300     max_hor_fill = max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
    01301     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    01302   }
    01303   /* 1b. Make the container wider if needed to accommodate all children nicely. */
    01304   uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height.
    01305   uint cur_width = this->smallest_x;
    01306   for (;;) {
    01307     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01308       uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
    01309       uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01310       if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting children are not interesting.
    01311         uint remainder = (cur_width - child_width) % step_size;
    01312         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01313           cur_width += step_size - remainder;
    01314           assert(cur_width < max_smallest); // Safeguard against infinite width expansion.
    01315           /* Remaining children will adapt to the new cur_width, thus speeding up the computation. */
    01316         }
    01317       }
    01318     }
    01319     if (this->smallest_x == cur_width) break;
    01320     this->smallest_x = cur_width; // Smallest width got changed, try again.
    01321   }
    01322   /* 2. For containers that must maintain equal width, extend children minimal size. */
    01323   if (this->flags & NC_EQUALSIZE) {
    01324     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01325       if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
    01326     }
    01327   }
    01328   /* 3. Move PIP space to the child, compute smallest, fill, and resize values of the container. */
    01329   if (this->head != NULL) this->head->padding_top += this->pip_pre;
    01330   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01331     if (child_wid->next != NULL) {
    01332       child_wid->padding_bottom += this->pip_inter;
    01333     } else {
    01334       child_wid->padding_bottom += this->pip_post;
    01335     }
    01336
    01337     this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01338     if (child_wid->fill_y > 0) {
    01339       if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
    01340     }
    01341     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    01342
    01343     if (child_wid->resize_y > 0) {
    01344       if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
    01345     }
    01346     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    01347   }
    01348   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01349   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01350 }
    01351
    01352 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01353 {
    01354   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01355
    01356   /* Compute additional height given to us. */
    01357   uint additional_length = given_height;
    01358   if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
    01359     /* For EQUALSIZE containers this does not sum to smallest_y during initialisation */
    01360     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01361       additional_length -= child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01362     }
    01363   } else {
    01364     additional_length -= this->smallest_y;
    01365   }
    01366
    01367   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01368
    01369   /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the children with the biggest resize steps.
    01370    * It also stores computed widths and heights into current_x and current_y values of the child.
    01371    */
    01372
    01373   /* 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. */
    01374   int num_changing_childs = 0; // Number of children that can change size.
    01375   uint biggest_stepsize = 0;
    01376   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01377     uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01378     if (vert_step > 0) {
    01379       num_changing_childs++;
    01380       biggest_stepsize = max(biggest_stepsize, vert_step);
    01381     } else {
    01382       child_wid->current_y = child_wid->smallest_y;
    01383     }
    01384
    01385     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    01386     child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    01387   }
    01388
    01389   /* Second loop: Allocate the additional vertical space over the resizing children, starting with the biggest resize steps. */
    01390   while (biggest_stepsize > 0) {
    01391     uint next_biggest_stepsize = 0;
    01392     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01393       uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01394       if (vert_step > biggest_stepsize) continue; // Already done
    01395       if (vert_step == biggest_stepsize) {
    01396         uint increment = additional_length / num_changing_childs;
    01397         num_changing_childs--;
    01398         if (vert_step > 1) increment -= increment % vert_step;
    01399         child_wid->current_y = child_wid->smallest_y + increment;
    01400         additional_length -= increment;
    01401         continue;
    01402       }
    01403       next_biggest_stepsize = max(next_biggest_stepsize, vert_step);
    01404     }
    01405     biggest_stepsize = next_biggest_stepsize;
    01406   }
    01407   assert(num_changing_childs == 0);
    01408
    01409   /* Third loop: Compute position and call the child. */
    01410   uint position = 0; // Place to put next child relative to origin of the container.
    01411   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01412     uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01413     uint child_height = child_wid->current_y;
    01414
    01415     child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
    01416     position += child_height + child_wid->padding_top + child_wid->padding_bottom;
    01417   }
    01418 }
    01419
    01425 NWidgetSpacer::NWidgetSpacer(int length, int height) : NWidgetResizeBase(NWID_SPACER, 0, 0)
    01426 {
    01427   this->SetMinimalSize(length, height);
    01428   this->SetResize(0, 0);
    01429 }
    01430
    01431 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
    01432 {
    01433   this->smallest_x = this->min_x;
    01434   this->smallest_y = this->min_y;
    01435 }
    01436
    01437 void NWidgetSpacer::FillNestedArray(NWidgetBase **array, uint length)
    01438 {
    01439 }
    01440
    01441 void NWidgetSpacer::Draw(const Window *w)
    01442 {
    01443   /* Spacer widget is never visible. */
    01444 }
    01445
    01446 void NWidgetSpacer::SetDirty(const Window *w) const
    01447 {
    01448   /* Spacer widget never need repainting. */
    01449 }
    01450
    01451 NWidgetCore *NWidgetSpacer::GetWidgetFromPos(int x, int y)
    01452 {
    01453   return NULL;
    01454 }
    01455
    01456 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX, NC_EQUALSIZE), index(-1), clicked(-1), count(-1)
    01457 {
    01458 }
    01459
    01460 void NWidgetMatrix::SetIndex(int index)
    01461 {
    01462   this->index = index;
    01463 }
    01464
    01465 void NWidgetMatrix::SetColour(Colours colour)
    01466 {
    01467   this->colour = colour;
    01468 }
    01469
    01474 void NWidgetMatrix::SetClicked(int clicked)
    01475 {
    01476   this->clicked = clicked;
    01477   if (this->clicked >= 0 && this->sb != NULL && this->widgets_x != 0) {
    01478     int vpos = (this->clicked / this->widgets_x) * this->widget_h; // Vertical position of the top.
    01479     /* Need to scroll down -> Scroll to the bottom.
    01480      * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
    01481     if (this->sb->GetPosition() < vpos) vpos += this->widget_h - this->pip_inter - 1;
    01482     this->sb->ScrollTowards(vpos);
    01483   }
    01484 }
    01485
    01491 void NWidgetMatrix::SetCount(int count)
    01492 {
    01493   this->count = count;
    01494
    01495   if (this->sb == NULL || this->widgets_x == 0) return;
    01496
    01497   /* We need to get the number of pixels the matrix is high/wide.
    01498    * So, determine the number of rows/columns based on the number of
    01499    * columns/rows (one is constant/unscrollable).
    01500    * Then multiply that by the height of a widget, and add the pre
    01501    * and post spacing "offsets". */
    01502   count = CeilDiv(count, this->sb->IsVertical() ? this->widgets_x : this->widgets_y);
    01503   count *= (this->sb->IsVertical() ? this->head->smallest_y : this->head->smallest_x) + this->pip_inter;
    01504   if (count > 0) count -= this->pip_inter; // We counted an inter too much in the multiplication above
    01505   count += this->pip_pre + this->pip_post;
    01506   this->sb->SetCount(count);
    01507   this->sb->SetCapacity(this->sb->IsVertical() ? this->current_y : this->current_x);
    01508   this->sb->SetStepSize(this->sb->IsVertical() ? this->widget_h  : this->widget_w);
    01509 }
    01510
    01515 void NWidgetMatrix::SetScrollbar(Scrollbar *sb)
    01516 {
    01517   this->sb = sb;
    01518 }
    01519
    01520 void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array)
    01521 {
    01522   assert(this->head != NULL);
    01523   assert(this->head->next == NULL);
    01524
    01525   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    01526     assert(w->nested_array_size > (uint)this->index);
    01527     w->nested_array[this->index] = this;
    01528   }
    01529
    01530   /* Reset the widget number. */
    01531   NWidgetCore *nw = dynamic_cast<NWidgetCore *>(this->head);
    01532   assert(nw != NULL);
    01533   SB(nw->index, 16, 16, 0);
    01534   this->head->SetupSmallestSize(w, init_array);
    01535
    01536   Dimension padding = {this->pip_pre + this->pip_post, this->pip_pre + this->pip_post};
    01537   Dimension size    = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height};
    01538   Dimension fill    = {0, 0};
    01539   Dimension resize  = {this->pip_inter + this->head->smallest_x, this->pip_inter + this->head->smallest_y};
    01540
    01541   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    01542
    01543   this->smallest_x = size.width;
    01544   this->smallest_y = size.height;
    01545   this->fill_x = fill.width;
    01546   this->fill_y = fill.height;
    01547   this->resize_x = resize.width;
    01548   this->resize_y = resize.height;
    01549 }
    01550
    01551 void NWidgetMatrix::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01552 {
    01553   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01554
    01555   this->pos_x = x;
    01556   this->pos_y = y;
    01557   this->current_x = given_width;
    01558   this->current_y = given_height;
    01559
    01560   /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
    01561   this->widget_w = this->head->smallest_x + this->pip_inter;
    01562   this->widget_h = this->head->smallest_y + this->pip_inter;
    01563
    01564   /* Account for the pip_inter is between widgets, so we need to account for that when
    01565    * the division assumes pip_inter is used for all widgets. */
    01566   this->widgets_x = CeilDiv(this->current_x - this->pip_pre - this->pip_post + this->pip_inter, this->widget_w);
    01567   this->widgets_y = CeilDiv(this->current_y - this->pip_pre - this->pip_post + this->pip_inter, this->widget_h);
    01568
    01569   /* When resizing, update the scrollbar's count. E.g. with a vertical
    01570    * scrollbar becoming wider or narrower means the amount of rows in
    01571    * the scrollbar becomes respectively smaller or higher. */
    01572   this->SetCount(this->count);
    01573 }
    01574
    01575 void NWidgetMatrix::FillNestedArray(NWidgetBase **array, uint length)
    01576 {
    01577   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01578   NWidgetContainer::FillNestedArray(array, length);
    01579 }
    01580
    01581 NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y)
    01582 {
    01583   /* Falls outside of the matrix widget. */
    01584   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01585
    01586   int start_x, start_y, base_offs_x, base_offs_y;
    01587   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01588
    01589   bool rtl = _current_text_dir == TD_RTL;
    01590
    01591   int widget_col = (rtl ?
    01592         -x + (int)this->pip_post + (int)this->pos_x + base_offs_x + (int)this->widget_w - 1 - (int)this->pip_inter :
    01593          x - (int)this->pip_pre  - (int)this->pos_x - base_offs_x
    01594       ) / this->widget_w;
    01595
    01596   int widget_row = (y - base_offs_y - (int)this->pip_pre - (int)this->pos_y) / this->widget_h;
    01597
    01598   int sub_wid = (widget_row + start_y) * this->widgets_x + start_x + widget_col;
    01599   if (sub_wid >= this->count) return NULL;
    01600
    01601   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01602   assert(child != NULL);
    01603   child->AssignSizePosition(ST_RESIZE,
    01604       this->pos_x + (rtl ? this->pip_post - widget_col * this->widget_w : this->pip_pre + widget_col * this->widget_w) + base_offs_x,
    01605       this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y,
    01606       child->smallest_x, child->smallest_y, rtl);
    01607
    01608   SB(child->index, 16, 16, sub_wid);
    01609
    01610   return child->GetWidgetFromPos(x, y);
    01611 }
    01612
    01613 /* virtual */ void NWidgetMatrix::Draw(const Window *w)
    01614 {
    01615   /* Fill the background. */
    01616   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]);
    01617
    01618   /* Set up a clipping area for the previews. */
    01619   bool rtl = _current_text_dir == TD_RTL;
    01620   DrawPixelInfo tmp_dpi;
    01621   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;
    01622   DrawPixelInfo *old_dpi = _cur_dpi;
    01623   _cur_dpi = &tmp_dpi;
    01624
    01625   /* Get the appropriate offsets so we can draw the right widgets. */
    01626   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01627   assert(child != NULL);
    01628   int start_x, start_y, base_offs_x, base_offs_y;
    01629   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01630
    01631   int offs_y = base_offs_y;
    01632   for (int y = start_y; y < start_y + this->widgets_y + 1; y++, offs_y += this->widget_h) {
    01633     /* Are we within bounds? */
    01634     if (offs_y + child->smallest_y <= 0) continue;
    01635     if (offs_y >= (int)this->current_y) break;
    01636
    01637     /* We've passed our amount of widgets. */
    01638     if (y * this->widgets_x >= this->count) break;
    01639
    01640     int offs_x = base_offs_x;
    01641     for (int x = start_x; x < start_x + this->widgets_x + 1; x++, offs_x += rtl ? -this->widget_w : this->widget_w) {
    01642       /* Are we within bounds? */
    01643       if (offs_x + child->smallest_x <= 0) continue;
    01644       if (offs_x >= (int)this->current_x) continue;
    01645
    01646       /* Do we have this many widgets? */
    01647       int sub_wid = y * this->widgets_x + x;
    01648       if (sub_wid >= this->count) break;
    01649
    01650       child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl);
    01651       child->SetLowered(this->clicked == sub_wid);
    01652       SB(child->index, 16, 16, sub_wid);
    01653       child->Draw(w);
    01654     }
    01655   }
    01656
    01657   /* Restore the clipping area. */
    01658   _cur_dpi = old_dpi;
    01659 }
    01660
    01668 void NWidgetMatrix::GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
    01669 {
    01670   base_offs_x = _current_text_dir == TD_RTL ? this->widget_w * (this->widgets_x - 1) : 0;
    01671   base_offs_y = 0;
    01672   start_x = 0;
    01673   start_y = 0;
    01674   if (this->sb != NULL) {
    01675     if (this->sb->IsVertical()) {
    01676       start_y = this->sb->GetPosition() / this->widget_h;
    01677       base_offs_y += -this->sb->GetPosition() + start_y * this->widget_h;
    01678     } else {
    01679       start_x = this->sb->GetPosition() / this->widget_w;
    01680       int sub_x = this->sb->GetPosition() - start_x * this->widget_w;
    01681       if (_current_text_dir == TD_RTL) {
    01682         base_offs_x += sub_x;
    01683       } else {
    01684         base_offs_x -= sub_x;
    01685       }
    01686     }
    01687   }
    01688 }
    01689
    01699 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
    01700 {
    01701   assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
    01702   if (index >= 0) this->SetIndex(index);
    01703   this->child = child;
    01704 }
    01705
    01706 NWidgetBackground::~NWidgetBackground()
    01707 {
    01708   if (this->child != NULL) delete this->child;
    01709 }
    01710
    01718 void NWidgetBackground::Add(NWidgetBase *nwid)
    01719 {
    01720   if (this->child == NULL) {
    01721     this->child = new NWidgetVertical();
    01722   }
    01723   this->child->Add(nwid);
    01724 }
    01725
    01736 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01737 {
    01738   if (this->child == NULL) {
    01739     this->child = new NWidgetVertical();
    01740   }
    01741   this->child->SetPIP(pip_pre, pip_inter, pip_post);
    01742 }
    01743
    01744 void NWidgetBackground::SetupSmallestSize(Window *w, bool init_array)
    01745 {
    01746   if (init_array && this->index >= 0) {
    01747     assert(w->nested_array_size > (uint)this->index);
    01748     w->nested_array[this->index] = this;
    01749   }
    01750   if (this->child != NULL) {
    01751     this->child->SetupSmallestSize(w, init_array);
    01752
    01753     this->smallest_x = this->child->smallest_x;
    01754     this->smallest_y = this->child->smallest_y;
    01755     this->fill_x = this->child->fill_x;
    01756     this->fill_y = this->child->fill_y;
    01757     this->resize_x = this->child->resize_x;
    01758     this->resize_y = this->child->resize_y;
    01759
    01760     /* Account for the size of the frame's text if that exists */
    01761     if (w != NULL && this->type == WWT_FRAME) {
    01762       this->child->padding_left   = WD_FRAMETEXT_LEFT;
    01763       this->child->padding_right  = WD_FRAMETEXT_RIGHT;
    01764       this->child->padding_top    = max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
    01765       this->child->padding_bottom = WD_FRAMETEXT_BOTTOM;
    01766
    01767       this->smallest_x += this->child->padding_left + this->child->padding_right;
    01768       this->smallest_y += this->child->padding_top + this->child->padding_bottom;
    01769
    01770       if (this->index >= 0) w->SetStringParameters(this->index);
    01771       this->smallest_x = max(this->smallest_x, GetStringBoundingBox(this->widget_data).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
    01772     }
    01773   } else {
    01774     Dimension d = {this->min_x, this->min_y};
    01775     Dimension fill = {this->fill_x, this->fill_y};
    01776     Dimension resize  = {this->resize_x, this->resize_y};
    01777     if (w != NULL) { // A non-NULL window pointer acts as switch to turn dynamic widget size on.
    01778       if (this->type == WWT_FRAME || this->type == WWT_INSET) {
    01779         if (this->index >= 0) w->SetStringParameters(this->index);
    01780         Dimension background = GetStringBoundingBox(this->widget_data);
    01781         background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
    01782         d = maxdim(d, background);
    01783       }
    01784       if (this->index >= 0) {
    01785         static const Dimension padding = {0, 0};
    01786         w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
    01787       }
    01788     }
    01789     this->smallest_x = d.width;
    01790     this->smallest_y = d.height;
    01791     this->fill_x = fill.width;
    01792     this->fill_y = fill.height;
    01793     this->resize_x = resize.width;
    01794     this->resize_y = resize.height;
    01795   }
    01796 }
    01797
    01798 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01799 {
    01800   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01801
    01802   if (this->child != NULL) {
    01803     uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
    01804     uint width = given_width - this->child->padding_right - this->child->padding_left;
    01805     uint height = given_height - this->child->padding_top - this->child->padding_bottom;
    01806     this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
    01807   }
    01808 }
    01809
    01810 void NWidgetBackground::FillNestedArray(NWidgetBase **array, uint length)
    01811 {
    01812   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01813   if (this->child != NULL) this->child->FillNestedArray(array, length);
    01814 }
    01815
    01816 void NWidgetBackground::Draw(const Window *w)
    01817 {
    01818   if (this->current_x == 0 || this->current_y == 0) return;
    01819
    01820   Rect r;
    01821   r.left = this->pos_x;
    01822   r.right = this->pos_x + this->current_x - 1;
    01823   r.top = this->pos_y;
    01824   r.bottom = this->pos_y + this->current_y - 1;
    01825
    01826   const DrawPixelInfo *dpi = _cur_dpi;
    01827   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01828
    01829   switch (this->type) {
    01830     case WWT_PANEL:
    01831       assert(this->widget_data == 0);
    01832       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
    01833       break;
    01834
    01835     case WWT_FRAME:
    01836       if (this->index >= 0) w->SetStringParameters(this->index);
    01837       DrawFrame(r, this->colour, this->widget_data);
    01838       break;
    01839
    01840     case WWT_INSET:
    01841       if (this->index >= 0) w->SetStringParameters(this->index);
    01842       DrawInset(r, this->colour, this->widget_data);
    01843       break;
    01844
    01845     default:
    01846       NOT_REACHED();
    01847   }
    01848
    01849   if (this->index >= 0) w->DrawWidget(r, this->index);
    01850   if (this->child != NULL) this->child->Draw(w);
    01851
    01852   if (this->IsDisabled()) {
    01853     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    01854   }
    01855 }
    01856
    01857 NWidgetCore *NWidgetBackground::GetWidgetFromPos(int x, int y)
    01858 {
    01859   NWidgetCore *nwid = NULL;
    01860   if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
    01861     if (this->child != NULL) nwid = this->child->GetWidgetFromPos(x, y);
    01862     if (nwid == NULL) nwid = this;
    01863   }
    01864   return nwid;
    01865 }
    01866
    01867 NWidgetBase *NWidgetBackground::GetWidgetOfType(WidgetType tp)
    01868 {
    01869   NWidgetBase *nwid = NULL;
    01870   if (this->child != NULL) nwid = this->child->GetWidgetOfType(tp);
    01871   if (nwid == NULL && this->type == tp) nwid = this;
    01872   return nwid;
    01873 }
    01874
    01875 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
    01876 {
    01877   this->SetIndex(index);
    01878 }
    01879
    01880 void NWidgetViewport::SetupSmallestSize(Window *w, bool init_array)
    01881 {
    01882   if (init_array && this->index >= 0) {
    01883     assert(w->nested_array_size > (uint)this->index);
    01884     w->nested_array[this->index] = this;
    01885   }
    01886   this->smallest_x = this->min_x;
    01887   this->smallest_y = this->min_y;
    01888 }
    01889
    01890 void NWidgetViewport::Draw(const Window *w)
    01891 {
    01892   if (this->disp_flags & ND_NO_TRANSPARENCY) {
    01893     TransparencyOptionBits to_backup = _transparency_opt;
    01894     _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_LOADING); // Disable all transparency, except textual stuff
    01895     w->DrawViewport();
    01896     _transparency_opt = to_backup;
    01897   } else {
    01898     w->DrawViewport();
    01899   }
    01900
    01901   /* Optionally shade the viewport. */
    01902   if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
    01903     GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
    01904         (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
    01905   }
    01906 }
    01907
    01914 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
    01915 {
    01916   InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
    01917 }
    01918
    01923 void NWidgetViewport::UpdateViewportCoordinates(Window *w)
    01924 {
    01925   ViewPort *vp = w->viewport;
    01926   if (vp != NULL) {
    01927     vp->left = w->left + this->pos_x;
    01928     vp->top  = w->top + this->pos_y;
    01929     vp->width  = this->current_x;
    01930     vp->height = this->current_y;
    01931
    01932     vp->virtual_width  = ScaleByZoom(vp->width, vp->zoom);
    01933     vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
    01934   }
    01935 }
    01936
    01946 int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
    01947 {
    01948   uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
    01949   if (pos != INT_MAX) pos += this->GetPosition();
    01950   return (pos >= this->GetCount()) ? INT_MAX : pos;
    01951 }
    01952
    01960 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
    01961 {
    01962   NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
    01963   if (this->IsVertical()) {
    01964     this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
    01965   } else {
    01966     this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
    01967   }
    01968 }
    01969
    01976 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL), Scrollbar(tp != NWID_HSCROLLBAR)
    01977 {
    01978   assert(tp == NWID_HSCROLLBAR || tp == NWID_VSCROLLBAR);
    01979   this->SetIndex(index);
    01980
    01981   switch (this->type) {
    01982     case NWID_HSCROLLBAR:
    01983       this->SetMinimalSize(0, NWidgetScrollbar::GetHorizontalDimension().height);
    01984       this->SetResize(1, 0);
    01985       this->SetFill(1, 0);
    01986       this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
    01987       break;
    01988
    01989     case NWID_VSCROLLBAR:
    01990       this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width, 0);
    01991       this->SetResize(0, 1);
    01992       this->SetFill(0, 1);
    01993       this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
    01994       break;
    01995
    01996     default: NOT_REACHED();
    01997   }
    01998 }
    01999
    02000 void NWidgetScrollbar::SetupSmallestSize(Window *w, bool init_array)
    02001 {
    02002   if (init_array && this->index >= 0) {
    02003     assert(w->nested_array_size > (uint)this->index);
    02004     w->nested_array[this->index] = this;
    02005   }
    02006   this->smallest_x = this->min_x;
    02007   this->smallest_y = this->min_y;
    02008 }
    02009
    02010 void NWidgetScrollbar::Draw(const Window *w)
    02011 {
    02012   if (this->current_x == 0 || this->current_y == 0) return;
    02013
    02014   Rect r;
    02015   r.left = this->pos_x;
    02016   r.right = this->pos_x + this->current_x - 1;
    02017   r.top = this->pos_y;
    02018   r.bottom = this->pos_y + this->current_y - 1;
    02019
    02020   const DrawPixelInfo *dpi = _cur_dpi;
    02021   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    02022
    02023   bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
    02024   bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
    02025   bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->scrolling_scrollbar == this->index;
    02026
    02027   if (this->type == NWID_HSCROLLBAR) {
    02028     DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    02029   } else {
    02030     DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    02031   }
    02032
    02033   if (this->IsDisabled()) {
    02034     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    02035   }
    02036 }
    02037
    02038 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
    02039 {
    02040   vertical_dimension.width   = vertical_dimension.height   = 0;
    02041   horizontal_dimension.width = horizontal_dimension.height = 0;
    02042 }
    02043
    02044 /* static */ Dimension NWidgetScrollbar::GetVerticalDimension()
    02045 {
    02046   static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
    02047   if (vertical_dimension.width == 0) {
    02048     vertical_dimension = maxdim(GetSpriteSize(SPR_ARROW_UP), GetSpriteSize(SPR_ARROW_DOWN));
    02049     vertical_dimension.width += extra.width;
    02050     vertical_dimension.height += extra.height;
    02051   }
    02052   return vertical_dimension;
    02053 }
    02054
    02055 /* static */ Dimension NWidgetScrollbar::GetHorizontalDimension()
    02056 {
    02057   static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
    02058   if (horizontal_dimension.width == 0) {
    02059     horizontal_dimension = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    02060     horizontal_dimension.width += extra.width;
    02061     horizontal_dimension.height += extra.height;
    02062   }
    02063   return horizontal_dimension;
    02064 }
    02065
    02066 Dimension NWidgetScrollbar::vertical_dimension = {0, 0};
    02067 Dimension NWidgetScrollbar::horizontal_dimension = {0, 0};
    02068
    02070 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
    02071 {
    02072   shadebox_dimension.width   = shadebox_dimension.height   = 0;
    02073   debugbox_dimension.width   = debugbox_dimension.height   = 0;
    02074   defsizebox_dimension.width = defsizebox_dimension.height = 0;
    02075   stickybox_dimension.width  = stickybox_dimension.height  = 0;
    02076   resizebox_dimension.width  = resizebox_dimension.height  = 0;
    02077   closebox_dimension.width   = closebox_dimension.height   = 0;
    02078   dropdown_dimension.width   = dropdown_dimension.height   = 0;
    02079 }
    02080
    02081 Dimension NWidgetLeaf::shadebox_dimension   = {0, 0};
    02082 Dimension NWidgetLeaf::debugbox_dimension   = {0, 0};
    02083 Dimension NWidgetLeaf::defsizebox_dimension = {0, 0};
    02084 Dimension NWidgetLeaf::stickybox_dimension  = {0, 0};
    02085 Dimension NWidgetLeaf::resizebox_dimension  = {0, 0};
    02086 Dimension NWidgetLeaf::closebox_dimension   = {0, 0};
    02087 Dimension NWidgetLeaf::dropdown_dimension   = {0, 0};
    02088
    02097 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint16 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
    02098 {
    02099   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);
    02100   if (index >= 0) this->SetIndex(index);
    02101   this->SetMinimalSize(0, 0);
    02102   this->SetResize(0, 0);
    02103
    02104   switch (tp) {
    02105     case WWT_EMPTY:
    02106       break;
    02107
    02108     case WWT_PUSHBTN:
    02109     case WWT_IMGBTN:
    02110     case WWT_PUSHIMGBTN:
    02111     case WWT_IMGBTN_2:
    02112     case WWT_TEXTBTN:
    02113     case WWT_PUSHTXTBTN:
    02114     case WWT_TEXTBTN_2:
    02115     case WWT_LABEL:
    02116     case WWT_TEXT:
    02117     case WWT_MATRIX:
    02118     case NWID_BUTTON_DROPDOWN:
    02119     case NWID_PUSHBUTTON_DROPDOWN:
    02120     case WWT_ARROWBTN:
    02121     case WWT_PUSHARROWBTN:
    02122       this->SetFill(0, 0);
    02123       break;
    02124
    02125     case WWT_EDITBOX: {
    02126       Dimension sprite_size = GetSpriteSize(_current_text_dir == TD_RTL ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
    02127       this->SetMinimalSize(30 + sprite_size.width, sprite_size.height);
    02128       this->SetFill(0, 0);
    02129       break;
    02130     }
    02131
    02132     case WWT_CAPTION:
    02133       this->SetFill(1, 0);
    02134       this->SetResize(1, 0);
    02135       this->min_y = WD_CAPTION_HEIGHT;
    02136       this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
    02137       break;
    02138
    02139     case WWT_STICKYBOX:
    02140       this->SetFill(0, 0);
    02141       this->SetMinimalSize(WD_STICKYBOX_WIDTH, WD_CAPTION_HEIGHT);
    02142       this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
    02143       break;
    02144
    02145     case WWT_SHADEBOX:
    02146       this->SetFill(0, 0);
    02147       this->SetMinimalSize(WD_SHADEBOX_TOP, WD_CAPTION_HEIGHT);
    02148       this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
    02149       break;
    02150
    02151     case WWT_DEBUGBOX:
    02152       this->SetFill(0, 0);
    02153       this->SetMinimalSize(WD_DEBUGBOX_TOP, WD_CAPTION_HEIGHT);
    02154       this->SetDataTip(STR_NULL, STR_TOOLTIP_DEBUG);
    02155       break;
    02156
    02157     case WWT_DEFSIZEBOX:
    02158       this->SetFill(0, 0);
    02159       this->SetMinimalSize(WD_DEFSIZEBOX_TOP, WD_CAPTION_HEIGHT);
    02160       this->SetDataTip(STR_NULL, STR_TOOLTIP_DEFSIZE);
    02161       break;
    02162
    02163     case WWT_RESIZEBOX:
    02164       this->SetFill(0, 0);
    02165       this->SetMinimalSize(WD_RESIZEBOX_WIDTH, 12);
    02166       this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
    02167       break;
    02168
    02169     case WWT_CLOSEBOX:
    02170       this->SetFill(0, 0);
    02171       this->SetMinimalSize(WD_CLOSEBOX_WIDTH, WD_CAPTION_HEIGHT);
    02172       this->SetDataTip(STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW);
    02173       break;
    02174
    02175     case WWT_DROPDOWN:
    02176       this->SetFill(0, 0);
    02177       this->min_y = WD_DROPDOWN_HEIGHT;
    02178       break;
    02179
    02180     default:
    02181       NOT_REACHED();
    02182   }
    02183 }
    02184
    02185 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
    02186 {
    02187   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    02188     assert(w->nested_array_size > (uint)this->index);
    02189     w->nested_array[this->index] = this;
    02190   }
    02191
    02192   Dimension size = {this->min_x, this->min_y};
    02193   Dimension fill = {this->fill_x, this->fill_y};
    02194   Dimension resize = {this->resize_x, this->resize_y};
    02195   /* Get padding, and update size with the real content size if appropriate. */
    02196   const Dimension *padding = NULL;
    02197   switch (this->type) {
    02198     case WWT_EMPTY: {
    02199       static const Dimension extra = {0, 0};
    02200       padding = &extra;
    02201       break;
    02202     }
    02203     case WWT_MATRIX: {
    02204       static const Dimension extra = {WD_MATRIX_LEFT + WD_MATRIX_RIGHT, WD_MATRIX_TOP + WD_MATRIX_BOTTOM};
    02205       padding = &extra;
    02206       break;
    02207     }
    02208     case WWT_SHADEBOX: {
    02209       static const Dimension extra = {WD_SHADEBOX_LEFT + WD_SHADEBOX_RIGHT, WD_SHADEBOX_TOP + WD_SHADEBOX_BOTTOM};
    02210       padding = &extra;
    02211       if (NWidgetLeaf::shadebox_dimension.width == 0) {
    02212         NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
    02213         NWidgetLeaf::shadebox_dimension.width += extra.width;
    02214         NWidgetLeaf::shadebox_dimension.height += extra.height;
    02215       }
    02216       size = maxdim(size, NWidgetLeaf::shadebox_dimension);
    02217       break;
    02218     }
    02219     case WWT_DEBUGBOX:
    02220       if (_settings_client.gui.newgrf_developer_tools && w->IsNewGRFInspectable()) {
    02221         static const Dimension extra = {WD_DEBUGBOX_LEFT + WD_DEBUGBOX_RIGHT, WD_DEBUGBOX_TOP + WD_DEBUGBOX_BOTTOM};
    02222         padding = &extra;
    02223         if (NWidgetLeaf::debugbox_dimension.width == 0) {
    02224           NWidgetLeaf::debugbox_dimension = GetSpriteSize(SPR_WINDOW_DEBUG);
    02225           NWidgetLeaf::debugbox_dimension.width += extra.width;
    02226           NWidgetLeaf::debugbox_dimension.height += extra.height;
    02227         }
    02228         size = maxdim(size, NWidgetLeaf::debugbox_dimension);
    02229       } else {
    02230         /* If the setting is disabled we don't want to see it! */
    02231         size.width = 0;
    02232         fill.width = 0;
    02233         resize.width = 0;
    02234       }
    02235       break;
    02236
    02237     case WWT_STICKYBOX: {
    02238       static const Dimension extra = {WD_STICKYBOX_LEFT + WD_STICKYBOX_RIGHT, WD_STICKYBOX_TOP + WD_STICKYBOX_BOTTOM};
    02239       padding = &extra;
    02240       if (NWidgetLeaf::stickybox_dimension.width == 0) {
    02241         NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
    02242         NWidgetLeaf::stickybox_dimension.width += extra.width;
    02243         NWidgetLeaf::stickybox_dimension.height += extra.height;
    02244       }
    02245       size = maxdim(size, NWidgetLeaf::stickybox_dimension);
    02246       break;
    02247     }
    02248
    02249     case WWT_DEFSIZEBOX: {
    02250       static const Dimension extra = {WD_DEFSIZEBOX_LEFT + WD_DEFSIZEBOX_RIGHT, WD_DEFSIZEBOX_TOP + WD_DEFSIZEBOX_BOTTOM};
    02251       padding = &extra;
    02252       if (NWidgetLeaf::defsizebox_dimension.width == 0) {
    02253         NWidgetLeaf::defsizebox_dimension = GetSpriteSize(SPR_WINDOW_DEFSIZE);
    02254         NWidgetLeaf::defsizebox_dimension.width += extra.width;
    02255         NWidgetLeaf::defsizebox_dimension.height += extra.height;
    02256       }
    02257       size = maxdim(size, NWidgetLeaf::defsizebox_dimension);
    02258       break;
    02259     }
    02260
    02261     case WWT_RESIZEBOX: {
    02262       static const Dimension extra = {WD_RESIZEBOX_LEFT + WD_RESIZEBOX_RIGHT, WD_RESIZEBOX_TOP + WD_RESIZEBOX_BOTTOM};
    02263       padding = &extra;
    02264       if (NWidgetLeaf::resizebox_dimension.width == 0) {
    02265         NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
    02266         NWidgetLeaf::resizebox_dimension.width += extra.width;
    02267         NWidgetLeaf::resizebox_dimension.height += extra.height;
    02268       }
    02269       size = maxdim(size, NWidgetLeaf::resizebox_dimension);
    02270       break;
    02271     }
    02272     case WWT_EDITBOX:
    02273       size.height = max(size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    02274       /* FALL THROUGH */
    02275     case WWT_PUSHBTN: {
    02276       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02277       padding = &extra;
    02278       break;
    02279     }
    02280     case WWT_IMGBTN:
    02281     case WWT_IMGBTN_2:
    02282     case WWT_PUSHIMGBTN: {
    02283       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02284       padding = &extra;
    02285       Dimension d2 = GetSpriteSize(this->widget_data);
    02286       if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
    02287       d2.width += extra.width;
    02288       d2.height += extra.height;
    02289       size = maxdim(size, d2);
    02290       break;
    02291     }
    02292     case WWT_ARROWBTN:
    02293     case WWT_PUSHARROWBTN: {
    02294       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02295       padding = &extra;
    02296       Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    02297       d2.width += extra.width;
    02298       d2.height += extra.height;
    02299       size = maxdim(size, d2);
    02300       break;
    02301     }
    02302
    02303     case WWT_CLOSEBOX: {
    02304       static const Dimension extra = {WD_CLOSEBOX_LEFT + WD_CLOSEBOX_RIGHT, WD_CLOSEBOX_TOP + WD_CLOSEBOX_BOTTOM};
    02305       padding = &extra;
    02306       if (NWidgetLeaf::closebox_dimension.width == 0) {
    02307         NWidgetLeaf::closebox_dimension = maxdim(GetStringBoundingBox(STR_BLACK_CROSS), GetStringBoundingBox(STR_SILVER_CROSS));
    02308         NWidgetLeaf::closebox_dimension.width += extra.width;
    02309         NWidgetLeaf::closebox_dimension.height += extra.height;
    02310       }
    02311       size = maxdim(size, NWidgetLeaf::closebox_dimension);
    02312       break;
    02313     }
    02314     case WWT_TEXTBTN:
    02315     case WWT_PUSHTXTBTN:
    02316     case WWT_TEXTBTN_2: {
    02317       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT,  WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02318       padding = &extra;
    02319       if (this->index >= 0) w->SetStringParameters(this->index);
    02320       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02321       d2.width += extra.width;
    02322       d2.height += extra.height;
    02323       size = maxdim(size, d2);
    02324       break;
    02325     }
    02326     case WWT_LABEL:
    02327     case WWT_TEXT: {
    02328       static const Dimension extra = {0, 0};
    02329       padding = &extra;
    02330       if (this->index >= 0) w->SetStringParameters(this->index);
    02331       size = maxdim(size, GetStringBoundingBox(this->widget_data));
    02332       break;
    02333     }
    02334     case WWT_CAPTION: {
    02335       static const Dimension extra = {WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT, WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM};
    02336       padding = &extra;
    02337       if (this->index >= 0) w->SetStringParameters(this->index);
    02338       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02339       d2.width += extra.width;
    02340       d2.height += extra.height;
    02341       size = maxdim(size, d2);
    02342       break;
    02343     }
    02344     case WWT_DROPDOWN:
    02345     case NWID_BUTTON_DROPDOWN:
    02346     case NWID_PUSHBUTTON_DROPDOWN: {
    02347       static Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
    02348       padding = &extra;
    02349       if (NWidgetLeaf::dropdown_dimension.width == 0) {
    02350         NWidgetLeaf::dropdown_dimension = GetSpriteSize(SPR_ARROW_DOWN);
    02351         NWidgetLeaf::dropdown_dimension.width += WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT;
    02352         NWidgetLeaf::dropdown_dimension.height += WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM;
    02353         extra.width = WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT + NWidgetLeaf::dropdown_dimension.width;
    02354       }
    02355       if (this->index >= 0) w->SetStringParameters(this->index);
    02356       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02357       d2.width += extra.width;
    02358       d2.height += extra.height;
    02359       size = maxdim(size, d2);
    02360       break;
    02361     }
    02362     default:
    02363       NOT_REACHED();
    02364   }
    02365
    02366   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
    02367
    02368   this->smallest_x = size.width;
    02369   this->smallest_y = size.height;
    02370   this->fill_x = fill.width;
    02371   this->fill_y = fill.height;
    02372   this->resize_x = resize.width;
    02373   this->resize_y = resize.height;
    02374 }
    02375
    02376 void NWidgetLeaf::Draw(const Window *w)
    02377 {
    02378   if (this->current_x == 0 || this->current_y == 0) return;
    02379
    02380   Rect r;
    02381   r.left = this->pos_x;
    02382   r.right = this->pos_x + this->current_x - 1;
    02383   r.top = this->pos_y;
    02384   r.bottom = this->pos_y + this->current_y - 1;
    02385
    02386   const DrawPixelInfo *dpi = _cur_dpi;
    02387   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    02388
    02389   bool clicked = this->IsLowered();
    02390   switch (this->type) {
    02391     case WWT_EMPTY:
    02392       break;
    02393
    02394     case WWT_PUSHBTN:
    02395       assert(this->widget_data == 0);
    02396       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02397       break;
    02398
    02399     case WWT_IMGBTN:
    02400     case WWT_PUSHIMGBTN:
    02401     case WWT_IMGBTN_2:
    02402       DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
    02403       break;
    02404
    02405     case WWT_TEXTBTN:
    02406     case WWT_PUSHTXTBTN:
    02407     case WWT_TEXTBTN_2:
    02408       if (this->index >= 0) w->SetStringParameters(this->index);
    02409       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02410       DrawLabel(r, this->type, clicked, this->widget_data);
    02411       break;
    02412
    02413     case WWT_ARROWBTN:
    02414     case WWT_PUSHARROWBTN: {
    02415       SpriteID sprite;
    02416       switch (this->widget_data) {
    02417         case AWV_DECREASE: sprite = _current_text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02418         case AWV_INCREASE: sprite = _current_text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02419         case AWV_LEFT:     sprite = SPR_ARROW_LEFT;  break;
    02420         case AWV_RIGHT:    sprite = SPR_ARROW_RIGHT; break;
    02421         default: NOT_REACHED();
    02422       }
    02423       DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
    02424       break;
    02425     }
    02426
    02427     case WWT_LABEL:
    02428       if (this->index >= 0) w->SetStringParameters(this->index);
    02429       DrawLabel(r, this->type, clicked, this->widget_data);
    02430       break;
    02431
    02432     case WWT_TEXT:
    02433       if (this->index >= 0) w->SetStringParameters(this->index);
    02434       DrawText(r, (TextColour)this->colour, this->widget_data);
    02435       break;
    02436
    02437     case WWT_MATRIX:
    02438       DrawMatrix(r, this->colour, clicked, this->widget_data, this->resize_x, this->resize_y);
    02439       break;
    02440
    02441     case WWT_EDITBOX: {
    02442       const QueryString *query = w->GetQueryString(this->index);
    02443       if (query != NULL) query->DrawEditBox(w, this->index);
    02444       break;
    02445     }
    02446
    02447     case WWT_CAPTION:
    02448       if (this->index >= 0) w->SetStringParameters(this->index);
    02449       DrawCaption(r, this->colour, w->owner, this->widget_data);
    02450       break;
    02451
    02452     case WWT_SHADEBOX:
    02453       assert(this->widget_data == 0);
    02454       DrawShadeBox(r, this->colour, w->IsShaded());
    02455       break;
    02456
    02457     case WWT_DEBUGBOX:
    02458       DrawDebugBox(r, this->colour, clicked);
    02459       break;
    02460
    02461     case WWT_STICKYBOX:
    02462       assert(this->widget_data == 0);
    02463       DrawStickyBox(r, this->colour, !!(w->flags & WF_STICKY));
    02464       break;
    02465
    02466     case WWT_DEFSIZEBOX:
    02467       assert(this->widget_data == 0);
    02468       DrawDefSizeBox(r, this->colour, clicked);
    02469       break;
    02470
    02471     case WWT_RESIZEBOX:
    02472       assert(this->widget_data == 0);
    02473       DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags & WF_SIZING));
    02474       break;
    02475
    02476     case WWT_CLOSEBOX:
    02477       DrawCloseBox(r, this->colour, this->widget_data);
    02478       break;
    02479
    02480     case WWT_DROPDOWN:
    02481       if (this->index >= 0) w->SetStringParameters(this->index);
    02482       DrawDropdown(r, this->colour, clicked, this->widget_data);
    02483       break;
    02484
    02485     case NWID_BUTTON_DROPDOWN:
    02486     case NWID_PUSHBUTTON_DROPDOWN:
    02487       if (this->index >= 0) w->SetStringParameters(this->index);
    02488       DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
    02489       break;
    02490
    02491     default:
    02492       NOT_REACHED();
    02493   }
    02494   if (this->index >= 0) w->DrawWidget(r, this->index);
    02495
    02496   if (this->IsDisabled()) {
    02497     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    02498   }
    02499 }
    02500
    02508 bool NWidgetLeaf::ButtonHit(const Point &pt)
    02509 {
    02510   if (_current_text_dir == TD_LTR) {
    02511     int button_width = this->pos_x + this->current_x - 12;
    02512     return pt.x < button_width;
    02513   } else {
    02514     int button_left = this->pos_x + 12;
    02515     return pt.x >= button_left;
    02516   }
    02517 }
    02518
    02519 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
    02520
    02536 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
    02537 {
    02538   int num_used = 0;
    02539
    02540   *dest = NULL;
    02541   *fill_dest = false;
    02542
    02543   while (count > num_used) {
    02544     switch (parts->type) {
    02545       case NWID_SPACER:
    02546         if (*dest != NULL) return num_used;
    02547         *dest = new NWidgetSpacer(0, 0);
    02548         break;
    02549
    02550       case NWID_HORIZONTAL:
    02551         if (*dest != NULL) return num_used;
    02552         *dest = new NWidgetHorizontal(parts->u.cont_flags);
    02553         *fill_dest = true;
    02554         break;
    02555
    02556       case NWID_HORIZONTAL_LTR:
    02557         if (*dest != NULL) return num_used;
    02558         *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
    02559         *fill_dest = true;
    02560         break;
    02561
    02562       case WWT_PANEL:
    02563       case WWT_INSET:
    02564       case WWT_FRAME:
    02565         if (*dest != NULL) return num_used;
    02566         *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02567         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02568         *fill_dest = true;
    02569         break;
    02570
    02571       case NWID_VERTICAL:
    02572         if (*dest != NULL) return num_used;
    02573         *dest = new NWidgetVertical(parts->u.cont_flags);
    02574         *fill_dest = true;
    02575         break;
    02576
    02577       case NWID_MATRIX: {
    02578         if (*dest != NULL) return num_used;
    02579         NWidgetMatrix *nwm = new NWidgetMatrix();
    02580         *dest = nwm;
    02581         *fill_dest = true;
    02582         nwm->SetIndex(parts->u.widget.index);
    02583         nwm->SetColour(parts->u.widget.colour);
    02584         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02585         break;
    02586       }
    02587
    02588       case WPT_FUNCTION: {
    02589         if (*dest != NULL) return num_used;
    02590         /* Ensure proper functioning even when the called code simply writes its largest index. */
    02591         int biggest = -1;
    02592         *dest = parts->u.func_ptr(&biggest);
    02593         *biggest_index = max(*biggest_index, biggest);
    02594         *fill_dest = false;
    02595         break;
    02596       }
    02597
    02598       case WPT_RESIZE: {
    02599         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02600         if (nwrb != NULL) {
    02601           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02602           nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
    02603         }
    02604         break;
    02605       }
    02606
    02607       case WPT_MINSIZE: {
    02608         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02609         if (nwrb != NULL) {
    02610           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02611           nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
    02612         }
    02613         break;
    02614       }
    02615
    02616       case WPT_MINTEXTLINES: {
    02617         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02618         if (nwrb != NULL) {
    02619           assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
    02620           nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
    02621         }
    02622         break;
    02623       }
    02624
    02625       case WPT_FILL: {
    02626         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02627         if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
    02628         break;
    02629       }
    02630
    02631       case WPT_DATATIP: {
    02632         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02633         if (nwc != NULL) {
    02634           nwc->widget_data = parts->u.data_tip.data;
    02635           nwc->tool_tip = parts->u.data_tip.tooltip;
    02636         }
    02637         break;
    02638       }
    02639
    02640       case WPT_PADDING:
    02641         if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
    02642         break;
    02643
    02644       case WPT_PIPSPACE: {
    02645         NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
    02646         if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02647
    02648         NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
    02649         if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02650         break;
    02651       }
    02652
    02653       case WPT_SCROLLBAR: {
    02654         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02655         if (nwc != NULL) {
    02656           nwc->scrollbar_index = parts->u.widget.index;
    02657         }
    02658         break;
    02659       }
    02660
    02661       case WPT_ENDCONTAINER:
    02662         return num_used;
    02663
    02664       case NWID_VIEWPORT:
    02665         if (*dest != NULL) return num_used;
    02666         *dest = new NWidgetViewport(parts->u.widget.index);
    02667         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02668         break;
    02669
    02670       case NWID_HSCROLLBAR:
    02671       case NWID_VSCROLLBAR:
    02672         if (*dest != NULL) return num_used;
    02673         *dest = new NWidgetScrollbar(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02674         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02675         break;
    02676
    02677       case NWID_SELECTION: {
    02678         if (*dest != NULL) return num_used;
    02679         NWidgetStacked *nws = new NWidgetStacked();
    02680         *dest = nws;
    02681         *fill_dest = true;
    02682         nws->SetIndex(parts->u.widget.index);
    02683         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02684         break;
    02685       }
    02686
    02687       default:
    02688         if (*dest != NULL) return num_used;
    02689         assert((parts->type & WWT_MASK) < WWT_LAST || (parts->type & WWT_MASK) == NWID_BUTTON_DROPDOWN);
    02690         *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
    02691         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02692         break;
    02693     }
    02694     num_used++;
    02695     parts++;
    02696   }
    02697
    02698   return num_used;
    02699 }
    02700
    02710 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
    02711 {
    02712   /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
    02713    * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
    02714   NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
    02715   NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
    02716   assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
    02717
    02718   int total_used = 0;
    02719   for (;;) {
    02720     NWidgetBase *sub_widget = NULL;
    02721     bool fill_sub = false;
    02722     int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
    02723     parts += num_used;
    02724     total_used += num_used;
    02725
    02726     /* Break out of loop when end reached */
    02727     if (sub_widget == NULL) break;
    02728
    02729     /* If sub-widget is a container, recursively fill that container. */
    02730     WidgetType tp = sub_widget->type;
    02731     if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
    02732               || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
    02733       NWidgetBase *sub_ptr = sub_widget;
    02734       int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
    02735       parts += num_used;
    02736       total_used += num_used;
    02737     }
    02738
    02739     /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
    02740     if (nwid_cont != NULL) nwid_cont->Add(sub_widget);
    02741     if (nwid_parent != NULL) nwid_parent->Add(sub_widget);
    02742     if (nwid_cont == NULL && nwid_parent == NULL) {
    02743       *parent = sub_widget;
    02744       return total_used;
    02745     }
    02746   }
    02747
    02748   if (count == total_used) return total_used; // Reached the end of the array of parts?
    02749
    02750   assert(total_used < count);
    02751   assert(parts->type == WPT_ENDCONTAINER);
    02752   return total_used + 1; // *parts is also 'used'
    02753 }
    02754
    02766 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
    02767 {
    02768   *biggest_index = -1;
    02769   if (container == NULL) container = new NWidgetVertical();
    02770   NWidgetBase *cont_ptr = container;
    02771   MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
    02772   return container;
    02773 }
    02774
    02788 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
    02789 {
    02790   *biggest_index = -1;
    02791
    02792   /* Read the first widget recursively from the array. */
    02793   NWidgetBase *nwid = NULL;
    02794   int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
    02795   assert(nwid != NULL);
    02796   parts += num_used;
    02797   count -= num_used;
    02798
    02799   NWidgetContainer *root = new NWidgetVertical;
    02800   root->Add(nwid);
    02801   if (count == 0) { // There is no body at all.
    02802     *shade_select = NULL;
    02803     return root;
    02804   }
    02805
    02806   /* If the first widget looks like a titlebar, treat it as such.
    02807    * If it has a shading box, silently add a shade selection widget in the tree. */
    02808   NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
    02809   NWidgetContainer *body;
    02810   if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
    02811     *shade_select = new NWidgetStacked;
    02812     root->Add(*shade_select);
    02813     body = new NWidgetVertical;
    02814     (*shade_select)->Add(body);
    02815   } else {
    02816     *shade_select = NULL;
    02817     body = root;
    02818   }
    02819
    02820   /* Load the remaining parts into 'body'. */
    02821   int biggest2 = -1;
    02822   MakeNWidgets(parts, count, &biggest2, body);
    02823
    02824   *biggest_index = max(*biggest_index, biggest2);
    02825   return root;
    02826 }
    02827
    02838 NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
    02839 {
    02840   assert(max_length >= 1);
    02841   NWidgetVertical *vert = NULL; // Storage for all rows.
    02842   NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
    02843   int hor_length = 0;
    02844
    02845   Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
    02846   sprite_size.width  += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
    02847   sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
    02848
    02849   for (int widnum = widget_first; widnum <= widget_last; widnum++) {
    02850     /* Ensure there is room in 'hor' for another button. */
    02851     if (hor_length == max_length) {
    02852       if (vert == NULL) vert = new NWidgetVertical();
    02853       vert->Add(hor);
    02854       hor = NULL;
    02855       hor_length = 0;
    02856     }
    02857     if (hor == NULL) {
    02858       hor = new NWidgetHorizontal();
    02859       hor_length = 0;
    02860     }
    02861
    02862     NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
    02863     panel->SetMinimalSize(sprite_size.width, sprite_size.height);
    02864     panel->SetFill(1, 1);
    02865     panel->SetResize(1, 0);
    02866     panel->SetDataTip(0x0, button_tooltip);
    02867     hor->Add(panel);
    02868     hor_length++;
    02869   }
    02870   *biggest_index = widget_last;
    02871   if (vert == NULL) return hor; // All buttons fit in a single row.
    02872
    02873   if (hor_length > 0 && hor_length < max_length) {
    02874     /* Last row is partial, add a spacer at the end to force all buttons to the left. */
    02875     NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
    02876     spc->SetFill(1, 1);
    02877     spc->SetResize(1, 0);
    02878     hor->Add(spc);
    02879   }
    02880   if (hor != NULL) vert->Add(hor);
    02881   return vert;
    02882 }