tron@2186: /* $Id$ */ tron@2186: rubidium@10455: /** @file widget.cpp Handling of the default/simple widgets. */ glx@9574: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" rubidium@9724: #include "core/math_func.hpp" rubidium@9724: #include "player_func.h" rubidium@9723: #include "gfx_func.h" rubidium@9723: #include "window_gui.h" rubidium@9723: #include "window_func.h" rubidium@9724: #include "widgets/dropdown_func.h" rubidium@9723: rubidium@9724: #include "table/sprites.h" rubidium@9724: #include "table/strings.h" rubidium@9724: rubidium@9724: static const char *UPARROW = "\xEE\x8A\xA0"; rubidium@9724: static const char *DOWNARROW = "\xEE\x8A\xAA"; truelight@0: Darkvater@2436: static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom) truelight@0: { truelight@0: Point pt; truelight@0: int height, count, pos, cap; truelight@0: truelight@0: top += 10; truelight@0: bottom -= 9; truelight@193: truelight@0: height = (bottom - top); truelight@0: truelight@0: pos = sb->pos; truelight@0: count = sb->count; truelight@0: cap = sb->cap; truelight@0: tron@2026: if (count != 0) top += height * pos / count; truelight@193: truelight@0: if (cap > count) cap = count; tron@2639: if (count != 0) bottom -= (count - pos - cap) * height / count; truelight@0: truelight@0: pt.x = top; truelight@0: pt.y = bottom - 1; truelight@0: return pt; truelight@0: } truelight@0: rubidium@9601: /** Special handling for the scrollbar widget type. truelight@0: * Handles the special scrolling buttons and other truelight@0: * scrolling. rubidium@9601: * @param w Window on which a scroll was performed. rubidium@9601: * @param wi Pointer to the scrollbar widget. rubidium@9601: * @param x The X coordinate of the mouse click. rubidium@9601: * @param y The Y coordinate of the mouse click. */ truelight@0: void ScrollbarClickHandler(Window *w, const Widget *wi, int x, int y) truelight@0: { truelight@0: int mi, ma, pos; truelight@0: Scrollbar *sb; truelight@0: bjarni@842: switch (wi->type) { rubidium@9826: case WWT_SCROLLBAR: glx@9574: /* vertical scroller */ bjarni@842: w->flags4 &= ~WF_HSCROLL; bjarni@842: w->flags4 &= ~WF_SCROLL2; bjarni@842: mi = wi->top; bjarni@842: ma = wi->bottom; bjarni@842: pos = y; bjarni@842: sb = &w->vscroll; bjarni@842: break; rubidium@9826: rubidium@9826: case WWT_SCROLL2BAR: glx@9574: /* 2nd vertical scroller */ bjarni@842: w->flags4 &= ~WF_HSCROLL; bjarni@842: w->flags4 |= WF_SCROLL2; bjarni@842: mi = wi->top; bjarni@842: ma = wi->bottom; bjarni@842: pos = y; bjarni@842: sb = &w->vscroll2; bjarni@842: break; rubidium@9826: rubidium@9826: case WWT_HSCROLLBAR: glx@9574: /* horizontal scroller */ truelight@867: w->flags4 &= ~WF_SCROLL2; bjarni@842: w->flags4 |= WF_HSCROLL; bjarni@842: mi = wi->left; bjarni@842: ma = wi->right; bjarni@842: pos = x; bjarni@842: sb = &w->hscroll; truelight@867: break; rubidium@9826: rubidium@9826: default: NOT_REACHED(); truelight@0: } rubidium@10249: if (pos <= mi + 9) { glx@9574: /* Pressing the upper button? */ tron@2597: w->flags4 |= WF_SCROLL_UP; tron@2597: if (_scroller_click_timeout == 0) { tron@2597: _scroller_click_timeout = 6; tron@2597: if (sb->pos != 0) sb->pos--; truelight@0: } tron@2597: _left_button_clicked = false; rubidium@9826: } else if (pos >= ma - 10) { glx@9574: /* Pressing the lower button? */ tron@2597: w->flags4 |= WF_SCROLL_DOWN; truelight@193: tron@2597: if (_scroller_click_timeout == 0) { tron@2597: _scroller_click_timeout = 6; tron@2597: if ((byte)(sb->pos + sb->cap) < sb->count) tron@2597: sb->pos++; truelight@0: } tron@2597: _left_button_clicked = false; truelight@0: } else { truelight@0: Point pt = HandleScrollbarHittest(sb, mi, ma); truelight@0: truelight@0: if (pos < pt.x) { truelight@0: sb->pos = max(sb->pos - sb->cap, 0); truelight@0: } else if (pos > pt.y) { truelight@0: sb->pos = min( truelight@193: sb->pos + sb->cap, truelight@0: max(sb->count - sb->cap, 0) truelight@0: ); truelight@0: } else { truelight@0: _scrollbar_start_pos = pt.x - mi - 9; truelight@0: _scrollbar_size = ma - mi - 23; truelight@0: w->flags4 |= WF_SCROLL_MIDDLE; truelight@0: _scrolling_scrollbar = true; truelight@0: _cursorpos_drag_start = _cursor.pos; truelight@0: } truelight@0: } truelight@0: rubidium@10455: w->SetDirty(); truelight@0: } truelight@0: Darkvater@2021: /** Returns the index for the widget located at the given position Darkvater@2021: * relative to the window. It includes all widget-corner pixels as well. Darkvater@2021: * @param *w Window to look inside rubidium@9601: * @param x The Window client X coordinate rubidium@9601: * @param y The Window client y coordinate Darkvater@2021: * @return A widget index, or -1 if no widget was found. truelight@0: */ Darkvater@2436: int GetWidgetFromPos(const Window *w, int x, int y) truelight@0: { rubidium@5236: uint index; rubidium@5236: int found_index = -1; truelight@0: glx@9574: /* Go through the widgets and check if we find the widget that the coordinate is glx@9574: * inside. */ rubidium@5236: for (index = 0; index < w->widget_count; index++) { rubidium@5236: const Widget *wi = &w->widget[index]; tron@2639: if (wi->type == WWT_EMPTY || wi->type == WWT_FRAME) continue; truelight@0: Darkvater@2021: if (x >= wi->left && x <= wi->right && y >= wi->top && y <= wi->bottom && rubidium@9723: !w->IsWidgetHidden(index)) { tron@2639: found_index = index; truelight@0: } truelight@0: } truelight@0: truelight@0: return found_index; truelight@0: } truelight@0: truelight@0: tron@4437: void DrawFrameRect(int left, int top, int right, int bottom, int ctab, FrameFlags flags) tron@4437: { tron@4444: uint dark = _colour_gradient[ctab][3]; tron@4444: uint medium_dark = _colour_gradient[ctab][5]; tron@4444: uint medium_light = _colour_gradient[ctab][6]; tron@4444: uint light = _colour_gradient[ctab][7]; tron@4437: tron@4438: if (flags & FR_TRANSPARENT) { peter1138@5919: GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT | (1 << USE_COLORTABLE)); tron@4437: } else { tron@4437: uint interior; tron@4437: tron@4437: if (flags & FR_LOWERED) { tron@4437: GfxFillRect(left, top, left, bottom, dark); tron@4437: GfxFillRect(left + 1, top, right, top, dark); tron@4437: GfxFillRect(right, top + 1, right, bottom - 1, light); tron@4437: GfxFillRect(left + 1, bottom, right, bottom, light); tron@4437: interior = (flags & FR_DARKENED ? medium_dark : medium_light); tron@4437: } else { tron@4437: GfxFillRect(left, top, left, bottom - 1, light); tron@4437: GfxFillRect(left + 1, top, right - 1, top, light); tron@4437: GfxFillRect(right, top, right, bottom - 1, dark); tron@4437: GfxFillRect(left, bottom, right, bottom, dark); tron@4437: interior = medium_dark; tron@4437: } tron@4437: if (!(flags & FR_BORDERONLY)) { tron@4437: GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, interior); tron@4437: } tron@4437: } tron@4437: } tron@4437: tron@4437: rubidium@10142: /** rubidium@10142: * Paint all widgets of a window. rubidium@10142: * @param w Window rubidium@10142: */ glx@10645: void Window::DrawWidgets() const truelight@0: { tron@2548: const DrawPixelInfo* dpi = _cur_dpi; truelight@193: glx@10645: for (uint i = 0; i < this->widget_count; i++) { glx@10645: const Widget *wi = &this->widget[i]; glx@10645: bool clicked = this->IsWidgetLowered(i); rubidium@9723: Rect r; Darkvater@1657: rubidium@9723: if (dpi->left > (r.right = wi->right) || rubidium@9723: dpi->left + dpi->width <= (r.left = wi->left) || rubidium@9723: dpi->top > (r.bottom = wi->bottom) || rubidium@9723: dpi->top + dpi->height <= (r.top = wi->top) || glx@10645: this->IsWidgetHidden(i)) { tron@2639: continue; tron@2639: } truelight@0: Darkvater@1657: switch (wi->type & WWT_MASK) { Darkvater@4938: case WWT_IMGBTN: Darkvater@4938: case WWT_IMGBTN_2: { rubidium@9723: SpriteID img = wi->data; Darkvater@4938: assert(img != 0); rubidium@5838: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); truelight@0: Darkvater@4938: /* show different image when clicked for WWT_IMGBTN_2 */ Darkvater@4938: if ((wi->type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; peter1138@5919: DrawSprite(img, PAL_NONE, r.left + 1 + clicked, r.top + 1 + clicked); rubidium@9724: break; Darkvater@4938: } Darkvater@1657: rubidium@9826: case WWT_PANEL: Darkvater@4938: assert(wi->data == 0); rubidium@5838: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); rubidium@9724: break; rubidium@9826: rubidium@9826: case WWT_EDITBOX: rubidium@9826: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_LOWERED | FR_DARKENED); rubidium@9826: break; truelight@0: Darkvater@4939: case WWT_TEXTBTN: rubidium@9826: case WWT_TEXTBTN_2: rubidium@5838: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); rubidium@9826: /* FALL THROUGH */ truelight@0: belugas@4345: case WWT_LABEL: { Darkvater@4547: StringID str = wi->data; truelight@0: Darkvater@4939: if ((wi->type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++; truelight@193: rubidium@9722: DrawStringCentered(((r.left + r.right + 1) >> 1) + clicked, ((r.top + r.bottom + 1) >> 1) - 5 + clicked, str, TC_FROMSTRING); rubidium@9724: break; truelight@0: } truelight@0: belugas@6604: case WWT_TEXT: { rubidium@9723: const StringID str = wi->data; belugas@6604: belugas@6604: if (str != STR_NULL) DrawStringTruncated(r.left, r.top, str, wi->color, r.right - r.left); belugas@6604: break; belugas@6604: } belugas@6604: Darkvater@4939: case WWT_INSET: { rubidium@9723: const StringID str = wi->data; hackykid@1938: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_LOWERED | FR_DARKENED); truelight@0: rubidium@9722: if (str != STR_NULL) DrawStringTruncated(r.left + 2, r.top + 1, str, TC_FROMSTRING, r.right - r.left - 10); rubidium@9724: break; truelight@0: } truelight@0: truelight@0: case WWT_MATRIX: { rubidium@5838: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); truelight@193: rubidium@9826: int c = GB(wi->data, 0, 8); rubidium@9826: int amt1 = (wi->right - wi->left + 1) / c; truelight@0: rubidium@9826: int d = GB(wi->data, 8, 8); rubidium@9826: int amt2 = (wi->bottom - wi->top + 1) / d; truelight@0: rubidium@9826: int color = _colour_gradient[wi->color & 0xF][6]; truelight@193: rubidium@9826: int x = r.left; rubidium@9826: for (int ctr = c; ctr > 1; ctr--) { truelight@0: x += amt1; tron@2639: GfxFillRect(x, r.top + 1, x, r.bottom - 1, color); truelight@0: } truelight@0: truelight@0: x = r.top; rubidium@9826: for (int ctr = d; ctr > 1; ctr--) { truelight@0: x += amt2; tron@2639: GfxFillRect(r.left + 1, x, r.right - 1, x, color); truelight@0: } truelight@0: rubidium@9723: color = _colour_gradient[wi->color & 0xF][4]; truelight@0: tron@2639: x = r.left - 1; rubidium@9826: for (int ctr = c; ctr > 1; ctr--) { truelight@0: x += amt1; tron@2639: GfxFillRect(x, r.top + 1, x, r.bottom - 1, color); truelight@0: } truelight@0: tron@2639: x = r.top - 1; rubidium@9826: for (int ctr = d; ctr > 1; ctr--) { truelight@0: x += amt2; rubidium@9723: GfxFillRect(r.left + 1, x, r.right - 1, x, color); truelight@0: } truelight@0: rubidium@9724: break; truelight@0: } truelight@0: glx@9574: /* vertical scrollbar */ truelight@0: case WWT_SCROLLBAR: { rubidium@9723: assert(wi->data == 0); rubidium@9826: assert(r.right - r.left == 11); // To ensure the same sizes are used everywhere! darkvater@893: glx@9574: /* draw up/down buttons */ glx@10645: clicked = ((this->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_UP); rubidium@5838: DrawFrameRect(r.left, r.top, r.right, r.top + 9, wi->color, (clicked) ? FR_LOWERED : FR_NONE); rubidium@9722: DoDrawString(UPARROW, r.left + 2 + clicked, r.top + clicked, TC_BLACK); truelight@0: glx@10645: clicked = (((this->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_DOWN)); rubidium@5838: DrawFrameRect(r.left, r.bottom - 9, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); rubidium@9722: DoDrawString(DOWNARROW, r.left + 2 + clicked, r.bottom - 9 + clicked, TC_BLACK); truelight@0: rubidium@9826: int c1 = _colour_gradient[wi->color & 0xF][3]; rubidium@9826: int c2 = _colour_gradient[wi->color & 0xF][7]; truelight@0: glx@9574: /* draw "shaded" background */ rubidium@9723: GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c2); rubidium@9723: GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c1 | (1 << PALETTE_MODIFIER_GREYOUT)); truelight@0: glx@9574: /* draw shaded lines */ rubidium@9723: GfxFillRect(r.left + 2, r.top + 10, r.left + 2, r.bottom - 10, c1); rubidium@9723: GfxFillRect(r.left + 3, r.top + 10, r.left + 3, r.bottom - 10, c2); rubidium@9723: GfxFillRect(r.left + 7, r.top + 10, r.left + 7, r.bottom - 10, c1); rubidium@9723: GfxFillRect(r.left + 8, r.top + 10, r.left + 8, r.bottom - 10, c2); truelight@193: glx@10645: Point pt = HandleScrollbarHittest(&this->vscroll, r.top, r.bottom); glx@10645: DrawFrameRect(r.left, pt.x, r.right, pt.y, wi->color, (this->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_MIDDLE ? FR_LOWERED : FR_NONE); bjarni@842: break; bjarni@842: } rubidium@9826: bjarni@842: case WWT_SCROLL2BAR: { rubidium@9723: assert(wi->data == 0); rubidium@9826: assert(r.right - r.left == 11); // To ensure the same sizes are used everywhere! darkvater@893: glx@9574: /* draw up/down buttons */ glx@10645: clicked = ((this->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_UP | WF_SCROLL2)); rubidium@5838: DrawFrameRect(r.left, r.top, r.right, r.top + 9, wi->color, (clicked) ? FR_LOWERED : FR_NONE); rubidium@9722: DoDrawString(UPARROW, r.left + 2 + clicked, r.top + clicked, TC_BLACK); bjarni@842: glx@10645: clicked = ((this->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_DOWN | WF_SCROLL2)); rubidium@5838: DrawFrameRect(r.left, r.bottom - 9, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); rubidium@9722: DoDrawString(DOWNARROW, r.left + 2 + clicked, r.bottom - 9 + clicked, TC_BLACK); bjarni@842: rubidium@9826: int c1 = _colour_gradient[wi->color & 0xF][3]; rubidium@9826: int c2 = _colour_gradient[wi->color & 0xF][7]; bjarni@842: glx@9574: /* draw "shaded" background */ rubidium@9723: GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c2); rubidium@9723: GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c1 | (1 << PALETTE_MODIFIER_GREYOUT)); bjarni@842: glx@9574: /* draw shaded lines */ rubidium@9723: GfxFillRect(r.left + 2, r.top + 10, r.left + 2, r.bottom - 10, c1); rubidium@9723: GfxFillRect(r.left + 3, r.top + 10, r.left + 3, r.bottom - 10, c2); rubidium@9723: GfxFillRect(r.left + 7, r.top + 10, r.left + 7, r.bottom - 10, c1); rubidium@9723: GfxFillRect(r.left + 8, r.top + 10, r.left + 8, r.bottom - 10, c2); bjarni@842: glx@10645: Point pt = HandleScrollbarHittest(&this->vscroll2, r.top, r.bottom); glx@10645: DrawFrameRect(r.left, pt.x, r.right, pt.y, wi->color, (this->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_MIDDLE | WF_SCROLL2) ? FR_LOWERED : FR_NONE); truelight@0: break; truelight@0: } truelight@0: glx@9574: /* horizontal scrollbar */ truelight@0: case WWT_HSCROLLBAR: { rubidium@9723: assert(wi->data == 0); rubidium@9826: assert(r.bottom - r.top == 11); // To ensure the same sizes are used everywhere! darkvater@893: glx@10645: clicked = ((this->flags4 & (WF_SCROLL_UP | WF_HSCROLL)) == (WF_SCROLL_UP | WF_HSCROLL)); rubidium@5838: DrawFrameRect(r.left, r.top, r.left + 9, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); peter1138@5919: DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + clicked, r.top + 1 + clicked); truelight@0: glx@10645: clicked = ((this->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL)) == (WF_SCROLL_DOWN | WF_HSCROLL)); rubidium@9723: DrawFrameRect(r.right - 9, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); peter1138@5919: DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - 8 + clicked, r.top + 1 + clicked); truelight@0: rubidium@9826: int c1 = _colour_gradient[wi->color & 0xF][3]; rubidium@9826: int c2 = _colour_gradient[wi->color & 0xF][7]; truelight@0: glx@9574: /* draw "shaded" background */ rubidium@9723: GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c2); rubidium@9723: GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c1 | (1 << PALETTE_MODIFIER_GREYOUT)); truelight@0: glx@9574: /* draw shaded lines */ rubidium@9723: GfxFillRect(r.left + 10, r.top + 2, r.right - 10, r.top + 2, c1); rubidium@9723: GfxFillRect(r.left + 10, r.top + 3, r.right - 10, r.top + 3, c2); rubidium@9723: GfxFillRect(r.left + 10, r.top + 7, r.right - 10, r.top + 7, c1); rubidium@9723: GfxFillRect(r.left + 10, r.top + 8, r.right - 10, r.top + 8, c2); truelight@0: glx@9574: /* draw actual scrollbar */ glx@10645: Point pt = HandleScrollbarHittest(&this->hscroll, r.left, r.right); glx@10645: DrawFrameRect(pt.x, r.top, pt.y, r.bottom, wi->color, (this->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL)) == (WF_SCROLL_MIDDLE | WF_HSCROLL) ? FR_LOWERED : FR_NONE); truelight@0: truelight@0: break; truelight@0: } truelight@0: truelight@0: case WWT_FRAME: { rubidium@9723: const StringID str = wi->data; darkvater@860: int x2 = r.left; // by default the left side is the left side of the widget truelight@0: rubidium@9723: if (str != STR_NULL) x2 = DrawString(r.left + 6, r.top, str, TC_FROMSTRING); truelight@0: rubidium@9826: int c1 = _colour_gradient[wi->color][3]; rubidium@9826: int c2 = _colour_gradient[wi->color][7]; truelight@0: rubidium@9723: /* Line from upper left corner to start of text */ rubidium@9723: GfxFillRect(r.left, r.top + 4, r.left + 4, r.top + 4, c1); rubidium@9723: GfxFillRect(r.left + 1, r.top + 5, r.left + 4, r.top + 5, c2); truelight@0: glx@9574: /* Line from end of text to upper right corner */ rubidium@9723: GfxFillRect(x2, r.top + 4, r.right - 1, r.top + 4, c1); rubidium@9723: GfxFillRect(x2, r.top + 5, r.right - 2, r.top + 5, c2); truelight@0: glx@9574: /* Line from upper left corner to bottom left corner */ rubidium@9723: GfxFillRect(r.left, r.top + 5, r.left, r.bottom - 1, c1); rubidium@9723: GfxFillRect(r.left + 1, r.top + 6, r.left + 1, r.bottom - 2, c2); truelight@0: glx@9574: /*Line from upper right corner to bottom right corner */ rubidium@9723: GfxFillRect(r.right - 1, r.top + 5, r.right - 1, r.bottom - 2, c1); rubidium@9723: GfxFillRect(r.right, r.top + 4, r.right, r.bottom - 1, c2); truelight@0: rubidium@9723: GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1); truelight@0: GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2); truelight@0: rubidium@9724: break; truelight@0: } truelight@0: rubidium@9826: case WWT_STICKYBOX: rubidium@9723: assert(wi->data == 0); rubidium@9826: assert(r.right - r.left == 11); // To ensure the same sizes are used everywhere! peter1138@2703: glx@10645: clicked = !!(this->flags4 & WF_STICKY); rubidium@5838: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); peter1138@5919: DrawSprite((clicked) ? SPR_PIN_UP : SPR_PIN_DOWN, PAL_NONE, r.left + 2 + clicked, r.top + 3 + clicked); darkvater@682: break; truelight@867: rubidium@9826: case WWT_RESIZEBOX: rubidium@9723: assert(wi->data == 0); rubidium@9826: assert(r.right - r.left == 11); // To ensure the same sizes are used everywhere! tron@915: glx@10645: clicked = !!(this->flags4 & WF_SIZING); rubidium@5838: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE); peter1138@5919: DrawSprite(SPR_WINDOW_RESIZE, PAL_NONE, r.left + 3 + clicked, r.top + 3 + clicked); truelight@867: break; truelight@867: peter1138@2757: case WWT_CLOSEBOX: { rubidium@9723: const StringID str = wi->data; rubidium@9723: rubidium@9723: assert(str == STR_00C5 || str == STR_00C6); // black or silver cross rubidium@9826: assert(r.right - r.left == 10); // To ensure the same sizes are used everywhere peter1138@2757: rubidium@5838: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_NONE); rubidium@9723: DrawString(r.left + 2, r.top + 2, str, TC_FROMSTRING); peter1138@2757: break; peter1138@2757: } peter1138@2757: rubidium@9826: case WWT_CAPTION: rubidium@9826: assert(r.bottom - r.top == 13); // To ensure the same sizes are used everywhere! hackykid@1938: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_BORDERONLY); glx@10645: DrawFrameRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, wi->color, (this->caption_color == 0xFF) ? FR_LOWERED | FR_DARKENED : FR_LOWERED | FR_DARKENED | FR_BORDERONLY); truelight@193: glx@10645: if (this->caption_color != 0xFF) { glx@10645: GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_player_colors[this->caption_color]][4]); truelight@0: } truelight@0: rubidium@9723: DrawStringCenteredTruncated(r.left + 2, r.right - 2, r.top + 2, wi->data, 0x84); rubidium@9724: break; rubidium@9724: rubidium@9724: case WWT_DROPDOWN: { rubidium@9724: assert(r.bottom - r.top == 11); // ensure consistent size rubidium@9724: rubidium@9724: StringID str = wi->data; rubidium@9724: DrawFrameRect(r.left, r.top, r.right - 12, r.bottom, wi->color, FR_NONE); rubidium@9724: DrawFrameRect(r.right - 11, r.top, r.right, r.bottom, wi->color, clicked ? FR_LOWERED : FR_NONE); rubidium@9724: DrawString(r.right - (clicked ? 8 : 9), r.top + (clicked ? 2 : 1), STR_0225, TC_BLACK); rubidium@9724: if (str != STR_NULL) DrawStringTruncated(r.left + 2, r.top + 1, str, TC_BLACK, r.right - r.left - 12); rubidium@9724: break; rubidium@9724: } rubidium@9724: rubidium@9724: case WWT_DROPDOWNIN: { rubidium@9724: assert(r.bottom - r.top == 11); // ensure consistent size rubidium@9724: rubidium@9724: StringID str = wi->data; rubidium@9724: DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_LOWERED | FR_DARKENED); rubidium@9724: DrawFrameRect(r.right - 11, r.top + 1, r.right - 1, r.bottom - 1, wi->color, clicked ? FR_LOWERED : FR_NONE); rubidium@9724: DrawString(r.right - (clicked ? 8 : 9), r.top + (clicked ? 2 : 1), STR_0225, TC_BLACK); rubidium@9724: if (str != STR_NULL) DrawStringTruncated(r.left + 2, r.top + 2, str, TC_BLACK, r.right - r.left - 12); rubidium@9724: break; rubidium@9724: } rubidium@9724: } rubidium@9724: glx@10645: if (this->IsWidgetDisabled(i)) { rubidium@9724: GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[wi->color & 0xF][2] | (1 << PALETTE_MODIFIER_GREYOUT)); truelight@0: } rubidium@5236: } truelight@193: truelight@0: glx@10645: if (this->flags4 & WF_WHITE_BORDER_MASK) { glx@10645: DrawFrameRect(0, 0, this->width - 1, this->height - 1, 0xF, FR_BORDERONLY); truelight@0: } truelight@0: truelight@0: } darkvater@164: bjarni@6075: static void ResizeWidgets(Window *w, byte a, byte b) bjarni@6075: { bjarni@6075: int16 offset = w->widget[a].left; bjarni@6075: int16 length = w->widget[b].right - offset; bjarni@6075: rubidium@9826: w->widget[a].right = (length / 2) + offset; bjarni@6075: bjarni@6073: w->widget[b].left = w->widget[a].right + 1; bjarni@6073: } bjarni@6073: bjarni@6075: static void ResizeWidgets(Window *w, byte a, byte b, byte c) bjarni@6073: { bjarni@6075: int16 offset = w->widget[a].left; bjarni@6075: int16 length = w->widget[c].right - offset; bjarni@6073: bjarni@6075: w->widget[a].right = length / 3; bjarni@6073: w->widget[b].right = w->widget[a].right * 2; bjarni@6073: bjarni@6075: w->widget[a].right += offset; bjarni@6075: w->widget[b].right += offset; bjarni@6075: bjarni@6073: /* Now the right side of the buttons are set. We will now set the left sides next to them */ bjarni@6073: w->widget[b].left = w->widget[a].right + 1; bjarni@6073: w->widget[c].left = w->widget[b].right + 1; bjarni@6073: } bjarni@6073: bjarni@6075: /** Evenly distribute some widgets when resizing horizontally (often a button row) bjarni@6075: * When only two arguments are given, the widgets are presumed to be on a line and only the ends are given rubidium@9601: * @param w Window to modify bjarni@6075: * @param left The leftmost widget to resize bjarni@6075: * @param right The rightmost widget to resize. Since right side of it is used, remember to set it to RESIZE_RIGHT bjarni@6075: */ bjarni@6073: void ResizeButtons(Window *w, byte left, byte right) bjarni@6073: { bjarni@6075: int16 num_widgets = right - left + 1; bjarni@6075: bjarni@6075: if (num_widgets < 2) NOT_REACHED(); bjarni@6075: bjarni@6075: switch (num_widgets) { bjarni@6075: case 2: ResizeWidgets(w, left, right); break; bjarni@6075: case 3: ResizeWidgets(w, left, left + 1, right); break; bjarni@6075: default: { bjarni@6075: /* Looks like we got more than 3 widgets to resize bjarni@6075: * Now we will find the middle of the space desinated for the widgets bjarni@6075: * and place half of the widgets on each side of it and call recursively. bjarni@6075: * Eventually we will get down to blocks of 2-3 widgets and we got code to handle those cases */ bjarni@6075: int16 offset = w->widget[left].left; bjarni@6075: int16 length = w->widget[right].right - offset; bjarni@6075: byte widget = ((num_widgets - 1)/ 2) + left; // rightmost widget of the left side bjarni@6075: bjarni@6075: /* Now we need to find the middle of the widgets. bjarni@6075: * It will not always be the middle because if we got an uneven number of widgets, bjarni@6075: * we will need it to be 2/5, 3/7 and so on bjarni@6075: * To get this, we multiply with num_widgets/num_widgets. Since we calculate in int, we will get: bjarni@6075: * bjarni@6075: * num_widgets/2 (rounding down) bjarni@6075: * --------------- bjarni@6075: * num_widgets bjarni@6075: * bjarni@6075: * as multiplier to length. We just multiply before divide to that we stay in the int area though */ bjarni@6075: int16 middle = ((length * num_widgets) / (2 * num_widgets)) + offset; bjarni@6075: bjarni@6075: /* Set left and right on the widgets, that's next to our "middle" */ bjarni@6075: w->widget[widget].right = middle; bjarni@6075: w->widget[widget + 1].left = w->widget[widget].right + 1; bjarni@6075: /* Now resize the left and right of the middle */ bjarni@6075: ResizeButtons(w, left, widget); bjarni@6075: ResizeButtons(w, widget + 1, right); bjarni@6075: } bjarni@6073: } bjarni@6073: } rubidium@9724: rubidium@10142: /** Resize a widget and shuffle other widgets around to fit. */ rubidium@9724: void ResizeWindowForWidget(Window *w, int widget, int delta_x, int delta_y) rubidium@9724: { rubidium@9724: int right = w->widget[widget].right; rubidium@9724: int bottom = w->widget[widget].bottom; rubidium@9724: rubidium@9724: for (uint i = 0; i < w->widget_count; i++) { rubidium@9724: if (w->widget[i].left >= right) w->widget[i].left += delta_x; rubidium@9724: if (w->widget[i].right >= right) w->widget[i].right += delta_x; rubidium@9724: if (w->widget[i].top >= bottom) w->widget[i].top += delta_y; rubidium@9724: if (w->widget[i].bottom >= bottom) w->widget[i].bottom += delta_y; rubidium@9724: } rubidium@9724: rubidium@9724: w->width += delta_x; rubidium@9724: w->height += delta_y; rubidium@9724: w->resize.width += delta_x; rubidium@9724: w->resize.height += delta_y; rubidium@9724: } rubidium@9724: rubidium@9724: /** Draw a sort button's up or down arrow symbol. rubidium@9724: * @param w Window of widget rubidium@9724: * @param widget Sort button widget rubidium@9724: * @param state State of sort button rubidium@9724: */ glx@10645: void Window::DrawSortButtonState(int widget, SortButtonState state) const rubidium@9724: { rubidium@9724: if (state == SBS_OFF) return; rubidium@9724: glx@10645: int offset = this->IsWidgetLowered(widget) ? 1 : 0; glx@10645: DoDrawString(state == SBS_DOWN ? DOWNARROW : UPARROW, this->widget[widget].right - 11 + offset, this->widget[widget].top + 1 + offset, TC_BLACK); rubidium@9724: }