Files
@ r8373:e7760b9340e5
Branch filter:
Location: cpp/openttd-patchpack/source/src/widgets/dropdown.cpp - annotation
r8373:e7760b9340e5
8.7 KiB
text/x-c
(svn r11939) -Codechange: some type fixes and very initial steps into supporting NDS by default. Based on work by Dominik.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8301:31a3e7192fcd r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8301:31a3e7192fcd r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8294:274ad70e7731 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8294:274ad70e7731 r8294:274ad70e7731 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8301:31a3e7192fcd r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8299:b72ff9844433 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8299:b72ff9844433 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8335:2b3d487c4a5f r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8335:2b3d487c4a5f r8335:2b3d487c4a5f r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8358:1dc4fbe86887 r8284:d769847bf11d r8358:1dc4fbe86887 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8358:1dc4fbe86887 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8299:b72ff9844433 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8299:b72ff9844433 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d | /* $Id$ */
#include "../stdafx.h"
#include "../openttd.h"
#include "../strings_type.h"
#include "../window_gui.h"
#include "../strings_func.h"
#include "../strings_type.h"
#include "../gfx_func.h"
#include "../window_func.h"
#include "../core/math_func.hpp"
#include "dropdown_type.h"
#include "dropdown_func.h"
#include "../table/sprites.h"
#include "table/strings.h"
StringID DropDownListItem::String() const
{
return STR_NULL;
}
StringID DropDownListStringItem::String() const
{
return this->string;
}
StringID DropDownListParamStringItem::String() const
{
for (uint i = 0; i < lengthof(this->decode_params); i++) SetDParam(i, this->decode_params[i]);
return this->string;
}
/**
* Delete all items of a drop down list and the list itself
* @param list List to delete.
*/
static void DeleteDropDownList(DropDownList *list)
{
for (DropDownList::iterator it = list->begin(); it != list->end(); ++it) {
DropDownListItem *item = *it;
delete item;
}
delete list;
}
struct dropdown_d {
WindowClass parent_wnd_class;
WindowNumber parent_wnd_num;
byte parent_button;
DropDownList *list;
byte selected_index;
byte click_delay;
bool drag_mode;
int scrolling;
};
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(dropdown_d));
static const Widget _dropdown_menu_widgets[] = {
{ WWT_PANEL, RESIZE_NONE, 0, 0, 0, 0, 0, 0x0, STR_NULL},
{ WWT_SCROLLBAR, RESIZE_NONE, 0, 0, 0, 0, 0, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
{ WIDGETS_END},
};
static int GetDropDownItem(const Window *w)
{
if (GetWidgetFromPos(w, _cursor.pos.x - w->left, _cursor.pos.y - w->top) < 0) return -1;
int y = _cursor.pos.y - w->top - 2 + w->vscroll.pos * 10;
if (y < 0) return -1;
uint selected_row = y / 10;
const DropDownList *list = WP(w, dropdown_d).list;
if (selected_row >= list->size()) return -1;
for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it, selected_row--) {
if (selected_row == 0) {
const DropDownListItem *item = *it;
if (item->masked || item->String() == STR_NULL) return -1;
return item->result;
}
}
return -1;
}
static void DropDownMenuWndProc(Window *w, WindowEvent *e)
{
switch (e->event) {
case WE_PAINT: {
DrawWindowWidgets(w);
int x = 1;
int y = 2 - w->vscroll.pos * 10;
int sel = WP(w, dropdown_d).selected_index;
int width = w->widget[0].right - 3;
int height = w->widget[0].bottom - 3;
DropDownList *list = WP(w, dropdown_d).list;
for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
if (y >= 0 && y <= height) {
const DropDownListItem *item = *it;
if (item->String() != STR_NULL) {
if (sel == item->result) GfxFillRect(x + 1, y, x + width, y + 9, 0);
DrawStringTruncated(x + 2, y, item->String(), sel == item->result ? TC_WHITE : TC_BLACK, x + width);
if (item->masked) {
GfxFillRect(x, y, x + width, y + 9,
(1 << PALETTE_MODIFIER_GREYOUT) | _colour_gradient[w->widget[0].color][5]
);
}
} else {
int c1 = _colour_gradient[w->widget[0].color][3];
int c2 = _colour_gradient[w->widget[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);
}
}
y += 10;
}
} break;
case WE_CLICK: {
if (e->we.click.widget != 0) break;
int item = GetDropDownItem(w);
if (item >= 0) {
WP(w, dropdown_d).click_delay = 4;
WP(w, dropdown_d).selected_index = item;
SetWindowDirty(w);
}
} break;
case WE_TICK:
if (WP(w, dropdown_d).scrolling == -1) {
w->vscroll.pos = max(0, w->vscroll.pos - 1);
SetWindowDirty(w);
} else if (WP(w, dropdown_d).scrolling == 1) {
w->vscroll.pos = min(w->vscroll.count - w->vscroll.cap, w->vscroll.pos + 1);
SetWindowDirty(w);
}
WP(w, dropdown_d).scrolling = 0;
break;
case WE_MOUSELOOP: {
Window *w2 = FindWindowById(WP(w, dropdown_d).parent_wnd_class, WP(w,dropdown_d).parent_wnd_num);
if (w2 == NULL) {
DeleteWindow(w);
return;
}
if (WP(w, dropdown_d).click_delay != 0 && --WP(w,dropdown_d).click_delay == 0) {
WindowEvent e;
e.event = WE_DROPDOWN_SELECT;
e.we.dropdown.button = WP(w, dropdown_d).parent_button;
e.we.dropdown.index = WP(w, dropdown_d).selected_index;
w2->wndproc(w2, &e);
DeleteWindow(w);
return;
}
if (WP(w, dropdown_d).drag_mode) {
int item = GetDropDownItem(w);
if (!_left_button_clicked) {
WP(w, dropdown_d).drag_mode = false;
if (item < 0) return;
WP(w, dropdown_d).click_delay = 2;
} else {
if (_cursor.pos.y <= w->top + 2) {
/* Cursor is above the list, set scroll up */
WP(w, dropdown_d).scrolling = -1;
return;
} else if (_cursor.pos.y >= w->top + w->height - 2) {
/* Cursor is below list, set scroll down */
WP(w, dropdown_d).scrolling = 1;
return;
}
if (item < 0) return;
}
WP(w, dropdown_d).selected_index = item;
SetWindowDirty(w);
}
} break;
case WE_DESTROY: {
Window *w2 = FindWindowById(WP(w, dropdown_d).parent_wnd_class, WP(w,dropdown_d).parent_wnd_num);
if (w2 != NULL) {
w2->RaiseWidget(WP(w, dropdown_d).parent_button);
w2->InvalidateWidget(WP(w, dropdown_d).parent_button);
}
DeleteDropDownList(WP(w, dropdown_d).list);
} break;
}
}
void ShowDropDownList(Window *w, DropDownList *list, int selected, int button)
{
bool is_dropdown_menu_shown = w->IsWidgetLowered(button);
DeleteWindowById(WC_DROPDOWN_MENU, 0);
if (is_dropdown_menu_shown) {
DeleteDropDownList(list);
return;
}
w->LowerWidget(button);
w->InvalidateWidget(button);
/* Our parent's button widget is used to determine where to place the drop
* down list window. */
const Widget *wi = &w->widget[button];
/* The preferred position is just below the dropdown calling widget */
int top = w->top + wi->bottom + 1;
int height = list->size() * 10 + 4;
/* Check if the status bar is visible, as we don't want to draw over it */
Window *w3 = FindWindowById(WC_STATUS_BAR, 0);
int screen_bottom = w3 == NULL ? _screen.height : w3->top;
bool scroll = false;
/* Check if the dropdown will fully fit below the widget */
if (top + height >= screen_bottom) {
w3 = FindWindowById(WC_MAIN_TOOLBAR, 0);
int 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 > screen_top) {
top = w->top + wi->top - height;
} else {
/* ... and lastly if it won't, enable the scroll bar and fit the
* list in below the widget */
int rows = (screen_bottom - 4 - top) / 10;
height = rows * 10 + 4;
scroll = true;
}
}
Window *dw = AllocateWindow(
w->left + wi->left,
top,
wi->right - wi->left + 1,
height,
DropDownMenuWndProc,
WC_DROPDOWN_MENU,
_dropdown_menu_widgets);
dw->widget[0].color = wi->color;
dw->widget[0].right = wi->right - wi->left;
dw->widget[0].bottom = height - 1;
dw->SetWidgetHiddenState(1, !scroll);
if (scroll) {
/* We're scrolling, so enable the scroll bar and shrink the list by
* the scrollbar's width */
dw->widget[1].color = wi->color;
dw->widget[1].right = dw->widget[0].right;
dw->widget[1].left = dw->widget[1].right - 11;
dw->widget[1].bottom = height - 1;
dw->widget[0].right -= 12;
dw->vscroll.cap = (height - 4) / 10;
dw->vscroll.count = list->size();
}
dw->desc_flags = WDF_DEF_WIDGET;
dw->flags4 &= ~WF_WHITE_BORDER_MASK;
WP(dw, dropdown_d).parent_wnd_class = w->window_class;
WP(dw, dropdown_d).parent_wnd_num = w->window_number;
WP(dw, dropdown_d).parent_button = button;
WP(dw, dropdown_d).list = list;
WP(dw, dropdown_d).selected_index = selected;
WP(dw, dropdown_d).click_delay = 0;
WP(dw, dropdown_d).drag_mode = true;
}
void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask)
{
/* Don't create a new list if we're just closing an existing menu */
if (w->IsWidgetLowered(button)) {
DeleteWindowById(WC_DROPDOWN_MENU, 0);
return;
}
uint result = 0;
DropDownList *list = new DropDownList();
for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
if (!HasBit(hidden_mask, i)) {
list->push_back(new DropDownListStringItem(strings[i], result, HasBit(disabled_mask, i)));
}
result++;
}
/* No entries in the list? */
if (list->size() == 0) {
DeleteDropDownList(list);
return;
}
ShowDropDownList(w, list, selected, button);
}
void HideDropDownMenu(Window *pw)
{
Window **wz;
FOR_ALL_WINDOWS(wz) {
if ((*wz)->window_class != WC_DROPDOWN_MENU) continue;
if (pw->window_class == WP(*wz, dropdown_d).parent_wnd_class &&
pw->window_number == WP(*wz, dropdown_d).parent_wnd_num) {
DeleteWindow(*wz);
break;
}
}
}
|