widget.cpp

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

Generated on Mon May 9 05:19:04 2011 for OpenTTD by  doxygen 1.6.1