2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/** @file widget.cpp */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2008-01-12 14:10:35 +00:00
|
|
|
#include "core/math_func.hpp"
|
|
|
|
#include "player_func.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "gfx_func.h"
|
2007-12-19 19:44:29 +00:00
|
|
|
#include "window_gui.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "window_func.h"
|
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/strings.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Point pt;
|
|
|
|
int height, count, pos, cap;
|
|
|
|
|
|
|
|
top += 10;
|
|
|
|
bottom -= 9;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
height = (bottom - top);
|
|
|
|
|
|
|
|
pos = sb->pos;
|
|
|
|
count = sb->count;
|
|
|
|
cap = sb->cap;
|
|
|
|
|
2005-07-08 22:25:24 +00:00
|
|
|
if (count != 0) top += height * pos / count;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (cap > count) cap = count;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (count != 0) bottom -= (count - pos - cap) * height / count;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
pt.x = top;
|
|
|
|
pt.y = bottom - 1;
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2007-04-17 20:23:13 +00:00
|
|
|
/** Special handling for the scrollbar widget type.
|
2004-08-09 17:04:08 +00:00
|
|
|
* Handles the special scrolling buttons and other
|
|
|
|
* scrolling.
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param w Window on which a scroll was performed.
|
|
|
|
* @param wi Pointer to the scrollbar widget.
|
|
|
|
* @param x The X coordinate of the mouse click.
|
|
|
|
* @param y The Y coordinate of the mouse click. */
|
2004-08-09 17:04:08 +00:00
|
|
|
void ScrollbarClickHandler(Window *w, const Widget *wi, int x, int y)
|
|
|
|
{
|
|
|
|
int mi, ma, pos;
|
|
|
|
Scrollbar *sb;
|
|
|
|
|
2005-01-02 17:23:04 +00:00
|
|
|
switch (wi->type) {
|
|
|
|
case WWT_SCROLLBAR: {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* vertical scroller */
|
2005-01-02 17:23:04 +00:00
|
|
|
w->flags4 &= ~WF_HSCROLL;
|
|
|
|
w->flags4 &= ~WF_SCROLL2;
|
|
|
|
mi = wi->top;
|
|
|
|
ma = wi->bottom;
|
|
|
|
pos = y;
|
|
|
|
sb = &w->vscroll;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WWT_SCROLL2BAR: {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* 2nd vertical scroller */
|
2005-01-02 17:23:04 +00:00
|
|
|
w->flags4 &= ~WF_HSCROLL;
|
|
|
|
w->flags4 |= WF_SCROLL2;
|
|
|
|
mi = wi->top;
|
|
|
|
ma = wi->bottom;
|
|
|
|
pos = y;
|
|
|
|
sb = &w->vscroll2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WWT_HSCROLLBAR: {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* horizontal scroller */
|
2005-01-03 19:45:18 +00:00
|
|
|
w->flags4 &= ~WF_SCROLL2;
|
2005-01-02 17:23:04 +00:00
|
|
|
w->flags4 |= WF_HSCROLL;
|
|
|
|
mi = wi->left;
|
|
|
|
ma = wi->right;
|
|
|
|
pos = x;
|
|
|
|
sb = &w->hscroll;
|
2005-01-03 19:45:18 +00:00
|
|
|
break;
|
2005-01-02 17:23:04 +00:00
|
|
|
}
|
2005-01-02 22:28:56 +00:00
|
|
|
default: return; //this should never happen
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
if (pos <= mi+9) {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Pressing the upper button? */
|
2005-11-04 14:57:53 +00:00
|
|
|
w->flags4 |= WF_SCROLL_UP;
|
|
|
|
if (_scroller_click_timeout == 0) {
|
|
|
|
_scroller_click_timeout = 6;
|
|
|
|
if (sb->pos != 0) sb->pos--;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-11-04 14:57:53 +00:00
|
|
|
_left_button_clicked = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (pos >= ma-10) {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Pressing the lower button? */
|
2005-11-04 14:57:53 +00:00
|
|
|
w->flags4 |= WF_SCROLL_DOWN;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-11-04 14:57:53 +00:00
|
|
|
if (_scroller_click_timeout == 0) {
|
|
|
|
_scroller_click_timeout = 6;
|
|
|
|
if ((byte)(sb->pos + sb->cap) < sb->count)
|
|
|
|
sb->pos++;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-11-04 14:57:53 +00:00
|
|
|
_left_button_clicked = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
Point pt = HandleScrollbarHittest(sb, mi, ma);
|
|
|
|
|
|
|
|
if (pos < pt.x) {
|
|
|
|
sb->pos = max(sb->pos - sb->cap, 0);
|
|
|
|
} else if (pos > pt.y) {
|
|
|
|
sb->pos = min(
|
2004-09-10 19:02:27 +00:00
|
|
|
sb->pos + sb->cap,
|
2004-08-09 17:04:08 +00:00
|
|
|
max(sb->count - sb->cap, 0)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
_scrollbar_start_pos = pt.x - mi - 9;
|
|
|
|
_scrollbar_size = ma - mi - 23;
|
|
|
|
w->flags4 |= WF_SCROLL_MIDDLE;
|
|
|
|
_scrolling_scrollbar = true;
|
|
|
|
_cursorpos_drag_start = _cursor.pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetWindowDirty(w);
|
|
|
|
}
|
|
|
|
|
2005-07-08 00:14:19 +00:00
|
|
|
/** Returns the index for the widget located at the given position
|
|
|
|
* relative to the window. It includes all widget-corner pixels as well.
|
|
|
|
* @param *w Window to look inside
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param x The Window client X coordinate
|
|
|
|
* @param y The Window client y coordinate
|
2005-07-08 00:14:19 +00:00
|
|
|
* @return A widget index, or -1 if no widget was found.
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2005-09-18 20:56:44 +00:00
|
|
|
int GetWidgetFromPos(const Window *w, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-12-04 13:57:04 +00:00
|
|
|
uint index;
|
|
|
|
int found_index = -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Go through the widgets and check if we find the widget that the coordinate is
|
|
|
|
* inside. */
|
2006-12-04 13:57:04 +00:00
|
|
|
for (index = 0; index < w->widget_count; index++) {
|
|
|
|
const Widget *wi = &w->widget[index];
|
2005-11-14 19:48:04 +00:00
|
|
|
if (wi->type == WWT_EMPTY || wi->type == WWT_FRAME) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-08 00:14:19 +00:00
|
|
|
if (x >= wi->left && x <= wi->right && y >= wi->top && y <= wi->bottom &&
|
2007-12-02 14:29:48 +00:00
|
|
|
!w->IsWidgetHidden(index)) {
|
2005-11-14 19:48:04 +00:00
|
|
|
found_index = index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-29 06:07:57 +00:00
|
|
|
void DrawFrameRect(int left, int top, int right, int bottom, int ctab, FrameFlags flags)
|
|
|
|
{
|
2006-08-29 19:26:13 +00:00
|
|
|
uint dark = _colour_gradient[ctab][3];
|
|
|
|
uint medium_dark = _colour_gradient[ctab][5];
|
|
|
|
uint medium_light = _colour_gradient[ctab][6];
|
|
|
|
uint light = _colour_gradient[ctab][7];
|
2006-08-29 06:07:57 +00:00
|
|
|
|
2006-08-29 06:39:00 +00:00
|
|
|
if (flags & FR_TRANSPARENT) {
|
2007-01-14 19:57:49 +00:00
|
|
|
GfxFillRect(left, top, right, bottom, PALETTE_TO_TRANSPARENT | (1 << USE_COLORTABLE));
|
2006-08-29 06:07:57 +00:00
|
|
|
} else {
|
|
|
|
uint interior;
|
|
|
|
|
|
|
|
if (flags & FR_LOWERED) {
|
|
|
|
GfxFillRect(left, top, left, bottom, dark);
|
|
|
|
GfxFillRect(left + 1, top, right, top, dark);
|
|
|
|
GfxFillRect(right, top + 1, right, bottom - 1, light);
|
|
|
|
GfxFillRect(left + 1, bottom, right, bottom, light);
|
|
|
|
interior = (flags & FR_DARKENED ? medium_dark : medium_light);
|
|
|
|
} else {
|
|
|
|
GfxFillRect(left, top, left, bottom - 1, light);
|
|
|
|
GfxFillRect(left + 1, top, right - 1, top, light);
|
|
|
|
GfxFillRect(right, top, right, bottom - 1, dark);
|
|
|
|
GfxFillRect(left, bottom, right, bottom, dark);
|
|
|
|
interior = medium_dark;
|
|
|
|
}
|
|
|
|
if (!(flags & FR_BORDERONLY)) {
|
|
|
|
GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, interior);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
void DrawWindowWidgets(const Window *w)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-10-22 06:39:32 +00:00
|
|
|
const DrawPixelInfo* dpi = _cur_dpi;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
for (uint i = 0; i < w->widget_count; i++) {
|
2006-12-04 13:57:04 +00:00
|
|
|
const Widget *wi = &w->widget[i];
|
2007-12-02 14:29:48 +00:00
|
|
|
bool clicked = w->IsWidgetLowered(i);
|
2007-11-25 00:41:31 +00:00
|
|
|
Rect r;
|
2005-04-07 00:59:54 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
if (dpi->left > (r.right = wi->right) ||
|
|
|
|
dpi->left + dpi->width <= (r.left = wi->left) ||
|
|
|
|
dpi->top > (r.bottom = wi->bottom) ||
|
|
|
|
dpi->top + dpi->height <= (r.top = wi->top) ||
|
2007-12-02 14:29:48 +00:00
|
|
|
w->IsWidgetHidden(i)) {
|
2005-11-14 19:48:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-04-07 00:59:54 +00:00
|
|
|
switch (wi->type & WWT_MASK) {
|
2006-10-24 14:15:17 +00:00
|
|
|
case WWT_IMGBTN:
|
|
|
|
case WWT_IMGBTN_2: {
|
2007-11-25 00:41:31 +00:00
|
|
|
SpriteID img = wi->data;
|
2006-10-24 14:15:17 +00:00
|
|
|
assert(img != 0);
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2005-04-07 00:59:54 +00:00
|
|
|
|
2006-10-24 14:15:17 +00:00
|
|
|
/* show different image when clicked for WWT_IMGBTN_2 */
|
|
|
|
if ((wi->type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++;
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(img, PAL_NONE, r.left + 1 + clicked, r.top + 1 + clicked);
|
2006-10-24 14:15:17 +00:00
|
|
|
goto draw_default;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-10-24 14:15:17 +00:00
|
|
|
case WWT_PANEL: {
|
|
|
|
assert(wi->data == 0);
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2004-08-09 17:04:08 +00:00
|
|
|
goto draw_default;
|
|
|
|
}
|
|
|
|
|
2006-10-24 16:27:18 +00:00
|
|
|
case WWT_TEXTBTN:
|
|
|
|
case WWT_TEXTBTN_2: {
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
|
2006-08-22 15:23:25 +00:00
|
|
|
case WWT_LABEL: {
|
2006-09-04 15:44:28 +00:00
|
|
|
StringID str = wi->data;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-10-24 16:27:18 +00:00
|
|
|
if ((wi->type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-11-04 00:08:57 +00:00
|
|
|
DrawStringCentered(((r.left + r.right + 1) >> 1) + clicked, ((r.top + r.bottom + 1) >> 1) - 5 + clicked, str, TC_FROMSTRING);
|
2005-07-15 15:09:52 +00:00
|
|
|
goto draw_default;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 03:25:15 +00:00
|
|
|
case WWT_TEXT: {
|
2007-11-25 00:41:31 +00:00
|
|
|
const StringID str = wi->data;
|
2007-03-10 03:25:15 +00:00
|
|
|
|
|
|
|
if (str != STR_NULL) DrawStringTruncated(r.left, r.top, str, wi->color, r.right - r.left);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-10-24 16:27:18 +00:00
|
|
|
case WWT_INSET: {
|
2007-11-25 00:41:31 +00:00
|
|
|
const StringID str = wi->data;
|
2005-06-15 17:27:14 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_LOWERED | FR_DARKENED);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-04 00:08:57 +00:00
|
|
|
if (str != STR_NULL) DrawStringTruncated(r.left + 2, r.top + 1, str, TC_FROMSTRING, r.right - r.left - 10);
|
2004-08-09 17:04:08 +00:00
|
|
|
goto draw_default;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WWT_MATRIX: {
|
2004-09-10 19:02:27 +00:00
|
|
|
int c, d, ctr;
|
2004-08-09 17:04:08 +00:00
|
|
|
int x, amt1, amt2;
|
|
|
|
int color;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-09-04 15:44:28 +00:00
|
|
|
c = GB(wi->data, 0, 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
amt1 = (wi->right - wi->left + 1) / c;
|
|
|
|
|
2006-09-04 15:44:28 +00:00
|
|
|
d = GB(wi->data, 8, 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
amt2 = (wi->bottom - wi->top + 1) / d;
|
|
|
|
|
2006-08-29 19:26:13 +00:00
|
|
|
color = _colour_gradient[wi->color & 0xF][6];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
x = r.left;
|
2005-12-28 08:39:43 +00:00
|
|
|
for (ctr = c; ctr > 1; ctr--) {
|
2004-08-09 17:04:08 +00:00
|
|
|
x += amt1;
|
2005-11-14 19:48:04 +00:00
|
|
|
GfxFillRect(x, r.top + 1, x, r.bottom - 1, color);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
x = r.top;
|
2005-12-28 08:39:43 +00:00
|
|
|
for (ctr = d; ctr > 1; ctr--) {
|
2004-08-09 17:04:08 +00:00
|
|
|
x += amt2;
|
2005-11-14 19:48:04 +00:00
|
|
|
GfxFillRect(r.left + 1, x, r.right - 1, x, color);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
color = _colour_gradient[wi->color & 0xF][4];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
x = r.left - 1;
|
2005-12-28 08:39:43 +00:00
|
|
|
for (ctr = c; ctr > 1; ctr--) {
|
2004-08-09 17:04:08 +00:00
|
|
|
x += amt1;
|
2005-11-14 19:48:04 +00:00
|
|
|
GfxFillRect(x, r.top + 1, x, r.bottom - 1, color);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
x = r.top - 1;
|
2005-12-28 08:39:43 +00:00
|
|
|
for (ctr = d; ctr > 1; ctr--) {
|
2004-08-09 17:04:08 +00:00
|
|
|
x += amt2;
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left + 1, x, r.right - 1, x, color);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
goto draw_default;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* vertical scrollbar */
|
2004-08-09 17:04:08 +00:00
|
|
|
case WWT_SCROLLBAR: {
|
|
|
|
Point pt;
|
2007-11-25 00:41:31 +00:00
|
|
|
int c1, c2;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
assert(wi->data == 0);
|
2005-01-04 21:28:09 +00:00
|
|
|
assert(r.right - r.left == 11); // XXX - to ensure the same sizes are used everywhere!
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* draw up/down buttons */
|
2006-10-24 16:28:34 +00:00
|
|
|
clicked = ((w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_UP);
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.top + 9, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2007-11-04 00:08:57 +00:00
|
|
|
DoDrawString(UPARROW, r.left + 2 + clicked, r.top + clicked, TC_BLACK);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-10-24 16:28:34 +00:00
|
|
|
clicked = (((w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_DOWN));
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.bottom - 9, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2007-11-04 00:08:57 +00:00
|
|
|
DoDrawString(DOWNARROW, r.left + 2 + clicked, r.bottom - 9 + clicked, TC_BLACK);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
c1 = _colour_gradient[wi->color & 0xF][3];
|
|
|
|
c2 = _colour_gradient[wi->color & 0xF][7];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* draw "shaded" background */
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c2);
|
|
|
|
GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c1 | (1 << PALETTE_MODIFIER_GREYOUT));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* draw shaded lines */
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left + 2, r.top + 10, r.left + 2, r.bottom - 10, c1);
|
|
|
|
GfxFillRect(r.left + 3, r.top + 10, r.left + 3, r.bottom - 10, c2);
|
|
|
|
GfxFillRect(r.left + 7, r.top + 10, r.left + 7, r.bottom - 10, c1);
|
|
|
|
GfxFillRect(r.left + 8, r.top + 10, r.left + 8, r.bottom - 10, c2);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
pt = HandleScrollbarHittest(&w->vscroll, r.top, r.bottom);
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, pt.x, r.right, pt.y, wi->color, (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_MIDDLE ? FR_LOWERED : FR_NONE);
|
2005-01-02 17:23:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WWT_SCROLL2BAR: {
|
|
|
|
Point pt;
|
2007-11-25 00:41:31 +00:00
|
|
|
int c1, c2;
|
2005-01-02 17:23:04 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
assert(wi->data == 0);
|
2005-01-04 21:28:09 +00:00
|
|
|
assert(r.right - r.left == 11); // XXX - to ensure the same sizes are used everywhere!
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* draw up/down buttons */
|
2006-10-24 16:28:34 +00:00
|
|
|
clicked = ((w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_UP | WF_SCROLL2));
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.top + 9, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2007-11-04 00:08:57 +00:00
|
|
|
DoDrawString(UPARROW, r.left + 2 + clicked, r.top + clicked, TC_BLACK);
|
2005-01-02 17:23:04 +00:00
|
|
|
|
2006-10-24 16:28:34 +00:00
|
|
|
clicked = ((w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_DOWN | WF_SCROLL2));
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.bottom - 9, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2007-11-04 00:08:57 +00:00
|
|
|
DoDrawString(DOWNARROW, r.left + 2 + clicked, r.bottom - 9 + clicked, TC_BLACK);
|
2005-01-02 17:23:04 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
c1 = _colour_gradient[wi->color & 0xF][3];
|
|
|
|
c2 = _colour_gradient[wi->color & 0xF][7];
|
2005-01-02 17:23:04 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* draw "shaded" background */
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c2);
|
|
|
|
GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c1 | (1 << PALETTE_MODIFIER_GREYOUT));
|
2005-01-02 17:23:04 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* draw shaded lines */
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left + 2, r.top + 10, r.left + 2, r.bottom - 10, c1);
|
|
|
|
GfxFillRect(r.left + 3, r.top + 10, r.left + 3, r.bottom - 10, c2);
|
|
|
|
GfxFillRect(r.left + 7, r.top + 10, r.left + 7, r.bottom - 10, c1);
|
|
|
|
GfxFillRect(r.left + 8, r.top + 10, r.left + 8, r.bottom - 10, c2);
|
2005-01-02 17:23:04 +00:00
|
|
|
|
|
|
|
pt = HandleScrollbarHittest(&w->vscroll2, r.top, r.bottom);
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, pt.x, r.right, pt.y, wi->color, (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_MIDDLE | WF_SCROLL2) ? FR_LOWERED : FR_NONE);
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* horizontal scrollbar */
|
2004-08-09 17:04:08 +00:00
|
|
|
case WWT_HSCROLLBAR: {
|
|
|
|
Point pt;
|
2007-11-25 00:41:31 +00:00
|
|
|
int c1, c2;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
assert(wi->data == 0);
|
2005-01-04 21:45:38 +00:00
|
|
|
assert(r.bottom - r.top == 11); // XXX - to ensure the same sizes are used everywhere!
|
2005-01-04 21:28:09 +00:00
|
|
|
|
2006-10-24 16:28:34 +00:00
|
|
|
clicked = ((w->flags4 & (WF_SCROLL_UP | WF_HSCROLL)) == (WF_SCROLL_UP | WF_HSCROLL));
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.left + 9, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + clicked, r.top + 1 + clicked);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-10-24 16:28:34 +00:00
|
|
|
clicked = ((w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL)) == (WF_SCROLL_DOWN | WF_HSCROLL));
|
2007-11-25 00:41:31 +00:00
|
|
|
DrawFrameRect(r.right - 9, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - 8 + clicked, r.top + 1 + clicked);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
c1 = _colour_gradient[wi->color & 0xF][3];
|
|
|
|
c2 = _colour_gradient[wi->color & 0xF][7];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* draw "shaded" background */
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c2);
|
|
|
|
GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c1 | (1 << PALETTE_MODIFIER_GREYOUT));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* draw shaded lines */
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left + 10, r.top + 2, r.right - 10, r.top + 2, c1);
|
|
|
|
GfxFillRect(r.left + 10, r.top + 3, r.right - 10, r.top + 3, c2);
|
|
|
|
GfxFillRect(r.left + 10, r.top + 7, r.right - 10, r.top + 7, c1);
|
|
|
|
GfxFillRect(r.left + 10, r.top + 8, r.right - 10, r.top + 8, c2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* draw actual scrollbar */
|
2004-08-09 17:04:08 +00:00
|
|
|
pt = HandleScrollbarHittest(&w->hscroll, r.left, r.right);
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(pt.x, r.top, pt.y, r.bottom, wi->color, (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL)) == (WF_SCROLL_MIDDLE | WF_HSCROLL) ? FR_LOWERED : FR_NONE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WWT_FRAME: {
|
2007-11-25 00:41:31 +00:00
|
|
|
const StringID str = wi->data;
|
|
|
|
int c1, c2;
|
2005-01-03 17:55:25 +00:00
|
|
|
int x2 = r.left; // by default the left side is the left side of the widget
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
if (str != STR_NULL) x2 = DrawString(r.left + 6, r.top, str, TC_FROMSTRING);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-29 19:26:13 +00:00
|
|
|
c1 = _colour_gradient[wi->color][3];
|
|
|
|
c2 = _colour_gradient[wi->color][7];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
/* Line from upper left corner to start of text */
|
|
|
|
GfxFillRect(r.left, r.top + 4, r.left + 4, r.top + 4, c1);
|
|
|
|
GfxFillRect(r.left + 1, r.top + 5, r.left + 4, r.top + 5, c2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Line from end of text to upper right corner */
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(x2, r.top + 4, r.right - 1, r.top + 4, c1);
|
|
|
|
GfxFillRect(x2, r.top + 5, r.right - 2, r.top + 5, c2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Line from upper left corner to bottom left corner */
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left, r.top + 5, r.left, r.bottom - 1, c1);
|
|
|
|
GfxFillRect(r.left + 1, r.top + 6, r.left + 1, r.bottom - 2, c2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/*Line from upper right corner to bottom right corner */
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.right - 1, r.top + 5, r.right - 1, r.bottom - 2, c1);
|
|
|
|
GfxFillRect(r.right, r.top + 4, r.right, r.bottom - 1, c2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
|
2004-08-09 17:04:08 +00:00
|
|
|
GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
|
|
|
|
|
2005-07-15 15:09:52 +00:00
|
|
|
goto draw_default;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-15 23:33:04 +00:00
|
|
|
case WWT_STICKYBOX: {
|
2007-11-25 00:41:31 +00:00
|
|
|
assert(wi->data == 0);
|
2005-01-04 21:28:09 +00:00
|
|
|
assert(r.right - r.left == 11); // XXX - to ensure the same sizes are used everywhere!
|
2005-11-29 22:04:02 +00:00
|
|
|
|
|
|
|
clicked = !!(w->flags4 & WF_STICKY);
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite((clicked) ? SPR_PIN_UP : SPR_PIN_DOWN, PAL_NONE, r.left + 2 + clicked, r.top + 3 + clicked);
|
2005-01-03 19:45:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WWT_RESIZEBOX: {
|
2007-11-25 00:41:31 +00:00
|
|
|
assert(wi->data == 0);
|
2005-01-04 21:28:09 +00:00
|
|
|
assert(r.right - r.left == 11); // XXX - to ensure the same sizes are used everywhere!
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2005-04-07 00:59:54 +00:00
|
|
|
clicked = !!(w->flags4 & WF_SIZING);
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(SPR_WINDOW_RESIZE, PAL_NONE, r.left + 3 + clicked, r.top + 3 + clicked);
|
2004-12-15 23:33:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-01-03 19:45:18 +00:00
|
|
|
|
2005-12-14 08:05:36 +00:00
|
|
|
case WWT_CLOSEBOX: {
|
2007-11-25 00:41:31 +00:00
|
|
|
const StringID str = wi->data;
|
|
|
|
|
|
|
|
assert(str == STR_00C5 || str == STR_00C6); // black or silver cross
|
2005-12-14 08:05:36 +00:00
|
|
|
assert(r.right - r.left == 10); // ensure the same sizes are used everywhere
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_NONE);
|
2007-11-25 00:41:31 +00:00
|
|
|
DrawString(r.left + 2, r.top + 2, str, TC_FROMSTRING);
|
2005-12-14 08:05:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
case WWT_CAPTION: {
|
2005-01-04 21:28:09 +00:00
|
|
|
assert(r.bottom - r.top == 13); // XXX - to ensure the same sizes are used everywhere!
|
2005-06-15 17:27:14 +00:00
|
|
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_BORDERONLY);
|
2007-11-25 00:41:31 +00:00
|
|
|
DrawFrameRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, wi->color, (w->caption_color == 0xFF) ? FR_LOWERED | FR_DARKENED : FR_LOWERED | FR_DARKENED | FR_BORDERONLY);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->caption_color != 0xFF) {
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_player_colors[w->caption_color]][4]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-11-25 00:41:31 +00:00
|
|
|
DrawStringCenteredTruncated(r.left + 2, r.right - 2, r.top + 2, wi->data, 0x84);
|
2004-08-09 17:04:08 +00:00
|
|
|
draw_default:;
|
2007-12-02 14:29:48 +00:00
|
|
|
if (w->IsWidgetDisabled(i)) {
|
2007-11-25 00:41:31 +00:00
|
|
|
GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[wi->color & 0xF][2] | (1 << PALETTE_MODIFIER_GREYOUT));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-12-04 13:57:04 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (w->flags4 & WF_WHITE_BORDER_MASK) {
|
2007-11-25 00:41:31 +00:00
|
|
|
DrawFrameRect(0, 0, w->width - 1, w->height - 1, 0xF, FR_BORDERONLY);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2005-01-04 13:45:58 +00:00
|
|
|
static const Widget _dropdown_menu_widgets[] = {
|
2006-10-24 14:15:17 +00:00
|
|
|
{ WWT_PANEL, RESIZE_NONE, 0, 0, 0, 0, 0, 0x0, STR_NULL},
|
2006-11-06 22:06:29 +00:00
|
|
|
{ WWT_SCROLLBAR, RESIZE_NONE, 0, 0, 0, 0, 0, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
|
2004-09-07 21:48:09 +00:00
|
|
|
{ WIDGETS_END},
|
2004-09-06 18:15:13 +00:00
|
|
|
};
|
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
static int GetDropdownItem(const Window *w)
|
2004-09-06 18:15:13 +00:00
|
|
|
{
|
2005-09-23 07:44:03 +00:00
|
|
|
byte item, counter;
|
2004-09-06 18:15:13 +00:00
|
|
|
int y;
|
|
|
|
|
|
|
|
if (GetWidgetFromPos(w, _cursor.pos.x - w->left, _cursor.pos.y - w->top) < 0)
|
|
|
|
return -1;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-11-30 21:46:47 +00:00
|
|
|
y = _cursor.pos.y - w->top - 2 + w->vscroll.pos * 10;
|
2004-09-06 18:15:13 +00:00
|
|
|
|
|
|
|
if (y < 0)
|
|
|
|
return - 1;
|
|
|
|
|
|
|
|
item = y / 10;
|
2007-12-16 10:54:08 +00:00
|
|
|
if (item >= WP(w, dropdown_d).num_items || (HasBit(WP(w,dropdown_d).disabled_state, item) && !HasBit(WP(w,dropdown_d).hidden_state, item)) || WP(w,dropdown_d).items[item] == 0)
|
2004-09-06 18:15:13 +00:00
|
|
|
return - 1;
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Skip hidden items -- +1 for each hidden item before the clicked item. */
|
2005-09-23 07:44:03 +00:00
|
|
|
for (counter = 0; item >= counter; ++counter)
|
2007-12-16 10:54:08 +00:00
|
|
|
if (HasBit(WP(w, dropdown_d).hidden_state, counter)) item++;
|
2005-09-23 07:44:03 +00:00
|
|
|
|
2004-09-06 18:15:13 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void DropdownMenuWndProc(Window *w, WindowEvent *e)
|
2004-09-06 18:15:13 +00:00
|
|
|
{
|
|
|
|
int item;
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
switch (e->event) {
|
2005-01-02 17:23:04 +00:00
|
|
|
case WE_PAINT: {
|
|
|
|
int x,y,i,sel;
|
2006-11-30 21:46:47 +00:00
|
|
|
int width, height;
|
2005-01-02 17:23:04 +00:00
|
|
|
|
|
|
|
DrawWindowWidgets(w);
|
|
|
|
|
|
|
|
x = 1;
|
2006-11-30 21:46:47 +00:00
|
|
|
y = 2 - w->vscroll.pos * 10;
|
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
sel = WP(w, dropdown_d).selected_index;
|
2006-11-30 21:46:47 +00:00
|
|
|
width = w->widget[0].right - 3;
|
|
|
|
height = w->widget[0].bottom - 3;
|
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
for (i = 0; WP(w, dropdown_d).items[i] != INVALID_STRING_ID; i++, sel--) {
|
|
|
|
if (HasBit(WP(w, dropdown_d).hidden_state, i)) continue;
|
2006-11-30 21:46:47 +00:00
|
|
|
|
|
|
|
if (y >= 0 && y <= height) {
|
2007-12-16 10:54:08 +00:00
|
|
|
if (WP(w, dropdown_d).items[i] != STR_NULL) {
|
2006-11-30 21:46:47 +00:00
|
|
|
if (sel == 0) GfxFillRect(x + 1, y, x + width, y + 9, 0);
|
2007-12-16 10:54:08 +00:00
|
|
|
DrawStringTruncated(x + 2, y, WP(w, dropdown_d).items[i], sel == 0 ? TC_WHITE : TC_BLACK, x + width);
|
2006-11-30 21:46:47 +00:00
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
if (HasBit(WP(w, dropdown_d).disabled_state, i)) {
|
2006-11-30 21:46:47 +00:00
|
|
|
GfxFillRect(x, y, x + width, y + 9,
|
2007-01-14 19:57:49 +00:00
|
|
|
(1 << PALETTE_MODIFIER_GREYOUT) | _colour_gradient[_dropdown_menu_widgets[0].color][5]
|
2006-11-30 21:46:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int c1 = _colour_gradient[_dropdown_menu_widgets[0].color][3];
|
|
|
|
int c2 = _colour_gradient[_dropdown_menu_widgets[0].color][7];
|
|
|
|
|
|
|
|
GfxFillRect(x + 1, y + 3, x + w->width - 5, y + 3, c1);
|
|
|
|
GfxFillRect(x + 1, y + 4, x + w->width - 5, y + 4, c2);
|
2005-01-26 11:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
y += 10;
|
2005-01-02 17:23:04 +00:00
|
|
|
}
|
|
|
|
} break;
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2005-01-02 17:23:04 +00:00
|
|
|
case WE_CLICK: {
|
2006-11-06 22:06:29 +00:00
|
|
|
if (e->we.click.widget != 0) break;
|
2005-01-02 17:23:04 +00:00
|
|
|
item = GetDropdownItem(w);
|
|
|
|
if (item >= 0) {
|
2007-12-16 10:54:08 +00:00
|
|
|
WP(w, dropdown_d).click_delay = 4;
|
|
|
|
WP(w, dropdown_d).selected_index = item;
|
2005-01-02 17:23:04 +00:00
|
|
|
SetWindowDirty(w);
|
2004-09-06 18:15:13 +00:00
|
|
|
}
|
2005-01-02 17:23:04 +00:00
|
|
|
} break;
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2005-01-02 17:23:04 +00:00
|
|
|
case WE_MOUSELOOP: {
|
2007-12-16 10:54:08 +00:00
|
|
|
Window *w2 = FindWindowById(WP(w, dropdown_d).parent_wnd_class, WP(w,dropdown_d).parent_wnd_num);
|
2005-01-02 17:23:04 +00:00
|
|
|
if (w2 == NULL) {
|
|
|
|
DeleteWindow(w);
|
|
|
|
return;
|
|
|
|
}
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
if (WP(w, dropdown_d).click_delay != 0 && --WP(w,dropdown_d).click_delay == 0) {
|
2005-01-02 17:23:04 +00:00
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_DROPDOWN_SELECT;
|
2007-12-16 10:54:08 +00:00
|
|
|
e.we.dropdown.button = WP(w, dropdown_d).parent_button;
|
|
|
|
e.we.dropdown.index = WP(w, dropdown_d).selected_index;
|
2005-01-02 17:23:04 +00:00
|
|
|
w2->wndproc(w2, &e);
|
|
|
|
DeleteWindow(w);
|
|
|
|
return;
|
|
|
|
}
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
if (WP(w, dropdown_d).drag_mode) {
|
2005-01-02 17:23:04 +00:00
|
|
|
item = GetDropdownItem(w);
|
|
|
|
|
|
|
|
if (!_left_button_clicked) {
|
2007-12-16 10:54:08 +00:00
|
|
|
WP(w, dropdown_d).drag_mode = false;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (item < 0) return;
|
2007-12-16 10:54:08 +00:00
|
|
|
WP(w, dropdown_d).click_delay = 2;
|
2005-01-02 17:23:04 +00:00
|
|
|
} else {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (item < 0) return;
|
2005-01-02 17:23:04 +00:00
|
|
|
}
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
WP(w, dropdown_d).selected_index = item;
|
2005-01-02 17:23:04 +00:00
|
|
|
SetWindowDirty(w);
|
2004-09-06 18:15:13 +00:00
|
|
|
}
|
2005-01-02 17:23:04 +00:00
|
|
|
} break;
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2005-01-02 17:23:04 +00:00
|
|
|
case WE_DESTROY: {
|
2007-12-16 10:54:08 +00:00
|
|
|
Window *w2 = FindWindowById(WP(w, dropdown_d).parent_wnd_class, WP(w,dropdown_d).parent_wnd_num);
|
2005-01-02 17:23:04 +00:00
|
|
|
if (w2 != NULL) {
|
2007-12-16 10:54:08 +00:00
|
|
|
w2->RaiseWidget(WP(w, dropdown_d).parent_button);
|
|
|
|
w2->InvalidateWidget(WP(w, dropdown_d).parent_button);
|
2005-01-02 17:23:04 +00:00
|
|
|
}
|
|
|
|
} break;
|
2004-09-06 18:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-23 07:44:03 +00:00
|
|
|
void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask)
|
2004-09-06 18:15:13 +00:00
|
|
|
{
|
2005-01-03 19:45:18 +00:00
|
|
|
int i;
|
2004-09-06 18:15:13 +00:00
|
|
|
const Widget *wi;
|
|
|
|
Window *w2;
|
2006-11-06 22:06:29 +00:00
|
|
|
const Window *w3;
|
2007-12-02 14:29:48 +00:00
|
|
|
bool is_dropdown_menu_shown = w->IsWidgetLowered(button);
|
2006-11-06 22:06:29 +00:00
|
|
|
int top, height;
|
|
|
|
int screen_top, screen_bottom;
|
|
|
|
bool scroll = false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-09-06 18:15:13 +00:00
|
|
|
DeleteWindowById(WC_DROPDOWN_MENU, 0);
|
|
|
|
|
2006-10-03 20:16:20 +00:00
|
|
|
if (is_dropdown_menu_shown) return;
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2007-12-02 14:29:48 +00:00
|
|
|
w->LowerWidget(button);
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2007-12-07 18:05:49 +00:00
|
|
|
w->InvalidateWidget(button);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
for (i = 0; strings[i] != INVALID_STRING_ID; i++) {}
|
|
|
|
if (i == 0) return;
|
2004-09-06 18:15:13 +00:00
|
|
|
|
|
|
|
wi = &w->widget[button];
|
|
|
|
|
2005-09-23 07:44:03 +00:00
|
|
|
if (hidden_mask != 0) {
|
2005-11-14 19:48:04 +00:00
|
|
|
uint j;
|
|
|
|
|
2005-11-14 08:42:45 +00:00
|
|
|
for (j = 0; strings[j] != INVALID_STRING_ID; j++) {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(hidden_mask, j)) i--;
|
2005-01-02 17:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-06 22:06:29 +00:00
|
|
|
/* The preferred position is just below the dropdown calling widget */
|
|
|
|
top = w->top + wi->bottom + 2;
|
|
|
|
height = i * 10 + 4;
|
|
|
|
|
|
|
|
w3 = FindWindowById(WC_STATUS_BAR, 0);
|
|
|
|
screen_bottom = w3 == NULL ? _screen.height : w3->top;
|
|
|
|
|
|
|
|
/* Check if the dropdown will fully fit below the widget */
|
|
|
|
if (top + height >= screen_bottom) {
|
|
|
|
w3 = FindWindowById(WC_MAIN_TOOLBAR, 0);
|
|
|
|
screen_top = w3 == NULL ? 0 : w3->top + w3->height;
|
|
|
|
|
|
|
|
/* If not, check if it will fit above the widget */
|
|
|
|
if (w->top + wi->top - height - 1 > screen_top) {
|
|
|
|
top = w->top + wi->top - height - 1;
|
|
|
|
} else {
|
|
|
|
/* ... and lastly if it won't, enable the scroll bar and fit the
|
|
|
|
* list in below the widget */
|
2006-11-30 21:46:47 +00:00
|
|
|
int rows = (screen_bottom - 4 - top) / 10;
|
|
|
|
height = rows * 10 + 4;
|
2006-11-06 22:06:29 +00:00
|
|
|
scroll = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-06 18:15:13 +00:00
|
|
|
w2 = AllocateWindow(
|
|
|
|
w->left + wi[-1].left + 1,
|
2006-11-06 22:06:29 +00:00
|
|
|
top,
|
2005-01-03 19:45:18 +00:00
|
|
|
wi->right - wi[-1].left + 1,
|
2006-11-06 22:06:29 +00:00
|
|
|
height,
|
2004-09-06 18:15:13 +00:00
|
|
|
DropdownMenuWndProc,
|
2006-09-08 03:59:38 +00:00
|
|
|
WC_DROPDOWN_MENU,
|
2004-09-06 18:15:13 +00:00
|
|
|
_dropdown_menu_widgets);
|
|
|
|
|
2005-01-03 19:45:18 +00:00
|
|
|
w2->widget[0].color = wi->color;
|
|
|
|
w2->widget[0].right = wi->right - wi[-1].left;
|
2006-11-06 22:06:29 +00:00
|
|
|
w2->widget[0].bottom = height - 1;
|
|
|
|
|
2007-12-02 14:29:48 +00:00
|
|
|
w2->SetWidgetHiddenState(1, !scroll);
|
2006-11-06 22:06:29 +00:00
|
|
|
|
|
|
|
if (scroll) {
|
|
|
|
/* We're scrolling, so enable the scroll bar and shrink the list by
|
|
|
|
* the scrollbar's width */
|
|
|
|
w2->widget[1].color = wi->color;
|
|
|
|
w2->widget[1].right = w2->widget[0].right;
|
|
|
|
w2->widget[1].left = w2->widget[1].right - 11;
|
|
|
|
w2->widget[1].bottom = height - 1;
|
|
|
|
w2->widget[0].right -= 12;
|
|
|
|
|
2006-11-30 21:46:47 +00:00
|
|
|
w2->vscroll.cap = (height - 4) / 10;
|
|
|
|
w2->vscroll.count = i;
|
2006-11-06 22:06:29 +00:00
|
|
|
}
|
2004-09-06 18:15:13 +00:00
|
|
|
|
2006-11-06 22:06:29 +00:00
|
|
|
w2->desc_flags = WDF_DEF_WIDGET;
|
2004-09-06 18:15:13 +00:00
|
|
|
w2->flags4 &= ~WF_WHITE_BORDER_MASK;
|
2005-11-12 11:10:12 +00:00
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
WP(w2, dropdown_d).disabled_state = disabled_mask;
|
|
|
|
WP(w2, dropdown_d).hidden_state = hidden_mask;
|
2005-11-14 08:42:45 +00:00
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
WP(w2, dropdown_d).parent_wnd_class = w->window_class;
|
|
|
|
WP(w2, dropdown_d).parent_wnd_num = w->window_number;
|
|
|
|
WP(w2, dropdown_d).parent_button = button;
|
2005-11-14 08:42:45 +00:00
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
WP(w2, dropdown_d).num_items = i;
|
|
|
|
WP(w2, dropdown_d).selected_index = selected;
|
|
|
|
WP(w2, dropdown_d).items = strings;
|
2005-11-14 08:42:45 +00:00
|
|
|
|
2007-12-16 10:54:08 +00:00
|
|
|
WP(w2, dropdown_d).click_delay = 0;
|
|
|
|
WP(w2, dropdown_d).drag_mode = true;
|
2004-09-06 18:15:13 +00:00
|
|
|
}
|
2007-01-24 02:36:55 +00:00
|
|
|
|
2007-01-24 14:32:20 +00:00
|
|
|
|
|
|
|
static void ResizeWidgets(Window *w, byte a, byte b)
|
2007-01-24 02:36:55 +00:00
|
|
|
{
|
2007-01-24 14:32:20 +00:00
|
|
|
int16 offset = w->widget[a].left;
|
|
|
|
int16 length = w->widget[b].right - offset;
|
|
|
|
|
|
|
|
w->widget[a].right = (length / 2) + offset;
|
2007-01-24 02:36:55 +00:00
|
|
|
|
|
|
|
w->widget[b].left = w->widget[a].right + 1;
|
|
|
|
}
|
|
|
|
|
2007-01-24 14:32:20 +00:00
|
|
|
static void ResizeWidgets(Window *w, byte a, byte b, byte c)
|
2007-01-24 02:36:55 +00:00
|
|
|
{
|
2007-01-24 14:32:20 +00:00
|
|
|
int16 offset = w->widget[a].left;
|
|
|
|
int16 length = w->widget[c].right - offset;
|
2007-01-24 02:36:55 +00:00
|
|
|
|
2007-01-24 14:32:20 +00:00
|
|
|
w->widget[a].right = length / 3;
|
2007-01-24 02:36:55 +00:00
|
|
|
w->widget[b].right = w->widget[a].right * 2;
|
|
|
|
|
2007-01-24 14:32:20 +00:00
|
|
|
w->widget[a].right += offset;
|
|
|
|
w->widget[b].right += offset;
|
|
|
|
|
2007-01-24 02:36:55 +00:00
|
|
|
/* Now the right side of the buttons are set. We will now set the left sides next to them */
|
|
|
|
w->widget[b].left = w->widget[a].right + 1;
|
|
|
|
w->widget[c].left = w->widget[b].right + 1;
|
|
|
|
}
|
|
|
|
|
2007-01-24 14:32:20 +00:00
|
|
|
/** Evenly distribute some widgets when resizing horizontally (often a button row)
|
|
|
|
* When only two arguments are given, the widgets are presumed to be on a line and only the ends are given
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param w Window to modify
|
2007-01-24 14:32:20 +00:00
|
|
|
* @param left The leftmost widget to resize
|
|
|
|
* @param right The rightmost widget to resize. Since right side of it is used, remember to set it to RESIZE_RIGHT
|
|
|
|
*/
|
2007-01-24 02:36:55 +00:00
|
|
|
void ResizeButtons(Window *w, byte left, byte right)
|
|
|
|
{
|
2007-01-24 14:32:20 +00:00
|
|
|
int16 num_widgets = right - left + 1;
|
|
|
|
|
|
|
|
if (num_widgets < 2) NOT_REACHED();
|
|
|
|
|
|
|
|
switch (num_widgets) {
|
|
|
|
case 2: ResizeWidgets(w, left, right); break;
|
|
|
|
case 3: ResizeWidgets(w, left, left + 1, right); break;
|
|
|
|
default: {
|
|
|
|
/* Looks like we got more than 3 widgets to resize
|
|
|
|
* Now we will find the middle of the space desinated for the widgets
|
|
|
|
* and place half of the widgets on each side of it and call recursively.
|
|
|
|
* Eventually we will get down to blocks of 2-3 widgets and we got code to handle those cases */
|
|
|
|
int16 offset = w->widget[left].left;
|
|
|
|
int16 length = w->widget[right].right - offset;
|
|
|
|
byte widget = ((num_widgets - 1)/ 2) + left; // rightmost widget of the left side
|
|
|
|
|
|
|
|
/* Now we need to find the middle of the widgets.
|
|
|
|
* It will not always be the middle because if we got an uneven number of widgets,
|
|
|
|
* we will need it to be 2/5, 3/7 and so on
|
|
|
|
* To get this, we multiply with num_widgets/num_widgets. Since we calculate in int, we will get:
|
|
|
|
*
|
|
|
|
* num_widgets/2 (rounding down)
|
|
|
|
* ---------------
|
|
|
|
* num_widgets
|
|
|
|
*
|
|
|
|
* as multiplier to length. We just multiply before divide to that we stay in the int area though */
|
|
|
|
int16 middle = ((length * num_widgets) / (2 * num_widgets)) + offset;
|
|
|
|
|
|
|
|
/* Set left and right on the widgets, that's next to our "middle" */
|
|
|
|
w->widget[widget].right = middle;
|
|
|
|
w->widget[widget + 1].left = w->widget[widget].right + 1;
|
|
|
|
/* Now resize the left and right of the middle */
|
|
|
|
ResizeButtons(w, left, widget);
|
|
|
|
ResizeButtons(w, widget + 1, right);
|
|
|
|
}
|
2007-01-24 02:36:55 +00:00
|
|
|
}
|
|
|
|
}
|