# 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 DrawDebugBox(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_DEBUG, PAL_NONE, r.left + WD_DEBUGBOX_LEFT + clicked, r.top + WD_DEBUGBOX_TOP + clicked);
    00473 }
    00474
    00482 static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
    00483 {
    00484   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00485   if (at_left) {
    00486     DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked,
    00487          r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_LEFT).height + clicked);
    00488   } else {
    00489     DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.left + WD_RESIZEBOX_LEFT + clicked,
    00490          r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT).height + clicked);
    00491   }
    00492 }
    00493
    00500 static inline void DrawCloseBox(const Rect &r, Colours colour, StringID str)
    00501 {
    00502   assert(str == STR_BLACK_CROSS || str == STR_SILVER_CROSS); // black or silver cross
    00503   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE);
    00504   DrawString(r.left + WD_CLOSEBOX_LEFT, r.right - WD_CLOSEBOX_RIGHT, r.top + WD_CLOSEBOX_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
    00505 }
    00506
    00514 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
    00515 {
    00516   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_BORDERONLY);
    00517   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);
    00518
    00519   if (owner != INVALID_OWNER) {
    00520     GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]);
    00521   }
    00522
    00523   if (str != STR_NULL) {
    00524     Dimension d = GetStringBoundingBox(str);
    00525     int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00526     DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + offset, str, TC_FROMSTRING, SA_HOR_CENTER);
    00527   }
    00528 }
    00529
    00540 static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
    00541 {
    00542   int text_offset = max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered
    00543
    00544   if (_current_text_dir == TD_LTR) {
    00545     DrawFrameRect(r.left, r.top, r.right - 12, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00546     DrawFrameRect(r.right - 11, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00547     DrawString(r.right - (clicked_dropdown ? 10 : 11), r.right, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00548     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);
    00549   } else {
    00550     DrawFrameRect(r.left + 12, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00551     DrawFrameRect(r.left, r.top, r.left + 11, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00552     DrawString(r.left + clicked_dropdown, r.left + 11, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00553     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);
    00554   }
    00555 }
    00556
    00564 static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
    00565 {
    00566   DrawButtonDropdown(r, colour, false, clicked, str);
    00567 }
    00568
    00572 void Window::DrawWidgets() const
    00573 {
    00574   this->nested_root->Draw(this);
    00575
    00576   if (this->flags & WF_WHITE_BORDER) {
    00577     DrawFrameRect(0, 0, this->width - 1, this->height - 1, COLOUR_WHITE, FR_BORDERONLY);
    00578   }
    00579
    00580   if (this->flags & WF_HIGHLIGHTED) {
    00581     extern bool _window_highlight_colour;
    00582     for (uint i = 0; i < this->nested_array_size; i++) {
    00583       const NWidgetBase *widget = this->GetWidget<NWidgetBase>(i);
    00584       if (widget == NULL || !widget->IsHighlighted()) continue;
    00585
    00586       int left = widget->pos_x;
    00587       int top  = widget->pos_y;
    00588       int right  = left + widget->current_x - 1;
    00589       int bottom = top  + widget->current_y - 1;
    00590
    00591       int colour = _string_colourmap[_window_highlight_colour ? widget->GetHighlightColour() : TC_WHITE];
    00592
    00593       GfxFillRect(left,                 top,    left,                   bottom - WD_BEVEL_BOTTOM, colour);
    00594       GfxFillRect(left + WD_BEVEL_LEFT, top,    right - WD_BEVEL_RIGHT, top,                      colour);
    00595       GfxFillRect(right,                top,    right,                  bottom - WD_BEVEL_BOTTOM, colour);
    00596       GfxFillRect(left,                 bottom, right,                  bottom,                   colour);
    00597     }
    00598   }
    00599 }
    00600
    00606 void Window::DrawSortButtonState(int widget, SortButtonState state) const
    00607 {
    00608   if (state == SBS_OFF) return;
    00609
    00610   assert(this->nested_array != NULL);
    00611   const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
    00612
    00613   int offset = this->IsWidgetLowered(widget) ? 1 : 0;
    00614   int base = offset + nwid->pos_x + (_current_text_dir == TD_LTR ? nwid->current_x - WD_SORTBUTTON_ARROW_WIDTH : 0);
    00615   int top = nwid->pos_y;
    00616
    00617   DrawString(base, base + WD_SORTBUTTON_ARROW_WIDTH, top + 1 + offset, state == SBS_DOWN ? DOWNARROW : UPARROW, TC_BLACK, SA_HOR_CENTER);
    00618 }
    00619
    00620
    00678 NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator()
    00679 {
    00680   this->type = tp;
    00681 }
    00682
    00683 /* ~NWidgetContainer() takes care of #next and #prev data members. */
    00684
    00732 void NWidgetBase::SetDirty(const Window *w) const
    00733 {
    00734   int abs_left = w->left + this->pos_x;
    00735   int abs_top = w->top + this->pos_y;
    00736   SetDirtyBlocks(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
    00737 }
    00738
    00752 NWidgetBase *NWidgetBase::GetWidgetOfType(WidgetType tp)
    00753 {
    00754   return (this->type == tp) ? this : NULL;
    00755 }
    00756
    00763 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y) : NWidgetBase(tp)
    00764 {
    00765   this->fill_x = fill_x;
    00766   this->fill_y = fill_y;
    00767 }
    00768
    00774 void NWidgetResizeBase::SetMinimalSize(uint min_x, uint min_y)
    00775 {
    00776   this->min_x = min_x;
    00777   this->min_y = min_y;
    00778 }
    00779
    00786 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
    00787 {
    00788   this->min_y = min_lines * GetCharacterHeight(size) + spacing;
    00789 }
    00790
    00796 void NWidgetResizeBase::SetFill(uint fill_x, uint fill_y)
    00797 {
    00798   this->fill_x = fill_x;
    00799   this->fill_y = fill_y;
    00800 }
    00801
    00807 void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y)
    00808 {
    00809   this->resize_x = resize_x;
    00810   this->resize_y = resize_y;
    00811 }
    00812
    00813 void NWidgetResizeBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00814 {
    00815   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    00816 }
    00817
    00827 NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint32 widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y)
    00828 {
    00829   this->colour = colour;
    00830   this->index = -1;
    00831   this->widget_data = widget_data;
    00832   this->tool_tip = tool_tip;
    00833   this->scrollbar_index = -1;
    00834 }
    00835
    00840 void NWidgetCore::SetIndex(int index)
    00841 {
    00842   assert(index >= 0);
    00843   this->index = index;
    00844 }
    00845
    00851 void NWidgetCore::SetDataTip(uint32 widget_data, StringID tool_tip)
    00852 {
    00853   this->widget_data = widget_data;
    00854   this->tool_tip = tool_tip;
    00855 }
    00856
    00857 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
    00858 {
    00859   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    00860 }
    00861
    00862 NWidgetCore *NWidgetCore::GetWidgetFromPos(int x, int y)
    00863 {
    00864   return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : NULL;
    00865 }
    00866
    00871 NWidgetContainer::NWidgetContainer(WidgetType tp) : NWidgetBase(tp)
    00872 {
    00873   this->head = NULL;
    00874   this->tail = NULL;
    00875 }
    00876
    00877 NWidgetContainer::~NWidgetContainer()
    00878 {
    00879   while (this->head != NULL) {
    00880     NWidgetBase *wid = this->head->next;
    00881     delete this->head;
    00882     this->head = wid;
    00883   }
    00884   this->tail = NULL;
    00885 }
    00886
    00887 NWidgetBase *NWidgetContainer::GetWidgetOfType(WidgetType tp)
    00888 {
    00889   if (this->type == tp) return this;
    00890   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00891     NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
    00892     if (nwid != NULL) return nwid;
    00893   }
    00894   return NULL;
    00895 }
    00896
    00901 void NWidgetContainer::Add(NWidgetBase *wid)
    00902 {
    00903   assert(wid->next == NULL && wid->prev == NULL);
    00904
    00905   if (this->head == NULL) {
    00906     this->head = wid;
    00907     this->tail = wid;
    00908   } else {
    00909     assert(this->tail != NULL);
    00910     assert(this->tail->next == NULL);
    00911
    00912     this->tail->next = wid;
    00913     wid->prev = this->tail;
    00914     this->tail = wid;
    00915   }
    00916 }
    00917
    00918 void NWidgetContainer::FillNestedArray(NWidgetBase **array, uint length)
    00919 {
    00920   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00921     child_wid->FillNestedArray(array, length);
    00922   }
    00923 }
    00924
    00928 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION)
    00929 {
    00930   this->index = -1;
    00931 }
    00932
    00933 void NWidgetStacked::SetIndex(int index)
    00934 {
    00935   this->index = index;
    00936 }
    00937
    00938 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
    00939 {
    00940   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    00941     assert(w->nested_array_size > (uint)this->index);
    00942     w->nested_array[this->index] = this;
    00943   }
    00944
    00945   /* Zero size plane selected */
    00946   if (this->shown_plane >= SZSP_BEGIN) {
    00947     Dimension size    = {0, 0};
    00948     Dimension padding = {0, 0};
    00949     Dimension fill    = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00950     Dimension resize  = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00951     /* Here we're primarily interested in the value of resize */
    00952     if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    00953
    00954     this->smallest_x = size.width;
    00955     this->smallest_y = size.height;
    00956     this->fill_x = fill.width;
    00957     this->fill_y = fill.height;
    00958     this->resize_x = resize.width;
    00959     this->resize_y = resize.height;
    00960     return;
    00961   }
    00962
    00963   /* First sweep, recurse down and compute minimal size and filling. */
    00964   this->smallest_x = 0;
    00965   this->smallest_y = 0;
    00966   this->fill_x = (this->head != NULL) ? 1 : 0;
    00967   this->fill_y = (this->head != NULL) ? 1 : 0;
    00968   this->resize_x = (this->head != NULL) ? 1 : 0;
    00969   this->resize_y = (this->head != NULL) ? 1 : 0;
    00970   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00971     child_wid->SetupSmallestSize(w, init_array);
    00972
    00973     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    00974     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    00975     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    00976     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    00977     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    00978     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    00979   }
    00980 }
    00981
    00982 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00983 {
    00984   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    00985   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    00986
    00987   if (this->shown_plane >= SZSP_BEGIN) return;
    00988
    00989   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00990     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    00991     uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    00992     uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
    00993
    00994     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    00995     uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    00996     uint child_pos_y = child_wid->padding_top;
    00997
    00998     child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
    00999   }
    01000 }
    01001
    01002 void NWidgetStacked::FillNestedArray(NWidgetBase **array, uint length)
    01003 {
    01004   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01005   NWidgetContainer::FillNestedArray(array, length);
    01006 }
    01007
    01008 void NWidgetStacked::Draw(const Window *w)
    01009 {
    01010   if (this->shown_plane >= SZSP_BEGIN) return;
    01011
    01012   int plane = 0;
    01013   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01014     if (plane == this->shown_plane) {
    01015       child_wid->Draw(w);
    01016       return;
    01017     }
    01018   }
    01019
    01020   NOT_REACHED();
    01021 }
    01022
    01023 NWidgetCore *NWidgetStacked::GetWidgetFromPos(int x, int y)
    01024 {
    01025   if (this->shown_plane >= SZSP_BEGIN) return NULL;
    01026
    01027   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01028   int plane = 0;
    01029   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01030     if (plane == this->shown_plane) {
    01031       return child_wid->GetWidgetFromPos(x, y);
    01032     }
    01033   }
    01034   return NULL;
    01035 }
    01036
    01041 void NWidgetStacked::SetDisplayedPlane(int plane)
    01042 {
    01043   this->shown_plane = plane;
    01044 }
    01045
    01046 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
    01047 {
    01048   this->flags = flags;
    01049 }
    01050
    01060 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01061 {
    01062   this->pip_pre = pip_pre;
    01063   this->pip_inter = pip_inter;
    01064   this->pip_post = pip_post;
    01065 }
    01066
    01067 void NWidgetPIPContainer::Draw(const Window *w)
    01068 {
    01069   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01070     child_wid->Draw(w);
    01071   }
    01072 }
    01073
    01074 NWidgetCore *NWidgetPIPContainer::GetWidgetFromPos(int x, int y)
    01075 {
    01076   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01077
    01078   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01079     NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
    01080     if (nwid != NULL) return nwid;
    01081   }
    01082   return NULL;
    01083 }
    01084
    01086 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
    01087 {
    01088 }
    01089
    01090 void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array)
    01091 {
    01092   this->smallest_x = 0; // Sum of minimal size of all children.
    01093   this->smallest_y = 0; // Biggest child.
    01094   this->fill_x = 0;     // smallest non-zero child widget fill step.
    01095   this->fill_y = 1;     // smallest common child fill step.
    01096   this->resize_x = 0;   // smallest non-zero child widget resize step.
    01097   this->resize_y = 1;   // smallest common child resize step.
    01098
    01099   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01100   uint longest = 0; // Longest child found.
    01101   uint max_vert_fill = 0; // Biggest vertical fill step.
    01102   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01103     child_wid->SetupSmallestSize(w, init_array);
    01104     longest = max(longest, child_wid->smallest_x);
    01105     max_vert_fill = max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
    01106     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    01107   }
    01108   /* 1b. Make the container higher if needed to accommodate all children nicely. */
    01109   uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height.
    01110   uint cur_height = this->smallest_y;
    01111   for (;;) {
    01112     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01113       uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
    01114       uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01115       if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting children are not interesting.
    01116         uint remainder = (cur_height - child_height) % step_size;
    01117         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01118           cur_height += step_size - remainder;
    01119           assert(cur_height < max_smallest); // Safeguard against infinite height expansion.
    01120           /* Remaining children will adapt to the new cur_height, thus speeding up the computation. */
    01121         }
    01122       }
    01123     }
    01124     if (this->smallest_y == cur_height) break;
    01125     this->smallest_y = cur_height; // Smallest height got changed, try again.
    01126   }
    01127   /* 2. For containers that must maintain equal width, extend child minimal size. */
    01128   if (this->flags & NC_EQUALSIZE) {
    01129     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01130       if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
    01131     }
    01132   }
    01133   /* 3. Move PIP space to the children, compute smallest, fill, and resize values of the container. */
    01134   if (this->head != NULL) this->head->padding_left += this->pip_pre;
    01135   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01136     if (child_wid->next != NULL) {
    01137       child_wid->padding_right += this->pip_inter;
    01138     } else {
    01139       child_wid->padding_right += this->pip_post;
    01140     }
    01141
    01142     this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01143     if (child_wid->fill_x > 0) {
    01144       if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
    01145     }
    01146     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    01147
    01148     if (child_wid->resize_x > 0) {
    01149       if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
    01150     }
    01151     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    01152   }
    01153   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01154   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01155 }
    01156
    01157 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01158 {
    01159   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01160
    01161   /* Compute additional width given to us. */
    01162   uint additional_length = given_width;
    01163   if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
    01164     /* For EQUALSIZE containers this does not sum to smallest_x during initialisation */
    01165     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01166       additional_length -= child_wid->smallest_x + child_wid->padding_right + child_wid->padding_left;
    01167     }
    01168   } else {
    01169     additional_length -= this->smallest_x;
    01170   }
    01171
    01172   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01173
    01174   /* In principle, the additional horizontal space is distributed evenly over the available resizable children. Due to step sizes, this may not always be feasible.
    01175    * To make resizing work as good as possible, first children with biggest step sizes are done. These may get less due to rounding down.
    01176    * 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
    01177    * of the child with the smallest non-zero stepsize.
    01178    *
    01179    * 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
    01180    * size and position, and directly call child->AssignSizePosition() with the computed values.
    01181    * 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
    01182    * then we call the child.
    01183    */
    01184
    01185   /* First loop: Find biggest stepsize, find number of children that want a piece of the pie, handle vertical size for all children,
    01186    * handle horizontal size for non-resizing children.
    01187    */
    01188   int num_changing_childs = 0; // Number of children that can change size.
    01189   uint biggest_stepsize = 0;
    01190   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01191     uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01192     if (hor_step > 0) {
    01193       num_changing_childs++;
    01194       biggest_stepsize = max(biggest_stepsize, hor_step);
    01195     } else {
    01196       child_wid->current_x = child_wid->smallest_x;
    01197     }
    01198
    01199     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    01200     child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    01201   }
    01202
    01203   /* Second loop: Allocate the additional horizontal space over the resizing children, starting with the biggest resize steps. */
    01204   while (biggest_stepsize > 0) {
    01205     uint next_biggest_stepsize = 0;
    01206     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01207       uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01208       if (hor_step > biggest_stepsize) continue; // Already done
    01209       if (hor_step == biggest_stepsize) {
    01210         uint increment = additional_length / num_changing_childs;
    01211         num_changing_childs--;
    01212         if (hor_step > 1) increment -= increment % hor_step;
    01213         child_wid->current_x = child_wid->smallest_x + increment;
    01214         additional_length -= increment;
    01215         continue;
    01216       }
    01217       next_biggest_stepsize = max(next_biggest_stepsize, hor_step);
    01218     }
    01219     biggest_stepsize = next_biggest_stepsize;
    01220   }
    01221   assert(num_changing_childs == 0);
    01222
    01223   /* Third loop: Compute position and call the child. */
    01224   uint position = 0; // Place to put next child relative to origin of the container.
    01225   NWidgetBase *child_wid = rtl ? this->tail : this->head;
    01226   while (child_wid != NULL) {
    01227     uint child_width = child_wid->current_x;
    01228     uint child_x = x + position + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01229     uint child_y = y + child_wid->padding_top;
    01230
    01231     child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
    01232     position += child_width + child_wid->padding_right + child_wid->padding_left;
    01233
    01234     child_wid = rtl ? child_wid->prev : child_wid->next;
    01235   }
    01236 }
    01237
    01239 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
    01240 {
    01241   this->type = NWID_HORIZONTAL_LTR;
    01242 }
    01243
    01244 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01245 {
    01246   NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
    01247 }
    01248
    01250 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
    01251 {
    01252 }
    01253
    01254 void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array)
    01255 {
    01256   this->smallest_x = 0; // Biggest child.
    01257   this->smallest_y = 0; // Sum of minimal size of all children.
    01258   this->fill_x = 1;     // smallest common child fill step.
    01259   this->fill_y = 0;     // smallest non-zero child widget fill step.
    01260   this->resize_x = 1;   // smallest common child resize step.
    01261   this->resize_y = 0;   // smallest non-zero child widget resize step.
    01262
    01263   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01264   uint highest = 0; // Highest child found.
    01265   uint max_hor_fill = 0; // Biggest horizontal fill step.
    01266   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01267     child_wid->SetupSmallestSize(w, init_array);
    01268     highest = max(highest, child_wid->smallest_y);
    01269     max_hor_fill = max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
    01270     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    01271   }
    01272   /* 1b. Make the container wider if needed to accommodate all children nicely. */
    01273   uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height.
    01274   uint cur_width = this->smallest_x;
    01275   for (;;) {
    01276     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01277       uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
    01278       uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01279       if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting children are not interesting.
    01280         uint remainder = (cur_width - child_width) % step_size;
    01281         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01282           cur_width += step_size - remainder;
    01283           assert(cur_width < max_smallest); // Safeguard against infinite width expansion.
    01284           /* Remaining children will adapt to the new cur_width, thus speeding up the computation. */
    01285         }
    01286       }
    01287     }
    01288     if (this->smallest_x == cur_width) break;
    01289     this->smallest_x = cur_width; // Smallest width got changed, try again.
    01290   }
    01291   /* 2. For containers that must maintain equal width, extend children minimal size. */
    01292   if (this->flags & NC_EQUALSIZE) {
    01293     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01294       if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
    01295     }
    01296   }
    01297   /* 3. Move PIP space to the child, compute smallest, fill, and resize values of the container. */
    01298   if (this->head != NULL) this->head->padding_top += this->pip_pre;
    01299   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01300     if (child_wid->next != NULL) {
    01301       child_wid->padding_bottom += this->pip_inter;
    01302     } else {
    01303       child_wid->padding_bottom += this->pip_post;
    01304     }
    01305
    01306     this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01307     if (child_wid->fill_y > 0) {
    01308       if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
    01309     }
    01310     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    01311
    01312     if (child_wid->resize_y > 0) {
    01313       if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
    01314     }
    01315     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    01316   }
    01317   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01318   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01319 }
    01320
    01321 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01322 {
    01323   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01324
    01325   /* Compute additional height given to us. */
    01326   uint additional_length = given_height;
    01327   if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
    01328     /* For EQUALSIZE containers this does not sum to smallest_y during initialisation */
    01329     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01330       additional_length -= child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01331     }
    01332   } else {
    01333     additional_length -= this->smallest_y;
    01334   }
    01335
    01336   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01337
    01338   /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the children with the biggest resize steps.
    01339    * It also stores computed widths and heights into current_x and current_y values of the child.
    01340    */
    01341
    01342   /* 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. */
    01343   int num_changing_childs = 0; // Number of children that can change size.
    01344   uint biggest_stepsize = 0;
    01345   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01346     uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01347     if (vert_step > 0) {
    01348       num_changing_childs++;
    01349       biggest_stepsize = max(biggest_stepsize, vert_step);
    01350     } else {
    01351       child_wid->current_y = child_wid->smallest_y;
    01352     }
    01353
    01354     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    01355     child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    01356   }
    01357
    01358   /* Second loop: Allocate the additional vertical space over the resizing children, starting with the biggest resize steps. */
    01359   while (biggest_stepsize > 0) {
    01360     uint next_biggest_stepsize = 0;
    01361     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01362       uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01363       if (vert_step > biggest_stepsize) continue; // Already done
    01364       if (vert_step == biggest_stepsize) {
    01365         uint increment = additional_length / num_changing_childs;
    01366         num_changing_childs--;
    01367         if (vert_step > 1) increment -= increment % vert_step;
    01368         child_wid->current_y = child_wid->smallest_y + increment;
    01369         additional_length -= increment;
    01370         continue;
    01371       }
    01372       next_biggest_stepsize = max(next_biggest_stepsize, vert_step);
    01373     }
    01374     biggest_stepsize = next_biggest_stepsize;
    01375   }
    01376   assert(num_changing_childs == 0);
    01377
    01378   /* Third loop: Compute position and call the child. */
    01379   uint position = 0; // Place to put next child relative to origin of the container.
    01380   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01381     uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01382     uint child_height = child_wid->current_y;
    01383
    01384     child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
    01385     position += child_height + child_wid->padding_top + child_wid->padding_bottom;
    01386   }
    01387 }
    01388
    01394 NWidgetSpacer::NWidgetSpacer(int length, int height) : NWidgetResizeBase(NWID_SPACER, 0, 0)
    01395 {
    01396   this->SetMinimalSize(length, height);
    01397   this->SetResize(0, 0);
    01398 }
    01399
    01400 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
    01401 {
    01402   this->smallest_x = this->min_x;
    01403   this->smallest_y = this->min_y;
    01404 }
    01405
    01406 void NWidgetSpacer::FillNestedArray(NWidgetBase **array, uint length)
    01407 {
    01408 }
    01409
    01410 void NWidgetSpacer::Draw(const Window *w)
    01411 {
    01412   /* Spacer widget is never visible. */
    01413 }
    01414
    01415 void NWidgetSpacer::SetDirty(const Window *w) const
    01416 {
    01417   /* Spacer widget never need repainting. */
    01418 }
    01419
    01420 NWidgetCore *NWidgetSpacer::GetWidgetFromPos(int x, int y)
    01421 {
    01422   return NULL;
    01423 }
    01424
    01425 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX, NC_EQUALSIZE), index(-1), clicked(-1), count(-1)
    01426 {
    01427 }
    01428
    01429 void NWidgetMatrix::SetIndex(int index)
    01430 {
    01431   this->index = index;
    01432 }
    01433
    01434 void NWidgetMatrix::SetColour(Colours colour)
    01435 {
    01436   this->colour = colour;
    01437 }
    01438
    01443 void NWidgetMatrix::SetClicked(int clicked)
    01444 {
    01445   this->clicked = clicked;
    01446   if (this->clicked >= 0 && this->sb != NULL && this->widgets_x != 0) {
    01447     int vpos = (this->clicked / this->widgets_x) * this->widget_h; // Vertical position of the top.
    01448     /* Need to scroll down -> Scroll to the bottom.
    01449      * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
    01450     if (this->sb->GetPosition() < vpos) vpos += this->widget_h - this->pip_inter - 1;
    01451     this->sb->ScrollTowards(vpos);
    01452   }
    01453 }
    01454
    01460 void NWidgetMatrix::SetCount(int count)
    01461 {
    01462   this->count = count;
    01463
    01464   if (this->sb == NULL || this->widgets_x == 0) return;
    01465
    01466   /* We need to get the number of pixels the matrix is high/wide.
    01467    * So, determine the number of rows/columns based on the number of
    01468    * columns/rows (one is constant/unscrollable).
    01469    * Then multiply that by the height of a widget, and add the pre
    01470    * and post spacing "offsets". */
    01471   count = CeilDiv(count, this->sb->IsVertical() ? this->widgets_x : this->widgets_y);
    01472   count *= (this->sb->IsVertical() ? this->head->smallest_y : this->head->smallest_x) + this->pip_inter;
    01473   if (count > 0) count -= this->pip_inter; // We counted an inter too much in the multiplication above
    01474   count += this->pip_pre + this->pip_post;
    01475   this->sb->SetCount(count);
    01476   this->sb->SetCapacity(this->sb->IsVertical() ? this->current_y : this->current_x);
    01477   this->sb->SetStepSize(this->sb->IsVertical() ? this->widget_h  : this->widget_w);
    01478 }
    01479
    01484 void NWidgetMatrix::SetScrollbar(Scrollbar *sb)
    01485 {
    01486   this->sb = sb;
    01487 }
    01488
    01489 void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array)
    01490 {
    01491   assert(this->head != NULL);
    01492   assert(this->head->next == NULL);
    01493
    01494   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    01495     assert(w->nested_array_size > (uint)this->index);
    01496     w->nested_array[this->index] = this;
    01497   }
    01498
    01499   /* Reset the widget number. */
    01500   SB(dynamic_cast<NWidgetCore *>(this->head)->index, 16, 16, 0);
    01501   this->head->SetupSmallestSize(w, init_array);
    01502
    01503   Dimension padding = {this->pip_pre + this->pip_post, this->pip_pre + this->pip_post};
    01504   Dimension size    = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height};
    01505   Dimension fill    = {0, 0};
    01506   Dimension resize  = {this->pip_inter + this->head->smallest_x, this->pip_inter + this->head->smallest_y};
    01507
    01508   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    01509
    01510   this->smallest_x = size.width;
    01511   this->smallest_y = size.height;
    01512   this->fill_x = fill.width;
    01513   this->fill_y = fill.height;
    01514   this->resize_x = resize.width;
    01515   this->resize_y = resize.height;
    01516 }
    01517
    01518 void NWidgetMatrix::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01519 {
    01520   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01521
    01522   this->pos_x = x;
    01523   this->pos_y = y;
    01524   this->current_x = given_width;
    01525   this->current_y = given_height;
    01526
    01527   /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
    01528   this->widget_w = this->head->smallest_x + this->pip_inter;
    01529   this->widget_h = this->head->smallest_y + this->pip_inter;
    01530
    01531   /* Account for the pip_inter is between widgets, so we need to account for that when
    01532    * the division assumes pip_inter is used for all widgets. */
    01533   this->widgets_x = CeilDiv(this->current_x - this->pip_pre - this->pip_post + this->pip_inter, this->widget_w);
    01534   this->widgets_y = CeilDiv(this->current_y - this->pip_pre - this->pip_post + this->pip_inter, this->widget_h);
    01535
    01536   /* When resizing, update the scrollbar's count. E.g. with a vertical
    01537    * scrollbar becoming wider or narrower means the amount of rows in
    01538    * the scrollbar becomes respectively smaller or higher. */
    01539   this->SetCount(this->count);
    01540 }
    01541
    01542 void NWidgetMatrix::FillNestedArray(NWidgetBase **array, uint length)
    01543 {
    01544   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01545   NWidgetContainer::FillNestedArray(array, length);
    01546 }
    01547
    01548 NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y)
    01549 {
    01550   /* Falls outside of the matrix widget. */
    01551   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01552
    01553   int start_x, start_y, base_offs_x, base_offs_y;
    01554   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01555
    01556   /* Swap the x offset around for RTL, so it'll behave like LTR for RTL as well. */
    01557   bool rtl = _current_text_dir == TD_RTL;
    01558   if (rtl) base_offs_x -= (this->widgets_x - 1) * this->widget_w;
    01559
    01560   int widget_col = (x - base_offs_x - (int)this->pip_pre - (int)this->pos_x) / this->widget_w;
    01561   int widget_row = (y - base_offs_y - (int)this->pip_pre - (int)this->pos_y) / this->widget_h;
    01562
    01563   if (widget_row * this->widgets_x + widget_col >= this->count) return NULL;
    01564
    01565   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01566   child->AssignSizePosition(ST_RESIZE,
    01567       this->pos_x + this->pip_pre + widget_col * this->widget_w + base_offs_x,
    01568       this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y,
    01569       child->smallest_x, child->smallest_y, rtl);
    01570
    01571   /* "Undo" the RTL swap here to get the right widget index. */
    01572   if (rtl) widget_col = this->widgets_x - widget_col - 1;
    01573   SB(child->index, 16, 16, (widget_row + start_y) * this->widgets_x + widget_col + start_x);
    01574
    01575   return child->GetWidgetFromPos(x, y);
    01576 }
    01577
    01578 /* virtual */ void NWidgetMatrix::Draw(const Window *w)
    01579 {
    01580   /* Fill the background. */
    01581   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]);
    01582
    01583   /* Set up a clipping area for the previews. */
    01584   DrawPixelInfo tmp_dpi;
    01585   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;
    01586   DrawPixelInfo *old_dpi = _cur_dpi;
    01587   _cur_dpi = &tmp_dpi;
    01588
    01589   /* Get the appropriate offsets so we can draw the right widgets. */
    01590   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01591   bool rtl = _current_text_dir == TD_RTL;
    01592   int start_x, start_y, base_offs_x, base_offs_y;
    01593   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01594
    01595   int offs_y = base_offs_y;
    01596   for (int y = start_y; y < start_y + this->widgets_y + 1; y++, offs_y += this->widget_h) {
    01597     /* Are we within bounds? */
    01598     if (offs_y + child->smallest_y <= 0) continue;
    01599     if (offs_y >= (int)this->current_y) break;
    01600
    01601     /* We've passed our amount of widgets. */
    01602     if (y * this->widgets_x >= this->count) break;
    01603
    01604     int offs_x = base_offs_x;
    01605     for (int x = start_x; x < start_x + this->widgets_x + 1; x++, offs_x += rtl ? -this->widget_w : this->widget_w) {
    01606       /* Are we within bounds? */
    01607       if (offs_x + child->smallest_x <= 0) continue;
    01608       if (offs_x >= (int)this->current_x) continue;
    01609
    01610       /* Do we have this many widgets? */
    01611       int sub_wid = y * this->widgets_x + x;
    01612       if (sub_wid >= this->count) break;
    01613
    01614       child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl);
    01615       child->SetLowered(this->clicked == sub_wid);
    01616       SB(child->index, 16, 16, sub_wid);
    01617       child->Draw(w);
    01618     }
    01619   }
    01620
    01621   /* Restore the clipping area. */
    01622   _cur_dpi = old_dpi;
    01623 }
    01624
    01632 void NWidgetMatrix::GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
    01633 {
    01634   base_offs_x = 0;
    01635   base_offs_y = 0;
    01636   start_x = 0;
    01637   start_y = 0;
    01638   if (this->sb != NULL) {
    01639     if (this->sb->IsVertical()) {
    01640       start_y = this->sb->GetPosition() / this->widget_h;
    01641       base_offs_y = -this->sb->GetPosition() + start_y * this->widget_h;
    01642       if (_current_text_dir == TD_RTL) base_offs_x = this->pip_pre + this->widget_w * (this->widgets_x - 1) - this->pip_inter;
    01643     } else {
    01644       start_x = this->sb->GetPosition() / this->widget_w;
    01645       if (_current_text_dir == TD_RTL) {
    01646         base_offs_x = this->sb->GetCapacity() + this->sb->GetPosition() - (start_x + 1) * this->widget_w + this->pip_inter - this->pip_post - this->pip_pre;
    01647       } else {
    01648         base_offs_x = -this->sb->GetPosition() + start_x * this->widget_w;
    01649       }
    01650     }
    01651   }
    01652 }
    01653
    01663 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
    01664 {
    01665   assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
    01666   if (index >= 0) this->SetIndex(index);
    01667   this->child = child;
    01668 }
    01669
    01670 NWidgetBackground::~NWidgetBackground()
    01671 {
    01672   if (this->child != NULL) delete this->child;
    01673 }
    01674
    01682 void NWidgetBackground::Add(NWidgetBase *nwid)
    01683 {
    01684   if (this->child == NULL) {
    01685     this->child = new NWidgetVertical();
    01686   }
    01687   this->child->Add(nwid);
    01688 }
    01689
    01700 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01701 {
    01702   if (this->child == NULL) {
    01703     this->child = new NWidgetVertical();
    01704   }
    01705   this->child->SetPIP(pip_pre, pip_inter, pip_post);
    01706 }
    01707
    01708 void NWidgetBackground::SetupSmallestSize(Window *w, bool init_array)
    01709 {
    01710   if (init_array && this->index >= 0) {
    01711     assert(w->nested_array_size > (uint)this->index);
    01712     w->nested_array[this->index] = this;
    01713   }
    01714   if (this->child != NULL) {
    01715     this->child->SetupSmallestSize(w, init_array);
    01716
    01717     this->smallest_x = this->child->smallest_x;
    01718     this->smallest_y = this->child->smallest_y;
    01719     this->fill_x = this->child->fill_x;
    01720     this->fill_y = this->child->fill_y;
    01721     this->resize_x = this->child->resize_x;
    01722     this->resize_y = this->child->resize_y;
    01723
    01724     /* Account for the size of the frame's text if that exists */
    01725     if (w != NULL && this->type == WWT_FRAME) {
    01726       this->child->padding_left   = WD_FRAMETEXT_LEFT;
    01727       this->child->padding_right  = WD_FRAMETEXT_RIGHT;
    01728       this->child->padding_top    = max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
    01729       this->child->padding_bottom = WD_FRAMETEXT_BOTTOM;
    01730
    01731       this->smallest_x += this->child->padding_left + this->child->padding_right;
    01732       this->smallest_y += this->child->padding_top + this->child->padding_bottom;
    01733
    01734       if (this->index >= 0) w->SetStringParameters(this->index);
    01735       this->smallest_x = max(this->smallest_x, GetStringBoundingBox(this->widget_data).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
    01736     }
    01737   } else {
    01738     Dimension d = {this->min_x, this->min_y};
    01739     Dimension fill = {this->fill_x, this->fill_y};
    01740     Dimension resize  = {this->resize_x, this->resize_y};
    01741     if (w != NULL) { // A non-NULL window pointer acts as switch to turn dynamic widget size on.
    01742       if (this->type == WWT_FRAME || this->type == WWT_INSET) {
    01743         if (this->index >= 0) w->SetStringParameters(this->index);
    01744         Dimension background = GetStringBoundingBox(this->widget_data);
    01745         background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
    01746         d = maxdim(d, background);
    01747       }
    01748       if (this->index >= 0) {
    01749         static const Dimension padding = {0, 0};
    01750         w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
    01751       }
    01752     }
    01753     this->smallest_x = d.width;
    01754     this->smallest_y = d.height;
    01755     this->fill_x = fill.width;
    01756     this->fill_y = fill.height;
    01757     this->resize_x = resize.width;
    01758     this->resize_y = resize.height;
    01759   }
    01760 }
    01761
    01762 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01763 {
    01764   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01765
    01766   if (this->child != NULL) {
    01767     uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
    01768     uint width = given_width - this->child->padding_right - this->child->padding_left;
    01769     uint height = given_height - this->child->padding_top - this->child->padding_bottom;
    01770     this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
    01771   }
    01772 }
    01773
    01774 void NWidgetBackground::FillNestedArray(NWidgetBase **array, uint length)
    01775 {
    01776   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01777   if (this->child != NULL) this->child->FillNestedArray(array, length);
    01778 }
    01779
    01780 void NWidgetBackground::Draw(const Window *w)
    01781 {
    01782   if (this->current_x == 0 || this->current_y == 0) return;
    01783
    01784   Rect r;
    01785   r.left = this->pos_x;
    01786   r.right = this->pos_x + this->current_x - 1;
    01787   r.top = this->pos_y;
    01788   r.bottom = this->pos_y + this->current_y - 1;
    01789
    01790   const DrawPixelInfo *dpi = _cur_dpi;
    01791   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01792
    01793   switch (this->type) {
    01794     case WWT_PANEL:
    01795       assert(this->widget_data == 0);
    01796       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
    01797       break;
    01798
    01799     case WWT_FRAME:
    01800       if (this->index >= 0) w->SetStringParameters(this->index);
    01801       DrawFrame(r, this->colour, this->widget_data);
    01802       break;
    01803
    01804     case WWT_INSET:
    01805       if (this->index >= 0) w->SetStringParameters(this->index);
    01806       DrawInset(r, this->colour, this->widget_data);
    01807       break;
    01808
    01809     default:
    01810       NOT_REACHED();
    01811   }
    01812
    01813   if (this->index >= 0) w->DrawWidget(r, this->index);
    01814   if (this->child != NULL) this->child->Draw(w);
    01815
    01816   if (this->IsDisabled()) {
    01817     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    01818   }
    01819 }
    01820
    01821 NWidgetCore *NWidgetBackground::GetWidgetFromPos(int x, int y)
    01822 {
    01823   NWidgetCore *nwid = NULL;
    01824   if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
    01825     if (this->child != NULL) nwid = this->child->GetWidgetFromPos(x, y);
    01826     if (nwid == NULL) nwid = this;
    01827   }
    01828   return nwid;
    01829 }
    01830
    01831 NWidgetBase *NWidgetBackground::GetWidgetOfType(WidgetType tp)
    01832 {
    01833   NWidgetBase *nwid = NULL;
    01834   if (this->child != NULL) nwid = this->child->GetWidgetOfType(tp);
    01835   if (nwid == NULL && this->type == tp) nwid = this;
    01836   return nwid;
    01837 }
    01838
    01839 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
    01840 {
    01841   this->SetIndex(index);
    01842 }
    01843
    01844 void NWidgetViewport::SetupSmallestSize(Window *w, bool init_array)
    01845 {
    01846   if (init_array && this->index >= 0) {
    01847     assert(w->nested_array_size > (uint)this->index);
    01848     w->nested_array[this->index] = this;
    01849   }
    01850   this->smallest_x = this->min_x;
    01851   this->smallest_y = this->min_y;
    01852 }
    01853
    01854 void NWidgetViewport::Draw(const Window *w)
    01855 {
    01856   if (this->disp_flags & ND_NO_TRANSPARENCY) {
    01857     TransparencyOptionBits to_backup = _transparency_opt;
    01858     _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_LOADING); // Disable all transparency, except textual stuff
    01859     w->DrawViewport();
    01860     _transparency_opt = to_backup;
    01861   } else {
    01862     w->DrawViewport();
    01863   }
    01864
    01865   /* Optionally shade the viewport. */
    01866   if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
    01867     GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
    01868         (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
    01869   }
    01870 }
    01871
    01878 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
    01879 {
    01880   InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
    01881 }
    01882
    01887 void NWidgetViewport::UpdateViewportCoordinates(Window *w)
    01888 {
    01889   ViewPort *vp = w->viewport;
    01890   if (vp != NULL) {
    01891     vp->left = w->left + this->pos_x;
    01892     vp->top  = w->top + this->pos_y;
    01893     vp->width  = this->current_x;
    01894     vp->height = this->current_y;
    01895
    01896     vp->virtual_width  = ScaleByZoom(vp->width, vp->zoom);
    01897     vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
    01898   }
    01899 }
    01900
    01910 int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
    01911 {
    01912   uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
    01913   if (pos != INT_MAX) pos += this->GetPosition();
    01914   return (pos >= this->GetCount()) ? INT_MAX : pos;
    01915 }
    01916
    01924 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
    01925 {
    01926   NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
    01927   if (this->IsVertical()) {
    01928     this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
    01929   } else {
    01930     this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
    01931   }
    01932 }
    01933
    01940 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL), Scrollbar(tp != NWID_HSCROLLBAR)
    01941 {
    01942   assert(tp == NWID_HSCROLLBAR || tp == NWID_VSCROLLBAR);
    01943   this->SetIndex(index);
    01944
    01945   switch (this->type) {
    01946     case NWID_HSCROLLBAR:
    01947       this->SetMinimalSize(0, NWidgetScrollbar::GetHorizontalDimension().height);
    01948       this->SetResize(1, 0);
    01949       this->SetFill(1, 0);
    01950       this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
    01951       break;
    01952
    01953     case NWID_VSCROLLBAR:
    01954       this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width, 0);
    01955       this->SetResize(0, 1);
    01956       this->SetFill(0, 1);
    01957       this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
    01958       break;
    01959
    01960     default: NOT_REACHED();
    01961   }
    01962 }
    01963
    01964 void NWidgetScrollbar::SetupSmallestSize(Window *w, bool init_array)
    01965 {
    01966   if (init_array && this->index >= 0) {
    01967     assert(w->nested_array_size > (uint)this->index);
    01968     w->nested_array[this->index] = this;
    01969   }
    01970   this->smallest_x = this->min_x;
    01971   this->smallest_y = this->min_y;
    01972 }
    01973
    01974 void NWidgetScrollbar::Draw(const Window *w)
    01975 {
    01976   if (this->current_x == 0 || this->current_y == 0) return;
    01977
    01978   Rect r;
    01979   r.left = this->pos_x;
    01980   r.right = this->pos_x + this->current_x - 1;
    01981   r.top = this->pos_y;
    01982   r.bottom = this->pos_y + this->current_y - 1;
    01983
    01984   const DrawPixelInfo *dpi = _cur_dpi;
    01985   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01986
    01987   bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
    01988   bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
    01989   bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->scrolling_scrollbar == this->index;
    01990
    01991   if (this->type == NWID_HSCROLLBAR) {
    01992     DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    01993   } else {
    01994     DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    01995   }
    01996
    01997   if (this->IsDisabled()) {
    01998     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    01999   }
    02000 }
    02001
    02002 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
    02003 {
    02004   vertical_dimension.width   = vertical_dimension.height   = 0;
    02005   horizontal_dimension.width = horizontal_dimension.height = 0;
    02006 }
    02007
    02008 /* static */ Dimension NWidgetScrollbar::GetVerticalDimension()
    02009 {
    02010   static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
    02011   if (vertical_dimension.width == 0) {
    02012     vertical_dimension = maxdim(GetSpriteSize(SPR_ARROW_UP), GetSpriteSize(SPR_ARROW_DOWN));
    02013     vertical_dimension.width += extra.width;
    02014     vertical_dimension.height += extra.height;
    02015   }
    02016   return vertical_dimension;
    02017 }
    02018
    02019 /* static */ Dimension NWidgetScrollbar::GetHorizontalDimension()
    02020 {
    02021   static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
    02022   if (horizontal_dimension.width == 0) {
    02023     horizontal_dimension = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    02024     horizontal_dimension.width += extra.width;
    02025     horizontal_dimension.height += extra.height;
    02026   }
    02027   return horizontal_dimension;
    02028 }
    02029
    02030 Dimension NWidgetScrollbar::vertical_dimension = {0, 0};
    02031 Dimension NWidgetScrollbar::horizontal_dimension = {0, 0};
    02032
    02034 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
    02035 {
    02036   shadebox_dimension.width  = shadebox_dimension.height  = 0;
    02037   debugbox_dimension.width  = debugbox_dimension.height  = 0;
    02038   stickybox_dimension.width = stickybox_dimension.height = 0;
    02039   resizebox_dimension.width = resizebox_dimension.height = 0;
    02040   closebox_dimension.width  = closebox_dimension.height  = 0;
    02041 }
    02042
    02043 Dimension NWidgetLeaf::shadebox_dimension  = {0, 0};
    02044 Dimension NWidgetLeaf::debugbox_dimension  = {0, 0};
    02045 Dimension NWidgetLeaf::stickybox_dimension = {0, 0};
    02046 Dimension NWidgetLeaf::resizebox_dimension = {0, 0};
    02047 Dimension NWidgetLeaf::closebox_dimension  = {0, 0};
    02048
    02057 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint16 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
    02058 {
    02059   assert(index >= 0 || tp == WWT_LABEL || tp == WWT_TEXT || tp == WWT_CAPTION || tp == WWT_RESIZEBOX || tp == WWT_SHADEBOX || tp == WWT_DEBUGBOX || tp == WWT_STICKYBOX || tp == WWT_CLOSEBOX);
    02060   if (index >= 0) this->SetIndex(index);
    02061   this->SetMinimalSize(0, 0);
    02062   this->SetResize(0, 0);
    02063
    02064   switch (tp) {
    02065     case WWT_EMPTY:
    02066       break;
    02067
    02068     case WWT_PUSHBTN:
    02069     case WWT_IMGBTN:
    02070     case WWT_PUSHIMGBTN:
    02071     case WWT_IMGBTN_2:
    02072     case WWT_TEXTBTN:
    02073     case WWT_PUSHTXTBTN:
    02074     case WWT_TEXTBTN_2:
    02075     case WWT_LABEL:
    02076     case WWT_TEXT:
    02077     case WWT_MATRIX:
    02078     case NWID_BUTTON_DROPDOWN:
    02079     case NWID_PUSHBUTTON_DROPDOWN:
    02080     case WWT_ARROWBTN:
    02081     case WWT_PUSHARROWBTN:
    02082       this->SetFill(0, 0);
    02083       break;
    02084
    02085     case WWT_EDITBOX: {
    02086       Dimension sprite_size = GetSpriteSize(_current_text_dir == TD_RTL ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
    02087       this->SetMinimalSize(30 + sprite_size.width, sprite_size.height);
    02088       this->SetFill(0, 0);
    02089       break;
    02090     }
    02091
    02092     case WWT_CAPTION:
    02093       this->SetFill(1, 0);
    02094       this->SetResize(1, 0);
    02095       this->min_y = WD_CAPTION_HEIGHT;
    02096       this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
    02097       break;
    02098
    02099     case WWT_STICKYBOX:
    02100       this->SetFill(0, 0);
    02101       this->SetMinimalSize(WD_STICKYBOX_WIDTH, WD_CAPTION_HEIGHT);
    02102       this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
    02103       break;
    02104
    02105     case WWT_SHADEBOX:
    02106       this->SetFill(0, 0);
    02107       this->SetMinimalSize(WD_SHADEBOX_TOP, WD_CAPTION_HEIGHT);
    02108       this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
    02109       break;
    02110
    02111     case WWT_DEBUGBOX:
    02112       this->SetFill(0, 0);
    02113       this->SetMinimalSize(WD_DEBUGBOX_TOP, WD_CAPTION_HEIGHT);
    02114       this->SetDataTip(STR_NULL, STR_TOOLTIP_DEBUG);
    02115       break;
    02116
    02117     case WWT_RESIZEBOX:
    02118       this->SetFill(0, 0);
    02119       this->SetMinimalSize(WD_RESIZEBOX_WIDTH, 12);
    02120       this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
    02121       break;
    02122
    02123     case WWT_CLOSEBOX:
    02124       this->SetFill(0, 0);
    02125       this->SetMinimalSize(WD_CLOSEBOX_WIDTH, WD_CAPTION_HEIGHT);
    02126       this->SetDataTip(STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW);
    02127       break;
    02128
    02129     case WWT_DROPDOWN:
    02130       this->SetFill(0, 0);
    02131       this->min_y = WD_DROPDOWN_HEIGHT;
    02132       break;
    02133
    02134     default:
    02135       NOT_REACHED();
    02136   }
    02137 }
    02138
    02139 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
    02140 {
    02141   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    02142     assert(w->nested_array_size > (uint)this->index);
    02143     w->nested_array[this->index] = this;
    02144   }
    02145
    02146   Dimension size = {this->min_x, this->min_y};
    02147   Dimension fill = {this->fill_x, this->fill_y};
    02148   Dimension resize = {this->resize_x, this->resize_y};
    02149   /* Get padding, and update size with the real content size if appropriate. */
    02150   const Dimension *padding = NULL;
    02151   switch (this->type) {
    02152     case WWT_EMPTY: {
    02153       static const Dimension extra = {0, 0};
    02154       padding = &extra;
    02155       break;
    02156     }
    02157     case WWT_MATRIX: {
    02158       static const Dimension extra = {WD_MATRIX_LEFT + WD_MATRIX_RIGHT, WD_MATRIX_TOP + WD_MATRIX_BOTTOM};
    02159       padding = &extra;
    02160       break;
    02161     }
    02162     case WWT_SHADEBOX: {
    02163       static const Dimension extra = {WD_SHADEBOX_LEFT + WD_SHADEBOX_RIGHT, WD_SHADEBOX_TOP + WD_SHADEBOX_BOTTOM};
    02164       padding = &extra;
    02165       if (NWidgetLeaf::shadebox_dimension.width == 0) {
    02166         NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
    02167         NWidgetLeaf::shadebox_dimension.width += extra.width;
    02168         NWidgetLeaf::shadebox_dimension.height += extra.height;
    02169       }
    02170       size = maxdim(size, NWidgetLeaf::shadebox_dimension);
    02171       break;
    02172     }
    02173     case WWT_DEBUGBOX:
    02174       if (_settings_client.gui.newgrf_developer_tools && w->IsNewGRFInspectable()) {
    02175         static const Dimension extra = {WD_DEBUGBOX_LEFT + WD_DEBUGBOX_RIGHT, WD_DEBUGBOX_TOP + WD_DEBUGBOX_BOTTOM};
    02176         padding = &extra;
    02177         if (NWidgetLeaf::debugbox_dimension.width == 0) {
    02178           NWidgetLeaf::debugbox_dimension = GetSpriteSize(SPR_WINDOW_DEBUG);
    02179           NWidgetLeaf::debugbox_dimension.width += extra.width;
    02180           NWidgetLeaf::debugbox_dimension.height += extra.height;
    02181         }
    02182         size = maxdim(size, NWidgetLeaf::debugbox_dimension);
    02183       } else {
    02184         /* If the setting is disabled we don't want to see it! */
    02185         size.width = 0;
    02186         fill.width = 0;
    02187         resize.width = 0;
    02188       }
    02189       break;
    02190
    02191     case WWT_STICKYBOX: {
    02192       static const Dimension extra = {WD_STICKYBOX_LEFT + WD_STICKYBOX_RIGHT, WD_STICKYBOX_TOP + WD_STICKYBOX_BOTTOM};
    02193       padding = &extra;
    02194       if (NWidgetLeaf::stickybox_dimension.width == 0) {
    02195         NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
    02196         NWidgetLeaf::stickybox_dimension.width += extra.width;
    02197         NWidgetLeaf::stickybox_dimension.height += extra.height;
    02198       }
    02199       size = maxdim(size, NWidgetLeaf::stickybox_dimension);
    02200       break;
    02201     }
    02202     case WWT_RESIZEBOX: {
    02203       static const Dimension extra = {WD_RESIZEBOX_LEFT + WD_RESIZEBOX_RIGHT, WD_RESIZEBOX_TOP + WD_RESIZEBOX_BOTTOM};
    02204       padding = &extra;
    02205       if (NWidgetLeaf::resizebox_dimension.width == 0) {
    02206         NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
    02207         NWidgetLeaf::resizebox_dimension.width += extra.width;
    02208         NWidgetLeaf::resizebox_dimension.height += extra.height;
    02209       }
    02210       size = maxdim(size, NWidgetLeaf::resizebox_dimension);
    02211       break;
    02212     }
    02213     case WWT_EDITBOX:
    02214       size.height = max(size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    02215       /* FALL THROUGH */
    02216     case WWT_PUSHBTN: {
    02217       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02218       padding = &extra;
    02219       break;
    02220     }
    02221     case WWT_IMGBTN:
    02222     case WWT_IMGBTN_2:
    02223     case WWT_PUSHIMGBTN: {
    02224       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02225       padding = &extra;
    02226       Dimension d2 = GetSpriteSize(this->widget_data);
    02227       if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
    02228       d2.width += extra.width;
    02229       d2.height += extra.height;
    02230       size = maxdim(size, d2);
    02231       break;
    02232     }
    02233     case WWT_ARROWBTN:
    02234     case WWT_PUSHARROWBTN: {
    02235       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02236       padding = &extra;
    02237       Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    02238       d2.width += extra.width;
    02239       d2.height += extra.height;
    02240       size = maxdim(size, d2);
    02241       break;
    02242     }
    02243
    02244     case WWT_CLOSEBOX: {
    02245       static const Dimension extra = {WD_CLOSEBOX_LEFT + WD_CLOSEBOX_RIGHT, WD_CLOSEBOX_TOP + WD_CLOSEBOX_BOTTOM};
    02246       padding = &extra;
    02247       if (NWidgetLeaf::closebox_dimension.width == 0) {
    02248         NWidgetLeaf::closebox_dimension = maxdim(GetStringBoundingBox(STR_BLACK_CROSS), GetStringBoundingBox(STR_SILVER_CROSS));
    02249         NWidgetLeaf::closebox_dimension.width += extra.width;
    02250         NWidgetLeaf::closebox_dimension.height += extra.height;
    02251       }
    02252       size = maxdim(size, NWidgetLeaf::closebox_dimension);
    02253       break;
    02254     }
    02255     case WWT_TEXTBTN:
    02256     case WWT_PUSHTXTBTN:
    02257     case WWT_TEXTBTN_2: {
    02258       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT,  WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02259       padding = &extra;
    02260       if (this->index >= 0) w->SetStringParameters(this->index);
    02261       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02262       d2.width += extra.width;
    02263       d2.height += extra.height;
    02264       size = maxdim(size, d2);
    02265       break;
    02266     }
    02267     case WWT_LABEL:
    02268     case WWT_TEXT: {
    02269       static const Dimension extra = {0, 0};
    02270       padding = &extra;
    02271       if (this->index >= 0) w->SetStringParameters(this->index);
    02272       size = maxdim(size, GetStringBoundingBox(this->widget_data));
    02273       break;
    02274     }
    02275     case WWT_CAPTION: {
    02276       static const Dimension extra = {WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT, WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM};
    02277       padding = &extra;
    02278       if (this->index >= 0) w->SetStringParameters(this->index);
    02279       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02280       d2.width += extra.width;
    02281       d2.height += extra.height;
    02282       size = maxdim(size, d2);
    02283       break;
    02284     }
    02285     case WWT_DROPDOWN:
    02286     case NWID_BUTTON_DROPDOWN:
    02287     case NWID_PUSHBUTTON_DROPDOWN: {
    02288       static const Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
    02289       padding = &extra;
    02290       if (this->index >= 0) w->SetStringParameters(this->index);
    02291       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02292       d2.width += extra.width;
    02293       d2.height += extra.height;
    02294       size = maxdim(size, d2);
    02295       break;
    02296     }
    02297     default:
    02298       NOT_REACHED();
    02299   }
    02300
    02301   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
    02302
    02303   this->smallest_x = size.width;
    02304   this->smallest_y = size.height;
    02305   this->fill_x = fill.width;
    02306   this->fill_y = fill.height;
    02307   this->resize_x = resize.width;
    02308   this->resize_y = resize.height;
    02309 }
    02310
    02311 void NWidgetLeaf::Draw(const Window *w)
    02312 {
    02313   if (this->current_x == 0 || this->current_y == 0) return;
    02314
    02315   Rect r;
    02316   r.left = this->pos_x;
    02317   r.right = this->pos_x + this->current_x - 1;
    02318   r.top = this->pos_y;
    02319   r.bottom = this->pos_y + this->current_y - 1;
    02320
    02321   const DrawPixelInfo *dpi = _cur_dpi;
    02322   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    02323
    02324   bool clicked = this->IsLowered();
    02325   switch (this->type) {
    02326     case WWT_EMPTY:
    02327       break;
    02328
    02329     case WWT_PUSHBTN:
    02330       assert(this->widget_data == 0);
    02331       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02332       break;
    02333
    02334     case WWT_IMGBTN:
    02335     case WWT_PUSHIMGBTN:
    02336     case WWT_IMGBTN_2:
    02337       DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
    02338       break;
    02339
    02340     case WWT_TEXTBTN:
    02341     case WWT_PUSHTXTBTN:
    02342     case WWT_TEXTBTN_2:
    02343       if (this->index >= 0) w->SetStringParameters(this->index);
    02344       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02345       DrawLabel(r, this->type, clicked, this->widget_data);
    02346       break;
    02347
    02348     case WWT_ARROWBTN:
    02349     case WWT_PUSHARROWBTN: {
    02350       SpriteID sprite;
    02351       switch (this->widget_data) {
    02352         case AWV_DECREASE: sprite = _current_text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02353         case AWV_INCREASE: sprite = _current_text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02354         case AWV_LEFT:     sprite = SPR_ARROW_LEFT;  break;
    02355         case AWV_RIGHT:    sprite = SPR_ARROW_RIGHT; break;
    02356         default: NOT_REACHED();
    02357       }
    02358       DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
    02359     }
    02360
    02361     case WWT_LABEL:
    02362       if (this->index >= 0) w->SetStringParameters(this->index);
    02363       DrawLabel(r, this->type, clicked, this->widget_data);
    02364       break;
    02365
    02366     case WWT_TEXT:
    02367       if (this->index >= 0) w->SetStringParameters(this->index);
    02368       DrawText(r, (TextColour)this->colour, this->widget_data);
    02369       break;
    02370
    02371     case WWT_MATRIX:
    02372       DrawMatrix(r, this->colour, clicked, this->widget_data);
    02373       break;
    02374
    02375     case WWT_EDITBOX: {
    02376       const QueryString *query = w->GetQueryString(this->index);
    02377       if (query != NULL) query->DrawEditBox(w, this->index);
    02378       break;
    02379     }
    02380
    02381     case WWT_CAPTION:
    02382       if (this->index >= 0) w->SetStringParameters(this->index);
    02383       DrawCaption(r, this->colour, w->owner, this->widget_data);
    02384       break;
    02385
    02386     case WWT_SHADEBOX:
    02387       assert(this->widget_data == 0);
    02388       DrawShadeBox(r, this->colour, w->IsShaded());
    02389       break;
    02390
    02391     case WWT_DEBUGBOX:
    02392       DrawDebugBox(r, this->colour, clicked);
    02393       break;
    02394
    02395     case WWT_STICKYBOX:
    02396       assert(this->widget_data == 0);
    02397       DrawStickyBox(r, this->colour, !!(w->flags & WF_STICKY));
    02398       break;
    02399
    02400     case WWT_RESIZEBOX:
    02401       assert(this->widget_data == 0);
    02402       DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags & WF_SIZING));
    02403       break;
    02404
    02405     case WWT_CLOSEBOX:
    02406       DrawCloseBox(r, this->colour, this->widget_data);
    02407       break;
    02408
    02409     case WWT_DROPDOWN:
    02410       if (this->index >= 0) w->SetStringParameters(this->index);
    02411       DrawDropdown(r, this->colour, clicked, this->widget_data);
    02412       break;
    02413
    02414     case NWID_BUTTON_DROPDOWN:
    02415     case NWID_PUSHBUTTON_DROPDOWN:
    02416       if (this->index >= 0) w->SetStringParameters(this->index);
    02417       DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
    02418       break;
    02419
    02420     default:
    02421       NOT_REACHED();
    02422   }
    02423   if (this->index >= 0) w->DrawWidget(r, this->index);
    02424
    02425   if (this->IsDisabled()) {
    02426     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    02427   }
    02428 }
    02429
    02437 bool NWidgetLeaf::ButtonHit(const Point &pt)
    02438 {
    02439   if (_current_text_dir == TD_LTR) {
    02440     int button_width = this->pos_x + this->current_x - 12;
    02441     return pt.x < button_width;
    02442   } else {
    02443     int button_left = this->pos_x + 12;
    02444     return pt.x >= button_left;
    02445   }
    02446 }
    02447
    02448 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
    02449
    02465 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
    02466 {
    02467   int num_used = 0;
    02468
    02469   *dest = NULL;
    02470   *fill_dest = false;
    02471
    02472   while (count > num_used) {
    02473     switch (parts->type) {
    02474       case NWID_SPACER:
    02475         if (*dest != NULL) return num_used;
    02476         *dest = new NWidgetSpacer(0, 0);
    02477         break;
    02478
    02479       case NWID_HORIZONTAL:
    02480         if (*dest != NULL) return num_used;
    02481         *dest = new NWidgetHorizontal(parts->u.cont_flags);
    02482         *fill_dest = true;
    02483         break;
    02484
    02485       case NWID_HORIZONTAL_LTR:
    02486         if (*dest != NULL) return num_used;
    02487         *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
    02488         *fill_dest = true;
    02489         break;
    02490
    02491       case WWT_PANEL:
    02492       case WWT_INSET:
    02493       case WWT_FRAME:
    02494         if (*dest != NULL) return num_used;
    02495         *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02496         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02497         *fill_dest = true;
    02498         break;
    02499
    02500       case NWID_VERTICAL:
    02501         if (*dest != NULL) return num_used;
    02502         *dest = new NWidgetVertical(parts->u.cont_flags);
    02503         *fill_dest = true;
    02504         break;
    02505
    02506       case NWID_MATRIX: {
    02507         if (*dest != NULL) return num_used;
    02508         NWidgetMatrix *nwm = new NWidgetMatrix();
    02509         *dest = nwm;
    02510         *fill_dest = true;
    02511         nwm->SetIndex(parts->u.widget.index);
    02512         nwm->SetColour(parts->u.widget.colour);
    02513         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02514         break;
    02515       }
    02516
    02517       case WPT_FUNCTION: {
    02518         if (*dest != NULL) return num_used;
    02519         /* Ensure proper functioning even when the called code simply writes its largest index. */
    02520         int biggest = -1;
    02521         *dest = parts->u.func_ptr(&biggest);
    02522         *biggest_index = max(*biggest_index, biggest);
    02523         *fill_dest = false;
    02524         break;
    02525       }
    02526
    02527       case WPT_RESIZE: {
    02528         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02529         if (nwrb != NULL) {
    02530           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02531           nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
    02532         }
    02533         break;
    02534       }
    02535
    02536       case WPT_MINSIZE: {
    02537         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02538         if (nwrb != NULL) {
    02539           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02540           nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
    02541         }
    02542         break;
    02543       }
    02544
    02545       case WPT_MINTEXTLINES: {
    02546         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02547         if (nwrb != NULL) {
    02548           assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
    02549           nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
    02550         }
    02551         break;
    02552       }
    02553
    02554       case WPT_FILL: {
    02555         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02556         if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
    02557         break;
    02558       }
    02559
    02560       case WPT_DATATIP: {
    02561         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02562         if (nwc != NULL) {
    02563           nwc->widget_data = parts->u.data_tip.data;
    02564           nwc->tool_tip = parts->u.data_tip.tooltip;
    02565         }
    02566         break;
    02567       }
    02568
    02569       case WPT_PADDING:
    02570         if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
    02571         break;
    02572
    02573       case WPT_PIPSPACE: {
    02574         NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
    02575         if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02576
    02577         NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
    02578         if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02579         break;
    02580       }
    02581
    02582       case WPT_SCROLLBAR: {
    02583         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02584         if (nwc != NULL) {
    02585           nwc->scrollbar_index = parts->u.widget.index;
    02586         }
    02587         break;
    02588       }
    02589
    02590       case WPT_ENDCONTAINER:
    02591         return num_used;
    02592
    02593       case NWID_VIEWPORT:
    02594         if (*dest != NULL) return num_used;
    02595         *dest = new NWidgetViewport(parts->u.widget.index);
    02596         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02597         break;
    02598
    02599       case NWID_HSCROLLBAR:
    02600       case NWID_VSCROLLBAR:
    02601         if (*dest != NULL) return num_used;
    02602         *dest = new NWidgetScrollbar(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02603         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02604         break;
    02605
    02606       case NWID_SELECTION: {
    02607         if (*dest != NULL) return num_used;
    02608         NWidgetStacked *nws = new NWidgetStacked();
    02609         *dest = nws;
    02610         *fill_dest = true;
    02611         nws->SetIndex(parts->u.widget.index);
    02612         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02613         break;
    02614       }
    02615
    02616       default:
    02617         if (*dest != NULL) return num_used;
    02618         assert((parts->type & WWT_MASK) < WWT_LAST || (parts->type & WWT_MASK) == NWID_BUTTON_DROPDOWN);
    02619         *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
    02620         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02621         break;
    02622     }
    02623     num_used++;
    02624     parts++;
    02625   }
    02626
    02627   return num_used;
    02628 }
    02629
    02639 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
    02640 {
    02641   /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
    02642    * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
    02643   NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
    02644   NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
    02645   assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
    02646
    02647   int total_used = 0;
    02648   for (;;) {
    02649     NWidgetBase *sub_widget = NULL;
    02650     bool fill_sub = false;
    02651     int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
    02652     parts += num_used;
    02653     total_used += num_used;
    02654
    02655     /* Break out of loop when end reached */
    02656     if (sub_widget == NULL) break;
    02657
    02658     /* If sub-widget is a container, recursively fill that container. */
    02659     WidgetType tp = sub_widget->type;
    02660     if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
    02661               || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
    02662       NWidgetBase *sub_ptr = sub_widget;
    02663       int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
    02664       parts += num_used;
    02665       total_used += num_used;
    02666     }
    02667
    02668     /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
    02669     if (nwid_cont != NULL) nwid_cont->Add(sub_widget);
    02670     if (nwid_parent != NULL) nwid_parent->Add(sub_widget);
    02671     if (nwid_cont == NULL && nwid_parent == NULL) {
    02672       *parent = sub_widget;
    02673       return total_used;
    02674     }
    02675   }
    02676
    02677   if (count == total_used) return total_used; // Reached the end of the array of parts?
    02678
    02679   assert(total_used < count);
    02680   assert(parts->type == WPT_ENDCONTAINER);
    02681   return total_used + 1; // *parts is also 'used'
    02682 }
    02683
    02695 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
    02696 {
    02697   *biggest_index = -1;
    02698   if (container == NULL) container = new NWidgetVertical();
    02699   NWidgetBase *cont_ptr = container;
    02700   MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
    02701   return container;
    02702 }
    02703
    02717 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
    02718 {
    02719   *biggest_index = -1;
    02720
    02721   /* Read the first widget recursively from the array. */
    02722   NWidgetBase *nwid = NULL;
    02723   int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
    02724   assert(nwid != NULL);
    02725   parts += num_used;
    02726   count -= num_used;
    02727
    02728   NWidgetContainer *root = new NWidgetVertical;
    02729   root->Add(nwid);
    02730   if (count == 0) { // There is no body at all.
    02731     *shade_select = NULL;
    02732     return root;
    02733   }
    02734
    02735   /* If the first widget looks like a titlebar, treat it as such.
    02736    * If it has a shading box, silently add a shade selection widget in the tree. */
    02737   NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
    02738   NWidgetContainer *body;
    02739   if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
    02740     *shade_select = new NWidgetStacked;
    02741     root->Add(*shade_select);
    02742     body = new NWidgetVertical;
    02743     (*shade_select)->Add(body);
    02744   } else {
    02745     *shade_select = NULL;
    02746     body = root;
    02747   }
    02748
    02749   /* Load the remaining parts into 'body'. */
    02750   int biggest2 = -1;
    02751   MakeNWidgets(parts, count, &biggest2, body);
    02752
    02753   *biggest_index = max(*biggest_index, biggest2);
    02754   return root;
    02755 }
    02756
    02767 NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
    02768 {
    02769   NWidgetVertical *vert = NULL; // Storage for all rows.
    02770   NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
    02771   int hor_length = 0;
    02772
    02773   Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
    02774   sprite_size.width  += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
    02775   sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
    02776
    02777   for (int widnum = widget_first; widnum <= widget_last; widnum++) {
    02778     /* Ensure there is room in 'hor' for another button. */
    02779     if (hor_length == max_length) {
    02780       if (vert == NULL) vert = new NWidgetVertical();
    02781       vert->Add(hor);
    02782       hor = NULL;
    02783       hor_length = 0;
    02784     }
    02785     if (hor == NULL) {
    02786       hor = new NWidgetHorizontal();
    02787       hor_length = 0;
    02788     }
    02789
    02790     NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
    02791     panel->SetMinimalSize(sprite_size.width, sprite_size.height);
    02792     panel->SetFill(1, 0);
    02793     panel->SetResize(1, 0);
    02794     panel->SetDataTip(0x0, button_tooltip);
    02795     hor->Add(panel);
    02796     hor_length++;
    02797   }
    02798   *biggest_index = widget_last;
    02799   if (vert == NULL) return hor; // All buttons fit in a single row.
    02800
    02801   if (hor_length > 0 && hor_length < max_length) {
    02802     /* Last row is partial, add a spacer at the end to force all buttons to the left. */
    02803     NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
    02804     spc->SetFill(1, 0);
    02805     spc->SetResize(1, 0);
    02806     hor->Add(spc);
    02807   }
    02808   if (hor != NULL) vert->Add(hor);
    02809   return vert;
    02810 }