widget.cpp

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

Generated on Sun Jan 9 16:02:05 2011 for OpenTTD by  doxygen 1.6.1