# widget.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "company_func.h"
    00014 #include "window_gui.h"
    00015 #include "viewport_func.h"
    00016 #include "zoom_func.h"
    00017 #include "strings_func.h"
    00018 #include "transparency.h"
    00019 #include "core/geometry_func.hpp"
    00020 #include "settings_type.h"
    00021
    00022 #include "table/sprites.h"
    00023 #include "table/strings.h"
    00024 #include "table/palettes.h"
    00025
    00026 static const char *UPARROW   = "\xEE\x8A\xA0";
    00027 static const char *DOWNARROW = "\xEE\x8A\xAA";
    00028
    00038 static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bool horizontal)
    00039 {
    00040   /* Base for reversion */
    00041   int rev_base = top + bottom;
    00042   int button_size;
    00043   if (horizontal) {
    00044     button_size = NWidgetScrollbar::GetHorizontalDimension().width;
    00045   } else {
    00046     button_size = NWidgetScrollbar::GetVerticalDimension().height;
    00047   }
    00048   top += button_size;    // top    points to just below the up-button
    00049   bottom -= button_size; // bottom points to top of the down-button
    00050
    00051   int height = (bottom - top);
    00052   int pos = sb->GetPosition();
    00053   int count = sb->GetCount();
    00054   int cap = sb->GetCapacity();
    00055
    00056   if (count != 0) top += height * pos / count;
    00057
    00058   if (cap > count) cap = count;
    00059   if (count != 0) bottom -= (count - pos - cap) * height / count;
    00060
    00061   Point pt;
    00062   if (horizontal && _current_text_dir == TD_RTL) {
    00063     pt.x = rev_base - bottom;
    00064     pt.y = rev_base - top;
    00065   } else {
    00066     pt.x = top;
    00067     pt.y = bottom;
    00068   }
    00069   return pt;
    00070 }
    00071
    00081 static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma)
    00082 {
    00083   int pos;
    00084   int button_size;
    00085   bool rtl = false;
    00086
    00087   if (sb->type == NWID_HSCROLLBAR) {
    00088     pos = x;
    00089     rtl = _current_text_dir == TD_RTL;
    00090     button_size = NWidgetScrollbar::GetHorizontalDimension().width;
    00091   } else {
    00092     pos = y;
    00093     button_size = NWidgetScrollbar::GetVerticalDimension().height;
    00094   }
    00095   if (pos < mi + button_size) {
    00096     /* Pressing the upper button? */
    00097     SetBit(sb->disp_flags, NDB_SCROLLBAR_UP);
    00098     if (_scroller_click_timeout <= 1) {
    00099       _scroller_click_timeout = 3;
    00100       sb->UpdatePosition(rtl ? 1 : -1);
    00101     }
    00102     w->scrolling_scrollbar = sb->index;
    00103   } else if (pos >= ma - button_size) {
    00104     /* Pressing the lower button? */
    00105     SetBit(sb->disp_flags, NDB_SCROLLBAR_DOWN);
    00106
    00107     if (_scroller_click_timeout <= 1) {
    00108       _scroller_click_timeout = 3;
    00109       sb->UpdatePosition(rtl ? -1 : 1);
    00110     }
    00111     w->scrolling_scrollbar = sb->index;
    00112   } else {
    00113     Point pt = HandleScrollbarHittest(sb, mi, ma, sb->type == NWID_HSCROLLBAR);
    00114
    00115     if (pos < pt.x) {
    00116       sb->UpdatePosition(rtl ? 1 : -1, Scrollbar::SS_BIG);
    00117     } else if (pos > pt.y) {
    00118       sb->UpdatePosition(rtl ? -1 : 1, Scrollbar::SS_BIG);
    00119     } else {
    00120       _scrollbar_start_pos = pt.x - mi - button_size;
    00121       _scrollbar_size = ma - mi - button_size * 2;
    00122       w->scrolling_scrollbar = sb->index;
    00123       _cursorpos_drag_start = _cursor.pos;
    00124     }
    00125   }
    00126
    00127   w->SetDirty();
    00128 }
    00129
    00138 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
    00139 {
    00140   int mi, ma;
    00141
    00142   if (nw->type == NWID_HSCROLLBAR) {
    00143     mi = nw->pos_x;
    00144     ma = nw->pos_x + nw->current_x;
    00145   } else {
    00146     mi = nw->pos_y;
    00147     ma = nw->pos_y + nw->current_y;
    00148   }
    00149   ScrollbarClickPositioning(w, dynamic_cast<NWidgetScrollbar*>(nw), x, y, mi, ma);
    00150 }
    00151
    00160 int GetWidgetFromPos(const Window *w, int x, int y)
    00161 {
    00162   NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
    00163   return (nw != NULL) ? nw->index : -1;
    00164 }
    00165
    00175 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
    00176 {
    00177   uint dark         = _colour_gradient[colour][3];
    00178   uint medium_dark  = _colour_gradient[colour][5];
    00179   uint medium_light = _colour_gradient[colour][6];
    00180   uint light        = _colour_gradient[colour][7];
    00181
    00182   if (flags & FR_TRANSPARENT) {
    00183     GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR);
    00184   } else {
    00185     uint interior;
    00186
    00187     if (flags & FR_LOWERED) {
    00188       GfxFillRect(left,                 top,                left,                   bottom,                   dark);
    00189       GfxFillRect(left + WD_BEVEL_LEFT, top,                right,                  top,                      dark);
    00190       GfxFillRect(right,                top + WD_BEVEL_TOP, right,                  bottom - WD_BEVEL_BOTTOM, light);
    00191       GfxFillRect(left + WD_BEVEL_LEFT, bottom,             right,                  bottom,                   light);
    00192       interior = (flags & FR_DARKENED ? medium_dark : medium_light);
    00193     } else {
    00194       GfxFillRect(left,                 top,                left,                   bottom - WD_BEVEL_BOTTOM, light);
    00195       GfxFillRect(left + WD_BEVEL_LEFT, top,                right - WD_BEVEL_RIGHT, top,                      light);
    00196       GfxFillRect(right,                top,                right,                  bottom - WD_BEVEL_BOTTOM, dark);
    00197       GfxFillRect(left,                 bottom,             right,                  bottom,                   dark);
    00198       interior = medium_dark;
    00199     }
    00200     if (!(flags & FR_BORDERONLY)) {
    00201       GfxFillRect(left + WD_BEVEL_LEFT, top + WD_BEVEL_TOP, right - WD_BEVEL_RIGHT, bottom - WD_BEVEL_BOTTOM, interior);
    00202     }
    00203   }
    00204 }
    00205
    00214 static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
    00215 {
    00216   assert(img != 0);
    00217   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00218
    00219   if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2.
    00220   DrawSprite(img, PAL_NONE, r.left + WD_IMGBTN_LEFT + clicked, r.top + WD_IMGBTN_TOP + clicked);
    00221 }
    00222
    00230 static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
    00231 {
    00232   if (str == STR_NULL) return;
    00233   if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
    00234   Dimension d = GetStringBoundingBox(str);
    00235   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00236   DrawString(r.left + clicked, r.right + clicked, r.top + offset + clicked, str, TC_FROMSTRING, SA_HOR_CENTER);
    00237 }
    00238
    00245 static inline void DrawText(const Rect &r, TextColour colour, StringID str)
    00246 {
    00247   Dimension d = GetStringBoundingBox(str);
    00248   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00249   if (str != STR_NULL) DrawString(r.left, r.right, r.top + offset, str, colour);
    00250 }
    00251
    00258 static inline void DrawInset(const Rect &r, Colours colour, StringID str)
    00259 {
    00260   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
    00261   if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str);
    00262 }
    00263
    00271 static inline void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint16 data)
    00272 {
    00273   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00274
    00275   int num_columns = GB(data, MAT_COL_START, MAT_COL_BITS);  // Lower 8 bits of the widget data: Number of columns in the matrix.
    00276   int column_width = (r.right - r.left + 1) / num_columns; // Width of a single column in the matrix.
    00277
    00278   int num_rows = GB(data, MAT_ROW_START, MAT_ROW_BITS); // Upper 8 bits of the widget data: Number of rows in the matrix.
    00279   int row_height = (r.bottom - r.top + 1) / num_rows; // Height of a single row in the matrix.
    00280
    00281   int col = _colour_gradient[colour & 0xF][6];
    00282
    00283   int x = r.left;
    00284   for (int ctr = num_columns; ctr > 1; ctr--) {
    00285     x += column_width;
    00286     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00287   }
    00288
    00289   x = r.top;
    00290   for (int ctr = num_rows; ctr > 1; ctr--) {
    00291     x += row_height;
    00292     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00293   }
    00294
    00295   col = _colour_gradient[colour & 0xF][4];
    00296
    00297   x = r.left - 1;
    00298   for (int ctr = num_columns; ctr > 1; ctr--) {
    00299     x += column_width;
    00300     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00301   }
    00302
    00303   x = r.top - 1;
    00304   for (int ctr = num_rows; ctr > 1; ctr--) {
    00305     x += row_height;
    00306     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00307   }
    00308 }
    00309
    00319 static inline void DrawVerticalScrollbar(const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
    00320 {
    00321   int centre = (r.right - r.left) / 2;
    00322   int height = NWidgetScrollbar::GetVerticalDimension().height;
    00323
    00324   /* draw up/down buttons */
    00325   DrawFrameRect(r.left, r.top, r.right, r.top + height - 1, colour, (up_clicked) ? FR_LOWERED : FR_NONE);
    00326   DrawString(r.left + up_clicked, r.right + up_clicked, r.top + up_clicked, UPARROW, TC_BLACK, SA_HOR_CENTER);
    00327
    00328   DrawFrameRect(r.left, r.bottom - (height - 1), r.right, r.bottom, colour, (down_clicked) ? FR_LOWERED : FR_NONE);
    00329   DrawString(r.left + down_clicked, r.right + down_clicked, r.bottom - (height - 1) + down_clicked, DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00330
    00331   int c1 = _colour_gradient[colour & 0xF][3];
    00332   int c2 = _colour_gradient[colour & 0xF][7];
    00333
    00334   /* draw "shaded" background */
    00335   GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c2);
    00336   GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c1, FILLRECT_CHECKER);
    00337
    00338   /* draw shaded lines */
    00339   GfxFillRect(r.left + centre - 3, r.top + height, r.left + centre - 3, r.bottom - height, c1);
    00340   GfxFillRect(r.left + centre - 2, r.top + height, r.left + centre - 2, r.bottom - height, c2);
    00341   GfxFillRect(r.left + centre + 2, r.top + height, r.left + centre + 2, r.bottom - height, c1);
    00342   GfxFillRect(r.left + centre + 3, r.top + height, r.left + centre + 3, r.bottom - height, c2);
    00343
    00344   Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom, false);
    00345   DrawFrameRect(r.left, pt.x, r.right, pt.y, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00346 }
    00347
    00357 static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
    00358 {
    00359   int centre = (r.bottom - r.top) / 2;
    00360   int width = NWidgetScrollbar::GetHorizontalDimension().width;
    00361
    00362   DrawFrameRect(r.left, r.top, r.left + width - 1, r.bottom, colour, left_clicked ? FR_LOWERED : FR_NONE);
    00363   DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + left_clicked, r.top + 1 + left_clicked);
    00364
    00365   DrawFrameRect(r.right - (width - 1), r.top, r.right, r.bottom, colour, right_clicked ? FR_LOWERED : FR_NONE);
    00366   DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - (width - 2) + right_clicked, r.top + 1 + right_clicked);
    00367
    00368   int c1 = _colour_gradient[colour & 0xF][3];
    00369   int c2 = _colour_gradient[colour & 0xF][7];
    00370
    00371   /* draw "shaded" background */
    00372   GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c2);
    00373   GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c1, FILLRECT_CHECKER);
    00374
    00375   /* draw shaded lines */
    00376   GfxFillRect(r.left + width, r.top + centre - 3, r.right - width, r.top + centre - 3, c1);
    00377   GfxFillRect(r.left + width, r.top + centre - 2, r.right - width, r.top + centre - 2, c2);
    00378   GfxFillRect(r.left + width, r.top + centre + 2, r.right - width, r.top + centre + 2, c1);
    00379   GfxFillRect(r.left + width, r.top + centre + 3, r.right - width, r.top + centre + 3, c2);
    00380
    00381   /* draw actual scrollbar */
    00382   Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right, true);
    00383   DrawFrameRect(pt.x, r.top, pt.y, r.bottom, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00384 }
    00385
    00392 static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
    00393 {
    00394   int x2 = r.left; // by default the left side is the left side of the widget
    00395
    00396   if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str);
    00397
    00398   int c1 = _colour_gradient[colour][3];
    00399   int c2 = _colour_gradient[colour][7];
    00400
    00401   /* If the frame has text, adjust the top bar to fit half-way through */
    00402   int dy1 = 4;
    00403   if (str != STR_NULL) dy1 = FONT_HEIGHT_NORMAL / 2 - 1;
    00404   int dy2 = dy1 + 1;
    00405
    00406   if (_current_text_dir == TD_LTR) {
    00407     /* Line from upper left corner to start of text */
    00408     GfxFillRect(r.left, r.top + dy1, r.left + 4, r.top + dy1, c1);
    00409     GfxFillRect(r.left + 1, r.top + dy2, r.left + 4, r.top + dy2, c2);
    00410
    00411     /* Line from end of text to upper right corner */
    00412     GfxFillRect(x2, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00413     GfxFillRect(x2, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00414   } else {
    00415     /* Line from upper left corner to start of text */
    00416     GfxFillRect(r.left, r.top + dy1, x2 - 2, r.top + dy1, c1);
    00417     GfxFillRect(r.left + 1, r.top + dy2, x2 - 2, r.top + dy2, c2);
    00418
    00419     /* Line from end of text to upper right corner */
    00420     GfxFillRect(r.right - 5, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00421     GfxFillRect(r.right - 5, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00422   }
    00423
    00424   /* Line from upper left corner to bottom left corner */
    00425   GfxFillRect(r.left, r.top + dy2, r.left, r.bottom - 1, c1);
    00426   GfxFillRect(r.left + 1, r.top + dy2 + 1, r.left + 1, r.bottom - 2, c2);
    00427
    00428   /* Line from upper right corner to bottom right corner */
    00429   GfxFillRect(r.right - 1, r.top + dy2, r.right - 1, r.bottom - 2, c1);
    00430   GfxFillRect(r.right, r.top + dy1, r.right, r.bottom - 1, c2);
    00431
    00432   GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
    00433   GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
    00434 }
    00435
    00442 static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
    00443 {
    00444   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00445   DrawSprite((clicked) ? SPR_WINDOW_SHADE : SPR_WINDOW_UNSHADE, PAL_NONE, r.left + WD_SHADEBOX_LEFT + clicked, r.top + WD_SHADEBOX_TOP + clicked);
    00446 }
    00447
    00454 static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
    00455 {
    00456   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00457   DrawSprite((clicked) ? SPR_PIN_UP : SPR_PIN_DOWN, PAL_NONE, r.left + WD_STICKYBOX_LEFT + clicked, r.top + WD_STICKYBOX_TOP + clicked);
    00458 }
    00459
    00466 static inline void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
    00467 {
    00468   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00469   DrawSprite(SPR_WINDOW_DEBUG, PAL_NONE, r.left + WD_DEBUGBOX_LEFT + clicked, r.top + WD_DEBUGBOX_TOP + clicked);
    00470 }
    00471
    00479 static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
    00480 {
    00481   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00482   if (at_left) {
    00483     DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked,
    00484          r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_LEFT).height + clicked);
    00485   } else {
    00486     DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.left + WD_RESIZEBOX_LEFT + clicked,
    00487          r.bottom - WD_RESIZEBOX_BOTTOM - GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT).height + clicked);
    00488   }
    00489 }
    00490
    00497 static inline void DrawCloseBox(const Rect &r, Colours colour, StringID str)
    00498 {
    00499   assert(str == STR_BLACK_CROSS || str == STR_SILVER_CROSS); // black or silver cross
    00500   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE);
    00501   DrawString(r.left + WD_CLOSEBOX_LEFT, r.right - WD_CLOSEBOX_RIGHT, r.top + WD_CLOSEBOX_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
    00502 }
    00503
    00511 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
    00512 {
    00513   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_BORDERONLY);
    00514   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);
    00515
    00516   if (owner != INVALID_OWNER) {
    00517     GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]);
    00518   }
    00519
    00520   if (str != STR_NULL) {
    00521     Dimension d = GetStringBoundingBox(str);
    00522     int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00523     DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + offset, str, TC_FROMSTRING, SA_HOR_CENTER);
    00524   }
    00525 }
    00526
    00537 static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
    00538 {
    00539   int text_offset = max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered
    00540
    00541   if (_current_text_dir == TD_LTR) {
    00542     DrawFrameRect(r.left, r.top, r.right - 12, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00543     DrawFrameRect(r.right - 11, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00544     DrawString(r.right - (clicked_dropdown ? 10 : 11), r.right, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00545     if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_LEFT + clicked_button, r.right - WD_DROPDOWNTEXT_RIGHT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK);
    00546   } else {
    00547     DrawFrameRect(r.left + 12, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00548     DrawFrameRect(r.left, r.top, r.left + 11, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00549     DrawString(r.left + clicked_dropdown, r.left + 11, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00550     if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_RIGHT + clicked_button, r.right - WD_DROPDOWNTEXT_LEFT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK);
    00551   }
    00552 }
    00553
    00561 static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
    00562 {
    00563   DrawButtonDropdown(r, colour, false, clicked, str);
    00564 }
    00565
    00569 void Window::DrawWidgets() const
    00570 {
    00571   this->nested_root->Draw(this);
    00572
    00573   if (this->flags & WF_WHITE_BORDER) {
    00574     DrawFrameRect(0, 0, this->width - 1, this->height - 1, COLOUR_WHITE, FR_BORDERONLY);
    00575   }
    00576
    00577   if (this->flags & WF_HIGHLIGHTED) {
    00578     extern bool _window_highlight_colour;
    00579     for (uint i = 0; i < this->nested_array_size; i++) {
    00580       const NWidgetBase *widget = this->GetWidget<NWidgetBase>(i);
    00581       if (widget == NULL || !widget->IsHighlighted()) continue;
    00582
    00583       int left = widget->pos_x;
    00584       int top  = widget->pos_y;
    00585       int right  = left + widget->current_x - 1;
    00586       int bottom = top  + widget->current_y - 1;
    00587
    00588       int colour = _string_colourmap[_window_highlight_colour ? widget->GetHighlightColour() : TC_WHITE];
    00589
    00590       GfxFillRect(left,                 top,    left,                   bottom - WD_BEVEL_BOTTOM, colour);
    00591       GfxFillRect(left + WD_BEVEL_LEFT, top,    right - WD_BEVEL_RIGHT, top,                      colour);
    00592       GfxFillRect(right,                top,    right,                  bottom - WD_BEVEL_BOTTOM, colour);
    00593       GfxFillRect(left,                 bottom, right,                  bottom,                   colour);
    00594     }
    00595   }
    00596 }
    00597
    00603 void Window::DrawSortButtonState(int widget, SortButtonState state) const
    00604 {
    00605   if (state == SBS_OFF) return;
    00606
    00607   assert(this->nested_array != NULL);
    00608   const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
    00609
    00610   int offset = this->IsWidgetLowered(widget) ? 1 : 0;
    00611   int base = offset + nwid->pos_x + (_current_text_dir == TD_LTR ? nwid->current_x - WD_SORTBUTTON_ARROW_WIDTH : 0);
    00612   int top = nwid->pos_y;
    00613
    00614   DrawString(base, base + WD_SORTBUTTON_ARROW_WIDTH, top + 1 + offset, state == SBS_DOWN ? DOWNARROW : UPARROW, TC_BLACK, SA_HOR_CENTER);
    00615 }
    00616
    00617
    00675 NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator()
    00676 {
    00677   this->type = tp;
    00678 }
    00679
    00680 /* ~NWidgetContainer() takes care of #next and #prev data members. */
    00681
    00729 void NWidgetBase::SetDirty(const Window *w) const
    00730 {
    00731   int abs_left = w->left + this->pos_x;
    00732   int abs_top = w->top + this->pos_y;
    00733   SetDirtyBlocks(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
    00734 }
    00735
    00749 NWidgetBase *NWidgetBase::GetWidgetOfType(WidgetType tp)
    00750 {
    00751   return (this->type == tp) ? this : NULL;
    00752 }
    00753
    00760 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y) : NWidgetBase(tp)
    00761 {
    00762   this->fill_x = fill_x;
    00763   this->fill_y = fill_y;
    00764 }
    00765
    00771 void NWidgetResizeBase::SetMinimalSize(uint min_x, uint min_y)
    00772 {
    00773   this->min_x = min_x;
    00774   this->min_y = min_y;
    00775 }
    00776
    00783 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
    00784 {
    00785   this->min_y = min_lines * GetCharacterHeight(size) + spacing;
    00786 }
    00787
    00793 void NWidgetResizeBase::SetFill(uint fill_x, uint fill_y)
    00794 {
    00795   this->fill_x = fill_x;
    00796   this->fill_y = fill_y;
    00797 }
    00798
    00804 void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y)
    00805 {
    00806   this->resize_x = resize_x;
    00807   this->resize_y = resize_y;
    00808 }
    00809
    00810 void NWidgetResizeBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00811 {
    00812   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    00813 }
    00814
    00824 NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint16 widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y)
    00825 {
    00826   this->colour = colour;
    00827   this->index = -1;
    00828   this->widget_data = widget_data;
    00829   this->tool_tip = tool_tip;
    00830   this->scrollbar_index = -1;
    00831 }
    00832
    00837 void NWidgetCore::SetIndex(int index)
    00838 {
    00839   assert(index >= 0);
    00840   this->index = index;
    00841 }
    00842
    00848 void NWidgetCore::SetDataTip(uint16 widget_data, StringID tool_tip)
    00849 {
    00850   this->widget_data = widget_data;
    00851   this->tool_tip = tool_tip;
    00852 }
    00853
    00854 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
    00855 {
    00856   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    00857 }
    00858
    00859 NWidgetCore *NWidgetCore::GetWidgetFromPos(int x, int y)
    00860 {
    00861   return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : NULL;
    00862 }
    00863
    00868 NWidgetContainer::NWidgetContainer(WidgetType tp) : NWidgetBase(tp)
    00869 {
    00870   this->head = NULL;
    00871   this->tail = NULL;
    00872 }
    00873
    00874 NWidgetContainer::~NWidgetContainer()
    00875 {
    00876   while (this->head != NULL) {
    00877     NWidgetBase *wid = this->head->next;
    00878     delete this->head;
    00879     this->head = wid;
    00880   }
    00881   this->tail = NULL;
    00882 }
    00883
    00884 NWidgetBase *NWidgetContainer::GetWidgetOfType(WidgetType tp)
    00885 {
    00886   if (this->type == tp) return this;
    00887   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00888     NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
    00889     if (nwid != NULL) return nwid;
    00890   }
    00891   return NULL;
    00892 }
    00893
    00898 void NWidgetContainer::Add(NWidgetBase *wid)
    00899 {
    00900   assert(wid->next == NULL && wid->prev == NULL);
    00901
    00902   if (this->head == NULL) {
    00903     this->head = wid;
    00904     this->tail = wid;
    00905   } else {
    00906     assert(this->tail != NULL);
    00907     assert(this->tail->next == NULL);
    00908
    00909     this->tail->next = wid;
    00910     wid->prev = this->tail;
    00911     this->tail = wid;
    00912   }
    00913 }
    00914
    00915 void NWidgetContainer::FillNestedArray(NWidgetBase **array, uint length)
    00916 {
    00917   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00918     child_wid->FillNestedArray(array, length);
    00919   }
    00920 }
    00921
    00925 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION)
    00926 {
    00927   this->index = -1;
    00928 }
    00929
    00930 void NWidgetStacked::SetIndex(int index)
    00931 {
    00932   this->index = index;
    00933 }
    00934
    00935 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
    00936 {
    00937   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    00938     assert(w->nested_array_size > (uint)this->index);
    00939     w->nested_array[this->index] = this;
    00940   }
    00941
    00942   /* Zero size plane selected */
    00943   if (this->shown_plane >= SZSP_BEGIN) {
    00944     Dimension size    = {0, 0};
    00945     Dimension padding = {0, 0};
    00946     Dimension fill    = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00947     Dimension resize  = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00948     /* Here we're primarily interested in the value of resize */
    00949     if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    00950
    00951     this->smallest_x = size.width;
    00952     this->smallest_y = size.height;
    00953     this->fill_x = fill.width;
    00954     this->fill_y = fill.height;
    00955     this->resize_x = resize.width;
    00956     this->resize_y = resize.height;
    00957     return;
    00958   }
    00959
    00960   /* First sweep, recurse down and compute minimal size and filling. */
    00961   this->smallest_x = 0;
    00962   this->smallest_y = 0;
    00963   this->fill_x = (this->head != NULL) ? 1 : 0;
    00964   this->fill_y = (this->head != NULL) ? 1 : 0;
    00965   this->resize_x = (this->head != NULL) ? 1 : 0;
    00966   this->resize_y = (this->head != NULL) ? 1 : 0;
    00967   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00968     child_wid->SetupSmallestSize(w, init_array);
    00969
    00970     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    00971     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    00972     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    00973     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    00974     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    00975     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    00976   }
    00977 }
    00978
    00979 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00980 {
    00981   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    00982   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    00983
    00984   if (this->shown_plane >= SZSP_BEGIN) return;
    00985
    00986   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00987     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    00988     uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    00989     uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
    00990
    00991     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    00992     uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    00993     uint child_pos_y = child_wid->padding_top;
    00994
    00995     child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
    00996   }
    00997 }
    00998
    00999 void NWidgetStacked::FillNestedArray(NWidgetBase **array, uint length)
    01000 {
    01001   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01002   NWidgetContainer::FillNestedArray(array, length);
    01003 }
    01004
    01005 void NWidgetStacked::Draw(const Window *w)
    01006 {
    01007   if (this->shown_plane >= SZSP_BEGIN) return;
    01008
    01009   int plane = 0;
    01010   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01011     if (plane == this->shown_plane) {
    01012       child_wid->Draw(w);
    01013       return;
    01014     }
    01015   }
    01016
    01017   NOT_REACHED();
    01018 }
    01019
    01020 NWidgetCore *NWidgetStacked::GetWidgetFromPos(int x, int y)
    01021 {
    01022   if (this->shown_plane >= SZSP_BEGIN) return NULL;
    01023
    01024   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01025   int plane = 0;
    01026   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01027     if (plane == this->shown_plane) {
    01028       return child_wid->GetWidgetFromPos(x, y);
    01029     }
    01030   }
    01031   return NULL;
    01032 }
    01033
    01038 void NWidgetStacked::SetDisplayedPlane(int plane)
    01039 {
    01040   this->shown_plane = plane;
    01041 }
    01042
    01043 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
    01044 {
    01045   this->flags = flags;
    01046 }
    01047
    01057 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01058 {
    01059   this->pip_pre = pip_pre;
    01060   this->pip_inter = pip_inter;
    01061   this->pip_post = pip_post;
    01062 }
    01063
    01064 void NWidgetPIPContainer::Draw(const Window *w)
    01065 {
    01066   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01067     child_wid->Draw(w);
    01068   }
    01069 }
    01070
    01071 NWidgetCore *NWidgetPIPContainer::GetWidgetFromPos(int x, int y)
    01072 {
    01073   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01074
    01075   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01076     NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
    01077     if (nwid != NULL) return nwid;
    01078   }
    01079   return NULL;
    01080 }
    01081
    01083 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
    01084 {
    01085 }
    01086
    01087 void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array)
    01088 {
    01089   this->smallest_x = 0; // Sum of minimal size of all childs.
    01090   this->smallest_y = 0; // Biggest child.
    01091   this->fill_x = 0;     // smallest non-zero child widget fill step.
    01092   this->fill_y = 1;     // smallest common child fill step.
    01093   this->resize_x = 0;   // smallest non-zero child widget resize step.
    01094   this->resize_y = 1;   // smallest common child resize step.
    01095
    01096   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01097   uint longest = 0; // Longest child found.
    01098   uint max_vert_fill = 0; // Biggest vertical fill step.
    01099   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01100     child_wid->SetupSmallestSize(w, init_array);
    01101     longest = max(longest, child_wid->smallest_x);
    01102     max_vert_fill = max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
    01103     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    01104   }
    01105   /* 1b. Make the container higher if needed to accomadate all childs nicely. */
    01106   uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height.
    01107   uint cur_height = this->smallest_y;
    01108   for (;;) {
    01109     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01110       uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
    01111       uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01112       if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting childs are not interesting.
    01113         uint remainder = (cur_height - child_height) % step_size;
    01114         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01115           cur_height += step_size - remainder;
    01116           assert(cur_height < max_smallest); // Safeguard against infinite height expansion.
    01117           /* Remaining childs will adapt to the new cur_height, thus speeding up the computation. */
    01118         }
    01119       }
    01120     }
    01121     if (this->smallest_y == cur_height) break;
    01122     this->smallest_y = cur_height; // Smallest height got changed, try again.
    01123   }
    01124   /* 2. For containers that must maintain equal width, extend child minimal size. */
    01125   if (this->flags & NC_EQUALSIZE) {
    01126     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01127       if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
    01128     }
    01129   }
    01130   /* 3. Move PIP space to the childs, compute smallest, fill, and resize values of the container. */
    01131   if (this->head != NULL) this->head->padding_left += this->pip_pre;
    01132   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01133     if (child_wid->next != NULL) {
    01134       child_wid->padding_right += this->pip_inter;
    01135     } else {
    01136       child_wid->padding_right += this->pip_post;
    01137     }
    01138
    01139     this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01140     if (child_wid->fill_x > 0) {
    01141       if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
    01142     }
    01143     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    01144
    01145     if (child_wid->resize_x > 0) {
    01146       if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
    01147     }
    01148     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    01149   }
    01150   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01151   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01152 }
    01153
    01154 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01155 {
    01156   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01157
    01158   /* Compute additional width given to us. */
    01159   uint additional_length = given_width;
    01160   if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
    01161     /* For EQUALSIZE containers this does not sum to smallest_x during initialisation */
    01162     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01163       additional_length -= child_wid->smallest_x + child_wid->padding_right + child_wid->padding_left;
    01164     }
    01165   } else {
    01166     additional_length -= this->smallest_x;
    01167   }
    01168
    01169   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01170
    01171   /* In principle, the additional horizontal space is distributed evenly over the available resizable childs. Due to step sizes, this may not always be feasible.
    01172    * To make resizing work as good as possible, first childs with biggest step sizes are done. These may get less due to rounding down.
    01173    * This additional space is then given to childs with smaller step sizes. This will give a good result when resize steps of each child is a multiple
    01174    * of the child with the smallest non-zero stepsize.
    01175    *
    01176    * 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
    01177    * size and position, and directly call child->AssignSizePosition() with the computed values.
    01178    * 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
    01179    * then we call the child.
    01180    */
    01181
    01182   /* First loop: Find biggest stepsize, find number of childs that want a piece of the pie, handle vertical size for all childs, handle horizontal size for non-resizing childs. */
    01183   int num_changing_childs = 0; // Number of childs that can change size.
    01184   uint biggest_stepsize = 0;
    01185   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01186     uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01187     if (hor_step > 0) {
    01188       num_changing_childs++;
    01189       biggest_stepsize = max(biggest_stepsize, hor_step);
    01190     } else {
    01191       child_wid->current_x = child_wid->smallest_x;
    01192     }
    01193
    01194     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    01195     child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    01196   }
    01197
    01198   /* Second loop: Allocate the additional horizontal space over the resizing childs, starting with the biggest resize steps. */
    01199   while (biggest_stepsize > 0) {
    01200     uint next_biggest_stepsize = 0;
    01201     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01202       uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01203       if (hor_step > biggest_stepsize) continue; // Already done
    01204       if (hor_step == biggest_stepsize) {
    01205         uint increment = additional_length / num_changing_childs;
    01206         num_changing_childs--;
    01207         if (hor_step > 1) increment -= increment % hor_step;
    01208         child_wid->current_x = child_wid->smallest_x + increment;
    01209         additional_length -= increment;
    01210         continue;
    01211       }
    01212       next_biggest_stepsize = max(next_biggest_stepsize, hor_step);
    01213     }
    01214     biggest_stepsize = next_biggest_stepsize;
    01215   }
    01216   assert(num_changing_childs == 0);
    01217
    01218   /* Third loop: Compute position and call the child. */
    01219   uint position = 0; // Place to put next child relative to origin of the container.
    01220   NWidgetBase *child_wid = rtl ? this->tail : this->head;
    01221   while (child_wid != NULL) {
    01222     uint child_width = child_wid->current_x;
    01223     uint child_x = x + position + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01224     uint child_y = y + child_wid->padding_top;
    01225
    01226     child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
    01227     position += child_width + child_wid->padding_right + child_wid->padding_left;
    01228
    01229     child_wid = rtl ? child_wid->prev : child_wid->next;
    01230   }
    01231 }
    01232
    01234 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
    01235 {
    01236   this->type = NWID_HORIZONTAL_LTR;
    01237 }
    01238
    01239 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01240 {
    01241   NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
    01242 }
    01243
    01245 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
    01246 {
    01247 }
    01248
    01249 void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array)
    01250 {
    01251   this->smallest_x = 0; // Biggest child.
    01252   this->smallest_y = 0; // Sum of minimal size of all childs.
    01253   this->fill_x = 1;     // smallest common child fill step.
    01254   this->fill_y = 0;     // smallest non-zero child widget fill step.
    01255   this->resize_x = 1;   // smallest common child resize step.
    01256   this->resize_y = 0;   // smallest non-zero child widget resize step.
    01257
    01258   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01259   uint highest = 0; // Highest child found.
    01260   uint max_hor_fill = 0; // Biggest horizontal fill step.
    01261   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01262     child_wid->SetupSmallestSize(w, init_array);
    01263     highest = max(highest, child_wid->smallest_y);
    01264     max_hor_fill = max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
    01265     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    01266   }
    01267   /* 1b. Make the container wider if needed to accomadate all childs nicely. */
    01268   uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height.
    01269   uint cur_width = this->smallest_x;
    01270   for (;;) {
    01271     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01272       uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
    01273       uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01274       if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting childs are not interesting.
    01275         uint remainder = (cur_width - child_width) % step_size;
    01276         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01277           cur_width += step_size - remainder;
    01278           assert(cur_width < max_smallest); // Safeguard against infinite width expansion.
    01279           /* Remaining childs will adapt to the new cur_width, thus speeding up the computation. */
    01280         }
    01281       }
    01282     }
    01283     if (this->smallest_x == cur_width) break;
    01284     this->smallest_x = cur_width; // Smallest width got changed, try again.
    01285   }
    01286   /* 2. For containers that must maintain equal width, extend child minimal size. */
    01287   if (this->flags & NC_EQUALSIZE) {
    01288     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01289       if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
    01290     }
    01291   }
    01292   /* 3. Move PIP space to the childs, compute smallest, fill, and resize values of the container. */
    01293   if (this->head != NULL) this->head->padding_top += this->pip_pre;
    01294   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01295     if (child_wid->next != NULL) {
    01296       child_wid->padding_bottom += this->pip_inter;
    01297     } else {
    01298       child_wid->padding_bottom += this->pip_post;
    01299     }
    01300
    01301     this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01302     if (child_wid->fill_y > 0) {
    01303       if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
    01304     }
    01305     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    01306
    01307     if (child_wid->resize_y > 0) {
    01308       if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
    01309     }
    01310     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    01311   }
    01312   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01313   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01314 }
    01315
    01316 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01317 {
    01318   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01319
    01320   /* Compute additional height given to us. */
    01321   uint additional_length = given_height;
    01322   if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
    01323     /* For EQUALSIZE containers this does not sum to smallest_y during initialisation */
    01324     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01325       additional_length -= child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01326     }
    01327   } else {
    01328     additional_length -= this->smallest_y;
    01329   }
    01330
    01331   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01332
    01333   /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the childs with the biggest resize steps.
    01334    * It also stores computed widths and heights into current_x and current_y values of the child.
    01335    */
    01336
    01337   /* First loop: Find biggest stepsize, find number of childs that want a piece of the pie, handle horizontal size for all childs, handle vertical size for non-resizing childs. */
    01338   int num_changing_childs = 0; // Number of childs that can change size.
    01339   uint biggest_stepsize = 0;
    01340   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01341     uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01342     if (vert_step > 0) {
    01343       num_changing_childs++;
    01344       biggest_stepsize = max(biggest_stepsize, vert_step);
    01345     } else {
    01346       child_wid->current_y = child_wid->smallest_y;
    01347     }
    01348
    01349     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    01350     child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    01351   }
    01352
    01353   /* Second loop: Allocate the additional vertical space over the resizing childs, starting with the biggest resize steps. */
    01354   while (biggest_stepsize > 0) {
    01355     uint next_biggest_stepsize = 0;
    01356     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01357       uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01358       if (vert_step > biggest_stepsize) continue; // Already done
    01359       if (vert_step == biggest_stepsize) {
    01360         uint increment = additional_length / num_changing_childs;
    01361         num_changing_childs--;
    01362         if (vert_step > 1) increment -= increment % vert_step;
    01363         child_wid->current_y = child_wid->smallest_y + increment;
    01364         additional_length -= increment;
    01365         continue;
    01366       }
    01367       next_biggest_stepsize = max(next_biggest_stepsize, vert_step);
    01368     }
    01369     biggest_stepsize = next_biggest_stepsize;
    01370   }
    01371   assert(num_changing_childs == 0);
    01372
    01373   /* Third loop: Compute position and call the child. */
    01374   uint position = 0; // Place to put next child relative to origin of the container.
    01375   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01376     uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01377     uint child_height = child_wid->current_y;
    01378
    01379     child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
    01380     position += child_height + child_wid->padding_top + child_wid->padding_bottom;
    01381   }
    01382 }
    01383
    01389 NWidgetSpacer::NWidgetSpacer(int length, int height) : NWidgetResizeBase(NWID_SPACER, 0, 0)
    01390 {
    01391   this->SetMinimalSize(length, height);
    01392   this->SetResize(0, 0);
    01393 }
    01394
    01395 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
    01396 {
    01397   this->smallest_x = this->min_x;
    01398   this->smallest_y = this->min_y;
    01399 }
    01400
    01401 void NWidgetSpacer::FillNestedArray(NWidgetBase **array, uint length)
    01402 {
    01403 }
    01404
    01405 void NWidgetSpacer::Draw(const Window *w)
    01406 {
    01407   /* Spacer widget is never visible. */
    01408 }
    01409
    01410 void NWidgetSpacer::SetDirty(const Window *w) const
    01411 {
    01412   /* Spacer widget never need repainting. */
    01413 }
    01414
    01415 NWidgetCore *NWidgetSpacer::GetWidgetFromPos(int x, int y)
    01416 {
    01417   return NULL;
    01418 }
    01419
    01420 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX, NC_EQUALSIZE), index(-1), clicked(-1), count(-1)
    01421 {
    01422 }
    01423
    01424 void NWidgetMatrix::SetIndex(int index)
    01425 {
    01426   this->index = index;
    01427 }
    01428
    01429 void NWidgetMatrix::SetColour(Colours colour)
    01430 {
    01431   this->colour = colour;
    01432 }
    01433
    01438 void NWidgetMatrix::SetClicked(int clicked)
    01439 {
    01440   this->clicked = clicked;
    01441   if (this->clicked >= 0 && this->sb != NULL && this->widgets_x != 0) {
    01442     int vpos = (this->clicked / this->widgets_x) * this->widget_h; // Vertical position of the top.
    01443     /* Need to scroll down -> Scroll to the bottom.
    01444      * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
    01445     if (this->sb->GetPosition() < vpos) vpos += this->widget_h - this->pip_inter - 1;
    01446     this->sb->ScrollTowards(vpos);
    01447   }
    01448 }
    01449
    01455 void NWidgetMatrix::SetCount(int count)
    01456 {
    01457   this->count = count;
    01458
    01459   if (this->sb == NULL || this->widgets_x == 0) return;
    01460
    01461   /* We need to get the number of pixels the matrix is high/wide.
    01462    * So, determine the number of rows/columns based on the number of
    01463    * columns/rows (one is constant/unscrollable).
    01464    * Then multiply that by the height of a widget, and add the pre
    01465    * and post spacing "offsets". */
    01466   count = CeilDiv(count, this->sb->IsVertical() ? this->widgets_x : this->widgets_y);
    01467   count *= (this->sb->IsVertical() ? this->head->smallest_y : this->head->smallest_x) + this->pip_inter;
    01468   count += -this->pip_inter + this->pip_pre + this->pip_post; // We counted an inter too much in the multiplication above
    01469   this->sb->SetCount(count);
    01470   this->sb->SetCapacity(this->sb->IsVertical() ? this->current_y : this->current_x);
    01471   this->sb->SetStepSize(this->sb->IsVertical() ? this->widget_h  : this->widget_w);
    01472 }
    01473
    01478 void NWidgetMatrix::SetScrollbar(Scrollbar *sb)
    01479 {
    01480   this->sb = sb;
    01481 }
    01482
    01483 void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array)
    01484 {
    01485   assert(this->head != NULL);
    01486   assert(this->head->next == NULL);
    01487
    01488   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    01489     assert(w->nested_array_size > (uint)this->index);
    01490     w->nested_array[this->index] = this;
    01491   }
    01492
    01493   /* Reset the widget number. */
    01494   SB(dynamic_cast<NWidgetCore *>(this->head)->index, 16, 16, 0);
    01495   this->head->SetupSmallestSize(w, init_array);
    01496
    01497   Dimension padding = {this->pip_pre + this->pip_post, this->pip_pre + this->pip_post};
    01498   Dimension size    = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height};
    01499   Dimension fill    = {0, 0};
    01500   Dimension resize  = {this->pip_inter + this->head->smallest_x, this->pip_inter + this->head->smallest_y};
    01501
    01502   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    01503
    01504   this->smallest_x = size.width;
    01505   this->smallest_y = size.height;
    01506   this->fill_x = fill.width;
    01507   this->fill_y = fill.height;
    01508   this->resize_x = resize.width;
    01509   this->resize_y = resize.height;
    01510 }
    01511
    01512 void NWidgetMatrix::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01513 {
    01514   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01515
    01516   this->pos_x = x;
    01517   this->pos_y = y;
    01518   this->current_x = given_width;
    01519   this->current_y = given_height;
    01520
    01521   /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
    01522   this->widget_w = this->head->smallest_x + this->pip_inter;
    01523   this->widget_h = this->head->smallest_y + this->pip_inter;
    01524
    01525   /* Account for the pip_inter is between widgets, so we need to account for that when
    01526    * the division assumes pip_inter is used for all widgets. */
    01527   this->widgets_x = CeilDiv(this->current_x - this->pip_pre - this->pip_post + this->pip_inter, this->widget_w);
    01528   this->widgets_y = CeilDiv(this->current_y - this->pip_pre - this->pip_post + this->pip_inter, this->widget_h);
    01529
    01530   /* When resizing, update the scrollbar's count. E.g. with a vertical
    01531    * scrollbar becoming wider or narrower means the amount of rows in
    01532    * the scrollbar becomes respectively smaller or higher. */
    01533   this->SetCount(this->count);
    01534 }
    01535
    01536 void NWidgetMatrix::FillNestedArray(NWidgetBase **array, uint length)
    01537 {
    01538   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01539   NWidgetContainer::FillNestedArray(array, length);
    01540 }
    01541
    01542 NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y)
    01543 {
    01544   /* Falls outside of the matrix widget. */
    01545   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01546
    01547   int start_x, start_y, base_offs_x, base_offs_y;
    01548   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01549
    01550   /* Swap the x offset around for RTL, so it'll behave like LTR for RTL as well. */
    01551   bool rtl = _current_text_dir == TD_RTL;
    01552   if (rtl) base_offs_x -= (this->widgets_x - 1) * this->widget_w;
    01553
    01554   int widget_col = (x - base_offs_x - (int)this->pip_pre - (int)this->pos_x) / this->widget_w;
    01555   int widget_row = (y - base_offs_y - (int)this->pip_pre - (int)this->pos_y) / this->widget_h;
    01556
    01557   if (widget_row * this->widgets_x + widget_col >= this->count) return NULL;
    01558
    01559   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01560   child->AssignSizePosition(ST_RESIZE,
    01561       this->pos_x + this->pip_pre + widget_col * this->widget_w + base_offs_x,
    01562       this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y,
    01563       child->smallest_x, child->smallest_y, rtl);
    01564
    01565   /* "Undo" the RTL swap here to get the right widget index. */
    01566   if (rtl) widget_col = this->widgets_x - widget_col - 1;
    01567   SB(child->index, 16, 16, (widget_row + start_y) * this->widgets_x + widget_col + start_x);
    01568
    01569   return child->GetWidgetFromPos(x, y);
    01570 }
    01571
    01572 /* virtual */ void NWidgetMatrix::Draw(const Window *w)
    01573 {
    01574   /* Fill the background. */
    01575   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]);
    01576
    01577   /* Set up a clipping area for the previews. */
    01578   DrawPixelInfo tmp_dpi;
    01579   if (!FillDrawPixelInfo(&tmp_dpi, this->pos_x + this->pip_pre, this->pos_y + this->pip_pre, this->current_x - this->pip_pre - this->pip_post, this->current_y - this->pip_pre - this->pip_post)) return;
    01580   DrawPixelInfo *old_dpi = _cur_dpi;
    01581   _cur_dpi = &tmp_dpi;
    01582
    01583   /* Get the appropriate offsets so we can draw the right widgets. */
    01584   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01585   bool rtl = _current_text_dir == TD_RTL;
    01586   int start_x, start_y, base_offs_x, base_offs_y;
    01587   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01588
    01589   int offs_y = base_offs_y;
    01590   for (int y = start_y; y < start_y + this->widgets_y + 1; y++, offs_y += this->widget_h) {
    01591     /* Are we within bounds? */
    01592     if (offs_y + child->smallest_y <= 0) continue;
    01593     if (offs_y >= (int)this->current_y) break;
    01594
    01595     /* We've passed our amount of widgets. */
    01596     if (y * this->widgets_x >= this->count) break;
    01597
    01598     int offs_x = base_offs_x;
    01599     for (int x = start_x; x < start_x + this->widgets_x + 1; x++, offs_x += rtl ? -this->widget_w : this->widget_w) {
    01600       /* Are we within bounds? */
    01601       if (offs_x + child->smallest_x <= 0) continue;
    01602       if (offs_x >= (int)this->current_x) continue;
    01603
    01604       /* Do we have this many widgets? */
    01605       int sub_wid = y * this->widgets_x + x;
    01606       if (sub_wid >= this->count) break;
    01607
    01608       child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl);
    01609       child->SetLowered(this->clicked == sub_wid);
    01610       SB(child->index, 16, 16, sub_wid);
    01611       child->Draw(w);
    01612     }
    01613   }
    01614
    01615   /* Restore the clipping area. */
    01616   _cur_dpi = old_dpi;
    01617 }
    01618
    01626 void NWidgetMatrix::GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
    01627 {
    01628   base_offs_x = 0;
    01629   base_offs_y = 0;
    01630   start_x = 0;
    01631   start_y = 0;
    01632   if (this->sb != NULL) {
    01633     if (this->sb->IsVertical()) {
    01634       start_y = this->sb->GetPosition() / this->widget_h;
    01635       base_offs_y = -this->sb->GetPosition() + start_y * this->widget_h;
    01636       if (_current_text_dir == TD_RTL) base_offs_x = this->pip_pre + this->widget_w * (this->widgets_x - 1) - this->pip_inter;
    01637     } else {
    01638       start_x = this->sb->GetPosition() / this->widget_w;
    01639       if (_current_text_dir == TD_RTL) {
    01640         base_offs_x = this->sb->GetCapacity() + this->sb->GetPosition() - (start_x + 1) * this->widget_w + this->pip_inter - this->pip_post - this->pip_pre;
    01641       } else {
    01642         base_offs_x = -this->sb->GetPosition() + start_x * this->widget_w;
    01643       }
    01644     }
    01645   }
    01646 }
    01647
    01657 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
    01658 {
    01659   assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
    01660   if (index >= 0) this->SetIndex(index);
    01661   this->child = child;
    01662 }
    01663
    01664 NWidgetBackground::~NWidgetBackground()
    01665 {
    01666   if (this->child != NULL) delete this->child;
    01667 }
    01668
    01676 void NWidgetBackground::Add(NWidgetBase *nwid)
    01677 {
    01678   if (this->child == NULL) {
    01679     this->child = new NWidgetVertical();
    01680   }
    01681   this->child->Add(nwid);
    01682 }
    01683
    01694 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01695 {
    01696   if (this->child == NULL) {
    01697     this->child = new NWidgetVertical();
    01698   }
    01699   this->child->SetPIP(pip_pre, pip_inter, pip_post);
    01700 }
    01701
    01702 void NWidgetBackground::SetupSmallestSize(Window *w, bool init_array)
    01703 {
    01704   if (init_array && this->index >= 0) {
    01705     assert(w->nested_array_size > (uint)this->index);
    01706     w->nested_array[this->index] = this;
    01707   }
    01708   if (this->child != NULL) {
    01709     this->child->SetupSmallestSize(w, init_array);
    01710
    01711     this->smallest_x = this->child->smallest_x;
    01712     this->smallest_y = this->child->smallest_y;
    01713     this->fill_x = this->child->fill_x;
    01714     this->fill_y = this->child->fill_y;
    01715     this->resize_x = this->child->resize_x;
    01716     this->resize_y = this->child->resize_y;
    01717
    01718     /* Account for the size of the frame's text if that exists */
    01719     if (w != NULL && this->type == WWT_FRAME) {
    01720       this->child->padding_left   = WD_FRAMETEXT_LEFT;
    01721       this->child->padding_right  = WD_FRAMETEXT_RIGHT;
    01722       this->child->padding_top    = max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
    01723       this->child->padding_bottom = WD_FRAMETEXT_BOTTOM;
    01724
    01725       this->smallest_x += this->child->padding_left + this->child->padding_right;
    01726       this->smallest_y += this->child->padding_top + this->child->padding_bottom;
    01727
    01728       if (this->index >= 0) w->SetStringParameters(this->index);
    01729       this->smallest_x = max(this->smallest_x, GetStringBoundingBox(this->widget_data).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
    01730     }
    01731   } else {
    01732     Dimension d = {this->min_x, this->min_y};
    01733     Dimension fill = {this->fill_x, this->fill_y};
    01734     Dimension resize  = {this->resize_x, this->resize_y};
    01735     if (w != NULL) { // A non-NULL window pointer acts as switch to turn dynamic widget size on.
    01736       if (this->type == WWT_FRAME || this->type == WWT_INSET) {
    01737         if (this->index >= 0) w->SetStringParameters(this->index);
    01738         Dimension background = GetStringBoundingBox(this->widget_data);
    01739         background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
    01740         d = maxdim(d, background);
    01741       }
    01742       if (this->index >= 0) {
    01743         static const Dimension padding = {0, 0};
    01744         w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
    01745       }
    01746     }
    01747     this->smallest_x = d.width;
    01748     this->smallest_y = d.height;
    01749     this->fill_x = fill.width;
    01750     this->fill_y = fill.height;
    01751     this->resize_x = resize.width;
    01752     this->resize_y = resize.height;
    01753   }
    01754 }
    01755
    01756 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01757 {
    01758   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01759
    01760   if (this->child != NULL) {
    01761     uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
    01762     uint width = given_width - this->child->padding_right - this->child->padding_left;
    01763     uint height = given_height - this->child->padding_top - this->child->padding_bottom;
    01764     this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
    01765   }
    01766 }
    01767
    01768 void NWidgetBackground::FillNestedArray(NWidgetBase **array, uint length)
    01769 {
    01770   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01771   if (this->child != NULL) this->child->FillNestedArray(array, length);
    01772 }
    01773
    01774 void NWidgetBackground::Draw(const Window *w)
    01775 {
    01776   if (this->current_x == 0 || this->current_y == 0) return;
    01777
    01778   Rect r;
    01779   r.left = this->pos_x;
    01780   r.right = this->pos_x + this->current_x - 1;
    01781   r.top = this->pos_y;
    01782   r.bottom = this->pos_y + this->current_y - 1;
    01783
    01784   const DrawPixelInfo *dpi = _cur_dpi;
    01785   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01786
    01787   switch (this->type) {
    01788     case WWT_PANEL:
    01789       assert(this->widget_data == 0);
    01790       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
    01791       break;
    01792
    01793     case WWT_FRAME:
    01794       if (this->index >= 0) w->SetStringParameters(this->index);
    01795       DrawFrame(r, this->colour, this->widget_data);
    01796       break;
    01797
    01798     case WWT_INSET:
    01799       if (this->index >= 0) w->SetStringParameters(this->index);
    01800       DrawInset(r, this->colour, this->widget_data);
    01801       break;
    01802
    01803     default:
    01804       NOT_REACHED();
    01805   }
    01806
    01807   if (this->index >= 0) w->DrawWidget(r, this->index);
    01808   if (this->child != NULL) this->child->Draw(w);
    01809
    01810   if (this->IsDisabled()) {
    01811     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    01812   }
    01813 }
    01814
    01815 NWidgetCore *NWidgetBackground::GetWidgetFromPos(int x, int y)
    01816 {
    01817   NWidgetCore *nwid = NULL;
    01818   if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
    01819     if (this->child != NULL) nwid = this->child->GetWidgetFromPos(x, y);
    01820     if (nwid == NULL) nwid = this;
    01821   }
    01822   return nwid;
    01823 }
    01824
    01825 NWidgetBase *NWidgetBackground::GetWidgetOfType(WidgetType tp)
    01826 {
    01827   NWidgetBase *nwid = NULL;
    01828   if (this->child != NULL) nwid = this->child->GetWidgetOfType(tp);
    01829   if (nwid == NULL && this->type == tp) nwid = this;
    01830   return nwid;
    01831 }
    01832
    01833 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
    01834 {
    01835   this->SetIndex(index);
    01836 }
    01837
    01838 void NWidgetViewport::SetupSmallestSize(Window *w, bool init_array)
    01839 {
    01840   if (init_array && this->index >= 0) {
    01841     assert(w->nested_array_size > (uint)this->index);
    01842     w->nested_array[this->index] = this;
    01843   }
    01844   this->smallest_x = this->min_x;
    01845   this->smallest_y = this->min_y;
    01846 }
    01847
    01848 void NWidgetViewport::Draw(const Window *w)
    01849 {
    01850   if (this->disp_flags & ND_NO_TRANSPARENCY) {
    01851     TransparencyOptionBits to_backup = _transparency_opt;
    01852     _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_LOADING); // Disable all transparency, except textual stuff
    01853     w->DrawViewport();
    01854     _transparency_opt = to_backup;
    01855   } else {
    01856     w->DrawViewport();
    01857   }
    01858
    01859   /* Optionally shade the viewport. */
    01860   if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
    01861     GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
    01862         (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
    01863   }
    01864 }
    01865
    01872 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
    01873 {
    01874   InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
    01875 }
    01876
    01881 void NWidgetViewport::UpdateViewportCoordinates(Window *w)
    01882 {
    01883   ViewPort *vp = w->viewport;
    01884   if (vp != NULL) {
    01885     vp->left = w->left + this->pos_x;
    01886     vp->top  = w->top + this->pos_y;
    01887     vp->width  = this->current_x;
    01888     vp->height = this->current_y;
    01889
    01890     vp->virtual_width  = ScaleByZoom(vp->width, vp->zoom);
    01891     vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
    01892   }
    01893 }
    01894
    01904 int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
    01905 {
    01906   uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
    01907   if (pos != INT_MAX) pos += this->GetPosition();
    01908   return (pos >= this->GetCount()) ? INT_MAX : pos;
    01909 }
    01910
    01918 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
    01919 {
    01920   NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
    01921   if (this->IsVertical()) {
    01922     this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
    01923   } else {
    01924     this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
    01925   }
    01926 }
    01927
    01934 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL), Scrollbar(tp != NWID_HSCROLLBAR)
    01935 {
    01936   assert(tp == NWID_HSCROLLBAR || tp == NWID_VSCROLLBAR);
    01937   this->SetIndex(index);
    01938
    01939   switch (this->type) {
    01940     case NWID_HSCROLLBAR:
    01941       this->SetMinimalSize(0, NWidgetScrollbar::GetHorizontalDimension().height);
    01942       this->SetResize(1, 0);
    01943       this->SetFill(1, 0);
    01944       this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
    01945       break;
    01946
    01947     case NWID_VSCROLLBAR:
    01948       this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width, 0);
    01949       this->SetResize(0, 1);
    01950       this->SetFill(0, 1);
    01951       this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
    01952       break;
    01953
    01954     default: NOT_REACHED();
    01955   }
    01956 }
    01957
    01958 void NWidgetScrollbar::SetupSmallestSize(Window *w, bool init_array)
    01959 {
    01960   if (init_array && this->index >= 0) {
    01961     assert(w->nested_array_size > (uint)this->index);
    01962     w->nested_array[this->index] = this;
    01963   }
    01964   this->smallest_x = this->min_x;
    01965   this->smallest_y = this->min_y;
    01966 }
    01967
    01968 void NWidgetScrollbar::Draw(const Window *w)
    01969 {
    01970   if (this->current_x == 0 || this->current_y == 0) return;
    01971
    01972   Rect r;
    01973   r.left = this->pos_x;
    01974   r.right = this->pos_x + this->current_x - 1;
    01975   r.top = this->pos_y;
    01976   r.bottom = this->pos_y + this->current_y - 1;
    01977
    01978   const DrawPixelInfo *dpi = _cur_dpi;
    01979   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01980
    01981   bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
    01982   bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
    01983   bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->scrolling_scrollbar == this->index;
    01984
    01985   if (this->type == NWID_HSCROLLBAR) {
    01986     DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    01987   } else {
    01988     DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    01989   }
    01990
    01991   if (this->IsDisabled()) {
    01992     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    01993   }
    01994 }
    01995
    01996 /* static */ void NWidgetScrollbar::InvalidateDimensionCache()
    01997 {
    01998   vertical_dimension.width   = vertical_dimension.height   = 0;
    01999   horizontal_dimension.width = horizontal_dimension.height = 0;
    02000 }
    02001
    02002 /* static */ Dimension NWidgetScrollbar::GetVerticalDimension()
    02003 {
    02004   static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
    02005   if (vertical_dimension.width == 0) {
    02006     vertical_dimension = maxdim(GetSpriteSize(SPR_ARROW_UP), GetSpriteSize(SPR_ARROW_DOWN));
    02007     vertical_dimension.width += extra.width;
    02008     vertical_dimension.height += extra.height;
    02009   }
    02010   return vertical_dimension;
    02011 }
    02012
    02013 /* static */ Dimension NWidgetScrollbar::GetHorizontalDimension()
    02014 {
    02015   static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
    02016   if (horizontal_dimension.width == 0) {
    02017     horizontal_dimension = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    02018     horizontal_dimension.width += extra.width;
    02019     horizontal_dimension.height += extra.height;
    02020   }
    02021   return horizontal_dimension;
    02022 }
    02023
    02024 Dimension NWidgetScrollbar::vertical_dimension = {0, 0};
    02025 Dimension NWidgetScrollbar::horizontal_dimension = {0, 0};
    02026
    02028 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
    02029 {
    02030   shadebox_dimension.width  = shadebox_dimension.height  = 0;
    02031   debugbox_dimension.width  = debugbox_dimension.height  = 0;
    02032   stickybox_dimension.width = stickybox_dimension.height = 0;
    02033   resizebox_dimension.width = resizebox_dimension.height = 0;
    02034   closebox_dimension.width  = closebox_dimension.height  = 0;
    02035 }
    02036
    02037 Dimension NWidgetLeaf::shadebox_dimension  = {0, 0};
    02038 Dimension NWidgetLeaf::debugbox_dimension  = {0, 0};
    02039 Dimension NWidgetLeaf::stickybox_dimension = {0, 0};
    02040 Dimension NWidgetLeaf::resizebox_dimension = {0, 0};
    02041 Dimension NWidgetLeaf::closebox_dimension  = {0, 0};
    02042
    02051 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint16 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
    02052 {
    02053   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);
    02054   if (index >= 0) this->SetIndex(index);
    02055   this->SetMinimalSize(0, 0);
    02056   this->SetResize(0, 0);
    02057
    02058   switch (tp) {
    02059     case WWT_EMPTY:
    02060       break;
    02061
    02062     case WWT_PUSHBTN:
    02063     case WWT_IMGBTN:
    02064     case WWT_PUSHIMGBTN:
    02065     case WWT_IMGBTN_2:
    02066     case WWT_TEXTBTN:
    02067     case WWT_PUSHTXTBTN:
    02068     case WWT_TEXTBTN_2:
    02069     case WWT_LABEL:
    02070     case WWT_TEXT:
    02071     case WWT_MATRIX:
    02072     case NWID_BUTTON_DROPDOWN:
    02073     case NWID_PUSHBUTTON_DROPDOWN:
    02074     case WWT_ARROWBTN:
    02075     case WWT_PUSHARROWBTN:
    02076       this->SetFill(0, 0);
    02077       break;
    02078
    02079     case WWT_EDITBOX:
    02080       this->SetMinimalSize(10, 0);
    02081       this->SetFill(0, 0);
    02082       break;
    02083
    02084     case WWT_CAPTION:
    02085       this->SetFill(1, 0);
    02086       this->SetResize(1, 0);
    02087       this->min_y = WD_CAPTION_HEIGHT;
    02088       this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
    02089       break;
    02090
    02091     case WWT_STICKYBOX:
    02092       this->SetFill(0, 0);
    02093       this->SetMinimalSize(WD_STICKYBOX_WIDTH, WD_CAPTION_HEIGHT);
    02094       this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
    02095       break;
    02096
    02097     case WWT_SHADEBOX:
    02098       this->SetFill(0, 0);
    02099       this->SetMinimalSize(WD_SHADEBOX_TOP, WD_CAPTION_HEIGHT);
    02100       this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
    02101       break;
    02102
    02103     case WWT_DEBUGBOX:
    02104       this->SetFill(0, 0);
    02105       this->SetMinimalSize(WD_DEBUGBOX_TOP, WD_CAPTION_HEIGHT);
    02106       this->SetDataTip(STR_NULL, STR_TOOLTIP_DEBUG);
    02107       break;
    02108
    02109     case WWT_RESIZEBOX:
    02110       this->SetFill(0, 0);
    02111       this->SetMinimalSize(WD_RESIZEBOX_WIDTH, 12);
    02112       this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
    02113       break;
    02114
    02115     case WWT_CLOSEBOX:
    02116       this->SetFill(0, 0);
    02117       this->SetMinimalSize(WD_CLOSEBOX_WIDTH, WD_CAPTION_HEIGHT);
    02118       this->SetDataTip(STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW);
    02119       break;
    02120
    02121     case WWT_DROPDOWN:
    02122       this->SetFill(0, 0);
    02123       this->min_y = WD_DROPDOWN_HEIGHT;
    02124       break;
    02125
    02126     default:
    02127       NOT_REACHED();
    02128   }
    02129 }
    02130
    02131 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
    02132 {
    02133   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    02134     assert(w->nested_array_size > (uint)this->index);
    02135     w->nested_array[this->index] = this;
    02136   }
    02137
    02138   Dimension size = {this->min_x, this->min_y};
    02139   Dimension fill = {this->fill_x, this->fill_y};
    02140   Dimension resize = {this->resize_x, this->resize_y};
    02141   /* Get padding, and update size with the real content size if appropriate. */
    02142   const Dimension *padding = NULL;
    02143   switch (this->type) {
    02144     case WWT_EMPTY: {
    02145       static const Dimension extra = {0, 0};
    02146       padding = &extra;
    02147       break;
    02148     }
    02149     case WWT_MATRIX: {
    02150       static const Dimension extra = {WD_MATRIX_LEFT + WD_MATRIX_RIGHT, WD_MATRIX_TOP + WD_MATRIX_BOTTOM};
    02151       padding = &extra;
    02152       break;
    02153     }
    02154     case WWT_SHADEBOX: {
    02155       static const Dimension extra = {WD_SHADEBOX_LEFT + WD_SHADEBOX_RIGHT, WD_SHADEBOX_TOP + WD_SHADEBOX_BOTTOM};
    02156       padding = &extra;
    02157       if (NWidgetLeaf::shadebox_dimension.width == 0) {
    02158         NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
    02159         NWidgetLeaf::shadebox_dimension.width += extra.width;
    02160         NWidgetLeaf::shadebox_dimension.height += extra.height;
    02161       }
    02162       size = maxdim(size, NWidgetLeaf::shadebox_dimension);
    02163       break;
    02164     }
    02165     case WWT_DEBUGBOX:
    02166       if (_settings_client.gui.newgrf_developer_tools && w->IsNewGRFInspectable()) {
    02167         static const Dimension extra = {WD_DEBUGBOX_LEFT + WD_DEBUGBOX_RIGHT, WD_DEBUGBOX_TOP + WD_DEBUGBOX_BOTTOM};
    02168         padding = &extra;
    02169         if (NWidgetLeaf::debugbox_dimension.width == 0) {
    02170           NWidgetLeaf::debugbox_dimension = GetSpriteSize(SPR_WINDOW_DEBUG);
    02171           NWidgetLeaf::debugbox_dimension.width += extra.width;
    02172           NWidgetLeaf::debugbox_dimension.height += extra.height;
    02173         }
    02174         size = maxdim(size, NWidgetLeaf::debugbox_dimension);
    02175       } else {
    02176         /* If the setting is disabled we don't want to see it! */
    02177         size.width = 0;
    02178         fill.width = 0;
    02179         resize.width = 0;
    02180       }
    02181       break;
    02182
    02183     case WWT_STICKYBOX: {
    02184       static const Dimension extra = {WD_STICKYBOX_LEFT + WD_STICKYBOX_RIGHT, WD_STICKYBOX_TOP + WD_STICKYBOX_BOTTOM};
    02185       padding = &extra;
    02186       if (NWidgetLeaf::stickybox_dimension.width == 0) {
    02187         NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
    02188         NWidgetLeaf::stickybox_dimension.width += extra.width;
    02189         NWidgetLeaf::stickybox_dimension.height += extra.height;
    02190       }
    02191       size = maxdim(size, NWidgetLeaf::stickybox_dimension);
    02192       break;
    02193     }
    02194     case WWT_RESIZEBOX: {
    02195       static const Dimension extra = {WD_RESIZEBOX_LEFT + WD_RESIZEBOX_RIGHT, WD_RESIZEBOX_TOP + WD_RESIZEBOX_BOTTOM};
    02196       padding = &extra;
    02197       if (NWidgetLeaf::resizebox_dimension.width == 0) {
    02198         NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
    02199         NWidgetLeaf::resizebox_dimension.width += extra.width;
    02200         NWidgetLeaf::resizebox_dimension.height += extra.height;
    02201       }
    02202       size = maxdim(size, NWidgetLeaf::resizebox_dimension);
    02203       break;
    02204     }
    02205     case WWT_EDITBOX:
    02206       size.height = max(size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    02207       /* FALL THROUGH */
    02208     case WWT_PUSHBTN: {
    02209       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02210       padding = &extra;
    02211       break;
    02212     }
    02213     case WWT_IMGBTN:
    02214     case WWT_IMGBTN_2:
    02215     case WWT_PUSHIMGBTN: {
    02216       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02217       padding = &extra;
    02218       Dimension d2 = GetSpriteSize(this->widget_data);
    02219       if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
    02220       d2.width += extra.width;
    02221       d2.height += extra.height;
    02222       size = maxdim(size, d2);
    02223       break;
    02224     }
    02225     case WWT_ARROWBTN:
    02226     case WWT_PUSHARROWBTN: {
    02227       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02228       padding = &extra;
    02229       Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    02230       d2.width += extra.width;
    02231       d2.height += extra.height;
    02232       size = maxdim(size, d2);
    02233       break;
    02234     }
    02235
    02236     case WWT_CLOSEBOX: {
    02237       static const Dimension extra = {WD_CLOSEBOX_LEFT + WD_CLOSEBOX_RIGHT, WD_CLOSEBOX_TOP + WD_CLOSEBOX_BOTTOM};
    02238       padding = &extra;
    02239       if (NWidgetLeaf::closebox_dimension.width == 0) {
    02240         NWidgetLeaf::closebox_dimension = maxdim(GetStringBoundingBox(STR_BLACK_CROSS), GetStringBoundingBox(STR_SILVER_CROSS));
    02241         NWidgetLeaf::closebox_dimension.width += extra.width;
    02242         NWidgetLeaf::closebox_dimension.height += extra.height;
    02243       }
    02244       size = maxdim(size, NWidgetLeaf::closebox_dimension);
    02245       break;
    02246     }
    02247     case WWT_TEXTBTN:
    02248     case WWT_PUSHTXTBTN:
    02249     case WWT_TEXTBTN_2: {
    02250       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT,  WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02251       padding = &extra;
    02252       if (this->index >= 0) w->SetStringParameters(this->index);
    02253       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02254       d2.width += extra.width;
    02255       d2.height += extra.height;
    02256       size = maxdim(size, d2);
    02257       break;
    02258     }
    02259     case WWT_LABEL:
    02260     case WWT_TEXT: {
    02261       static const Dimension extra = {0, 0};
    02262       padding = &extra;
    02263       if (this->index >= 0) w->SetStringParameters(this->index);
    02264       size = maxdim(size, GetStringBoundingBox(this->widget_data));
    02265       break;
    02266     }
    02267     case WWT_CAPTION: {
    02268       static const Dimension extra = {WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT, WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM};
    02269       padding = &extra;
    02270       if (this->index >= 0) w->SetStringParameters(this->index);
    02271       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02272       d2.width += extra.width;
    02273       d2.height += extra.height;
    02274       size = maxdim(size, d2);
    02275       break;
    02276     }
    02277     case WWT_DROPDOWN:
    02278     case NWID_BUTTON_DROPDOWN:
    02279     case NWID_PUSHBUTTON_DROPDOWN: {
    02280       static const Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
    02281       padding = &extra;
    02282       if (this->index >= 0) w->SetStringParameters(this->index);
    02283       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02284       d2.width += extra.width;
    02285       d2.height += extra.height;
    02286       size = maxdim(size, d2);
    02287       break;
    02288     }
    02289     default:
    02290       NOT_REACHED();
    02291   }
    02292
    02293   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
    02294
    02295   this->smallest_x = size.width;
    02296   this->smallest_y = size.height;
    02297   this->fill_x = fill.width;
    02298   this->fill_y = fill.height;
    02299   this->resize_x = resize.width;
    02300   this->resize_y = resize.height;
    02301 }
    02302
    02303 void NWidgetLeaf::Draw(const Window *w)
    02304 {
    02305   if (this->current_x == 0 || this->current_y == 0) return;
    02306
    02307   Rect r;
    02308   r.left = this->pos_x;
    02309   r.right = this->pos_x + this->current_x - 1;
    02310   r.top = this->pos_y;
    02311   r.bottom = this->pos_y + this->current_y - 1;
    02312
    02313   const DrawPixelInfo *dpi = _cur_dpi;
    02314   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    02315
    02316   bool clicked = this->IsLowered();
    02317   switch (this->type) {
    02318     case WWT_EMPTY:
    02319       break;
    02320
    02321     case WWT_PUSHBTN:
    02322       assert(this->widget_data == 0);
    02323       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02324       break;
    02325
    02326     case WWT_IMGBTN:
    02327     case WWT_PUSHIMGBTN:
    02328     case WWT_IMGBTN_2:
    02329       DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
    02330       break;
    02331
    02332     case WWT_TEXTBTN:
    02333     case WWT_PUSHTXTBTN:
    02334     case WWT_TEXTBTN_2:
    02335       if (this->index >= 0) w->SetStringParameters(this->index);
    02336       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02337       DrawLabel(r, this->type, clicked, this->widget_data);
    02338       break;
    02339
    02340     case WWT_ARROWBTN:
    02341     case WWT_PUSHARROWBTN: {
    02342       SpriteID sprite;
    02343       switch (this->widget_data) {
    02344         case AWV_DECREASE: sprite = _current_text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02345         case AWV_INCREASE: sprite = _current_text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02346         case AWV_LEFT:     sprite = SPR_ARROW_LEFT;  break;
    02347         case AWV_RIGHT:    sprite = SPR_ARROW_RIGHT; break;
    02348         default: NOT_REACHED();
    02349       }
    02350       DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
    02351     }
    02352
    02353     case WWT_LABEL:
    02354       if (this->index >= 0) w->SetStringParameters(this->index);
    02355       DrawLabel(r, this->type, clicked, this->widget_data);
    02356       break;
    02357
    02358     case WWT_TEXT:
    02359       if (this->index >= 0) w->SetStringParameters(this->index);
    02360       DrawText(r, (TextColour)this->colour, this->widget_data);
    02361       break;
    02362
    02363     case WWT_MATRIX:
    02364       DrawMatrix(r, this->colour, clicked, this->widget_data);
    02365       break;
    02366
    02367     case WWT_EDITBOX:
    02368       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, FR_LOWERED | FR_DARKENED);
    02369       break;
    02370
    02371     case WWT_CAPTION:
    02372       if (this->index >= 0) w->SetStringParameters(this->index);
    02373       DrawCaption(r, this->colour, w->owner, this->widget_data);
    02374       break;
    02375
    02376     case WWT_SHADEBOX:
    02377       assert(this->widget_data == 0);
    02378       DrawShadeBox(r, this->colour, w->IsShaded());
    02379       break;
    02380
    02381     case WWT_DEBUGBOX:
    02382       DrawDebugBox(r, this->colour, clicked);
    02383       break;
    02384
    02385     case WWT_STICKYBOX:
    02386       assert(this->widget_data == 0);
    02387       DrawStickyBox(r, this->colour, !!(w->flags & WF_STICKY));
    02388       break;
    02389
    02390     case WWT_RESIZEBOX:
    02391       assert(this->widget_data == 0);
    02392       DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags & WF_SIZING));
    02393       break;
    02394
    02395     case WWT_CLOSEBOX:
    02396       DrawCloseBox(r, this->colour, this->widget_data);
    02397       break;
    02398
    02399     case WWT_DROPDOWN:
    02400       if (this->index >= 0) w->SetStringParameters(this->index);
    02401       DrawDropdown(r, this->colour, clicked, this->widget_data);
    02402       break;
    02403
    02404     case NWID_BUTTON_DROPDOWN:
    02405     case NWID_PUSHBUTTON_DROPDOWN:
    02406       if (this->index >= 0) w->SetStringParameters(this->index);
    02407       DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
    02408       break;
    02409
    02410     default:
    02411       NOT_REACHED();
    02412   }
    02413   if (this->index >= 0) w->DrawWidget(r, this->index);
    02414
    02415   if (this->IsDisabled()) {
    02416     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    02417   }
    02418 }
    02419
    02427 bool NWidgetLeaf::ButtonHit(const Point &pt)
    02428 {
    02429   if (_current_text_dir == TD_LTR) {
    02430     int button_width = this->pos_x + this->current_x - 12;
    02431     return pt.x < button_width;
    02432   } else {
    02433     int button_left = this->pos_x + 12;
    02434     return pt.x >= button_left;
    02435   }
    02436 }
    02437
    02438 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
    02439
    02455 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
    02456 {
    02457   int num_used = 0;
    02458
    02459   *dest = NULL;
    02460   *fill_dest = false;
    02461
    02462   while (count > num_used) {
    02463     switch (parts->type) {
    02464       case NWID_SPACER:
    02465         if (*dest != NULL) return num_used;
    02466         *dest = new NWidgetSpacer(0, 0);
    02467         break;
    02468
    02469       case NWID_HORIZONTAL:
    02470         if (*dest != NULL) return num_used;
    02471         *dest = new NWidgetHorizontal(parts->u.cont_flags);
    02472         *fill_dest = true;
    02473         break;
    02474
    02475       case NWID_HORIZONTAL_LTR:
    02476         if (*dest != NULL) return num_used;
    02477         *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
    02478         *fill_dest = true;
    02479         break;
    02480
    02481       case WWT_PANEL:
    02482       case WWT_INSET:
    02483       case WWT_FRAME:
    02484         if (*dest != NULL) return num_used;
    02485         *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02486         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02487         *fill_dest = true;
    02488         break;
    02489
    02490       case NWID_VERTICAL:
    02491         if (*dest != NULL) return num_used;
    02492         *dest = new NWidgetVertical(parts->u.cont_flags);
    02493         *fill_dest = true;
    02494         break;
    02495
    02496       case NWID_MATRIX: {
    02497         if (*dest != NULL) return num_used;
    02498         NWidgetMatrix *nwm = new NWidgetMatrix();
    02499         *dest = nwm;
    02500         *fill_dest = true;
    02501         nwm->SetIndex(parts->u.widget.index);
    02502         nwm->SetColour(parts->u.widget.colour);
    02503         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02504         break;
    02505       }
    02506
    02507       case WPT_FUNCTION: {
    02508         if (*dest != NULL) return num_used;
    02509         /* Ensure proper functioning even when the called code simply writes its largest index. */
    02510         int biggest = -1;
    02511         *dest = parts->u.func_ptr(&biggest);
    02512         *biggest_index = max(*biggest_index, biggest);
    02513         *fill_dest = false;
    02514         break;
    02515       }
    02516
    02517       case WPT_RESIZE: {
    02518         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02519         if (nwrb != NULL) {
    02520           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02521           nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
    02522         }
    02523         break;
    02524       }
    02525
    02526       case WPT_MINSIZE: {
    02527         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02528         if (nwrb != NULL) {
    02529           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02530           nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
    02531         }
    02532         break;
    02533       }
    02534
    02535       case WPT_MINTEXTLINES: {
    02536         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02537         if (nwrb != NULL) {
    02538           assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
    02539           nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
    02540         }
    02541         break;
    02542       }
    02543
    02544       case WPT_FILL: {
    02545         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02546         if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
    02547         break;
    02548       }
    02549
    02550       case WPT_DATATIP: {
    02551         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02552         if (nwc != NULL) {
    02553           nwc->widget_data = parts->u.data_tip.data;
    02554           nwc->tool_tip = parts->u.data_tip.tooltip;
    02555         }
    02556         break;
    02557       }
    02558
    02559       case WPT_PADDING:
    02560         if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
    02561         break;
    02562
    02563       case WPT_PIPSPACE: {
    02564         NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
    02565         if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02566
    02567         NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
    02568         if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02569         break;
    02570       }
    02571
    02572       case WPT_SCROLLBAR: {
    02573         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02574         if (nwc != NULL) {
    02575           nwc->scrollbar_index = parts->u.widget.index;
    02576         }
    02577         break;
    02578       }
    02579
    02580       case WPT_ENDCONTAINER:
    02581         return num_used;
    02582
    02583       case NWID_VIEWPORT:
    02584         if (*dest != NULL) return num_used;
    02585         *dest = new NWidgetViewport(parts->u.widget.index);
    02586         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02587         break;
    02588
    02589       case NWID_HSCROLLBAR:
    02590       case NWID_VSCROLLBAR:
    02591         if (*dest != NULL) return num_used;
    02592         *dest = new NWidgetScrollbar(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02593         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02594         break;
    02595
    02596       case NWID_SELECTION: {
    02597         if (*dest != NULL) return num_used;
    02598         NWidgetStacked *nws = new NWidgetStacked();
    02599         *dest = nws;
    02600         *fill_dest = true;
    02601         nws->SetIndex(parts->u.widget.index);
    02602         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02603         break;
    02604       }
    02605
    02606       default:
    02607         if (*dest != NULL) return num_used;
    02608         assert((parts->type & WWT_MASK) < WWT_LAST || (parts->type & WWT_MASK) == NWID_BUTTON_DROPDOWN);
    02609         *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
    02610         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02611         break;
    02612     }
    02613     num_used++;
    02614     parts++;
    02615   }
    02616
    02617   return num_used;
    02618 }
    02619
    02629 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
    02630 {
    02631   /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
    02632    * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
    02633   NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
    02634   NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
    02635   assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
    02636
    02637   int total_used = 0;
    02638   for (;;) {
    02639     NWidgetBase *sub_widget = NULL;
    02640     bool fill_sub = false;
    02641     int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
    02642     parts += num_used;
    02643     total_used += num_used;
    02644
    02645     /* Break out of loop when end reached */
    02646     if (sub_widget == NULL) break;
    02647
    02648     /* If sub-widget is a container, recursively fill that container. */
    02649     WidgetType tp = sub_widget->type;
    02650     if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
    02651               || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
    02652       NWidgetBase *sub_ptr = sub_widget;
    02653       int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
    02654       parts += num_used;
    02655       total_used += num_used;
    02656     }
    02657
    02658     /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
    02659     if (nwid_cont != NULL) nwid_cont->Add(sub_widget);
    02660     if (nwid_parent != NULL) nwid_parent->Add(sub_widget);
    02661     if (nwid_cont == NULL && nwid_parent == NULL) {
    02662       *parent = sub_widget;
    02663       return total_used;
    02664     }
    02665   }
    02666
    02667   if (count == total_used) return total_used; // Reached the end of the array of parts?
    02668
    02669   assert(total_used < count);
    02670   assert(parts->type == WPT_ENDCONTAINER);
    02671   return total_used + 1; // *parts is also 'used'
    02672 }
    02673
    02685 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
    02686 {
    02687   *biggest_index = -1;
    02688   if (container == NULL) container = new NWidgetVertical();
    02689   NWidgetBase *cont_ptr = container;
    02690   MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
    02691   return container;
    02692 }
    02693
    02707 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
    02708 {
    02709   *biggest_index = -1;
    02710
    02711   /* Read the first widget recursively from the array. */
    02712   NWidgetBase *nwid = NULL;
    02713   int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
    02714   assert(nwid != NULL);
    02715   parts += num_used;
    02716   count -= num_used;
    02717
    02718   NWidgetContainer *root = new NWidgetVertical;
    02719   root->Add(nwid);
    02720   if (count == 0) { // There is no body at all.
    02721     *shade_select = NULL;
    02722     return root;
    02723   }
    02724
    02725   /* If the first widget looks like a titlebar, treat it as such.
    02726    * If it has a shading box, silently add a shade selection widget in the tree. */
    02727   NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
    02728   NWidgetContainer *body;
    02729   if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
    02730     *shade_select = new NWidgetStacked;
    02731     root->Add(*shade_select);
    02732     body = new NWidgetVertical;
    02733     (*shade_select)->Add(body);
    02734   } else {
    02735     *shade_select = NULL;
    02736     body = root;
    02737   }
    02738
    02739   /* Load the remaining parts into 'body'. */
    02740   int biggest2 = -1;
    02741   MakeNWidgets(parts, count, &biggest2, body);
    02742
    02743   *biggest_index = max(*biggest_index, biggest2);
    02744   return root;
    02745 }
    02746
    02757 NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
    02758 {
    02759   NWidgetVertical *vert = NULL; // Storage for all rows.
    02760   NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
    02761   int hor_length = 0;
    02762
    02763   Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
    02764   sprite_size.width  += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
    02765   sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
    02766
    02767   for (int widnum = widget_first; widnum <= widget_last; widnum++) {
    02768     /* Ensure there is room in 'hor' for another button. */
    02769     if (hor_length == max_length) {
    02770       if (vert == NULL) vert = new NWidgetVertical();
    02771       vert->Add(hor);
    02772       hor = NULL;
    02773       hor_length = 0;
    02774     }
    02775     if (hor == NULL) {
    02776       hor = new NWidgetHorizontal();
    02777       hor_length = 0;
    02778     }
    02779
    02780     NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
    02781     panel->SetMinimalSize(sprite_size.width, sprite_size.height);
    02782     panel->SetFill(1, 0);
    02783     panel->SetResize(1, 0);
    02784     panel->SetDataTip(0x0, button_tooltip);
    02785     hor->Add(panel);
    02786     hor_length++;
    02787   }
    02788   *biggest_index = widget_last;
    02789   if (vert == NULL) return hor; // All buttons fit in a single row.
    02790
    02791   if (hor_length > 0 && hor_length < max_length) {
    02792     /* Last row is partial, add a spacer at the end to force all buttons to the left. */
    02793     NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
    02794     spc->SetFill(1, 0);
    02795     spc->SetResize(1, 0);
    02796     hor->Add(spc);
    02797   }
    02798   if (hor != NULL) vert->Add(hor);
    02799   return vert;
    02800 }