# widget.cpp

Go to the documentation of this file.

    00001 /* $Id: widget.cpp 20091 2010-07-08 19:39:20Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "company_func.h"
    00014 #include "window_gui.h"
    00015 #include "viewport_func.h"
    00016 #include "zoom_func.h"
    00017 #include "strings_func.h"
    00018 #include "transparency.h"
    00019 #include "core/geometry_func.hpp"
    00020
    00021 #include "table/sprites.h"
    00022 #include "table/strings.h"
    00023
    00024 static const char *UPARROW   = "\xEE\x8A\xA0";
    00025 static const char *DOWNARROW = "\xEE\x8A\xAA";
    00026
    00036 static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bool horizontal)
    00037 {
    00038   /* Base for reversion */
    00039   int rev_base = top + bottom;
    00040   top += 10;   // top    points to just below the up-button
    00041   bottom -= 9; // bottom points to top of the down-button
    00042
    00043   int height = (bottom - top);
    00044   int pos = sb->GetPosition();
    00045   int count = sb->GetCount();
    00046   int cap = sb->GetCapacity();
    00047
    00048   if (count != 0) top += height * pos / count;
    00049
    00050   if (cap > count) cap = count;
    00051   if (count != 0) bottom -= (count - pos - cap) * height / count;
    00052
    00053   Point pt;
    00054   if (horizontal && _dynlang.text_dir == TD_RTL) {
    00055     pt.x = rev_base - (bottom - 1);
    00056     pt.y = rev_base - top;
    00057   } else {
    00058     pt.x = top;
    00059     pt.y = bottom - 1;
    00060   }
    00061   return pt;
    00062 }
    00063
    00073 static void ScrollbarClickPositioning(Window *w, WidgetType wtp, int x, int y, int mi, int ma)
    00074 {
    00075   int pos;
    00076   Scrollbar *sb;
    00077   bool rtl = false;
    00078
    00079   switch (wtp) {
    00080     case WWT_SCROLLBAR:
    00081       /* vertical scroller */
    00082       w->flags4 &= ~WF_HSCROLL;
    00083       w->flags4 &= ~WF_SCROLL2;
    00084       pos = y;
    00085       sb = &w->vscroll;
    00086       break;
    00087
    00088     case WWT_SCROLL2BAR:
    00089       /* 2nd vertical scroller */
    00090       w->flags4 &= ~WF_HSCROLL;
    00091       w->flags4 |= WF_SCROLL2;
    00092       pos = y;
    00093       sb = &w->vscroll2;
    00094       break;
    00095
    00096     case WWT_HSCROLLBAR:
    00097       /* horizontal scroller */
    00098       w->flags4 &= ~WF_SCROLL2;
    00099       w->flags4 |= WF_HSCROLL;
    00100       pos = x;
    00101       sb = &w->hscroll;
    00102       rtl = _dynlang.text_dir == TD_RTL;
    00103       break;
    00104
    00105     default: NOT_REACHED();
    00106   }
    00107   if (pos <= mi + 9) {
    00108     /* Pressing the upper button? */
    00109     w->flags4 |= WF_SCROLL_UP;
    00110     if (_scroller_click_timeout == 0) {
    00111       _scroller_click_timeout = 6;
    00112       sb->UpdatePosition(rtl ? 1 : -1);
    00113     }
    00114     _left_button_clicked = false;
    00115   } else if (pos >= ma - 10) {
    00116     /* Pressing the lower button? */
    00117     w->flags4 |= WF_SCROLL_DOWN;
    00118
    00119     if (_scroller_click_timeout == 0) {
    00120       _scroller_click_timeout = 6;
    00121       sb->UpdatePosition(rtl ? -1 : 1);
    00122     }
    00123     _left_button_clicked = false;
    00124   } else {
    00125     Point pt = HandleScrollbarHittest(sb, mi, ma, wtp == WWT_HSCROLLBAR);
    00126
    00127     if (pos < pt.x) {
    00128       sb->UpdatePosition(rtl ? sb->GetCapacity() : -sb->GetCapacity());
    00129     } else if (pos > pt.y) {
    00130       sb->UpdatePosition(rtl ? -sb->GetCapacity() : sb->GetCapacity());
    00131     } else {
    00132       _scrollbar_start_pos = pt.x - mi - 9;
    00133       _scrollbar_size = ma - mi - 23;
    00134       w->flags4 |= WF_SCROLL_MIDDLE;
    00135       _scrolling_scrollbar = true;
    00136       _cursorpos_drag_start = _cursor.pos;
    00137     }
    00138   }
    00139
    00140   w->SetDirty();
    00141 }
    00142
    00150 void ScrollbarClickHandler(Window *w, const NWidgetCore *nw, int x, int y)
    00151 {
    00152   int mi, ma;
    00153
    00154   switch (nw->type) {
    00155     case WWT_SCROLLBAR:
    00156       /* vertical scroller */
    00157       mi = nw->pos_y;
    00158       ma = nw->pos_y + nw->current_y;
    00159       break;
    00160
    00161     case WWT_SCROLL2BAR:
    00162       /* 2nd vertical scroller */
    00163       mi = nw->pos_y;
    00164       ma = nw->pos_y + nw->current_y;
    00165       break;
    00166
    00167     case WWT_HSCROLLBAR:
    00168       /* horizontal scroller */
    00169       mi = nw->pos_x;
    00170       ma = nw->pos_x + nw->current_x;
    00171       break;
    00172
    00173     default: NOT_REACHED();
    00174   }
    00175   ScrollbarClickPositioning(w, nw->type, x, y, mi, ma);
    00176 }
    00177
    00185 int GetWidgetFromPos(const Window *w, int x, int y)
    00186 {
    00187   NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
    00188   return (nw != NULL) ? nw->index : -1;
    00189 }
    00190
    00200 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
    00201 {
    00202   uint dark         = _colour_gradient[colour][3];
    00203   uint medium_dark  = _colour_gradient[colour][5];
    00204   uint medium_light = _colour_gradient[colour][6];
    00205   uint light        = _colour_gradient[colour][7];
    00206
    00207   if (flags & FR_TRANSPARENT) {
    00208     GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR);
    00209   } else {
    00210     uint interior;
    00211
    00212     if (flags & FR_LOWERED) {
    00213       GfxFillRect(left,     top,     left,  bottom,     dark);
    00214       GfxFillRect(left + 1, top,     right, top,        dark);
    00215       GfxFillRect(right,    top + 1, right, bottom - 1, light);
    00216       GfxFillRect(left + 1, bottom,  right, bottom,     light);
    00217       interior = (flags & FR_DARKENED ? medium_dark : medium_light);
    00218     } else {
    00219       GfxFillRect(left,     top,    left,      bottom - 1, light);
    00220       GfxFillRect(left + 1, top,    right - 1, top,        light);
    00221       GfxFillRect(right,    top,    right,     bottom - 1, dark);
    00222       GfxFillRect(left,     bottom, right,     bottom,     dark);
    00223       interior = medium_dark;
    00224     }
    00225     if (!(flags & FR_BORDERONLY)) {
    00226       GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, interior);
    00227     }
    00228   }
    00229 }
    00230
    00239 static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
    00240 {
    00241   assert(img != 0);
    00242   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00243
    00244   if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2.
    00245   DrawSprite(img, PAL_NONE, r.left + WD_IMGBTN_LEFT + clicked, r.top + WD_IMGBTN_TOP + clicked);
    00246 }
    00247
    00255 static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
    00256 {
    00257   if (str == STR_NULL) return;
    00258   if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
    00259   Dimension d = GetStringBoundingBox(str);
    00260   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00261   DrawString(r.left + clicked, r.right + clicked, r.top + offset + clicked, str, TC_FROMSTRING, SA_CENTER);
    00262 }
    00263
    00270 static inline void DrawText(const Rect &r, TextColour colour, StringID str)
    00271 {
    00272   Dimension d = GetStringBoundingBox(str);
    00273   int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
    00274   if (str != STR_NULL) DrawString(r.left, r.right, r.top + offset, str, colour);
    00275 }
    00276
    00283 static inline void DrawInset(const Rect &r, Colours colour, StringID str)
    00284 {
    00285   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
    00286   if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str);
    00287 }
    00288
    00296 static inline void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint16 data)
    00297 {
    00298   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00299
    00300   int num_columns = GB(data, MAT_COL_START, MAT_COL_BITS);  // Lower 8 bits of the widget data: Number of columns in the matrix.
    00301   int column_width = (r.right - r.left + 1) / num_columns; // Width of a single column in the matrix.
    00302
    00303   int num_rows = GB(data, MAT_ROW_START, MAT_ROW_BITS); // Upper 8 bits of the widget data: Number of rows in the matrix.
    00304   int row_height = (r.bottom - r.top + 1) / num_rows; // Height of a single row in the matrix.
    00305
    00306   int col = _colour_gradient[colour & 0xF][6];
    00307
    00308   int x = r.left;
    00309   for (int ctr = num_columns; ctr > 1; ctr--) {
    00310     x += column_width;
    00311     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00312   }
    00313
    00314   x = r.top;
    00315   for (int ctr = num_rows; ctr > 1; ctr--) {
    00316     x += row_height;
    00317     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00318   }
    00319
    00320   col = _colour_gradient[colour & 0xF][4];
    00321
    00322   x = r.left - 1;
    00323   for (int ctr = num_columns; ctr > 1; ctr--) {
    00324     x += column_width;
    00325     GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
    00326   }
    00327
    00328   x = r.top - 1;
    00329   for (int ctr = num_rows; ctr > 1; ctr--) {
    00330     x += row_height;
    00331     GfxFillRect(r.left + 1, x, r.right - 1, x, col);
    00332   }
    00333 }
    00334
    00344 static inline void DrawVerticalScrollbar(const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
    00345 {
    00346   /* draw up/down buttons */
    00347   DrawFrameRect(r.left, r.top, r.right, r.top + 9, colour, (up_clicked) ? FR_LOWERED : FR_NONE);
    00348   DrawString(r.left + up_clicked, r.right + up_clicked, r.top + up_clicked, UPARROW, TC_BLACK, SA_CENTER);
    00349
    00350   DrawFrameRect(r.left, r.bottom - 9, r.right, r.bottom, colour, (down_clicked) ? FR_LOWERED : FR_NONE);
    00351   DrawString(r.left + down_clicked, r.right + down_clicked, r.bottom - 9 + down_clicked, DOWNARROW, TC_BLACK, SA_CENTER);
    00352
    00353   int c1 = _colour_gradient[colour & 0xF][3];
    00354   int c2 = _colour_gradient[colour & 0xF][7];
    00355
    00356   /* draw "shaded" background */
    00357   GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c2);
    00358   GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c1, FILLRECT_CHECKER);
    00359
    00360   /* draw shaded lines */
    00361   GfxFillRect(r.left + 2, r.top + 10, r.left + 2, r.bottom - 10, c1);
    00362   GfxFillRect(r.left + 3, r.top + 10, r.left + 3, r.bottom - 10, c2);
    00363   GfxFillRect(r.left + 7, r.top + 10, r.left + 7, r.bottom - 10, c1);
    00364   GfxFillRect(r.left + 8, r.top + 10, r.left + 8, r.bottom - 10, c2);
    00365
    00366   Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom, false);
    00367   DrawFrameRect(r.left, pt.x, r.right, pt.y, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00368 }
    00369
    00379 static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
    00380 {
    00381   DrawFrameRect(r.left, r.top, r.left + 9, r.bottom, colour, left_clicked ? FR_LOWERED : FR_NONE);
    00382   DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + left_clicked, r.top + 1 + left_clicked);
    00383
    00384   DrawFrameRect(r.right - 9, r.top, r.right, r.bottom, colour, right_clicked ? FR_LOWERED : FR_NONE);
    00385   DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - 8 + right_clicked, r.top + 1 + right_clicked);
    00386
    00387   int c1 = _colour_gradient[colour & 0xF][3];
    00388   int c2 = _colour_gradient[colour & 0xF][7];
    00389
    00390   /* draw "shaded" background */
    00391   GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c2);
    00392   GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c1, FILLRECT_CHECKER);
    00393
    00394   /* draw shaded lines */
    00395   GfxFillRect(r.left + 10, r.top + 2, r.right - 10, r.top + 2, c1);
    00396   GfxFillRect(r.left + 10, r.top + 3, r.right - 10, r.top + 3, c2);
    00397   GfxFillRect(r.left + 10, r.top + 7, r.right - 10, r.top + 7, c1);
    00398   GfxFillRect(r.left + 10, r.top + 8, r.right - 10, r.top + 8, c2);
    00399
    00400   /* draw actual scrollbar */
    00401   Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right, true);
    00402   DrawFrameRect(pt.x, r.top, pt.y, r.bottom, colour, bar_dragged ? FR_LOWERED : FR_NONE);
    00403 }
    00404
    00411 static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
    00412 {
    00413   int x2 = r.left; // by default the left side is the left side of the widget
    00414
    00415   if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str);
    00416
    00417   int c1 = _colour_gradient[colour][3];
    00418   int c2 = _colour_gradient[colour][7];
    00419
    00420   /* If the frame has text, adjust the top bar to fit half-way through */
    00421   int dy1 = 4;
    00422   if (str != STR_NULL) dy1 = FONT_HEIGHT_NORMAL / 2 - 1;
    00423   int dy2 = dy1 + 1;
    00424
    00425   if (_dynlang.text_dir == TD_LTR) {
    00426     /* Line from upper left corner to start of text */
    00427     GfxFillRect(r.left, r.top + dy1, r.left + 4, r.top + dy1, c1);
    00428     GfxFillRect(r.left + 1, r.top + dy2, r.left + 4, r.top + dy2, c2);
    00429
    00430     /* Line from end of text to upper right corner */
    00431     GfxFillRect(x2, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00432     GfxFillRect(x2, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00433   } else {
    00434     /* Line from upper left corner to start of text */
    00435     GfxFillRect(r.left, r.top + dy1, x2 - 2, r.top + dy1, c1);
    00436     GfxFillRect(r.left + 1, r.top + dy2, x2 - 2, r.top + dy2, c2);
    00437
    00438     /* Line from end of text to upper right corner */
    00439     GfxFillRect(r.right - 5, r.top + dy1, r.right - 1, r.top + dy1, c1);
    00440     GfxFillRect(r.right - 5, r.top + dy2, r.right - 2, r.top + dy2, c2);
    00441   }
    00442
    00443   /* Line from upper left corner to bottom left corner */
    00444   GfxFillRect(r.left, r.top + dy2, r.left, r.bottom - 1, c1);
    00445   GfxFillRect(r.left + 1, r.top + dy2 + 1, r.left + 1, r.bottom - 2, c2);
    00446
    00447   /* Line from upper right corner to bottom right corner */
    00448   GfxFillRect(r.right - 1, r.top + dy2, r.right - 1, r.bottom - 2, c1);
    00449   GfxFillRect(r.right, r.top + dy1, r.right, r.bottom - 1, c2);
    00450
    00451   GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
    00452   GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
    00453 }
    00454
    00461 static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
    00462 {
    00463   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00464   DrawSprite((clicked) ? SPR_WINDOW_SHADE : SPR_WINDOW_UNSHADE, PAL_NONE, r.left + WD_SHADEBOX_LEFT + clicked, r.top + WD_SHADEBOX_TOP + clicked);
    00465 }
    00466
    00473 static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
    00474 {
    00475   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00476   DrawSprite((clicked) ? SPR_PIN_UP : SPR_PIN_DOWN, PAL_NONE, r.left + WD_STICKYBOX_LEFT + clicked, r.top + WD_STICKYBOX_TOP + clicked);
    00477 }
    00478
    00486 static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
    00487 {
    00488   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
    00489   if (at_left) {
    00490     DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked, r.top + WD_RESIZEBOX_TOP + clicked);
    00491   } else {
    00492     DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.left + WD_RESIZEBOX_LEFT + clicked, r.top + WD_RESIZEBOX_TOP + clicked);
    00493   }
    00494 }
    00495
    00502 static inline void DrawCloseBox(const Rect &r, Colours colour, StringID str)
    00503 {
    00504   assert(str == STR_BLACK_CROSS || str == STR_SILVER_CROSS); // black or silver cross
    00505   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE);
    00506   DrawString(r.left + WD_CLOSEBOX_LEFT, r.right - WD_CLOSEBOX_RIGHT, r.top + WD_CLOSEBOX_TOP, str, TC_FROMSTRING, SA_CENTER);
    00507 }
    00508
    00516 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
    00517 {
    00518   DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_BORDERONLY);
    00519   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);
    00520
    00521   if (owner != INVALID_OWNER) {
    00522     GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]);
    00523   }
    00524
    00525   if (str != STR_NULL) DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + WD_CAPTIONTEXT_TOP, str, TC_FROMSTRING, SA_CENTER);
    00526 }
    00527
    00538 static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
    00539 {
    00540   if (_dynlang.text_dir == TD_LTR) {
    00541     DrawFrameRect(r.left, r.top, r.right - 12, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00542     DrawFrameRect(r.right - 11, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00543     DrawString(r.right - (clicked_dropdown ? 10 : 11), r.right, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_CENTER);
    00544     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);
    00545   } else {
    00546     DrawFrameRect(r.left + 12, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
    00547     DrawFrameRect(r.left, r.top, r.left + 11, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
    00548     DrawString(r.left + clicked_dropdown, r.left + 11, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_CENTER);
    00549     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);
    00550   }
    00551 }
    00552
    00560 static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
    00561 {
    00562   DrawButtonDropdown(r, colour, false, clicked, str);
    00563 }
    00564
    00568 void Window::DrawWidgets() const
    00569 {
    00570   this->nested_root->Draw(this);
    00571
    00572   if (this->flags4 & WF_WHITE_BORDER_MASK) {
    00573     DrawFrameRect(0, 0, this->width - 1, this->height - 1, COLOUR_WHITE, FR_BORDERONLY);
    00574   }
    00575 }
    00576
    00582 void Window::DrawSortButtonState(int widget, SortButtonState state) const
    00583 {
    00584   if (state == SBS_OFF) return;
    00585
    00586   assert(this->nested_array != NULL);
    00587   const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
    00588
    00589   int offset = this->IsWidgetLowered(widget) ? 1 : 0;
    00590   int base = offset + nwid->pos_x + (_dynlang.text_dir == TD_LTR ? nwid->current_x - WD_SORTBUTTON_ARROW_WIDTH : 0);
    00591   int top = nwid->pos_y;
    00592
    00593   DrawString(base, base + WD_SORTBUTTON_ARROW_WIDTH, top + 1 + offset, state == SBS_DOWN ? DOWNARROW : UPARROW, TC_BLACK, SA_CENTER);
    00594 }
    00595
    00596
    00653 NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator()
    00654 {
    00655   this->type = tp;
    00656 }
    00657
    00658 /* ~NWidgetContainer() takes care of #next and #prev data members. */
    00659
    00704 inline void NWidgetBase::StoreSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height)
    00705 {
    00706   this->pos_x = x;
    00707   this->pos_y = y;
    00708   if (sizing == ST_SMALLEST) {
    00709     this->smallest_x = given_width;
    00710     this->smallest_y = given_height;
    00711   }
    00712   this->current_x = given_width;
    00713   this->current_y = given_height;
    00714 }
    00715
    00727 void NWidgetBase::SetDirty(const Window *w) const
    00728 {
    00729   int abs_left = w->left + this->pos_x;
    00730   int abs_top = w->top + this->pos_y;
    00731   SetDirtyBlocks(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
    00732 }
    00733
    00747 NWidgetBase *NWidgetBase::GetWidgetOfType(WidgetType tp)
    00748 {
    00749   return (this->type == tp) ? this : NULL;
    00750 }
    00751
    00758 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y) : NWidgetBase(tp)
    00759 {
    00760   this->fill_x = fill_x;
    00761   this->fill_y = fill_y;
    00762 }
    00763
    00769 void NWidgetResizeBase::SetMinimalSize(uint min_x, uint min_y)
    00770 {
    00771   this->min_x = min_x;
    00772   this->min_y = min_y;
    00773 }
    00774
    00781 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
    00782 {
    00783   this->min_y = min_lines * GetCharacterHeight(size) + spacing;
    00784 }
    00785
    00791 void NWidgetResizeBase::SetFill(uint fill_x, uint fill_y)
    00792 {
    00793   this->fill_x = fill_x;
    00794   this->fill_y = fill_y;
    00795 }
    00796
    00802 void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y)
    00803 {
    00804   this->resize_x = resize_x;
    00805   this->resize_y = resize_y;
    00806 }
    00807
    00808 void NWidgetResizeBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00809 {
    00810   StoreSizePosition(sizing, x, y, given_width, given_height);
    00811 }
    00812
    00822 NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint16 widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y)
    00823 {
    00824   this->colour = colour;
    00825   this->index = -1;
    00826   this->widget_data = widget_data;
    00827   this->tool_tip = tool_tip;
    00828 }
    00829
    00834 void NWidgetCore::SetIndex(int index)
    00835 {
    00836   assert(index >= 0);
    00837   this->index = index;
    00838 }
    00839
    00845 void NWidgetCore::SetDataTip(uint16 widget_data, StringID tool_tip)
    00846 {
    00847   this->widget_data = widget_data;
    00848   this->tool_tip = tool_tip;
    00849 }
    00850
    00851 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
    00852 {
    00853   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    00854 }
    00855
    00856 NWidgetCore *NWidgetCore::GetWidgetFromPos(int x, int y)
    00857 {
    00858   return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : NULL;
    00859 }
    00860
    00877 NWidgetContainer::NWidgetContainer(WidgetType tp) : NWidgetBase(tp)
    00878 {
    00879   this->head = NULL;
    00880   this->tail = NULL;
    00881 }
    00882
    00883 NWidgetContainer::~NWidgetContainer()
    00884 {
    00885   while (this->head != NULL) {
    00886     NWidgetBase *wid = this->head->next;
    00887     delete this->head;
    00888     this->head = wid;
    00889   }
    00890   this->tail = NULL;
    00891 }
    00892
    00893 NWidgetBase *NWidgetContainer::GetWidgetOfType(WidgetType tp)
    00894 {
    00895   if (this->type == tp) return this;
    00896   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00897     NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
    00898     if (nwid != NULL) return nwid;
    00899   }
    00900   return NULL;
    00901 }
    00902
    00907 void NWidgetContainer::Add(NWidgetBase *wid)
    00908 {
    00909   assert(wid->next == NULL && wid->prev == NULL);
    00910
    00911   if (this->head == NULL) {
    00912     this->head = wid;
    00913     this->tail = wid;
    00914   } else {
    00915     assert(this->tail != NULL);
    00916     assert(this->tail->next == NULL);
    00917
    00918     this->tail->next = wid;
    00919     wid->prev = this->tail;
    00920     this->tail = wid;
    00921   }
    00922 }
    00923
    00924 void NWidgetContainer::FillNestedArray(NWidgetBase **array, uint length)
    00925 {
    00926   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00927     child_wid->FillNestedArray(array, length);
    00928   }
    00929 }
    00930
    00938 static inline uint ComputeMaxSize(uint base, uint max_space, uint step)
    00939 {
    00940   if (base >= max_space || step == 0) return base;
    00941   if (step == 1) return max_space;
    00942   int increment = max_space - base;
    00943   increment -= increment % step;
    00944   return base + increment;
    00945 }
    00946
    00950 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION)
    00951 {
    00952   this->index = -1;
    00953 }
    00954
    00955 void NWidgetStacked::SetIndex(int index)
    00956 {
    00957   this->index = index;
    00958 }
    00959
    00960 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
    00961 {
    00962   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    00963     assert(w->nested_array_size > (uint)this->index);
    00964     w->nested_array[this->index] = this;
    00965   }
    00966
    00967   /* Zero size plane selected */
    00968   if (this->shown_plane >= SZSP_BEGIN) {
    00969     Dimension size    = {0, 0};
    00970     Dimension padding = {0, 0};
    00971     Dimension fill    = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00972     Dimension resize  = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
    00973     /* Here we're primarily interested in the value of resize */
    00974     if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
    00975
    00976     this->smallest_x = size.width;
    00977     this->smallest_y = size.height;
    00978     this->fill_x = fill.width;
    00979     this->fill_y = fill.height;
    00980     this->resize_x = resize.width;
    00981     this->resize_y = resize.height;
    00982     return;
    00983   }
    00984
    00985   /* First sweep, recurse down and compute minimal size and filling. */
    00986   this->smallest_x = 0;
    00987   this->smallest_y = 0;
    00988   this->fill_x = (this->head != NULL) ? 1 : 0;
    00989   this->fill_y = (this->head != NULL) ? 1 : 0;
    00990   this->resize_x = (this->head != NULL) ? 1 : 0;
    00991   this->resize_y = (this->head != NULL) ? 1 : 0;
    00992   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00993     child_wid->SetupSmallestSize(w, init_array);
    00994
    00995     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    00996     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    00997     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    00998     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    00999     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    01000     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    01001   }
    01002 }
    01003
    01004 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01005 {
    01006   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01007   StoreSizePosition(sizing, x, y, given_width, given_height);
    01008
    01009   if (this->shown_plane >= SZSP_BEGIN) return;
    01010
    01011   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01012     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    01013     uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    01014     uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
    01015
    01016     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    01017     uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    01018     uint child_pos_y = child_wid->padding_top;
    01019
    01020     child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
    01021   }
    01022 }
    01023
    01024 void NWidgetStacked::FillNestedArray(NWidgetBase **array, uint length)
    01025 {
    01026   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01027   NWidgetContainer::FillNestedArray(array, length);
    01028 }
    01029
    01030 void NWidgetStacked::Draw(const Window *w)
    01031 {
    01032   if (this->shown_plane >= SZSP_BEGIN) return;
    01033
    01034   int plane = 0;
    01035   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01036     if (plane == this->shown_plane) {
    01037       child_wid->Draw(w);
    01038       return;
    01039     }
    01040   }
    01041
    01042   NOT_REACHED();
    01043 }
    01044
    01045 NWidgetCore *NWidgetStacked::GetWidgetFromPos(int x, int y)
    01046 {
    01047   if (this->shown_plane >= SZSP_BEGIN) return NULL;
    01048
    01049   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01050   int plane = 0;
    01051   for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
    01052     if (plane == this->shown_plane) {
    01053       return child_wid->GetWidgetFromPos(x, y);
    01054     }
    01055   }
    01056   return NULL;
    01057 }
    01058
    01062 void NWidgetStacked::SetDisplayedPlane(int plane)
    01063 {
    01064   this->shown_plane = plane;
    01065 }
    01066
    01067 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
    01068 {
    01069   this->flags = flags;
    01070 }
    01071
    01081 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01082 {
    01083   this->pip_pre = pip_pre;
    01084   this->pip_inter = pip_inter;
    01085   this->pip_post = pip_post;
    01086 }
    01087
    01088 void NWidgetPIPContainer::Draw(const Window *w)
    01089 {
    01090   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01091     child_wid->Draw(w);
    01092   }
    01093 }
    01094
    01095 NWidgetCore *NWidgetPIPContainer::GetWidgetFromPos(int x, int y)
    01096 {
    01097   if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01098
    01099   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01100     NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
    01101     if (nwid != NULL) return nwid;
    01102   }
    01103   return NULL;
    01104 }
    01105
    01107 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
    01108 {
    01109 }
    01110
    01111 void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array)
    01112 {
    01113   this->smallest_x = 0; // Sum of minimal size of all childs.
    01114   this->smallest_y = 0; // Biggest child.
    01115   this->fill_x = 0;     // smallest non-zero child widget fill step.
    01116   this->fill_y = 1;     // smallest common child fill step.
    01117   this->resize_x = 0;   // smallest non-zero child widget resize step.
    01118   this->resize_y = 1;   // smallest common child resize step.
    01119
    01120   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01121   uint longest = 0; // Longest child found.
    01122   uint max_vert_fill = 0; // Biggest vertical fill step.
    01123   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01124     child_wid->SetupSmallestSize(w, init_array);
    01125     longest = max(longest, child_wid->smallest_x);
    01126     max_vert_fill = max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
    01127     this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    01128   }
    01129   /* 1b. Make the container higher if needed to accomadate all childs nicely. */
    01130   uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height.
    01131   uint cur_height = this->smallest_y;
    01132   while (true) {
    01133     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01134       uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
    01135       uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01136       if (step_size > 1 && child_height < cur_height) { // Small step sizes or already fitting childs are not interesting.
    01137         uint remainder = (cur_height - child_height) % step_size;
    01138         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01139           cur_height += step_size - remainder;
    01140           assert(cur_height < max_smallest); // Safeguard against infinite height expansion.
    01141           /* Remaining childs will adapt to the new cur_height, thus speeding up the computation. */
    01142         }
    01143       }
    01144     }
    01145     if (this->smallest_y == cur_height) break;
    01146     this->smallest_y = cur_height; // Smallest height got changed, try again.
    01147   }
    01148   /* 2. For containers that must maintain equal width, extend child minimal size. */
    01149   if (this->flags & NC_EQUALSIZE) {
    01150     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01151       if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
    01152     }
    01153   }
    01154   /* 3. Move PIP space to the childs, compute smallest, fill, and resize values of the container. */
    01155   if (this->head != NULL) this->head->padding_left += this->pip_pre;
    01156   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01157     if (child_wid->next != NULL) {
    01158       child_wid->padding_right += this->pip_inter;
    01159     } else {
    01160       child_wid->padding_right += this->pip_post;
    01161     }
    01162
    01163     this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01164     if (child_wid->fill_x > 0) {
    01165       if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
    01166     }
    01167     this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
    01168
    01169     if (child_wid->resize_x > 0) {
    01170       if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
    01171     }
    01172     this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
    01173   }
    01174   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01175   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01176 }
    01177
    01178 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01179 {
    01180   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01181
    01182   uint additional_length = given_width - this->smallest_x; // Additional width given to us.
    01183   StoreSizePosition(sizing, x, y, given_width, given_height);
    01184
    01185   /* In principle, the additional horizontal space is distributed evenly over the available resizable childs. Due to step sizes, this may not always be feasible.
    01186    * To make resizing work as good as possible, first childs with biggest step sizes are done. These may get less due to rounding down.
    01187    * 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
    01188    * of the child with the smallest non-zero stepsize.
    01189    *
    01190    * 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
    01191    * size and position, and directly call child->AssignSizePosition() with the computed values.
    01192    * 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
    01193    * then we call the child.
    01194    */
    01195
    01196   /* 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. */
    01197   int num_changing_childs = 0; // Number of childs that can change size.
    01198   uint biggest_stepsize = 0;
    01199   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01200     uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01201     if (hor_step > 0) {
    01202       num_changing_childs++;
    01203       biggest_stepsize = max(biggest_stepsize, hor_step);
    01204     } else {
    01205       child_wid->current_x = child_wid->smallest_x;
    01206     }
    01207
    01208     uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
    01209     child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
    01210   }
    01211
    01212   /* Second loop: Allocate the additional horizontal space over the resizing childs, starting with the biggest resize steps. */
    01213   while (biggest_stepsize > 0) {
    01214     uint next_biggest_stepsize = 0;
    01215     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01216       uint hor_step = child_wid->GetHorizontalStepSize(sizing);
    01217       if (hor_step > biggest_stepsize) continue; // Already done
    01218       if (hor_step == biggest_stepsize) {
    01219         uint increment = additional_length / num_changing_childs;
    01220         num_changing_childs--;
    01221         if (hor_step > 1) increment -= increment % hor_step;
    01222         child_wid->current_x = child_wid->smallest_x + increment;
    01223         additional_length -= increment;
    01224         continue;
    01225       }
    01226       next_biggest_stepsize = max(next_biggest_stepsize, hor_step);
    01227     }
    01228     biggest_stepsize = next_biggest_stepsize;
    01229   }
    01230   assert(num_changing_childs == 0);
    01231
    01232   /* Third loop: Compute position and call the child. */
    01233   uint position = 0; // Place to put next child relative to origin of the container.
    01234   NWidgetBase *child_wid = rtl ? this->tail : this->head;
    01235   while (child_wid != NULL) {
    01236     uint child_width = child_wid->current_x;
    01237     uint child_x = x + position + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01238     uint child_y = y + child_wid->padding_top;
    01239
    01240     child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
    01241     position += child_width + child_wid->padding_right + child_wid->padding_left;
    01242
    01243     child_wid = rtl ? child_wid->prev : child_wid->next;
    01244   }
    01245 }
    01246
    01248 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
    01249 {
    01250   this->type = NWID_HORIZONTAL_LTR;
    01251 }
    01252
    01253 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01254 {
    01255   NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
    01256 }
    01257
    01259 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
    01260 {
    01261 }
    01262
    01263 void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array)
    01264 {
    01265   this->smallest_x = 0; // Biggest child.
    01266   this->smallest_y = 0; // Sum of minimal size of all childs.
    01267   this->fill_x = 1;     // smallest common child fill step.
    01268   this->fill_y = 0;     // smallest non-zero child widget fill step.
    01269   this->resize_x = 1;   // smallest common child resize step.
    01270   this->resize_y = 0;   // smallest non-zero child widget resize step.
    01271
    01272   /* 1a. Forward call, collect biggest nested array index, and longest/widest child length. */
    01273   uint highest = 0; // Highest child found.
    01274   uint max_hor_fill = 0; // Biggest horizontal fill step.
    01275   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01276     child_wid->SetupSmallestSize(w, init_array);
    01277     highest = max(highest, child_wid->smallest_y);
    01278     max_hor_fill = max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
    01279     this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
    01280   }
    01281   /* 1b. Make the container wider if needed to accomadate all childs nicely. */
    01282   uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height.
    01283   uint cur_width = this->smallest_x;
    01284   while (true) {
    01285     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01286       uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
    01287       uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
    01288       if (step_size > 1 && child_width < cur_width) { // Small step sizes or already fitting childs are not interesting.
    01289         uint remainder = (cur_width - child_width) % step_size;
    01290         if (remainder > 0) { // Child did not fit entirely, widen the container.
    01291           cur_width += step_size - remainder;
    01292           assert(cur_width < max_smallest); // Safeguard against infinite width expansion.
    01293           /* Remaining childs will adapt to the new cur_width, thus speeding up the computation. */
    01294         }
    01295       }
    01296     }
    01297     if (this->smallest_x == cur_width) break;
    01298     this->smallest_x = cur_width; // Smallest width got changed, try again.
    01299   }
    01300   /* 2. For containers that must maintain equal width, extend child minimal size. */
    01301   if (this->flags & NC_EQUALSIZE) {
    01302     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01303       if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
    01304     }
    01305   }
    01306   /* 3. Move PIP space to the childs, compute smallest, fill, and resize values of the container. */
    01307   if (this->head != NULL) this->head->padding_top += this->pip_pre;
    01308   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01309     if (child_wid->next != NULL) {
    01310       child_wid->padding_bottom += this->pip_inter;
    01311     } else {
    01312       child_wid->padding_bottom += this->pip_post;
    01313     }
    01314
    01315     this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
    01316     if (child_wid->fill_y > 0) {
    01317       if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
    01318     }
    01319     this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
    01320
    01321     if (child_wid->resize_y > 0) {
    01322       if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
    01323     }
    01324     this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
    01325   }
    01326   /* We need to zero the PIP settings so we can re-initialize the tree. */
    01327   this->pip_pre = this->pip_inter = this->pip_post = 0;
    01328 }
    01329
    01330 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01331 {
    01332   assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    01333
    01334   int additional_length = given_height - this->smallest_y; // Additional height given to us.
    01335   StoreSizePosition(sizing, x, y, given_width, given_height);
    01336
    01337   /* Like the horizontal container, the vertical container also distributes additional height evenly, starting with the childs with the biggest resize steps.
    01338    * It also stores computed widths and heights into current_x and current_y values of the child.
    01339    */
    01340
    01341   /* 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. */
    01342   int num_changing_childs = 0; // Number of childs that can change size.
    01343   uint biggest_stepsize = 0;
    01344   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01345     uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01346     if (vert_step > 0) {
    01347       num_changing_childs++;
    01348       biggest_stepsize = max(biggest_stepsize, vert_step);
    01349     } else {
    01350       child_wid->current_y = child_wid->smallest_y;
    01351     }
    01352
    01353     uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
    01354     child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
    01355   }
    01356
    01357   /* Second loop: Allocate the additional vertical space over the resizing childs, starting with the biggest resize steps. */
    01358   while (biggest_stepsize > 0) {
    01359     uint next_biggest_stepsize = 0;
    01360     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01361       uint vert_step = child_wid->GetVerticalStepSize(sizing);
    01362       if (vert_step > biggest_stepsize) continue; // Already done
    01363       if (vert_step == biggest_stepsize) {
    01364         uint increment = additional_length / num_changing_childs;
    01365         num_changing_childs--;
    01366         if (vert_step > 1) increment -= increment % vert_step;
    01367         child_wid->current_y = child_wid->smallest_y + increment;
    01368         additional_length -= increment;
    01369         continue;
    01370       }
    01371       next_biggest_stepsize = max(next_biggest_stepsize, vert_step);
    01372     }
    01373     biggest_stepsize = next_biggest_stepsize;
    01374   }
    01375   assert(num_changing_childs == 0);
    01376
    01377   /* Third loop: Compute position and call the child. */
    01378   uint position = 0; // Place to put next child relative to origin of the container.
    01379   for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    01380     uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
    01381     uint child_height = child_wid->current_y;
    01382
    01383     child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
    01384     position += child_height + child_wid->padding_top + child_wid->padding_bottom;
    01385   }
    01386 }
    01387
    01393 NWidgetSpacer::NWidgetSpacer(int length, int height) : NWidgetResizeBase(NWID_SPACER, 0, 0)
    01394 {
    01395   this->SetMinimalSize(length, height);
    01396   this->SetResize(0, 0);
    01397 }
    01398
    01399 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
    01400 {
    01401   this->smallest_x = this->min_x;
    01402   this->smallest_y = this->min_y;
    01403 }
    01404
    01405 void NWidgetSpacer::FillNestedArray(NWidgetBase **array, uint length)
    01406 {
    01407 }
    01408
    01409 void NWidgetSpacer::Draw(const Window *w)
    01410 {
    01411   /* Spacer widget is never visible. */
    01412 }
    01413
    01414 void NWidgetSpacer::SetDirty(const Window *w) const
    01415 {
    01416   /* Spacer widget never need repainting. */
    01417 }
    01418
    01419 NWidgetCore *NWidgetSpacer::GetWidgetFromPos(int x, int y)
    01420 {
    01421   return NULL;
    01422 }
    01423
    01433 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
    01434 {
    01435   assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
    01436   if (index >= 0) this->SetIndex(index);
    01437   this->child = child;
    01438 }
    01439
    01440 NWidgetBackground::~NWidgetBackground()
    01441 {
    01442   if (this->child != NULL) delete this->child;
    01443 }
    01444
    01452 void NWidgetBackground::Add(NWidgetBase *nwid)
    01453 {
    01454   if (this->child == NULL) {
    01455     this->child = new NWidgetVertical();
    01456   }
    01457   this->child->Add(nwid);
    01458 }
    01459
    01470 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
    01471 {
    01472   if (this->child == NULL) {
    01473     this->child = new NWidgetVertical();
    01474   }
    01475   this->child->SetPIP(pip_pre, pip_inter, pip_post);
    01476 }
    01477
    01478 void NWidgetBackground::SetupSmallestSize(Window *w, bool init_array)
    01479 {
    01480   if (init_array && this->index >= 0) {
    01481     assert(w->nested_array_size > (uint)this->index);
    01482     w->nested_array[this->index] = this;
    01483   }
    01484   if (this->child != NULL) {
    01485     this->child->SetupSmallestSize(w, init_array);
    01486
    01487     this->smallest_x = this->child->smallest_x;
    01488     this->smallest_y = this->child->smallest_y;
    01489     this->fill_x = this->child->fill_x;
    01490     this->fill_y = this->child->fill_y;
    01491     this->resize_x = this->child->resize_x;
    01492     this->resize_y = this->child->resize_y;
    01493
    01494     /* Account for the size of the frame's text if that exists */
    01495     if (w != NULL && this->type == WWT_FRAME) {
    01496       this->child->padding_left   = WD_FRAMETEXT_LEFT;
    01497       this->child->padding_right  = WD_FRAMETEXT_RIGHT;
    01498       this->child->padding_top    = max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
    01499       this->child->padding_bottom = WD_FRAMETEXT_BOTTOM;
    01500
    01501       this->smallest_x += this->child->padding_left + this->child->padding_right;
    01502       this->smallest_y += this->child->padding_top + this->child->padding_bottom;
    01503
    01504       if (this->index >= 0) w->SetStringParameters(this->index);
    01505       this->smallest_x = max(this->smallest_x, GetStringBoundingBox(this->widget_data).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
    01506     }
    01507   } else {
    01508     Dimension d = {this->min_x, this->min_y};
    01509     Dimension fill = {this->fill_x, this->fill_y};
    01510     Dimension resize  = {this->resize_x, this->resize_y};
    01511     if (w != NULL) { // A non-NULL window pointer acts as switch to turn dynamic widget size on.
    01512       if (this->type == WWT_FRAME || this->type == WWT_INSET) {
    01513         if (this->index >= 0) w->SetStringParameters(this->index);
    01514         Dimension background = GetStringBoundingBox(this->widget_data);
    01515         background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
    01516         d = maxdim(d, background);
    01517       }
    01518       if (this->index >= 0) {
    01519         static const Dimension padding = {0, 0};
    01520         w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
    01521       }
    01522     }
    01523     this->smallest_x = d.width;
    01524     this->smallest_y = d.height;
    01525     this->fill_x = fill.width;
    01526     this->fill_y = fill.height;
    01527     this->resize_x = resize.width;
    01528     this->resize_y = resize.height;
    01529   }
    01530 }
    01531
    01532 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01533 {
    01534   StoreSizePosition(sizing, x, y, given_width, given_height);
    01535
    01536   if (this->child != NULL) {
    01537     uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
    01538     uint width = given_width - this->child->padding_right - this->child->padding_left;
    01539     uint height = given_height - this->child->padding_top - this->child->padding_bottom;
    01540     this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
    01541   }
    01542 }
    01543
    01544 void NWidgetBackground::FillNestedArray(NWidgetBase **array, uint length)
    01545 {
    01546   if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
    01547   if (this->child != NULL) this->child->FillNestedArray(array, length);
    01548 }
    01549
    01550 void NWidgetBackground::Draw(const Window *w)
    01551 {
    01552   if (this->current_x == 0 || this->current_y == 0) return;
    01553
    01554   Rect r;
    01555   r.left = this->pos_x;
    01556   r.right = this->pos_x + this->current_x - 1;
    01557   r.top = this->pos_y;
    01558   r.bottom = this->pos_y + this->current_y - 1;
    01559
    01560   const DrawPixelInfo *dpi = _cur_dpi;
    01561   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01562
    01563   switch (this->type) {
    01564     case WWT_PANEL:
    01565       assert(this->widget_data == 0);
    01566       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
    01567       break;
    01568
    01569     case WWT_FRAME:
    01570       if (this->index >= 0) w->SetStringParameters(this->index);
    01571       DrawFrame(r, this->colour, this->widget_data);
    01572       break;
    01573
    01574     case WWT_INSET:
    01575       if (this->index >= 0) w->SetStringParameters(this->index);
    01576       DrawInset(r, this->colour, this->widget_data);
    01577       break;
    01578
    01579     default:
    01580       NOT_REACHED();
    01581   }
    01582
    01583   if (this->index >= 0) w->DrawWidget(r, this->index);
    01584   if (this->child != NULL) this->child->Draw(w);
    01585
    01586   if (this->IsDisabled()) {
    01587     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    01588   }
    01589 }
    01590
    01591 NWidgetCore *NWidgetBackground::GetWidgetFromPos(int x, int y)
    01592 {
    01593   NWidgetCore *nwid = NULL;
    01594   if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
    01595     if (this->child != NULL) nwid = this->child->GetWidgetFromPos(x, y);
    01596     if (nwid == NULL) nwid = this;
    01597   }
    01598   return nwid;
    01599 }
    01600
    01601 Scrollbar *NWidgetBackground::FindScrollbar(Window *w, bool allow_next) const
    01602 {
    01603   if (this->index >= 0 && allow_next && this->child == NULL && (uint)(this->index) + 1 < w->nested_array_size) {
    01604     /* GetWidget ensures that the widget is of the given type.
    01605      * As we might have cases where the next widget in the array
    01606      * is a non-Core widget (e.g. NWID_SELECTION) we first get
    01607      * the base class and then dynamic_cast that. */
    01608     const NWidgetCore *next_wid = dynamic_cast<NWidgetCore*>(w->GetWidget<NWidgetBase>(this->index + 1));
    01609     if (next_wid != NULL) return next_wid->FindScrollbar(w, false);
    01610   }
    01611   return NULL;
    01612 }
    01613
    01614 NWidgetBase *NWidgetBackground::GetWidgetOfType(WidgetType tp)
    01615 {
    01616   NWidgetBase *nwid = NULL;
    01617   if (this->child != NULL) nwid = this->child->GetWidgetOfType(tp);
    01618   if (nwid == NULL && this->type == tp) nwid = this;
    01619   return nwid;
    01620 }
    01621
    01622 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
    01623 {
    01624   this->SetIndex(index);
    01625 }
    01626
    01627 void NWidgetViewport::SetupSmallestSize(Window *w, bool init_array)
    01628 {
    01629   if (init_array && this->index >= 0) {
    01630     assert(w->nested_array_size > (uint)this->index);
    01631     w->nested_array[this->index] = this;
    01632   }
    01633   this->smallest_x = this->min_x;
    01634   this->smallest_y = this->min_y;
    01635 }
    01636
    01637 void NWidgetViewport::Draw(const Window *w)
    01638 {
    01639   if (this->disp_flags & ND_NO_TRANSPARENCY) {
    01640     TransparencyOptionBits to_backup = _transparency_opt;
    01641     _transparency_opt = 0; // Disable all transparency
    01642     w->DrawViewport();
    01643     _transparency_opt = to_backup;
    01644   } else {
    01645     w->DrawViewport();
    01646   }
    01647
    01648   /* Optionally shade the viewport. */
    01649   if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
    01650     GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
    01651         (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_TO_STRUCT_GREY, FILLRECT_RECOLOUR);
    01652   }
    01653 }
    01654
    01655 Scrollbar *NWidgetViewport::FindScrollbar(Window *w, bool allow_next) const
    01656 {
    01657   return NULL;
    01658 }
    01659
    01666 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
    01667 {
    01668   InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
    01669 }
    01670
    01675 void NWidgetViewport::UpdateViewportCoordinates(Window *w)
    01676 {
    01677   ViewPort *vp = w->viewport;
    01678   if (vp != NULL) {
    01679     vp->left = w->left + this->pos_x;
    01680     vp->top  = w->top + this->pos_y;
    01681     vp->width  = this->current_x;
    01682     vp->height = this->current_y;
    01683
    01684     vp->virtual_width  = ScaleByZoom(vp->width, vp->zoom);
    01685     vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
    01686   }
    01687 }
    01688
    01690 /* static */ void NWidgetLeaf::InvalidateDimensionCache()
    01691 {
    01692   shadebox_dimension.width  = shadebox_dimension.height  = 0;
    01693   stickybox_dimension.width = stickybox_dimension.height = 0;
    01694   resizebox_dimension.width = resizebox_dimension.height = 0;
    01695   closebox_dimension.width  = closebox_dimension.height  = 0;
    01696 }
    01697
    01698 Dimension NWidgetLeaf::shadebox_dimension  = {0, 0};
    01699 Dimension NWidgetLeaf::stickybox_dimension = {0, 0};
    01700 Dimension NWidgetLeaf::resizebox_dimension = {0, 0};
    01701 Dimension NWidgetLeaf::closebox_dimension  = {0, 0};
    01702
    01711 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint16 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
    01712 {
    01713   assert(index >= 0 || tp == WWT_LABEL || tp == WWT_TEXT || tp == WWT_CAPTION || tp == WWT_RESIZEBOX || tp == WWT_SHADEBOX || tp == WWT_STICKYBOX || tp == WWT_CLOSEBOX);
    01714   if (index >= 0) this->SetIndex(index);
    01715   this->SetMinimalSize(0, 0);
    01716   this->SetResize(0, 0);
    01717
    01718   switch (tp) {
    01719     case WWT_EMPTY:
    01720       break;
    01721
    01722     case WWT_PUSHBTN:
    01723       this->SetFill(0, 0);
    01724       break;
    01725
    01726     case WWT_IMGBTN:
    01727     case WWT_PUSHIMGBTN:
    01728     case WWT_IMGBTN_2:
    01729       this->SetFill(0, 0);
    01730       break;
    01731
    01732     case WWT_TEXTBTN:
    01733     case WWT_PUSHTXTBTN:
    01734     case WWT_TEXTBTN_2:
    01735     case WWT_LABEL:
    01736     case WWT_TEXT:
    01737     case WWT_MATRIX:
    01738     case WWT_EDITBOX:
    01739     case NWID_BUTTON_DROPDOWN:
    01740     case NWID_BUTTON_ARROW:
    01741       this->SetFill(0, 0);
    01742       break;
    01743
    01744     case WWT_SCROLLBAR:
    01745     case WWT_SCROLL2BAR:
    01746       this->SetFill(0, 1);
    01747       this->SetResize(0, 1);
    01748       this->min_x = WD_VSCROLLBAR_WIDTH;
    01749       this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
    01750       break;
    01751
    01752     case WWT_CAPTION:
    01753       this->SetFill(1, 0);
    01754       this->SetResize(1, 0);
    01755       this->min_y = WD_CAPTION_HEIGHT;
    01756       this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
    01757       break;
    01758
    01759     case WWT_HSCROLLBAR:
    01760       this->SetFill(1, 0);
    01761       this->SetResize(1, 0);
    01762       this->min_y = WD_HSCROLLBAR_HEIGHT;
    01763       this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
    01764       break;
    01765
    01766     case WWT_STICKYBOX:
    01767       this->SetFill(0, 0);
    01768       this->SetMinimalSize(WD_STICKYBOX_WIDTH, 14);
    01769       this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
    01770       break;
    01771
    01772     case WWT_SHADEBOX:
    01773       this->SetFill(0, 0);
    01774       this->SetMinimalSize(WD_SHADEBOX_TOP, 14);
    01775       this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
    01776       break;
    01777
    01778     case WWT_RESIZEBOX:
    01779       this->SetFill(0, 0);
    01780       this->SetMinimalSize(WD_RESIZEBOX_WIDTH, 12);
    01781       this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
    01782       break;
    01783
    01784     case WWT_CLOSEBOX:
    01785       this->SetFill(0, 0);
    01786       this->SetMinimalSize(WD_CLOSEBOX_WIDTH, 14);
    01787       this->SetDataTip(STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW);
    01788       break;
    01789
    01790     case WWT_DROPDOWN:
    01791       this->SetFill(0, 0);
    01792       this->min_y = WD_DROPDOWN_HEIGHT;
    01793       break;
    01794
    01795     default:
    01796       NOT_REACHED();
    01797   }
    01798 }
    01799
    01800 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
    01801 {
    01802   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    01803     assert(w->nested_array_size > (uint)this->index);
    01804     w->nested_array[this->index] = this;
    01805   }
    01806
    01807   Dimension size = {this->min_x, this->min_y};
    01808   Dimension fill = {this->fill_x, this->fill_y};
    01809   Dimension resize = {this->resize_x, this->resize_y};
    01810   /* Get padding, and update size with the real content size if appropriate. */
    01811   const Dimension *padding = NULL;
    01812   switch (this->type) {
    01813     case WWT_EMPTY:
    01814     case WWT_SCROLLBAR:
    01815     case WWT_SCROLL2BAR:
    01816     case WWT_HSCROLLBAR: {
    01817       static const Dimension extra = {0, 0};
    01818       padding = &extra;
    01819       break;
    01820     }
    01821     case WWT_MATRIX: {
    01822       static const Dimension extra = {WD_MATRIX_LEFT + WD_MATRIX_RIGHT, WD_MATRIX_TOP + WD_MATRIX_BOTTOM};
    01823       padding = &extra;
    01824       break;
    01825     }
    01826     case WWT_SHADEBOX: {
    01827       static const Dimension extra = {WD_SHADEBOX_LEFT + WD_SHADEBOX_RIGHT, WD_SHADEBOX_TOP + WD_SHADEBOX_BOTTOM};
    01828       padding = &extra;
    01829       if (NWidgetLeaf::shadebox_dimension.width == 0) {
    01830         NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
    01831         NWidgetLeaf::shadebox_dimension.width += extra.width;
    01832         NWidgetLeaf::shadebox_dimension.height += extra.height;
    01833       }
    01834       size = maxdim(size, NWidgetLeaf::shadebox_dimension);
    01835       break;
    01836     }
    01837     case WWT_STICKYBOX: {
    01838       static const Dimension extra = {WD_STICKYBOX_LEFT + WD_STICKYBOX_RIGHT, WD_STICKYBOX_TOP + WD_STICKYBOX_BOTTOM};
    01839       padding = &extra;
    01840       if (NWidgetLeaf::stickybox_dimension.width == 0) {
    01841         NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
    01842         NWidgetLeaf::stickybox_dimension.width += extra.width;
    01843         NWidgetLeaf::stickybox_dimension.height += extra.height;
    01844       }
    01845       size = maxdim(size, NWidgetLeaf::stickybox_dimension);
    01846       break;
    01847     }
    01848     case WWT_RESIZEBOX: {
    01849       static const Dimension extra = {WD_RESIZEBOX_LEFT + WD_RESIZEBOX_RIGHT, WD_RESIZEBOX_TOP + WD_RESIZEBOX_BOTTOM};
    01850       padding = &extra;
    01851       if (NWidgetLeaf::resizebox_dimension.width == 0) {
    01852         NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
    01853         NWidgetLeaf::resizebox_dimension.width += extra.width;
    01854         NWidgetLeaf::resizebox_dimension.height += extra.height;
    01855       }
    01856       size = maxdim(size, NWidgetLeaf::resizebox_dimension);
    01857       break;
    01858     }
    01859     case WWT_EDITBOX:
    01860       size.height = max(size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    01861       /* fall through */
    01862     case WWT_PUSHBTN: {
    01863       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    01864       padding = &extra;
    01865       break;
    01866     }
    01867     case WWT_IMGBTN:
    01868     case WWT_IMGBTN_2:
    01869     case WWT_PUSHIMGBTN: {
    01870       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    01871       padding = &extra;
    01872       Dimension d2 = GetSpriteSize(this->widget_data);
    01873       if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
    01874       d2.width += extra.width;
    01875       d2.height += extra.height;
    01876       size = maxdim(size, d2);
    01877       break;
    01878     }
    01879     case NWID_BUTTON_ARROW: {
    01880       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    01881       padding = &extra;
    01882       Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    01883       d2.width += extra.width;
    01884       d2.height += extra.height;
    01885       size = maxdim(size, d2);
    01886       break;
    01887     }
    01888
    01889     case WWT_CLOSEBOX: {
    01890       static const Dimension extra = {WD_CLOSEBOX_LEFT + WD_CLOSEBOX_RIGHT, WD_CLOSEBOX_TOP + WD_CLOSEBOX_BOTTOM};
    01891       padding = &extra;
    01892       if (NWidgetLeaf::closebox_dimension.width == 0) {
    01893         NWidgetLeaf::closebox_dimension = maxdim(GetStringBoundingBox(STR_BLACK_CROSS), GetStringBoundingBox(STR_SILVER_CROSS));
    01894         NWidgetLeaf::closebox_dimension.width += extra.width;
    01895         NWidgetLeaf::closebox_dimension.height += extra.height;
    01896       }
    01897       size = maxdim(size, NWidgetLeaf::closebox_dimension);
    01898       break;
    01899     }
    01900     case WWT_TEXTBTN:
    01901     case WWT_PUSHTXTBTN:
    01902     case WWT_TEXTBTN_2: {
    01903       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT,  WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    01904       padding = &extra;
    01905       if (this->index >= 0) w->SetStringParameters(this->index);
    01906       Dimension d2 = GetStringBoundingBox(this->widget_data);
    01907       d2.width += extra.width;
    01908       d2.height += extra.height;
    01909       size = maxdim(size, d2);
    01910       break;
    01911     }
    01912     case WWT_LABEL:
    01913     case WWT_TEXT: {
    01914       static const Dimension extra = {0, 0};
    01915       padding = &extra;
    01916       if (this->index >= 0) w->SetStringParameters(this->index);
    01917       size = maxdim(size, GetStringBoundingBox(this->widget_data));
    01918       break;
    01919     }
    01920     case WWT_CAPTION: {
    01921       static const Dimension extra = {WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT, WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM};
    01922       padding = &extra;
    01923       if (this->index >= 0) w->SetStringParameters(this->index);
    01924       Dimension d2 = GetStringBoundingBox(this->widget_data);
    01925       d2.width += extra.width;
    01926       d2.height += extra.height;
    01927       size = maxdim(size, d2);
    01928       break;
    01929     }
    01930     case WWT_DROPDOWN:
    01931     case NWID_BUTTON_DROPDOWN: {
    01932       static const Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
    01933       padding = &extra;
    01934       if (this->index >= 0) w->SetStringParameters(this->index);
    01935       Dimension d2 = GetStringBoundingBox(this->widget_data);
    01936       d2.width += extra.width;
    01937       d2.height += extra.height;
    01938       size = maxdim(size, d2);
    01939       break;
    01940     }
    01941     default:
    01942       NOT_REACHED();
    01943   }
    01944
    01945   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
    01946
    01947   this->smallest_x = size.width;
    01948   this->smallest_y = size.height;
    01949   this->fill_x = fill.width;
    01950   this->fill_y = fill.height;
    01951   this->resize_x = resize.width;
    01952   this->resize_y = resize.height;
    01953 }
    01954
    01955 void NWidgetLeaf::Draw(const Window *w)
    01956 {
    01957   if (this->current_x == 0 || this->current_y == 0) return;
    01958
    01959   Rect r;
    01960   r.left = this->pos_x;
    01961   r.right = this->pos_x + this->current_x - 1;
    01962   r.top = this->pos_y;
    01963   r.bottom = this->pos_y + this->current_y - 1;
    01964
    01965   const DrawPixelInfo *dpi = _cur_dpi;
    01966   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01967
    01968   bool clicked = this->IsLowered();
    01969   switch (this->type) {
    01970     case WWT_EMPTY:
    01971       break;
    01972
    01973     case WWT_PUSHBTN:
    01974       assert(this->widget_data == 0);
    01975       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    01976       break;
    01977
    01978     case WWT_IMGBTN:
    01979     case WWT_PUSHIMGBTN:
    01980     case WWT_IMGBTN_2:
    01981       DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
    01982       break;
    01983
    01984     case WWT_TEXTBTN:
    01985     case WWT_PUSHTXTBTN:
    01986     case WWT_TEXTBTN_2:
    01987       if (this->index >= 0) w->SetStringParameters(this->index);
    01988       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    01989       DrawLabel(r, this->type, clicked, this->widget_data);
    01990       break;
    01991
    01992     case NWID_BUTTON_ARROW: {
    01993       SpriteID sprite;
    01994       switch (this->widget_data) {
    01995         case AWV_DECREASE: sprite = _dynlang.text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    01996         case AWV_INCREASE: sprite = _dynlang.text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    01997         case AWV_LEFT:     sprite = SPR_ARROW_LEFT;  break;
    01998         case AWV_RIGHT:    sprite = SPR_ARROW_RIGHT; break;
    01999         default: NOT_REACHED();
    02000       }
    02001       DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
    02002     }
    02003
    02004     case WWT_LABEL:
    02005       if (this->index >= 0) w->SetStringParameters(this->index);
    02006       DrawLabel(r, this->type, clicked, this->widget_data);
    02007       break;
    02008
    02009     case WWT_TEXT:
    02010       if (this->index >= 0) w->SetStringParameters(this->index);
    02011       DrawText(r, (TextColour)this->colour, this->widget_data);
    02012       break;
    02013
    02014     case WWT_MATRIX:
    02015       DrawMatrix(r, this->colour, clicked, this->widget_data);
    02016       break;
    02017
    02018     case WWT_EDITBOX:
    02019       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, FR_LOWERED | FR_DARKENED);
    02020       break;
    02021
    02022     case WWT_SCROLLBAR:
    02023       assert(this->widget_data == 0);
    02024       DrawVerticalScrollbar(r, this->colour, (w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_UP,
    02025                 (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_MIDDLE,
    02026                 (w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_DOWN, &w->vscroll);
    02027       break;
    02028
    02029     case WWT_SCROLL2BAR:
    02030       assert(this->widget_data == 0);
    02031       DrawVerticalScrollbar(r, this->colour, (w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_UP | WF_SCROLL2),
    02032                 (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_MIDDLE | WF_SCROLL2),
    02033                 (w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_DOWN | WF_SCROLL2), &w->vscroll2);
    02034       break;
    02035
    02036     case WWT_CAPTION:
    02037       if (this->index >= 0) w->SetStringParameters(this->index);
    02038       DrawCaption(r, this->colour, w->owner, this->widget_data);
    02039       break;
    02040
    02041     case WWT_HSCROLLBAR:
    02042       assert(this->widget_data == 0);
    02043       DrawHorizontalScrollbar(r, this->colour, (w->flags4 & (WF_SCROLL_UP | WF_HSCROLL)) == (WF_SCROLL_UP | WF_HSCROLL),
    02044                 (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL)) == (WF_SCROLL_MIDDLE | WF_HSCROLL),
    02045                 (w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL)) == (WF_SCROLL_DOWN | WF_HSCROLL), &w->hscroll);
    02046       break;
    02047
    02048     case WWT_SHADEBOX:
    02049       assert(this->widget_data == 0);
    02050       DrawShadeBox(r, this->colour, w->IsShaded());
    02051       break;
    02052
    02053     case WWT_STICKYBOX:
    02054       assert(this->widget_data == 0);
    02055       DrawStickyBox(r, this->colour, !!(w->flags4 & WF_STICKY));
    02056       break;
    02057
    02058     case WWT_RESIZEBOX:
    02059       assert(this->widget_data == 0);
    02060       DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags4 & WF_SIZING));
    02061       break;
    02062
    02063     case WWT_CLOSEBOX:
    02064       DrawCloseBox(r, this->colour, this->widget_data);
    02065       break;
    02066
    02067     case WWT_DROPDOWN:
    02068       if (this->index >= 0) w->SetStringParameters(this->index);
    02069       DrawDropdown(r, this->colour, clicked, this->widget_data);
    02070       break;
    02071
    02072     case NWID_BUTTON_DROPDOWN:
    02073       if (this->index >= 0) w->SetStringParameters(this->index);
    02074       DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
    02075       break;
    02076
    02077     default:
    02078       NOT_REACHED();
    02079   }
    02080   if (this->index >= 0) w->DrawWidget(r, this->index);
    02081
    02082   if (this->IsDisabled()) {
    02083     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    02084   }
    02085 }
    02086
    02087 Scrollbar *NWidgetLeaf::FindScrollbar(Window *w, bool allow_next) const
    02088 {
    02089   if (this->type == WWT_SCROLLBAR) return &w->vscroll;
    02090   if (this->type == WWT_SCROLL2BAR) return &w->vscroll2;
    02091
    02092   if (this->index >= 0 && allow_next && (uint)(this->index) + 1 < w->nested_array_size) {
    02093     /* GetWidget ensures that the widget is of the given type.
    02094      * As we might have cases where the next widget in the array
    02095      * is a non-Core widget (e.g. NWID_SELECTION) we first get
    02096      * the base class and then dynamic_cast that. */
    02097     const NWidgetCore *next_wid = dynamic_cast<NWidgetCore*>(w->GetWidget<NWidgetBase>(this->index + 1));
    02098     if (next_wid != NULL) return next_wid->FindScrollbar(w, false);
    02099   }
    02100   return NULL;
    02101 }
    02102
    02110 bool NWidgetLeaf::ButtonHit(const Point &pt)
    02111 {
    02112   if (_dynlang.text_dir == TD_LTR) {
    02113     int button_width = this->pos_x + this->current_x - 12;
    02114     return pt.x < button_width;
    02115   } else {
    02116     int button_left = this->pos_x + 12;
    02117     return pt.x >= button_left;
    02118   }
    02119 }
    02120
    02121 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
    02122
    02138 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
    02139 {
    02140   int num_used = 0;
    02141
    02142   *dest = NULL;
    02143   *fill_dest = false;
    02144
    02145   while (count > num_used) {
    02146     switch (parts->type) {
    02147       case NWID_SPACER:
    02148         if (*dest != NULL) return num_used;
    02149         *dest = new NWidgetSpacer(0, 0);
    02150         break;
    02151
    02152       case NWID_HORIZONTAL:
    02153         if (*dest != NULL) return num_used;
    02154         *dest = new NWidgetHorizontal(parts->u.cont_flags);
    02155         *fill_dest = true;
    02156         break;
    02157
    02158       case NWID_HORIZONTAL_LTR:
    02159         if (*dest != NULL) return num_used;
    02160         *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
    02161         *fill_dest = true;
    02162         break;
    02163
    02164       case WWT_PANEL:
    02165       case WWT_INSET:
    02166       case WWT_FRAME:
    02167         if (*dest != NULL) return num_used;
    02168         *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02169         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02170         *fill_dest = true;
    02171         break;
    02172
    02173       case NWID_VERTICAL:
    02174         if (*dest != NULL) return num_used;
    02175         *dest = new NWidgetVertical(parts->u.cont_flags);
    02176         *fill_dest = true;
    02177         break;
    02178
    02179       case WPT_FUNCTION: {
    02180         if (*dest != NULL) return num_used;
    02181         /* Ensure proper functioning even when the called code simply writes its largest index. */
    02182         int biggest = -1;
    02183         *dest = parts->u.func_ptr(&biggest);
    02184         *biggest_index = max(*biggest_index, biggest);
    02185         *fill_dest = false;
    02186         break;
    02187       }
    02188
    02189       case WPT_RESIZE: {
    02190         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02191         if (nwrb != NULL) {
    02192           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02193           nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
    02194         }
    02195         break;
    02196       }
    02197
    02198       case WPT_MINSIZE: {
    02199         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02200         if (nwrb != NULL) {
    02201           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02202           nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
    02203         }
    02204         break;
    02205       }
    02206
    02207       case WPT_MINTEXTLINES: {
    02208         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02209         if (nwrb != NULL) {
    02210           assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
    02211           nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
    02212         }
    02213         break;
    02214       }
    02215
    02216       case WPT_FILL: {
    02217         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02218         if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
    02219         break;
    02220       }
    02221
    02222       case WPT_DATATIP: {
    02223         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02224         if (nwc != NULL) {
    02225           nwc->widget_data = parts->u.data_tip.data;
    02226           nwc->tool_tip = parts->u.data_tip.tooltip;
    02227         }
    02228         break;
    02229       }
    02230
    02231       case WPT_PADDING:
    02232         if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
    02233         break;
    02234
    02235       case WPT_PIPSPACE: {
    02236         NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
    02237         if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02238
    02239         NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
    02240         if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02241         break;
    02242       }
    02243
    02244       case WPT_ENDCONTAINER:
    02245         return num_used;
    02246
    02247       case NWID_VIEWPORT:
    02248         if (*dest != NULL) return num_used;
    02249         *dest = new NWidgetViewport(parts->u.widget.index);
    02250         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02251         break;
    02252
    02253       case NWID_SELECTION: {
    02254         if (*dest != NULL) return num_used;
    02255         NWidgetStacked *nws = new NWidgetStacked();
    02256         *dest = nws;
    02257         *fill_dest = true;
    02258         nws->SetIndex(parts->u.widget.index);
    02259         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02260         break;
    02261       }
    02262
    02263       default:
    02264         if (*dest != NULL) return num_used;
    02265         assert((parts->type & WWT_MASK) < WWT_LAST || parts->type == NWID_BUTTON_DROPDOWN || parts->type == NWID_BUTTON_ARROW);
    02266         *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
    02267         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02268         break;
    02269     }
    02270     num_used++;
    02271     parts++;
    02272   }
    02273
    02274   return num_used;
    02275 }
    02276
    02286 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
    02287 {
    02288   /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
    02289    * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
    02290   NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
    02291   NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
    02292   assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
    02293
    02294   int total_used = 0;
    02295   while (true) {
    02296     NWidgetBase *sub_widget = NULL;
    02297     bool fill_sub = false;
    02298     int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
    02299     parts += num_used;
    02300     total_used += num_used;
    02301
    02302     /* Break out of loop when end reached */
    02303     if (sub_widget == NULL) break;
    02304
    02305     /* If sub-widget is a container, recursively fill that container. */
    02306     WidgetType tp = sub_widget->type;
    02307     if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL
    02308               || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
    02309       NWidgetBase *sub_ptr = sub_widget;
    02310       int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
    02311       parts += num_used;
    02312       total_used += num_used;
    02313     }
    02314
    02315     /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
    02316     if (nwid_cont) nwid_cont->Add(sub_widget);
    02317     if (nwid_parent) nwid_parent->Add(sub_widget);
    02318     if (!nwid_cont && !nwid_parent) {
    02319       *parent = sub_widget;
    02320       return total_used;
    02321     }
    02322   }
    02323
    02324   if (count == total_used) return total_used; // Reached the end of the array of parts?
    02325
    02326   assert(total_used < count);
    02327   assert(parts->type == WPT_ENDCONTAINER);
    02328   return total_used + 1; // *parts is also 'used'
    02329 }
    02330
    02342 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
    02343 {
    02344   *biggest_index = -1;
    02345   if (container == NULL) container = new NWidgetVertical();
    02346   NWidgetBase *cont_ptr = container;
    02347   MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
    02348   return container;
    02349 }
    02350
    02363 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
    02364 {
    02365   *biggest_index = -1;
    02366
    02367   /* Read the first widget recursively from the array. */
    02368   NWidgetBase *nwid = NULL;
    02369   int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
    02370   assert(nwid != NULL);
    02371   parts += num_used;
    02372   count -= num_used;
    02373
    02374   NWidgetContainer *root = new NWidgetVertical;
    02375   root->Add(nwid);
    02376   if (count == 0) { // There is no body at all.
    02377     *shade_select = NULL;
    02378     return root;
    02379   }
    02380
    02381   /* If the first widget looks like a titlebar, treat it as such.
    02382    * If it has a shading box, silently add a shade selection widget in the tree. */
    02383   NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
    02384   NWidgetContainer *body;
    02385   if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
    02386     *shade_select = new NWidgetStacked;
    02387     root->Add(*shade_select);
    02388     body = new NWidgetVertical;
    02389     (*shade_select)->Add(body);
    02390   } else {
    02391     *shade_select = NULL;
    02392     body = root;
    02393   }
    02394
    02395   /* Load the remaining parts into 'body'. */
    02396   int biggest2 = -1;
    02397   MakeNWidgets(parts, count, &biggest2, body);
    02398
    02399   *biggest_index = max(*biggest_index, biggest2);
    02400   return root;
    02401 }