widget.cpp

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