00001
00002
00003
00004
00005
00006
00007
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 #include "table/palettes.h"
00025
00026 static const char *UPARROW = "\xEE\x8A\xA0";
00027 static const char *DOWNARROW = "\xEE\x8A\xAA";
00028
00038 static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bool horizontal)
00039 {
00040
00041 int rev_base = top + bottom;
00042 int button_size;
00043 if (horizontal) {
00044 button_size = NWidgetScrollbar::GetHorizontalDimension().width;
00045 } else {
00046 button_size = NWidgetScrollbar::GetVerticalDimension().height;
00047 }
00048 top += button_size;
00049 bottom -= button_size;
00050
00051 int height = (bottom - top);
00052 int pos = sb->GetPosition();
00053 int count = sb->GetCount();
00054 int cap = sb->GetCapacity();
00055
00056 if (count != 0) top += height * pos / count;
00057
00058 if (cap > count) cap = count;
00059 if (count != 0) bottom -= (count - pos - cap) * height / count;
00060
00061 Point pt;
00062 if (horizontal && _current_text_dir == TD_RTL) {
00063 pt.x = rev_base - bottom;
00064 pt.y = rev_base - top;
00065 } else {
00066 pt.x = top;
00067 pt.y = bottom;
00068 }
00069 return pt;
00070 }
00071
00081 static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma)
00082 {
00083 int pos;
00084 int button_size;
00085 bool rtl = false;
00086
00087 if (sb->type == NWID_HSCROLLBAR) {
00088 pos = x;
00089 rtl = _current_text_dir == TD_RTL;
00090 button_size = NWidgetScrollbar::GetHorizontalDimension().width;
00091 } else {
00092 pos = y;
00093 button_size = NWidgetScrollbar::GetVerticalDimension().height;
00094 }
00095 if (pos < mi + button_size) {
00096
00097 SetBit(sb->disp_flags, NDB_SCROLLBAR_UP);
00098 if (_scroller_click_timeout <= 1) {
00099 _scroller_click_timeout = 3;
00100 sb->UpdatePosition(rtl ? 1 : -1);
00101 }
00102 w->scrolling_scrollbar = sb->index;
00103 } else if (pos >= ma - button_size) {
00104
00105 SetBit(sb->disp_flags, NDB_SCROLLBAR_DOWN);
00106
00107 if (_scroller_click_timeout <= 1) {
00108 _scroller_click_timeout = 3;
00109 sb->UpdatePosition(rtl ? -1 : 1);
00110 }
00111 w->scrolling_scrollbar = sb->index;
00112 } else {
00113 Point pt = HandleScrollbarHittest(sb, mi, ma, sb->type == NWID_HSCROLLBAR);
00114
00115 if (pos < pt.x) {
00116 sb->UpdatePosition(rtl ? 1 : -1, Scrollbar::SS_BIG);
00117 } else if (pos > pt.y) {
00118 sb->UpdatePosition(rtl ? -1 : 1, Scrollbar::SS_BIG);
00119 } else {
00120 _scrollbar_start_pos = pt.x - mi - button_size;
00121 _scrollbar_size = ma - mi - button_size * 2;
00122 w->scrolling_scrollbar = sb->index;
00123 _cursorpos_drag_start = _cursor.pos;
00124 }
00125 }
00126
00127 w->SetDirty();
00128 }
00129
00138 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
00139 {
00140 int mi, ma;
00141
00142 if (nw->type == NWID_HSCROLLBAR) {
00143 mi = nw->pos_x;
00144 ma = nw->pos_x + nw->current_x;
00145 } else {
00146 mi = nw->pos_y;
00147 ma = nw->pos_y + nw->current_y;
00148 }
00149 ScrollbarClickPositioning(w, dynamic_cast<NWidgetScrollbar*>(nw), x, y, mi, ma);
00150 }
00151
00160 int GetWidgetFromPos(const Window *w, int x, int y)
00161 {
00162 NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
00163 return (nw != NULL) ? nw->index : -1;
00164 }
00165
00175 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
00176 {
00177 uint dark = _colour_gradient[colour][3];
00178 uint medium_dark = _colour_gradient[colour][5];
00179 uint medium_light = _colour_gradient[colour][6];
00180 uint light = _colour_gradient[colour][7];
00181
00182 if (flags & FR_TRANSPARENT) {
00183 GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR);
00184 } else {
00185 uint interior;
00186
00187 if (flags & FR_LOWERED) {
00188 GfxFillRect(left, top, left, bottom, dark);
00189 GfxFillRect(left + WD_BEVEL_LEFT, top, right, top, dark);
00190 GfxFillRect(right, top + WD_BEVEL_TOP, right, bottom - WD_BEVEL_BOTTOM, light);
00191 GfxFillRect(left + WD_BEVEL_LEFT, bottom, right, bottom, light);
00192 interior = (flags & FR_DARKENED ? medium_dark : medium_light);
00193 } else {
00194 GfxFillRect(left, top, left, bottom - WD_BEVEL_BOTTOM, light);
00195 GfxFillRect(left + WD_BEVEL_LEFT, top, right - WD_BEVEL_RIGHT, top, light);
00196 GfxFillRect(right, top, right, bottom - WD_BEVEL_BOTTOM, dark);
00197 GfxFillRect(left, bottom, right, bottom, dark);
00198 interior = medium_dark;
00199 }
00200 if (!(flags & FR_BORDERONLY)) {
00201 GfxFillRect(left + WD_BEVEL_LEFT, top + WD_BEVEL_TOP, right - WD_BEVEL_RIGHT, bottom - WD_BEVEL_BOTTOM, interior);
00202 }
00203 }
00204 }
00205
00214 static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
00215 {
00216 assert(img != 0);
00217 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
00218
00219 if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++;
00220 DrawSprite(img, PAL_NONE, r.left + WD_IMGBTN_LEFT + clicked, r.top + WD_IMGBTN_TOP + clicked);
00221 }
00222
00230 static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
00231 {
00232 if (str == STR_NULL) return;
00233 if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
00234 Dimension d = GetStringBoundingBox(str);
00235 int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2);
00236 DrawString(r.left + clicked, r.right + clicked, r.top + offset + clicked, str, TC_FROMSTRING, SA_HOR_CENTER);
00237 }
00238
00245 static inline void DrawText(const Rect &r, TextColour colour, StringID str)
00246 {
00247 Dimension d = GetStringBoundingBox(str);
00248 int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2);
00249 if (str != STR_NULL) DrawString(r.left, r.right, r.top + offset, str, colour);
00250 }
00251
00258 static inline void DrawInset(const Rect &r, Colours colour, StringID str)
00259 {
00260 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
00261 if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str);
00262 }
00263
00271 static inline void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint16 data)
00272 {
00273 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
00274
00275 int num_columns = GB(data, MAT_COL_START, MAT_COL_BITS);
00276 int column_width = (r.right - r.left + 1) / num_columns;
00277
00278 int num_rows = GB(data, MAT_ROW_START, MAT_ROW_BITS);
00279 int row_height = (r.bottom - r.top + 1) / num_rows;
00280
00281 int col = _colour_gradient[colour & 0xF][6];
00282
00283 int x = r.left;
00284 for (int ctr = num_columns; ctr > 1; ctr--) {
00285 x += column_width;
00286 GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
00287 }
00288
00289 x = r.top;
00290 for (int ctr = num_rows; ctr > 1; ctr--) {
00291 x += row_height;
00292 GfxFillRect(r.left + 1, x, r.right - 1, x, col);
00293 }
00294
00295 col = _colour_gradient[colour & 0xF][4];
00296
00297 x = r.left - 1;
00298 for (int ctr = num_columns; ctr > 1; ctr--) {
00299 x += column_width;
00300 GfxFillRect(x, r.top + 1, x, r.bottom - 1, col);
00301 }
00302
00303 x = r.top - 1;
00304 for (int ctr = num_rows; ctr > 1; ctr--) {
00305 x += row_height;
00306 GfxFillRect(r.left + 1, x, r.right - 1, x, col);
00307 }
00308 }
00309
00319 static inline void DrawVerticalScrollbar(const Rect &r, Colours colour, bool up_clicked, bool bar_dragged, bool down_clicked, const Scrollbar *scrollbar)
00320 {
00321 int centre = (r.right - r.left) / 2;
00322 int height = NWidgetScrollbar::GetVerticalDimension().height;
00323
00324
00325 DrawFrameRect(r.left, r.top, r.right, r.top + height - 1, colour, (up_clicked) ? FR_LOWERED : FR_NONE);
00326 DrawString(r.left + up_clicked, r.right + up_clicked, r.top + up_clicked, UPARROW, TC_BLACK, SA_HOR_CENTER);
00327
00328 DrawFrameRect(r.left, r.bottom - (height - 1), r.right, r.bottom, colour, (down_clicked) ? FR_LOWERED : FR_NONE);
00329 DrawString(r.left + down_clicked, r.right + down_clicked, r.bottom - (height - 1) + down_clicked, DOWNARROW, TC_BLACK, SA_HOR_CENTER);
00330
00331 int c1 = _colour_gradient[colour & 0xF][3];
00332 int c2 = _colour_gradient[colour & 0xF][7];
00333
00334
00335 GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c2);
00336 GfxFillRect(r.left, r.top + height, r.right, r.bottom - height, c1, FILLRECT_CHECKER);
00337
00338
00339 GfxFillRect(r.left + centre - 3, r.top + height, r.left + centre - 3, r.bottom - height, c1);
00340 GfxFillRect(r.left + centre - 2, r.top + height, r.left + centre - 2, r.bottom - height, c2);
00341 GfxFillRect(r.left + centre + 2, r.top + height, r.left + centre + 2, r.bottom - height, c1);
00342 GfxFillRect(r.left + centre + 3, r.top + height, r.left + centre + 3, r.bottom - height, c2);
00343
00344 Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom, false);
00345 DrawFrameRect(r.left, pt.x, r.right, pt.y, colour, bar_dragged ? FR_LOWERED : FR_NONE);
00346 }
00347
00357 static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool left_clicked, bool bar_dragged, bool right_clicked, const Scrollbar *scrollbar)
00358 {
00359 int centre = (r.bottom - r.top) / 2;
00360 int width = NWidgetScrollbar::GetHorizontalDimension().width;
00361
00362 DrawFrameRect(r.left, r.top, r.left + width - 1, r.bottom, colour, left_clicked ? FR_LOWERED : FR_NONE);
00363 DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + left_clicked, r.top + 1 + left_clicked);
00364
00365 DrawFrameRect(r.right - (width - 1), r.top, r.right, r.bottom, colour, right_clicked ? FR_LOWERED : FR_NONE);
00366 DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - (width - 2) + right_clicked, r.top + 1 + right_clicked);
00367
00368 int c1 = _colour_gradient[colour & 0xF][3];
00369 int c2 = _colour_gradient[colour & 0xF][7];
00370
00371
00372 GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c2);
00373 GfxFillRect(r.left + width, r.top, r.right - width, r.bottom, c1, FILLRECT_CHECKER);
00374
00375
00376 GfxFillRect(r.left + width, r.top + centre - 3, r.right - width, r.top + centre - 3, c1);
00377 GfxFillRect(r.left + width, r.top + centre - 2, r.right - width, r.top + centre - 2, c2);
00378 GfxFillRect(r.left + width, r.top + centre + 2, r.right - width, r.top + centre + 2, c1);
00379 GfxFillRect(r.left + width, r.top + centre + 3, r.right - width, r.top + centre + 3, c2);
00380
00381
00382 Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right, true);
00383 DrawFrameRect(pt.x, r.top, pt.y, r.bottom, colour, bar_dragged ? FR_LOWERED : FR_NONE);
00384 }
00385
00392 static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
00393 {
00394 int x2 = r.left;
00395
00396 if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str);
00397
00398 int c1 = _colour_gradient[colour][3];
00399 int c2 = _colour_gradient[colour][7];
00400
00401
00402 int dy1 = 4;
00403 if (str != STR_NULL) dy1 = FONT_HEIGHT_NORMAL / 2 - 1;
00404 int dy2 = dy1 + 1;
00405
00406 if (_current_text_dir == TD_LTR) {
00407
00408 GfxFillRect(r.left, r.top + dy1, r.left + 4, r.top + dy1, c1);
00409 GfxFillRect(r.left + 1, r.top + dy2, r.left + 4, r.top + dy2, c2);
00410
00411
00412 GfxFillRect(x2, r.top + dy1, r.right - 1, r.top + dy1, c1);
00413 GfxFillRect(x2, r.top + dy2, r.right - 2, r.top + dy2, c2);
00414 } else {
00415
00416 GfxFillRect(r.left, r.top + dy1, x2 - 2, r.top + dy1, c1);
00417 GfxFillRect(r.left + 1, r.top + dy2, x2 - 2, r.top + dy2, c2);
00418
00419
00420 GfxFillRect(r.right - 5, r.top + dy1, r.right - 1, r.top + dy1, c1);
00421 GfxFillRect(r.right - 5, r.top + dy2, r.right - 2, r.top + dy2, c2);
00422 }
00423
00424
00425 GfxFillRect(r.left, r.top + dy2, r.left, r.bottom - 1, c1);
00426 GfxFillRect(r.left + 1, r.top + dy2 + 1, r.left + 1, r.bottom - 2, c2);
00427
00428
00429 GfxFillRect(r.right - 1, r.top + dy2, r.right - 1, r.bottom - 2, c1);
00430 GfxFillRect(r.right, r.top + dy1, r.right, r.bottom - 1, c2);
00431
00432 GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
00433 GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
00434 }
00435
00442 static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
00443 {
00444 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
00445 DrawSprite((clicked) ? SPR_WINDOW_SHADE : SPR_WINDOW_UNSHADE, PAL_NONE, r.left + WD_SHADEBOX_LEFT + clicked, r.top + WD_SHADEBOX_TOP + clicked);
00446 }
00447
00454 static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
00455 {
00456 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
00457 DrawSprite((clicked) ? SPR_PIN_UP : SPR_PIN_DOWN, PAL_NONE, r.left + WD_STICKYBOX_LEFT + clicked, r.top + WD_STICKYBOX_TOP + clicked);
00458 }
00459
00466 static inline void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
00467 {
00468 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
00469 DrawSprite(SPR_WINDOW_DEBUG, PAL_NONE, r.left + WD_DEBUGBOX_LEFT + clicked, r.top + WD_DEBUGBOX_TOP + clicked);
00470 }
00471
00479 static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bool clicked)
00480 {
00481 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
00482 if (at_left) {
00483 DrawSprite(SPR_WINDOW_RESIZE_LEFT, PAL_NONE, r.left + WD_RESIZEBOX_RIGHT + clicked, r.top + WD_RESIZEBOX_TOP + clicked);
00484 } else {
00485 DrawSprite(SPR_WINDOW_RESIZE_RIGHT, PAL_NONE, r.left + WD_RESIZEBOX_LEFT + clicked, r.top + WD_RESIZEBOX_TOP + clicked);
00486 }
00487 }
00488
00495 static inline void DrawCloseBox(const Rect &r, Colours colour, StringID str)
00496 {
00497 assert(str == STR_BLACK_CROSS || str == STR_SILVER_CROSS);
00498 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE);
00499 DrawString(r.left + WD_CLOSEBOX_LEFT, r.right - WD_CLOSEBOX_RIGHT, r.top + WD_CLOSEBOX_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
00500 }
00501
00509 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
00510 {
00511 DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_BORDERONLY);
00512 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);
00513
00514 if (owner != INVALID_OWNER) {
00515 GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]);
00516 }
00517
00518 if (str != STR_NULL) {
00519 Dimension d = GetStringBoundingBox(str);
00520 int offset = max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2);
00521 DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + offset, str, TC_FROMSTRING, SA_HOR_CENTER);
00522 }
00523 }
00524
00535 static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
00536 {
00537 int text_offset = max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2);
00538
00539 if (_current_text_dir == TD_LTR) {
00540 DrawFrameRect(r.left, r.top, r.right - 12, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
00541 DrawFrameRect(r.right - 11, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
00542 DrawString(r.right - (clicked_dropdown ? 10 : 11), r.right, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
00543 if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_LEFT + clicked_button, r.right - WD_DROPDOWNTEXT_RIGHT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK);
00544 } else {
00545 DrawFrameRect(r.left + 12, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
00546 DrawFrameRect(r.left, r.top, r.left + 11, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
00547 DrawString(r.left + clicked_dropdown, r.left + 11, r.top + (clicked_dropdown ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
00548 if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_RIGHT + clicked_button, r.right - WD_DROPDOWNTEXT_LEFT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK);
00549 }
00550 }
00551
00559 static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
00560 {
00561 DrawButtonDropdown(r, colour, false, clicked, str);
00562 }
00563
00567 void Window::DrawWidgets() const
00568 {
00569 this->nested_root->Draw(this);
00570
00571 if (this->flags & WF_WHITE_BORDER) {
00572 DrawFrameRect(0, 0, this->width - 1, this->height - 1, COLOUR_WHITE, FR_BORDERONLY);
00573 }
00574
00575 if (this->flags & WF_HIGHLIGHTED) {
00576 extern bool _window_highlight_colour;
00577 for (uint i = 0; i < this->nested_array_size; i++) {
00578 const NWidgetBase *widget = this->GetWidget<NWidgetBase>(i);
00579 if (widget == NULL || !widget->IsHighlighted()) continue;
00580
00581 int left = widget->pos_x;
00582 int top = widget->pos_y;
00583 int right = left + widget->current_x - 1;
00584 int bottom = top + widget->current_y - 1;
00585
00586 int colour = _string_colourmap[_window_highlight_colour ? widget->GetHighlightColour() : TC_WHITE];
00587
00588 GfxFillRect(left, top, left, bottom - WD_BEVEL_BOTTOM, colour);
00589 GfxFillRect(left + WD_BEVEL_LEFT, top, right - WD_BEVEL_RIGHT, top, colour);
00590 GfxFillRect(right, top, right, bottom - WD_BEVEL_BOTTOM, colour);
00591 GfxFillRect(left, bottom, right, bottom, colour);
00592 }
00593 }
00594 }
00595
00601 void Window::DrawSortButtonState(int widget, SortButtonState state) const
00602 {
00603 if (state == SBS_OFF) return;
00604
00605 assert(this->nested_array != NULL);
00606 const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
00607
00608 int offset = this->IsWidgetLowered(widget) ? 1 : 0;
00609 int base = offset + nwid->pos_x + (_current_text_dir == TD_LTR ? nwid->current_x - WD_SORTBUTTON_ARROW_WIDTH : 0);
00610 int top = nwid->pos_y;
00611
00612 DrawString(base, base + WD_SORTBUTTON_ARROW_WIDTH, top + 1 + offset, state == SBS_DOWN ? DOWNARROW : UPARROW, TC_BLACK, SA_HOR_CENTER);
00613 }
00614
00615
00673 NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator()
00674 {
00675 this->type = tp;
00676 }
00677
00678
00679
00727 void NWidgetBase::SetDirty(const Window *w) const
00728 {
00729 int abs_left = w->left + this->pos_x;
00730 int abs_top = w->top + this->pos_y;
00731 SetDirtyBlocks(abs_left, abs_top, abs_left + this->current_x, abs_top + this->current_y);
00732 }
00733
00747 NWidgetBase *NWidgetBase::GetWidgetOfType(WidgetType tp)
00748 {
00749 return (this->type == tp) ? this : NULL;
00750 }
00751
00758 NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y) : NWidgetBase(tp)
00759 {
00760 this->fill_x = fill_x;
00761 this->fill_y = fill_y;
00762 }
00763
00769 void NWidgetResizeBase::SetMinimalSize(uint min_x, uint min_y)
00770 {
00771 this->min_x = min_x;
00772 this->min_y = min_y;
00773 }
00774
00781 void NWidgetResizeBase::SetMinimalTextLines(uint8 min_lines, uint8 spacing, FontSize size)
00782 {
00783 this->min_y = min_lines * GetCharacterHeight(size) + spacing;
00784 }
00785
00791 void NWidgetResizeBase::SetFill(uint fill_x, uint fill_y)
00792 {
00793 this->fill_x = fill_x;
00794 this->fill_y = fill_y;
00795 }
00796
00802 void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y)
00803 {
00804 this->resize_x = resize_x;
00805 this->resize_y = resize_y;
00806 }
00807
00808 void NWidgetResizeBase::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
00809 {
00810 this->StoreSizePosition(sizing, x, y, given_width, given_height);
00811 }
00812
00822 NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y, uint16 widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y)
00823 {
00824 this->colour = colour;
00825 this->index = -1;
00826 this->widget_data = widget_data;
00827 this->tool_tip = tool_tip;
00828 this->scrollbar_index = -1;
00829 }
00830
00835 void NWidgetCore::SetIndex(int index)
00836 {
00837 assert(index >= 0);
00838 this->index = index;
00839 }
00840
00846 void NWidgetCore::SetDataTip(uint16 widget_data, StringID tool_tip)
00847 {
00848 this->widget_data = widget_data;
00849 this->tool_tip = tool_tip;
00850 }
00851
00852 void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
00853 {
00854 if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
00855 }
00856
00857 NWidgetCore *NWidgetCore::GetWidgetFromPos(int x, int y)
00858 {
00859 return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : NULL;
00860 }
00861
00866 NWidgetContainer::NWidgetContainer(WidgetType tp) : NWidgetBase(tp)
00867 {
00868 this->head = NULL;
00869 this->tail = NULL;
00870 }
00871
00872 NWidgetContainer::~NWidgetContainer()
00873 {
00874 while (this->head != NULL) {
00875 NWidgetBase *wid = this->head->next;
00876 delete this->head;
00877 this->head = wid;
00878 }
00879 this->tail = NULL;
00880 }
00881
00882 NWidgetBase *NWidgetContainer::GetWidgetOfType(WidgetType tp)
00883 {
00884 if (this->type == tp) return this;
00885 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
00886 NWidgetBase *nwid = child_wid->GetWidgetOfType(tp);
00887 if (nwid != NULL) return nwid;
00888 }
00889 return NULL;
00890 }
00891
00896 void NWidgetContainer::Add(NWidgetBase *wid)
00897 {
00898 assert(wid->next == NULL && wid->prev == NULL);
00899
00900 if (this->head == NULL) {
00901 this->head = wid;
00902 this->tail = wid;
00903 } else {
00904 assert(this->tail != NULL);
00905 assert(this->tail->next == NULL);
00906
00907 this->tail->next = wid;
00908 wid->prev = this->tail;
00909 this->tail = wid;
00910 }
00911 }
00912
00913 void NWidgetContainer::FillNestedArray(NWidgetBase **array, uint length)
00914 {
00915 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
00916 child_wid->FillNestedArray(array, length);
00917 }
00918 }
00919
00923 NWidgetStacked::NWidgetStacked() : NWidgetContainer(NWID_SELECTION)
00924 {
00925 this->index = -1;
00926 }
00927
00928 void NWidgetStacked::SetIndex(int index)
00929 {
00930 this->index = index;
00931 }
00932
00933 void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array)
00934 {
00935 if (this->index >= 0 && init_array) {
00936 assert(w->nested_array_size > (uint)this->index);
00937 w->nested_array[this->index] = this;
00938 }
00939
00940
00941 if (this->shown_plane >= SZSP_BEGIN) {
00942 Dimension size = {0, 0};
00943 Dimension padding = {0, 0};
00944 Dimension fill = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
00945 Dimension resize = {(this->shown_plane == SZSP_HORIZONTAL), (this->shown_plane == SZSP_VERTICAL)};
00946
00947 if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
00948
00949 this->smallest_x = size.width;
00950 this->smallest_y = size.height;
00951 this->fill_x = fill.width;
00952 this->fill_y = fill.height;
00953 this->resize_x = resize.width;
00954 this->resize_y = resize.height;
00955 return;
00956 }
00957
00958
00959 this->smallest_x = 0;
00960 this->smallest_y = 0;
00961 this->fill_x = (this->head != NULL) ? 1 : 0;
00962 this->fill_y = (this->head != NULL) ? 1 : 0;
00963 this->resize_x = (this->head != NULL) ? 1 : 0;
00964 this->resize_y = (this->head != NULL) ? 1 : 0;
00965 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
00966 child_wid->SetupSmallestSize(w, init_array);
00967
00968 this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
00969 this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
00970 this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
00971 this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
00972 this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
00973 this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
00974 }
00975 }
00976
00977 void NWidgetStacked::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
00978 {
00979 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
00980 this->StoreSizePosition(sizing, x, y, given_width, given_height);
00981
00982 if (this->shown_plane >= SZSP_BEGIN) return;
00983
00984 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
00985 uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
00986 uint child_width = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
00987 uint child_pos_x = (rtl ? child_wid->padding_right : child_wid->padding_left);
00988
00989 uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
00990 uint child_height = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
00991 uint child_pos_y = child_wid->padding_top;
00992
00993 child_wid->AssignSizePosition(sizing, x + child_pos_x, y + child_pos_y, child_width, child_height, rtl);
00994 }
00995 }
00996
00997 void NWidgetStacked::FillNestedArray(NWidgetBase **array, uint length)
00998 {
00999 if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
01000 NWidgetContainer::FillNestedArray(array, length);
01001 }
01002
01003 void NWidgetStacked::Draw(const Window *w)
01004 {
01005 if (this->shown_plane >= SZSP_BEGIN) return;
01006
01007 int plane = 0;
01008 for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
01009 if (plane == this->shown_plane) {
01010 child_wid->Draw(w);
01011 return;
01012 }
01013 }
01014
01015 NOT_REACHED();
01016 }
01017
01018 NWidgetCore *NWidgetStacked::GetWidgetFromPos(int x, int y)
01019 {
01020 if (this->shown_plane >= SZSP_BEGIN) return NULL;
01021
01022 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
01023 int plane = 0;
01024 for (NWidgetBase *child_wid = this->head; child_wid != NULL; plane++, child_wid = child_wid->next) {
01025 if (plane == this->shown_plane) {
01026 return child_wid->GetWidgetFromPos(x, y);
01027 }
01028 }
01029 return NULL;
01030 }
01031
01036 void NWidgetStacked::SetDisplayedPlane(int plane)
01037 {
01038 this->shown_plane = plane;
01039 }
01040
01041 NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
01042 {
01043 this->flags = flags;
01044 }
01045
01055 void NWidgetPIPContainer::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
01056 {
01057 this->pip_pre = pip_pre;
01058 this->pip_inter = pip_inter;
01059 this->pip_post = pip_post;
01060 }
01061
01062 void NWidgetPIPContainer::Draw(const Window *w)
01063 {
01064 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01065 child_wid->Draw(w);
01066 }
01067 }
01068
01069 NWidgetCore *NWidgetPIPContainer::GetWidgetFromPos(int x, int y)
01070 {
01071 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
01072
01073 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01074 NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
01075 if (nwid != NULL) return nwid;
01076 }
01077 return NULL;
01078 }
01079
01081 NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
01082 {
01083 }
01084
01085 void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array)
01086 {
01087 this->smallest_x = 0;
01088 this->smallest_y = 0;
01089 this->fill_x = 0;
01090 this->fill_y = 1;
01091 this->resize_x = 0;
01092 this->resize_y = 1;
01093
01094
01095 uint longest = 0;
01096 uint max_vert_fill = 0;
01097 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01098 child_wid->SetupSmallestSize(w, init_array);
01099 longest = max(longest, child_wid->smallest_x);
01100 max_vert_fill = max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST));
01101 this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
01102 }
01103
01104 uint max_smallest = this->smallest_y + 3 * max_vert_fill;
01105 uint cur_height = this->smallest_y;
01106 for (;;) {
01107 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01108 uint step_size = child_wid->GetVerticalStepSize(ST_SMALLEST);
01109 uint child_height = child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
01110 if (step_size > 1 && child_height < cur_height) {
01111 uint remainder = (cur_height - child_height) % step_size;
01112 if (remainder > 0) {
01113 cur_height += step_size - remainder;
01114 assert(cur_height < max_smallest);
01115
01116 }
01117 }
01118 }
01119 if (this->smallest_y == cur_height) break;
01120 this->smallest_y = cur_height;
01121 }
01122
01123 if (this->flags & NC_EQUALSIZE) {
01124 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01125 if (child_wid->fill_x == 1) child_wid->smallest_x = longest;
01126 }
01127 }
01128
01129 if (this->head != NULL) this->head->padding_left += this->pip_pre;
01130 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01131 if (child_wid->next != NULL) {
01132 child_wid->padding_right += this->pip_inter;
01133 } else {
01134 child_wid->padding_right += this->pip_post;
01135 }
01136
01137 this->smallest_x += child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
01138 if (child_wid->fill_x > 0) {
01139 if (this->fill_x == 0 || this->fill_x > child_wid->fill_x) this->fill_x = child_wid->fill_x;
01140 }
01141 this->fill_y = LeastCommonMultiple(this->fill_y, child_wid->fill_y);
01142
01143 if (child_wid->resize_x > 0) {
01144 if (this->resize_x == 0 || this->resize_x > child_wid->resize_x) this->resize_x = child_wid->resize_x;
01145 }
01146 this->resize_y = LeastCommonMultiple(this->resize_y, child_wid->resize_y);
01147 }
01148
01149 this->pip_pre = this->pip_inter = this->pip_post = 0;
01150 }
01151
01152 void NWidgetHorizontal::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01153 {
01154 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
01155
01156
01157 uint additional_length = given_width;
01158 if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
01159
01160 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01161 additional_length -= child_wid->smallest_x + child_wid->padding_right + child_wid->padding_left;
01162 }
01163 } else {
01164 additional_length -= this->smallest_x;
01165 }
01166
01167 this->StoreSizePosition(sizing, x, y, given_width, given_height);
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181 int num_changing_childs = 0;
01182 uint biggest_stepsize = 0;
01183 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01184 uint hor_step = child_wid->GetHorizontalStepSize(sizing);
01185 if (hor_step > 0) {
01186 num_changing_childs++;
01187 biggest_stepsize = max(biggest_stepsize, hor_step);
01188 } else {
01189 child_wid->current_x = child_wid->smallest_x;
01190 }
01191
01192 uint vert_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetVerticalStepSize(sizing);
01193 child_wid->current_y = ComputeMaxSize(child_wid->smallest_y, given_height - child_wid->padding_top - child_wid->padding_bottom, vert_step);
01194 }
01195
01196
01197 while (biggest_stepsize > 0) {
01198 uint next_biggest_stepsize = 0;
01199 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01200 uint hor_step = child_wid->GetHorizontalStepSize(sizing);
01201 if (hor_step > biggest_stepsize) continue;
01202 if (hor_step == biggest_stepsize) {
01203 uint increment = additional_length / num_changing_childs;
01204 num_changing_childs--;
01205 if (hor_step > 1) increment -= increment % hor_step;
01206 child_wid->current_x = child_wid->smallest_x + increment;
01207 additional_length -= increment;
01208 continue;
01209 }
01210 next_biggest_stepsize = max(next_biggest_stepsize, hor_step);
01211 }
01212 biggest_stepsize = next_biggest_stepsize;
01213 }
01214 assert(num_changing_childs == 0);
01215
01216
01217 uint position = 0;
01218 NWidgetBase *child_wid = rtl ? this->tail : this->head;
01219 while (child_wid != NULL) {
01220 uint child_width = child_wid->current_x;
01221 uint child_x = x + position + (rtl ? child_wid->padding_right : child_wid->padding_left);
01222 uint child_y = y + child_wid->padding_top;
01223
01224 child_wid->AssignSizePosition(sizing, child_x, child_y, child_width, child_wid->current_y, rtl);
01225 position += child_width + child_wid->padding_right + child_wid->padding_left;
01226
01227 child_wid = rtl ? child_wid->prev : child_wid->next;
01228 }
01229 }
01230
01232 NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
01233 {
01234 this->type = NWID_HORIZONTAL_LTR;
01235 }
01236
01237 void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01238 {
01239 NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
01240 }
01241
01243 NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
01244 {
01245 }
01246
01247 void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array)
01248 {
01249 this->smallest_x = 0;
01250 this->smallest_y = 0;
01251 this->fill_x = 1;
01252 this->fill_y = 0;
01253 this->resize_x = 1;
01254 this->resize_y = 0;
01255
01256
01257 uint highest = 0;
01258 uint max_hor_fill = 0;
01259 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01260 child_wid->SetupSmallestSize(w, init_array);
01261 highest = max(highest, child_wid->smallest_y);
01262 max_hor_fill = max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST));
01263 this->smallest_x = max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right);
01264 }
01265
01266 uint max_smallest = this->smallest_x + 3 * max_hor_fill;
01267 uint cur_width = this->smallest_x;
01268 for (;;) {
01269 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01270 uint step_size = child_wid->GetHorizontalStepSize(ST_SMALLEST);
01271 uint child_width = child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right;
01272 if (step_size > 1 && child_width < cur_width) {
01273 uint remainder = (cur_width - child_width) % step_size;
01274 if (remainder > 0) {
01275 cur_width += step_size - remainder;
01276 assert(cur_width < max_smallest);
01277
01278 }
01279 }
01280 }
01281 if (this->smallest_x == cur_width) break;
01282 this->smallest_x = cur_width;
01283 }
01284
01285 if (this->flags & NC_EQUALSIZE) {
01286 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01287 if (child_wid->fill_y == 1) child_wid->smallest_y = highest;
01288 }
01289 }
01290
01291 if (this->head != NULL) this->head->padding_top += this->pip_pre;
01292 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01293 if (child_wid->next != NULL) {
01294 child_wid->padding_bottom += this->pip_inter;
01295 } else {
01296 child_wid->padding_bottom += this->pip_post;
01297 }
01298
01299 this->smallest_y += child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
01300 if (child_wid->fill_y > 0) {
01301 if (this->fill_y == 0 || this->fill_y > child_wid->fill_y) this->fill_y = child_wid->fill_y;
01302 }
01303 this->fill_x = LeastCommonMultiple(this->fill_x, child_wid->fill_x);
01304
01305 if (child_wid->resize_y > 0) {
01306 if (this->resize_y == 0 || this->resize_y > child_wid->resize_y) this->resize_y = child_wid->resize_y;
01307 }
01308 this->resize_x = LeastCommonMultiple(this->resize_x, child_wid->resize_x);
01309 }
01310
01311 this->pip_pre = this->pip_inter = this->pip_post = 0;
01312 }
01313
01314 void NWidgetVertical::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01315 {
01316 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
01317
01318
01319 uint additional_length = given_height;
01320 if (sizing == ST_SMALLEST && (this->flags & NC_EQUALSIZE)) {
01321
01322 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01323 additional_length -= child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom;
01324 }
01325 } else {
01326 additional_length -= this->smallest_y;
01327 }
01328
01329 this->StoreSizePosition(sizing, x, y, given_width, given_height);
01330
01331
01332
01333
01334
01335
01336 int num_changing_childs = 0;
01337 uint biggest_stepsize = 0;
01338 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01339 uint vert_step = child_wid->GetVerticalStepSize(sizing);
01340 if (vert_step > 0) {
01341 num_changing_childs++;
01342 biggest_stepsize = max(biggest_stepsize, vert_step);
01343 } else {
01344 child_wid->current_y = child_wid->smallest_y;
01345 }
01346
01347 uint hor_step = (sizing == ST_SMALLEST) ? 1 : child_wid->GetHorizontalStepSize(sizing);
01348 child_wid->current_x = ComputeMaxSize(child_wid->smallest_x, given_width - child_wid->padding_left - child_wid->padding_right, hor_step);
01349 }
01350
01351
01352 while (biggest_stepsize > 0) {
01353 uint next_biggest_stepsize = 0;
01354 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01355 uint vert_step = child_wid->GetVerticalStepSize(sizing);
01356 if (vert_step > biggest_stepsize) continue;
01357 if (vert_step == biggest_stepsize) {
01358 uint increment = additional_length / num_changing_childs;
01359 num_changing_childs--;
01360 if (vert_step > 1) increment -= increment % vert_step;
01361 child_wid->current_y = child_wid->smallest_y + increment;
01362 additional_length -= increment;
01363 continue;
01364 }
01365 next_biggest_stepsize = max(next_biggest_stepsize, vert_step);
01366 }
01367 biggest_stepsize = next_biggest_stepsize;
01368 }
01369 assert(num_changing_childs == 0);
01370
01371
01372 uint position = 0;
01373 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01374 uint child_x = x + (rtl ? child_wid->padding_right : child_wid->padding_left);
01375 uint child_height = child_wid->current_y;
01376
01377 child_wid->AssignSizePosition(sizing, child_x, y + position + child_wid->padding_top, child_wid->current_x, child_height, rtl);
01378 position += child_height + child_wid->padding_top + child_wid->padding_bottom;
01379 }
01380 }
01381
01387 NWidgetSpacer::NWidgetSpacer(int length, int height) : NWidgetResizeBase(NWID_SPACER, 0, 0)
01388 {
01389 this->SetMinimalSize(length, height);
01390 this->SetResize(0, 0);
01391 }
01392
01393 void NWidgetSpacer::SetupSmallestSize(Window *w, bool init_array)
01394 {
01395 this->smallest_x = this->min_x;
01396 this->smallest_y = this->min_y;
01397 }
01398
01399 void NWidgetSpacer::FillNestedArray(NWidgetBase **array, uint length)
01400 {
01401 }
01402
01403 void NWidgetSpacer::Draw(const Window *w)
01404 {
01405
01406 }
01407
01408 void NWidgetSpacer::SetDirty(const Window *w) const
01409 {
01410
01411 }
01412
01413 NWidgetCore *NWidgetSpacer::GetWidgetFromPos(int x, int y)
01414 {
01415 return NULL;
01416 }
01417
01418 NWidgetMatrix::NWidgetMatrix() : NWidgetPIPContainer(NWID_MATRIX, NC_EQUALSIZE), index(-1), clicked(-1), count(-1)
01419 {
01420 }
01421
01422 void NWidgetMatrix::SetIndex(int index)
01423 {
01424 this->index = index;
01425 }
01426
01427 void NWidgetMatrix::SetColour(Colours colour)
01428 {
01429 this->colour = colour;
01430 }
01431
01436 void NWidgetMatrix::SetClicked(int clicked)
01437 {
01438 this->clicked = clicked;
01439 if (this->clicked >= 0 && this->sb != NULL && this->widgets_x != 0) {
01440 int vpos = (this->clicked / this->widgets_x) * this->widget_h;
01441
01442
01443 if (this->sb->GetPosition() < vpos) vpos += this->widget_h - this->pip_inter - 1;
01444 this->sb->ScrollTowards(vpos);
01445 }
01446 }
01447
01453 void NWidgetMatrix::SetCount(int count)
01454 {
01455 this->count = count;
01456
01457 if (this->sb == NULL || this->widgets_x == 0) return;
01458
01459
01460
01461
01462
01463
01464 count = CeilDiv(count, this->sb->IsVertical() ? this->widgets_x : this->widgets_y);
01465 count *= (this->sb->IsVertical() ? this->head->smallest_y : this->head->smallest_x) + this->pip_inter;
01466 count += -this->pip_inter + this->pip_pre + this->pip_post;
01467 this->sb->SetCount(count);
01468 this->sb->SetCapacity(this->sb->IsVertical() ? this->current_y : this->current_x);
01469 this->sb->SetStepSize(this->sb->IsVertical() ? this->widget_h : this->widget_w);
01470 }
01471
01476 void NWidgetMatrix::SetScrollbar(Scrollbar *sb)
01477 {
01478 this->sb = sb;
01479 }
01480
01481 void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array)
01482 {
01483 assert(this->head != NULL);
01484 assert(this->head->next == NULL);
01485
01486 if (this->index >= 0 && init_array) {
01487 assert(w->nested_array_size > (uint)this->index);
01488 w->nested_array[this->index] = this;
01489 }
01490
01491
01492 SB(dynamic_cast<NWidgetCore *>(this->head)->index, 16, 16, 0);
01493 this->head->SetupSmallestSize(w, init_array);
01494
01495 Dimension padding = {this->pip_pre + this->pip_post, this->pip_pre + this->pip_post};
01496 Dimension size = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height};
01497 Dimension fill = {0, 0};
01498 Dimension resize = {this->pip_inter + this->head->smallest_x, this->pip_inter + this->head->smallest_y};
01499
01500 if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, padding, &fill, &resize);
01501
01502 this->smallest_x = size.width;
01503 this->smallest_y = size.height;
01504 this->fill_x = fill.width;
01505 this->fill_y = fill.height;
01506 this->resize_x = resize.width;
01507 this->resize_y = resize.height;
01508 }
01509
01510 void NWidgetMatrix::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01511 {
01512 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
01513
01514 this->pos_x = x;
01515 this->pos_y = y;
01516 this->current_x = given_width;
01517 this->current_y = given_height;
01518
01519
01520 this->widget_w = this->head->smallest_x + this->pip_inter;
01521 this->widget_h = this->head->smallest_y + this->pip_inter;
01522
01523
01524
01525 this->widgets_x = CeilDiv(this->current_x - this->pip_pre - this->pip_post + this->pip_inter, this->widget_w);
01526 this->widgets_y = CeilDiv(this->current_y - this->pip_pre - this->pip_post + this->pip_inter, this->widget_h);
01527
01528
01529
01530
01531 this->SetCount(this->count);
01532 }
01533
01534 void NWidgetMatrix::FillNestedArray(NWidgetBase **array, uint length)
01535 {
01536 if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
01537 NWidgetContainer::FillNestedArray(array, length);
01538 }
01539
01540 NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y)
01541 {
01542
01543 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
01544
01545 int start_x, start_y, base_offs_x, base_offs_y;
01546 this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
01547
01548
01549 bool rtl = _current_text_dir == TD_RTL;
01550 if (rtl) base_offs_x -= (this->widgets_x - 1) * this->widget_w;
01551
01552 int widget_col = (x - base_offs_x - (int)this->pip_pre - (int)this->pos_x) / this->widget_w;
01553 int widget_row = (y - base_offs_y - (int)this->pip_pre - (int)this->pos_y) / this->widget_h;
01554
01555 if (widget_row * this->widgets_x + widget_col >= this->count) return NULL;
01556
01557 NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
01558 child->AssignSizePosition(ST_RESIZE,
01559 this->pos_x + this->pip_pre + widget_col * this->widget_w + base_offs_x,
01560 this->pos_y + this->pip_pre + widget_row * this->widget_h + base_offs_y,
01561 child->smallest_x, child->smallest_y, rtl);
01562
01563
01564 if (rtl) widget_col = this->widgets_x - widget_col - 1;
01565 SB(child->index, 16, 16, (widget_row + start_y) * this->widgets_x + widget_col + start_x);
01566
01567 return child->GetWidgetFromPos(x, y);
01568 }
01569
01570 void NWidgetMatrix::Draw(const Window *w)
01571 {
01572
01573 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]);
01574
01575
01576 DrawPixelInfo tmp_dpi;
01577 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;
01578 DrawPixelInfo *old_dpi = _cur_dpi;
01579 _cur_dpi = &tmp_dpi;
01580
01581
01582 NWidgetCore *child = dynamic_cast<NWidgetCore *>(this->head);
01583 bool rtl = _current_text_dir == TD_RTL;
01584 int start_x, start_y, base_offs_x, base_offs_y;
01585 this->GetScrollOffsets(start_x, start_y, base_offs_x, base_offs_y);
01586
01587 int offs_y = base_offs_y;
01588 for (int y = start_y; y < start_y + this->widgets_y + 1; y++, offs_y += this->widget_h) {
01589
01590 if (offs_y + child->smallest_y <= 0) continue;
01591 if (offs_y >= (int)this->current_y) break;
01592
01593
01594 if (y * this->widgets_x >= this->count) break;
01595
01596 int offs_x = base_offs_x;
01597 for (int x = start_x; x < start_x + this->widgets_x + 1; x++, offs_x += rtl ? -this->widget_w : this->widget_w) {
01598
01599 if (offs_x + child->smallest_x <= 0) continue;
01600 if (offs_x >= (int)this->current_x) continue;
01601
01602
01603 int sub_wid = y * this->widgets_x + x;
01604 if (sub_wid >= this->count) break;
01605
01606 child->AssignSizePosition(ST_RESIZE, offs_x, offs_y, child->smallest_x, child->smallest_y, rtl);
01607 child->SetLowered(this->clicked == sub_wid);
01608 SB(child->index, 16, 16, sub_wid);
01609 child->Draw(w);
01610 }
01611 }
01612
01613
01614 _cur_dpi = old_dpi;
01615 }
01616
01624 void NWidgetMatrix::GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
01625 {
01626 base_offs_x = 0;
01627 base_offs_y = 0;
01628 start_x = 0;
01629 start_y = 0;
01630 if (this->sb != NULL) {
01631 if (this->sb->IsVertical()) {
01632 start_y = this->sb->GetPosition() / this->widget_h;
01633 base_offs_y = -this->sb->GetPosition() + start_y * this->widget_h;
01634 if (_current_text_dir == TD_RTL) base_offs_x = this->pip_pre + this->widget_w * (this->widgets_x - 1) - this->pip_inter;
01635 } else {
01636 start_x = this->sb->GetPosition() / this->widget_w;
01637 if (_current_text_dir == TD_RTL) {
01638 base_offs_x = this->sb->GetCapacity() + this->sb->GetPosition() - (start_x + 1) * this->widget_w + this->pip_inter - this->pip_post - this->pip_pre;
01639 } else {
01640 base_offs_x = -this->sb->GetPosition() + start_x * this->widget_w;
01641 }
01642 }
01643 }
01644 }
01645
01655 NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, NWidgetPIPContainer *child) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL)
01656 {
01657 assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
01658 if (index >= 0) this->SetIndex(index);
01659 this->child = child;
01660 }
01661
01662 NWidgetBackground::~NWidgetBackground()
01663 {
01664 if (this->child != NULL) delete this->child;
01665 }
01666
01674 void NWidgetBackground::Add(NWidgetBase *nwid)
01675 {
01676 if (this->child == NULL) {
01677 this->child = new NWidgetVertical();
01678 }
01679 this->child->Add(nwid);
01680 }
01681
01692 void NWidgetBackground::SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post)
01693 {
01694 if (this->child == NULL) {
01695 this->child = new NWidgetVertical();
01696 }
01697 this->child->SetPIP(pip_pre, pip_inter, pip_post);
01698 }
01699
01700 void NWidgetBackground::SetupSmallestSize(Window *w, bool init_array)
01701 {
01702 if (init_array && this->index >= 0) {
01703 assert(w->nested_array_size > (uint)this->index);
01704 w->nested_array[this->index] = this;
01705 }
01706 if (this->child != NULL) {
01707 this->child->SetupSmallestSize(w, init_array);
01708
01709 this->smallest_x = this->child->smallest_x;
01710 this->smallest_y = this->child->smallest_y;
01711 this->fill_x = this->child->fill_x;
01712 this->fill_y = this->child->fill_y;
01713 this->resize_x = this->child->resize_x;
01714 this->resize_y = this->child->resize_y;
01715
01716
01717 if (w != NULL && this->type == WWT_FRAME) {
01718 this->child->padding_left = WD_FRAMETEXT_LEFT;
01719 this->child->padding_right = WD_FRAMETEXT_RIGHT;
01720 this->child->padding_top = max((int)WD_FRAMETEXT_TOP, this->widget_data != STR_NULL ? FONT_HEIGHT_NORMAL + WD_FRAMETEXT_TOP / 2 : 0);
01721 this->child->padding_bottom = WD_FRAMETEXT_BOTTOM;
01722
01723 this->smallest_x += this->child->padding_left + this->child->padding_right;
01724 this->smallest_y += this->child->padding_top + this->child->padding_bottom;
01725
01726 if (this->index >= 0) w->SetStringParameters(this->index);
01727 this->smallest_x = max(this->smallest_x, GetStringBoundingBox(this->widget_data).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
01728 }
01729 } else {
01730 Dimension d = {this->min_x, this->min_y};
01731 Dimension fill = {this->fill_x, this->fill_y};
01732 Dimension resize = {this->resize_x, this->resize_y};
01733 if (w != NULL) {
01734 if (this->type == WWT_FRAME || this->type == WWT_INSET) {
01735 if (this->index >= 0) w->SetStringParameters(this->index);
01736 Dimension background = GetStringBoundingBox(this->widget_data);
01737 background.width += (this->type == WWT_FRAME) ? (WD_FRAMETEXT_LEFT + WD_FRAMERECT_RIGHT) : (WD_INSET_LEFT + WD_INSET_RIGHT);
01738 d = maxdim(d, background);
01739 }
01740 if (this->index >= 0) {
01741 static const Dimension padding = {0, 0};
01742 w->UpdateWidgetSize(this->index, &d, padding, &fill, &resize);
01743 }
01744 }
01745 this->smallest_x = d.width;
01746 this->smallest_y = d.height;
01747 this->fill_x = fill.width;
01748 this->fill_y = fill.height;
01749 this->resize_x = resize.width;
01750 this->resize_y = resize.height;
01751 }
01752 }
01753
01754 void NWidgetBackground::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01755 {
01756 this->StoreSizePosition(sizing, x, y, given_width, given_height);
01757
01758 if (this->child != NULL) {
01759 uint x_offset = (rtl ? this->child->padding_right : this->child->padding_left);
01760 uint width = given_width - this->child->padding_right - this->child->padding_left;
01761 uint height = given_height - this->child->padding_top - this->child->padding_bottom;
01762 this->child->AssignSizePosition(sizing, x + x_offset, y + this->child->padding_top, width, height, rtl);
01763 }
01764 }
01765
01766 void NWidgetBackground::FillNestedArray(NWidgetBase **array, uint length)
01767 {
01768 if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
01769 if (this->child != NULL) this->child->FillNestedArray(array, length);
01770 }
01771
01772 void NWidgetBackground::Draw(const Window *w)
01773 {
01774 if (this->current_x == 0 || this->current_y == 0) return;
01775
01776 Rect r;
01777 r.left = this->pos_x;
01778 r.right = this->pos_x + this->current_x - 1;
01779 r.top = this->pos_y;
01780 r.bottom = this->pos_y + this->current_y - 1;
01781
01782 const DrawPixelInfo *dpi = _cur_dpi;
01783 if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
01784
01785 switch (this->type) {
01786 case WWT_PANEL:
01787 assert(this->widget_data == 0);
01788 DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, this->IsLowered() ? FR_LOWERED : FR_NONE);
01789 break;
01790
01791 case WWT_FRAME:
01792 if (this->index >= 0) w->SetStringParameters(this->index);
01793 DrawFrame(r, this->colour, this->widget_data);
01794 break;
01795
01796 case WWT_INSET:
01797 if (this->index >= 0) w->SetStringParameters(this->index);
01798 DrawInset(r, this->colour, this->widget_data);
01799 break;
01800
01801 default:
01802 NOT_REACHED();
01803 }
01804
01805 if (this->index >= 0) w->DrawWidget(r, this->index);
01806 if (this->child != NULL) this->child->Draw(w);
01807
01808 if (this->IsDisabled()) {
01809 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
01810 }
01811 }
01812
01813 NWidgetCore *NWidgetBackground::GetWidgetFromPos(int x, int y)
01814 {
01815 NWidgetCore *nwid = NULL;
01816 if (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) {
01817 if (this->child != NULL) nwid = this->child->GetWidgetFromPos(x, y);
01818 if (nwid == NULL) nwid = this;
01819 }
01820 return nwid;
01821 }
01822
01823 NWidgetBase *NWidgetBackground::GetWidgetOfType(WidgetType tp)
01824 {
01825 NWidgetBase *nwid = NULL;
01826 if (this->child != NULL) nwid = this->child->GetWidgetOfType(tp);
01827 if (nwid == NULL && this->type == tp) nwid = this;
01828 return nwid;
01829 }
01830
01831 NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID_COLOUR, 1, 1, 0x0, STR_NULL)
01832 {
01833 this->SetIndex(index);
01834 }
01835
01836 void NWidgetViewport::SetupSmallestSize(Window *w, bool init_array)
01837 {
01838 if (init_array && this->index >= 0) {
01839 assert(w->nested_array_size > (uint)this->index);
01840 w->nested_array[this->index] = this;
01841 }
01842 this->smallest_x = this->min_x;
01843 this->smallest_y = this->min_y;
01844 }
01845
01846 void NWidgetViewport::Draw(const Window *w)
01847 {
01848 if (this->disp_flags & ND_NO_TRANSPARENCY) {
01849 TransparencyOptionBits to_backup = _transparency_opt;
01850 _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_LOADING);
01851 w->DrawViewport();
01852 _transparency_opt = to_backup;
01853 } else {
01854 w->DrawViewport();
01855 }
01856
01857
01858 if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) {
01859 GfxFillRect(this->pos_x, this->pos_y, this->pos_x + this->current_x - 1, this->pos_y + this->current_y - 1,
01860 (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
01861 }
01862 }
01863
01870 void NWidgetViewport::InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
01871 {
01872 InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom);
01873 }
01874
01879 void NWidgetViewport::UpdateViewportCoordinates(Window *w)
01880 {
01881 ViewPort *vp = w->viewport;
01882 if (vp != NULL) {
01883 vp->left = w->left + this->pos_x;
01884 vp->top = w->top + this->pos_y;
01885 vp->width = this->current_x;
01886 vp->height = this->current_y;
01887
01888 vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
01889 vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
01890 }
01891 }
01892
01902 int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
01903 {
01904 uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
01905 if (pos != INT_MAX) pos += this->GetPosition();
01906 return (pos >= this->GetCount()) ? INT_MAX : pos;
01907 }
01908
01916 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
01917 {
01918 NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
01919 if (this->IsVertical()) {
01920 this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
01921 } else {
01922 this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
01923 }
01924 }
01925
01932 NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : NWidgetCore(tp, colour, 1, 1, 0x0, STR_NULL), Scrollbar(tp != NWID_HSCROLLBAR)
01933 {
01934 assert(tp == NWID_HSCROLLBAR || tp == NWID_VSCROLLBAR);
01935 this->SetIndex(index);
01936
01937 switch (this->type) {
01938 case NWID_HSCROLLBAR:
01939 this->SetMinimalSize(0, NWidgetScrollbar::GetHorizontalDimension().height);
01940 this->SetResize(1, 0);
01941 this->SetFill(1, 0);
01942 this->SetDataTip(0x0, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01943 break;
01944
01945 case NWID_VSCROLLBAR:
01946 this->SetMinimalSize(NWidgetScrollbar::GetVerticalDimension().width, 0);
01947 this->SetResize(0, 1);
01948 this->SetFill(0, 1);
01949 this->SetDataTip(0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST);
01950 break;
01951
01952 default: NOT_REACHED();
01953 }
01954 }
01955
01956 void NWidgetScrollbar::SetupSmallestSize(Window *w, bool init_array)
01957 {
01958 if (init_array && this->index >= 0) {
01959 assert(w->nested_array_size > (uint)this->index);
01960 w->nested_array[this->index] = this;
01961 }
01962 this->smallest_x = this->min_x;
01963 this->smallest_y = this->min_y;
01964 }
01965
01966 void NWidgetScrollbar::Draw(const Window *w)
01967 {
01968 if (this->current_x == 0 || this->current_y == 0) return;
01969
01970 Rect r;
01971 r.left = this->pos_x;
01972 r.right = this->pos_x + this->current_x - 1;
01973 r.top = this->pos_y;
01974 r.bottom = this->pos_y + this->current_y - 1;
01975
01976 const DrawPixelInfo *dpi = _cur_dpi;
01977 if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
01978
01979 bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
01980 bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
01981 bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->scrolling_scrollbar == this->index;
01982
01983 if (this->type == NWID_HSCROLLBAR) {
01984 DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
01985 } else {
01986 DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
01987 }
01988
01989 if (this->IsDisabled()) {
01990 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
01991 }
01992 }
01993
01994 void NWidgetScrollbar::InvalidateDimensionCache()
01995 {
01996 vertical_dimension.width = vertical_dimension.height = 0;
01997 horizontal_dimension.width = horizontal_dimension.height = 0;
01998 }
01999
02000 Dimension NWidgetScrollbar::GetVerticalDimension()
02001 {
02002 static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
02003 if (vertical_dimension.width == 0) {
02004 vertical_dimension = maxdim(GetSpriteSize(SPR_ARROW_UP), GetSpriteSize(SPR_ARROW_DOWN));
02005 vertical_dimension.width += extra.width;
02006 vertical_dimension.height += extra.height;
02007 }
02008 return vertical_dimension;
02009 }
02010
02011 Dimension NWidgetScrollbar::GetHorizontalDimension()
02012 {
02013 static const Dimension extra = {WD_SCROLLBAR_LEFT + WD_SCROLLBAR_RIGHT, WD_SCROLLBAR_TOP + WD_SCROLLBAR_BOTTOM};
02014 if (horizontal_dimension.width == 0) {
02015 horizontal_dimension = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
02016 horizontal_dimension.width += extra.width;
02017 horizontal_dimension.height += extra.height;
02018 }
02019 return horizontal_dimension;
02020 }
02021
02022 Dimension NWidgetScrollbar::vertical_dimension = {0, 0};
02023 Dimension NWidgetScrollbar::horizontal_dimension = {0, 0};
02024
02026 void NWidgetLeaf::InvalidateDimensionCache()
02027 {
02028 shadebox_dimension.width = shadebox_dimension.height = 0;
02029 debugbox_dimension.width = debugbox_dimension.height = 0;
02030 stickybox_dimension.width = stickybox_dimension.height = 0;
02031 resizebox_dimension.width = resizebox_dimension.height = 0;
02032 closebox_dimension.width = closebox_dimension.height = 0;
02033 }
02034
02035 Dimension NWidgetLeaf::shadebox_dimension = {0, 0};
02036 Dimension NWidgetLeaf::debugbox_dimension = {0, 0};
02037 Dimension NWidgetLeaf::stickybox_dimension = {0, 0};
02038 Dimension NWidgetLeaf::resizebox_dimension = {0, 0};
02039 Dimension NWidgetLeaf::closebox_dimension = {0, 0};
02040
02049 NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint16 data, StringID tip) : NWidgetCore(tp, colour, 1, 1, data, tip)
02050 {
02051 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);
02052 if (index >= 0) this->SetIndex(index);
02053 this->SetMinimalSize(0, 0);
02054 this->SetResize(0, 0);
02055
02056 switch (tp) {
02057 case WWT_EMPTY:
02058 break;
02059
02060 case WWT_PUSHBTN:
02061 case WWT_IMGBTN:
02062 case WWT_PUSHIMGBTN:
02063 case WWT_IMGBTN_2:
02064 case WWT_TEXTBTN:
02065 case WWT_PUSHTXTBTN:
02066 case WWT_TEXTBTN_2:
02067 case WWT_LABEL:
02068 case WWT_TEXT:
02069 case WWT_MATRIX:
02070 case NWID_BUTTON_DROPDOWN:
02071 case NWID_PUSHBUTTON_DROPDOWN:
02072 case WWT_ARROWBTN:
02073 case WWT_PUSHARROWBTN:
02074 this->SetFill(0, 0);
02075 break;
02076
02077 case WWT_EDITBOX:
02078 this->SetMinimalSize(10, 0);
02079 this->SetFill(0, 0);
02080 break;
02081
02082 case WWT_CAPTION:
02083 this->SetFill(1, 0);
02084 this->SetResize(1, 0);
02085 this->min_y = WD_CAPTION_HEIGHT;
02086 this->SetDataTip(data, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
02087 break;
02088
02089 case WWT_STICKYBOX:
02090 this->SetFill(0, 0);
02091 this->SetMinimalSize(WD_STICKYBOX_WIDTH, WD_CAPTION_HEIGHT);
02092 this->SetDataTip(STR_NULL, STR_TOOLTIP_STICKY);
02093 break;
02094
02095 case WWT_SHADEBOX:
02096 this->SetFill(0, 0);
02097 this->SetMinimalSize(WD_SHADEBOX_TOP, WD_CAPTION_HEIGHT);
02098 this->SetDataTip(STR_NULL, STR_TOOLTIP_SHADE);
02099 break;
02100
02101 case WWT_DEBUGBOX:
02102 this->SetFill(0, 0);
02103 this->SetMinimalSize(WD_DEBUGBOX_TOP, WD_CAPTION_HEIGHT);
02104 this->SetDataTip(STR_NULL, STR_TOOLTIP_DEBUG);
02105 break;
02106
02107 case WWT_RESIZEBOX:
02108 this->SetFill(0, 0);
02109 this->SetMinimalSize(WD_RESIZEBOX_WIDTH, 12);
02110 this->SetDataTip(STR_NULL, STR_TOOLTIP_RESIZE);
02111 break;
02112
02113 case WWT_CLOSEBOX:
02114 this->SetFill(0, 0);
02115 this->SetMinimalSize(WD_CLOSEBOX_WIDTH, WD_CAPTION_HEIGHT);
02116 this->SetDataTip(STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW);
02117 break;
02118
02119 case WWT_DROPDOWN:
02120 this->SetFill(0, 0);
02121 this->min_y = WD_DROPDOWN_HEIGHT;
02122 break;
02123
02124 default:
02125 NOT_REACHED();
02126 }
02127 }
02128
02129 void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
02130 {
02131 if (this->index >= 0 && init_array) {
02132 assert(w->nested_array_size > (uint)this->index);
02133 w->nested_array[this->index] = this;
02134 }
02135
02136 Dimension size = {this->min_x, this->min_y};
02137 Dimension fill = {this->fill_x, this->fill_y};
02138 Dimension resize = {this->resize_x, this->resize_y};
02139
02140 const Dimension *padding = NULL;
02141 switch (this->type) {
02142 case WWT_EMPTY: {
02143 static const Dimension extra = {0, 0};
02144 padding = &extra;
02145 break;
02146 }
02147 case WWT_MATRIX: {
02148 static const Dimension extra = {WD_MATRIX_LEFT + WD_MATRIX_RIGHT, WD_MATRIX_TOP + WD_MATRIX_BOTTOM};
02149 padding = &extra;
02150 break;
02151 }
02152 case WWT_SHADEBOX: {
02153 static const Dimension extra = {WD_SHADEBOX_LEFT + WD_SHADEBOX_RIGHT, WD_SHADEBOX_TOP + WD_SHADEBOX_BOTTOM};
02154 padding = &extra;
02155 if (NWidgetLeaf::shadebox_dimension.width == 0) {
02156 NWidgetLeaf::shadebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_SHADE), GetSpriteSize(SPR_WINDOW_UNSHADE));
02157 NWidgetLeaf::shadebox_dimension.width += extra.width;
02158 NWidgetLeaf::shadebox_dimension.height += extra.height;
02159 }
02160 size = maxdim(size, NWidgetLeaf::shadebox_dimension);
02161 break;
02162 }
02163 case WWT_DEBUGBOX:
02164 if (_settings_client.gui.newgrf_developer_tools && w->IsNewGRFInspectable()) {
02165 static const Dimension extra = {WD_DEBUGBOX_LEFT + WD_DEBUGBOX_RIGHT, WD_DEBUGBOX_TOP + WD_DEBUGBOX_BOTTOM};
02166 padding = &extra;
02167 if (NWidgetLeaf::debugbox_dimension.width == 0) {
02168 NWidgetLeaf::debugbox_dimension = GetSpriteSize(SPR_WINDOW_DEBUG);
02169 NWidgetLeaf::debugbox_dimension.width += extra.width;
02170 NWidgetLeaf::debugbox_dimension.height += extra.height;
02171 }
02172 size = maxdim(size, NWidgetLeaf::debugbox_dimension);
02173 } else {
02174
02175 size.width = 0;
02176 fill.width = 0;
02177 resize.width = 0;
02178 }
02179 break;
02180
02181 case WWT_STICKYBOX: {
02182 static const Dimension extra = {WD_STICKYBOX_LEFT + WD_STICKYBOX_RIGHT, WD_STICKYBOX_TOP + WD_STICKYBOX_BOTTOM};
02183 padding = &extra;
02184 if (NWidgetLeaf::stickybox_dimension.width == 0) {
02185 NWidgetLeaf::stickybox_dimension = maxdim(GetSpriteSize(SPR_PIN_UP), GetSpriteSize(SPR_PIN_DOWN));
02186 NWidgetLeaf::stickybox_dimension.width += extra.width;
02187 NWidgetLeaf::stickybox_dimension.height += extra.height;
02188 }
02189 size = maxdim(size, NWidgetLeaf::stickybox_dimension);
02190 break;
02191 }
02192 case WWT_RESIZEBOX: {
02193 static const Dimension extra = {WD_RESIZEBOX_LEFT + WD_RESIZEBOX_RIGHT, WD_RESIZEBOX_TOP + WD_RESIZEBOX_BOTTOM};
02194 padding = &extra;
02195 if (NWidgetLeaf::resizebox_dimension.width == 0) {
02196 NWidgetLeaf::resizebox_dimension = maxdim(GetSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
02197 NWidgetLeaf::resizebox_dimension.width += extra.width;
02198 NWidgetLeaf::resizebox_dimension.height += extra.height;
02199 }
02200 size = maxdim(size, NWidgetLeaf::resizebox_dimension);
02201 break;
02202 }
02203 case WWT_EDITBOX:
02204 size.height = max(size.height, GetStringBoundingBox("_").height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
02205
02206 case WWT_PUSHBTN: {
02207 static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
02208 padding = &extra;
02209 break;
02210 }
02211 case WWT_IMGBTN:
02212 case WWT_IMGBTN_2:
02213 case WWT_PUSHIMGBTN: {
02214 static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
02215 padding = &extra;
02216 Dimension d2 = GetSpriteSize(this->widget_data);
02217 if (this->type == WWT_IMGBTN_2) d2 = maxdim(d2, GetSpriteSize(this->widget_data + 1));
02218 d2.width += extra.width;
02219 d2.height += extra.height;
02220 size = maxdim(size, d2);
02221 break;
02222 }
02223 case WWT_ARROWBTN:
02224 case WWT_PUSHARROWBTN: {
02225 static const Dimension extra = {WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM};
02226 padding = &extra;
02227 Dimension d2 = maxdim(GetSpriteSize(SPR_ARROW_LEFT), GetSpriteSize(SPR_ARROW_RIGHT));
02228 d2.width += extra.width;
02229 d2.height += extra.height;
02230 size = maxdim(size, d2);
02231 break;
02232 }
02233
02234 case WWT_CLOSEBOX: {
02235 static const Dimension extra = {WD_CLOSEBOX_LEFT + WD_CLOSEBOX_RIGHT, WD_CLOSEBOX_TOP + WD_CLOSEBOX_BOTTOM};
02236 padding = &extra;
02237 if (NWidgetLeaf::closebox_dimension.width == 0) {
02238 NWidgetLeaf::closebox_dimension = maxdim(GetStringBoundingBox(STR_BLACK_CROSS), GetStringBoundingBox(STR_SILVER_CROSS));
02239 NWidgetLeaf::closebox_dimension.width += extra.width;
02240 NWidgetLeaf::closebox_dimension.height += extra.height;
02241 }
02242 size = maxdim(size, NWidgetLeaf::closebox_dimension);
02243 break;
02244 }
02245 case WWT_TEXTBTN:
02246 case WWT_PUSHTXTBTN:
02247 case WWT_TEXTBTN_2: {
02248 static const Dimension extra = {WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM};
02249 padding = &extra;
02250 if (this->index >= 0) w->SetStringParameters(this->index);
02251 Dimension d2 = GetStringBoundingBox(this->widget_data);
02252 d2.width += extra.width;
02253 d2.height += extra.height;
02254 size = maxdim(size, d2);
02255 break;
02256 }
02257 case WWT_LABEL:
02258 case WWT_TEXT: {
02259 static const Dimension extra = {0, 0};
02260 padding = &extra;
02261 if (this->index >= 0) w->SetStringParameters(this->index);
02262 size = maxdim(size, GetStringBoundingBox(this->widget_data));
02263 break;
02264 }
02265 case WWT_CAPTION: {
02266 static const Dimension extra = {WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT, WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM};
02267 padding = &extra;
02268 if (this->index >= 0) w->SetStringParameters(this->index);
02269 Dimension d2 = GetStringBoundingBox(this->widget_data);
02270 d2.width += extra.width;
02271 d2.height += extra.height;
02272 size = maxdim(size, d2);
02273 break;
02274 }
02275 case WWT_DROPDOWN:
02276 case NWID_BUTTON_DROPDOWN:
02277 case NWID_PUSHBUTTON_DROPDOWN: {
02278 static const Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
02279 padding = &extra;
02280 if (this->index >= 0) w->SetStringParameters(this->index);
02281 Dimension d2 = GetStringBoundingBox(this->widget_data);
02282 d2.width += extra.width;
02283 d2.height += extra.height;
02284 size = maxdim(size, d2);
02285 break;
02286 }
02287 default:
02288 NOT_REACHED();
02289 }
02290
02291 if (this->index >= 0) w->UpdateWidgetSize(this->index, &size, *padding, &fill, &resize);
02292
02293 this->smallest_x = size.width;
02294 this->smallest_y = size.height;
02295 this->fill_x = fill.width;
02296 this->fill_y = fill.height;
02297 this->resize_x = resize.width;
02298 this->resize_y = resize.height;
02299 }
02300
02301 void NWidgetLeaf::Draw(const Window *w)
02302 {
02303 if (this->current_x == 0 || this->current_y == 0) return;
02304
02305 Rect r;
02306 r.left = this->pos_x;
02307 r.right = this->pos_x + this->current_x - 1;
02308 r.top = this->pos_y;
02309 r.bottom = this->pos_y + this->current_y - 1;
02310
02311 const DrawPixelInfo *dpi = _cur_dpi;
02312 if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
02313
02314 bool clicked = this->IsLowered();
02315 switch (this->type) {
02316 case WWT_EMPTY:
02317 break;
02318
02319 case WWT_PUSHBTN:
02320 assert(this->widget_data == 0);
02321 DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
02322 break;
02323
02324 case WWT_IMGBTN:
02325 case WWT_PUSHIMGBTN:
02326 case WWT_IMGBTN_2:
02327 DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
02328 break;
02329
02330 case WWT_TEXTBTN:
02331 case WWT_PUSHTXTBTN:
02332 case WWT_TEXTBTN_2:
02333 if (this->index >= 0) w->SetStringParameters(this->index);
02334 DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
02335 DrawLabel(r, this->type, clicked, this->widget_data);
02336 break;
02337
02338 case WWT_ARROWBTN:
02339 case WWT_PUSHARROWBTN: {
02340 SpriteID sprite;
02341 switch (this->widget_data) {
02342 case AWV_DECREASE: sprite = _current_text_dir != TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
02343 case AWV_INCREASE: sprite = _current_text_dir == TD_RTL ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT; break;
02344 case AWV_LEFT: sprite = SPR_ARROW_LEFT; break;
02345 case AWV_RIGHT: sprite = SPR_ARROW_RIGHT; break;
02346 default: NOT_REACHED();
02347 }
02348 DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
02349 }
02350
02351 case WWT_LABEL:
02352 if (this->index >= 0) w->SetStringParameters(this->index);
02353 DrawLabel(r, this->type, clicked, this->widget_data);
02354 break;
02355
02356 case WWT_TEXT:
02357 if (this->index >= 0) w->SetStringParameters(this->index);
02358 DrawText(r, (TextColour)this->colour, this->widget_data);
02359 break;
02360
02361 case WWT_MATRIX:
02362 DrawMatrix(r, this->colour, clicked, this->widget_data);
02363 break;
02364
02365 case WWT_EDITBOX:
02366 DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, FR_LOWERED | FR_DARKENED);
02367 break;
02368
02369 case WWT_CAPTION:
02370 if (this->index >= 0) w->SetStringParameters(this->index);
02371 DrawCaption(r, this->colour, w->owner, this->widget_data);
02372 break;
02373
02374 case WWT_SHADEBOX:
02375 assert(this->widget_data == 0);
02376 DrawShadeBox(r, this->colour, w->IsShaded());
02377 break;
02378
02379 case WWT_DEBUGBOX:
02380 DrawDebugBox(r, this->colour, clicked);
02381 break;
02382
02383 case WWT_STICKYBOX:
02384 assert(this->widget_data == 0);
02385 DrawStickyBox(r, this->colour, !!(w->flags & WF_STICKY));
02386 break;
02387
02388 case WWT_RESIZEBOX:
02389 assert(this->widget_data == 0);
02390 DrawResizeBox(r, this->colour, this->pos_x < (uint)(w->width / 2), !!(w->flags & WF_SIZING));
02391 break;
02392
02393 case WWT_CLOSEBOX:
02394 DrawCloseBox(r, this->colour, this->widget_data);
02395 break;
02396
02397 case WWT_DROPDOWN:
02398 if (this->index >= 0) w->SetStringParameters(this->index);
02399 DrawDropdown(r, this->colour, clicked, this->widget_data);
02400 break;
02401
02402 case NWID_BUTTON_DROPDOWN:
02403 case NWID_PUSHBUTTON_DROPDOWN:
02404 if (this->index >= 0) w->SetStringParameters(this->index);
02405 DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
02406 break;
02407
02408 default:
02409 NOT_REACHED();
02410 }
02411 if (this->index >= 0) w->DrawWidget(r, this->index);
02412
02413 if (this->IsDisabled()) {
02414 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER);
02415 }
02416 }
02417
02425 bool NWidgetLeaf::ButtonHit(const Point &pt)
02426 {
02427 if (_current_text_dir == TD_LTR) {
02428 int button_width = this->pos_x + this->current_x - 12;
02429 return pt.x < button_width;
02430 } else {
02431 int button_left = this->pos_x + 12;
02432 return pt.x >= button_left;
02433 }
02434 }
02435
02436
02437
02453 static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
02454 {
02455 int num_used = 0;
02456
02457 *dest = NULL;
02458 *fill_dest = false;
02459
02460 while (count > num_used) {
02461 switch (parts->type) {
02462 case NWID_SPACER:
02463 if (*dest != NULL) return num_used;
02464 *dest = new NWidgetSpacer(0, 0);
02465 break;
02466
02467 case NWID_HORIZONTAL:
02468 if (*dest != NULL) return num_used;
02469 *dest = new NWidgetHorizontal(parts->u.cont_flags);
02470 *fill_dest = true;
02471 break;
02472
02473 case NWID_HORIZONTAL_LTR:
02474 if (*dest != NULL) return num_used;
02475 *dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
02476 *fill_dest = true;
02477 break;
02478
02479 case WWT_PANEL:
02480 case WWT_INSET:
02481 case WWT_FRAME:
02482 if (*dest != NULL) return num_used;
02483 *dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
02484 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
02485 *fill_dest = true;
02486 break;
02487
02488 case NWID_VERTICAL:
02489 if (*dest != NULL) return num_used;
02490 *dest = new NWidgetVertical(parts->u.cont_flags);
02491 *fill_dest = true;
02492 break;
02493
02494 case NWID_MATRIX: {
02495 if (*dest != NULL) return num_used;
02496 NWidgetMatrix *nwm = new NWidgetMatrix();
02497 *dest = nwm;
02498 *fill_dest = true;
02499 nwm->SetIndex(parts->u.widget.index);
02500 nwm->SetColour(parts->u.widget.colour);
02501 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
02502 break;
02503 }
02504
02505 case WPT_FUNCTION: {
02506 if (*dest != NULL) return num_used;
02507
02508 int biggest = -1;
02509 *dest = parts->u.func_ptr(&biggest);
02510 *biggest_index = max(*biggest_index, biggest);
02511 *fill_dest = false;
02512 break;
02513 }
02514
02515 case WPT_RESIZE: {
02516 NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
02517 if (nwrb != NULL) {
02518 assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
02519 nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
02520 }
02521 break;
02522 }
02523
02524 case WPT_MINSIZE: {
02525 NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
02526 if (nwrb != NULL) {
02527 assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
02528 nwrb->SetMinimalSize(parts->u.xy.x, parts->u.xy.y);
02529 }
02530 break;
02531 }
02532
02533 case WPT_MINTEXTLINES: {
02534 NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
02535 if (nwrb != NULL) {
02536 assert(parts->u.text_lines.size >= FS_BEGIN && parts->u.text_lines.size < FS_END);
02537 nwrb->SetMinimalTextLines(parts->u.text_lines.lines, parts->u.text_lines.spacing, parts->u.text_lines.size);
02538 }
02539 break;
02540 }
02541
02542 case WPT_FILL: {
02543 NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
02544 if (nwrb != NULL) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
02545 break;
02546 }
02547
02548 case WPT_DATATIP: {
02549 NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
02550 if (nwc != NULL) {
02551 nwc->widget_data = parts->u.data_tip.data;
02552 nwc->tool_tip = parts->u.data_tip.tooltip;
02553 }
02554 break;
02555 }
02556
02557 case WPT_PADDING:
02558 if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
02559 break;
02560
02561 case WPT_PIPSPACE: {
02562 NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
02563 if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre, parts->u.pip.inter, parts->u.pip.post);
02564
02565 NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
02566 if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre, parts->u.pip.inter, parts->u.pip.post);
02567 break;
02568 }
02569
02570 case WPT_SCROLLBAR: {
02571 NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
02572 if (nwc != NULL) {
02573 nwc->scrollbar_index = parts->u.widget.index;
02574 }
02575 break;
02576 }
02577
02578 case WPT_ENDCONTAINER:
02579 return num_used;
02580
02581 case NWID_VIEWPORT:
02582 if (*dest != NULL) return num_used;
02583 *dest = new NWidgetViewport(parts->u.widget.index);
02584 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
02585 break;
02586
02587 case NWID_HSCROLLBAR:
02588 case NWID_VSCROLLBAR:
02589 if (*dest != NULL) return num_used;
02590 *dest = new NWidgetScrollbar(parts->type, parts->u.widget.colour, parts->u.widget.index);
02591 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
02592 break;
02593
02594 case NWID_SELECTION: {
02595 if (*dest != NULL) return num_used;
02596 NWidgetStacked *nws = new NWidgetStacked();
02597 *dest = nws;
02598 *fill_dest = true;
02599 nws->SetIndex(parts->u.widget.index);
02600 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
02601 break;
02602 }
02603
02604 default:
02605 if (*dest != NULL) return num_used;
02606 assert((parts->type & WWT_MASK) < WWT_LAST || (parts->type & WWT_MASK) == NWID_BUTTON_DROPDOWN);
02607 *dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
02608 *biggest_index = max(*biggest_index, (int)parts->u.widget.index);
02609 break;
02610 }
02611 num_used++;
02612 parts++;
02613 }
02614
02615 return num_used;
02616 }
02617
02627 static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase **parent, int *biggest_index)
02628 {
02629
02630
02631 NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(*parent);
02632 NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
02633 assert(*parent == NULL || (nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
02634
02635 int total_used = 0;
02636 for (;;) {
02637 NWidgetBase *sub_widget = NULL;
02638 bool fill_sub = false;
02639 int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
02640 parts += num_used;
02641 total_used += num_used;
02642
02643
02644 if (sub_widget == NULL) break;
02645
02646
02647 WidgetType tp = sub_widget->type;
02648 if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
02649 || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
02650 NWidgetBase *sub_ptr = sub_widget;
02651 int num_used = MakeWidgetTree(parts, count - total_used, &sub_ptr, biggest_index);
02652 parts += num_used;
02653 total_used += num_used;
02654 }
02655
02656
02657 if (nwid_cont != NULL) nwid_cont->Add(sub_widget);
02658 if (nwid_parent != NULL) nwid_parent->Add(sub_widget);
02659 if (nwid_cont == NULL && nwid_parent == NULL) {
02660 *parent = sub_widget;
02661 return total_used;
02662 }
02663 }
02664
02665 if (count == total_used) return total_used;
02666
02667 assert(total_used < count);
02668 assert(parts->type == WPT_ENDCONTAINER);
02669 return total_used + 1;
02670 }
02671
02683 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
02684 {
02685 *biggest_index = -1;
02686 if (container == NULL) container = new NWidgetVertical();
02687 NWidgetBase *cont_ptr = container;
02688 MakeWidgetTree(parts, count, &cont_ptr, biggest_index);
02689 return container;
02690 }
02691
02705 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
02706 {
02707 *biggest_index = -1;
02708
02709
02710 NWidgetBase *nwid = NULL;
02711 int num_used = MakeWidgetTree(parts, count, &nwid, biggest_index);
02712 assert(nwid != NULL);
02713 parts += num_used;
02714 count -= num_used;
02715
02716 NWidgetContainer *root = new NWidgetVertical;
02717 root->Add(nwid);
02718 if (count == 0) {
02719 *shade_select = NULL;
02720 return root;
02721 }
02722
02723
02724
02725 NWidgetHorizontal *hor_cont = dynamic_cast<NWidgetHorizontal *>(nwid);
02726 NWidgetContainer *body;
02727 if (hor_cont != NULL && hor_cont->GetWidgetOfType(WWT_CAPTION) != NULL && hor_cont->GetWidgetOfType(WWT_SHADEBOX) != NULL) {
02728 *shade_select = new NWidgetStacked;
02729 root->Add(*shade_select);
02730 body = new NWidgetVertical;
02731 (*shade_select)->Add(body);
02732 } else {
02733 *shade_select = NULL;
02734 body = root;
02735 }
02736
02737
02738 int biggest2 = -1;
02739 MakeNWidgets(parts, count, &biggest2, body);
02740
02741 *biggest_index = max(*biggest_index, biggest2);
02742 return root;
02743 }
02744
02755 NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
02756 {
02757 NWidgetVertical *vert = NULL;
02758 NWidgetHorizontal *hor = NULL;
02759 int hor_length = 0;
02760
02761 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
02762 sprite_size.width += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
02763 sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1;
02764
02765 for (int widnum = widget_first; widnum <= widget_last; widnum++) {
02766
02767 if (hor_length == max_length) {
02768 if (vert == NULL) vert = new NWidgetVertical();
02769 vert->Add(hor);
02770 hor = NULL;
02771 hor_length = 0;
02772 }
02773 if (hor == NULL) {
02774 hor = new NWidgetHorizontal();
02775 hor_length = 0;
02776 }
02777
02778 NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
02779 panel->SetMinimalSize(sprite_size.width, sprite_size.height);
02780 panel->SetFill(1, 0);
02781 panel->SetResize(1, 0);
02782 panel->SetDataTip(0x0, button_tooltip);
02783 hor->Add(panel);
02784 hor_length++;
02785 }
02786 *biggest_index = widget_last;
02787 if (vert == NULL) return hor;
02788
02789 if (hor_length > 0 && hor_length < max_length) {
02790
02791 NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
02792 spc->SetFill(1, 0);
02793 spc->SetResize(1, 0);
02794 hor->Add(spc);
02795 }
02796 if (hor != NULL) vert->Add(hor);
02797 return vert;
02798 }