# widget.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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   ScrollbarClickPositioning(w, dynamic_cast<NWidgetScrollbar*>(nw), x, y, mi, ma);
    00151 }
    00152
    00161 int GetWidgetFromPos(const Window *w, int x, int y)
    00162 {
    00163   NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
    00164   return (nw != NULL) ? nw->index : -1;
    00165 }
    00166
    00176 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
    00177 {
    00178   assert(colour < COLOUR_END);
    00179
    00180   uint dark         = _colour_gradient[colour][3];
    00181   uint medium_dark  = _colour_gradient[colour][5];
    00182   uint medium_light = _colour_gradient[colour][6];
    00183   uint light        = _colour_gradient[colour][7];
    00184
    00185   if (flags & FR_TRANSPARENT) {
    00186     GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR);
    00187   } else {
    00188     uint interior;
    00189
    00190     if (flags & FR_LOWERED) {
    00191       GfxFillRect(left,                 top,                left,                   bottom,                   dark);
    00192       GfxFillRect(left + WD_BEVEL_LEFT, top,                right,                  top,                      dark);
    00193       GfxFillRect(right,                top + WD_BEVEL_TOP, right,                  bottom - WD_BEVEL_BOTTOM, light);
    00194       GfxFillRect(left + WD_BEVEL_LEFT, bottom,             right,                  bottom,                   light);
    00195       interior = (flags & FR_DARKENED ? medium_dark : medium_light);
    00196     } else {
    00197       GfxFillRect(left,                 top,                left,                   bottom - WD_BEVEL_BOTTOM, light);
    00198       GfxFillRect(left + WD_BEVEL_LEFT, top,                right - WD_BEVEL_RIGHT, top,                      light);
    00199       GfxFillRect(right,                top,                right,                  bottom - WD_BEVEL_BOTTOM, dark);
    00200       GfxFillRect(left,                 bottom,             right,                  bottom,                   dark);
    00201       interior = medium_dark;
    00202     }
    00203     if (!(flags & FR_BORDERONLY)) {
    00204       GfxFillRect(left + WD_BEVEL_LEFT, top + WD_BEVEL_TOP, right - WD_BEVEL_RIGHT, bottom - WD_BEVEL_BOTTOM, interior);
    00205     }
    00206   }
    00207 }
    00208
    00217 static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
    00218 {
    00219   assert(img != 0);
    00220   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00221
    00222   if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2.
    00223   DrawSprite(img, PAL_NONE, r.left + WD_IMGBTN_LEFT + clicked, r.top + WD_IMGBTN_TOP + clicked);
    00224 }
    00225
    00233 static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
    00234 {
    00235   if (str == STR_NULL) return;
    00236   if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
    00237   Dimension d = GetStringBoundingBox(str);
    00238   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00239   DrawString(r.left + clicked, r.right + clicked, r.top + offset + clicked, str, TC_FROMSTRING, SA_HOR_CENTER);
    00240 }
    00241
    00248 static inline void DrawText(const Rect &r, TextColour colour, StringID str)
    00249 {
    00250   Dimension d = GetStringBoundingBox(str);
    00251   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00252   if (str != STR_NULL) DrawString(r.left, r.right, r.top + offset, str, colour);
    00253 }
    00254
    00261 static inline void DrawInset(const Rect &r, Colours colour, StringID str)
    00262 {
    00263   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
    00264   if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str);
    00265 }
    00266
    00274 static inline void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint16 data)
    00275 {
    00276   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00277
    00278   int num_columns = GB(data, MAT_COL_START, MAT_COL_BITS);  // Lower 8 bits of the widget data: Number of columns in the matrix.
    00279   int column_width = (r.right - r.left + 1) / num_columns; // Width of a single column in the matrix.
    00280
    00281   int num_rows = GB(data, MAT_ROW_START, MAT_ROW_BITS); // Upper 8 bits of the widget data: Number of rows in the matrix.
    00282   int row_height = (r.bottom - r.top + 1) / num_rows; // Height of a single row in the matrix.
    00283
    00284   int col = _colour_gradient[colour & 0xF][6];
    00285
    00286   int x = r.left;
    00287   for (int ctr = num_columns; ctr > 1; ctr--) {
    00288     x += column_width;
    00289     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00290   }
    00291
    00292   x = r.top;
    00293   for (int ctr = num_rows; ctr > 1; ctr--) {
    00294     x += row_height;
    00295     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00296   }
    00297
    00298   col = _colour_gradient[colour & 0xF][4];
    00299
    00300   x = r.left - 1;
    00301   for (int ctr = num_columns; ctr > 1; ctr--) {
    00302     x += column_width;
    00303     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00304   }
    00305
    00306   x = r.top - 1;
    00307   for (int ctr = num_rows; ctr > 1; ctr--) {
    00308     x += row_height;
    00309     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00310   }
    00311 }
    00312
    00322 static inline void DrawVerticalScrollbar(const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
    00323 {
    00324   int centre = (r.right - r.left) / 2;
    00325   int height = NWidgetScrollbar::GetVerticalDimension().height;
    00326
    00327   /* draw up/down buttons */
    00328   DrawFrameRect(r.left, r.top, r.right, r.top + height - 1, colour, (up_clicked) ? FR_LOWERED : FR_NONE);
    00329   DrawString(r.left + up_clicked, r.right + up_clicked, r.top + up_clicked, UPARROW, TC_BLACK, SA_HOR_CENTER);
    00330
    00331   DrawFrameRect(r.left, r.bottom - (height - 1), r.right, r.bottom, colour, (down_clicked) ? FR_LOWERED : FR_NONE);
    00332   DrawString(r.left + down_clicked, r.right + down_clicked, r.bottom - (height - 1) + down_clicked, DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00333
    00334   int c1 = _colour_gradient[colour & 0xF][3];
    00335   int c2 = _colour_gradient[colour & 0xF][7];
    00336
    00337   /* draw "shaded" background */
    00338   GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c2);
    00339   GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c1, FILLRECT_CHECKER);
    00340
    00341   /* draw shaded lines */
    00342   GfxFillRect(r.left + centre - 3, r.top + height, r.left + centre - 3, r.bottom - height, c1);
    00343   GfxFillRect(r.left + centre - 2, r.top + height, r.left + centre - 2, r.bottom - height, c2);
    00344   GfxFillRect(r.left + centre + 2, r.top + height, r.left + centre + 2, r.bottom - height, c1);
    00345   GfxFillRect(r.left + centre + 3, r.top + height, r.left + centre + 3, r.bottom - height, c2);
    00346
    00347   Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom, false);
    00348   DrawFrameRect(r.left, pt.x, r.right, pt.y, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00349 }
    00350
    00360 static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
    00361 {
    00362   int centre = (r.bottom - r.top) / 2;
    00363   int width = NWidgetScrollbar::GetHorizontalDimension().width;
    00364
    00365   DrawFrameRect(r.left, r.top, r.left + width - 1, r.bottom, colour, left_clicked ? FR_LOWERED : FR_NONE);
    00366   DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + left_clicked, r.top + 1 + left_clicked);
    00367
    00368   DrawFrameRect(r.right - (width - 1), r.top, r.right, r.bottom, colour, right_clicked ? FR_LOWERED : FR_NONE);
    00369   DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - (width - 2) + right_clicked, r.top + 1 + right_clicked);
    00370
    00371   int c1 = _colour_gradient[colour & 0xF][3];
    00372   int c2 = _colour_gradient[colour & 0xF][7];
    00373
    00374   /* draw "shaded" background */
    00375   GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c2);
    00376   GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c1, FILLRECT_CHECKER);
    00377
    00378   /* draw shaded lines */
    00379   GfxFillRect(r.left + width, r.top + centre - 3, r.right - width, r.top + centre - 3, c1);
    00380   GfxFillRect(r.left + width, r.top + centre - 2, r.right - width, r.top + centre - 2, c2);
    00381   GfxFillRect(r.left + width, r.top + centre + 2, r.right - width, r.top + centre + 2, c1);
    00382   GfxFillRect(r.left + width, r.top + centre + 3, r.right - width, r.top + centre + 3, c2);
    00383
    00384   /* draw actual scrollbar */
    00385   Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right, true);
    00386   DrawFrameRect(pt.x, r.top, pt.y, r.bottom, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00387 }
    00388
    00395 static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
    00396 {
    00397   int x2 = r.left; // by default the left side is the left side of the widget
    00398
    00399   if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str);
    00400
    00401   int c1 = _colour_gradient[colour][3];
    00402   int c2 = _colour_gradient[colour][7];
    00403
    00404   /* If the frame has text, adjust the top bar to fit half-way through */
    00405   int dy1 = 4;
    00406   if (str != STR_NULL) dy1 = FONT_HEIGHT_NORMAL / 2 - 1;
    00407   int dy2 = dy1 + 1;
    00408
    00409   if (_current_text_dir == TD_LTR) {
    00410     /* Line from upper left corner to start of text */
    00411     GfxFillRect(r.left, r.top + dy1, r.left + 4, r.top + dy1, c1);
    00412     GfxFillRect(r.left + 1, r.top + dy2, r.left + 4, r.top + dy2, c2);
    00413
    00414     /* Line from end of text to upper right corner */
    00415     GfxFillRect(x2, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00416     GfxFillRect(x2, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00417   } else {
    00418     /* Line from upper left corner to start of text */
    00419     GfxFillRect(r.left, r.top + dy1, x2 - 2, r.top + dy1, c1);
    00420     GfxFillRect(r.left + 1, r.top + dy2, x2 - 2, r.top + dy2, c2);
    00421
    00422     /* Line from end of text to upper right corner */
    00423     GfxFillRect(r.right - 5, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00424     GfxFillRect(r.right - 5, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00425   }
    00426
    00427   /* Line from upper left corner to bottom left corner */
    00428   GfxFillRect(r.left, r.top + dy2, r.left, r.bottom - 1, c1);
    00429   GfxFillRect(r.left + 1, r.top + dy2 + 1, r.left + 1, r.bottom - 2, c2);
    00430
    00431   /* Line from upper right corner to bottom right corner */
    00432   GfxFillRect(r.right - 1, r.top + dy2, r.right - 1, r.bottom - 2, c1);
    00433   GfxFillRect(r.right, r.top + dy1, r.right, r.bottom - 1, c2);
    00434
    00435   GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
    00436   GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
    00437 }
    00438
    00445 static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
    00446 {
    00447   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00448   DrawSprite((clicked) ? SPR_WINDOW_SHADE : SPR_WINDOW_UNSHADE, PAL_NONE, r.left + WD_SHADEBOX_LEFT + clicked, r.top + WD_SHADEBOX_TOP + clicked);
    00449 }
    00450
    00457 static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
    00458 {
    00459   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00460   DrawSprite((clicked) ? SPR_PIN_UP : SPR_PIN_DOWN, PAL_NONE, r.left + WD_STICKYBOX_LEFT + clicked, r.top + WD_STICKYBOX_TOP + clicked);
    00461 }
    00462
    00469 static inline void DrawDefSizeBox(const Rect &r, Colours colour, bool clicked)
    00470 {
    00471   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00472   DrawSprite(SPR_WINDOW_DEFSIZE, PAL_NONE, r.left + WD_DEFSIZEBOX_LEFT + clicked, r.top + WD_DEFSIZEBOX_TOP + clicked);
    00473 }
    00474
    00481 static inline void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
    00482 {
    00483   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00484   DrawSprite(SPR_WINDOW_DEBUG, PAL_NONE, r.left + WD_DEBUGBOX_LEFT + clicked, r.top + WD_DEBUGBOX_TOP + clicked);
    00485 }
    00486
    00494 static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
    00495 {
    00496   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00497   if (at_left) {
    00498     DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked,
    00499          r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_LEFT).height + clicked);
    00500   } else {
    00501     DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.left + WD_RESIZEBOX_LEFT + clicked,
    00502          r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT).height + clicked);
    00503   }
    00504 }
    00505
    00512 static inline void DrawCloseBox(const Rect &r, Colours colour, StringID str)
    00513 {
    00514   assert(str == STR_BLACK_CROSS || str == STR_SILVER_CROSS); // black or silver cross
    00515   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE);
    00516   DrawString(r.left + WD_CLOSEBOX_LEFT, r.right - WD_CLOSEBOX_RIGHT, r.top + WD_CLOSEBOX_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
    00517 }
    00518
    00526 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
    00527 {
    00528   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_BORDERONLY);
    00529   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);
    00530
    00531   if (owner != INVALID_OWNER) {
    00532     GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]);
    00533   }
    00534
    00535   if (str != STR_NULL) {
    00536     Dimension d = GetStringBoundingBox(str);
    00537     int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00538     DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + offset, str, TC_FROMSTRING, SA_HOR_CENTER);
    00539   }
    00540 }
    00541
    00552 static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
    00553 {
    00554   int text_offset = max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered
    00555
    00556   if (_current_text_dir == TD_LTR) {
    00557     DrawFrameRect(r.left, r.top, r.right - 12, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00558     DrawFrameRect(r.right - 11, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00559     DrawString(r.right - (clicked_dropdown ? 10 : 11), r.right, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00560     if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_LEFT + clicked_button, r.right - WD_DROPDOWNTEXT_RIGHT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK);
    00561   } else {
    00562     DrawFrameRect(r.left + 12, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00563     DrawFrameRect(r.left, r.top, r.left + 11, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00564     DrawString(r.left + clicked_dropdown, r.left + 11, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00565     if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_RIGHT + clicked_button, r.right - WD_DROPDOWNTEXT_LEFT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK);
    00566   }
    00567 }
    00568
    00576 static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
    00577 {
    00578   DrawButtonDropdown(r, colour, false, clicked, str);
    00579 }
    00580
    00584 void Window::DrawWidgets() const
    00585 {
    00586   this->nested_root->Draw(this);
    00587
    00588   if (this->flags & WF_WHITE_BORDER) {
    00589     DrawFrameRect(0, 0, this->width - 1, this->height - 1, COLOUR_WHITE, FR_BORDERONLY);
    00590   }
    00591
    00592   if (this->flags & WF_HIGHLIGHTED) {
    00593     extern bool _window_highlight_colour;
    00594     for (uint i = 0; i < this->nested_array_size; i++) {
    00595       const NWidgetBase *widget = this->GetWidget<NWidgetBase>(i);
    00596       if (widget == NULL || !widget->IsHighlighted()) continue;
    00597
    00598       int left = widget->pos_x;
    00599       int top  = widget->pos_y;
    00600       int right  = left + widget->current_x - 1;
    00601       int bottom = top  + widget->current_y - 1;
    00602
    00603       int colour = _string_colourmap[_window_highlight_colour ? widget->GetHighlightColour() : TC_WHITE];
    00604
    00605       GfxFillRect(left,                 top,    left,                   bottom - WD_BEVEL_BOTTOM, colour);
    00606       GfxFillRect(left + WD_BEVEL_LEFT, top,    right - WD_BEVEL_RIGHT, top,                      colour);
    00607       GfxFillRect(right,                top,    right,                  bottom - WD_BEVEL_BOTTOM, colour);
    00608       GfxFillRect(left,                 bottom, right,                  bottom,                   colour);
    00609     }
    00610   }
    00611 }
    00612
    00618 void Window::DrawSortButtonState(int widget, SortButtonState state) const
    00619 {
    00620   if (state == SBS_OFF) return;
    00621
    00622   assert(this->nested_array != NULL);
    00623   const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
    00624
    00625   int offset = this->IsWidgetLowered(widget) ? 1 : 0;
    00626   int base = offset + nwid->pos_x + (_current_text_dir == TD_LTR ? nwid->current_x - WD_SORTBUTTON_ARROW_WIDTH : 0);
    00627   int top = nwid->pos_y;
    00628
    00629   DrawString(base, base + WD_SORTBUTTON_ARROW_WIDTH, top + 1 + offset, state == SBS_DOWN ? DOWNARROW : UPARROW, TC_BLACK, SA_HOR_CENTER);
    00630 }
    00631
    00632
    00690 NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator()
    00691 {
    00692   this->type = tp;
    00693 }
    00694
    00695 /* ~NWidgetContainer() takes care of #next and #prev data members. */
    00696
    00744 void NWidgetBase::SetDirty(const Window *w) const
    00745 {
    00746   int abs_left = w->left + this->pos_x;
    00747   int abs_top = w->top + this->pos_y;
    00748   SetDirtyBlocks(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
    00749 }
    00750
    00764 NWidgetBase *NWidgetBase::GetWidgetOfType(WidgetType tp)
    00765 {
    00766   return (this->type == tp) ? this : NULL;
    00767 }
    00768
    00775 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y) : NWidgetBase(tp)
    00776 {
    00777   this->fill_x = fill_x;
    00778   this->fill_y = fill_y;
    00779 }
    00780
    00786 void NWidgetResizeBase::SetMinimalSize(uint min_x, uint min_y)
    00787 {
    00788   this->min_x = min_x;
    00789   this->min_y = min_y;
    00790 }
    00791
    00798 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
    00799 {
    00800   this->min_y = min_lines * GetCharacterHeight(size) + spacing;
    00801 }
    00802
    00808 void NWidgetResizeBase::SetFill(uint fill_x, uint fill_y)
    00809 {
    00810   this->fill_x = fill_x;
    00811   this->fill_y = fill_y;
    00812 }
    00813
    00819 void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y)
    00820 {
    00821   this->resize_x = resize_x;
    00822   this->resize_y = resize_y;
    00823 }
    00824
    00825 void NWidgetResizeBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00826 {
    00827   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    00828 }
    00829
    00839 NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint32 widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y)
    00840 {
    00841   this->colour = colour;
    00842   this->index = -1;
    00843   this->widget_data = widget_data;
    00844   this->tool_tip = tool_tip;
    00845   this->scrollbar_index = -1;
    00846 }
    00847
    00852 void NWidgetCore::SetIndex(int index)
    00853 {
    00854   assert(index >= 0);
    00855   this->index = index;
    00856 }
    00857
    00863 void NWidgetCore::SetDataTip(uint32 widget_data, StringID tool_tip)
    00864 {
    00865   this->widget_data = widget_data;
    00866   this->tool_tip = tool_tip;
    00867 }
    00868
    00869 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
    00870 {
    00871   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    00872 }
    00873
    00874 NWidgetCore *NWidgetCore::GetWidgetFromPos(int x, int y)
    00875 {
    00876   return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : NULL;
    00877 }
    00878
    00883 NWidgetContainer::NWidgetContainer(WidgetType tp) : NWidgetBase(tp)
    00884 {
    00885   this->head = NULL;
    00886   this->tail = NULL;
    00887 }
    00888
    00889 NWidgetContainer::~NWidgetContainer()
    00890 {
    00891   while (this->head != NULL) {
    00892     NWidgetBase *wid = this->head->next;
    00893     delete this->head;
    00894     this->head = wid;
    00895   }
    00896   this->tail = NULL;
    00897 }
    00898
    00899 NWidgetBase *NWidgetContainer::GetWidgetOfType(WidgetType tp)
    00900 {
    00901   if (this->type == tp) return this;
    00902   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00903     NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
    00904     if (nwid != NULL) return nwid;
    00905   }
    00906   return NULL;
    00907 }
    00908
    00913 void NWidgetContainer::Add(NWidgetBase *wid)
    00914 {
    00915   assert(wid->next == NULL && wid->prev == NULL);
    00916
    00917   if (this->head == NULL) {
    00918     this->head = wid;
    00919     this->tail = wid;
    00920   } else {
    00921     assert(this->tail != NULL);
    00922     assert(this->tail->next == NULL);
    00923
    00924     this->tail->next = wid;
    00925     wid->prev = this->tail;
    00926     this->tail = wid;
    00927   }
    00928 }
    00929
    00930 void NWidgetContainer::FillNestedArray(NWidgetBase **array, uint length)
    00931 {
    00932   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00933     child_wid->FillNestedArray(array, length);
    00934   }
    00935 }
    00936
    00940 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION)
    00941 {
    00942   this->index = -1;
    00943 }
    00944
    00945 void NWidgetStacked::SetIndex(int index)
    00946 {
    00947   this->index = index;
    00948 }
    00949
    00950 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
    00951 {
    00952   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    00953     assert(w->nested_array_size > (uint)this->index);
    00954     w->nested_array[this->index] = this;
    00955   }
    00956
    00957   /* Zero size plane selected */
    00958   if (this->shown_plane >= SZSP_BEGIN) {
    00959     Dimension size    = {0, 0};
    00960     Dimension padding = {0, 0};
    00961     Dimension fill    = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00962     Dimension resize  = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00963     /* Here we're primarily interested in the value of resize */
    00964     if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    00965
    00966     this->smallest_x = size.width;
    00967     this->smallest_y = size.height;
    00968     this->fill_x = fill.width;
    00969     this->fill_y = fill.height;
    00970     this->resize_x = resize.width;
    00971     this->resize_y = resize.height;
    00972     return;
    00973   }
    00974
    00975   /* First sweep, recurse down and compute minimal size and filling. */
    00976   this->smallest_x = 0;
    00977   this->smallest_y = 0;
    00978   this->fill_x = (this->head != NULL) ? 1 : 0;
    00979   this->fill_y = (this->head != NULL) ? 1 : 0;
    00980   this->resize_x = (this->head != NULL) ? 1 : 0;
    00981   this->resize_y = (this->head != NULL) ? 1 : 0;
    00982   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00983     child_wid->SetupSmallestSize(w, init_array);
    00984
    00985     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    00986     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    00987     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    00988     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    00989     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    00990     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    00991   }
    00992 }
    00993
    00994 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00995 {
    00996   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    00997   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    00998
    00999   if (this->shown_plane >= SZSP_BEGIN) return;
    01000
    01001   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01002     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    01003     uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    01004     uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
    01005
    01006     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    01007     uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    01008     uint child_pos_y = child_wid->padding_top;
    01009
    01010     child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
    01011   }
    01012 }
    01013
    01014 void NWidgetStacked::FillNestedArray(NWidgetBase **array, uint length)
    01015 {
    01016   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01017   NWidgetContainer::FillNestedArray(array, length);
    01018 }
    01019
    01020 void NWidgetStacked::Draw(const Window *w)
    01021 {
    01022   if (this->shown_plane >= SZSP_BEGIN) return;
    01023
    01024   int plane = 0;
    01025   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01026     if (plane == this->shown_plane) {
    01027       child_wid->Draw(w);
    01028       return;
    01029     }
    01030   }
    01031
    01032   NOT_REACHED();
    01033 }
    01034
    01035 NWidgetCore *NWidgetStacked::GetWidgetFromPos(int x, int y)
    01036 {
    01037   if (this->shown_plane >= SZSP_BEGIN) return NULL;
    01038
    01039   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01040   int plane = 0;
    01041   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01042     if (plane == this->shown_plane) {
    01043       return child_wid->GetWidgetFromPos(x, y);
    01044     }
    01045   }
    01046   return NULL;
    01047 }
    01048
    01053 void NWidgetStacked::SetDisplayedPlane(int plane)
    01054 {
    01055   this->shown_plane = plane;
    01056 }
    01057
    01058 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
    01059 {
    01060   this->flags = flags;
    01061 }
    01062
    01072 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01073 {
    01074   this->pip_pre = pip_pre;
    01075   this->pip_inter = pip_inter;
    01076   this->pip_post = pip_post;
    01077 }
    01078
    01079 void NWidgetPIPContainer::Draw(const Window *w)
    01080 {
    01081   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01082     child_wid->Draw(w);
    01083   }
    01084 }
    01085
    01086 NWidgetCore *NWidgetPIPContainer::GetWidgetFromPos(int x, int y)
    01087 {
    01088   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01089
    01090   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01091     NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
    01092     if (nwid != NULL) return nwid;
    01093   }
    01094   return NULL;
    01095 }
    01096
    01098 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
    01099 {
    01100 }
    01101
    01102 void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array)
    01103 {
    01104   this->smallest_x = 0; // Sum of minimal size of all children.
    01105   this->smallest_y = 0; // Biggest child.
    01106   this->fill_x = 0;     // smallest non-zero child widget fill step.
    01107   this->fill_y = 1;     // smallest common child fill step.
    01108   this->resize_x = 0;   // smallest non-zero child widget resize step.
    01109   this->resize_y = 1;   // smallest common child resize step.
    01110
    01111   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01112   uint longest = 0; // Longest child found.
    01113   uint max_vert_fill = 0; // Biggest vertical fill step.
    01114   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01115     child_wid->SetupSmallestSize(w, init_array);
    01116     longest = max(longest, child_wid->smallest_x);
    01117     max_vert_fill = max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
    01118     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    01119   }
    01120   /* 1b. Make the container higher if needed to accommodate all children nicely. */
    01121   uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height.
    01122   uint cur_height = this->smallest_y;
    01123   for (;;) {
    01124     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01125       uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
    01126       uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01127       if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting children are not interesting.
    01128         uint remainder = (cur_height - child_height) % step_size;
    01129         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01130           cur_height += step_size - remainder;
    01131           assert(cur_height < max_smallest); // Safeguard against infinite height expansion.
    01132           /* Remaining children will adapt to the new cur_height, thus speeding up the computation. */
    01133         }
    01134       }
    01135     }
    01136     if (this->smallest_y == cur_height) break;
    01137     this->smallest_y = cur_height; // Smallest height got changed, try again.
    01138   }
    01139   /* 2. For containers that must maintain equal width, extend child minimal size. */
    01140   if (this->flags & NC_EQUALSIZE) {
    01141     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01142       if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
    01143     }
    01144   }
    01145   /* 3. Move PIP space to the children, compute smallest, fill, and resize values of the container. */
    01146   if (this->head != NULL) this->head->padding_left += this->pip_pre;
    01147   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01148     if (child_wid->next != NULL) {
    01149       child_wid->padding_right += this->pip_inter;
    01150     } else {
    01151       child_wid->padding_right += this->pip_post;
    01152     }
    01153
    01154     this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01155     if (child_wid->fill_x > 0) {
    01156       if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
    01157     }
    01158     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    01159
    01160     if (child_wid->resize_x > 0) {
    01161       if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
    01162     }
    01163     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    01164   }
    01165   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01166   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01167 }
    01168
    01169 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01170 {
    01171   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01172
    01173   /* Compute additional width given to us. */
    01174   uint additional_length = given_width;
    01175   if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
    01176     /* For EQUALSIZE containers this does not sum to smallest_x during initialisation */
    01177     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01178       additional_length -= child_wid->smallest_x + child_wid->padding_right + child_wid->padding_left;
    01179     }
    01180   } else {
    01181     additional_length -= this->smallest_x;
    01182   }
    01183
    01184   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01185
    01186   /* In principle, the additional horizontal space is distributed evenly over the available resizable children. Due to step sizes, this may not always be feasible.
    01187    * To make resizing work as good as possible, first children with biggest step sizes are done. These may get less due to rounding down.
    01188    * 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
    01189    * of the child with the smallest non-zero stepsize.
    01190    *
    01191    * 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
    01192    * size and position, and directly call child->AssignSizePosition() with the computed values.
    01193    * 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
    01194    * then we call the child.
    01195    */
    01196
    01197   /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle vertical size for all children,
    01198    * handle horizontal size for non-resizing children.
    01199    */
    01200   int num_changing_childs = 0; // Number of children that can change size.
    01201   uint biggest_stepsize = 0;
    01202   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01203     uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01204     if (hor_step > 0) {
    01205       num_changing_childs++;
    01206       biggest_stepsize = max(biggest_stepsize, hor_step);
    01207     } else {
    01208       child_wid->current_x = child_wid->smallest_x;
    01209     }
    01210
    01211     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    01212     child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    01213   }
    01214
    01215   /* Second loop: Allocate the additional horizontal space over the resizing children, starting with the biggest resize steps. */
    01216   while (biggest_stepsize > 0) {
    01217     uint next_biggest_stepsize = 0;
    01218     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01219       uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01220       if (hor_step > biggest_stepsize) continue; // Already done
    01221       if (hor_step == biggest_stepsize) {
    01222         uint increment = additional_length / num_changing_childs;
    01223         num_changing_childs--;
    01224         if (hor_step > 1) increment -= increment % hor_step;
    01225         child_wid->current_x = child_wid->smallest_x + increment;
    01226         additional_length -= increment;
    01227         continue;
    01228       }
    01229       next_biggest_stepsize = max(next_biggest_stepsize, hor_step);
    01230     }
    01231     biggest_stepsize = next_biggest_stepsize;
    01232   }
    01233   assert(num_changing_childs == 0);
    01234
    01235   /* Third loop: Compute position and call the child. */
    01236   uint position = 0; // Place to put next child relative to origin of the container.
    01237   NWidgetBase *child_wid = rtl ? this->tail : this->head;
    01238   while (child_wid != NULL) {
    01239     uint child_width = child_wid->current_x;
    01240     uint child_x = x + position + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01241     uint child_y = y + child_wid->padding_top;
    01242
    01243     child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
    01244     position += child_width + child_wid->padding_right + child_wid->padding_left;
    01245
    01246     child_wid = rtl ? child_wid->prev : child_wid->next;
    01247   }
    01248 }
    01249
    01251 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
    01252 {
    01253   this->type = NWID_HORIZONTAL_LTR;
    01254 }
    01255
    01256 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01257 {
    01258   NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
    01259 }
    01260
    01262 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
    01263 {
    01264 }
    01265
    01266 void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array)
    01267 {
    01268   this->smallest_x = 0; // Biggest child.
    01269   this->smallest_y = 0; // Sum of minimal size of all children.
    01270   this->fill_x = 1;     // smallest common child fill step.
    01271   this->fill_y = 0;     // smallest non-zero child widget fill step.
    01272   this->resize_x = 1;   // smallest common child resize step.
    01273   this->resize_y = 0;   // smallest non-zero child widget resize step.
    01274
    01275   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01276   uint highest = 0; // Highest child found.
    01277   uint max_hor_fill = 0; // Biggest horizontal fill step.
    01278   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01279     child_wid->SetupSmallestSize(w, init_array);
    01280     highest = max(highest, child_wid->smallest_y);
    01281     max_hor_fill = max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
    01282     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    01283   }
    01284   /* 1b. Make the container wider if needed to accommodate all children nicely. */
    01285   uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height.
    01286   uint cur_width = this->smallest_x;
    01287   for (;;) {
    01288     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01289       uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
    01290       uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01291       if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting children are not interesting.
    01292         uint remainder = (cur_width - child_width) % step_size;
    01293         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01294           cur_width += step_size - remainder;
    01295           assert(cur_width < max_smallest); // Safeguard against infinite width expansion.
    01296           /* Remaining children will adapt to the new cur_width, thus speeding up the computation. */
    01297         }
    01298       }
    01299     }
    01300     if (this->smallest_x == cur_width) break;
    01301     this->smallest_x = cur_width; // Smallest width got changed, try again.
    01302   }
    01303   /* 2. For containers that must maintain equal width, extend children minimal size. */
    01304   if (this->flags & NC_EQUALSIZE) {
    01305     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01306       if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
    01307     }
    01308   }
    01309   /* 3. Move PIP space to the child, compute smallest, fill, and resize values of the container. */
    01310   if (this->head != NULL) this->head->padding_top += this->pip_pre;
    01311   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01312     if (child_wid->next != NULL) {
    01313       child_wid->padding_bottom += this->pip_inter;
    01314     } else {
    01315       child_wid->padding_bottom += this->pip_post;
    01316     }
    01317
    01318     this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01319     if (child_wid->fill_y > 0) {
    01320       if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
    01321     }
    01322     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    01323
    01324     if (child_wid->resize_y > 0) {
    01325       if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
    01326     }
    01327     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    01328   }
    01329   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01330   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01331 }
    01332
    01333 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01334 {
    01335   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01336
    01337   /* Compute additional height given to us. */
    01338   uint additional_length = given_height;
    01339   if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
    01340     /* For EQUALSIZE containers this does not sum to smallest_y during initialisation */
    01341     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01342       additional_length -= child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01343     }
    01344   } else {
    01345     additional_length -= this->smallest_y;
    01346   }
    01347
    01348   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01349
    01350   /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the children with the biggest resize steps.
    01351    * It also stores computed widths and heights into current_x and current_y values of the child.
    01352    */
    01353
    01354   /* 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. */
    01355   int num_changing_childs = 0; // Number of children that can change size.
    01356   uint biggest_stepsize = 0;
    01357   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01358     uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01359     if (vert_step > 0) {
    01360       num_changing_childs++;
    01361       biggest_stepsize = max(biggest_stepsize, vert_step);
    01362     } else {
    01363       child_wid->current_y = child_wid->smallest_y;
    01364     }
    01365
    01366     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    01367     child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    01368   }
    01369
    01370   /* Second loop: Allocate the additional vertical space over the resizing children, starting with the biggest resize steps. */
    01371   while (biggest_stepsize > 0) {
    01372     uint next_biggest_stepsize = 0;
    01373     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01374       uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01375       if (vert_step > biggest_stepsize) continue; // Already done
    01376       if (vert_step == biggest_stepsize) {
    01377         uint increment = additional_length / num_changing_childs;
    01378         num_changing_childs--;
    01379         if (vert_step > 1) increment -= increment % vert_step;
    01380         child_wid->current_y = child_wid->smallest_y + increment;
    01381         additional_length -= increment;
    01382         continue;
    01383       }
    01384       next_biggest_stepsize = max(next_biggest_stepsize, vert_step);
    01385     }
    01386     biggest_stepsize = next_biggest_stepsize;
    01387   }
    01388   assert(num_changing_childs == 0);
    01389
    01390   /* Third loop: Compute position and call the child. */
    01391   uint position = 0; // Place to put next child relative to origin of the container.
    01392   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01393     uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01394     uint child_height = child_wid->current_y;
    01395
    01396     child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
    01397     position += child_height + child_wid->padding_top + child_wid->padding_bottom;
    01398   }
    01399 }
    01400
    01406 NWidgetSpacer::NWidgetSpacer(int length, int height) : NWidgetResizeBase(NWID_SPACER, 0, 0)
    01407 {
    01408   this->SetMinimalSize(length, height);
    01409   this->SetResize(0, 0);
    01410 }
    01411
    01412 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
    01413 {
    01414   this->smallest_x = this->min_x;
    01415   this->smallest_y = this->min_y;
    01416 }
    01417
    01418 void NWidgetSpacer::FillNestedArray(NWidgetBase **array, uint length)
    01419 {
    01420 }
    01421
    01422 void NWidgetSpacer::Draw(const Window *w)
    01423 {
    01424   /* Spacer widget is never visible. */
    01425 }
    01426
    01427 void NWidgetSpacer::SetDirty(const Window *w) const
    01428 {
    01429   /* Spacer widget never need repainting. */
    01430 }
    01431
    01432 NWidgetCore *NWidgetSpacer::GetWidgetFromPos(int x, int y)
    01433 {
    01434   return NULL;
    01435 }
    01436
    01437 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX, NC_EQUALSIZE), index(-1), clicked(-1), count(-1)
    01438 {
    01439 }
    01440
    01441 void NWidgetMatrix::SetIndex(int index)
    01442 {
    01443   this->index = index;
    01444 }
    01445
    01446 void NWidgetMatrix::SetColour(Colours colour)
    01447 {
    01448   this->colour = colour;
    01449 }
    01450
    01455 void NWidgetMatrix::SetClicked(int clicked)
    01456 {
    01457   this->clicked = clicked;
    01458   if (this->clicked >= 0 && this->sb != NULL && this->widgets_x != 0) {
    01459     int vpos = (this->clicked / this->widgets_x) * this->widget_h; // Vertical position of the top.
    01460     /* Need to scroll down -> Scroll to the bottom.
    01461      * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
    01462     if (this->sb->GetPosition() < vpos) vpos += this->widget_h - this->pip_inter - 1;
    01463     this->sb->ScrollTowards(vpos);
    01464   }
    01465 }
    01466
    01472 void NWidgetMatrix::SetCount(int count)
    01473 {
    01474   this->count = count;
    01475
    01476   if (this->sb == NULL || this->widgets_x == 0) return;
    01477
    01478   /* We need to get the number of pixels the matrix is high/wide.
    01479    * So, determine the number of rows/columns based on the number of
    01480    * columns/rows (one is constant/unscrollable).
    01481    * Then multiply that by the height of a widget, and add the pre
    01482    * and post spacing "offsets". */
    01483   count = CeilDiv(count, this->sb->IsVertical() ? this->widgets_x : this->widgets_y);
    01484   count *= (this->sb->IsVertical() ? this->head->smallest_y : this->head->smallest_x) + this->pip_inter;
    01485   if (count > 0) count -= this->pip_inter; // We counted an inter too much in the multiplication above
    01486   count += this->pip_pre + this->pip_post;
    01487   this->sb->SetCount(count);
    01488   this->sb->SetCapacity(this->sb->IsVertical() ? this->current_y : this->current_x);
    01489   this->sb->SetStepSize(this->sb->IsVertical() ? this->widget_h  : this->widget_w);
    01490 }
    01491
    01496 void NWidgetMatrix::SetScrollbar(Scrollbar *sb)
    01497 {
    01498   this->sb = sb;
    01499 }
    01500
    01501 void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array)
    01502 {
    01503   assert(this->head != NULL);
    01504   assert(this->head->next == NULL);
    01505
    01506   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    01507     assert(w->nested_array_size > (uint)this->index);
    01508     w->nested_array[this->index] = this;
    01509   }
    01510
    01511   /* Reset the widget number. */
    01512   SB(dynamic_cast<NWidgetCore *>(this->head)->index, 16, 16, 0);
    01513   this->head->SetupSmallestSize(w, init_array);
    01514
    01515   Dimension padding = {this->pip_pre + this->pip_post, this->pip_pre + this->pip_post};
    01516   Dimension size    = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height};
    01517   Dimension fill    = {0, 0};
    01518   Dimension resize  = {this->pip_inter + this->head->smallest_x, this->pip_inter + this->head->smallest_y};
    01519
    01520   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    01521
    01522   this->smallest_x = size.width;
    01523   this->smallest_y = size.height;
    01524   this->fill_x = fill.width;
    01525   this->fill_y = fill.height;
    01526   this->resize_x = resize.width;
    01527   this->resize_y = resize.height;
    01528 }
    01529
    01530 void NWidgetMatrix::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01531 {
    01532   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01533
    01534   this->pos_x = x;
    01535   this->pos_y = y;
    01536   this->current_x = given_width;
    01537   this->current_y = given_height;
    01538
    01539   /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
    01540   this->widget_w = this->head->smallest_x + this->pip_inter;
    01541   this->widget_h = this->head->smallest_y + this->pip_inter;
    01542
    01543   /* Account for the pip_inter is between widgets, so we need to account for that when
    01544    * the division assumes pip_inter is used for all widgets. */
    01545   this->widgets_x = CeilDiv(this->current_x - this->pip_pre - this->pip_post + this->pip_inter, this->widget_w);
    01546   this->widgets_y = CeilDiv(this->current_y - this->pip_pre - this->pip_post + this->pip_inter, this->widget_h);
    01547
    01548   /* When resizing, update the scrollbar's count. E.g. with a vertical
    01549    * scrollbar becoming wider or narrower means the amount of rows in
    01550    * the scrollbar becomes respectively smaller or higher. */
    01551   this->SetCount(this->count);
    01552 }
    01553
    01554 void NWidgetMatrix::FillNestedArray(NWidgetBase **array, uint length)
    01555 {
    01556   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01557   NWidgetContainer::FillNestedArray(array, length);
    01558 }
    01559
    01560 NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y)
    01561 {
    01562   /* Falls outside of the matrix widget. */
    01563   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01564
    01565   int start_x, start_y, base_offs_x, base_offs_y;
    01566   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01567
    01568   /* Swap the x offset around for RTL, so it'll behave like LTR for RTL as well. */
    01569   bool rtl = _current_text_dir == TD_RTL;
    01570   if (rtl) base_offs_x -= (this->widgets_x - 1) * this->widget_w;
    01571
    01572   int widget_col = (x - base_offs_x - (int)this->pip_pre - (int)this->pos_x) / this->widget_w;
    01573   int widget_row = (y - base_offs_y - (int)this->pip_pre - (int)this->pos_y) / this->widget_h;
    01574
    01575   if (widget_row * this->widgets_x + widget_col >= this->count) return NULL;
    01576
    01577   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01578   child->AssignSizePosition(ST_RESIZE,
    01579       this->pos_x + this->pip_pre + widget_col * this->widget_w + base_offs_x,
    01580       this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y,
    01581       child->smallest_x, child->smallest_y, rtl);
    01582
    01583   /* "Undo" the RTL swap here to get the right widget index. */
    01584   if (rtl) widget_col = this->widgets_x - widget_col - 1;
    01585   SB(child->index, 16, 16, (widget_row + start_y) * this->widgets_x + widget_col + start_x);
    01586
    01587   return child->GetWidgetFromPos(x, y);
    01588 }
    01589
    01590 /* virtual */ void NWidgetMatrix::Draw(const Window *w)
    01591 {
    01592   /* Fill the background. */
    01593   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]);
    01594
    01595   /* Set up a clipping area for the previews. */
    01596   DrawPixelInfo tmp_dpi;
    01597   if (!FillDrawPixelInfo(&tmp_dpi, this->pos_x + 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;
    01598   DrawPixelInfo *old_dpi = _cur_dpi;
    01599   _cur_dpi = &tmp_dpi;
    01600
    01601   /* Get the appropriate offsets so we can draw the right widgets. */
    01602   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01603   bool rtl = _current_text_dir == TD_RTL;
    01604   int start_x, start_y, base_offs_x, base_offs_y;
    01605   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01606
    01607   int offs_y = base_offs_y;
    01608   for (int y = start_y; y < start_y + this->widgets_y + 1; y++, offs_y += this->widget_h) {
    01609     /* Are we within bounds? */
    01610     if (offs_y + child->smallest_y <= 0) continue;
    01611     if (offs_y >= (int)this->current_y) break;
    01612
    01613     /* We've passed our amount of widgets. */
    01614     if (y * this->widgets_x >= this->count) break;
    01615
    01616     int offs_x = base_offs_x;
    01617     for (int x = start_x; x < start_x + this->widgets_x + 1; x++, offs_x += rtl ? -this->widget_w : this->widget_w) {
    01618       /* Are we within bounds? */
    01619       if (offs_x + child->smallest_x <= 0) continue;
    01620       if (offs_x >= (int)this->current_x) continue;
    01621
    01622       /* Do we have this many widgets? */
    01623       int sub_wid = y * this->widgets_x + x;
    01624       if (sub_wid >= this->count) break;
    01625
    01626       child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl);
    01627       child->SetLowered(this->clicked == sub_wid);
    01628       SB(child->index, 16, 16, sub_wid);
    01629       child->Draw(w);
    01630     }
    01631   }
    01632
    01633   /* Restore the clipping area. */
    01634   _cur_dpi = old_dpi;
    01635 }
    01636
    01644 void NWidgetMatrix::GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
    01645 {
    01646   base_offs_x = 0;
    01647   base_offs_y = 0;
    01648   start_x = 0;
    01649   start_y = 0;
    01650   if (this->sb != NULL) {
    01651     if (this->sb->IsVertical()) {
    01652       start_y = this->sb->GetPosition() / this->widget_h;
    01653       base_offs_y = -this->sb->GetPosition() + start_y * this->widget_h;
    01654       if (_current_text_dir == TD_RTL) base_offs_x = this->pip_pre + this->widget_w * (this->widgets_x - 1) - this->pip_inter;
    01655     } else {
    01656       start_x = this->sb->GetPosition() / this->widget_w;
    01657       if (_current_text_dir == TD_RTL) {
    01658         base_offs_x = this->sb->GetCapacity() + this->sb->GetPosition() - (start_x + 1) * this->widget_w + this->pip_inter - this->pip_post - this->pip_pre;
    01659       } else {
    01660         base_offs_x = -this->sb->GetPosition() + start_x * this->widget_w;
    01661       }
    01662     }
    01663   }
    01664 }
    01665
    01675 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
    01676 {
    01677   assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
    01678   if (index >= 0) this->SetIndex(index);
    01679   this->child = child;
    01680 }
    01681
    01682 NWidgetBackground::~NWidgetBackground()
    01683 {
    01684   if (this->child != NULL) delete this->child;
    01685 }
    01686
    01694 void NWidgetBackground::Add(NWidgetBase *nwid)
    01695 {
    01696   if (this->child == NULL) {
    01697     this->child = new NWidgetVertical();
    01698   }
    01699   this->child->Add(nwid);
    01700 }
    01701
    01712 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01713 {
    01714   if (this->child == NULL) {
    01715     this->child = new NWidgetVertical();
    01716   }
    01717   this->child->SetPIP(pip_pre, pip_inter, pip_post);
    01718 }
    01719
    01720 void NWidgetBackground::SetupSmallestSize(Window *w, bool init_array)
    01721 {
    01722   if (init_array && this->index >= 0) {
    01723     assert(w->nested_array_size > (uint)this->index);
    01724     w->nested_array[this->index] = this;
    01725   }
    01726   if (this->child != NULL) {
    01727     this->child->SetupSmallestSize(w, init_array);
    01728
    01729     this->smallest_x = this->child->smallest_x;
    01730     this->smallest_y = this->child->smallest_y;
    01731     this->fill_x = this->child->fill_x;
    01732     this->fill_y = this->child->fill_y;
    01733     this->resize_x = this->child->resize_x;
    01734     this->resize_y = this->child->resize_y;
    01735
    01736     /* Account for the size of the frame's text if that exists */
    01737     if (w != NULL && this->type == WWT_FRAME) {
    01738       this->child->padding_left   = WD_FRAMETEXT_LEFT;
    01739       this->child->padding_right  = WD_FRAMETEXT_RIGHT;
    01740       this->child->padding_top    = max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
    01741       this->child->padding_bottom = WD_FRAMETEXT_BOTTOM;
    01742
    01743       this->smallest_x += this->child->padding_left + this->child->padding_right;
    01744       this->smallest_y += this->child->padding_top + this->child->padding_bottom;
    01745
    01746       if (this->index >= 0) w->SetStringParameters(this->index);
    01747       this->smallest_x = max(this->smallest_x, GetStringBoundingBox(this->widget_data).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
    01748     }
    01749   } else {
    01750     Dimension d = {this->min_x, this->min_y};
    01751     Dimension fill = {this->fill_x, this->fill_y};
    01752     Dimension resize  = {this->resize_x, this->resize_y};
    01753     if (w != NULL) { // A non-NULL window pointer acts as switch to turn dynamic widget size on.
    01754       if (this->type == WWT_FRAME || this->type == WWT_INSET) {
    01755         if (this->index >= 0) w->SetStringParameters(this->index);
    01756         Dimension background = GetStringBoundingBox(this->widget_data);
    01757         background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
    01758         d = maxdim(d, background);
    01759       }
    01760       if (this->index >= 0) {
    01761         static const Dimension padding = {0, 0};
    01762         w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
    01763       }
    01764     }
    01765     this->smallest_x = d.width;
    01766     this->smallest_y = d.height;
    01767     this->fill_x = fill.width;
    01768     this->fill_y = fill.height;
    01769     this->resize_x = resize.width;
    01770     this->resize_y = resize.height;
    01771   }
    01772 }
    01773
    01774 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01775 {
    01776   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01777
    01778   if (this->child != NULL) {
    01779     uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
    01780     uint width = given_width - this->child->padding_right - this->child->padding_left;
    01781     uint height = given_height - this->child->padding_top - this->child->padding_bottom;
    01782     this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
    01783   }
    01784 }
    01785
    01786 void NWidgetBackground::FillNestedArray(NWidgetBase **array, uint length)
    01787 {
    01788   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01789   if (this->child != NULL) this->child->FillNestedArray(array, length);
    01790 }
    01791
    01792 void NWidgetBackground::Draw(const Window *w)
    01793 {
    01794   if (this->current_x == 0 || this->current_y == 0) return;
    01795
    01796   Rect r;
    01797   r.left = this->pos_x;
    01798   r.right = this->pos_x + this->current_x - 1;
    01799   r.top = this->pos_y;
    01800   r.bottom = this->pos_y + this->current_y - 1;
    01801
    01802   const DrawPixelInfo *dpi = _cur_dpi;
    01803   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01804
    01805   switch (this->type) {
    01806     case WWT_PANEL:
    01807       assert(this->widget_data == 0);
    01808       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
    01809       break;
    01810
    01811     case WWT_FRAME:
    01812       if (this->index >= 0) w->SetStringParameters(this->index);
    01813       DrawFrame(r, this->colour, this->widget_data);
    01814       break;
    01815
    01816     case WWT_INSET:
    01817       if (this->index >= 0) w->SetStringParameters(this->index);
    01818       DrawInset(r, this->colour, this->widget_data);
    01819       break;
    01820
    01821     default:
    01822       NOT_REACHED();
    01823   }
    01824
    01825   if (this->index >= 0) w->DrawWidget(r, this->index);
    01826   if (this->child != NULL) this->child->Draw(w);
    01827
    01828   if (this->IsDisabled()) {
    01829     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    01830   }
    01831 }
    01832
    01833 NWidgetCore *NWidgetBackground::GetWidgetFromPos(int x, int y)
    01834 {
    01835   NWidgetCore *nwid = NULL;
    01836   if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
    01837     if (this->child != NULL) nwid = this->child->GetWidgetFromPos(x, y);
    01838     if (nwid == NULL) nwid = this;
    01839   }
    01840   return nwid;
    01841 }
    01842
    01843 NWidgetBase *NWidgetBackground::GetWidgetOfType(WidgetType tp)
    01844 {
    01845   NWidgetBase *nwid = NULL;
    01846   if (this->child != NULL) nwid = this->child->GetWidgetOfType(tp);
    01847   if (nwid == NULL && this->type == tp) nwid = this;
    01848   return nwid;
    01849 }
    01850
    01851 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
    01852 {
    01853   this->SetIndex(index);
    01854 }
    01855
    01856 void NWidgetViewport::SetupSmallestSize(Window *w, bool init_array)
    01857 {
    01858   if (init_array && this->index >= 0) {
    01859     assert(w->nested_array_size > (uint)this->index);
    01860     w->nested_array[this->index] = this;
    01861   }
    01862   this->smallest_x = this->min_x;
    01863   this->smallest_y = this->min_y;
    01864 }
    01865
    01866 void NWidgetViewport::Draw(const Window *w)
    01867 {
    01868   if (this->disp_flags & ND_NO_TRANSPARENCY) {
    01869     TransparencyOptionBits to_backup = _transparency_opt;
    01870     _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_LOADING); // Disable all transparency, except textual stuff
    01871     w->DrawViewport();
    01872     _transparency_opt = to_backup;
    01873   } else {
    01874     w->DrawViewport();
    01875   }
    01876
    01877   /* Optionally shade the viewport. */
    01878   if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
    01879     GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
    01880         (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
    01881   }
    01882 }
    01883
    01890 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
    01891 {
    01892   InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
    01893 }
    01894
    01899 void NWidgetViewport::UpdateViewportCoordinates(Window *w)
    01900 {
    01901   ViewPort *vp = w->viewport;
    01902   if (vp != NULL) {
    01903     vp->left = w->left + this->pos_x;
    01904     vp->top  = w->top + this->pos_y;
    01905     vp->width  = this->current_x;
    01906     vp->height = this->current_y;
    01907
    01908     vp->virtual_width  = ScaleByZoom(vp->width, vp->zoom);
    01909     vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
    01910   }
    01911 }
    01912
    01922 int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
    01923 {
    01924   uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
    01925   if (pos != INT_MAX) pos += this->GetPosition();
    01926   return (pos >= this->GetCount()) ? INT_MAX : pos;
    01927 }
    01928
    01936 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
    01937 {
    01938   NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
    01939   if (this->IsVertical()) {
    01940     this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
    01941   } else {
    01942     this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
    01943   }
    01944 }
    01945
    01952 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL), Scrollbar(tp != NWID_HSCROLLBAR)
    01953 {
    01954   assert(tp == NWID_HSCROLLBAR || tp == NWID_VSCROLLBAR);
    01955   this->SetIndex(index);
    01956
    01957   switch (this->type) {
    01958     case NWID_HSCROLLBAR:
    01959       this->SetMinimalSize(0, NWidgetScrollbar::GetHorizontalDimension().height);
    01960       this->SetResize(1, 0);
    01961       this->SetFill(1, 0);
    01962       this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
    01963       break;
    01964
    01965     case NWID_VSCROLLBAR:
    01966       this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width, 0);
    01967       this->SetResize(0, 1);
    01968       this->SetFill(0, 1);
    01969       this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
    01970       break;
    01971
    01972     default: NOT_REACHED();
    01973   }
    01974 }
    01975
    01976 void NWidgetScrollbar::SetupSmallestSize(Window *w, bool init_array)
    01977 {
    01978   if (init_array && this->index >= 0) {
    01979     assert(w->nested_array_size > (uint)this->index);
    01980     w->nested_array[this->index] = this;
    01981   }
    01982   this->smallest_x = this->min_x;
    01983   this->smallest_y = this->min_y;
    01984 }
    01985
    01986 void NWidgetScrollbar::Draw(const Window *w)
    01987 {
    01988   if (this->current_x == 0 || this->current_y == 0) return;
    01989
    01990   Rect r;
    01991   r.left = this->pos_x;
    01992   r.right = this->pos_x + this->current_x - 1;
    01993   r.top = this->pos_y;
    01994   r.bottom = this->pos_y + this->current_y - 1;
    01995
    01996   const DrawPixelInfo *dpi = _cur_dpi;
    01997   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01998
    01999   bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
    02000   bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
    02001   bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->scrolling_scrollbar == this->index;
    02002
    02003   if (this->type == NWID_HSCROLLBAR) {
    02004     DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    02005   } else {
    02006     DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    02007   }
    02008
    02009   if (this->IsDisabled()) {
    02010     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    02011   }
    02012 }
    02013
    02014 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
    02015 {
    02016   vertical_dimension.width   = vertical_dimension.height   = 0;
    02017   horizontal_dimension.width = horizontal_dimension.height = 0;
    02018 }
    02019
    02020 /* static */ Dimension NWidgetScrollbar::GetVerticalDimension()
    02021 {
    02022   static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
    02023   if (vertical_dimension.width == 0) {
    02024     vertical_dimension = maxdim(GetSpriteSize(SPR_ARROW_UP), GetSpriteSize(SPR_ARROW_DOWN));
    02025     vertical_dimension.width += extra.width;
    02026     vertical_dimension.height += extra.height;
    02027   }
    02028   return vertical_dimension;
    02029 }
    02030
    02031 /* static */ Dimension NWidgetScrollbar::GetHorizontalDimension()
    02032 {
    02033   static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
    02034   if (horizontal_dimension.width == 0) {
    02035     horizontal_dimension = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    02036     horizontal_dimension.width += extra.width;
    02037     horizontal_dimension.height += extra.height;
    02038   }
    02039   return horizontal_dimension;
    02040 }
    02041
    02042 Dimension NWidgetScrollbar::vertical_dimension = {0, 0};
    02043 Dimension NWidgetScrollbar::horizontal_dimension = {0, 0};
    02044
    02046 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
    02047 {
    02048   shadebox_dimension.width  = shadebox_dimension.height  = 0;
    02049   debugbox_dimension.width  = debugbox_dimension.height  = 0;
    02050   stickybox_dimension.width = stickybox_dimension.height = 0;
    02051   resizebox_dimension.width = resizebox_dimension.height = 0;
    02052   closebox_dimension.width  = closebox_dimension.height  = 0;
    02053 }
    02054
    02055 Dimension NWidgetLeaf::shadebox_dimension  = {0, 0};
    02056 Dimension NWidgetLeaf::debugbox_dimension  = {0, 0};
    02057 Dimension NWidgetLeaf::defsizebox_dimension = {0, 0};
    02058 Dimension NWidgetLeaf::stickybox_dimension = {0, 0};
    02059 Dimension NWidgetLeaf::resizebox_dimension = {0, 0};
    02060 Dimension NWidgetLeaf::closebox_dimension  = {0, 0};
    02061
    02070 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint16 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
    02071 {
    02072   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);
    02073   if (index >= 0) this->SetIndex(index);
    02074   this->SetMinimalSize(0, 0);
    02075   this->SetResize(0, 0);
    02076
    02077   switch (tp) {
    02078     case WWT_EMPTY:
    02079       break;
    02080
    02081     case WWT_PUSHBTN:
    02082     case WWT_IMGBTN:
    02083     case WWT_PUSHIMGBTN:
    02084     case WWT_IMGBTN_2:
    02085     case WWT_TEXTBTN:
    02086     case WWT_PUSHTXTBTN:
    02087     case WWT_TEXTBTN_2:
    02088     case WWT_LABEL:
    02089     case WWT_TEXT:
    02090     case WWT_MATRIX:
    02091     case NWID_BUTTON_DROPDOWN:
    02092     case NWID_PUSHBUTTON_DROPDOWN:
    02093     case WWT_ARROWBTN:
    02094     case WWT_PUSHARROWBTN:
    02095       this->SetFill(0, 0);
    02096       break;
    02097
    02098     case WWT_EDITBOX: {
    02099       Dimension sprite_size = GetSpriteSize(_current_text_dir == TD_RTL ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
    02100       this->SetMinimalSize(30 + sprite_size.width, sprite_size.height);
    02101       this->SetFill(0, 0);
    02102       break;
    02103     }
    02104
    02105     case WWT_CAPTION:
    02106       this->SetFill(1, 0);
    02107       this->SetResize(1, 0);
    02108       this->min_y = WD_CAPTION_HEIGHT;
    02109       this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
    02110       break;
    02111
    02112     case WWT_STICKYBOX:
    02113       this->SetFill(0, 0);
    02114       this->SetMinimalSize(WD_STICKYBOX_WIDTH, WD_CAPTION_HEIGHT);
    02115       this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
    02116       break;
    02117
    02118     case WWT_SHADEBOX:
    02119       this->SetFill(0, 0);
    02120       this->SetMinimalSize(WD_SHADEBOX_TOP, WD_CAPTION_HEIGHT);
    02121       this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
    02122       break;
    02123
    02124     case WWT_DEBUGBOX:
    02125       this->SetFill(0, 0);
    02126       this->SetMinimalSize(WD_DEBUGBOX_TOP, WD_CAPTION_HEIGHT);
    02127       this->SetDataTip(STR_NULL, STR_TOOLTIP_DEBUG);
    02128       break;
    02129
    02130     case WWT_DEFSIZEBOX:
    02131       this->SetFill(0, 0);
    02132       this->SetMinimalSize(WD_DEFSIZEBOX_TOP, WD_CAPTION_HEIGHT);
    02133       this->SetDataTip(STR_NULL, STR_TOOLTIP_DEFSIZE);
    02134       break;
    02135
    02136     case WWT_RESIZEBOX:
    02137       this->SetFill(0, 0);
    02138       this->SetMinimalSize(WD_RESIZEBOX_WIDTH, 12);
    02139       this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
    02140       break;
    02141
    02142     case WWT_CLOSEBOX:
    02143       this->SetFill(0, 0);
    02144       this->SetMinimalSize(WD_CLOSEBOX_WIDTH, WD_CAPTION_HEIGHT);
    02145       this->SetDataTip(STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW);
    02146       break;
    02147
    02148     case WWT_DROPDOWN:
    02149       this->SetFill(0, 0);
    02150       this->min_y = WD_DROPDOWN_HEIGHT;
    02151       break;
    02152
    02153     default:
    02154       NOT_REACHED();
    02155   }
    02156 }
    02157
    02158 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
    02159 {
    02160   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    02161     assert(w->nested_array_size > (uint)this->index);
    02162     w->nested_array[this->index] = this;
    02163   }
    02164
    02165   Dimension size = {this->min_x, this->min_y};
    02166   Dimension fill = {this->fill_x, this->fill_y};
    02167   Dimension resize = {this->resize_x, this->resize_y};
    02168   /* Get padding, and update size with the real content size if appropriate. */
    02169   const Dimension *padding = NULL;
    02170   switch (this->type) {
    02171     case WWT_EMPTY: {
    02172       static const Dimension extra = {0, 0};
    02173       padding = &extra;
    02174       break;
    02175     }
    02176     case WWT_MATRIX: {
    02177       static const Dimension extra = {WD_MATRIX_LEFT + WD_MATRIX_RIGHT, WD_MATRIX_TOP + WD_MATRIX_BOTTOM};
    02178       padding = &extra;
    02179       break;
    02180     }
    02181     case WWT_SHADEBOX: {
    02182       static const Dimension extra = {WD_SHADEBOX_LEFT + WD_SHADEBOX_RIGHT, WD_SHADEBOX_TOP + WD_SHADEBOX_BOTTOM};
    02183       padding = &extra;
    02184       if (NWidgetLeaf::shadebox_dimension.width == 0) {
    02185         NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
    02186         NWidgetLeaf::shadebox_dimension.width += extra.width;
    02187         NWidgetLeaf::shadebox_dimension.height += extra.height;
    02188       }
    02189       size = maxdim(size, NWidgetLeaf::shadebox_dimension);
    02190       break;
    02191     }
    02192     case WWT_DEBUGBOX:
    02193       if (_settings_client.gui.newgrf_developer_tools && w->IsNewGRFInspectable()) {
    02194         static const Dimension extra = {WD_DEBUGBOX_LEFT + WD_DEBUGBOX_RIGHT, WD_DEBUGBOX_TOP + WD_DEBUGBOX_BOTTOM};
    02195         padding = &extra;
    02196         if (NWidgetLeaf::debugbox_dimension.width == 0) {
    02197           NWidgetLeaf::debugbox_dimension = GetSpriteSize(SPR_WINDOW_DEBUG);
    02198           NWidgetLeaf::debugbox_dimension.width += extra.width;
    02199           NWidgetLeaf::debugbox_dimension.height += extra.height;
    02200         }
    02201         size = maxdim(size, NWidgetLeaf::debugbox_dimension);
    02202       } else {
    02203         /* If the setting is disabled we don't want to see it! */
    02204         size.width = 0;
    02205         fill.width = 0;
    02206         resize.width = 0;
    02207       }
    02208       break;
    02209
    02210     case WWT_STICKYBOX: {
    02211       static const Dimension extra = {WD_STICKYBOX_LEFT + WD_STICKYBOX_RIGHT, WD_STICKYBOX_TOP + WD_STICKYBOX_BOTTOM};
    02212       padding = &extra;
    02213       if (NWidgetLeaf::stickybox_dimension.width == 0) {
    02214         NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
    02215         NWidgetLeaf::stickybox_dimension.width += extra.width;
    02216         NWidgetLeaf::stickybox_dimension.height += extra.height;
    02217       }
    02218       size = maxdim(size, NWidgetLeaf::stickybox_dimension);
    02219       break;
    02220     }
    02221
    02222     case WWT_DEFSIZEBOX: {
    02223       static const Dimension extra = {WD_DEFSIZEBOX_LEFT + WD_DEFSIZEBOX_RIGHT, WD_DEFSIZEBOX_TOP + WD_DEFSIZEBOX_BOTTOM};
    02224       padding = &extra;
    02225       if (NWidgetLeaf::defsizebox_dimension.width == 0) {
    02226         NWidgetLeaf::defsizebox_dimension = GetSpriteSize(SPR_WINDOW_DEFSIZE);
    02227         NWidgetLeaf::defsizebox_dimension.width += extra.width;
    02228         NWidgetLeaf::defsizebox_dimension.height += extra.height;
    02229       }
    02230       size = maxdim(size, NWidgetLeaf::defsizebox_dimension);
    02231       break;
    02232     }
    02233
    02234     case WWT_RESIZEBOX: {
    02235       static const Dimension extra = {WD_RESIZEBOX_LEFT + WD_RESIZEBOX_RIGHT, WD_RESIZEBOX_TOP + WD_RESIZEBOX_BOTTOM};
    02236       padding = &extra;
    02237       if (NWidgetLeaf::resizebox_dimension.width == 0) {
    02238         NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
    02239         NWidgetLeaf::resizebox_dimension.width += extra.width;
    02240         NWidgetLeaf::resizebox_dimension.height += extra.height;
    02241       }
    02242       size = maxdim(size, NWidgetLeaf::resizebox_dimension);
    02243       break;
    02244     }
    02245     case WWT_EDITBOX:
    02246       size.height = max(size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    02247       /* FALL THROUGH */
    02248     case WWT_PUSHBTN: {
    02249       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02250       padding = &extra;
    02251       break;
    02252     }
    02253     case WWT_IMGBTN:
    02254     case WWT_IMGBTN_2:
    02255     case WWT_PUSHIMGBTN: {
    02256       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02257       padding = &extra;
    02258       Dimension d2 = GetSpriteSize(this->widget_data);
    02259       if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
    02260       d2.width += extra.width;
    02261       d2.height += extra.height;
    02262       size = maxdim(size, d2);
    02263       break;
    02264     }
    02265     case WWT_ARROWBTN:
    02266     case WWT_PUSHARROWBTN: {
    02267       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02268       padding = &extra;
    02269       Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    02270       d2.width += extra.width;
    02271       d2.height += extra.height;
    02272       size = maxdim(size, d2);
    02273       break;
    02274     }
    02275
    02276     case WWT_CLOSEBOX: {
    02277       static const Dimension extra = {WD_CLOSEBOX_LEFT + WD_CLOSEBOX_RIGHT, WD_CLOSEBOX_TOP + WD_CLOSEBOX_BOTTOM};
    02278       padding = &extra;
    02279       if (NWidgetLeaf::closebox_dimension.width == 0) {
    02280         NWidgetLeaf::closebox_dimension = maxdim(GetStringBoundingBox(STR_BLACK_CROSS), GetStringBoundingBox(STR_SILVER_CROSS));
    02281         NWidgetLeaf::closebox_dimension.width += extra.width;
    02282         NWidgetLeaf::closebox_dimension.height += extra.height;
    02283       }
    02284       size = maxdim(size, NWidgetLeaf::closebox_dimension);
    02285       break;
    02286     }
    02287     case WWT_TEXTBTN:
    02288     case WWT_PUSHTXTBTN:
    02289     case WWT_TEXTBTN_2: {
    02290       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT,  WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02291       padding = &extra;
    02292       if (this->index >= 0) w->SetStringParameters(this->index);
    02293       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02294       d2.width += extra.width;
    02295       d2.height += extra.height;
    02296       size = maxdim(size, d2);
    02297       break;
    02298     }
    02299     case WWT_LABEL:
    02300     case WWT_TEXT: {
    02301       static const Dimension extra = {0, 0};
    02302       padding = &extra;
    02303       if (this->index >= 0) w->SetStringParameters(this->index);
    02304       size = maxdim(size, GetStringBoundingBox(this->widget_data));
    02305       break;
    02306     }
    02307     case WWT_CAPTION: {
    02308       static const Dimension extra = {WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT, WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM};
    02309       padding = &extra;
    02310       if (this->index >= 0) w->SetStringParameters(this->index);
    02311       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02312       d2.width += extra.width;
    02313       d2.height += extra.height;
    02314       size = maxdim(size, d2);
    02315       break;
    02316     }
    02317     case WWT_DROPDOWN:
    02318     case NWID_BUTTON_DROPDOWN:
    02319     case NWID_PUSHBUTTON_DROPDOWN: {
    02320       static const Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
    02321       padding = &extra;
    02322       if (this->index >= 0) w->SetStringParameters(this->index);
    02323       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02324       d2.width += extra.width;
    02325       d2.height += extra.height;
    02326       size = maxdim(size, d2);
    02327       break;
    02328     }
    02329     default:
    02330       NOT_REACHED();
    02331   }
    02332
    02333   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
    02334
    02335   this->smallest_x = size.width;
    02336   this->smallest_y = size.height;
    02337   this->fill_x = fill.width;
    02338   this->fill_y = fill.height;
    02339   this->resize_x = resize.width;
    02340   this->resize_y = resize.height;
    02341 }
    02342
    02343 void NWidgetLeaf::Draw(const Window *w)
    02344 {
    02345   if (this->current_x == 0 || this->current_y == 0) return;
    02346
    02347   Rect r;
    02348   r.left = this->pos_x;
    02349   r.right = this->pos_x + this->current_x - 1;
    02350   r.top = this->pos_y;
    02351   r.bottom = this->pos_y + this->current_y - 1;
    02352
    02353   const DrawPixelInfo *dpi = _cur_dpi;
    02354   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    02355
    02356   bool clicked = this->IsLowered();
    02357   switch (this->type) {
    02358     case WWT_EMPTY:
    02359       break;
    02360
    02361     case WWT_PUSHBTN:
    02362       assert(this->widget_data == 0);
    02363       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02364       break;
    02365
    02366     case WWT_IMGBTN:
    02367     case WWT_PUSHIMGBTN:
    02368     case WWT_IMGBTN_2:
    02369       DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
    02370       break;
    02371
    02372     case WWT_TEXTBTN:
    02373     case WWT_PUSHTXTBTN:
    02374     case WWT_TEXTBTN_2:
    02375       if (this->index >= 0) w->SetStringParameters(this->index);
    02376       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02377       DrawLabel(r, this->type, clicked, this->widget_data);
    02378       break;
    02379
    02380     case WWT_ARROWBTN:
    02381     case WWT_PUSHARROWBTN: {
    02382       SpriteID sprite;
    02383       switch (this->widget_data) {
    02384         case AWV_DECREASE: sprite = _current_text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02385         case AWV_INCREASE: sprite = _current_text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02386         case AWV_LEFT:     sprite = SPR_ARROW_LEFT;  break;
    02387         case AWV_RIGHT:    sprite = SPR_ARROW_RIGHT; break;
    02388         default: NOT_REACHED();
    02389       }
    02390       DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
    02391     }
    02392
    02393     case WWT_LABEL:
    02394       if (this->index >= 0) w->SetStringParameters(this->index);
    02395       DrawLabel(r, this->type, clicked, this->widget_data);
    02396       break;
    02397
    02398     case WWT_TEXT:
    02399       if (this->index >= 0) w->SetStringParameters(this->index);
    02400       DrawText(r, (TextColour)this->colour, this->widget_data);
    02401       break;
    02402
    02403     case WWT_MATRIX:
    02404       DrawMatrix(r, this->colour, clicked, this->widget_data);
    02405       break;
    02406
    02407     case WWT_EDITBOX: {
    02408       const QueryString *query = w->GetQueryString(this->index);
    02409       if (query != NULL) query->DrawEditBox(w, this->index);
    02410       break;
    02411     }
    02412
    02413     case WWT_CAPTION:
    02414       if (this->index >= 0) w->SetStringParameters(this->index);
    02415       DrawCaption(r, this->colour, w->owner, this->widget_data);
    02416       break;
    02417
    02418     case WWT_SHADEBOX:
    02419       assert(this->widget_data == 0);
    02420       DrawShadeBox(r, this->colour, w->IsShaded());
    02421       break;
    02422
    02423     case WWT_DEBUGBOX:
    02424       DrawDebugBox(r, this->colour, clicked);
    02425       break;
    02426
    02427     case WWT_STICKYBOX:
    02428       assert(this->widget_data == 0);
    02429       DrawStickyBox(r, this->colour, !!(w->flags & WF_STICKY));
    02430       break;
    02431
    02432     case WWT_DEFSIZEBOX:
    02433       assert(this->widget_data == 0);
    02434       DrawDefSizeBox(r, this->colour, clicked);
    02435       break;
    02436
    02437     case WWT_RESIZEBOX:
    02438       assert(this->widget_data == 0);
    02439       DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags & WF_SIZING));
    02440       break;
    02441
    02442     case WWT_CLOSEBOX:
    02443       DrawCloseBox(r, this->colour, this->widget_data);
    02444       break;
    02445
    02446     case WWT_DROPDOWN:
    02447       if (this->index >= 0) w->SetStringParameters(this->index);
    02448       DrawDropdown(r, this->colour, clicked, this->widget_data);
    02449       break;
    02450
    02451     case NWID_BUTTON_DROPDOWN:
    02452     case NWID_PUSHBUTTON_DROPDOWN:
    02453       if (this->index >= 0) w->SetStringParameters(this->index);
    02454       DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
    02455       break;
    02456
    02457     default:
    02458       NOT_REACHED();
    02459   }
    02460   if (this->index >= 0) w->DrawWidget(r, this->index);
    02461
    02462   if (this->IsDisabled()) {
    02463     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    02464   }
    02465 }
    02466
    02474 bool NWidgetLeaf::ButtonHit(const Point &pt)
    02475 {
    02476   if (_current_text_dir == TD_LTR) {
    02477     int button_width = this->pos_x + this->current_x - 12;
    02478     return pt.x < button_width;
    02479   } else {
    02480     int button_left = this->pos_x + 12;
    02481     return pt.x >= button_left;
    02482   }
    02483 }
    02484
    02485 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
    02486
    02502 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
    02503 {
    02504   int num_used = 0;
    02505
    02506   *dest = NULL;
    02507   *fill_dest = false;
    02508
    02509   while (count > num_used) {
    02510     switch (parts->type) {
    02511       case NWID_SPACER:
    02512         if (*dest != NULL) return num_used;
    02513         *dest = new NWidgetSpacer(0, 0);
    02514         break;
    02515
    02516       case NWID_HORIZONTAL:
    02517         if (*dest != NULL) return num_used;
    02518         *dest = new NWidgetHorizontal(parts->u.cont_flags);
    02519         *fill_dest = true;
    02520         break;
    02521
    02522       case NWID_HORIZONTAL_LTR:
    02523         if (*dest != NULL) return num_used;
    02524         *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
    02525         *fill_dest = true;
    02526         break;
    02527
    02528       case WWT_PANEL:
    02529       case WWT_INSET:
    02530       case WWT_FRAME:
    02531         if (*dest != NULL) return num_used;
    02532         *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02533         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02534         *fill_dest = true;
    02535         break;
    02536
    02537       case NWID_VERTICAL:
    02538         if (*dest != NULL) return num_used;
    02539         *dest = new NWidgetVertical(parts->u.cont_flags);
    02540         *fill_dest = true;
    02541         break;
    02542
    02543       case NWID_MATRIX: {
    02544         if (*dest != NULL) return num_used;
    02545         NWidgetMatrix *nwm = new NWidgetMatrix();
    02546         *dest = nwm;
    02547         *fill_dest = true;
    02548         nwm->SetIndex(parts->u.widget.index);
    02549         nwm->SetColour(parts->u.widget.colour);
    02550         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02551         break;
    02552       }
    02553
    02554       case WPT_FUNCTION: {
    02555         if (*dest != NULL) return num_used;
    02556         /* Ensure proper functioning even when the called code simply writes its largest index. */
    02557         int biggest = -1;
    02558         *dest = parts->u.func_ptr(&biggest);
    02559         *biggest_index = max(*biggest_index, biggest);
    02560         *fill_dest = false;
    02561         break;
    02562       }
    02563
    02564       case WPT_RESIZE: {
    02565         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02566         if (nwrb != NULL) {
    02567           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02568           nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
    02569         }
    02570         break;
    02571       }
    02572
    02573       case WPT_MINSIZE: {
    02574         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02575         if (nwrb != NULL) {
    02576           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02577           nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
    02578         }
    02579         break;
    02580       }
    02581
    02582       case WPT_MINTEXTLINES: {
    02583         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02584         if (nwrb != NULL) {
    02585           assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
    02586           nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
    02587         }
    02588         break;
    02589       }
    02590
    02591       case WPT_FILL: {
    02592         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02593         if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
    02594         break;
    02595       }
    02596
    02597       case WPT_DATATIP: {
    02598         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02599         if (nwc != NULL) {
    02600           nwc->widget_data = parts->u.data_tip.data;
    02601           nwc->tool_tip = parts->u.data_tip.tooltip;
    02602         }
    02603         break;
    02604       }
    02605
    02606       case WPT_PADDING:
    02607         if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
    02608         break;
    02609
    02610       case WPT_PIPSPACE: {
    02611         NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
    02612         if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02613
    02614         NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
    02615         if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02616         break;
    02617       }
    02618
    02619       case WPT_SCROLLBAR: {
    02620         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02621         if (nwc != NULL) {
    02622           nwc->scrollbar_index = parts->u.widget.index;
    02623         }
    02624         break;
    02625       }
    02626
    02627       case WPT_ENDCONTAINER:
    02628         return num_used;
    02629
    02630       case NWID_VIEWPORT:
    02631         if (*dest != NULL) return num_used;
    02632         *dest = new NWidgetViewport(parts->u.widget.index);
    02633         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02634         break;
    02635
    02636       case NWID_HSCROLLBAR:
    02637       case NWID_VSCROLLBAR:
    02638         if (*dest != NULL) return num_used;
    02639         *dest = new NWidgetScrollbar(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02640         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02641         break;
    02642
    02643       case NWID_SELECTION: {
    02644         if (*dest != NULL) return num_used;
    02645         NWidgetStacked *nws = new NWidgetStacked();
    02646         *dest = nws;
    02647         *fill_dest = true;
    02648         nws->SetIndex(parts->u.widget.index);
    02649         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02650         break;
    02651       }
    02652
    02653       default:
    02654         if (*dest != NULL) return num_used;
    02655         assert((parts->type & WWT_MASK) < WWT_LAST || (parts->type & WWT_MASK) == NWID_BUTTON_DROPDOWN);
    02656         *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
    02657         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02658         break;
    02659     }
    02660     num_used++;
    02661     parts++;
    02662   }
    02663
    02664   return num_used;
    02665 }
    02666
    02676 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
    02677 {
    02678   /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
    02679    * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
    02680   NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
    02681   NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
    02682   assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
    02683
    02684   int total_used = 0;
    02685   for (;;) {
    02686     NWidgetBase *sub_widget = NULL;
    02687     bool fill_sub = false;
    02688     int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
    02689     parts += num_used;
    02690     total_used += num_used;
    02691
    02692     /* Break out of loop when end reached */
    02693     if (sub_widget == NULL) break;
    02694
    02695     /* If sub-widget is a container, recursively fill that container. */
    02696     WidgetType tp = sub_widget->type;
    02697     if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
    02698               || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
    02699       NWidgetBase *sub_ptr = sub_widget;
    02700       int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
    02701       parts += num_used;
    02702       total_used += num_used;
    02703     }
    02704
    02705     /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
    02706     if (nwid_cont != NULL) nwid_cont->Add(sub_widget);
    02707     if (nwid_parent != NULL) nwid_parent->Add(sub_widget);
    02708     if (nwid_cont == NULL && nwid_parent == NULL) {
    02709       *parent = sub_widget;
    02710       return total_used;
    02711     }
    02712   }
    02713
    02714   if (count == total_used) return total_used; // Reached the end of the array of parts?
    02715
    02716   assert(total_used < count);
    02717   assert(parts->type == WPT_ENDCONTAINER);
    02718   return total_used + 1; // *parts is also 'used'
    02719 }
    02720
    02732 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
    02733 {
    02734   *biggest_index = -1;
    02735   if (container == NULL) container = new NWidgetVertical();
    02736   NWidgetBase *cont_ptr = container;
    02737   MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
    02738   return container;
    02739 }
    02740
    02754 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
    02755 {
    02756   *biggest_index = -1;
    02757
    02758   /* Read the first widget recursively from the array. */
    02759   NWidgetBase *nwid = NULL;
    02760   int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
    02761   assert(nwid != NULL);
    02762   parts += num_used;
    02763   count -= num_used;
    02764
    02765   NWidgetContainer *root = new NWidgetVertical;
    02766   root->Add(nwid);
    02767   if (count == 0) { // There is no body at all.
    02768     *shade_select = NULL;
    02769     return root;
    02770   }
    02771
    02772   /* If the first widget looks like a titlebar, treat it as such.
    02773    * If it has a shading box, silently add a shade selection widget in the tree. */
    02774   NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
    02775   NWidgetContainer *body;
    02776   if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
    02777     *shade_select = new NWidgetStacked;
    02778     root->Add(*shade_select);
    02779     body = new NWidgetVertical;
    02780     (*shade_select)->Add(body);
    02781   } else {
    02782     *shade_select = NULL;
    02783     body = root;
    02784   }
    02785
    02786   /* Load the remaining parts into 'body'. */
    02787   int biggest2 = -1;
    02788   MakeNWidgets(parts, count, &biggest2, body);
    02789
    02790   *biggest_index = max(*biggest_index, biggest2);
    02791   return root;
    02792 }
    02793
    02804 NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
    02805 {
    02806   NWidgetVertical *vert = NULL; // Storage for all rows.
    02807   NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
    02808   int hor_length = 0;
    02809
    02810   Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
    02811   sprite_size.width  += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
    02812   sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
    02813
    02814   for (int widnum = widget_first; widnum <= widget_last; widnum++) {
    02815     /* Ensure there is room in 'hor' for another button. */
    02816     if (hor_length == max_length) {
    02817       if (vert == NULL) vert = new NWidgetVertical();
    02818       vert->Add(hor);
    02819       hor = NULL;
    02820       hor_length = 0;
    02821     }
    02822     if (hor == NULL) {
    02823       hor = new NWidgetHorizontal();
    02824       hor_length = 0;
    02825     }
    02826
    02827     NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
    02828     panel->SetMinimalSize(sprite_size.width, sprite_size.height);
    02829     panel->SetFill(1, 0);
    02830     panel->SetResize(1, 0);
    02831     panel->SetDataTip(0x0, button_tooltip);
    02832     hor->Add(panel);
    02833     hor_length++;
    02834   }
    02835   *biggest_index = widget_last;
    02836   if (vert == NULL) return hor; // All buttons fit in a single row.
    02837
    02838   if (hor_length > 0 && hor_length < max_length) {
    02839     /* Last row is partial, add a spacer at the end to force all buttons to the left. */
    02840     NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
    02841     spc->SetFill(1, 0);
    02842     spc->SetResize(1, 0);
    02843     hor->Add(spc);
    02844   }
    02845   if (hor != NULL) vert->Add(hor);
    02846   return vert;
    02847 }