Debian Codegraph @

common/ui/menubar.cpp

raw
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 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include "physfsx.h"
24 #include "u_mem.h"
25 #include "maths.h"
26 #include "pstypes.h"
27 #include "gr.h"
28 #include "strutil.h"
29 #include "event.h"
30 #include "window.h"
31 #include "ui.h"
32 #include "mouse.h"
33 #include "key.h"
34 #include "func.h"
35 #include "dxxerror.h"
36
37 #include "compiler-range_for.h"
38 #include "d_enumerate.h"
39 #include "d_range.h"
40 #include "partial_range.h"
41 #include <utility>
42
43 namespace dcx {
44
45 namespace {
46
47 struct ITEM {
48 short x{0}, y = 0, w = 0, h = 0;
49 short Hotkey = -1;
50 int (*user_function)(void) = nullptr;
51 std::unique_ptr<char[]> Text;
52 std::unique_ptr<char[]> InactiveText;
53 };
54
55 struct MENU {
56 window *wind = nullptr;
57 short x{0}, y = 0, w = 0, h = 0;
58 uint8_t ShowBar{0};
59 uint8_t CurrentItem{0};
60 uint16_t NumItems{0};
61 uint8_t Displayed{0};
62 uint8_t Active{0};
63 std::array<ITEM, 32> Item;
64 };
65
66 struct menubar_window : window
67 {
68 MENU &menubar;
69 explicit menubar_window(grs_canvas &src, const int x, const int y, const int w, const int h, MENU &menu) :
70 window(src, x, y, w, h),
71 menubar(menu)
72 {
73 }
74 virtual window_event_result event_handler(const d_event &) override;
75 };
76
77 struct menu_window : window
78 {
79 MENU &menu;
80 explicit menu_window(grs_canvas &src, const int x, const int y, const int w, const int h, MENU &menu) :
81 window(src, x, y, w, h),
82 menu(menu)
83 {
84 }
85 virtual window_event_result event_handler(const d_event &) override;
86 };
87
88 static std::array<MENU *, 30> Menu;
89
90 static unsigned num_menus;
91 static int state;
92
93 #define CMENU (Menu[0]->CurrentItem+1)
94
95 //------------------------- Show a menu item -------------------
96
97 static void item_show(const MENU &menu, const int n)
98 {
99 const auto &item = menu.Item[n];
100 if (!item.Text)
101 return;
102
103 gr_set_default_canvas();
104 auto &canvas = *grd_curcanv;
105 // If this is a seperator, then draw it.
106 if (item.Text[0] == '-')
107 {
108 const auto color{CBLACK};
109 gr_urect(canvas, item.x, item.y + item.h / 2, item.x + item.w - 1, item.y + item.h / 2, color);
110 return;
111 }
112
113 if (menu.CurrentItem == n && menu.ShowBar)
114 {
115 if (&menu != Menu[0])
116 {
117 const auto color{CBLACK};
118 gr_urect(canvas, item.x + 1, item.y + 1, item.x + menu.w - 2, item.y + item.h - 2, color);
119 }
120 gr_set_fontcolor(canvas, CWHITE, CBLACK);
121 }else {
122 if (&menu != Menu[0])
123 {
124 const auto color{CGREY};
125 gr_urect(canvas, item.x + 1, item.y + 1, item.x + menu.w - 2, item.y + item.h - 2, color);
126 }
127 gr_set_fontcolor(canvas, CBLACK, CGREY);
128 }
129
130 if (&menu != Menu[0])
131 {
132 if (menu.Active)
133 gr_ustring(canvas, *canvas.cv_font, item.x + 1, item.y + 1, item.Text.get());
134 else
135 gr_ustring(canvas, *canvas.cv_font, item.x + 1, item.y + 1, item.InactiveText.get());
136 } else {
137 if (menu.Active)
138 gr_ustring(canvas, *canvas.cv_font, item.x, item.y, item.Text.get());
139 else
140 gr_ustring(canvas, *canvas.cv_font, item.x, item.y, item.InactiveText.get());
141 }
142 }
143
144 static void menu_draw(const MENU &menu)
145 {
146 gr_set_default_canvas();
147 auto &canvas = *grd_curcanv;
148
149 // Draw the menu background
150 gr_urect(canvas, menu.x, menu.y, menu.x + menu.w - 1, menu.y + menu.h - 1, CGREY);
151 if (&menu != Menu[0])
152 {
153 gr_ubox(canvas, menu.x, menu.y, menu.x + menu.w - 1, menu.y + menu.h - 1, CBLACK);
154 }
155
156 // Draw the items
157
158 for (const auto i : xrange(menu.NumItems))
159 item_show( menu, i );
160 }
161
162 //---------------------------- Show a menu ---------------------
163
164 static void menu_show(MENU &menu)
165 {
166 menu.wind->set_visible(1);
167 // Mark as displayed.
168 menu.Displayed = 1;
169 }
170
171 static void menu_other_show()
172 {
173 auto &menu = *Menu[CMENU];
174 if (!menu.wind)
175 {
176 menu.wind = window_create<menu_window>(grd_curscreen->sc_canvas, menu.x, menu.y, menu.w, menu.h, menu);
177 }
178 menu_show(menu);
179 }
180
181 //-------------------------- Hide a menu -----------------------
182
183 static void menu_hide(MENU &menu)
184 {
185 // Can't hide if it's not already drawn
186 if (!menu.Displayed) return;
187
188 if (&menu != Menu[0] && menu.wind)
189 menu.wind->set_visible(0); // don't draw or receive events
190
191 menu.Active = 0;
192 if (&menu == Menu[0] && menu.wind)
193 menu.wind->set_modal(0);
194 // Mark as hidden.
195 menu.Displayed = 0;
196 }
197
198
199 //------------------------- Move the menu bar ------------------
200
201 static void menu_move_bar_to(MENU &menu, const int number)
202 {
203 const auto old_item{menu.CurrentItem};
204 menu.CurrentItem = number;
205
206 if (menu.Displayed && number != old_item)
207 {
208 item_show( menu, old_item );
209 item_show( menu, number );
210 }
211 }
212
213 //------------------------ Match keypress to item ------------------
214 static int menu_match_keypress(MENU &menu, int keypress)
215 {
216 char c;
217 if ((keypress & KEY_CTRLED) || (keypress & KEY_SHIFTED))
218 return -1;
219
220 keypress &= 0xFF;
221
222 c = key_ascii();
223
224 for (auto &&[i, item] : enumerate(partial_range(menu.Item, menu.NumItems)))
225 {
226 auto letter = strrchr(item.Text.get(), CC_UNDERLINE);
227 if (letter)
228 {
229 letter++;
230 if (c==tolower(*letter))
231 return i;
232 }
233 }
234 return -1;
235 }
236
237
238 static int menu_is_mouse_on(const ITEM &item)
239 {
240 const auto [x, y, z] = mouse_get_pos();
241 if (x >= item.x &&
242 x < item.x + item.w &&
243 y >= item.y &&
244 y <= item.y + item.h)
245 return 1;
246 else
247 return 0;
248 }
249
250 static int menu_check_mouse_item(const MENU &menu)
251 {
252 for (auto &&[i, item] : enumerate(partial_range(menu.Item, menu.NumItems)))
253 {
254 if (menu_is_mouse_on(item))
255 {
256 if (item.Text[0] == '-')
257 return -1;
258 else
259 return i;
260 }
261 }
262 return -1;
263 }
264
265
266 static void menu_hide_all()
267 {
268 range_for (auto &i, partial_range(Menu, 1u, num_menus))
269 menu_hide(*i);
270 auto &menubar = *Menu[0];
271 menubar.ShowBar = 0;
272 menubar.Active = 0;
273 if (menubar.wind)
274 menubar.wind->set_modal(0);
275 menubar_show();
276 }
277
278
279 static int state2_alt_down;
280
281 static window_event_result do_state_0(const d_event &event)
282 {
283 int keypress{0};
284 if (event.type == event_type::key_command)
285 keypress = event_key_get(event);
286
287 auto &menubar = *Menu[0];
288 menubar.Active = 0;
289 if (menubar.wind)
290 menubar.wind->set_modal(0);
291 menubar.ShowBar = 0;
292
293 if ( keypress & KEY_ALTED ) {
294 const auto i = menu_match_keypress(menubar, keypress);
295 if (i > -1 )
296 {
297 auto &menubar = *Menu[0];
298 menubar.CurrentItem = i;
299 menubar.Active = 0;
300 if (menubar.wind)
301 menubar.wind->set_modal(0);
302
303 state = 3;
304 auto &other_menu = *Menu[CMENU];
305 other_menu.ShowBar = 1;
306 other_menu.Active = 1;
307 menubar.ShowBar = 1;
308
309 menu_other_show();
310 menubar_show();
311 return window_event_result::handled;
312 }
313 }
314
315 range_for (auto &i, partial_const_range(Menu, num_menus))
316 for (auto &j : partial_const_range(i->Item, i->NumItems))
317 {
318 if ( j.Hotkey == keypress )
319 {
320 if (j.user_function)
321 j.user_function();
322 return window_event_result::handled;
323 }
324 }
325
326 if (keypress & KEY_ALTED)
327 //if ( (keypress & 0xFF) == KEY_LALT )
328 {
329 // Make sure the menubar receives events exclusively
330 state = 1;
331 menubar.Active = 1;
332
333 // Put the menubar in front - hope this doesn't mess anything up by leaving it there
334 // If it does, will need to remember the previous front window and restore it.
335 // (Personally, I just use either the mouse or 'hotkeys' for menus)
336 window_select(*menubar.wind);
337
338 menubar.wind->set_modal(1);
339 menubar_show();
340 return window_event_result::handled;
341 }
342
343 const auto i = menu_check_mouse_item(menubar);
344
345 if ( B1_JUST_PRESSED && (i > -1))
346 {
347 menubar.CurrentItem = i;
348 state = 3;
349 auto &other_menu = *Menu[CMENU];
350 other_menu.ShowBar = 1;
351 menubar.ShowBar = 1;
352 other_menu.Active = 1;
353 menubar.Active = 0;
354 menubar.wind->set_modal(0);
355 menu_other_show();
356 menubar_show();
357 return window_event_result::handled;
358 }
359 return window_event_result::ignored;
360 }
361
362 static window_event_result do_state_1(const d_event &event)
363 {
364 int keypress{0};
365 window_event_result rval = window_event_result::ignored;
366 if (event.type == event_type::key_command)
367 keypress = event_key_get(event);
368 auto &menubar = *Menu[0];
369 if (event.type == event_type::key_release && !(event_key_get(event) & KEY_ALTED))
370 {
371 state = 2;
372 state2_alt_down = 0;
373 menubar.ShowBar = 1;
374 menubar.Active = 1;
375 menubar_show();
376 rval = window_event_result::handled;
377 }
378
379 if (const auto i = menu_match_keypress(menubar, keypress); i > -1)
380 {
381 menubar.CurrentItem = i;
382 menubar.Active = 0;
383 menubar.wind->set_modal(0);
384 state = 3;
385 auto &other_menu = *Menu[CMENU];
386 other_menu.ShowBar = 1;
387 other_menu.Active = 1;
388 menubar.ShowBar = 1;
389
390 menu_other_show();
391 menubar_show();
392 rval = window_event_result::handled;
393 }
394
395 const auto i = menu_check_mouse_item(menubar);
396
397 if ( (i == -1) && B1_JUST_RELEASED )
398 {
399 state = 0;
400 menu_hide_all();
401 return window_event_result::handled;
402 }
403
404 if ( B1_JUST_PRESSED && (i > -1))
405 {
406 menubar.CurrentItem = i;
407 state = 3;
408 auto &other_menu = *Menu[CMENU];
409 other_menu.ShowBar = 1;
410 other_menu.Active = 1;
411 menubar.ShowBar = 1;
412 menubar.Active = 0;
413 menubar.wind->set_modal(0);
414 menu_other_show();
415 menubar_show();
416 return window_event_result::handled;
417 }
418
419 return rval;
420 }
421
422 static window_event_result do_state_2(const d_event &event)
423 {
424 int i;
425 int keypress{0};
426 window_event_result rval = window_event_result::ignored;
427 if (event.type == event_type::key_command)
428 keypress = event_key_get(event);
429 if (keypress & KEY_ALTED)
430 {
431 state2_alt_down = 1;
432 rval = window_event_result::handled;
433 }
434
435 if (event.type == event_type::key_release && !(event_key_get(event) & KEY_ALTED) && state2_alt_down)
436 {
437 state = 0;
438 menu_hide_all();
439 rval = window_event_result::handled;
440 }
441
442 switch( keypress )
443 {
444 case KEY_ESC:
445 state = 0;
446 menu_hide_all();
447 return window_event_result::handled;
448 case KEY_LEFT:
449 case KEY_PAD4:
450 {
451 auto &menubar = *Menu[0];
452 i = menubar.CurrentItem;
453 if (i <= 0)
454 i = menubar.NumItems;
455 menu_move_bar_to(menubar, i - 1);
456 return window_event_result::handled;
457 }
458 case KEY_RIGHT:
459 case KEY_PAD6:
460 {
461 auto &menubar = *Menu[0];
462 i = menubar.CurrentItem + 1;
463 if (i >= menubar.NumItems)
464 i = 0;
465 menu_move_bar_to(menubar, i);
466 return window_event_result::handled;
467 }
468 case KEY_ENTER:
469 case KEY_PADENTER:
470 case KEY_DOWN:
471 case KEY_PAD2:
472 state = 3;
473 {
474 auto &other_menu = *Menu[CMENU];
475 other_menu.ShowBar = 1;
476 other_menu.Active = 1;
477 }
478 {
479 auto &menubar = *Menu[0];
480 menubar.Active = 0;
481 menubar.wind->set_modal(0);
482 }
483 menubar_show();
484 menu_other_show();
485 return window_event_result::handled;
486
487 default:
488 {
489 auto &menubar = *Menu[0];
490 i = menu_match_keypress(menubar, keypress);
491
492 if (i > -1 )
493 {
494 menubar.CurrentItem = i;
495 menubar.Active = 0;
496 menubar.wind->set_modal(0);
497 state = 3;
498 auto &other_menu = *Menu[CMENU];
499 other_menu.ShowBar = 1;
500 other_menu.Active = 1;
501 menubar.ShowBar = 1;
502 menu_other_show();
503 menubar_show();
504 return window_event_result::handled;
505 }
506
507 i = menu_check_mouse_item(menubar);
508
509 if ( (i == -1) && B1_JUST_RELEASED )
510 {
511 state = 0;
512 menu_hide_all();
513 return window_event_result::handled;
514 }
515
516 if (i > -1)
517 {
518 menubar.CurrentItem = i;
519 menubar.Active = 0;
520 menubar.wind->set_modal(0);
521 state = 3;
522 auto &other_menu = *Menu[CMENU];
523 other_menu.ShowBar = 1;
524 other_menu.Active = 1;
525 menubar.ShowBar = 1;
526 menu_other_show();
527 menubar_show();
528 return window_event_result::handled;
529 }
530 }
531 }
532
533 return rval;
534 }
535
536 window_event_result menu_window::event_handler(const d_event &event)
537 {
538 int keypress{0};
539
540 if (state != 3)
541 return window_event_result::ignored;
542 if (event.type == event_type::key_command)
543 keypress = event_key_get(event);
544 else if (event.type == event_type::window_close) // quitting
545 {
546 state = 0;
547 menu_hide_all();
548 menu.wind = nullptr;
549 return window_event_result::ignored;
550 }
551 window_event_result rval = window_event_result::ignored;
552 switch( keypress )
553 {
554 case 0:
555 break;
556 case KEY_ESC:
557 state = 0;
558 menu_hide_all();
559 rval = window_event_result::handled;
560 break;
561 case KEY_DOWN:
562 case KEY_PAD2:
563 {
564 auto &other_menu = *Menu[CMENU];
565 auto i = other_menu.CurrentItem;
566 do {
567 i++;
568 if (i >= other_menu.NumItems)
569 i = 0;
570 } while(other_menu.Item[i].Text[0] == '-');
571 menu_move_bar_to(other_menu, i);
572 rval = window_event_result::handled;
573 break;
574 }
575 case KEY_UP:
576 case KEY_PAD8:
577 {
578 auto &other_menu = *Menu[CMENU];
579 auto i = other_menu.CurrentItem;
580 do
581 {
582 if (i <= 0)
583 i = other_menu.NumItems;
584 --i;
585 } while(other_menu.Item[i].Text[0] == '-');
586 menu_move_bar_to(other_menu, i);
587 rval = window_event_result::handled;
588 break;
589 }
590 case KEY_RIGHT:
591 case KEY_PAD6:
592 {
593 auto &other_menu = *Menu[CMENU];
594 menu_hide(other_menu);
595 auto &menubar = *Menu[0];
596 auto i = menubar.CurrentItem + 1;
597 if (i >= menubar.NumItems)
598 i = 0;
599 menu_move_bar_to(menubar, i);
600 other_menu.ShowBar = 1;
601 other_menu.Active = 1;
602 menu_other_show();
603 rval = window_event_result::handled;
604 break;
605 }
606 case KEY_LEFT:
607 case KEY_PAD4:
608 {
609 auto &other_menu = *Menu[CMENU];
610 menu_hide(other_menu);
611 auto &menubar = *Menu[0];
612 auto i = menubar.CurrentItem;
613 if (i <= 0)
614 i = menubar.NumItems;
615 --i;
616 menu_move_bar_to(menubar, i);
617 other_menu.ShowBar = 1;
618 other_menu.Active = 1;
619 menu_other_show();
620 rval = window_event_result::handled;
621 break;
622 }
623 case KEY_ENTER:
624 case KEY_PADENTER:
625 {
626 state = 0;
627 menu_hide_all();
628 auto &other_menu = *Menu[CMENU];
629
630 if (const auto f = other_menu.Item[other_menu.CurrentItem].user_function)
631 f();
632
633 rval = window_event_result::handled;
634 break;
635 }
636
637 default:
638 {
639 auto &other_menu = *Menu[CMENU];
640 const auto i = menu_match_keypress(other_menu, keypress);
641 if (i > -1 )
642 {
643 menu_move_bar_to(other_menu, i);
644 state = 0;
645 menu_hide_all();
646
647 if (const auto f = other_menu.Item[other_menu.CurrentItem].user_function)
648 f();
649 rval = window_event_result::handled;
650 break;
651 }
652 break;
653 }
654 }
655 if (event.type == event_type::mouse_moved || B1_JUST_RELEASED)
656 {
657 auto &other_menu = *Menu[CMENU];
658 const auto i = menu_check_mouse_item(other_menu);
659
660 if (i > -1 )
661 {
662 if ( B1_JUST_RELEASED )
663 {
664 menu_move_bar_to(other_menu, i);
665 state = 0;
666 menu_hide_all();
667
668 if (const auto f = other_menu.Item[other_menu.CurrentItem].user_function)
669 f();
670 rval = window_event_result::handled;
671 }
672 else
673 {
674 menu_move_bar_to(other_menu, i);
675 rval = window_event_result::handled;
676 }
677 } else {
678 auto &menubar = *Menu[0];
679 const auto i = menu_check_mouse_item(menubar);
680
681 if (i > -1)
682 {
683 if (menubar.CurrentItem != i)
684 {
685 menu_hide(other_menu);
686 menu_move_bar_to(menubar, i);
687 other_menu.ShowBar = 1;
688 other_menu.Active = 1;
689 menu_other_show();
690 }
691 rval = window_event_result::handled;
692 }
693
694 if ( B1_JUST_RELEASED )
695 {
696 state = 0;
697 menu_hide_all();
698 rval = window_event_result::handled;
699 }
700 }
701 }
702
703 if (event.type == event_type::window_draw)
704 {
705 auto &other_menu = *Menu[CMENU];
706 menu_draw(other_menu);
707 return window_event_result::handled;
708 }
709 return rval;
710 }
711
712 window_event_result menubar_window::event_handler(const d_event &event)
713 {
714 if (event.type == event_type::window_draw)
715 {
716 menu_draw(menubar);
717 return window_event_result::handled;
718 }
719 else if (event.type == event_type::window_close)
720 {
721 range_for (const auto pi, partial_range(Menu, 1u, num_menus))
722 {
723 auto &i = *pi;
724 if (i.wind)
725 {
726 window_close(std::exchange(i.wind, nullptr));
727 }
728 }
729 menubar.wind = nullptr;
730 }
731
732 switch (state)
733 {
734 case 0:
735 return do_state_0(event);
736 case 1:
737 return do_state_1(event);
738 case 2:
739 return do_state_2(event);
740 case 3:
741 break;
742 default:
743 state = 0;
744 menu_hide_all();
745 }
746 return window_event_result::ignored;
747 }
748
749 static void CommaParse(uint_fast32_t n, char * dest, const PHYSFSX_gets_line_t<200>::line_t &source)
750 {
751 int i{0}, j=0, cn = 0;
752
753 // Go to the n'th comma
754 while (cn < n )
755 if (source[i++] == ',' )
756 cn++;
757 // Read all the whitespace
758 while ( source[i]==' ' || source[i]=='\t' || source[i]==13 || source[i]==10 )
759 i++;
760
761 // Read up until the next comma
762 while ( source[i] != ',' )
763 {
764 dest[j] = source[i++];
765 j++;
766 }
767
768 // Null-terminate
769 dest[j++] = 0;
770 }
771
772 //translate '&' characters to the underline character
773 static void ul_xlate(char *s)
774 {
775 while ((s=strchr(s,'&'))!=NULL)
776 *s = CC_UNDERLINE;
777 }
778
779 static MENU &menu_allocate(MENU *& p)
780 {
781 if (p)
782 return *p;
783 p = new MENU;
784 return *p;
785 }
786
787 }
788
789 int menubar_init(grs_canvas &canvas, const char *const file)
790 {
791 int np;
792 char buf1[200];
793 char buf2[204];
794
795 num_menus = state = 0;
796
797 // This method should be faster than explicitly setting all the variables (I think)
798 Menu = {};
799
800 auto &&[infile, physfserr] = PHYSFSX_openReadBuffered(file);
801 if (!infile)
802 {
803 Warning("Menubar: failed to open \"%s\": %s", file, PHYSFS_getErrorByCode(physfserr));
804 return 0;
805 }
806
807 for (PHYSFSX_gets_line_t<200> buffer; PHYSFSX_fgets(buffer, infile);)
808 {
809 if ( buffer[0] == ';' ) continue;
810 CommaParse(0, buf1, buffer.line());
811 char *p;
812 const auto mi = strtoul(buf1, &p, 10);
813 if (mi >= Menu.size())
814 {
815 con_printf(CON_URGENT, "%s:%u: too many menus defined: max=%" DXX_PRI_size_type ", requested=%lu in \"%s\"", __FILE__, __LINE__, Menu.size(), mi, file);
816 break;
817 }
818
819 CommaParse(1, buf1, buffer.line());
820 const auto ii = strtoul(buf1, &p, 10);
821 auto &menu = menu_allocate(Menu[mi]);
822 if (ii >= menu.Item.size())
823 {
824 con_printf(CON_URGENT, "%s:%u: too many items defined: max=%" DXX_PRI_size_type ", requested=%lu in \"%s\"", __FILE__, __LINE__, menu.Item.size(), ii, file);
825 break;
826 }
827 auto &item = menu.Item[ii];
828
829 CommaParse(2, buf1, buffer.line());
830 ul_xlate(buf1);
831
832 item.Text = d_strdup(buf1[0] == '-' ? buf1 : (snprintf(buf2, sizeof(buf2), " %.197s ", buf1), buf2));
833 item.InactiveText = d_strdup(item.Text.get());
834 for (int i = 0, j = 0;; i++ )
835 {
836 np = item.Text[i];
837 if (np != CC_UNDERLINE)
838 item.InactiveText[j++] = np;
839 if (!np)
840 break;
841 }
842
843 CommaParse(3, buf1, buffer.line());
844 if (buf1[0]=='{' && buf1[1] =='}')
845 item.Hotkey = -1;
846 else {
847 const auto i = DecodeKeyText(buf1);
848 if (i<1) {
849 UserError("Unknown key, %s, in %s\n", buf1, file );
850 } else {
851 item.Hotkey = i;
852 }
853 }
854 CommaParse(4, buf1, buffer.line());
855
856 if (buf1[0])
857 {
858 item.user_function = func_get(buf1, &np);
859
860 if (!item.user_function)
861 {
862 con_printf(CON_URGENT, "%s:%u: unknown function \"%s\" in \"%s\"", __FILE__, __LINE__, buf1, file);
863 }
864 }
865 item.x = menu.x;
866 item.y = menu.y;
867
868 int w, h;
869 if (item.Text[0] == '-')
870 {
871 w = 1; h = 3;
872 } else {
873 const auto r = gr_get_string_size(*canvas.cv_font, item.Text.get());
874 w = r.width + 2;
875 h = r.height + 2;
876 }
877 if (mi == 0)
878 {
879 menu.h = h;
880
881 item.x = menu.x + menu.w;
882
883 item.y = menu.y;
884 auto &next_menu = menu_allocate(Menu[ii + 1]);
885 next_menu.x = menu.x + menu.w;
886 next_menu.y = menu.h - 2;
887
888 item.w = w;
889 item.h = h;
890
891 menu.w += w;
892
893 }else {
894 if (w > menu.w)
895 {
896 menu.w = w;
897 range_for (auto &i, partial_range(menu.Item, menu.NumItems))
898 i.w = w;
899 }
900 item.w = menu.w;
901 item.x = menu.x;
902 item.y = menu.y+menu.h;
903 item.h = h;
904 menu.h += h;
905 }
906 if (ii >= menu.NumItems)
907 {
908 menu.NumItems = ii + 1;
909 }
910
911 if (mi >= num_menus)
912 num_menus = mi + 1;
913 }
914 return 1;
915 }
916
917 void menubar_hide()
918 {
919 state = 0;
920 menu_hide_all();
921 menu_hide(*Menu[0]);
922 }
923
924 void menubar_show()
925 {
926 auto &menu = *Menu[0];
927 if (!menu.wind)
928 {
929 menu.wind = window_create<menubar_window>(grd_curscreen->sc_canvas, menu.x, menu.y, menu.w, menu.h, menu);
930 menu.wind->set_modal(0); // allow windows behind the menubar to accept events (e.g. the keypad dialogs)
931 }
932 menu_show(menu);
933 }
934
935 void menubar_close()
936 {
937 const auto menu = Menu[0];
938 if (!menu)
939 return;
940 auto &menubar = *menu;
941 if (!menubar.wind)
942 return;
943 window_close(std::exchange(menubar.wind, nullptr));
944 }
945
946 }