# 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
    00025 static const char *UPARROW   = "\xEE\x8A\xA0";
    00026 static const char *DOWNARROW = "\xEE\x8A\xAA";
    00027
    00037 static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bool horizontal)
    00038 {
    00039   /* Base for reversion */
    00040   int rev_base = top + bottom;
    00041   top += 10;   // top    points to just below the up-button
    00042   bottom -= 9; // bottom points to top of the down-button
    00043
    00044   int height = (bottom - top);
    00045   int pos = sb->GetPosition();
    00046   int count = sb->GetCount();
    00047   int cap = sb->GetCapacity();
    00048
    00049   if (count != 0) top += height * pos / count;
    00050
    00051   if (cap > count) cap = count;
    00052   if (count != 0) bottom -= (count - pos - cap) * height / count;
    00053
    00054   Point pt;
    00055   if (horizontal && _current_text_dir == TD_RTL) {
    00056     pt.x = rev_base - (bottom - 1);
    00057     pt.y = rev_base - top;
    00058   } else {
    00059     pt.x = top;
    00060     pt.y = bottom - 1;
    00061   }
    00062   return pt;
    00063 }
    00064
    00074 static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma)
    00075 {
    00076   int pos;
    00077   bool rtl = false;
    00078
    00079   if (sb->type == NWID_HSCROLLBAR) {
    00080     pos = x;
    00081     rtl = _current_text_dir == TD_RTL;
    00082   } else {
    00083     pos = y;
    00084   }
    00085   if (pos <= mi + 9) {
    00086     /* Pressing the upper button? */
    00087     SetBit(sb->disp_flags, NDB_SCROLLBAR_UP);
    00088     if (_scroller_click_timeout <= 1) {
    00089       _scroller_click_timeout = 3;
    00090       sb->UpdatePosition(rtl ? 1 : -1);
    00091     }
    00092     w->scrolling_scrollbar = sb->index;
    00093   } else if (pos >= ma - 10) {
    00094     /* Pressing the lower button? */
    00095     SetBit(sb->disp_flags, NDB_SCROLLBAR_DOWN);
    00096
    00097     if (_scroller_click_timeout <= 1) {
    00098       _scroller_click_timeout = 3;
    00099       sb->UpdatePosition(rtl ? -1 : 1);
    00100     }
    00101     w->scrolling_scrollbar = sb->index;
    00102   } else {
    00103     Point pt = HandleScrollbarHittest(sb, mi, ma, sb->type == NWID_HSCROLLBAR);
    00104
    00105     if (pos < pt.x) {
    00106       sb->UpdatePosition(rtl ? 1 : -1, Scrollbar::SS_BIG);
    00107     } else if (pos > pt.y) {
    00108       sb->UpdatePosition(rtl ? -1 : 1, Scrollbar::SS_BIG);
    00109     } else {
    00110       _scrollbar_start_pos = pt.x - mi - 9;
    00111       _scrollbar_size = ma - mi - 23;
    00112       w->scrolling_scrollbar = sb->index;
    00113       _cursorpos_drag_start = _cursor.pos;
    00114     }
    00115   }
    00116
    00117   w->SetDirty();
    00118 }
    00119
    00128 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
    00129 {
    00130   int mi, ma;
    00131
    00132   if (nw->type == NWID_HSCROLLBAR) {
    00133     mi = nw->pos_x;
    00134     ma = nw->pos_x + nw->current_x;
    00135   } else {
    00136     mi = nw->pos_y;
    00137     ma = nw->pos_y + nw->current_y;
    00138   }
    00139   ScrollbarClickPositioning(w, dynamic_cast<NWidgetScrollbar*>(nw), x, y, mi, ma);
    00140 }
    00141
    00150 int GetWidgetFromPos(const Window *w, int x, int y)
    00151 {
    00152   NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
    00153   return (nw != NULL) ? nw->index : -1;
    00154 }
    00155
    00165 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
    00166 {
    00167   uint dark         = _colour_gradient[colour][3];
    00168   uint medium_dark  = _colour_gradient[colour][5];
    00169   uint medium_light = _colour_gradient[colour][6];
    00170   uint light        = _colour_gradient[colour][7];
    00171
    00172   if (flags & FR_TRANSPARENT) {
    00173     GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR);
    00174   } else {
    00175     uint interior;
    00176
    00177     if (flags & FR_LOWERED) {
    00178       GfxFillRect(left,     top,     left,  bottom,     dark);
    00179       GfxFillRect(left + 1, top,     right, top,        dark);
    00180       GfxFillRect(right,    top + 1, right, bottom - 1, light);
    00181       GfxFillRect(left + 1, bottom,  right, bottom,     light);
    00182       interior = (flags & FR_DARKENED ? medium_dark : medium_light);
    00183     } else {
    00184       GfxFillRect(left,     top,    left,      bottom - 1, light);
    00185       GfxFillRect(left + 1, top,    right - 1, top,        light);
    00186       GfxFillRect(right,    top,    right,     bottom - 1, dark);
    00187       GfxFillRect(left,     bottom, right,     bottom,     dark);
    00188       interior = medium_dark;
    00189     }
    00190     if (!(flags & FR_BORDERONLY)) {
    00191       GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, interior);
    00192     }
    00193   }
    00194 }
    00195
    00204 static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
    00205 {
    00206   assert(img != 0);
    00207   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00208
    00209   if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2.
    00210   DrawSprite(img, PAL_NONE, r.left + WD_IMGBTN_LEFT + clicked, r.top + WD_IMGBTN_TOP + clicked);
    00211 }
    00212
    00220 static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
    00221 {
    00222   if (str == STR_NULL) return;
    00223   if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
    00224   Dimension d = GetStringBoundingBox(str);
    00225   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00226   DrawString(r.left + clicked, r.right + clicked, r.top + offset + clicked, str, TC_FROMSTRING, SA_HOR_CENTER);
    00227 }
    00228
    00235 static inline void DrawText(const Rect &r, TextColour colour, StringID str)
    00236 {
    00237   Dimension d = GetStringBoundingBox(str);
    00238   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00239   if (str != STR_NULL) DrawString(r.left, r.right, r.top + offset, str, colour);
    00240 }
    00241
    00248 static inline void DrawInset(const Rect &r, Colours colour, StringID str)
    00249 {
    00250   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
    00251   if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str);
    00252 }
    00253
    00261 static inline void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint16 data)
    00262 {
    00263   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00264
    00265   int num_columns = GB(data, MAT_COL_START, MAT_COL_BITS);  // Lower 8 bits of the widget data: Number of columns in the matrix.
    00266   int column_width = (r.right - r.left + 1) / num_columns; // Width of a single column in the matrix.
    00267
    00268   int num_rows = GB(data, MAT_ROW_START, MAT_ROW_BITS); // Upper 8 bits of the widget data: Number of rows in the matrix.
    00269   int row_height = (r.bottom - r.top + 1) / num_rows; // Height of a single row in the matrix.
    00270
    00271   int col = _colour_gradient[colour & 0xF][6];
    00272
    00273   int x = r.left;
    00274   for (int ctr = num_columns; ctr > 1; ctr--) {
    00275     x += column_width;
    00276     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00277   }
    00278
    00279   x = r.top;
    00280   for (int ctr = num_rows; ctr > 1; ctr--) {
    00281     x += row_height;
    00282     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00283   }
    00284
    00285   col = _colour_gradient[colour & 0xF][4];
    00286
    00287   x = r.left - 1;
    00288   for (int ctr = num_columns; ctr > 1; ctr--) {
    00289     x += column_width;
    00290     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00291   }
    00292
    00293   x = r.top - 1;
    00294   for (int ctr = num_rows; ctr > 1; ctr--) {
    00295     x += row_height;
    00296     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00297   }
    00298 }
    00299
    00309 static inline void DrawVerticalScrollbar(const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
    00310 {
    00311   /* draw up/down buttons */
    00312   DrawFrameRect(r.left, r.top, r.right, r.top + 9, colour, (up_clicked) ? FR_LOWERED : FR_NONE);
    00313   DrawString(r.left + up_clicked, r.right + up_clicked, r.top + up_clicked, UPARROW, TC_BLACK, SA_HOR_CENTER);
    00314
    00315   DrawFrameRect(r.left, r.bottom - 9, r.right, r.bottom, colour, (down_clicked) ? FR_LOWERED : FR_NONE);
    00316   DrawString(r.left + down_clicked, r.right + down_clicked, r.bottom - 9 + down_clicked, DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00317
    00318   int c1 = _colour_gradient[colour & 0xF][3];
    00319   int c2 = _colour_gradient[colour & 0xF][7];
    00320
    00321   /* draw "shaded" background */
    00322   GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c2);
    00323   GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c1, FILLRECT_CHECKER);
    00324
    00325   /* draw shaded lines */
    00326   GfxFillRect(r.left + 2, r.top + 10, r.left + 2, r.bottom - 10, c1);
    00327   GfxFillRect(r.left + 3, r.top + 10, r.left + 3, r.bottom - 10, c2);
    00328   GfxFillRect(r.left + 7, r.top + 10, r.left + 7, r.bottom - 10, c1);
    00329   GfxFillRect(r.left + 8, r.top + 10, r.left + 8, r.bottom - 10, c2);
    00330
    00331   Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom, false);
    00332   DrawFrameRect(r.left, pt.x, r.right, pt.y, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00333 }
    00334
    00344 static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
    00345 {
    00346   DrawFrameRect(r.left, r.top, r.left + 9, r.bottom, colour, left_clicked ? FR_LOWERED : FR_NONE);
    00347   DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + left_clicked, r.top + 1 + left_clicked);
    00348
    00349   DrawFrameRect(r.right - 9, r.top, r.right, r.bottom, colour, right_clicked ? FR_LOWERED : FR_NONE);
    00350   DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - 8 + right_clicked, r.top + 1 + right_clicked);
    00351
    00352   int c1 = _colour_gradient[colour & 0xF][3];
    00353   int c2 = _colour_gradient[colour & 0xF][7];
    00354
    00355   /* draw "shaded" background */
    00356   GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c2);
    00357   GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c1, FILLRECT_CHECKER);
    00358
    00359   /* draw shaded lines */
    00360   GfxFillRect(r.left + 10, r.top + 2, r.right - 10, r.top + 2, c1);
    00361   GfxFillRect(r.left + 10, r.top + 3, r.right - 10, r.top + 3, c2);
    00362   GfxFillRect(r.left + 10, r.top + 7, r.right - 10, r.top + 7, c1);
    00363   GfxFillRect(r.left + 10, r.top + 8, r.right - 10, r.top + 8, c2);
    00364
    00365   /* draw actual scrollbar */
    00366   Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right, true);
    00367   DrawFrameRect(pt.x, r.top, pt.y, r.bottom, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00368 }
    00369
    00376 static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
    00377 {
    00378   int x2 = r.left; // by default the left side is the left side of the widget
    00379
    00380   if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str);
    00381
    00382   int c1 = _colour_gradient[colour][3];
    00383   int c2 = _colour_gradient[colour][7];
    00384
    00385   /* If the frame has text, adjust the top bar to fit half-way through */
    00386   int dy1 = 4;
    00387   if (str != STR_NULL) dy1 = FONT_HEIGHT_NORMAL / 2 - 1;
    00388   int dy2 = dy1 + 1;
    00389
    00390   if (_current_text_dir == TD_LTR) {
    00391     /* Line from upper left corner to start of text */
    00392     GfxFillRect(r.left, r.top + dy1, r.left + 4, r.top + dy1, c1);
    00393     GfxFillRect(r.left + 1, r.top + dy2, r.left + 4, r.top + dy2, c2);
    00394
    00395     /* Line from end of text to upper right corner */
    00396     GfxFillRect(x2, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00397     GfxFillRect(x2, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00398   } else {
    00399     /* Line from upper left corner to start of text */
    00400     GfxFillRect(r.left, r.top + dy1, x2 - 2, r.top + dy1, c1);
    00401     GfxFillRect(r.left + 1, r.top + dy2, x2 - 2, r.top + dy2, c2);
    00402
    00403     /* Line from end of text to upper right corner */
    00404     GfxFillRect(r.right - 5, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00405     GfxFillRect(r.right - 5, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00406   }
    00407
    00408   /* Line from upper left corner to bottom left corner */
    00409   GfxFillRect(r.left, r.top + dy2, r.left, r.bottom - 1, c1);
    00410   GfxFillRect(r.left + 1, r.top + dy2 + 1, r.left + 1, r.bottom - 2, c2);
    00411
    00412   /* Line from upper right corner to bottom right corner */
    00413   GfxFillRect(r.right - 1, r.top + dy2, r.right - 1, r.bottom - 2, c1);
    00414   GfxFillRect(r.right, r.top + dy1, r.right, r.bottom - 1, c2);
    00415
    00416   GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
    00417   GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
    00418 }
    00419
    00426 static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
    00427 {
    00428   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00429   DrawSprite((clicked) ? SPR_WINDOW_SHADE : SPR_WINDOW_UNSHADE, PAL_NONE, r.left + WD_SHADEBOX_LEFT + clicked, r.top + WD_SHADEBOX_TOP + clicked);
    00430 }
    00431
    00438 static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
    00439 {
    00440   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00441   DrawSprite((clicked) ? SPR_PIN_UP : SPR_PIN_DOWN, PAL_NONE, r.left + WD_STICKYBOX_LEFT + clicked, r.top + WD_STICKYBOX_TOP + clicked);
    00442 }
    00443
    00450 static inline void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
    00451 {
    00452   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00453   DrawSprite(SPR_WINDOW_DEBUG, PAL_NONE, r.left + WD_DEBUGBOX_LEFT + clicked, r.top + WD_DEBUGBOX_TOP + clicked);
    00454 }
    00455
    00463 static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
    00464 {
    00465   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00466   if (at_left) {
    00467     DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked, r.top + WD_RESIZEBOX_TOP + clicked);
    00468   } else {
    00469     DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.left + WD_RESIZEBOX_LEFT + clicked, r.top + WD_RESIZEBOX_TOP + clicked);
    00470   }
    00471 }
    00472
    00479 static inline void DrawCloseBox(const Rect &r, Colours colour, StringID str)
    00480 {
    00481   assert(str == STR_BLACK_CROSS || str == STR_SILVER_CROSS); // black or silver cross
    00482   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE);
    00483   DrawString(r.left + WD_CLOSEBOX_LEFT, r.right - WD_CLOSEBOX_RIGHT, r.top + WD_CLOSEBOX_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
    00484 }
    00485
    00493 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
    00494 {
    00495   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_BORDERONLY);
    00496   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);
    00497
    00498   if (owner != INVALID_OWNER) {
    00499     GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]);
    00500   }
    00501
    00502   if (str != STR_NULL) DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + WD_CAPTIONTEXT_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
    00503 }
    00504
    00515 static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
    00516 {
    00517   if (_current_text_dir == TD_LTR) {
    00518     DrawFrameRect(r.left, r.top, r.right - 12, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00519     DrawFrameRect(r.right - 11, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00520     DrawString(r.right - (clicked_dropdown ? 10 : 11), r.right, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00521     if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_LEFT + clicked_button, r.right - WD_DROPDOWNTEXT_RIGHT + clicked_button, r.top + WD_DROPDOWNTEXT_TOP + clicked_button, str, TC_BLACK);
    00522   } else {
    00523     DrawFrameRect(r.left + 12, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00524     DrawFrameRect(r.left, r.top, r.left + 11, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00525     DrawString(r.left + clicked_dropdown, r.left + 11, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
    00526     if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_RIGHT + clicked_button, r.right - WD_DROPDOWNTEXT_LEFT + clicked_button, r.top + WD_DROPDOWNTEXT_TOP + clicked_button, str, TC_BLACK);
    00527   }
    00528 }
    00529
    00537 static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
    00538 {
    00539   DrawButtonDropdown(r, colour, false, clicked, str);
    00540 }
    00541
    00545 void Window::DrawWidgets() const
    00546 {
    00547   this->nested_root->Draw(this);
    00548
    00549   if (this->flags4 & WF_WHITE_BORDER_MASK) {
    00550     DrawFrameRect(0, 0, this->width - 1, this->height - 1, COLOUR_WHITE, FR_BORDERONLY);
    00551   }
    00552 }
    00553
    00559 void Window::DrawSortButtonState(int widget, SortButtonState state) const
    00560 {
    00561   if (state == SBS_OFF) return;
    00562
    00563   assert(this->nested_array != NULL);
    00564   const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
    00565
    00566   int offset = this->IsWidgetLowered(widget) ? 1 : 0;
    00567   int base = offset + nwid->pos_x + (_current_text_dir == TD_LTR ? nwid->current_x - WD_SORTBUTTON_ARROW_WIDTH : 0);
    00568   int top = nwid->pos_y;
    00569
    00570   DrawString(base, base + WD_SORTBUTTON_ARROW_WIDTH, top + 1 + offset, state == SBS_DOWN ? DOWNARROW : UPARROW, TC_BLACK, SA_HOR_CENTER);
    00571 }
    00572
    00573
    00631 NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator()
    00632 {
    00633   this->type = tp;
    00634 }
    00635
    00636 /* ~NWidgetContainer() takes care of #next and #prev data members. */
    00637
    00685 void NWidgetBase::SetDirty(const Window *w) const
    00686 {
    00687   int abs_left = w->left + this->pos_x;
    00688   int abs_top = w->top + this->pos_y;
    00689   SetDirtyBlocks(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
    00690 }
    00691
    00705 NWidgetBase *NWidgetBase::GetWidgetOfType(WidgetType tp)
    00706 {
    00707   return (this->type == tp) ? this : NULL;
    00708 }
    00709
    00716 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y) : NWidgetBase(tp)
    00717 {
    00718   this->fill_x = fill_x;
    00719   this->fill_y = fill_y;
    00720 }
    00721
    00727 void NWidgetResizeBase::SetMinimalSize(uint min_x, uint min_y)
    00728 {
    00729   this->min_x = min_x;
    00730   this->min_y = min_y;
    00731 }
    00732
    00739 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
    00740 {
    00741   this->min_y = min_lines * GetCharacterHeight(size) + spacing;
    00742 }
    00743
    00749 void NWidgetResizeBase::SetFill(uint fill_x, uint fill_y)
    00750 {
    00751   this->fill_x = fill_x;
    00752   this->fill_y = fill_y;
    00753 }
    00754
    00760 void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y)
    00761 {
    00762   this->resize_x = resize_x;
    00763   this->resize_y = resize_y;
    00764 }
    00765
    00766 void NWidgetResizeBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00767 {
    00768   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    00769 }
    00770
    00780 NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint16 widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y)
    00781 {
    00782   this->colour = colour;
    00783   this->index = -1;
    00784   this->widget_data = widget_data;
    00785   this->tool_tip = tool_tip;
    00786   this->scrollbar_index = -1;
    00787 }
    00788
    00793 void NWidgetCore::SetIndex(int index)
    00794 {
    00795   assert(index >= 0);
    00796   this->index = index;
    00797 }
    00798
    00804 void NWidgetCore::SetDataTip(uint16 widget_data, StringID tool_tip)
    00805 {
    00806   this->widget_data = widget_data;
    00807   this->tool_tip = tool_tip;
    00808 }
    00809
    00810 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
    00811 {
    00812   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    00813 }
    00814
    00815 NWidgetCore *NWidgetCore::GetWidgetFromPos(int x, int y)
    00816 {
    00817   return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : NULL;
    00818 }
    00819
    00824 NWidgetContainer::NWidgetContainer(WidgetType tp) : NWidgetBase(tp)
    00825 {
    00826   this->head = NULL;
    00827   this->tail = NULL;
    00828 }
    00829
    00830 NWidgetContainer::~NWidgetContainer()
    00831 {
    00832   while (this->head != NULL) {
    00833     NWidgetBase *wid = this->head->next;
    00834     delete this->head;
    00835     this->head = wid;
    00836   }
    00837   this->tail = NULL;
    00838 }
    00839
    00840 NWidgetBase *NWidgetContainer::GetWidgetOfType(WidgetType tp)
    00841 {
    00842   if (this->type == tp) return this;
    00843   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00844     NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
    00845     if (nwid != NULL) return nwid;
    00846   }
    00847   return NULL;
    00848 }
    00849
    00854 void NWidgetContainer::Add(NWidgetBase *wid)
    00855 {
    00856   assert(wid->next == NULL && wid->prev == NULL);
    00857
    00858   if (this->head == NULL) {
    00859     this->head = wid;
    00860     this->tail = wid;
    00861   } else {
    00862     assert(this->tail != NULL);
    00863     assert(this->tail->next == NULL);
    00864
    00865     this->tail->next = wid;
    00866     wid->prev = this->tail;
    00867     this->tail = wid;
    00868   }
    00869 }
    00870
    00871 void NWidgetContainer::FillNestedArray(NWidgetBase **array, uint length)
    00872 {
    00873   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00874     child_wid->FillNestedArray(array, length);
    00875   }
    00876 }
    00877
    00881 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION)
    00882 {
    00883   this->index = -1;
    00884 }
    00885
    00886 void NWidgetStacked::SetIndex(int index)
    00887 {
    00888   this->index = index;
    00889 }
    00890
    00891 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
    00892 {
    00893   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    00894     assert(w->nested_array_size > (uint)this->index);
    00895     w->nested_array[this->index] = this;
    00896   }
    00897
    00898   /* Zero size plane selected */
    00899   if (this->shown_plane >= SZSP_BEGIN) {
    00900     Dimension size    = {0, 0};
    00901     Dimension padding = {0, 0};
    00902     Dimension fill    = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00903     Dimension resize  = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00904     /* Here we're primarily interested in the value of resize */
    00905     if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    00906
    00907     this->smallest_x = size.width;
    00908     this->smallest_y = size.height;
    00909     this->fill_x = fill.width;
    00910     this->fill_y = fill.height;
    00911     this->resize_x = resize.width;
    00912     this->resize_y = resize.height;
    00913     return;
    00914   }
    00915
    00916   /* First sweep, recurse down and compute minimal size and filling. */
    00917   this->smallest_x = 0;
    00918   this->smallest_y = 0;
    00919   this->fill_x = (this->head != NULL) ? 1 : 0;
    00920   this->fill_y = (this->head != NULL) ? 1 : 0;
    00921   this->resize_x = (this->head != NULL) ? 1 : 0;
    00922   this->resize_y = (this->head != NULL) ? 1 : 0;
    00923   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00924     child_wid->SetupSmallestSize(w, init_array);
    00925
    00926     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    00927     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    00928     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    00929     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    00930     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    00931     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    00932   }
    00933 }
    00934
    00935 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00936 {
    00937   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    00938   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    00939
    00940   if (this->shown_plane >= SZSP_BEGIN) return;
    00941
    00942   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00943     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    00944     uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    00945     uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
    00946
    00947     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    00948     uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    00949     uint child_pos_y = child_wid->padding_top;
    00950
    00951     child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
    00952   }
    00953 }
    00954
    00955 void NWidgetStacked::FillNestedArray(NWidgetBase **array, uint length)
    00956 {
    00957   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    00958   NWidgetContainer::FillNestedArray(array, length);
    00959 }
    00960
    00961 void NWidgetStacked::Draw(const Window *w)
    00962 {
    00963   if (this->shown_plane >= SZSP_BEGIN) return;
    00964
    00965   int plane = 0;
    00966   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    00967     if (plane == this->shown_plane) {
    00968       child_wid->Draw(w);
    00969       return;
    00970     }
    00971   }
    00972
    00973   NOT_REACHED();
    00974 }
    00975
    00976 NWidgetCore *NWidgetStacked::GetWidgetFromPos(int x, int y)
    00977 {
    00978   if (this->shown_plane >= SZSP_BEGIN) return NULL;
    00979
    00980   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    00981   int plane = 0;
    00982   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    00983     if (plane == this->shown_plane) {
    00984       return child_wid->GetWidgetFromPos(x, y);
    00985     }
    00986   }
    00987   return NULL;
    00988 }
    00989
    00994 void NWidgetStacked::SetDisplayedPlane(int plane)
    00995 {
    00996   this->shown_plane = plane;
    00997 }
    00998
    00999 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
    01000 {
    01001   this->flags = flags;
    01002 }
    01003
    01013 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01014 {
    01015   this->pip_pre = pip_pre;
    01016   this->pip_inter = pip_inter;
    01017   this->pip_post = pip_post;
    01018 }
    01019
    01020 void NWidgetPIPContainer::Draw(const Window *w)
    01021 {
    01022   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01023     child_wid->Draw(w);
    01024   }
    01025 }
    01026
    01027 NWidgetCore *NWidgetPIPContainer::GetWidgetFromPos(int x, int y)
    01028 {
    01029   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01030
    01031   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01032     NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
    01033     if (nwid != NULL) return nwid;
    01034   }
    01035   return NULL;
    01036 }
    01037
    01039 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
    01040 {
    01041 }
    01042
    01043 void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array)
    01044 {
    01045   this->smallest_x = 0; // Sum of minimal size of all childs.
    01046   this->smallest_y = 0; // Biggest child.
    01047   this->fill_x = 0;     // smallest non-zero child widget fill step.
    01048   this->fill_y = 1;     // smallest common child fill step.
    01049   this->resize_x = 0;   // smallest non-zero child widget resize step.
    01050   this->resize_y = 1;   // smallest common child resize step.
    01051
    01052   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01053   uint longest = 0; // Longest child found.
    01054   uint max_vert_fill = 0; // Biggest vertical fill step.
    01055   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01056     child_wid->SetupSmallestSize(w, init_array);
    01057     longest = max(longest, child_wid->smallest_x);
    01058     max_vert_fill = max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
    01059     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    01060   }
    01061   /* 1b. Make the container higher if needed to accomadate all childs nicely. */
    01062   uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height.
    01063   uint cur_height = this->smallest_y;
    01064   while (true) {
    01065     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01066       uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
    01067       uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01068       if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting childs are not interesting.
    01069         uint remainder = (cur_height - child_height) % step_size;
    01070         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01071           cur_height += step_size - remainder;
    01072           assert(cur_height < max_smallest); // Safeguard against infinite height expansion.
    01073           /* Remaining childs will adapt to the new cur_height, thus speeding up the computation. */
    01074         }
    01075       }
    01076     }
    01077     if (this->smallest_y == cur_height) break;
    01078     this->smallest_y = cur_height; // Smallest height got changed, try again.
    01079   }
    01080   /* 2. For containers that must maintain equal width, extend child minimal size. */
    01081   if (this->flags & NC_EQUALSIZE) {
    01082     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01083       if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
    01084     }
    01085   }
    01086   /* 3. Move PIP space to the childs, compute smallest, fill, and resize values of the container. */
    01087   if (this->head != NULL) this->head->padding_left += this->pip_pre;
    01088   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01089     if (child_wid->next != NULL) {
    01090       child_wid->padding_right += this->pip_inter;
    01091     } else {
    01092       child_wid->padding_right += this->pip_post;
    01093     }
    01094
    01095     this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01096     if (child_wid->fill_x > 0) {
    01097       if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
    01098     }
    01099     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    01100
    01101     if (child_wid->resize_x > 0) {
    01102       if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
    01103     }
    01104     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    01105   }
    01106   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01107   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01108 }
    01109
    01110 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01111 {
    01112   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01113
    01114   uint additional_length = given_width - this->smallest_x; // Additional width given to us.
    01115   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01116
    01117   /* In principle, the additional horizontal space is distributed evenly over the available resizable childs. Due to step sizes, this may not always be feasible.
    01118    * To make resizing work as good as possible, first childs with biggest step sizes are done. These may get less due to rounding down.
    01119    * 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
    01120    * of the child with the smallest non-zero stepsize.
    01121    *
    01122    * 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
    01123    * size and position, and directly call child->AssignSizePosition() with the computed values.
    01124    * 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
    01125    * then we call the child.
    01126    */
    01127
    01128   /* 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. */
    01129   int num_changing_childs = 0; // Number of childs that can change size.
    01130   uint biggest_stepsize = 0;
    01131   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01132     uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01133     if (hor_step > 0) {
    01134       num_changing_childs++;
    01135       biggest_stepsize = max(biggest_stepsize, hor_step);
    01136     } else {
    01137       child_wid->current_x = child_wid->smallest_x;
    01138     }
    01139
    01140     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    01141     child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    01142   }
    01143
    01144   /* Second loop: Allocate the additional horizontal space over the resizing childs, starting with the biggest resize steps. */
    01145   while (biggest_stepsize > 0) {
    01146     uint next_biggest_stepsize = 0;
    01147     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01148       uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01149       if (hor_step > biggest_stepsize) continue; // Already done
    01150       if (hor_step == biggest_stepsize) {
    01151         uint increment = additional_length / num_changing_childs;
    01152         num_changing_childs--;
    01153         if (hor_step > 1) increment -= increment % hor_step;
    01154         child_wid->current_x = child_wid->smallest_x + increment;
    01155         additional_length -= increment;
    01156         continue;
    01157       }
    01158       next_biggest_stepsize = max(next_biggest_stepsize, hor_step);
    01159     }
    01160     biggest_stepsize = next_biggest_stepsize;
    01161   }
    01162   assert(num_changing_childs == 0);
    01163
    01164   /* Third loop: Compute position and call the child. */
    01165   uint position = 0; // Place to put next child relative to origin of the container.
    01166   NWidgetBase *child_wid = rtl ? this->tail : this->head;
    01167   while (child_wid != NULL) {
    01168     uint child_width = child_wid->current_x;
    01169     uint child_x = x + position + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01170     uint child_y = y + child_wid->padding_top;
    01171
    01172     child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
    01173     position += child_width + child_wid->padding_right + child_wid->padding_left;
    01174
    01175     child_wid = rtl ? child_wid->prev : child_wid->next;
    01176   }
    01177 }
    01178
    01180 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
    01181 {
    01182   this->type = NWID_HORIZONTAL_LTR;
    01183 }
    01184
    01185 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01186 {
    01187   NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
    01188 }
    01189
    01191 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
    01192 {
    01193 }
    01194
    01195 void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array)
    01196 {
    01197   this->smallest_x = 0; // Biggest child.
    01198   this->smallest_y = 0; // Sum of minimal size of all childs.
    01199   this->fill_x = 1;     // smallest common child fill step.
    01200   this->fill_y = 0;     // smallest non-zero child widget fill step.
    01201   this->resize_x = 1;   // smallest common child resize step.
    01202   this->resize_y = 0;   // smallest non-zero child widget resize step.
    01203
    01204   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01205   uint highest = 0; // Highest child found.
    01206   uint max_hor_fill = 0; // Biggest horizontal fill step.
    01207   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01208     child_wid->SetupSmallestSize(w, init_array);
    01209     highest = max(highest, child_wid->smallest_y);
    01210     max_hor_fill = max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
    01211     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    01212   }
    01213   /* 1b. Make the container wider if needed to accomadate all childs nicely. */
    01214   uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height.
    01215   uint cur_width = this->smallest_x;
    01216   while (true) {
    01217     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01218       uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
    01219       uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01220       if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting childs are not interesting.
    01221         uint remainder = (cur_width - child_width) % step_size;
    01222         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01223           cur_width += step_size - remainder;
    01224           assert(cur_width < max_smallest); // Safeguard against infinite width expansion.
    01225           /* Remaining childs will adapt to the new cur_width, thus speeding up the computation. */
    01226         }
    01227       }
    01228     }
    01229     if (this->smallest_x == cur_width) break;
    01230     this->smallest_x = cur_width; // Smallest width got changed, try again.
    01231   }
    01232   /* 2. For containers that must maintain equal width, extend child minimal size. */
    01233   if (this->flags & NC_EQUALSIZE) {
    01234     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01235       if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
    01236     }
    01237   }
    01238   /* 3. Move PIP space to the childs, compute smallest, fill, and resize values of the container. */
    01239   if (this->head != NULL) this->head->padding_top += this->pip_pre;
    01240   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01241     if (child_wid->next != NULL) {
    01242       child_wid->padding_bottom += this->pip_inter;
    01243     } else {
    01244       child_wid->padding_bottom += this->pip_post;
    01245     }
    01246
    01247     this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01248     if (child_wid->fill_y > 0) {
    01249       if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
    01250     }
    01251     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    01252
    01253     if (child_wid->resize_y > 0) {
    01254       if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
    01255     }
    01256     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    01257   }
    01258   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01259   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01260 }
    01261
    01262 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01263 {
    01264   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01265
    01266   int additional_length = given_height - this->smallest_y; // Additional height given to us.
    01267   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01268
    01269   /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the childs with the biggest resize steps.
    01270    * It also stores computed widths and heights into current_x and current_y values of the child.
    01271    */
    01272
    01273   /* 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. */
    01274   int num_changing_childs = 0; // Number of childs that can change size.
    01275   uint biggest_stepsize = 0;
    01276   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01277     uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01278     if (vert_step > 0) {
    01279       num_changing_childs++;
    01280       biggest_stepsize = max(biggest_stepsize, vert_step);
    01281     } else {
    01282       child_wid->current_y = child_wid->smallest_y;
    01283     }
    01284
    01285     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    01286     child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    01287   }
    01288
    01289   /* Second loop: Allocate the additional vertical space over the resizing childs, starting with the biggest resize steps. */
    01290   while (biggest_stepsize > 0) {
    01291     uint next_biggest_stepsize = 0;
    01292     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01293       uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01294       if (vert_step > biggest_stepsize) continue; // Already done
    01295       if (vert_step == biggest_stepsize) {
    01296         uint increment = additional_length / num_changing_childs;
    01297         num_changing_childs--;
    01298         if (vert_step > 1) increment -= increment % vert_step;
    01299         child_wid->current_y = child_wid->smallest_y + increment;
    01300         additional_length -= increment;
    01301         continue;
    01302       }
    01303       next_biggest_stepsize = max(next_biggest_stepsize, vert_step);
    01304     }
    01305     biggest_stepsize = next_biggest_stepsize;
    01306   }
    01307   assert(num_changing_childs == 0);
    01308
    01309   /* Third loop: Compute position and call the child. */
    01310   uint position = 0; // Place to put next child relative to origin of the container.
    01311   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01312     uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01313     uint child_height = child_wid->current_y;
    01314
    01315     child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
    01316     position += child_height + child_wid->padding_top + child_wid->padding_bottom;
    01317   }
    01318 }
    01319
    01325 NWidgetSpacer::NWidgetSpacer(int length, int height) : NWidgetResizeBase(NWID_SPACER, 0, 0)
    01326 {
    01327   this->SetMinimalSize(length, height);
    01328   this->SetResize(0, 0);
    01329 }
    01330
    01331 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
    01332 {
    01333   this->smallest_x = this->min_x;
    01334   this->smallest_y = this->min_y;
    01335 }
    01336
    01337 void NWidgetSpacer::FillNestedArray(NWidgetBase **array, uint length)
    01338 {
    01339 }
    01340
    01341 void NWidgetSpacer::Draw(const Window *w)
    01342 {
    01343   /* Spacer widget is never visible. */
    01344 }
    01345
    01346 void NWidgetSpacer::SetDirty(const Window *w) const
    01347 {
    01348   /* Spacer widget never need repainting. */
    01349 }
    01350
    01351 NWidgetCore *NWidgetSpacer::GetWidgetFromPos(int x, int y)
    01352 {
    01353   return NULL;
    01354 }
    01355
    01356 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX, NC_EQUALSIZE), index(-1), clicked(-1), count(-1)
    01357 {
    01358 }
    01359
    01360 void NWidgetMatrix::SetIndex(int index)
    01361 {
    01362   this->index = index;
    01363 }
    01364
    01365 void NWidgetMatrix::SetColour(Colours colour)
    01366 {
    01367   this->colour = colour;
    01368 }
    01369
    01374 void NWidgetMatrix::SetClicked(int clicked)
    01375 {
    01376   this->clicked = clicked;
    01377   if (this->clicked >= 0 && this->sb != NULL && this->widgets_x != 0) {
    01378     int vpos = (this->clicked / this->widgets_x) * this->widget_h; // Vertical position of the top.
    01379     /* Need to scroll down -> Scroll to the bottom.
    01380      * However, last entry has no 'this->pip_inter' underneath, and we must stay below this->sb->GetCount() */
    01381     if (this->sb->GetPosition() < vpos) vpos += this->widget_h - this->pip_inter - 1;
    01382     this->sb->ScrollTowards(vpos);
    01383   }
    01384 }
    01385
    01391 void NWidgetMatrix::SetCount(int count)
    01392 {
    01393   this->count = count;
    01394
    01395   if (this->sb == NULL || this->widgets_x == 0) return;
    01396
    01397   /* We need to get the number of pixels the matrix is high/wide.
    01398    * So, determine the number of rows/columns based on the number of
    01399    * columns/rows (one is constant/unscrollable).
    01400    * Then multiply that by the height of a widget, and add the pre
    01401    * and post spacing "offsets". */
    01402   count = CeilDiv(count, this->sb->IsVertical() ? this->widgets_x : this->widgets_y);
    01403   count *= (this->sb->IsVertical() ? this->head->smallest_y : this->head->smallest_x) + this->pip_inter;
    01404   count += -this->pip_inter + this->pip_pre + this->pip_post; // We counted an inter too much in the multiplication above
    01405   this->sb->SetCount(count);
    01406   this->sb->SetCapacity(this->sb->IsVertical() ? this->current_y : this->current_x);
    01407   this->sb->SetStepSize(this->sb->IsVertical() ? this->widget_h  : this->widget_w);
    01408 }
    01409
    01414 void NWidgetMatrix::SetScrollbar(Scrollbar *sb)
    01415 {
    01416   this->sb = sb;
    01417 }
    01418
    01419 void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array)
    01420 {
    01421   assert(this->head != NULL);
    01422   assert(this->head->next == NULL);
    01423
    01424   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    01425     assert(w->nested_array_size > (uint)this->index);
    01426     w->nested_array[this->index] = this;
    01427   }
    01428
    01429   /* Reset the widget number. */
    01430   SB(dynamic_cast<NWidgetCore *>(this->head)->index, 16, 16, 0);
    01431   this->head->SetupSmallestSize(w, init_array);
    01432
    01433   Dimension padding = {this->pip_pre + this->pip_post, this->pip_pre + this->pip_post};
    01434   Dimension size    = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height};
    01435   Dimension fill    = {0, 0};
    01436   Dimension resize  = {this->pip_inter + this->head->smallest_x, this->pip_inter + this->head->smallest_y};
    01437
    01438   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    01439
    01440   this->smallest_x = size.width;
    01441   this->smallest_y = size.height;
    01442   this->fill_x = fill.width;
    01443   this->fill_y = fill.height;
    01444   this->resize_x = resize.width;
    01445   this->resize_y = resize.height;
    01446 }
    01447
    01448 void NWidgetMatrix::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01449 {
    01450   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01451
    01452   this->pos_x = x;
    01453   this->pos_y = y;
    01454   this->current_x = given_width;
    01455   this->current_y = given_height;
    01456
    01457   /* Determine the size of the widgets, and the number of visible widgets on each of the axis. */
    01458   this->widget_w = this->head->smallest_x + this->pip_inter;
    01459   this->widget_h = this->head->smallest_y + this->pip_inter;
    01460
    01461   /* Account for the pip_inter is between widgets, so we need to account for that when
    01462    * the division assumes pip_inter is used for all widgets. */
    01463   this->widgets_x = CeilDiv(this->current_x - this->pip_pre - this->pip_post + this->pip_inter, this->widget_w);
    01464   this->widgets_y = CeilDiv(this->current_y - this->pip_pre - this->pip_post + this->pip_inter, this->widget_h);
    01465
    01466   /* When resizing, update the scrollbar's count. E.g. with a vertical
    01467    * scrollbar becoming wider or narrower means the amount of rows in
    01468    * the scrollbar becomes respectively smaller or higher. */
    01469   if (sizing == ST_RESIZE) {
    01470     this->SetCount(this->count);
    01471   }
    01472 }
    01473
    01474 void NWidgetMatrix::FillNestedArray(NWidgetBase **array, uint length)
    01475 {
    01476   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01477   NWidgetContainer::FillNestedArray(array, length);
    01478 }
    01479
    01480 NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y)
    01481 {
    01482   /* Falls outside of the matrix widget. */
    01483   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01484
    01485   int start_x, start_y, base_offs_x, base_offs_y;
    01486   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01487
    01488   /* Swap the x offset around for RTL, so it'll behave like LTR for RTL as well. */
    01489   bool rtl = _current_text_dir == TD_RTL;
    01490   if (rtl) base_offs_x -= (this->widgets_x - 1) * this->widget_w;
    01491
    01492   int widget_col = (x - base_offs_x - (int)this->pip_pre - (int)this->pos_x) / this->widget_w;
    01493   int widget_row = (y - base_offs_y - (int)this->pip_pre - (int)this->pos_y) / this->widget_h;
    01494
    01495   if (widget_row * this->widgets_x + widget_col >= this->count) return NULL;
    01496
    01497   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01498   child->AssignSizePosition(ST_RESIZE,
    01499       this->pos_x + this->pip_pre + widget_col * this->widget_w + base_offs_x,
    01500       this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y,
    01501       child->smallest_x, child->smallest_y, rtl);
    01502
    01503   /* "Undo" the RTL swap here to get the right widget index. */
    01504   if (rtl) widget_col = this->widgets_x - widget_col - 1;
    01505   SB(child->index, 16, 16, (widget_row + start_y) * this->widgets_x + widget_col + start_x);
    01506
    01507   return child->GetWidgetFromPos(x, y);
    01508 }
    01509
    01510 /* virtual */ void NWidgetMatrix::Draw(const Window *w)
    01511 {
    01512   /* Fill the background. */
    01513   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]);
    01514
    01515   /* Set up a clipping area for the previews. */
    01516   DrawPixelInfo tmp_dpi;
    01517   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;
    01518   DrawPixelInfo *old_dpi = _cur_dpi;
    01519   _cur_dpi = &tmp_dpi;
    01520
    01521   /* Get the appropriate offsets so we can draw the right widgets. */
    01522   NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
    01523   bool rtl = _current_text_dir == TD_RTL;
    01524   int start_x, start_y, base_offs_x, base_offs_y;
    01525   this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
    01526
    01527   int offs_y = base_offs_y;
    01528   for (int y = start_y; y < start_y + this->widgets_y + 1; y++, offs_y += this->widget_h) {
    01529     /* Are we within bounds? */
    01530     if (offs_y + child->smallest_y <= 0) continue;
    01531     if (offs_y >= (int)this->current_y) break;
    01532
    01533     /* We've passed our amount of widgets. */
    01534     if (y * this->widgets_x >= this->count) break;
    01535
    01536     int offs_x = base_offs_x;
    01537     for (int x = start_x; x < start_x + this->widgets_x + 1; x++, offs_x += rtl ? -this->widget_w : this->widget_w) {
    01538       /* Are we within bounds? */
    01539       if (offs_x + child->smallest_x <= 0) continue;
    01540       if (offs_x >= (int)this->current_x) continue;
    01541
    01542       /* Do we have this many widgets? */
    01543       int sub_wid = y * this->widgets_x + x;
    01544       if (sub_wid >= this->count) break;
    01545
    01546       child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl);
    01547       child->SetLowered(this->clicked == sub_wid);
    01548       SB(child->index, 16, 16, sub_wid);
    01549       child->Draw(w);
    01550     }
    01551   }
    01552
    01553   /* Restore the clipping area. */
    01554   _cur_dpi = old_dpi;
    01555 }
    01556
    01564 void NWidgetMatrix::GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
    01565 {
    01566   base_offs_x = 0;
    01567   base_offs_y = 0;
    01568   start_x = 0;
    01569   start_y = 0;
    01570   if (this->sb != NULL) {
    01571     if (this->sb->IsVertical()) {
    01572       start_y = this->sb->GetPosition() / this->widget_h;
    01573       base_offs_y = -this->sb->GetPosition() + start_y * this->widget_h;
    01574       if (_current_text_dir == TD_RTL) base_offs_x = this->pip_pre + this->widget_w * (this->widgets_x - 1) - this->pip_inter;
    01575     } else {
    01576       start_x = this->sb->GetPosition() / this->widget_w;
    01577       if (_current_text_dir == TD_RTL) {
    01578         base_offs_x = this->sb->GetCapacity() + this->sb->GetPosition() - (start_x + 1) * this->widget_w + this->pip_inter - this->pip_post - this->pip_pre;
    01579       } else {
    01580         base_offs_x = -this->sb->GetPosition() + start_x * this->widget_w;
    01581       }
    01582     }
    01583   }
    01584 }
    01585
    01595 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
    01596 {
    01597   assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
    01598   if (index >= 0) this->SetIndex(index);
    01599   this->child = child;
    01600 }
    01601
    01602 NWidgetBackground::~NWidgetBackground()
    01603 {
    01604   if (this->child != NULL) delete this->child;
    01605 }
    01606
    01614 void NWidgetBackground::Add(NWidgetBase *nwid)
    01615 {
    01616   if (this->child == NULL) {
    01617     this->child = new NWidgetVertical();
    01618   }
    01619   this->child->Add(nwid);
    01620 }
    01621
    01632 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01633 {
    01634   if (this->child == NULL) {
    01635     this->child = new NWidgetVertical();
    01636   }
    01637   this->child->SetPIP(pip_pre, pip_inter, pip_post);
    01638 }
    01639
    01640 void NWidgetBackground::SetupSmallestSize(Window *w, bool init_array)
    01641 {
    01642   if (init_array && this->index >= 0) {
    01643     assert(w->nested_array_size > (uint)this->index);
    01644     w->nested_array[this->index] = this;
    01645   }
    01646   if (this->child != NULL) {
    01647     this->child->SetupSmallestSize(w, init_array);
    01648
    01649     this->smallest_x = this->child->smallest_x;
    01650     this->smallest_y = this->child->smallest_y;
    01651     this->fill_x = this->child->fill_x;
    01652     this->fill_y = this->child->fill_y;
    01653     this->resize_x = this->child->resize_x;
    01654     this->resize_y = this->child->resize_y;
    01655
    01656     /* Account for the size of the frame's text if that exists */
    01657     if (w != NULL && this->type == WWT_FRAME) {
    01658       this->child->padding_left   = WD_FRAMETEXT_LEFT;
    01659       this->child->padding_right  = WD_FRAMETEXT_RIGHT;
    01660       this->child->padding_top    = max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
    01661       this->child->padding_bottom = WD_FRAMETEXT_BOTTOM;
    01662
    01663       this->smallest_x += this->child->padding_left + this->child->padding_right;
    01664       this->smallest_y += this->child->padding_top + this->child->padding_bottom;
    01665
    01666       if (this->index >= 0) w->SetStringParameters(this->index);
    01667       this->smallest_x = max(this->smallest_x, GetStringBoundingBox(this->widget_data).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
    01668     }
    01669   } else {
    01670     Dimension d = {this->min_x, this->min_y};
    01671     Dimension fill = {this->fill_x, this->fill_y};
    01672     Dimension resize  = {this->resize_x, this->resize_y};
    01673     if (w != NULL) { // A non-NULL window pointer acts as switch to turn dynamic widget size on.
    01674       if (this->type == WWT_FRAME || this->type == WWT_INSET) {
    01675         if (this->index >= 0) w->SetStringParameters(this->index);
    01676         Dimension background = GetStringBoundingBox(this->widget_data);
    01677         background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
    01678         d = maxdim(d, background);
    01679       }
    01680       if (this->index >= 0) {
    01681         static const Dimension padding = {0, 0};
    01682         w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
    01683       }
    01684     }
    01685     this->smallest_x = d.width;
    01686     this->smallest_y = d.height;
    01687     this->fill_x = fill.width;
    01688     this->fill_y = fill.height;
    01689     this->resize_x = resize.width;
    01690     this->resize_y = resize.height;
    01691   }
    01692 }
    01693
    01694 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01695 {
    01696   this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01697
    01698   if (this->child != NULL) {
    01699     uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
    01700     uint width = given_width - this->child->padding_right - this->child->padding_left;
    01701     uint height = given_height - this->child->padding_top - this->child->padding_bottom;
    01702     this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
    01703   }
    01704 }
    01705
    01706 void NWidgetBackground::FillNestedArray(NWidgetBase **array, uint length)
    01707 {
    01708   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01709   if (this->child != NULL) this->child->FillNestedArray(array, length);
    01710 }
    01711
    01712 void NWidgetBackground::Draw(const Window *w)
    01713 {
    01714   if (this->current_x == 0 || this->current_y == 0) return;
    01715
    01716   Rect r;
    01717   r.left = this->pos_x;
    01718   r.right = this->pos_x + this->current_x - 1;
    01719   r.top = this->pos_y;
    01720   r.bottom = this->pos_y + this->current_y - 1;
    01721
    01722   const DrawPixelInfo *dpi = _cur_dpi;
    01723   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01724
    01725   switch (this->type) {
    01726     case WWT_PANEL:
    01727       assert(this->widget_data == 0);
    01728       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
    01729       break;
    01730
    01731     case WWT_FRAME:
    01732       if (this->index >= 0) w->SetStringParameters(this->index);
    01733       DrawFrame(r, this->colour, this->widget_data);
    01734       break;
    01735
    01736     case WWT_INSET:
    01737       if (this->index >= 0) w->SetStringParameters(this->index);
    01738       DrawInset(r, this->colour, this->widget_data);
    01739       break;
    01740
    01741     default:
    01742       NOT_REACHED();
    01743   }
    01744
    01745   if (this->index >= 0) w->DrawWidget(r, this->index);
    01746   if (this->child != NULL) this->child->Draw(w);
    01747
    01748   if (this->IsDisabled()) {
    01749     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    01750   }
    01751 }
    01752
    01753 NWidgetCore *NWidgetBackground::GetWidgetFromPos(int x, int y)
    01754 {
    01755   NWidgetCore *nwid = NULL;
    01756   if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
    01757     if (this->child != NULL) nwid = this->child->GetWidgetFromPos(x, y);
    01758     if (nwid == NULL) nwid = this;
    01759   }
    01760   return nwid;
    01761 }
    01762
    01763 NWidgetBase *NWidgetBackground::GetWidgetOfType(WidgetType tp)
    01764 {
    01765   NWidgetBase *nwid = NULL;
    01766   if (this->child != NULL) nwid = this->child->GetWidgetOfType(tp);
    01767   if (nwid == NULL && this->type == tp) nwid = this;
    01768   return nwid;
    01769 }
    01770
    01771 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
    01772 {
    01773   this->SetIndex(index);
    01774 }
    01775
    01776 void NWidgetViewport::SetupSmallestSize(Window *w, bool init_array)
    01777 {
    01778   if (init_array && this->index >= 0) {
    01779     assert(w->nested_array_size > (uint)this->index);
    01780     w->nested_array[this->index] = this;
    01781   }
    01782   this->smallest_x = this->min_x;
    01783   this->smallest_y = this->min_y;
    01784 }
    01785
    01786 void NWidgetViewport::Draw(const Window *w)
    01787 {
    01788   if (this->disp_flags & ND_NO_TRANSPARENCY) {
    01789     TransparencyOptionBits to_backup = _transparency_opt;
    01790     _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_LOADING); // Disable all transparency, except textual stuff
    01791     w->DrawViewport();
    01792     _transparency_opt = to_backup;
    01793   } else {
    01794     w->DrawViewport();
    01795   }
    01796
    01797   /* Optionally shade the viewport. */
    01798   if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
    01799     GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
    01800         (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_TO_STRUCT_GREY, FILLRECT_RECOLOUR);
    01801   }
    01802 }
    01803
    01810 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
    01811 {
    01812   InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
    01813 }
    01814
    01819 void NWidgetViewport::UpdateViewportCoordinates(Window *w)
    01820 {
    01821   ViewPort *vp = w->viewport;
    01822   if (vp != NULL) {
    01823     vp->left = w->left + this->pos_x;
    01824     vp->top  = w->top + this->pos_y;
    01825     vp->width  = this->current_x;
    01826     vp->height = this->current_y;
    01827
    01828     vp->virtual_width  = ScaleByZoom(vp->width, vp->zoom);
    01829     vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
    01830   }
    01831 }
    01832
    01841 int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
    01842 {
    01843   uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
    01844   if (pos != INT_MAX) pos += this->GetPosition();
    01845   return (pos >= this->GetCount()) ? INT_MAX : pos;
    01846 }
    01847
    01855 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
    01856 {
    01857   NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
    01858   if (this->IsVertical()) {
    01859     this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
    01860   } else {
    01861     this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
    01862   }
    01863 }
    01864
    01871 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL), Scrollbar(tp != NWID_HSCROLLBAR)
    01872 {
    01873   assert(tp == NWID_HSCROLLBAR || tp == NWID_VSCROLLBAR);
    01874   this->SetIndex(index);
    01875
    01876   switch (this->type) {
    01877     case NWID_HSCROLLBAR:
    01878       this->SetMinimalSize(0, WD_HSCROLLBAR_HEIGHT);
    01879       this->SetResize(1, 0);
    01880       this->SetFill(1, 0);
    01881       this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
    01882       break;
    01883
    01884     case NWID_VSCROLLBAR:
    01885       this->SetMinimalSize(WD_VSCROLLBAR_WIDTH, 0);
    01886       this->SetResize(0, 1);
    01887       this->SetFill(0, 1);
    01888       this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
    01889       break;
    01890
    01891     default: NOT_REACHED();
    01892   }
    01893 }
    01894
    01895 void NWidgetScrollbar::SetupSmallestSize(Window *w, bool init_array)
    01896 {
    01897   if (init_array && this->index >= 0) {
    01898     assert(w->nested_array_size > (uint)this->index);
    01899     w->nested_array[this->index] = this;
    01900   }
    01901   this->smallest_x = this->min_x;
    01902   this->smallest_y = this->min_y;
    01903 }
    01904
    01905 void NWidgetScrollbar::Draw(const Window *w)
    01906 {
    01907   if (this->current_x == 0 || this->current_y == 0) return;
    01908
    01909   Rect r;
    01910   r.left = this->pos_x;
    01911   r.right = this->pos_x + this->current_x - 1;
    01912   r.top = this->pos_y;
    01913   r.bottom = this->pos_y + this->current_y - 1;
    01914
    01915   const DrawPixelInfo *dpi = _cur_dpi;
    01916   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01917
    01918   bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
    01919   bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
    01920   bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->scrolling_scrollbar == this->index;
    01921
    01922   if (this->type == NWID_HSCROLLBAR) {
    01923     DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    01924   } else {
    01925     DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
    01926   }
    01927
    01928   if (this->IsDisabled()) {
    01929     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    01930   }
    01931 }
    01932
    01934 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
    01935 {
    01936   shadebox_dimension.width  = shadebox_dimension.height  = 0;
    01937   debugbox_dimension.width  = debugbox_dimension.height  = 0;
    01938   stickybox_dimension.width = stickybox_dimension.height = 0;
    01939   resizebox_dimension.width = resizebox_dimension.height = 0;
    01940   closebox_dimension.width  = closebox_dimension.height  = 0;
    01941 }
    01942
    01943 Dimension NWidgetLeaf::shadebox_dimension  = {0, 0};
    01944 Dimension NWidgetLeaf::debugbox_dimension  = {0, 0};
    01945 Dimension NWidgetLeaf::stickybox_dimension = {0, 0};
    01946 Dimension NWidgetLeaf::resizebox_dimension = {0, 0};
    01947 Dimension NWidgetLeaf::closebox_dimension  = {0, 0};
    01948
    01957 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint16 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
    01958 {
    01959   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);
    01960   if (index >= 0) this->SetIndex(index);
    01961   this->SetMinimalSize(0, 0);
    01962   this->SetResize(0, 0);
    01963
    01964   switch (tp) {
    01965     case WWT_EMPTY:
    01966       break;
    01967
    01968     case WWT_PUSHBTN:
    01969     case WWT_IMGBTN:
    01970     case WWT_PUSHIMGBTN:
    01971     case WWT_IMGBTN_2:
    01972     case WWT_TEXTBTN:
    01973     case WWT_PUSHTXTBTN:
    01974     case WWT_TEXTBTN_2:
    01975     case WWT_LABEL:
    01976     case WWT_TEXT:
    01977     case WWT_MATRIX:
    01978     case NWID_BUTTON_DROPDOWN:
    01979     case WWT_ARROWBTN:
    01980     case WWT_PUSHARROWBTN:
    01981       this->SetFill(0, 0);
    01982       break;
    01983
    01984     case WWT_EDITBOX:
    01985       this->SetMinimalSize(10, 0);
    01986       this->SetFill(0, 0);
    01987       break;
    01988
    01989     case WWT_CAPTION:
    01990       this->SetFill(1, 0);
    01991       this->SetResize(1, 0);
    01992       this->min_y = WD_CAPTION_HEIGHT;
    01993       this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
    01994       break;
    01995
    01996     case WWT_STICKYBOX:
    01997       this->SetFill(0, 0);
    01998       this->SetMinimalSize(WD_STICKYBOX_WIDTH, 14);
    01999       this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
    02000       break;
    02001
    02002     case WWT_SHADEBOX:
    02003       this->SetFill(0, 0);
    02004       this->SetMinimalSize(WD_SHADEBOX_TOP, 14);
    02005       this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
    02006       break;
    02007
    02008     case WWT_DEBUGBOX:
    02009       this->SetFill(0, 0);
    02010       this->SetMinimalSize(WD_DEBUGBOX_TOP, 14);
    02011       this->SetDataTip(STR_NULL, STR_TOOLTIP_DEBUG);
    02012       break;
    02013
    02014     case WWT_RESIZEBOX:
    02015       this->SetFill(0, 0);
    02016       this->SetMinimalSize(WD_RESIZEBOX_WIDTH, 12);
    02017       this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
    02018       break;
    02019
    02020     case WWT_CLOSEBOX:
    02021       this->SetFill(0, 0);
    02022       this->SetMinimalSize(WD_CLOSEBOX_WIDTH, 14);
    02023       this->SetDataTip(STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW);
    02024       break;
    02025
    02026     case WWT_DROPDOWN:
    02027       this->SetFill(0, 0);
    02028       this->min_y = WD_DROPDOWN_HEIGHT;
    02029       break;
    02030
    02031     default:
    02032       NOT_REACHED();
    02033   }
    02034 }
    02035
    02036 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
    02037 {
    02038   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    02039     assert(w->nested_array_size > (uint)this->index);
    02040     w->nested_array[this->index] = this;
    02041   }
    02042
    02043   Dimension size = {this->min_x, this->min_y};
    02044   Dimension fill = {this->fill_x, this->fill_y};
    02045   Dimension resize = {this->resize_x, this->resize_y};
    02046   /* Get padding, and update size with the real content size if appropriate. */
    02047   const Dimension *padding = NULL;
    02048   switch (this->type) {
    02049     case WWT_EMPTY: {
    02050       static const Dimension extra = {0, 0};
    02051       padding = &extra;
    02052       break;
    02053     }
    02054     case WWT_MATRIX: {
    02055       static const Dimension extra = {WD_MATRIX_LEFT + WD_MATRIX_RIGHT, WD_MATRIX_TOP + WD_MATRIX_BOTTOM};
    02056       padding = &extra;
    02057       break;
    02058     }
    02059     case WWT_SHADEBOX: {
    02060       static const Dimension extra = {WD_SHADEBOX_LEFT + WD_SHADEBOX_RIGHT, WD_SHADEBOX_TOP + WD_SHADEBOX_BOTTOM};
    02061       padding = &extra;
    02062       if (NWidgetLeaf::shadebox_dimension.width == 0) {
    02063         NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
    02064         NWidgetLeaf::shadebox_dimension.width += extra.width;
    02065         NWidgetLeaf::shadebox_dimension.height += extra.height;
    02066       }
    02067       size = maxdim(size, NWidgetLeaf::shadebox_dimension);
    02068       break;
    02069     }
    02070     case WWT_DEBUGBOX:
    02071       if (_settings_client.gui.newgrf_developer_tools && w->IsNewGRFInspectable()) {
    02072         static const Dimension extra = {WD_DEBUGBOX_LEFT + WD_DEBUGBOX_RIGHT, WD_DEBUGBOX_TOP + WD_DEBUGBOX_BOTTOM};
    02073         padding = &extra;
    02074         if (NWidgetLeaf::debugbox_dimension.width == 0) {
    02075           NWidgetLeaf::debugbox_dimension = GetSpriteSize(SPR_WINDOW_DEBUG);
    02076           NWidgetLeaf::debugbox_dimension.width += extra.width;
    02077           NWidgetLeaf::debugbox_dimension.height += extra.height;
    02078         }
    02079         size = maxdim(size, NWidgetLeaf::debugbox_dimension);
    02080       } else {
    02081         /* If the setting is disabled we don't want to see it! */
    02082         size.width = 0;
    02083         fill.width = 0;
    02084         resize.width = 0;
    02085       }
    02086       break;
    02087
    02088     case WWT_STICKYBOX: {
    02089       static const Dimension extra = {WD_STICKYBOX_LEFT + WD_STICKYBOX_RIGHT, WD_STICKYBOX_TOP + WD_STICKYBOX_BOTTOM};
    02090       padding = &extra;
    02091       if (NWidgetLeaf::stickybox_dimension.width == 0) {
    02092         NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
    02093         NWidgetLeaf::stickybox_dimension.width += extra.width;
    02094         NWidgetLeaf::stickybox_dimension.height += extra.height;
    02095       }
    02096       size = maxdim(size, NWidgetLeaf::stickybox_dimension);
    02097       break;
    02098     }
    02099     case WWT_RESIZEBOX: {
    02100       static const Dimension extra = {WD_RESIZEBOX_LEFT + WD_RESIZEBOX_RIGHT, WD_RESIZEBOX_TOP + WD_RESIZEBOX_BOTTOM};
    02101       padding = &extra;
    02102       if (NWidgetLeaf::resizebox_dimension.width == 0) {
    02103         NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
    02104         NWidgetLeaf::resizebox_dimension.width += extra.width;
    02105         NWidgetLeaf::resizebox_dimension.height += extra.height;
    02106       }
    02107       size = maxdim(size, NWidgetLeaf::resizebox_dimension);
    02108       break;
    02109     }
    02110     case WWT_EDITBOX:
    02111       size.height = max(size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    02112       /* FALL THROUGH */
    02113     case WWT_PUSHBTN: {
    02114       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02115       padding = &extra;
    02116       break;
    02117     }
    02118     case WWT_IMGBTN:
    02119     case WWT_IMGBTN_2:
    02120     case WWT_PUSHIMGBTN: {
    02121       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02122       padding = &extra;
    02123       Dimension d2 = GetSpriteSize(this->widget_data);
    02124       if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
    02125       d2.width += extra.width;
    02126       d2.height += extra.height;
    02127       size = maxdim(size, d2);
    02128       break;
    02129     }
    02130     case WWT_ARROWBTN:
    02131     case WWT_PUSHARROWBTN: {
    02132       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    02133       padding = &extra;
    02134       Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    02135       d2.width += extra.width;
    02136       d2.height += extra.height;
    02137       size = maxdim(size, d2);
    02138       break;
    02139     }
    02140
    02141     case WWT_CLOSEBOX: {
    02142       static const Dimension extra = {WD_CLOSEBOX_LEFT + WD_CLOSEBOX_RIGHT, WD_CLOSEBOX_TOP + WD_CLOSEBOX_BOTTOM};
    02143       padding = &extra;
    02144       if (NWidgetLeaf::closebox_dimension.width == 0) {
    02145         NWidgetLeaf::closebox_dimension = maxdim(GetStringBoundingBox(STR_BLACK_CROSS), GetStringBoundingBox(STR_SILVER_CROSS));
    02146         NWidgetLeaf::closebox_dimension.width += extra.width;
    02147         NWidgetLeaf::closebox_dimension.height += extra.height;
    02148       }
    02149       size = maxdim(size, NWidgetLeaf::closebox_dimension);
    02150       break;
    02151     }
    02152     case WWT_TEXTBTN:
    02153     case WWT_PUSHTXTBTN:
    02154     case WWT_TEXTBTN_2: {
    02155       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT,  WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    02156       padding = &extra;
    02157       if (this->index >= 0) w->SetStringParameters(this->index);
    02158       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02159       d2.width += extra.width;
    02160       d2.height += extra.height;
    02161       size = maxdim(size, d2);
    02162       break;
    02163     }
    02164     case WWT_LABEL:
    02165     case WWT_TEXT: {
    02166       static const Dimension extra = {0, 0};
    02167       padding = &extra;
    02168       if (this->index >= 0) w->SetStringParameters(this->index);
    02169       size = maxdim(size, GetStringBoundingBox(this->widget_data));
    02170       break;
    02171     }
    02172     case WWT_CAPTION: {
    02173       static const Dimension extra = {WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT, WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM};
    02174       padding = &extra;
    02175       if (this->index >= 0) w->SetStringParameters(this->index);
    02176       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02177       d2.width += extra.width;
    02178       d2.height += extra.height;
    02179       size = maxdim(size, d2);
    02180       break;
    02181     }
    02182     case WWT_DROPDOWN:
    02183     case NWID_BUTTON_DROPDOWN: {
    02184       static const Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
    02185       padding = &extra;
    02186       if (this->index >= 0) w->SetStringParameters(this->index);
    02187       Dimension d2 = GetStringBoundingBox(this->widget_data);
    02188       d2.width += extra.width;
    02189       d2.height += extra.height;
    02190       size = maxdim(size, d2);
    02191       break;
    02192     }
    02193     default:
    02194       NOT_REACHED();
    02195   }
    02196
    02197   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
    02198
    02199   this->smallest_x = size.width;
    02200   this->smallest_y = size.height;
    02201   this->fill_x = fill.width;
    02202   this->fill_y = fill.height;
    02203   this->resize_x = resize.width;
    02204   this->resize_y = resize.height;
    02205 }
    02206
    02207 void NWidgetLeaf::Draw(const Window *w)
    02208 {
    02209   if (this->current_x == 0 || this->current_y == 0) return;
    02210
    02211   Rect r;
    02212   r.left = this->pos_x;
    02213   r.right = this->pos_x + this->current_x - 1;
    02214   r.top = this->pos_y;
    02215   r.bottom = this->pos_y + this->current_y - 1;
    02216
    02217   const DrawPixelInfo *dpi = _cur_dpi;
    02218   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    02219
    02220   bool clicked = this->IsLowered();
    02221   switch (this->type) {
    02222     case WWT_EMPTY:
    02223       break;
    02224
    02225     case WWT_PUSHBTN:
    02226       assert(this->widget_data == 0);
    02227       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02228       break;
    02229
    02230     case WWT_IMGBTN:
    02231     case WWT_PUSHIMGBTN:
    02232     case WWT_IMGBTN_2:
    02233       DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
    02234       break;
    02235
    02236     case WWT_TEXTBTN:
    02237     case WWT_PUSHTXTBTN:
    02238     case WWT_TEXTBTN_2:
    02239       if (this->index >= 0) w->SetStringParameters(this->index);
    02240       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    02241       DrawLabel(r, this->type, clicked, this->widget_data);
    02242       break;
    02243
    02244     case WWT_ARROWBTN:
    02245     case WWT_PUSHARROWBTN: {
    02246       SpriteID sprite;
    02247       switch (this->widget_data) {
    02248         case AWV_DECREASE: sprite = _current_text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02249         case AWV_INCREASE: sprite = _current_text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    02250         case AWV_LEFT:     sprite = SPR_ARROW_LEFT;  break;
    02251         case AWV_RIGHT:    sprite = SPR_ARROW_RIGHT; break;
    02252         default: NOT_REACHED();
    02253       }
    02254       DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
    02255     }
    02256
    02257     case WWT_LABEL:
    02258       if (this->index >= 0) w->SetStringParameters(this->index);
    02259       DrawLabel(r, this->type, clicked, this->widget_data);
    02260       break;
    02261
    02262     case WWT_TEXT:
    02263       if (this->index >= 0) w->SetStringParameters(this->index);
    02264       DrawText(r, (TextColour)this->colour, this->widget_data);
    02265       break;
    02266
    02267     case WWT_MATRIX:
    02268       DrawMatrix(r, this->colour, clicked, this->widget_data);
    02269       break;
    02270
    02271     case WWT_EDITBOX:
    02272       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, FR_LOWERED | FR_DARKENED);
    02273       break;
    02274
    02275     case WWT_CAPTION:
    02276       if (this->index >= 0) w->SetStringParameters(this->index);
    02277       DrawCaption(r, this->colour, w->owner, this->widget_data);
    02278       break;
    02279
    02280     case WWT_SHADEBOX:
    02281       assert(this->widget_data == 0);
    02282       DrawShadeBox(r, this->colour, w->IsShaded());
    02283       break;
    02284
    02285     case WWT_DEBUGBOX:
    02286       DrawDebugBox(r, this->colour, clicked);
    02287       break;
    02288
    02289     case WWT_STICKYBOX:
    02290       assert(this->widget_data == 0);
    02291       DrawStickyBox(r, this->colour, !!(w->flags4 & WF_STICKY));
    02292       break;
    02293
    02294     case WWT_RESIZEBOX:
    02295       assert(this->widget_data == 0);
    02296       DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags4 & WF_SIZING));
    02297       break;
    02298
    02299     case WWT_CLOSEBOX:
    02300       DrawCloseBox(r, this->colour, this->widget_data);
    02301       break;
    02302
    02303     case WWT_DROPDOWN:
    02304       if (this->index >= 0) w->SetStringParameters(this->index);
    02305       DrawDropdown(r, this->colour, clicked, this->widget_data);
    02306       break;
    02307
    02308     case NWID_BUTTON_DROPDOWN:
    02309       if (this->index >= 0) w->SetStringParameters(this->index);
    02310       DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
    02311       break;
    02312
    02313     default:
    02314       NOT_REACHED();
    02315   }
    02316   if (this->index >= 0) w->DrawWidget(r, this->index);
    02317
    02318   if (this->IsDisabled()) {
    02319     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    02320   }
    02321 }
    02322
    02330 bool NWidgetLeaf::ButtonHit(const Point &pt)
    02331 {
    02332   if (_current_text_dir == TD_LTR) {
    02333     int button_width = this->pos_x + this->current_x - 12;
    02334     return pt.x < button_width;
    02335   } else {
    02336     int button_left = this->pos_x + 12;
    02337     return pt.x >= button_left;
    02338   }
    02339 }
    02340
    02341 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
    02342
    02358 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
    02359 {
    02360   int num_used = 0;
    02361
    02362   *dest = NULL;
    02363   *fill_dest = false;
    02364
    02365   while (count > num_used) {
    02366     switch (parts->type) {
    02367       case NWID_SPACER:
    02368         if (*dest != NULL) return num_used;
    02369         *dest = new NWidgetSpacer(0, 0);
    02370         break;
    02371
    02372       case NWID_HORIZONTAL:
    02373         if (*dest != NULL) return num_used;
    02374         *dest = new NWidgetHorizontal(parts->u.cont_flags);
    02375         *fill_dest = true;
    02376         break;
    02377
    02378       case NWID_HORIZONTAL_LTR:
    02379         if (*dest != NULL) return num_used;
    02380         *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
    02381         *fill_dest = true;
    02382         break;
    02383
    02384       case WWT_PANEL:
    02385       case WWT_INSET:
    02386       case WWT_FRAME:
    02387         if (*dest != NULL) return num_used;
    02388         *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02389         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02390         *fill_dest = true;
    02391         break;
    02392
    02393       case NWID_VERTICAL:
    02394         if (*dest != NULL) return num_used;
    02395         *dest = new NWidgetVertical(parts->u.cont_flags);
    02396         *fill_dest = true;
    02397         break;
    02398
    02399       case NWID_MATRIX: {
    02400         if (*dest != NULL) return num_used;
    02401         NWidgetMatrix *nwm = new NWidgetMatrix();
    02402         *dest = nwm;
    02403         *fill_dest = true;
    02404         nwm->SetIndex(parts->u.widget.index);
    02405         nwm->SetColour(parts->u.widget.colour);
    02406         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02407         break;
    02408       }
    02409
    02410       case WPT_FUNCTION: {
    02411         if (*dest != NULL) return num_used;
    02412         /* Ensure proper functioning even when the called code simply writes its largest index. */
    02413         int biggest = -1;
    02414         *dest = parts->u.func_ptr(&biggest);
    02415         *biggest_index = max(*biggest_index, biggest);
    02416         *fill_dest = false;
    02417         break;
    02418       }
    02419
    02420       case WPT_RESIZE: {
    02421         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02422         if (nwrb != NULL) {
    02423           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02424           nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
    02425         }
    02426         break;
    02427       }
    02428
    02429       case WPT_MINSIZE: {
    02430         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02431         if (nwrb != NULL) {
    02432           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02433           nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
    02434         }
    02435         break;
    02436       }
    02437
    02438       case WPT_MINTEXTLINES: {
    02439         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02440         if (nwrb != NULL) {
    02441           assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
    02442           nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
    02443         }
    02444         break;
    02445       }
    02446
    02447       case WPT_FILL: {
    02448         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02449         if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
    02450         break;
    02451       }
    02452
    02453       case WPT_DATATIP: {
    02454         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02455         if (nwc != NULL) {
    02456           nwc->widget_data = parts->u.data_tip.data;
    02457           nwc->tool_tip = parts->u.data_tip.tooltip;
    02458         }
    02459         break;
    02460       }
    02461
    02462       case WPT_PADDING:
    02463         if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
    02464         break;
    02465
    02466       case WPT_PIPSPACE: {
    02467         NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
    02468         if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02469
    02470         NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
    02471         if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02472         break;
    02473       }
    02474
    02475       case WPT_SCROLLBAR: {
    02476         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02477         if (nwc != NULL) {
    02478           nwc->scrollbar_index = parts->u.widget.index;
    02479         }
    02480         break;
    02481       }
    02482
    02483       case WPT_ENDCONTAINER:
    02484         return num_used;
    02485
    02486       case NWID_VIEWPORT:
    02487         if (*dest != NULL) return num_used;
    02488         *dest = new NWidgetViewport(parts->u.widget.index);
    02489         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02490         break;
    02491
    02492       case NWID_HSCROLLBAR:
    02493       case NWID_VSCROLLBAR:
    02494         if (*dest != NULL) return num_used;
    02495         *dest = new NWidgetScrollbar(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02496         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02497         break;
    02498
    02499       case NWID_SELECTION: {
    02500         if (*dest != NULL) return num_used;
    02501         NWidgetStacked *nws = new NWidgetStacked();
    02502         *dest = nws;
    02503         *fill_dest = true;
    02504         nws->SetIndex(parts->u.widget.index);
    02505         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02506         break;
    02507       }
    02508
    02509       default:
    02510         if (*dest != NULL) return num_used;
    02511         assert((parts->type & WWT_MASK) < WWT_LAST || parts->type == NWID_BUTTON_DROPDOWN);
    02512         *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
    02513         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02514         break;
    02515     }
    02516     num_used++;
    02517     parts++;
    02518   }
    02519
    02520   return num_used;
    02521 }
    02522
    02532 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
    02533 {
    02534   /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
    02535    * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
    02536   NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
    02537   NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
    02538   assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
    02539
    02540   int total_used = 0;
    02541   while (true) {
    02542     NWidgetBase *sub_widget = NULL;
    02543     bool fill_sub = false;
    02544     int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
    02545     parts += num_used;
    02546     total_used += num_used;
    02547
    02548     /* Break out of loop when end reached */
    02549     if (sub_widget == NULL) break;
    02550
    02551     /* If sub-widget is a container, recursively fill that container. */
    02552     WidgetType tp = sub_widget->type;
    02553     if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
    02554               || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
    02555       NWidgetBase *sub_ptr = sub_widget;
    02556       int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
    02557       parts += num_used;
    02558       total_used += num_used;
    02559     }
    02560
    02561     /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
    02562     if (nwid_cont) nwid_cont->Add(sub_widget);
    02563     if (nwid_parent) nwid_parent->Add(sub_widget);
    02564     if (!nwid_cont && !nwid_parent) {
    02565       *parent = sub_widget;
    02566       return total_used;
    02567     }
    02568   }
    02569
    02570   if (count == total_used) return total_used; // Reached the end of the array of parts?
    02571
    02572   assert(total_used < count);
    02573   assert(parts->type == WPT_ENDCONTAINER);
    02574   return total_used + 1; // *parts is also 'used'
    02575 }
    02576
    02588 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
    02589 {
    02590   *biggest_index = -1;
    02591   if (container == NULL) container = new NWidgetVertical();
    02592   NWidgetBase *cont_ptr = container;
    02593   MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
    02594   return container;
    02595 }
    02596
    02610 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
    02611 {
    02612   *biggest_index = -1;
    02613
    02614   /* Read the first widget recursively from the array. */
    02615   NWidgetBase *nwid = NULL;
    02616   int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
    02617   assert(nwid != NULL);
    02618   parts += num_used;
    02619   count -= num_used;
    02620
    02621   NWidgetContainer *root = new NWidgetVertical;
    02622   root->Add(nwid);
    02623   if (count == 0) { // There is no body at all.
    02624     *shade_select = NULL;
    02625     return root;
    02626   }
    02627
    02628   /* If the first widget looks like a titlebar, treat it as such.
    02629    * If it has a shading box, silently add a shade selection widget in the tree. */
    02630   NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
    02631   NWidgetContainer *body;
    02632   if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
    02633     *shade_select = new NWidgetStacked;
    02634     root->Add(*shade_select);
    02635     body = new NWidgetVertical;
    02636     (*shade_select)->Add(body);
    02637   } else {
    02638     *shade_select = NULL;
    02639     body = root;
    02640   }
    02641
    02642   /* Load the remaining parts into 'body'. */
    02643   int biggest2 = -1;
    02644   MakeNWidgets(parts, count, &biggest2, body);
    02645
    02646   *biggest_index = max(*biggest_index, biggest2);
    02647   return root;
    02648 }
    02649
    02660 NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
    02661 {
    02662   NWidgetVertical *vert = NULL; // Storage for all rows.
    02663   NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
    02664   int hor_length = 0;
    02665
    02666   Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
    02667   sprite_size.width  += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
    02668   sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
    02669
    02670   for (int widnum = widget_first; widnum <= widget_last; widnum++) {
    02671     /* Ensure there is room in 'hor' for another button. */
    02672     if (hor_length == max_length) {
    02673       if (vert == NULL) vert = new NWidgetVertical();
    02674       vert->Add(hor);
    02675       hor = NULL;
    02676       hor_length = 0;
    02677     }
    02678     if (hor == NULL) {
    02679       hor = new NWidgetHorizontal();
    02680       hor_length = 0;
    02681     }
    02682
    02683     NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
    02684     panel->SetMinimalSize(sprite_size.width, sprite_size.height);
    02685     panel->SetFill(1, 0);
    02686     panel->SetResize(1, 0);
    02687     panel->SetDataTip(0x0, button_tooltip);
    02688     hor->Add(panel);
    02689     hor_length++;
    02690   }
    02691   *biggest_index = widget_last;
    02692   if (vert == NULL) return hor; // All buttons fit in a single row.
    02693
    02694   if (hor_length > 0 && hor_length < max_length) {
    02695     /* Last row is partial, add a spacer at the end to force all buttons to the left. */
    02696     NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
    02697     spc->SetFill(1, 0);
    02698     spc->SetResize(1, 0);
    02699     hor->Add(spc);
    02700   }
    02701   if (hor != NULL) vert->Add(hor);
    02702   return vert;
    02703 }