| 85 | | |
|---|
| 86 | | /* ======================= EVENTS ======================== */ |
|---|
| 87 | | |
|---|
| 88 | | /* List of event listeners--Helper class for event_target and the event loop */ |
|---|
| 89 | | struct listener_list |
|---|
| 90 | | { |
|---|
| 91 | | event_listener *listener; |
|---|
| 92 | | struct listener_list *next; |
|---|
| 93 | | }; |
|---|
| 94 | | |
|---|
| 95 | | |
|---|
| 96 | | void stop_event_loop() |
|---|
| 97 | | { |
|---|
| 98 | | event_type stop = { EVT_STOP, 0, 0, 0, 0 }; |
|---|
| 99 | | |
|---|
| 100 | | /* Stop right away! */ |
|---|
| 101 | | Term_event_push(&stop); |
|---|
| 102 | | } |
|---|
| 103 | | |
|---|
| 104 | | /* |
|---|
| 105 | | * Primitive event loop. |
|---|
| 106 | | * - target = the event target |
|---|
| 107 | | * - forever - if false, stop at first unhandled event. Otherwise, stop only |
|---|
| 108 | | * for STOP events |
|---|
| 109 | | * - start - optional initial event that allows you to prime the loop without |
|---|
| 110 | | * pushing the event queue. |
|---|
| 111 | | * Returns: |
|---|
| 112 | | * EVT_STOP - the loop was halted. |
|---|
| 113 | | * EVT_AGAIN - start was not handled, and forever is false |
|---|
| 114 | | * The first unhandled event - forever is false. |
|---|
| 115 | | */ |
|---|
| 116 | | event_type run_event_loop(event_target * target, bool forever, const event_type *start) |
|---|
| 117 | | { |
|---|
| 118 | | event_type ke = EVENT_EMPTY; |
|---|
| 119 | | bool handled = TRUE; |
|---|
| 120 | | |
|---|
| 121 | | while (forever || handled) |
|---|
| 122 | | { |
|---|
| 123 | | listener_list *list = target->observers; |
|---|
| 124 | | handled = FALSE; |
|---|
| 125 | | |
|---|
| 126 | | if (start) ke = *start; |
|---|
| 127 | | else ke = inkey_ex(); |
|---|
| 128 | | |
|---|
| 129 | | if (ke.type == EVT_STOP) |
|---|
| 130 | | break; |
|---|
| 131 | | |
|---|
| 132 | | if (ke.type & target->self.events.evt_flags) |
|---|
| 133 | | handled = target->self.handler(target->self.object, &ke); |
|---|
| 134 | | |
|---|
| 135 | | if (!target->is_modal) |
|---|
| 136 | | { |
|---|
| 137 | | while (list && !handled) |
|---|
| 138 | | { |
|---|
| 139 | | if (ke.type & list->listener->events.evt_flags) |
|---|
| 140 | | handled = list->listener->handler(list->listener->object, &ke); |
|---|
| 141 | | |
|---|
| 142 | | list = list->next; |
|---|
| 143 | | } |
|---|
| 144 | | } |
|---|
| 145 | | |
|---|
| 146 | | if (handled) start = NULL; |
|---|
| 147 | | } |
|---|
| 148 | | |
|---|
| 149 | | if (start) |
|---|
| 150 | | { |
|---|
| 151 | | ke.type = EVT_AGAIN; |
|---|
| 152 | | ke.key = '\xff'; |
|---|
| 153 | | } |
|---|
| 154 | | |
|---|
| 155 | | return ke; |
|---|
| 156 | | } |
|---|
| 157 | | |
|---|
| 158 | | void add_listener(event_target * target, event_listener * observer) |
|---|
| 159 | | { |
|---|
| 160 | | listener_list *link; |
|---|
| 161 | | |
|---|
| 162 | | link = ZNEW(listener_list); |
|---|
| 163 | | link->listener = observer; |
|---|
| 164 | | link->next = target->observers; |
|---|
| 165 | | target->observers = link; |
|---|
| 166 | | } |
|---|
| 167 | | |
|---|
| 168 | | void remove_listener(event_target * target, event_listener * observer) |
|---|
| 169 | | { |
|---|
| 170 | | listener_list *cur = target->observers; |
|---|
| 171 | | listener_list **prev = &target->observers; |
|---|
| 172 | | |
|---|
| 173 | | while (cur) |
|---|
| 174 | | { |
|---|
| 175 | | if (cur->listener == observer) |
|---|
| 176 | | { |
|---|
| 177 | | *prev = cur->next; |
|---|
| 178 | | FREE(cur); |
|---|
| 179 | | break; |
|---|
| 180 | | } |
|---|
| 181 | | } |
|---|
| 182 | | |
|---|
| 183 | | bell("remove_listener: no such observer"); |
|---|
| 184 | | } |
|---|
| 185 | | |
|---|
| 186 | | |
|---|
| 187 | | |
|---|
| 188 | | /* ======================= MN_EVT HELPER FUNCTIONS ====================== */ |
|---|
| 189 | | |
|---|
| 190 | | /* Display an event, with possible preference overrides */ |
|---|
| 191 | | static void display_event_aux(event_action *event, int menu_id, byte color, int row, int col, int wid) |
|---|
| 192 | | { |
|---|
| 193 | | /* TODO: add preference support */ |
|---|
| 194 | | /* TODO: wizard mode should show more data */ |
|---|
| 195 | | Term_erase(col, row, wid); |
|---|
| 196 | | |
|---|
| 197 | | if (event->name) |
|---|
| 198 | | Term_putstr(col, row, wid, color, event->name); |
|---|
| 199 | | } |
|---|
| 200 | | |
|---|
| 201 | | static void display_event(menu_type *menu, int oid, bool cursor, int row, int col, int width) |
|---|
| 202 | | { |
|---|
| 203 | | event_action *evts = (event_action *)menu->menu_data; |
|---|
| 204 | | byte color = curs_attrs[CURS_KNOWN][0 != cursor]; |
|---|
| 205 | | |
|---|
| 206 | | display_event_aux(&evts[oid], menu->target.self.object_id, color, row, col, |
|---|
| 207 | | width); |
|---|
| 208 | | } |
|---|
| 209 | | |
|---|
| 210 | | /* act on selection only */ |
|---|
| 211 | | /* Return: true if handled. */ |
|---|
| 212 | | static bool handle_menu_item_event(char cmd, void *db, int oid) |
|---|
| 213 | | { |
|---|
| 214 | | event_action *evt = &((event_action *)db)[oid]; |
|---|
| 215 | | |
|---|
| 216 | | if (cmd == '\xff' && evt->action) |
|---|
| 217 | | { |
|---|
| 218 | | evt->action(evt->data, evt->name); |
|---|
| 219 | | return TRUE; |
|---|
| 220 | | } |
|---|
| 221 | | else if (cmd == '\xff') |
|---|
| 222 | | { |
|---|
| 223 | | return TRUE; |
|---|
| 224 | | } |
|---|
| 225 | | |
|---|
| 226 | | return FALSE; |
|---|
| 227 | | } |
|---|
| 228 | | |
|---|
| 229 | | static int valid_menu_event(menu_type *menu, int oid) |
|---|
| 230 | | { |
|---|
| 231 | | event_action *evts = (event_action *)menu->menu_data; |
|---|
| 232 | | return (NULL != evts[oid].name); |
|---|
| 233 | | } |
|---|
| 234 | | |
|---|
| 235 | | /* Virtual function table for action_events */ |
|---|
| 236 | | static const menu_iter menu_iter_event = |
|---|
| 237 | | { |
|---|
| 238 | | MN_EVT, |
|---|
| 239 | | 0, |
|---|
| 240 | | valid_menu_event, |
|---|
| 241 | | display_event, |
|---|
| 242 | | handle_menu_item_event |
|---|
| 243 | | }; |
|---|
| 244 | | |
|---|
| 245 | | |
|---|
| 246 | | /*======================= MN_ACT HELPER FUNCTIONS ====================== */ |
|---|
| 247 | | |
|---|
| 248 | | static char tag_menu_item(menu_type *menu, int oid) |
|---|
| 249 | | { |
|---|
| 250 | | menu_item *items = (menu_item *)menu->menu_data; |
|---|
| 251 | | return items[oid].sel; |
|---|
| 252 | | } |
|---|
| 253 | | |
|---|
| 254 | | static void display_menu_item(menu_type *menu, int oid, bool cursor, int row, int col, int width) |
|---|
| 255 | | { |
|---|
| 256 | | menu_item *items = (menu_item *)menu->menu_data; |
|---|
| 257 | | byte color = curs_attrs[!(items[oid].flags & (MN_GRAYED))][0 != cursor]; |
|---|
| 258 | | |
|---|
| 259 | | display_event_aux(&items[oid].evt, menu->target.self.object_id, color, row, |
|---|
| 260 | | col, width); |
|---|
| 261 | | } |
|---|
| 262 | | |
|---|
| 263 | | /* act on selection only */ |
|---|
| 264 | | static bool handle_menu_item(char cmd, void *db, int oid) |
|---|
| 265 | | { |
|---|
| 266 | | if (cmd == '\xff') |
|---|
| 267 | | { |
|---|
| 268 | | menu_item *item = &((menu_item *)db)[oid]; |
|---|
| 269 | | |
|---|
| 270 | | if (item->flags & MN_DISABLED) |
|---|
| 271 | | return TRUE; |
|---|
| 272 | | if (item->evt.action) |
|---|
| 273 | | item->evt.action(item->evt.data, item->evt.name); |
|---|
| 274 | | if (item->flags & MN_SELECTABLE) |
|---|
| 275 | | item->flags ^= MN_SELECTED; |
|---|
| 276 | | |
|---|
| 277 | | return TRUE; |
|---|
| 278 | | } |
|---|
| 279 | | |
|---|
| 280 | | return FALSE; |
|---|
| 281 | | } |
|---|
| 282 | | |
|---|
| 283 | | static int valid_menu_item(menu_type *menu, int oid) |
|---|
| 284 | | { |
|---|
| 285 | | menu_item *items = (menu_item *)menu->menu_data; |
|---|
| 286 | | |
|---|
| 287 | | if (items[oid].flags & MN_HIDDEN) |
|---|
| 288 | | return 2; |
|---|
| 289 | | |
|---|
| 290 | | return (NULL != items[oid].evt.name); |
|---|
| 291 | | } |
|---|
| 292 | | |
|---|
| 293 | | /* Virtual function table for menu items */ |
|---|
| 294 | | static const menu_iter menu_iter_item = |
|---|
| 295 | | { |
|---|
| 296 | | MN_ACT, |
|---|
| 297 | | tag_menu_item, |
|---|
| 298 | | valid_menu_item, |
|---|
| 299 | | display_menu_item, |
|---|
| 300 | | handle_menu_item |
|---|
| 301 | | }; |
|---|
| 302 | | |
|---|
| 303 | | /* Simple strings - display and selection only */ |
|---|
| 304 | | static void display_string(menu_type *menu, int oid, bool cursor, |
|---|
| 305 | | int row, int col, int width) |
|---|
| 306 | | { |
|---|
| 307 | | const char **items = (const char **)menu->menu_data; |
|---|
| 308 | | byte color = curs_attrs[CURS_KNOWN][0 != cursor]; |
|---|
| 309 | | Term_putstr(col, row, width, color, items[oid]); |
|---|
| 310 | | } |
|---|
| 311 | | |
|---|
| 312 | | /* Virtual function table for displaying arrays of strings */ |
|---|
| 313 | | static const menu_iter menu_iter_string = |
|---|
| 314 | | { MN_STRING, 0, 0, display_string, 0 }; |
|---|
| 315 | | |
|---|
| 316 | | |
|---|
| 317 | | |
|---|
| 318 | | |
|---|
| 319 | | /* ================== SKINS ============== */ |
|---|
| 320 | | |
|---|
| 321 | | |
|---|
| 322 | | /* Scrolling menu */ |
|---|
| 323 | | /* Find the position of a cursor given a screen address */ |
|---|
| 324 | | static int scrolling_get_cursor(int row, int col, int n, int top, region *loc) |
|---|
| 325 | | { |
|---|
| 326 | | int cursor = row - loc->row + top; |
|---|
| 327 | | if (cursor >= n) cursor = n - 1; |
|---|
| 328 | | |
|---|
| 329 | | return cursor; |
|---|
| 330 | | } |
|---|
| 331 | | |
|---|
| 332 | | |
|---|
| 333 | | /* Display current view of a skin */ |
|---|
| 334 | | static void display_scrolling(menu_type *menu, int cursor, int *top, region *loc) |
|---|
| 335 | | { |
|---|
| 336 | | int col = loc->col; |
|---|
| 337 | | int row = loc->row; |
|---|
| 338 | | int rows_per_page = loc->page_rows; |
|---|
| 339 | | int n = menu->filter_count; |
|---|
| 340 | | int i; |
|---|
| 341 | | |
|---|
| 342 | | /* Keep a certain distance from the top when possible */ |
|---|
| 343 | | if ((cursor <= *top) && (*top > 0)) |
|---|
| 344 | | *top = cursor - jumpscroll - 1; |
|---|
| 345 | | |
|---|
| 346 | | /* Keep a certain distance from the bottom when possible */ |
|---|
| 347 | | if (cursor >= *top + (rows_per_page - 1)) |
|---|
| 348 | | *top = cursor - (rows_per_page - 1) + 1 + jumpscroll; |
|---|
| 349 | | |
|---|
| 350 | | /* Limit the top to legal places */ |
|---|
| 351 | | *top = MIN(*top, n - rows_per_page); |
|---|
| 352 | | *top = MAX(*top, 0); |
|---|
| 353 | | |
|---|
| 354 | | |
|---|
| 355 | | for (i = 0; i < rows_per_page && i < n; i++) |
|---|
| 356 | | { |
|---|
| 357 | | bool is_curs = (i == cursor - *top); |
|---|
| 358 | | display_menu_row(menu, i + *top, *top, is_curs, row + i, col, |
|---|
| 359 | | loc->width); |
|---|
| 360 | | } |
|---|
| 361 | | |
|---|
| 362 | | if (menu->cursor >= 0) |
|---|
| 363 | | Term_gotoxy(col, row + cursor - *top); |
|---|
| 364 | | } |
|---|
| 365 | | |
|---|
| 366 | | static char scroll_get_tag(menu_type *menu, int pos) |
|---|
| 367 | | { |
|---|
| 368 | | if (menu->selections) |
|---|
| 369 | | return menu->selections[pos - menu->top]; |
|---|
| 370 | | |
|---|
| 371 | | return 0; |
|---|
| 372 | | } |
|---|
| 373 | | |
|---|
| 374 | | /* Virtual function table for scrollable menu skin */ |
|---|
| 375 | | static const menu_skin scroll_skin = |
|---|
| 376 | | { |
|---|
| 377 | | MN_SCROLL, |
|---|
| 378 | | scrolling_get_cursor, |
|---|
| 379 | | display_scrolling, |
|---|
| 380 | | scroll_get_tag |
|---|
| 381 | | }; |
|---|
| 382 | | |
|---|
| 383 | | |
|---|
| 384 | | /* Multi-column menu */ |
|---|
| 385 | | /* Find the position of a cursor given a screen address */ |
|---|
| 386 | | static int columns_get_cursor(int row, int col, int n, int top, region *loc) |
|---|
| 387 | | { |
|---|
| 388 | | int rows_per_page = loc->page_rows; |
|---|
| 389 | | int colw = loc->width / (n + rows_per_page - 1) / rows_per_page; |
|---|
| 390 | | int cursor = row + rows_per_page * (col - loc->col) / colw; |
|---|
| 391 | | |
|---|
| 392 | | if (cursor < 0) cursor = 0; /* assert: This should never happen */ |
|---|
| 393 | | if (cursor >= n) cursor = n - 1; |
|---|
| 394 | | |
|---|
| 395 | | return cursor; |
|---|
| 396 | | } |
|---|
| 397 | | |
|---|
| 398 | | void display_columns(menu_type *menu, int cursor, int *top, region *loc) |
|---|
| 399 | | { |
|---|
| 400 | | int c, r; |
|---|
| 401 | | int w, h; |
|---|
| 402 | | int n = menu->filter_count; |
|---|
| 403 | | int col = loc->col; |
|---|
| 404 | | int row = loc->row; |
|---|
| 405 | | int rows_per_page = loc->page_rows; |
|---|
| 406 | | int cols = (n + rows_per_page - 1) / rows_per_page; |
|---|
| 407 | | int colw = menu_width; |
|---|
| 408 | | |
|---|
| 409 | | Term_get_size(&w, &h); |
|---|
| 410 | | |
|---|
| 411 | | if ((colw * cols) > (w - col)) |
|---|
| 412 | | colw = (w - col) / cols; |
|---|
| 413 | | |
|---|
| 414 | | for (c = 0; c < cols; c++) |
|---|
| 415 | | { |
|---|
| 416 | | for (r = 0; r < rows_per_page; r++) |
|---|
| 417 | | { |
|---|
| 418 | | int pos = c * rows_per_page + r; |
|---|
| 419 | | bool is_cursor = (pos == cursor); |
|---|
| 420 | | display_menu_row(menu, pos, 0, is_cursor, row + r, col + c * colw, |
|---|
| 421 | | colw); |
|---|
| 422 | | } |
|---|
| 423 | | } |
|---|
| 424 | | } |
|---|
| 425 | | |
|---|
| 426 | | static char column_get_tag(menu_type *menu, int pos) |
|---|
| 427 | | { |
|---|
| 428 | | if (menu->selections) |
|---|
| 429 | | return menu->selections[pos]; |
|---|
| 430 | | |
|---|
| 431 | | return 0; |
|---|
| 432 | | } |
|---|
| 433 | | |
|---|
| 434 | | /* Virtual function table for multi-column menu skin */ |
|---|
| 435 | | static const menu_skin column_skin = |
|---|
| 436 | | { |
|---|
| 437 | | MN_COLUMNS, |
|---|
| 438 | | columns_get_cursor, |
|---|
| 439 | | display_columns, |
|---|
| 440 | | column_get_tag |
|---|
| 441 | | }; |
|---|
| 442 | | |
|---|
| 443 | | /* ================== IMPLICIT MENU FOR KEY SELECTION ================== */ |
|---|
| 444 | | |
|---|
| 445 | | static void display_nothing(menu_type *menu, int cursor, int *top, region *loc) |
|---|
| 446 | | { |
|---|
| 447 | | } |
|---|
| 448 | | |
|---|
| 449 | | static int no_cursor(int row, int col, int n, int top, region *loc) |
|---|
| 450 | | { |
|---|
| 451 | | return -1; |
|---|
| 452 | | } |
|---|
| 453 | | |
|---|
| 454 | | static const menu_skin key_select_skin = |
|---|
| 455 | | { |
|---|
| 456 | | MN_KEY_ONLY, |
|---|
| 457 | | no_cursor, |
|---|
| 458 | | display_nothing, |
|---|
| 459 | | }; |
|---|
| 460 | | |
|---|
| 461 | | |
|---|
| 462 | | /* ================== GENERIC HELPER FUNCTIONS ============== */ |
|---|
| 463 | | |
|---|
| 464 | | static bool is_valid_row(menu_type *menu, int cursor) |
|---|
| 465 | | { |
|---|
| 466 | | int oid = cursor; |
|---|
| 467 | | |
|---|
| 468 | | if (cursor < 0 || cursor >= menu->filter_count) |
|---|
| 469 | | return FALSE; |
|---|
| 470 | | |
|---|
| 471 | | if (menu->object_list) |
|---|
| 472 | | oid = menu->object_list[cursor]; |
|---|
| 473 | | |
|---|
| 474 | | if (!menu->row_funcs->valid_row) |
|---|
| 475 | | return TRUE; |
|---|
| 476 | | |
|---|
| 477 | | return menu->row_funcs->valid_row(menu, oid); |
|---|
| 478 | | } |
|---|
| 479 | | |
|---|
| 480 | | /* |
|---|
| 481 | | * Return a new position in the menu based on the key |
|---|
| 482 | | * pressed and the flags and various handler functions. |
|---|
| 483 | | */ |
|---|
| 484 | | static int get_cursor_key(menu_type *menu, int top, char key) |
|---|
| 485 | | { |
|---|
| 486 | | int i; |
|---|
| 487 | | int n = menu->filter_count; |
|---|
| 488 | | |
|---|
| 489 | | if (menu->flags & MN_CASELESS_TAGS) |
|---|
| 490 | | key = toupper((unsigned char) key); |
|---|
| 491 | | |
|---|
| 492 | | if (menu->flags & MN_NO_TAGS) |
|---|
| 493 | | { |
|---|
| 494 | | return -1; |
|---|
| 495 | | } |
|---|
| 496 | | else if (menu->flags & MN_REL_TAGS) |
|---|
| 497 | | { |
|---|
| 498 | | for (i = 0; i < n; i++) |
|---|
| 499 | | { |
|---|
| 500 | | char c = menu->skin->get_tag(menu, i); |
|---|
| 501 | | |
|---|
| 502 | | if ((menu->flags & MN_CASELESS_TAGS) && c) |
|---|
| 503 | | c = toupper((unsigned char) c); |
|---|
| 504 | | |
|---|
| 505 | | if (c && c == key) |
|---|
| 506 | | return i + menu->top; |
|---|
| 507 | | } |
|---|
| 508 | | } |
|---|
| 509 | | else if (!(menu->flags & MN_PVT_TAGS) && menu->selections) |
|---|
| 510 | | { |
|---|
| 511 | | for (i = 0; menu->selections[i]; i++) |
|---|
| 512 | | { |
|---|
| 513 | | char c = menu->selections[i]; |
|---|
| 514 | | |
|---|
| 515 | | if (menu->flags & MN_CASELESS_TAGS) |
|---|
| 516 | | c = toupper((unsigned char) c); |
|---|
| 517 | | |
|---|
| 518 | | if (c == key) |
|---|
| 519 | | return i; |
|---|
| 520 | | } |
|---|
| 521 | | } |
|---|
| 522 | | else if (menu->row_funcs->get_tag) |
|---|
| 523 | | { |
|---|
| 524 | | for (i = 0; i < n; i++) |
|---|
| 525 | | { |
|---|
| 526 | | int oid = menu->object_list ? menu->object_list[i] : i; |
|---|
| 527 | | char c = menu->row_funcs->get_tag(menu, oid); |
|---|
| 528 | | |
|---|
| 529 | | if ((menu->flags & MN_CASELESS_TAGS) && c) |
|---|
| 530 | | c = toupper((unsigned char) c); |
|---|
| 531 | | |
|---|
| 532 | | if (c && c == key) |
|---|
| 533 | | return i; |
|---|
| 534 | | } |
|---|
| 535 | | } |
|---|
| 536 | | |
|---|
| 537 | | return -1; |
|---|
| 538 | | } |
|---|
| 539 | | |
|---|
| 540 | | /* |
|---|
| 541 | | * Event handler wrapper function |
|---|
| 542 | | * Filters unhandled keys & conditions |
|---|
| 543 | | */ |
|---|
| 544 | | static bool handle_menu_key(char cmd, menu_type *menu, int cursor) |
|---|
| 545 | | { |
|---|
| 546 | | int oid = cursor; |
|---|
| 547 | | int flags = menu->flags; |
|---|
| 548 | | |
|---|
| 549 | | if (menu->object_list) oid = menu->object_list[cursor]; |
|---|
| 550 | | if (flags & MN_NO_ACT) return FALSE; |
|---|
| 551 | | |
|---|
| 552 | | if (cmd == ESCAPE) return FALSE; |
|---|
| 553 | | if (!cmd == '\xff' && (!menu->cmd_keys || !strchr(menu->cmd_keys, cmd))) |
|---|
| 554 | | return FALSE; |
|---|
| 555 | | |
|---|
| 556 | | if (menu->row_funcs->row_handler && |
|---|
| 557 | | menu->row_funcs->row_handler(cmd, (void *)menu->menu_data, oid)) |
|---|
| 558 | | { |
|---|
| 559 | | event_type ke; |
|---|
| 560 | | ke.type = EVT_SELECT; |
|---|
| 561 | | ke.key = cmd; |
|---|
| 562 | | ke.index = cursor; |
|---|
| 563 | | Term_event_push(&ke); |
|---|
| 564 | | return TRUE; |
|---|
| 565 | | } |
|---|
| 566 | | |
|---|
| 567 | | return FALSE; |
|---|
| 568 | | } |
|---|
| 569 | | |
|---|
| 570 | | /* Modal display of menu */ |
|---|
| 571 | | static void display_menu_row(menu_type *menu, int pos, int top, |
|---|
| 572 | | bool cursor, int row, int col, int width) |
|---|
| 573 | | { |
|---|
| 574 | | int flags = menu->flags; |
|---|
| 575 | | char sel = 0; |
|---|
| 576 | | int oid = pos; |
|---|
| 577 | | |
|---|
| 578 | | if (menu->object_list) |
|---|
| 579 | | oid = menu->object_list[oid]; |
|---|
| 580 | | |
|---|
| 581 | | if (menu->row_funcs->valid_row && menu->row_funcs->valid_row(menu, oid) == 2) |
|---|
| 582 | | return; |
|---|
| 583 | | |
|---|
| 584 | | if (!(flags & MN_NO_TAGS)) |
|---|
| 585 | | { |
|---|
| 586 | | if (flags & MN_REL_TAGS) |
|---|
| 587 | | sel = menu->skin->get_tag(menu, pos); |
|---|
| 588 | | else if (menu->selections && !(flags & MN_PVT_TAGS)) |
|---|
| 589 | | sel = menu->selections[pos]; |
|---|
| 590 | | else if (menu->row_funcs->get_tag) |
|---|
| 591 | | sel = menu->row_funcs->get_tag(menu, oid); |
|---|
| 592 | | } |
|---|
| 593 | | |
|---|
| 594 | | if (sel) |
|---|
| 595 | | { |
|---|
| 596 | | /* TODO: CHECK FOR VALID */ |
|---|
| 597 | | byte color = curs_attrs[CURS_KNOWN][0 != (cursor)]; |
|---|
| 598 | | Term_putstr(col, row, 3, color, format("%c) ", sel)); |
|---|
| 599 | | col += 3; |
|---|
| 600 | | width -= 3; |
|---|
| 601 | | } |
|---|
| 602 | | |
|---|
| 603 | | menu->row_funcs->display_row(menu, oid, cursor, row, col, width); |
|---|
| 604 | | } |
|---|
| 605 | | |
|---|
| 606 | | void menu_refresh(menu_type *menu) |
|---|
| 607 | | { |
|---|
| 608 | | region *loc = &menu->boundary; |
|---|
| 609 | | int oid = menu->cursor; |
|---|
| 610 | | |
|---|
| 611 | | if (menu->object_list && menu->cursor >= 0) |
|---|
| 612 | | oid = menu->object_list[oid]; |
|---|
| 613 | | |
|---|
| 614 | | region_erase(&menu->boundary); |
|---|
| 615 | | |
|---|
| 616 | | if (menu->title) |
|---|
| 617 | | Term_putstr(loc->col, loc->row, loc->width, TERM_WHITE, menu->title); |
|---|
| 618 | | |
|---|
| 619 | | if (menu->prompt) |
|---|
| 620 | | Term_putstr(loc->col, loc->row + loc->page_rows - 1, loc->width, |
|---|
| 621 | | TERM_WHITE, menu->prompt); |
|---|
| 622 | | |
|---|
| 623 | | if (menu->browse_hook && oid >= 0) |
|---|
| 624 | | menu->browse_hook(oid, (void*) menu->menu_data, loc); |
|---|
| 625 | | |
|---|
| 626 | | menu->skin->display_list(menu, menu->cursor, &menu->top, &menu->active); |
|---|
| 627 | | } |
|---|
| 628 | | |
|---|
| 629 | | /* The menu event loop */ |
|---|
| 630 | | static bool menu_handle_event(menu_type *menu, const event_type *in) |
|---|
| 631 | | { |
|---|
| 632 | | int n = menu->filter_count; |
|---|
| 633 | | int *cursor = &menu->cursor; |
|---|
| 634 | | event_type out; |
|---|
| 635 | | |
|---|
| 636 | | out.key = '\xff'; |
|---|
| 637 | | |
|---|
| 638 | | if (menu->target.observers) |
|---|
| 639 | | { |
|---|
| 640 | | /* TODO: need a panel dispatcher here, not a generic target */ |
|---|
| 641 | | event_target t = { { 0, 0, 0, 0, { 0 } }, FALSE, 0 /* menu->target.observers */}; |
|---|
| 642 | | t.observers = menu->target.observers; |
|---|
| 643 | |   |
|---|