# widget.cpp

Go to the documentation of this file.

    00001 /* $Id: widget.cpp 20585 2010-08-20 18:28:46Z 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 <= 1) {
    00111       _scroller_click_timeout = 3;
    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 <= 1) {
    00120       _scroller_click_timeout = 3;
    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     case WWT_IMGBTN:
    01724     case WWT_PUSHIMGBTN:
    01725     case WWT_IMGBTN_2:
    01726     case WWT_TEXTBTN:
    01727     case WWT_PUSHTXTBTN:
    01728     case WWT_TEXTBTN_2:
    01729     case WWT_LABEL:
    01730     case WWT_TEXT:
    01731     case WWT_MATRIX:
    01732     case NWID_BUTTON_DROPDOWN:
    01733     case NWID_BUTTON_ARROW:
    01734       this->SetFill(0, 0);
    01735       break;
    01736
    01737     case WWT_EDITBOX:
    01738       this->SetMinimalSize(10, 0);
    01739       this->SetFill(0, 0);
    01740       break;
    01741
    01742     case WWT_SCROLLBAR:
    01743     case WWT_SCROLL2BAR:
    01744       this->SetFill(0, 1);
    01745       this->SetResize(0, 1);
    01746       this->min_x = WD_VSCROLLBAR_WIDTH;
    01747       this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
    01748       break;
    01749
    01750     case WWT_CAPTION:
    01751       this->SetFill(1, 0);
    01752       this->SetResize(1, 0);
    01753       this->min_y = WD_CAPTION_HEIGHT;
    01754       this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
    01755       break;
    01756
    01757     case WWT_HSCROLLBAR:
    01758       this->SetFill(1, 0);
    01759       this->SetResize(1, 0);
    01760       this->min_y = WD_HSCROLLBAR_HEIGHT;
    01761       this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
    01762       break;
    01763
    01764     case WWT_STICKYBOX:
    01765       this->SetFill(0, 0);
    01766       this->SetMinimalSize(WD_STICKYBOX_WIDTH, 14);
    01767       this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
    01768       break;
    01769
    01770     case WWT_SHADEBOX:
    01771       this->SetFill(0, 0);
    01772       this->SetMinimalSize(WD_SHADEBOX_TOP, 14);
    01773       this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
    01774       break;
    01775
    01776     case WWT_RESIZEBOX:
    01777       this->SetFill(0, 0);
    01778       this->SetMinimalSize(WD_RESIZEBOX_WIDTH, 12);
    01779       this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
    01780       break;
    01781
    01782     case WWT_CLOSEBOX:
    01783       this->SetFill(0, 0);
    01784       this->SetMinimalSize(WD_CLOSEBOX_WIDTH, 14);
    01785       this->SetDataTip(STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW);
    01786       break;
    01787
    01788     case WWT_DROPDOWN:
    01789       this->SetFill(0, 0);
    01790       this->min_y = WD_DROPDOWN_HEIGHT;
    01791       break;
    01792
    01793     default:
    01794       NOT_REACHED();
    01795   }
    01796 }
    01797
    01798 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
    01799 {
    01800   if (this->index >= 0 && init_array) { // Fill w->nested_array[]
    01801     assert(w->nested_array_size > (uint)this->index);
    01802     w->nested_array[this->index] = this;
    01803   }
    01804
    01805   Dimension size = {this->min_x, this->min_y};
    01806   Dimension fill = {this->fill_x, this->fill_y};
    01807   Dimension resize = {this->resize_x, this->resize_y};
    01808   /* Get padding, and update size with the real content size if appropriate. */
    01809   const Dimension *padding = NULL;
    01810   switch (this->type) {
    01811     case WWT_EMPTY:
    01812     case WWT_SCROLLBAR:
    01813     case WWT_SCROLL2BAR:
    01814     case WWT_HSCROLLBAR: {
    01815       static const Dimension extra = {0, 0};
    01816       padding = &extra;
    01817       break;
    01818     }
    01819     case WWT_MATRIX: {
    01820       static const Dimension extra = {WD_MATRIX_LEFT + WD_MATRIX_RIGHT, WD_MATRIX_TOP + WD_MATRIX_BOTTOM};
    01821       padding = &extra;
    01822       break;
    01823     }
    01824     case WWT_SHADEBOX: {
    01825       static const Dimension extra = {WD_SHADEBOX_LEFT + WD_SHADEBOX_RIGHT, WD_SHADEBOX_TOP + WD_SHADEBOX_BOTTOM};
    01826       padding = &extra;
    01827       if (NWidgetLeaf::shadebox_dimension.width == 0) {
    01828         NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
    01829         NWidgetLeaf::shadebox_dimension.width += extra.width;
    01830         NWidgetLeaf::shadebox_dimension.height += extra.height;
    01831       }
    01832       size = maxdim(size, NWidgetLeaf::shadebox_dimension);
    01833       break;
    01834     }
    01835     case WWT_STICKYBOX: {
    01836       static const Dimension extra = {WD_STICKYBOX_LEFT + WD_STICKYBOX_RIGHT, WD_STICKYBOX_TOP + WD_STICKYBOX_BOTTOM};
    01837       padding = &extra;
    01838       if (NWidgetLeaf::stickybox_dimension.width == 0) {
    01839         NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
    01840         NWidgetLeaf::stickybox_dimension.width += extra.width;
    01841         NWidgetLeaf::stickybox_dimension.height += extra.height;
    01842       }
    01843       size = maxdim(size, NWidgetLeaf::stickybox_dimension);
    01844       break;
    01845     }
    01846     case WWT_RESIZEBOX: {
    01847       static const Dimension extra = {WD_RESIZEBOX_LEFT + WD_RESIZEBOX_RIGHT, WD_RESIZEBOX_TOP + WD_RESIZEBOX_BOTTOM};
    01848       padding = &extra;
    01849       if (NWidgetLeaf::resizebox_dimension.width == 0) {
    01850         NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
    01851         NWidgetLeaf::resizebox_dimension.width += extra.width;
    01852         NWidgetLeaf::resizebox_dimension.height += extra.height;
    01853       }
    01854       size = maxdim(size, NWidgetLeaf::resizebox_dimension);
    01855       break;
    01856     }
    01857     case WWT_EDITBOX:
    01858       size.height = max(size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    01859       /* fall through */
    01860     case WWT_PUSHBTN: {
    01861       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    01862       padding = &extra;
    01863       break;
    01864     }
    01865     case WWT_IMGBTN:
    01866     case WWT_IMGBTN_2:
    01867     case WWT_PUSHIMGBTN: {
    01868       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    01869       padding = &extra;
    01870       Dimension d2 = GetSpriteSize(this->widget_data);
    01871       if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
    01872       d2.width += extra.width;
    01873       d2.height += extra.height;
    01874       size = maxdim(size, d2);
    01875       break;
    01876     }
    01877     case NWID_BUTTON_ARROW: {
    01878       static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT,  WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
    01879       padding = &extra;
    01880       Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
    01881       d2.width += extra.width;
    01882       d2.height += extra.height;
    01883       size = maxdim(size, d2);
    01884       break;
    01885     }
    01886
    01887     case WWT_CLOSEBOX: {
    01888       static const Dimension extra = {WD_CLOSEBOX_LEFT + WD_CLOSEBOX_RIGHT, WD_CLOSEBOX_TOP + WD_CLOSEBOX_BOTTOM};
    01889       padding = &extra;
    01890       if (NWidgetLeaf::closebox_dimension.width == 0) {
    01891         NWidgetLeaf::closebox_dimension = maxdim(GetStringBoundingBox(STR_BLACK_CROSS), GetStringBoundingBox(STR_SILVER_CROSS));
    01892         NWidgetLeaf::closebox_dimension.width += extra.width;
    01893         NWidgetLeaf::closebox_dimension.height += extra.height;
    01894       }
    01895       size = maxdim(size, NWidgetLeaf::closebox_dimension);
    01896       break;
    01897     }
    01898     case WWT_TEXTBTN:
    01899     case WWT_PUSHTXTBTN:
    01900     case WWT_TEXTBTN_2: {
    01901       static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT,  WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
    01902       padding = &extra;
    01903       if (this->index >= 0) w->SetStringParameters(this->index);
    01904       Dimension d2 = GetStringBoundingBox(this->widget_data);
    01905       d2.width += extra.width;
    01906       d2.height += extra.height;
    01907       size = maxdim(size, d2);
    01908       break;
    01909     }
    01910     case WWT_LABEL:
    01911     case WWT_TEXT: {
    01912       static const Dimension extra = {0, 0};
    01913       padding = &extra;
    01914       if (this->index >= 0) w->SetStringParameters(this->index);
    01915       size = maxdim(size, GetStringBoundingBox(this->widget_data));
    01916       break;
    01917     }
    01918     case WWT_CAPTION: {
    01919       static const Dimension extra = {WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT, WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM};
    01920       padding = &extra;
    01921       if (this->index >= 0) w->SetStringParameters(this->index);
    01922       Dimension d2 = GetStringBoundingBox(this->widget_data);
    01923       d2.width += extra.width;
    01924       d2.height += extra.height;
    01925       size = maxdim(size, d2);
    01926       break;
    01927     }
    01928     case WWT_DROPDOWN:
    01929     case NWID_BUTTON_DROPDOWN: {
    01930       static const Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
    01931       padding = &extra;
    01932       if (this->index >= 0) w->SetStringParameters(this->index);
    01933       Dimension d2 = GetStringBoundingBox(this->widget_data);
    01934       d2.width += extra.width;
    01935       d2.height += extra.height;
    01936       size = maxdim(size, d2);
    01937       break;
    01938     }
    01939     default:
    01940       NOT_REACHED();
    01941   }
    01942
    01943   if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
    01944
    01945   this->smallest_x = size.width;
    01946   this->smallest_y = size.height;
    01947   this->fill_x = fill.width;
    01948   this->fill_y = fill.height;
    01949   this->resize_x = resize.width;
    01950   this->resize_y = resize.height;
    01951 }
    01952
    01953 void NWidgetLeaf::Draw(const Window *w)
    01954 {
    01955   if (this->current_x == 0 || this->current_y == 0) return;
    01956
    01957   Rect r;
    01958   r.left = this->pos_x;
    01959   r.right = this->pos_x + this->current_x - 1;
    01960   r.top = this->pos_y;
    01961   r.bottom = this->pos_y + this->current_y - 1;
    01962
    01963   const DrawPixelInfo *dpi = _cur_dpi;
    01964   if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
    01965
    01966   bool clicked = this->IsLowered();
    01967   switch (this->type) {
    01968     case WWT_EMPTY:
    01969       break;
    01970
    01971     case WWT_PUSHBTN:
    01972       assert(this->widget_data == 0);
    01973       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    01974       break;
    01975
    01976     case WWT_IMGBTN:
    01977     case WWT_PUSHIMGBTN:
    01978     case WWT_IMGBTN_2:
    01979       DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
    01980       break;
    01981
    01982     case WWT_TEXTBTN:
    01983     case WWT_PUSHTXTBTN:
    01984     case WWT_TEXTBTN_2:
    01985       if (this->index >= 0) w->SetStringParameters(this->index);
    01986       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
    01987       DrawLabel(r, this->type, clicked, this->widget_data);
    01988       break;
    01989
    01990     case NWID_BUTTON_ARROW: {
    01991       SpriteID sprite;
    01992       switch (this->widget_data) {
    01993         case AWV_DECREASE: sprite = _dynlang.text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    01994         case AWV_INCREASE: sprite = _dynlang.text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
    01995         case AWV_LEFT:     sprite = SPR_ARROW_LEFT;  break;
    01996         case AWV_RIGHT:    sprite = SPR_ARROW_RIGHT; break;
    01997         default: NOT_REACHED();
    01998       }
    01999       DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
    02000     }
    02001
    02002     case WWT_LABEL:
    02003       if (this->index >= 0) w->SetStringParameters(this->index);
    02004       DrawLabel(r, this->type, clicked, this->widget_data);
    02005       break;
    02006
    02007     case WWT_TEXT:
    02008       if (this->index >= 0) w->SetStringParameters(this->index);
    02009       DrawText(r, (TextColour)this->colour, this->widget_data);
    02010       break;
    02011
    02012     case WWT_MATRIX:
    02013       DrawMatrix(r, this->colour, clicked, this->widget_data);
    02014       break;
    02015
    02016     case WWT_EDITBOX:
    02017       DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, FR_LOWERED | FR_DARKENED);
    02018       break;
    02019
    02020     case WWT_SCROLLBAR:
    02021       assert(this->widget_data == 0);
    02022       DrawVerticalScrollbar(r, this->colour, (w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_UP,
    02023                 (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_MIDDLE,
    02024                 (w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_DOWN, &w->vscroll);
    02025       break;
    02026
    02027     case WWT_SCROLL2BAR:
    02028       assert(this->widget_data == 0);
    02029       DrawVerticalScrollbar(r, this->colour, (w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_UP | WF_SCROLL2),
    02030                 (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_MIDDLE | WF_SCROLL2),
    02031                 (w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_DOWN | WF_SCROLL2), &w->vscroll2);
    02032       break;
    02033
    02034     case WWT_CAPTION:
    02035       if (this->index >= 0) w->SetStringParameters(this->index);
    02036       DrawCaption(r, this->colour, w->owner, this->widget_data);
    02037       break;
    02038
    02039     case WWT_HSCROLLBAR:
    02040       assert(this->widget_data == 0);
    02041       DrawHorizontalScrollbar(r, this->colour, (w->flags4 & (WF_SCROLL_UP | WF_HSCROLL)) == (WF_SCROLL_UP | WF_HSCROLL),
    02042                 (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL)) == (WF_SCROLL_MIDDLE | WF_HSCROLL),
    02043                 (w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL)) == (WF_SCROLL_DOWN | WF_HSCROLL), &w->hscroll);
    02044       break;
    02045
    02046     case WWT_SHADEBOX:
    02047       assert(this->widget_data == 0);
    02048       DrawShadeBox(r, this->colour, w->IsShaded());
    02049       break;
    02050
    02051     case WWT_STICKYBOX:
    02052       assert(this->widget_data == 0);
    02053       DrawStickyBox(r, this->colour, !!(w->flags4 & WF_STICKY));
    02054       break;
    02055
    02056     case WWT_RESIZEBOX:
    02057       assert(this->widget_data == 0);
    02058       DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags4 & WF_SIZING));
    02059       break;
    02060
    02061     case WWT_CLOSEBOX:
    02062       DrawCloseBox(r, this->colour, this->widget_data);
    02063       break;
    02064
    02065     case WWT_DROPDOWN:
    02066       if (this->index >= 0) w->SetStringParameters(this->index);
    02067       DrawDropdown(r, this->colour, clicked, this->widget_data);
    02068       break;
    02069
    02070     case NWID_BUTTON_DROPDOWN:
    02071       if (this->index >= 0) w->SetStringParameters(this->index);
    02072       DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
    02073       break;
    02074
    02075     default:
    02076       NOT_REACHED();
    02077   }
    02078   if (this->index >= 0) w->DrawWidget(r, this->index);
    02079
    02080   if (this->IsDisabled()) {
    02081     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
    02082   }
    02083 }
    02084
    02085 Scrollbar *NWidgetLeaf::FindScrollbar(Window *w, bool allow_next) const
    02086 {
    02087   if (this->type == WWT_SCROLLBAR) return &w->vscroll;
    02088   if (this->type == WWT_SCROLL2BAR) return &w->vscroll2;
    02089
    02090   if (this->index >= 0 && allow_next && (uint)(this->index) + 1 < w->nested_array_size) {
    02091     /* GetWidget ensures that the widget is of the given type.
    02092      * As we might have cases where the next widget in the array
    02093      * is a non-Core widget (e.g. NWID_SELECTION) we first get
    02094      * the base class and then dynamic_cast that. */
    02095     const NWidgetCore *next_wid = dynamic_cast<NWidgetCore*>(w->GetWidget<NWidgetBase>(this->index + 1));
    02096     if (next_wid != NULL) return next_wid->FindScrollbar(w, false);
    02097   }
    02098   return NULL;
    02099 }
    02100
    02108 bool NWidgetLeaf::ButtonHit(const Point &pt)
    02109 {
    02110   if (_dynlang.text_dir == TD_LTR) {
    02111     int button_width = this->pos_x + this->current_x - 12;
    02112     return pt.x < button_width;
    02113   } else {
    02114     int button_left = this->pos_x + 12;
    02115     return pt.x >= button_left;
    02116   }
    02117 }
    02118
    02119 /* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
    02120
    02136 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
    02137 {
    02138   int num_used = 0;
    02139
    02140   *dest = NULL;
    02141   *fill_dest = false;
    02142
    02143   while (count > num_used) {
    02144     switch (parts->type) {
    02145       case NWID_SPACER:
    02146         if (*dest != NULL) return num_used;
    02147         *dest = new NWidgetSpacer(0, 0);
    02148         break;
    02149
    02150       case NWID_HORIZONTAL:
    02151         if (*dest != NULL) return num_used;
    02152         *dest = new NWidgetHorizontal(parts->u.cont_flags);
    02153         *fill_dest = true;
    02154         break;
    02155
    02156       case NWID_HORIZONTAL_LTR:
    02157         if (*dest != NULL) return num_used;
    02158         *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
    02159         *fill_dest = true;
    02160         break;
    02161
    02162       case WWT_PANEL:
    02163       case WWT_INSET:
    02164       case WWT_FRAME:
    02165         if (*dest != NULL) return num_used;
    02166         *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
    02167         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02168         *fill_dest = true;
    02169         break;
    02170
    02171       case NWID_VERTICAL:
    02172         if (*dest != NULL) return num_used;
    02173         *dest = new NWidgetVertical(parts->u.cont_flags);
    02174         *fill_dest = true;
    02175         break;
    02176
    02177       case WPT_FUNCTION: {
    02178         if (*dest != NULL) return num_used;
    02179         /* Ensure proper functioning even when the called code simply writes its largest index. */
    02180         int biggest = -1;
    02181         *dest = parts->u.func_ptr(&biggest);
    02182         *biggest_index = max(*biggest_index, biggest);
    02183         *fill_dest = false;
    02184         break;
    02185       }
    02186
    02187       case WPT_RESIZE: {
    02188         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02189         if (nwrb != NULL) {
    02190           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02191           nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
    02192         }
    02193         break;
    02194       }
    02195
    02196       case WPT_MINSIZE: {
    02197         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02198         if (nwrb != NULL) {
    02199           assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
    02200           nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
    02201         }
    02202         break;
    02203       }
    02204
    02205       case WPT_MINTEXTLINES: {
    02206         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02207         if (nwrb != NULL) {
    02208           assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
    02209           nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
    02210         }
    02211         break;
    02212       }
    02213
    02214       case WPT_FILL: {
    02215         NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
    02216         if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
    02217         break;
    02218       }
    02219
    02220       case WPT_DATATIP: {
    02221         NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
    02222         if (nwc != NULL) {
    02223           nwc->widget_data = parts->u.data_tip.data;
    02224           nwc->tool_tip = parts->u.data_tip.tooltip;
    02225         }
    02226         break;
    02227       }
    02228
    02229       case WPT_PADDING:
    02230         if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
    02231         break;
    02232
    02233       case WPT_PIPSPACE: {
    02234         NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
    02235         if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02236
    02237         NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
    02238         if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
    02239         break;
    02240       }
    02241
    02242       case WPT_ENDCONTAINER:
    02243         return num_used;
    02244
    02245       case NWID_VIEWPORT:
    02246         if (*dest != NULL) return num_used;
    02247         *dest = new NWidgetViewport(parts->u.widget.index);
    02248         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02249         break;
    02250
    02251       case NWID_SELECTION: {
    02252         if (*dest != NULL) return num_used;
    02253         NWidgetStacked *nws = new NWidgetStacked();
    02254         *dest = nws;
    02255         *fill_dest = true;
    02256         nws->SetIndex(parts->u.widget.index);
    02257         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02258         break;
    02259       }
    02260
    02261       default:
    02262         if (*dest != NULL) return num_used;
    02263         assert((parts->type & WWT_MASK) < WWT_LAST || parts->type == NWID_BUTTON_DROPDOWN || parts->type == NWID_BUTTON_ARROW);
    02264         *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
    02265         *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
    02266         break;
    02267     }
    02268     num_used++;
    02269     parts++;
    02270   }
    02271
    02272   return num_used;
    02273 }
    02274
    02284 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
    02285 {
    02286   /* If *parent == NULL, only the first widget is read and returned. Otherwise, *parent must point to either
    02287    * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
    02288   NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
    02289   NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
    02290   assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
    02291
    02292   int total_used = 0;
    02293   while (true) {
    02294     NWidgetBase *sub_widget = NULL;
    02295     bool fill_sub = false;
    02296     int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
    02297     parts += num_used;
    02298     total_used += num_used;
    02299
    02300     /* Break out of loop when end reached */
    02301     if (sub_widget == NULL) break;
    02302
    02303     /* If sub-widget is a container, recursively fill that container. */
    02304     WidgetType tp = sub_widget->type;
    02305     if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL
    02306               || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
    02307       NWidgetBase *sub_ptr = sub_widget;
    02308       int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
    02309       parts += num_used;
    02310       total_used += num_used;
    02311     }
    02312
    02313     /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
    02314     if (nwid_cont) nwid_cont->Add(sub_widget);
    02315     if (nwid_parent) nwid_parent->Add(sub_widget);
    02316     if (!nwid_cont && !nwid_parent) {
    02317       *parent = sub_widget;
    02318       return total_used;
    02319     }
    02320   }
    02321
    02322   if (count == total_used) return total_used; // Reached the end of the array of parts?
    02323
    02324   assert(total_used < count);
    02325   assert(parts->type == WPT_ENDCONTAINER);
    02326   return total_used + 1; // *parts is also 'used'
    02327 }
    02328
    02340 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
    02341 {
    02342   *biggest_index = -1;
    02343   if (container == NULL) container = new NWidgetVertical();
    02344   NWidgetBase *cont_ptr = container;
    02345   MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
    02346   return container;
    02347 }
    02348
    02361 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
    02362 {
    02363   *biggest_index = -1;
    02364
    02365   /* Read the first widget recursively from the array. */
    02366   NWidgetBase *nwid = NULL;
    02367   int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
    02368   assert(nwid != NULL);
    02369   parts += num_used;
    02370   count -= num_used;
    02371
    02372   NWidgetContainer *root = new NWidgetVertical;
    02373   root->Add(nwid);
    02374   if (count == 0) { // There is no body at all.
    02375     *shade_select = NULL;
    02376     return root;
    02377   }
    02378
    02379   /* If the first widget looks like a titlebar, treat it as such.
    02380    * If it has a shading box, silently add a shade selection widget in the tree. */
    02381   NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
    02382   NWidgetContainer *body;
    02383   if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
    02384     *shade_select = new NWidgetStacked;
    02385     root->Add(*shade_select);
    02386     body = new NWidgetVertical;
    02387     (*shade_select)->Add(body);
    02388   } else {
    02389     *shade_select = NULL;
    02390     body = root;
    02391   }
    02392
    02393   /* Load the remaining parts into 'body'. */
    02394   int biggest2 = -1;
    02395   MakeNWidgets(parts, count, &biggest2, body);
    02396
    02397   *biggest_index = max(*biggest_index, biggest2);
    02398   return root;
    02399 }