blob: ccd48b0b99d51296245dcb6a71341dea47145dc3 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
2 * Copyright (c) 2005-2013,2014 Free Software Foundation, Inc. *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28/*
29 * $Id: demo_menus.c,v 1.54 2014/09/05 08:34:06 tom Exp $
30 *
31 * Demonstrate a variety of functions from the menu library.
32 * Thomas Dickey - 2005/4/9
33 */
34/*
35item_description -
36item_init -
37item_opts -
38item_opts_off -
39item_opts_on -
40item_term -
41item_visible -
42menu_back -
43menu_fore -
44menu_format -
45menu_grey -
46menu_init -
47menu_opts -
48menu_pad -
49menu_request_by_name -
50menu_request_name -
51menu_term -
52menu_userptr -
53set_current_item -
54set_item_opts -
55set_menu_grey -
56set_menu_items -
57set_menu_opts -
58set_menu_pad -
59set_menu_pattern -
60set_menu_spacing -
61set_menu_userptr -
62set_top_row -
63top_row -
64*/
65
66#include <test.priv.h>
67
68#if USE_LIBMENU
69
70#include <menu.h>
71
72#include <sys/types.h>
73#include <sys/stat.h>
74
75#ifdef NCURSES_VERSION
76#ifdef TRACE
77static unsigned save_trace = TRACE_ORDINARY | TRACE_CALLS;
78extern unsigned _nc_tracing;
79static MENU *mpTrace;
80#endif
81#else
82#undef TRACE
83#endif
84
85typedef enum {
86 eBanner = -1
87 ,eFile
88 ,eSelect
89#ifdef TRACE
90 ,eTrace
91#endif
92 ,eMAX
93} MenuNo;
94
95#define okMenuNo(n) (((n) > eBanner) && ((n) < eMAX))
96
97#define MENU_Y 1
98
99typedef struct {
100 NCURSES_CONST char *name;
101 void (*func) (int);
102 unsigned mask;
103} MENU_DATA;
104
105static void call_files(int);
106
107static MENU *mpBanner;
108static MENU *mpFile;
109static MENU *mpSelect;
110
111static WINDOW *status;
112
113static bool loaded_file = FALSE;
114
115static char empty[1];
116
117/* Common function to allow ^T to toggle trace-mode in the middle of a test
118 * so that trace-files can be made smaller.
119 */
120static int
121wGetchar(WINDOW *win)
122{
123 int c;
124#ifdef TRACE
125 while ((c = wgetch(win)) == CTRL('T')) {
126 if (_nc_tracing) {
127 save_trace = _nc_tracing;
128 Trace(("TOGGLE-TRACING OFF"));
129 _nc_tracing = 0;
130 } else {
131 _nc_tracing = save_trace;
132 }
133 trace(_nc_tracing);
134 if (_nc_tracing)
135 Trace(("TOGGLE-TRACING ON"));
136 }
137#else
138 c = wgetch(win);
139#endif
140 return c;
141}
142#define Getchar() wGetchar(stdscr)
143
144static int
145menu_virtualize(int c)
146{
147 int result;
148
149 if (c == '\n' || c == KEY_EXIT)
150 result = (MAX_COMMAND + 1);
151 else if (c == 'u')
152 result = (REQ_SCR_ULINE);
153 else if (c == 'd')
154 result = (REQ_SCR_DLINE);
155 else if (c == 'b' || c == KEY_NPAGE)
156 result = (REQ_SCR_UPAGE);
157 else if (c == 'f' || c == KEY_PPAGE)
158 result = (REQ_SCR_DPAGE);
159 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
160 result = (REQ_LEFT_ITEM);
161 else if (c == 'n' || c == KEY_DOWN)
162 result = (REQ_NEXT_ITEM);
163 else if (c == 'p' || c == KEY_UP)
164 result = (REQ_PREV_ITEM);
165 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
166 result = (REQ_RIGHT_ITEM);
167 else if (c == ' ')
168 result = (REQ_TOGGLE_ITEM);
169 else {
170 if (c != KEY_MOUSE)
171 beep();
172 result = (c);
173 }
174 return result;
175}
176
177static int
178menu_getc(MENU * m)
179{
180 return wGetchar(menu_win(m));
181}
182
183static int
184menu_offset(MenuNo number)
185{
186 int result = 0;
187
188 if (okMenuNo(number)) {
189 int spc_desc, spc_rows, spc_cols;
190
191#ifdef NCURSES_VERSION
192 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
193#else
194 spc_rows = 0;
195#endif
196
197 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
198 result = (number - (eBanner + 1)) * (menu_itemwidth(mpBanner) + spc_rows);
199 }
200 return result;
201}
202
203static void
204my_menu_init(MENU * menu)
205{
206 Trace(("called MenuHook my_menu_init"));
207 mvwprintw(status, 2, 0, "menu_init %p", (void *) menu);
208 wclrtoeol(status);
209 wrefresh(status);
210}
211
212static void
213my_menu_term(MENU * menu)
214{
215 Trace(("called MenuHook my_menu_term"));
216 mvwprintw(status, 2, 0, "menu_term %p", (void *) menu);
217 wclrtoeol(status);
218 wrefresh(status);
219}
220
221static void
222my_item_init(MENU * menu)
223{
224 ITEM *item = current_item(menu);
225 const char *name = item_name(item);
226
227 Trace(("called MenuHook my_item_init (%s)", name));
228 mvwprintw(status, 2, 0, "item_init %s", name);
229 wclrtoeol(status);
230 wrefresh(status);
231}
232
233static void
234my_item_term(MENU * menu)
235{
236 ITEM *item = current_item(menu);
237 const char *name = item_name(item);
238
239 Trace(("called MenuHook my_item_term (%s)", name));
240 mvwprintw(status, 2, 0, "item_term %s", name);
241 wclrtoeol(status);
242 wrefresh(status);
243}
244
245static MENU *
246menu_create(ITEM ** items, int count, int ncols, MenuNo number)
247{
248 MENU *result;
249 WINDOW *menuwin;
250 int mrows, mcols;
251 int y = okMenuNo(number) ? MENU_Y : 0;
252 int x = menu_offset(number);
253 int margin = (y == MENU_Y) ? 1 : 0;
254 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
255 int maxrow = (count + 1) / ncols;
256
257 if ((maxrow + y) >= (LINES - 4))
258 maxrow = LINES - 4 - y;
259
260 result = new_menu(items);
261
262 if (has_colors()) {
263 set_menu_fore(result, (chtype) COLOR_PAIR(1));
264 set_menu_back(result, (chtype) COLOR_PAIR(2));
265 }
266
267 set_menu_format(result, maxrow, maxcol);
268 scale_menu(result, &mrows, &mcols);
269
270 if (mcols + (2 * margin + x) >= COLS)
271 mcols = COLS - (2 * margin + x);
272
273#ifdef TRACE
274 if (number == eTrace)
275 menu_opts_off(result, O_ONEVALUE);
276 else
277 menu_opts_on(result, O_ONEVALUE);
278#endif
279
280 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
281 set_menu_win(result, menuwin);
282 keypad(menuwin, TRUE);
283 if (margin)
284 box(menuwin, 0, 0);
285
286 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
287
288 post_menu(result);
289
290 set_menu_init(result, my_menu_init);
291 set_menu_term(result, my_menu_term);
292 set_item_init(result, my_item_init);
293 set_item_term(result, my_item_term);
294 return result;
295}
296
297static void
298menu_destroy(MENU * m)
299{
300 int count;
301
302 Trace(("menu_destroy %p", (void *) m));
303 if (m != 0) {
304 ITEM **items = menu_items(m);
305 const char *blob = 0;
306
307 count = item_count(m);
308 Trace(("menu_destroy %p count %d", (void *) m, count));
309 if ((count > 0) && (m == mpSelect)) {
310 blob = item_name(*items);
311 }
312
313 unpost_menu(m);
314 free_menu(m);
315
316 /* free the extra data allocated in build_select_menu() */
317 if ((count > 0) && (m == mpSelect)) {
318 if (blob && loaded_file) {
319 Trace(("freeing blob %p", blob));
320 free((void *) blob);
321 }
322 free(items);
323 items = 0;
324 }
325#ifdef TRACE
326 if ((count > 0) && (m == mpTrace)) {
327 ITEM **ip = items;
328 if (ip != 0) {
329 while (*ip)
330 free(*ip++);
331 }
332 }
333#endif
334 }
335}
336
337/* force the given menu to appear */
338static void
339menu_display(MENU * m)
340{
341 touchwin(menu_win(m));
342 wrefresh(menu_win(m));
343}
344
345/*****************************************************************************/
346
347static void
348build_file_menu(MenuNo number)
349{
350 static MENU_DATA table[] =
351 {
352 {"Exit", call_files, 0},
353 {(char *) 0, 0, 0}
354 };
355 static ITEM *items[SIZEOF(table)];
356
357 ITEM **ip = items;
358 int n;
359
360 for (n = 0; table[n].name != 0; ++n) {
361 *ip = new_item(table[n].name, empty);
362 set_item_userptr(*ip, (void *) &table[n]);
363 ++ip;
364 }
365 *ip = (ITEM *) 0;
366
367 mpFile = menu_create(items, SIZEOF(table) - 1, 1, number);
368}
369
370static int
371perform_file_menu(int cmd)
372{
373 return menu_driver(mpFile, cmd);
374}
375
376/*****************************************************************************/
377
378static void
379call_select(int code)
380{
381 (void) code;
382 Trace(("Selected item %d", code));
383}
384
385static void
386build_select_menu(MenuNo number, char *filename)
387{
388#define MY_DATA(name) { name, call_select, 0 }
389 static MENU_DATA table[] =
390 {
391 MY_DATA("Lions"),
392 MY_DATA("Tigers"),
393 MY_DATA("Bears"),
394 MY_DATA("(Oh my!)"),
395 MY_DATA("Newts"),
396 MY_DATA("Platypi"),
397 MY_DATA("Lemurs"),
398 MY_DATA("(Oh really?!)"),
399 MY_DATA("Leopards"),
400 MY_DATA("Panthers"),
401 MY_DATA("Pumas"),
402 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
403 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
404 {(char *) 0, 0, 0}
405 };
406 static ITEM **items;
407
408 ITEM **ip;
409 MENU_DATA *ap = 0;
410 MENU_DATA *myList = 0;
411 int i;
412 size_t count = 0;
413
414 if (filename != 0) {
415 struct stat sb;
416 if (stat(filename, &sb) == 0
417 && (sb.st_mode & S_IFMT) == S_IFREG
418 && sb.st_size != 0) {
419 size_t size = (size_t) sb.st_size;
420 unsigned j, k;
421 char *blob = typeMalloc(char, size + 1);
422 MENU_DATA *list = typeCalloc(MENU_DATA, size + 1);
423
424 items = typeCalloc(ITEM *, size + 1);
425 Trace(("build_select_menu blob=%p, items=%p",
426 (void *) blob,
427 (void *) items));
428 if (blob != 0 && list != 0) {
429 FILE *fp = fopen(filename, "r");
430 if (fp != 0) {
431 if (fread(blob, sizeof(char), size, fp) == size) {
432 bool mark = TRUE;
433 for (j = k = 0; j < size; ++j) {
434 if (mark) {
435 list[k++].name = blob + j;
436 mark = FALSE;
437 }
438 if (blob[j] == '\n') {
439 blob[j] = '\0';
440 if (k > 0 && *list[k - 1].name == '\0')
441 --k;
442 mark = TRUE;
443 } else if (blob[j] == '\t') {
444 blob[j] = ' '; /* menu items are printable */
445 }
446 }
447 list[k].name = 0;
448 count = k;
449 ap = myList = list;
450 }
451 fclose(fp);
452 }
453 loaded_file = TRUE;
454 }
455 if (ap == 0)
456 free(items);
457 }
458 }
459 if (ap == 0) {
460 count = SIZEOF(table) - 1;
461 items = typeCalloc(ITEM *, count + 1);
462 ap = table;
463 }
464
465 ip = items;
466 for (i = 0; ap[i].name != 0; ++i) {
467 ap[i].func = call_select;
468 ap[i].mask = (unsigned) i;
469 *ip = new_item(ap[i].name, empty);
470 set_item_userptr(*ip, (void *) &table[i]);
471 ++ip;
472 }
473 *ip = 0;
474
475 mpSelect = menu_create(items, (int) count, 1, number);
476 if (myList != 0)
477 free(myList);
478}
479
480static int
481perform_select_menu(int cmd)
482{
483 return menu_driver(mpSelect, cmd);
484}
485
486/*****************************************************************************/
487
488#ifdef TRACE
489
490static void
491call_trace(int code)
492{
493 (void) code;
494 Trace(("Updating trace mask %d", code));
495}
496
497#define T_TBL(name) { #name, call_trace, name }
498static MENU_DATA t_tbl[] =
499{
500
501 T_TBL(TRACE_DISABLE),
502 T_TBL(TRACE_TIMES),
503 T_TBL(TRACE_TPUTS),
504 T_TBL(TRACE_UPDATE),
505 T_TBL(TRACE_MOVE),
506 T_TBL(TRACE_CHARPUT),
507 T_TBL(TRACE_ORDINARY),
508 T_TBL(TRACE_CALLS),
509 T_TBL(TRACE_VIRTPUT),
510 T_TBL(TRACE_IEVENT),
511 T_TBL(TRACE_BITS),
512 T_TBL(TRACE_ICALLS),
513 T_TBL(TRACE_CCALLS),
514 T_TBL(TRACE_DATABASE),
515 T_TBL(TRACE_ATTRS),
516 T_TBL(TRACE_MAXIMUM),
517 {
518 (char *) 0, 0, 0
519 }
520};
521
522static void
523build_trace_menu(MenuNo number)
524{
525 static ITEM *items[SIZEOF(t_tbl)];
526
527 ITEM **ip = items;
528 int n;
529
530 for (n = 0; t_tbl[n].name != 0; n++) {
531 *ip = new_item(t_tbl[n].name, empty);
532 set_item_userptr(*ip, (void *) &t_tbl[n]);
533 ++ip;
534 }
535 *ip = (ITEM *) 0;
536
537 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
538}
539
540static char *
541tracetrace(unsigned tlevel)
542{
543 static char *buf;
544 int n;
545
546 if (buf == 0) {
547 size_t need = 12;
548 for (n = 0; t_tbl[n].name != 0; n++)
549 need += strlen(t_tbl[n].name) + 2;
550 buf = typeMalloc(char, need);
551 }
552 sprintf(buf, "0x%02x = {", tlevel);
553 if (tlevel == 0) {
554 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
555 } else {
556 for (n = 1; t_tbl[n].name != 0; n++)
557 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
558 strcat(buf, t_tbl[n].name);
559 strcat(buf, ", ");
560 }
561 }
562 if (buf[strlen(buf) - 2] == ',')
563 buf[strlen(buf) - 2] = '\0';
564 return (strcat(buf, "}"));
565}
566
567/* fake a dynamically reconfigurable menu using the 0th entry to deselect
568 * the others
569 */
570static bool
571update_trace_menu(MENU * m)
572{
573 ITEM **items;
574 ITEM *i, **p;
575 bool changed = FALSE;
576
577 items = menu_items(m);
578 i = current_item(m);
579 if (i == items[0]) {
580 if (item_value(i)) {
581 for (p = items + 1; *p != 0; p++)
582 if (item_value(*p)) {
583 set_item_value(*p, FALSE);
584 changed = TRUE;
585 }
586 }
587 }
588 return changed;
589}
590
591static int
592perform_trace_menu(int cmd)
593/* interactively set the trace level */
594{
595 ITEM **ip;
596 unsigned newtrace;
597 int result;
598
599 for (ip = menu_items(mpTrace); *ip; ip++) {
600 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
601 unsigned mask = td->mask;
602 if (mask == 0)
603 set_item_value(*ip, _nc_tracing == 0);
604 else if ((mask & _nc_tracing) == mask)
605 set_item_value(*ip, TRUE);
606 }
607
608 result = menu_driver(mpTrace, cmd);
609
610 if (result == E_OK) {
611 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
612 newtrace = 0;
613 for (ip = menu_items(mpTrace); *ip; ip++) {
614 if (item_value(*ip)) {
615 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
616 newtrace |= td->mask;
617 }
618 }
619 trace(newtrace);
620 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
621
622 MvWPrintw(status, 1, 0,
623 "Trace level is %s\n", tracetrace(_nc_tracing));
624 wrefresh(status);
625 }
626 }
627 return result;
628}
629#endif /* TRACE */
630
631/*****************************************************************************/
632
633static int
634menu_number(void)
635{
636 return item_index(current_item(mpBanner)) - (eBanner + 1);
637}
638
639static MENU *
640current_menu(void)
641{
642 MENU *result;
643
644 switch (menu_number()) {
645 case eFile:
646 result = mpFile;
647 break;
648 case eSelect:
649 result = mpSelect;
650 break;
651#ifdef TRACE
652 case eTrace:
653 result = mpTrace;
654 break;
655#endif
656 default:
657 result = 0;
658 break;
659 }
660 return result;
661}
662
663static void
664call_menus(int code)
665{
666 (void) code;
667 Trace(("Activated menu %d\n", code));
668}
669
670static void
671build_menus(char *filename)
672{
673 static MENU_DATA table[] =
674 {
675 {"File", call_menus, 0},
676 {"Select", call_menus, 1},
677#ifdef TRACE
678 {"Trace", call_menus, 2},
679#endif
680 {(char *) 0, 0, 0}
681 };
682 static ITEM *items[SIZEOF(table)];
683
684 ITEM **ip = items;
685 int n;
686
687 for (n = 0; table[n].name != 0; ++n) {
688 *ip = new_item(table[n].name, empty);
689 set_item_userptr(*ip, (void *) &table[n]);
690 ++ip;
691 }
692 *ip = (ITEM *) 0;
693
694 mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
695 set_menu_mark(mpBanner, ">");
696
697 build_file_menu(eFile);
698 build_select_menu(eSelect, filename);
699#ifdef TRACE
700 build_trace_menu(eTrace);
701#endif
702}
703
704static int
705move_menu(MENU * menu, MENU * current, int by_y, int by_x)
706{
707 WINDOW *top_win = menu_win(menu);
708 WINDOW *sub_win = menu_sub(menu);
709 int y0, x0;
710 int y1, x1;
711 int result;
712
713 getbegyx(top_win, y0, x0);
714 y0 += by_y;
715 x0 += by_x;
716
717 getbegyx(sub_win, y1, x1);
718 y1 += by_y;
719 x1 += by_x;
720
721 if ((result = mvwin(top_win, y0, x0)) != ERR) {
722#if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
723 sub_win->_begy = y1;
724 sub_win->_begx = x1;
725#else
726 mvwin(sub_win, y1, x1);
727#endif
728 if (menu == current) {
729 touchwin(top_win);
730 wnoutrefresh(top_win);
731 }
732 }
733 return result;
734}
735
736/*
737 * Move the menus around on the screen, to test mvwin().
738 */
739static void
740move_menus(MENU * current, int by_y, int by_x)
741{
742 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
743 erase();
744 wnoutrefresh(stdscr);
745 move_menu(mpFile, current, by_y, by_x);
746 move_menu(mpSelect, current, by_y, by_x);
747#ifdef TRACE
748 move_menu(mpTrace, current, by_y, by_x);
749#endif
750 doupdate();
751 }
752}
753
754static void
755show_status(int ch, MENU * menu)
756{
757 wmove(status, 0, 0);
758 wprintw(status, "key %s, menu %d, mark %s, match %s",
759 keyname(ch),
760 menu_number(),
761 menu_mark(menu),
762 menu_pattern(menu));
763 wclrtoeol(status);
764 wrefresh(status);
765}
766
767static void
768perform_menus(void)
769{
770 MENU *this_menu;
771 MENU *last_menu = mpFile;
772 int code = E_UNKNOWN_COMMAND;
773 int cmd;
774 int ch = ERR;
775
776#ifdef NCURSES_MOUSE_VERSION
777 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
778#endif
779
780 menu_display(last_menu);
781
782 for (;;) {
783
784 if (ch != ERR)
785 show_status(ch, last_menu);
786
787 ch = menu_getc(mpBanner);
788
789 /*
790 * Provide for moving the menu around in the screen using shifted
791 * cursor keys.
792 */
793 switch (ch) {
794 case KEY_SF:
795 move_menus(last_menu, 1, 0);
796 continue;
797 case KEY_SR:
798 move_menus(last_menu, -1, 0);
799 continue;
800 case KEY_SLEFT:
801 move_menus(last_menu, 0, -1);
802 continue;
803 case KEY_SRIGHT:
804 move_menus(last_menu, 0, 1);
805 continue;
806 }
807 cmd = menu_virtualize(ch);
808
809 switch (cmd) {
810 /*
811 * The banner menu acts solely to select one of the other menus.
812 * Move between its items, wrapping at the left/right limits.
813 */
814 case REQ_LEFT_ITEM:
815 case REQ_RIGHT_ITEM:
816 code = menu_driver(mpBanner, cmd);
817 if (code == E_REQUEST_DENIED) {
818 if (menu_number() > 0)
819 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
820 else
821 code = menu_driver(mpBanner, REQ_LAST_ITEM);
822 }
823 break;
824 default:
825 switch (menu_number()) {
826 case eFile:
827 code = perform_file_menu(cmd);
828 break;
829 case eSelect:
830 code = perform_select_menu(cmd);
831 break;
832#ifdef TRACE
833 case eTrace:
834 code = perform_trace_menu(cmd);
835 break;
836#endif
837 }
838
839 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
840 code = menu_driver(mpBanner, cmd);
841 }
842
843 break;
844 }
845
846 if (code == E_OK) {
847 this_menu = current_menu();
848 if (this_menu != last_menu) {
849 move(1, 0);
850 clrtobot();
851 box(menu_win(this_menu), 0, 0);
852 refresh();
853
854 /* force the current menu to appear */
855 menu_display(this_menu);
856
857 last_menu = this_menu;
858 }
859 }
860 wrefresh(menu_win(last_menu));
861 if (code == E_UNKNOWN_COMMAND
862 || code == E_NOT_POSTED) {
863 ITEM *item = current_item(last_menu);
864 MENU_DATA *td = (MENU_DATA *) item_userptr(item);
865 td->func((int) td->mask);
866 }
867 if (code == E_REQUEST_DENIED)
868 beep();
869 continue;
870 }
871
872#ifdef NCURSES_MOUSE_VERSION
873 mousemask(0, (mmask_t *) 0);
874#endif
875}
876
877static void
878destroy_menus(void)
879{
880 menu_destroy(mpFile);
881 menu_destroy(mpSelect);
882#ifdef TRACE
883 menu_destroy(mpTrace);
884#endif
885 menu_destroy(mpBanner);
886}
887
888#if HAVE_RIPOFFLINE
889static int
890rip_footer(WINDOW *win, int cols)
891{
892 wbkgd(win, A_REVERSE);
893 werase(win);
894 wmove(win, 0, 0);
895 wprintw(win, "footer: %d columns", cols);
896 wnoutrefresh(win);
897 return OK;
898}
899
900static int
901rip_header(WINDOW *win, int cols)
902{
903 wbkgd(win, A_REVERSE);
904 werase(win);
905 wmove(win, 0, 0);
906 wprintw(win, "header: %d columns", cols);
907 wnoutrefresh(win);
908 return OK;
909}
910#endif /* HAVE_RIPOFFLINE */
911
912static void
913call_files(int code)
914{
915 switch (code) {
916 case 0:
917 destroy_menus();
918 endwin();
919 printf("DONE!\n");
920 ExitProgram(EXIT_SUCCESS);
921 }
922}
923
924static void
925usage(void)
926{
927 static const char *const tbl[] =
928 {
929 "Usage: demo_menus [options] [menu-file]"
930 ,""
931 ,"Options:"
932#if HAVE_RIPOFFLINE
933 ," -f rip-off footer line (can repeat)"
934 ," -h rip-off header line (can repeat)"
935#endif
936#ifdef TRACE
937 ," -t mask specify default trace-level (may toggle with ^T)"
938#endif
939 };
940 size_t n;
941 for (n = 0; n < SIZEOF(tbl); n++)
942 fprintf(stderr, "%s\n", tbl[n]);
943 ExitProgram(EXIT_FAILURE);
944}
945
946int
947main(int argc, char *argv[])
948{
949 int c;
950
951 setlocale(LC_ALL, "");
952
953 while ((c = getopt(argc, argv, "a:de:fhmp:s:t:")) != -1) {
954 switch (c) {
955#if HAVE_RIPOFFLINE
956 case 'f':
957 ripoffline(-1, rip_footer);
958 break;
959 case 'h':
960 ripoffline(1, rip_header);
961 break;
962#endif /* HAVE_RIPOFFLINE */
963#ifdef TRACE
964 case 't':
965 trace((unsigned) strtoul(optarg, 0, 0));
966 break;
967#endif
968 default:
969 usage();
970 }
971 }
972
973 initscr();
974 noraw();
975 cbreak();
976 noecho();
977
978 if (has_colors()) {
979 start_color();
980 init_pair(1, COLOR_RED, COLOR_BLACK);
981 init_pair(2, COLOR_BLUE, COLOR_WHITE);
982 }
983 status = newwin(3, COLS, LINES - 3, 0);
984 build_menus(argc > 1 ? argv[1] : 0);
985 perform_menus();
986 destroy_menus();
987
988 endwin();
989 ExitProgram(EXIT_SUCCESS);
990}
991#else
992int
993main(void)
994{
995 printf("This program requires the curses menu library\n");
996 ExitProgram(EXIT_FAILURE);
997}
998#endif