# widget.cpp

Go to the documentation of this file.

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