| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
|
| 23 |
#include "maths.h" |
| 24 |
#include "pstypes.h" |
| 25 |
#include "event.h" |
| 26 |
#include "gr.h" |
| 27 |
#include "ui.h" |
| 28 |
#include "mouse.h" |
| 29 |
#include "key.h" |
| 30 |
#include "timer.h" |
| 31 |
|
| 32 |
namespace dcx { |
| 33 |
|
| 34 |
namespace { |
| 35 |
|
| 36 |
static void gr_draw_sunken_border( short x1, short y1, short x2, short y2 ); |
| 37 |
|
| 38 |
void ui_draw_listbox(UI_DIALOG &dlg, UI_GADGET_LISTBOX &listbox) |
| 39 |
{ |
| 40 |
int x, y, stop; |
| 41 |
int w, h; |
| 42 |
|
| 43 |
//if (listbox.current_item<0) |
| 44 |
// listbox.current_item=0; |
| 45 |
//if (listbox.current_item>=listbox.num_items) |
| 46 |
// listbox.current_item = listbox.num_items - 1; |
| 47 |
//if (listbox.first_item<0) |
| 48 |
// listbox.first_item=0; |
| 49 |
//if (listbox.first_item>(listbox.num_items-listbox.num_items_displayed)) |
| 50 |
// listbox.first_item=(listbox.num_items-listbox.num_items_displayed); |
| 51 |
|
| 52 |
#if 0 //ndef OGL |
| 53 |
if ((listbox.status!=1) && !listbox.moved) |
| 54 |
return; |
| 55 |
#endif |
| 56 |
|
| 57 |
gr_set_current_canvas(listbox.canvas); |
| 58 |
auto &canvas = *grd_curcanv; |
| 59 |
|
| 60 |
w = listbox.width; |
| 61 |
h = listbox.height; |
| 62 |
|
| 63 |
gr_rect(canvas, 0, 0, w-1, h-1, CBLACK); |
| 64 |
|
| 65 |
gr_draw_sunken_border(-2, -2, w+listbox.scrollbar->width+4, h+1); |
| 66 |
|
| 67 |
stop = listbox.first_item+listbox.num_items_displayed; |
| 68 |
if (stop>listbox.num_items) stop = listbox.num_items; |
| 69 |
|
| 70 |
x = y = 0; |
| 71 |
|
| 72 |
for (int i = listbox.first_item; i < stop; ++i) |
| 73 |
{ |
| 74 |
const auto color = (i == listbox.current_item) |
| 75 |
? CGREY |
| 76 |
: CBLACK; |
| 77 |
gr_rect(canvas, x, y, listbox.width - 1, y + h - 1, color); |
| 78 |
|
| 79 |
if (i != listbox.current_item) |
| 80 |
{ |
| 81 |
if (listbox.current_item == -1 && dlg.keyboard_focus_gadget == &listbox && i == listbox.first_item) |
| 82 |
gr_set_fontcolor(canvas, CRED, -1); |
| 83 |
else |
| 84 |
gr_set_fontcolor(canvas, CWHITE, -1); |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
if (dlg.keyboard_focus_gadget == &listbox) |
| 89 |
gr_set_fontcolor(canvas, CRED, -1); |
| 90 |
else |
| 91 |
gr_set_fontcolor(canvas, CBLACK, -1); |
| 92 |
} |
| 93 |
const auto &&[w, h] = gr_get_string_size(*canvas.cv_font, listbox.list[i]); |
| 94 |
gr_string(canvas, *canvas.cv_font, x + 2, y, listbox.list[i], w, h); |
| 95 |
y += h; |
| 96 |
} |
| 97 |
|
| 98 |
if (stop < listbox.num_items_displayed - 1) |
| 99 |
{ |
| 100 |
gr_rect(canvas, x, y, listbox.width - 1, listbox.height-1, CBLACK); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static void gr_draw_sunken_border( short x1, short y1, short x2, short y2 ) |
| 105 |
{ |
| 106 |
const uint8_t cgrey = CGREY; |
| 107 |
const uint8_t cbright = CBRIGHT; |
| 108 |
Hline(*grd_curcanv, x1 - 1, x2 + 1, y1 - 1, cgrey); |
| 109 |
Vline(*grd_curcanv, y1 - 1, y2 + 1, x1 - 1, cgrey); |
| 110 |
|
| 111 |
Hline(*grd_curcanv, x1 - 1, x2 + 1, y2 + 1, cbright); |
| 112 |
Vline(*grd_curcanv, y1, y2 + 1, x2 + 1, cbright); |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
std::unique_ptr<UI_GADGET_LISTBOX> ui_add_gadget_listbox(UI_DIALOG &dlg, short x, short y, short w, short h, short numitems, char **list) |
| 118 |
{ |
| 119 |
int i; |
| 120 |
const auto th = gr_get_string_size(*grd_curcanv->cv_font, "*").height; |
| 121 |
|
| 122 |
i = h / th; |
| 123 |
h = i * th; |
| 124 |
|
| 125 |
auto listbox = ui_gadget_add<UI_GADGET_LISTBOX>(dlg, x, y, x + w - 1, y + h - 1); |
| 126 |
listbox->list = list; |
| 127 |
listbox->width = w; |
| 128 |
listbox->height = h; |
| 129 |
listbox->num_items = numitems; |
| 130 |
listbox->num_items_displayed = i; |
| 131 |
listbox->first_item = 0; |
| 132 |
listbox->current_item = -1; |
| 133 |
listbox->last_scrolled = 0; |
| 134 |
listbox->textheight = th; |
| 135 |
listbox->dragging = 0; |
| 136 |
listbox->selected_item = -1; |
| 137 |
listbox->moved = 1; |
| 138 |
listbox->scrollbar = ui_add_gadget_scrollbar(dlg, x + w + 3, y, 0, h, 0, numitems - i, 0, i); |
| 139 |
listbox->scrollbar->parent = listbox.get(); |
| 140 |
return listbox; |
| 141 |
} |
| 142 |
|
| 143 |
window_event_result UI_GADGET_LISTBOX::event_handler(UI_DIALOG &dlg, const d_event &event) |
| 144 |
{ |
| 145 |
int kf; |
| 146 |
if (event.type == event_type::window_draw) |
| 147 |
{ |
| 148 |
ui_draw_listbox(dlg, *this); |
| 149 |
return window_event_result::ignored; |
| 150 |
} |
| 151 |
const auto keypress = (event.type == event_type::key_command) |
| 152 |
? event_key_get(event) |
| 153 |
: 0u; |
| 154 |
selected_item = -1; |
| 155 |
|
| 156 |
moved = 0; |
| 157 |
|
| 158 |
if (num_items < 1 ) { |
| 159 |
current_item = -1; |
| 160 |
first_item = 0; |
| 161 |
old_current_item = current_item; |
| 162 |
old_first_item = first_item; |
| 163 |
|
| 164 |
if (dlg.keyboard_focus_gadget == this) |
| 165 |
{ |
| 166 |
dlg.keyboard_focus_gadget = &ui_gadget_get_next(*this); |
| 167 |
} |
| 168 |
|
| 169 |
return window_event_result::ignored; |
| 170 |
} |
| 171 |
|
| 172 |
old_current_item = current_item; |
| 173 |
old_first_item = first_item; |
| 174 |
|
| 175 |
|
| 176 |
if (GADGET_PRESSED(scrollbar.get())) |
| 177 |
{ |
| 178 |
moved = 1; |
| 179 |
|
| 180 |
first_item = scrollbar->position; |
| 181 |
|
| 182 |
if (current_item<first_item) |
| 183 |
current_item = first_item; |
| 184 |
|
| 185 |
if (current_item>(first_item+num_items_displayed-1)) |
| 186 |
current_item = first_item + num_items_displayed-1; |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
if (B1_JUST_RELEASED) |
| 191 |
dragging = 0; |
| 192 |
|
| 193 |
window_event_result rval = window_event_result::ignored; |
| 194 |
if (B1_JUST_PRESSED && ui_mouse_on_gadget(*this)) |
| 195 |
{ |
| 196 |
dragging = 1; |
| 197 |
rval = window_event_result::handled; |
| 198 |
} |
| 199 |
|
| 200 |
if (dlg.keyboard_focus_gadget == this) |
| 201 |
{ |
| 202 |
if (keypress==KEY_ENTER) { |
| 203 |
selected_item = current_item; |
| 204 |
rval = window_event_result::handled; |
| 205 |
} |
| 206 |
|
| 207 |
kf = 0; |
| 208 |
|
| 209 |
switch(keypress) |
| 210 |
{ |
| 211 |
case (KEY_UP): |
| 212 |
current_item--; |
| 213 |
kf = 1; |
| 214 |
break; |
| 215 |
case (KEY_DOWN): |
| 216 |
current_item++; |
| 217 |
kf = 1; |
| 218 |
break; |
| 219 |
case (KEY_HOME): |
| 220 |
current_item=0; |
| 221 |
kf = 1; |
| 222 |
break; |
| 223 |
case (KEY_END): |
| 224 |
current_item=num_items-1; |
| 225 |
kf = 1; |
| 226 |
break; |
| 227 |
case (KEY_PAGEUP): |
| 228 |
current_item -= num_items_displayed; |
| 229 |
kf = 1; |
| 230 |
break; |
| 231 |
case (KEY_PAGEDOWN): |
| 232 |
current_item += num_items_displayed; |
| 233 |
kf = 1; |
| 234 |
break; |
| 235 |
} |
| 236 |
|
| 237 |
if (kf==1) |
| 238 |
{ |
| 239 |
moved = 1; |
| 240 |
rval = window_event_result::handled; |
| 241 |
|
| 242 |
if (current_item<0) |
| 243 |
current_item=0; |
| 244 |
|
| 245 |
if (current_item>=num_items) |
| 246 |
current_item = num_items-1; |
| 247 |
|
| 248 |
if (current_item<first_item) |
| 249 |
first_item = current_item; |
| 250 |
|
| 251 |
if (current_item>=(first_item+num_items_displayed)) |
| 252 |
first_item = current_item-num_items_displayed+1; |
| 253 |
|
| 254 |
if (num_items <= num_items_displayed ) |
| 255 |
first_item = 0; |
| 256 |
else |
| 257 |
{ |
| 258 |
scrollbar->position = first_item; |
| 259 |
|
| 260 |
scrollbar->fake_position = scrollbar->position-scrollbar->start; |
| 261 |
scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size; |
| 262 |
scrollbar->fake_position /= (scrollbar->stop-scrollbar->start); |
| 263 |
|
| 264 |
if (scrollbar->fake_position<0) |
| 265 |
{ |
| 266 |
scrollbar->fake_position = 0; |
| 267 |
} |
| 268 |
if (scrollbar->fake_position > (scrollbar->height-scrollbar->fake_size)) |
| 269 |
{ |
| 270 |
scrollbar->fake_position = (scrollbar->height-scrollbar->fake_size); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
if (selected_gadget == this) |
| 278 |
{ |
| 279 |
if (dragging) |
| 280 |
{ |
| 281 |
const auto [x, y, z] = mouse_get_pos(); |
| 282 |
const auto mitem = (y < y1) |
| 283 |
? -1 |
| 284 |
: (y - y1) / textheight; |
| 285 |
|
| 286 |
if ((mitem < 0) && (timer_query() > last_scrolled + 1)) |
| 287 |
{ |
| 288 |
current_item--; |
| 289 |
last_scrolled = timer_query(); |
| 290 |
moved = 1; |
| 291 |
} |
| 292 |
|
| 293 |
if ((mitem >= num_items_displayed) && |
| 294 |
(timer_query() > last_scrolled + 1)) |
| 295 |
{ |
| 296 |
current_item++; |
| 297 |
last_scrolled = timer_query(); |
| 298 |
moved = 1; |
| 299 |
} |
| 300 |
|
| 301 |
if ((mitem>=0) && (mitem<num_items_displayed)) |
| 302 |
{ |
| 303 |
current_item = mitem+first_item; |
| 304 |
moved=1; |
| 305 |
} |
| 306 |
|
| 307 |
if (current_item <0 ) |
| 308 |
current_item = 0; |
| 309 |
|
| 310 |
if (current_item >= num_items ) |
| 311 |
current_item = num_items-1; |
| 312 |
|
| 313 |
if (current_item<first_item) |
| 314 |
first_item = current_item; |
| 315 |
|
| 316 |
if (current_item>=(first_item+num_items_displayed)) |
| 317 |
first_item = current_item-num_items_displayed+1; |
| 318 |
|
| 319 |
if (num_items <= num_items_displayed ) |
| 320 |
first_item = 0; |
| 321 |
else |
| 322 |
{ |
| 323 |
scrollbar->position = first_item; |
| 324 |
|
| 325 |
scrollbar->fake_position = scrollbar->position-scrollbar->start; |
| 326 |
scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size; |
| 327 |
scrollbar->fake_position /= (scrollbar->stop-scrollbar->start); |
| 328 |
|
| 329 |
if (scrollbar->fake_position<0) |
| 330 |
{ |
| 331 |
scrollbar->fake_position = 0; |
| 332 |
} |
| 333 |
if (scrollbar->fake_position > (scrollbar->height-scrollbar->fake_size)) |
| 334 |
{ |
| 335 |
scrollbar->fake_position = (scrollbar->height-scrollbar->fake_size); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
if (B1_DOUBLE_CLICKED ) |
| 342 |
{ |
| 343 |
selected_item = current_item; |
| 344 |
rval = window_event_result::handled; |
| 345 |
} |
| 346 |
|
| 347 |
} |
| 348 |
if (moved || (selected_item > 0)) |
| 349 |
{ |
| 350 |
rval = ui_gadget_send_event(dlg, (selected_item > 0) ? event_type::ui_listbox_selected : event_type::ui_listbox_moved, *this); |
| 351 |
if (rval == window_event_result::ignored) |
| 352 |
rval = window_event_result::handled; |
| 353 |
} |
| 354 |
|
| 355 |
return rval; |
| 356 |
} |
| 357 |
|
| 358 |
void ui_listbox_change(UI_DIALOG *, UI_GADGET_LISTBOX *listbox, uint_fast32_t numitems, const char *const *list) |
| 359 |
{ |
| 360 |
int stop, start; |
| 361 |
UI_GADGET_SCROLLBAR * scrollbar; |
| 362 |
|
| 363 |
listbox->list = list; |
| 364 |
listbox->num_items = numitems; |
| 365 |
listbox->first_item = 0; |
| 366 |
listbox->current_item = -1; |
| 367 |
listbox->last_scrolled = timer_query(); |
| 368 |
listbox->dragging = 0; |
| 369 |
listbox->selected_item = -1; |
| 370 |
listbox->first_item = 0; |
| 371 |
listbox->current_item = listbox->old_current_item = 0; |
| 372 |
listbox->moved = 0; |
| 373 |
|
| 374 |
scrollbar = listbox->scrollbar.get(); |
| 375 |
|
| 376 |
start=0; |
| 377 |
stop= numitems - listbox->num_items_displayed; |
| 378 |
|
| 379 |
if (stop < start) stop = start; |
| 380 |
|
| 381 |
scrollbar->horz = 0; |
| 382 |
scrollbar->start = start; |
| 383 |
scrollbar->stop = stop; |
| 384 |
scrollbar->fake_length = scrollbar->height; |
| 385 |
scrollbar->fake_position = 0; |
| 386 |
if (stop!=start) |
| 387 |
scrollbar->fake_size = (listbox->num_items_displayed * scrollbar->height)/(stop-start+1+listbox->num_items_displayed); |
| 388 |
else |
| 389 |
scrollbar->fake_size = scrollbar->height; |
| 390 |
|
| 391 |
if (scrollbar->fake_size < 7) scrollbar->fake_size = 7; |
| 392 |
scrollbar->dragging = 0; |
| 393 |
scrollbar->moved=0; |
| 394 |
|
| 395 |
|
| 396 |
} |
| 397 |
|
| 398 |
} |