Debian Codegraph @

addons/native_dialog/menu.c

raw
1 #define ALLEGRO_INTERNAL_UNSTABLE
2
3 #include "allegro5/allegro.h"
4 #include "allegro5/allegro_native_dialog.h"
5 #include "allegro5/internal/aintern_native_dialog.h"
6 #include "allegro5/internal/aintern_dtor.h"
7 #include "allegro5/internal/aintern_system.h"
8 #include "allegro5/internal/aintern_vector.h"
9
10 /* DISPLAY_MENU keeps track of which menu is associated with a display */
11 typedef struct DISPLAY_MENU DISPLAY_MENU;
12
13 struct DISPLAY_MENU
14 {
15 ALLEGRO_DISPLAY *display;
16 ALLEGRO_MENU *menu;
17 };
18
19 static _AL_VECTOR display_menus = _AL_VECTOR_INITIALIZER(DISPLAY_MENU);
20
21 /* The unique id. This is used to reverse lookup menus.
22 * The primarily need for this arises from Windows, which cannot store
23 * ALLEGRO_MENU_ID's wholesale.*/
24 static uint16_t unique_id;
25 static _AL_VECTOR menu_ids = _AL_VECTOR_INITIALIZER(_AL_MENU_ID);
26
27 /* The default event source is used with any menu that does not have
28 its own event source enabled. */
29 static ALLEGRO_EVENT_SOURCE default_menu_es;
30
31 /* Private functions */
32 static ALLEGRO_MENU *clone_menu(ALLEGRO_MENU *menu, bool popup);
33 static ALLEGRO_MENU_ITEM *create_menu_item(char const *title, uint16_t id, int flags, ALLEGRO_MENU *popup);
34 static void destroy_menu_item(ALLEGRO_MENU_ITEM *item);
35 static bool find_menu_item_r(ALLEGRO_MENU *menu, ALLEGRO_MENU_ITEM *item, int index, void *extra);
36 static ALLEGRO_MENU_ITEM *interpret_menu_id_param(ALLEGRO_MENU **menu, int *id);
37 static ALLEGRO_MENU_INFO *parse_menu_info(ALLEGRO_MENU *parent, ALLEGRO_MENU_INFO *info);
38 static bool set_menu_display_r(ALLEGRO_MENU *menu, ALLEGRO_MENU_ITEM *item, int index, void *extra);
39
40 /* True if the id is actually unique.
41 */
42 static bool get_unique_id(uint16_t* id)
43 {
44 if (unique_id + 1 == UINT16_MAX) {
45 return false;
46 }
47 *id = unique_id++;
48 return true;
49 }
50
51 /* The menu item owns the icon bitmap. It is converted to a memory bitmap
52 * when set to make sure any system threads will be able to read the data.
53 */
54 static void set_item_icon(ALLEGRO_MENU_ITEM *item, ALLEGRO_BITMAP *icon)
55 {
56 item->icon = icon;
57
58 if (icon && al_get_bitmap_flags(item->icon) & ALLEGRO_VIDEO_BITMAP) {
59 int old_flags = al_get_new_bitmap_flags();
60 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
61 item->icon = al_clone_bitmap(icon);
62 al_destroy_bitmap(icon);
63 al_set_new_bitmap_flags(old_flags);
64 }
65 }
66
67 static ALLEGRO_MENU_ITEM *create_menu_item(char const *title, uint16_t id, int flags, ALLEGRO_MENU *popup)
68 {
69 ALLEGRO_MENU_ITEM *item = al_calloc(1, sizeof(*item));
70 if (!item) return NULL;
71 if (!get_unique_id(&item->unique_id)) {
72 return NULL;
73 }
74
75 if (flags & ALLEGRO_MENU_ITEM_CHECKED)
76 flags |= ALLEGRO_MENU_ITEM_CHECKBOX;
77
78 if (title)
79 item->caption = al_ustr_new(title);
80 item->id = id;
81 item->flags = flags;
82 item->popup = popup;
83
84 return item;
85 }
86
87 /* Recursively walks over the entire menu structure, calling the user proc once per menu item,
88 * and once per menu. The deepest menu is called first. If the proc returns true, then the
89 * process terminates. It is not safe for the proc to modify the structure (add/remove items).
90 */
91 bool _al_walk_over_menu(ALLEGRO_MENU *menu,
92 bool (*proc)(ALLEGRO_MENU *menu, ALLEGRO_MENU_ITEM *item, int index, void *extra), void *extra)
93 {
94 ALLEGRO_MENU_ITEM **slot;
95 size_t i;
96 ASSERT(menu);
97 ASSERT(proc);
98
99 for (i = 0; i < _al_vector_size(&menu->items); ++i) {
100 slot = _al_vector_ref(&menu->items, i);
101
102 if ((*slot)->popup && _al_walk_over_menu((*slot)->popup, proc, extra))
103 return true;
104
105 if (proc(menu, *slot, i, extra))
106 return true;
107 }
108
109 return proc(menu, NULL, -1, extra);
110 }
111
112 /* A callback proc for _al_walk_over_menu that sets each menu's display parameter
113 * to the "extra" parameter.
114 */
115 static bool set_menu_display_r(ALLEGRO_MENU *menu, ALLEGRO_MENU_ITEM *item, int index, void *extra)
116 {
117 (void) index;
118
119 if (!item) {
120 menu->display = extra;
121 }
122
123 return false;
124 }
125
126 /* A callback proc for _al_walk_over_menu that searches a menu for a given id. If found it sets
127 * the "parent" parameter to the menu that contains it, and the "id" parameter to the index.
128 */
129 static bool find_menu_item_r(ALLEGRO_MENU *menu, ALLEGRO_MENU_ITEM *item, int index, void *extra)
130 {
131 ALLEGRO_MENU_ITEM *info = (ALLEGRO_MENU_ITEM *) extra;
132
133 if (item != NULL && info->id == item->id) {
134 info->id = index;
135 info->parent = menu;
136 return true;
137 }
138
139 return false;
140 }
141
142 /* Like find_menu_item_r, but searches by unique_id.
143 */
144 static bool find_menu_item_r_unique(ALLEGRO_MENU *menu, ALLEGRO_MENU_ITEM *item, int index, void *extra)
145 {
146 ALLEGRO_MENU_ITEM *info = (ALLEGRO_MENU_ITEM *) extra;
147
148 if (item != NULL && info->unique_id == item->unique_id) {
149 info->id = index;
150 info->parent = menu;
151 return true;
152 }
153
154 return false;
155 }
156
157 /* Carefully destroy a menu item... If the item is part of a menu, it must be
158 * removed from it.
159 */
160 static void destroy_menu_item(ALLEGRO_MENU_ITEM *item)
161 {
162 ASSERT(item);
163
164 if (!item->parent) {
165 /* This normally won't happen. */
166 _al_destroy_menu_item_at(item, -1);
167 }
168 else {
169 size_t i;
170 for (i = 0; i < _al_vector_size(&item->parent->items); ++i) {
171 if (*(ALLEGRO_MENU_ITEM **)_al_vector_ref(&item->parent->items, i) == item) {
172 /* Notify the platform that the item is to be removed. */
173 _al_destroy_menu_item_at(item, i);
174
175 /* Remove the command from the look-up vector. */
176 if (item->id != 0) {
177 _AL_MENU_ID *menu_id;
178 size_t j;
179
180 for (j = 0; j < _al_vector_size(&menu_ids); ++j) {
181 menu_id = (_AL_MENU_ID *) _al_vector_ref(&menu_ids, j);
182 if (menu_id->menu == item->parent && menu_id->unique_id == item->unique_id) {
183 _al_vector_delete_at(&menu_ids, j);
184 break;
185 }
186 }
187 }
188
189 /* Remove the menu from the parent's list. */
190 _al_vector_delete_at(&item->parent->items, i);
191
192 break;
193 }
194 }
195 }
196
197 if (item->caption)
198 al_ustr_free(item->caption);
199
200 if (item->popup) {
201 /* Delete the sub-menu. Must set the parent/display to NULL ahead of time to
202 * avoid recursing back here.
203 */
204 item->popup->parent = NULL;
205 item->popup->display = NULL;
206 al_destroy_menu(item->popup);
207 }
208
209 if (item->icon) {
210 al_destroy_bitmap(item->icon);
211 }
212
213 al_free(item);
214 }
215
216 /* An ALLEGRO_MENU_INFO structure represents a heirarchy of menus. This function
217 * recursively steps through it and builds the entire menu.
218 */
219 static ALLEGRO_MENU_INFO *parse_menu_info(ALLEGRO_MENU *parent, ALLEGRO_MENU_INFO *info)
220 {
221 ASSERT(parent);
222 ASSERT(info);
223
224 /* The end of the menu is marked by a NULL caption and an id of 0. */
225 while (info->caption || info->id) {
226 if (!info->caption) {
227 /* A separator */
228 al_append_menu_item(parent, NULL, 0, 0, NULL, NULL);
229 ++info;
230 }
231 else if (strlen(info->caption) > 2 &&
232 !strncmp("->", info->caption + strlen(info->caption) - 2, 2)) {
233 /* An item with a sub-menu has a -> marker as part of its caption.
234 * (e.g., "File->").
235 */
236 ALLEGRO_MENU *menu = al_create_menu();
237 if (menu) {
238 /* Strip the -> mark off the end. */
239 ALLEGRO_USTR *s = al_ustr_new(info->caption);
240 al_ustr_remove_range(s, al_ustr_size(s) - 2, al_ustr_size(s));
241 al_append_menu_item(parent, al_cstr(s), info->id, 0, NULL, menu);
242 info = parse_menu_info(menu, info + 1);
243 al_ustr_free(s);
244 }
245 }
246 else {
247 /* Just ar regular item */
248 al_append_menu_item(parent, info->caption, info->id, info->flags, info->icon, NULL);
249 ++info;
250 }
251 }
252
253 return info + 1;
254 }
255
256 /* All public functions that take a menu and id parameter have two interpretations:
257 *
258 * 1) If id > 0, then it represents an id anywhere within the menu structure,
259 * including child menus. If there are non-unique IDs, the first one found is
260 * returned, but the exact order is undefined. (IDs are meant to be unique.)
261 *
262 * 2) If id <= 0, then its absolute value represents an ordered index for that
263 * exact menu.
264 *
265 * If the parameters are valid, it returns a pointer to the corresponding
266 * MENU_ITEM, and the menu/id parameters are set to the item's parent and its
267 * index. Otherwise (on invalid parameters), it returns NULL and the menu/id
268 * parameters are left undefined.
269 *
270 * (Note that the private OS specific functions always take a direct index.)
271 */
272 static ALLEGRO_MENU_ITEM *interpret_menu_id_param(ALLEGRO_MENU **menu, int *id)
273 {
274 if (*id > 0) {
275 if (!al_find_menu_item(*menu, *id, menu, id))
276 return NULL;
277 }
278 else {
279 *id = (0 - *id);
280
281 if ((size_t) *id >= _al_vector_size(&((*menu)->items)))
282 return NULL;
283 }
284
285 return *(ALLEGRO_MENU_ITEM **) _al_vector_ref(&((*menu)->items), (size_t) *id);
286 }
287
288 /* A helper function for al_clone_menu() and al_clone_menu_for_popup().
289 * Note that only the root menu is created as a "popup" (if popup == TRUE).
290 */
291 static ALLEGRO_MENU *clone_menu(ALLEGRO_MENU *menu, bool popup)
292 {
293 ALLEGRO_MENU *clone = NULL;
294 size_t i;
295
296 if (menu) {
297 clone = popup ? al_create_popup_menu() : al_create_menu();
298
299 for (i = 0; i < _al_vector_size(&menu->items); ++i) {
300 const ALLEGRO_MENU_ITEM *item = *(ALLEGRO_MENU_ITEM **)_al_vector_ref(&menu->items, i);
301 ALLEGRO_BITMAP *icon = item->icon;
302
303 if (icon)
304 icon = al_clone_bitmap(icon);
305
306 al_append_menu_item(clone, item->caption ? al_cstr(item->caption) : NULL,
307 item->id, item->flags, icon, al_clone_menu(item->popup));
308 }
309 }
310
311 return clone;
312 }
313
314 /* Function: al_create_menu
315 */
316 ALLEGRO_MENU *al_create_menu(void)
317 {
318 ALLEGRO_MENU *m = al_calloc(1, sizeof(*m));
319
320 if (m) {
321 _al_vector_init(&m->items, sizeof(ALLEGRO_MENU_ITEM*));
322
323 /* Make sure the platform actually supports menus */
324 if (!_al_init_menu(m)) {
325 al_destroy_menu(m);
326 m = NULL;
327 }
328 }
329
330 return m;
331 }
332
333 /* Function: al_create_popup_menu
334 */
335 ALLEGRO_MENU *al_create_popup_menu(void)
336 {
337 ALLEGRO_MENU *m = al_calloc(1, sizeof(*m));
338
339 if (m) {
340 _al_vector_init(&m->items, sizeof(ALLEGRO_MENU_ITEM*));
341
342 if (!_al_init_popup_menu(m)) {
343 al_destroy_menu(m);
344 m = NULL;
345 }
346 else {
347 /* Popups are slightly different... They can be used multiple times
348 * with al_popup_menu(), but never as a display menu.
349 */
350 m->is_popup_menu = true;
351 }
352 }
353
354 return m;
355 }
356
357 /* Function: al_clone_menu
358 */
359 ALLEGRO_MENU *al_clone_menu(ALLEGRO_MENU *menu)
360 {
361 return clone_menu(menu, false);
362 }
363
364 /* Function: al_clone_menu_for_popup
365 */
366 ALLEGRO_MENU *al_clone_menu_for_popup(ALLEGRO_MENU *menu)
367 {
368 return clone_menu(menu, true);
369 }
370
371 /* Function: al_build_menu
372 */
373 ALLEGRO_MENU *al_build_menu(ALLEGRO_MENU_INFO *info)
374 {
375 ALLEGRO_MENU *root = al_create_menu();
376
377 if (root)
378 parse_menu_info(root, info);
379
380 return root;
381 }
382
383 /* Function: al_append_menu_item
384 */
385 int al_append_menu_item(ALLEGRO_MENU *parent, char const *title, uint16_t id,
386 int flags, ALLEGRO_BITMAP *icon, ALLEGRO_MENU *submenu)
387 {
388 ASSERT(parent);
389
390 /* Same thing as inserting a menu item at position == -SIZE */
391 return al_insert_menu_item(parent, 0 - (int) _al_vector_size(&parent->items),
392 title, id, flags, icon, submenu);
393 }
394
395 /* Function: al_insert_menu_item
396 */
397 int al_insert_menu_item(ALLEGRO_MENU *parent, int pos, char const *title,
398 uint16_t id, int flags, ALLEGRO_BITMAP *icon, ALLEGRO_MENU *submenu)
399 {
400 ALLEGRO_MENU_ITEM *item;
401 ALLEGRO_MENU_ITEM **slot;
402 _AL_MENU_ID *menu_id;
403 size_t i;
404
405 ASSERT(parent);
406
407 /* If not found, then treat as an append. */
408 if (!interpret_menu_id_param(&parent, &pos))
409 pos = _al_vector_size(&parent->items);
410
411 /* At this point pos == the _index_ of where to insert */
412
413 /* The sub-menu must not already be in use. */
414 if (submenu && (submenu->display || submenu->parent || submenu->is_popup_menu))
415 return -1;
416
417 item = create_menu_item(title, id, flags, submenu);
418 if (!item)
419 return -1;
420 item->parent = parent;
421
422 set_item_icon(item, icon);
423
424 i = (size_t) pos;
425
426 if (i >= _al_vector_size(&parent->items)) {
427 /* Append */
428 i = _al_vector_size(&parent->items);
429 slot = _al_vector_alloc_back(&parent->items);
430 }
431 else {
432 /* Insert */
433 slot = _al_vector_alloc_mid(&parent->items, i);
434 }
435
436 if (!slot) {
437 destroy_menu_item(item);
438 return -1;
439 }
440 *slot = item;
441
442 if (submenu) {
443 submenu->parent = item;
444
445 if (parent->display)
446 _al_walk_over_menu(submenu, set_menu_display_r, parent->display);
447 }
448
449 _al_insert_menu_item_at(item, (int) i);
450
451 if (id) {
452 /* Append the menu's ID to the search vector */
453 menu_id = (_AL_MENU_ID *) _al_vector_alloc_back(&menu_ids);
454 menu_id->unique_id = item->unique_id;
455 menu_id->id = id;
456 menu_id->menu = parent;
457 }
458
459 return (int) i;
460 }
461
462 /* Function: al_remove_menu_item
463 */
464 bool al_remove_menu_item(ALLEGRO_MENU *menu, int pos)
465 {
466 ALLEGRO_MENU_ITEM *item;
467
468 ASSERT(menu);
469
470 item = interpret_menu_id_param(&menu, &pos);
471 if (!item)
472 return false;
473
474 destroy_menu_item(item);
475
476 return true;
477 }
478
479 /* Function: al_find_menu
480 */
481 ALLEGRO_MENU *al_find_menu(ALLEGRO_MENU *haystack, uint16_t id)
482 {
483 int index;
484
485 return !al_find_menu_item(haystack, id, &haystack, &index) ? NULL :
486 (*(ALLEGRO_MENU_ITEM **)_al_vector_ref(&haystack->items, index))->popup;
487 }
488
489 /* Function: al_find_menu_item
490 */
491 bool al_find_menu_item(ALLEGRO_MENU *haystack, uint16_t id, ALLEGRO_MENU **menu,
492 int *index)
493 {
494 ALLEGRO_MENU_ITEM item;
495
496 ASSERT(haystack);
497
498 /* Abuse the ALLEGRO_MENU_ITEM struct as a container for the _al_walk_over_menu callback.
499 * If found, it will return true, and the "parent" field will be the menu that and
500 * the "id" will be the index.
501 */
502 item.id = id;
503
504 if (!_al_walk_over_menu(haystack, find_menu_item_r, &item))
505 return false;
506
507 if (menu)
508 *menu = item.parent;
509
510 if (index)
511 *index = item.id;
512
513 return true;
514 }
515
516 /* As al_find_menu_item, but searches by the unique id.
517 */
518 bool _al_find_menu_item_unique(ALLEGRO_MENU *haystack, uint16_t unique_id, ALLEGRO_MENU **menu,
519 int *index)
520 {
521 ALLEGRO_MENU_ITEM item;
522
523 ASSERT(haystack);
524
525 item.unique_id = unique_id;
526
527 if (!_al_walk_over_menu(haystack, find_menu_item_r_unique, &item))
528 return false;
529
530 if (menu)
531 *menu = item.parent;
532
533 if (index)
534 *index = item.id;
535
536 return true;
537 }
538
539 /* Function: al_get_menu_item_caption
540 */
541 const char *al_get_menu_item_caption(ALLEGRO_MENU *menu, int pos)
542 {
543 ALLEGRO_MENU_ITEM *item;
544
545 ASSERT(menu);
546
547 item = interpret_menu_id_param(&menu, &pos);
548 return item && item->caption ? al_cstr(item->caption) : NULL;
549 }
550
551
552 /* Function: al_set_menu_item_caption
553 */
554 void al_set_menu_item_caption(ALLEGRO_MENU *menu, int pos, const char *caption)
555 {
556 ALLEGRO_MENU_ITEM *item;
557
558 ASSERT(menu);
559
560 item = interpret_menu_id_param(&menu, &pos);
561
562 if (item && item->caption) {
563 al_ustr_free(item->caption);
564 item->caption = al_ustr_new(caption);
565 _al_update_menu_item_at(item, pos);
566 }
567 }
568
569 /* Function: al_get_menu_item_flags
570 */
571 int al_get_menu_item_flags(ALLEGRO_MENU *menu, int pos)
572 {
573 ALLEGRO_MENU_ITEM *item;
574
575 ASSERT(menu);
576
577 item = interpret_menu_id_param(&menu, &pos);
578 return item ? item->flags : -1;
579 }
580
581 /* Function: al_set_menu_item_flags
582 */
583 void al_set_menu_item_flags(ALLEGRO_MENU *menu, int pos, int flags)
584 {
585 ALLEGRO_MENU_ITEM *item;
586
587 ASSERT(menu);
588
589 item = interpret_menu_id_param(&menu, &pos);
590
591 if (item) {
592 /* The CHECKBOX flag is read-only after the menu is created, and
593 * the CHECKED flag can only be set if it is a CHECKBOX.
594 */
595 if (item->flags & ALLEGRO_MENU_ITEM_CHECKBOX)
596 flags |= ALLEGRO_MENU_ITEM_CHECKBOX;
597 else {
598 flags &= ~ALLEGRO_MENU_ITEM_CHECKED;
599 flags &= ~ALLEGRO_MENU_ITEM_CHECKBOX;
600 }
601
602 item->flags = flags;
603 _al_update_menu_item_at(item, pos);
604 }
605 }
606
607 /* Function: al_toggle_menu_item_flags
608 */
609 int al_toggle_menu_item_flags(ALLEGRO_MENU *menu, int pos, int flags)
610 {
611 ALLEGRO_MENU_ITEM *item;
612
613 ASSERT(menu);
614
615 item = interpret_menu_id_param(&menu, &pos);
616
617 if (!item)
618 return -1;
619
620 /* The CHECKBOX flag is read-only after the menu is created, and
621 * the CHECKED flag can only be set if it is a CHECKBOX.
622 */
623 flags &= ~ALLEGRO_MENU_ITEM_CHECKBOX;
624 if (!(item->flags & ALLEGRO_MENU_ITEM_CHECKBOX)) {
625 flags &= ~ALLEGRO_MENU_ITEM_CHECKED;
626 }
627
628 item->flags ^= flags;
629 _al_update_menu_item_at(item, pos);
630
631 return item->flags & flags;
632 }
633
634 /* Function: al_get_menu_item_icon
635 */
636 ALLEGRO_BITMAP *al_get_menu_item_icon(ALLEGRO_MENU *menu, int pos)
637 {
638 ALLEGRO_MENU_ITEM *item;
639
640 ASSERT(menu);
641
642 item = interpret_menu_id_param(&menu, &pos);
643 return item ? item->icon : NULL;
644 }
645
646
647 /* Function: al_set_menu_item_icon
648 */
649 void al_set_menu_item_icon(ALLEGRO_MENU *menu, int pos, ALLEGRO_BITMAP *icon)
650 {
651 ALLEGRO_MENU_ITEM *item;
652
653 ASSERT(menu);
654
655 item = interpret_menu_id_param(&menu, &pos);
656
657 if (item) {
658 if (item->icon)
659 al_destroy_bitmap(item->icon);
660
661 set_item_icon(item, icon);
662 _al_update_menu_item_at(item, pos);
663 }
664 }
665
666 /* Function: al_destroy_menu
667 */
668 void al_destroy_menu(ALLEGRO_MENU *menu)
669 {
670 ALLEGRO_MENU_ITEM **slot;
671 size_t i;
672 ASSERT(menu);
673
674 if (menu->parent) {
675 /* If the menu is attached to a menu item, then this is equivelant to
676 removing that menu item. */
677 ALLEGRO_MENU *parent = menu->parent->parent;
678 ASSERT(parent);
679
680 for (i = 0; i < _al_vector_size(&parent->items); ++i) {
681 slot = _al_vector_ref(&parent->items, i);
682 if (*slot == menu->parent) {
683 al_remove_menu_item(parent, 0 - (int) i);
684 return;
685 }
686 }
687
688 /* Should never get here. */
689 ASSERT(false);
690 return;
691 }
692 else if (menu->display && !menu->is_popup_menu) {
693 /* This is an active, top-level menu. */
694 al_remove_display_menu(menu->display);
695 }
696
697 /* Destroy each item associated with the menu. */
698 while (_al_vector_size(&menu->items)) {
699 slot = _al_vector_ref_back(&menu->items);
700 destroy_menu_item(*slot);
701 }
702
703 _al_vector_free(&menu->items);
704
705 al_disable_menu_event_source(menu);
706 al_free(menu);
707 }
708
709 /* Function: al_get_default_menu_event_source
710 */
711 ALLEGRO_EVENT_SOURCE *al_get_default_menu_event_source(void)
712 {
713 return &default_menu_es;
714 }
715
716 /* Function: al_enable_menu_event_source
717 */
718 ALLEGRO_EVENT_SOURCE *al_enable_menu_event_source(ALLEGRO_MENU *menu)
719 {
720 ASSERT(menu);
721
722 if (!menu->is_event_source) {
723 al_init_user_event_source(&menu->es);
724 menu->is_event_source = true;
725 }
726
727 return &menu->es;
728 }
729
730 /* Function: al_disable_menu_event_source
731 */
732 void al_disable_menu_event_source(ALLEGRO_MENU *menu)
733 {
734 ASSERT(menu);
735
736 if (menu->is_event_source) {
737 al_destroy_user_event_source(&menu->es);
738 menu->is_event_source = false;
739 }
740 }
741
742 /* Function: al_get_display_menu
743 */
744 ALLEGRO_MENU *al_get_display_menu(ALLEGRO_DISPLAY *display)
745 {
746 size_t i;
747 ASSERT(display);
748
749 /* Search through the display_menus vector to see if this display has
750 * a menu associated with it. */
751 for (i = 0; i < _al_vector_size(&display_menus); ++i) {
752 DISPLAY_MENU *dm = (DISPLAY_MENU *) _al_vector_ref(&display_menus, i);
753 if (dm->display == display)
754 return dm->menu;
755 }
756
757 return NULL;
758 }
759
760 /* Function: al_set_display_menu
761 */
762 bool al_set_display_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *menu)
763 {
764 DISPLAY_MENU *dm = NULL;
765 size_t i;
766 int menu_height = _al_get_menu_display_height();
767 bool automatic_menu_display_resize = true;
768 const char* automatic_menu_display_resize_value =
769 al_get_config_value(al_get_system_config(), "compatibility", "automatic_menu_display_resize");
770 if (automatic_menu_display_resize_value && strcmp(automatic_menu_display_resize_value, "false") == 0)
771 automatic_menu_display_resize = false;
772
773 ASSERT(display);
774
775 /* Check if this display has a menu associated with it */
776 for (i = 0; i < _al_vector_size(&display_menus); ++i) {
777 dm = (DISPLAY_MENU *) _al_vector_ref(&display_menus, i);
778 if (dm->display == display)
779 break;
780 }
781
782 /* If no display was found, reset dm to NULL */
783 if (i == _al_vector_size(&display_menus))
784 dm = NULL;
785
786 if (!menu) {
787 /* Removing the menu */
788
789 if (!dm)
790 return false;
791
792 _al_hide_display_menu(display, dm->menu);
793 _al_walk_over_menu(dm->menu, set_menu_display_r, NULL);
794 _al_vector_delete_at(&display_menus, i);
795
796 if (automatic_menu_display_resize && menu_height > 0) {
797 al_resize_display(display, al_get_display_width(display), al_get_display_height(display));
798 }
799 }
800 else {
801 /* Setting the menu. It must not currently be attached to any
802 * display, and it cannot have a parent menu. */
803 if (menu->display || menu->parent)
804 return false;
805
806 if (dm) {
807 /* hide the existing menu */
808 _al_hide_display_menu(display, dm->menu);
809 _al_walk_over_menu(dm->menu, set_menu_display_r, NULL);
810 }
811
812 if (!_al_show_display_menu(display, menu)) {
813 /* Unable to set the new menu, but already have hidden the
814 * previous one, so delete the display_menus slot. */
815 if (dm)
816 _al_vector_delete_at(&display_menus, i);
817 return false;
818 }
819
820 /* Set the entire menu tree as owned by the display */
821 _al_walk_over_menu(menu, set_menu_display_r, display);
822
823 if (!dm)
824 dm = _al_vector_alloc_back(&display_menus);
825
826 if (automatic_menu_display_resize && menu_height > 0) {
827 /* Temporarily disable the constraints so we don't send a RESIZE_EVENT. */
828 bool old_constraints = display->use_constraints;
829 display->use_constraints = false;
830 al_resize_display(display, al_get_display_width(display), al_get_display_height(display));
831 display->use_constraints = old_constraints;
832 }
833
834 dm->display = display;
835 dm->menu = menu;
836 }
837
838 return true;
839 }
840
841 /* Function: al_popup_menu
842 */
843 bool al_popup_menu(ALLEGRO_MENU *popup, ALLEGRO_DISPLAY *display)
844 {
845 bool ret;
846 ASSERT(popup);
847
848 if (!popup->is_popup_menu || popup->parent)
849 return false;
850
851 if (!display)
852 display = al_get_current_display();
853
854 /* Set the entire menu tree as owned by the display */
855 _al_walk_over_menu(popup, set_menu_display_r, display);
856
857 ret = _al_show_popup_menu(display, popup);
858
859 if (!ret) {
860 _al_walk_over_menu(popup, set_menu_display_r, NULL);
861 }
862 return ret;
863 }
864
865 /* Function: al_remove_display_menu
866 */
867 ALLEGRO_MENU *al_remove_display_menu(ALLEGRO_DISPLAY *display)
868 {
869 ALLEGRO_MENU *menu;
870
871 ASSERT(display);
872
873 menu = al_get_display_menu(display);
874
875 if (menu)
876 al_set_display_menu(display, NULL);
877
878 return menu;
879 }
880
881 /* Tries to find the menu that has a child with the given id. If display
882 * is not NULL, then it must also match. The first match is returned.
883 */
884 _AL_MENU_ID *_al_find_parent_menu_by_id(ALLEGRO_DISPLAY *display, uint16_t unique_id)
885 {
886 _AL_MENU_ID *menu_id;
887 size_t i;
888
889 for (i = 0; i < _al_vector_size(&menu_ids); ++i) {
890 menu_id = (_AL_MENU_ID *) _al_vector_ref(&menu_ids, i);
891 if (menu_id->unique_id == unique_id) {
892 if (!display || menu_id->menu->display == display) {
893 return menu_id;
894 }
895 }
896 }
897
898 return NULL;
899 }
900
901 /* Each platform implementation must call this when a menu has been clicked.
902 * The display parameter should be sent if at all possible! If it isn't sent,
903 * and the user is using non-unique ids, it won't know which display actually
904 * triggered the menu click.
905 */
906 bool _al_emit_menu_event(ALLEGRO_DISPLAY *display, uint16_t unique_id)
907 {
908 ALLEGRO_EVENT event;
909 _AL_MENU_ID *menu_id = NULL;
910 ALLEGRO_EVENT_SOURCE *source = al_get_default_menu_event_source();
911
912 /* try to find the menu that triggered the event */
913 menu_id = _al_find_parent_menu_by_id(display, unique_id);
914
915 if (!menu_id)
916 return false;
917
918 if (menu_id->id == 0)
919 return false;
920
921 if (menu_id) {
922 /* A menu was found associated with the id. See if it has an
923 * event source associated with it, and adjust "source" accordingly. */
924 ALLEGRO_MENU *m = menu_id->menu;
925 while (true) {
926 if (m->is_event_source) {
927 source = &m->es;
928 break;
929 }
930
931 if (!m->parent)
932 break;
933
934 /* m->parent is of type MENU_ITEM,
935 * which always has a parent of type MENU */
936 ASSERT(m->parent->parent);
937 m = m->parent->parent;
938 }
939 }
940
941 event.user.type = ALLEGRO_EVENT_MENU_CLICK;
942 event.user.data1 = menu_id->id;
943 event.user.data2 = (intptr_t) display;
944 event.user.data3 = (intptr_t) menu_id->menu;
945
946 al_emit_user_event(source, &event, NULL);
947
948 return true;
949 }
950
951
952 /* vim: set sts=3 sw=3 et: */