]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_menus.c
ncurses 6.0 - patch 20160827
[ncurses.git] / test / demo_menus.c
1 /****************************************************************************
2  * Copyright (c) 2005-2015,2016 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.59 2016/03/27 00:02:01 tom Exp $
30  *
31  * Demonstrate a variety of functions from the menu library.
32  * Thomas Dickey - 2005/4/9
33  */
34 /*
35 item_description                -
36 item_init                       -
37 item_opts                       -
38 item_opts_off                   -
39 item_opts_on                    -
40 item_term                       -
41 item_visible                    -
42 menu_back                       -
43 menu_fore                       -
44 menu_format                     -
45 menu_grey                       -
46 menu_init                       -
47 menu_opts                       -
48 menu_pad                        -
49 menu_request_by_name            -
50 menu_request_name               -
51 menu_term                       -
52 menu_userptr                    -
53 set_current_item                -
54 set_item_opts                   -
55 set_menu_grey                   -
56 set_menu_items                  -
57 set_menu_opts                   -
58 set_menu_pad                    -
59 set_menu_pattern                -
60 set_menu_spacing                -
61 set_menu_userptr                -
62 set_top_row                     -
63 top_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
77 static unsigned save_trace = TRACE_ORDINARY | TRACE_CALLS;
78 extern unsigned _nc_tracing;
79 static MENU *mpTrace;
80 #endif
81 #else
82 #undef TRACE
83 #endif
84
85 typedef 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
99 typedef struct {
100     NCURSES_CONST char *name;
101     void (*func) (int);
102     unsigned mask;
103 } MENU_DATA;
104
105 static void call_files(int);
106
107 static MENU *mpBanner;
108 static MENU *mpFile;
109 static MENU *mpSelect;
110
111 static WINDOW *status;
112
113 static bool loaded_file = FALSE;
114
115 static 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  */
120 static int
121 wGetchar(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
144 static int
145 menu_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
177 static int
178 menu_getc(MENU * m)
179 {
180     return wGetchar(menu_win(m));
181 }
182
183 static int
184 menu_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
203 static void
204 my_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
212 static void
213 my_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
221 static void
222 my_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
233 static void
234 my_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
245 static MENU *
246 menu_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     menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
274     set_menu_win(result, menuwin);
275     keypad(menuwin, TRUE);
276     if (margin)
277         box(menuwin, 0, 0);
278
279     set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
280
281 #ifdef TRACE
282     if (number == eTrace)
283         menu_opts_off(result, O_ONEVALUE);
284     else
285         menu_opts_on(result, O_ONEVALUE);
286 #endif
287 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
288     menu_opts_on(result, O_MOUSE_MENU);
289 #endif
290
291     post_menu(result);
292
293     set_menu_init(result, my_menu_init);
294     set_menu_term(result, my_menu_term);
295     set_item_init(result, my_item_init);
296     set_item_term(result, my_item_term);
297     return result;
298 }
299
300 static void
301 menu_destroy(MENU * m)
302 {
303     int count;
304
305     Trace(("menu_destroy %p", (void *) m));
306     if (m != 0) {
307         ITEM **items = menu_items(m);
308         const char *blob = 0;
309
310         count = item_count(m);
311         Trace(("menu_destroy %p count %d", (void *) m, count));
312         if ((count > 0) && (m == mpSelect)) {
313             blob = item_name(*items);
314         }
315
316         unpost_menu(m);
317         free_menu(m);
318
319         /* free the extra data allocated in build_select_menu() */
320         if ((count > 0) && (m == mpSelect)) {
321             if (blob && loaded_file) {
322                 Trace(("freeing blob %p", blob));
323                 free((void *) blob);
324             }
325             free(items);
326             items = 0;
327         }
328 #ifdef TRACE
329         if ((count > 0) && (m == mpTrace)) {
330             ITEM **ip = items;
331             if (ip != 0) {
332                 while (*ip)
333                     free(*ip++);
334             }
335         }
336 #endif
337     }
338 }
339
340 /* force the given menu to appear */
341 static void
342 menu_display(MENU * m)
343 {
344     touchwin(menu_win(m));
345     wrefresh(menu_win(m));
346 }
347
348 /*****************************************************************************/
349
350 static void
351 build_file_menu(MenuNo number)
352 {
353     static MENU_DATA table[] =
354     {
355         {"Exit", call_files, 0},
356         {(char *) 0, 0, 0}
357     };
358     static ITEM *items[SIZEOF(table)];
359
360     ITEM **ip = items;
361     int n;
362
363     for (n = 0; table[n].name != 0; ++n) {
364         *ip = new_item(table[n].name, empty);
365         set_item_userptr(*ip, (void *) &table[n]);
366         ++ip;
367     }
368     *ip = (ITEM *) 0;
369
370     mpFile = menu_create(items, SIZEOF(table) - 1, 1, number);
371 }
372
373 static int
374 perform_file_menu(int cmd)
375 {
376     return menu_driver(mpFile, cmd);
377 }
378
379 /*****************************************************************************/
380
381 static void
382 call_select(int code)
383 {
384     (void) code;
385     Trace(("Selected item %d", code));
386 }
387
388 static void
389 build_select_menu(MenuNo number, char *filename)
390 {
391 #define MY_DATA(name) { name, call_select, 0 }
392     static MENU_DATA table[] =
393     {
394         MY_DATA("Lions"),
395         MY_DATA("Tigers"),
396         MY_DATA("Bears"),
397         MY_DATA("(Oh my!)"),
398         MY_DATA("Newts"),
399         MY_DATA("Platypi"),
400         MY_DATA("Lemurs"),
401         MY_DATA("(Oh really?!)"),
402         MY_DATA("Leopards"),
403         MY_DATA("Panthers"),
404         MY_DATA("Pumas"),
405         MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
406         MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
407         {(char *) 0, 0, 0}
408     };
409     static ITEM **items;
410
411     ITEM **ip;
412     MENU_DATA *ap = 0;
413     MENU_DATA *myList = 0;
414     int i;
415     size_t count = 0;
416
417     if (filename != 0) {
418         struct stat sb;
419         if (stat(filename, &sb) == 0
420             && (sb.st_mode & S_IFMT) == S_IFREG
421             && sb.st_size != 0) {
422             size_t size = (size_t) sb.st_size;
423             unsigned j, k;
424             char *blob = typeMalloc(char, size + 1);
425             MENU_DATA *list = typeCalloc(MENU_DATA, size + 1);
426
427             items = typeCalloc(ITEM *, size + 1);
428             Trace(("build_select_menu blob=%p, items=%p",
429                    (void *) blob,
430                    (void *) items));
431             if (blob != 0 && list != 0) {
432                 FILE *fp = fopen(filename, "r");
433                 if (fp != 0) {
434                     if (fread(blob, sizeof(char), size, fp) == size) {
435                         bool mark = TRUE;
436                         for (j = k = 0; j < size; ++j) {
437                             if (mark) {
438                                 list[k++].name = blob + j;
439                                 mark = FALSE;
440                             }
441                             if (blob[j] == '\n') {
442                                 blob[j] = '\0';
443                                 if (k > 0 && *list[k - 1].name == '\0')
444                                     --k;
445                                 mark = TRUE;
446                             } else if (blob[j] == '\t') {
447                                 blob[j] = ' ';  /* menu items are printable */
448                             }
449                         }
450                         list[k].name = 0;
451                         count = k;
452                         ap = myList = list;
453                     }
454                     fclose(fp);
455                 }
456                 loaded_file = TRUE;
457             }
458             if (ap == 0)
459                 free(items);
460         }
461     }
462     if (ap == 0) {
463         count = SIZEOF(table) - 1;
464         items = typeCalloc(ITEM *, count + 1);
465         ap = table;
466     }
467
468     ip = items;
469     for (i = 0; ap[i].name != 0; ++i) {
470         ap[i].func = call_select;
471         ap[i].mask = (unsigned) i;
472         *ip = new_item(ap[i].name, empty);
473         set_item_userptr(*ip, (void *) &table[i]);
474         ++ip;
475     }
476     *ip = 0;
477
478     mpSelect = menu_create(items, (int) count, 1, number);
479     if (myList != 0)
480         free(myList);
481 }
482
483 static int
484 perform_select_menu(int cmd)
485 {
486     return menu_driver(mpSelect, cmd);
487 }
488
489 /*****************************************************************************/
490
491 #ifdef TRACE
492
493 static void
494 call_trace(int code)
495 {
496     (void) code;
497     Trace(("Updating trace mask %d", code));
498 }
499
500 #define T_TBL(name) { #name, call_trace, name }
501 static MENU_DATA t_tbl[] =
502 {
503
504     T_TBL(TRACE_DISABLE),
505     T_TBL(TRACE_TIMES),
506     T_TBL(TRACE_TPUTS),
507     T_TBL(TRACE_UPDATE),
508     T_TBL(TRACE_MOVE),
509     T_TBL(TRACE_CHARPUT),
510     T_TBL(TRACE_ORDINARY),
511     T_TBL(TRACE_CALLS),
512     T_TBL(TRACE_VIRTPUT),
513     T_TBL(TRACE_IEVENT),
514     T_TBL(TRACE_BITS),
515     T_TBL(TRACE_ICALLS),
516     T_TBL(TRACE_CCALLS),
517     T_TBL(TRACE_DATABASE),
518     T_TBL(TRACE_ATTRS),
519     T_TBL(TRACE_MAXIMUM),
520     {
521         (char *) 0, 0, 0
522     }
523 };
524
525 static void
526 build_trace_menu(MenuNo number)
527 {
528     static ITEM *items[SIZEOF(t_tbl)];
529
530     ITEM **ip = items;
531     int n;
532
533     for (n = 0; t_tbl[n].name != 0; n++) {
534         *ip = new_item(t_tbl[n].name, empty);
535         set_item_userptr(*ip, (void *) &t_tbl[n]);
536         ++ip;
537     }
538     *ip = (ITEM *) 0;
539
540     mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
541 }
542
543 static char *
544 tracetrace(unsigned tlevel)
545 {
546     static char *buf;
547     int n;
548
549     if (buf == 0) {
550         size_t need = 12;
551         for (n = 0; t_tbl[n].name != 0; n++)
552             need += strlen(t_tbl[n].name) + 2;
553         buf = typeMalloc(char, need);
554     }
555     sprintf(buf, "0x%02x = {", tlevel);
556     if (tlevel == 0) {
557         sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
558     } else {
559         for (n = 1; t_tbl[n].name != 0; n++)
560             if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
561                 strcat(buf, t_tbl[n].name);
562                 strcat(buf, ", ");
563             }
564     }
565     if (buf[strlen(buf) - 2] == ',')
566         buf[strlen(buf) - 2] = '\0';
567     return (strcat(buf, "}"));
568 }
569
570 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
571  * the others
572  */
573 static bool
574 update_trace_menu(MENU * m)
575 {
576     ITEM **items;
577     ITEM *i, **p;
578     bool changed = FALSE;
579
580     items = menu_items(m);
581     i = current_item(m);
582     if (i == items[0]) {
583         if (item_value(i)) {
584             for (p = items + 1; *p != 0; p++)
585                 if (item_value(*p)) {
586                     set_item_value(*p, FALSE);
587                     changed = TRUE;
588                 }
589         }
590     }
591     return changed;
592 }
593
594 static int
595 perform_trace_menu(int cmd)
596 /* interactively set the trace level */
597 {
598     ITEM **ip;
599     unsigned newtrace;
600     int result;
601
602     for (ip = menu_items(mpTrace); *ip; ip++) {
603         MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
604         unsigned mask = td->mask;
605         if (mask == 0)
606             set_item_value(*ip, _nc_tracing == 0);
607         else if ((mask & _nc_tracing) == mask)
608             set_item_value(*ip, TRUE);
609     }
610
611     result = menu_driver(mpTrace, cmd);
612
613     if (result == E_OK) {
614         if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
615             newtrace = 0;
616             for (ip = menu_items(mpTrace); *ip; ip++) {
617                 if (item_value(*ip)) {
618                     MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
619                     newtrace |= td->mask;
620                 }
621             }
622             trace(newtrace);
623             Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
624
625             MvWPrintw(status, 1, 0,
626                       "Trace level is %s\n", tracetrace(_nc_tracing));
627             wrefresh(status);
628         }
629     }
630     return result;
631 }
632 #endif /* TRACE */
633
634 /*****************************************************************************/
635
636 static int
637 menu_number(void)
638 {
639     return item_index(current_item(mpBanner)) - (eBanner + 1);
640 }
641
642 static MENU *
643 current_menu(void)
644 {
645     MENU *result;
646
647     switch (menu_number()) {
648     case eFile:
649         result = mpFile;
650         break;
651     case eSelect:
652         result = mpSelect;
653         break;
654 #ifdef TRACE
655     case eTrace:
656         result = mpTrace;
657         break;
658 #endif
659     default:
660         result = 0;
661         break;
662     }
663     return result;
664 }
665
666 static void
667 call_menus(int code)
668 {
669     (void) code;
670     Trace(("Activated menu %d\n", code));
671 }
672
673 static void
674 build_menus(char *filename)
675 {
676     static MENU_DATA table[] =
677     {
678         {"File", call_menus, 0},
679         {"Select", call_menus, 1},
680 #ifdef TRACE
681         {"Trace", call_menus, 2},
682 #endif
683         {(char *) 0, 0, 0}
684     };
685     static ITEM *items[SIZEOF(table)];
686
687     ITEM **ip = items;
688     int n;
689
690     for (n = 0; table[n].name != 0; ++n) {
691         *ip = new_item(table[n].name, empty);
692         set_item_userptr(*ip, (void *) &table[n]);
693         ++ip;
694     }
695     *ip = (ITEM *) 0;
696
697     mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
698     set_menu_mark(mpBanner, ">");
699
700     build_file_menu(eFile);
701     build_select_menu(eSelect, filename);
702 #ifdef TRACE
703     build_trace_menu(eTrace);
704 #endif
705 }
706
707 static int
708 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
709 {
710     WINDOW *top_win = menu_win(menu);
711     WINDOW *sub_win = menu_sub(menu);
712     int y0, x0;
713     int y1, x1;
714     int result;
715
716     getbegyx(top_win, y0, x0);
717     y0 += by_y;
718     x0 += by_x;
719
720     getbegyx(sub_win, y1, x1);
721     y1 += by_y;
722     x1 += by_x;
723
724     if ((result = mvwin(top_win, y0, x0)) != ERR) {
725 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
726         sub_win->_begy = y1;
727         sub_win->_begx = x1;
728 #else
729         mvwin(sub_win, y1, x1);
730 #endif
731         if (menu == current) {
732             touchwin(top_win);
733             wnoutrefresh(top_win);
734         }
735     }
736     return result;
737 }
738
739 /*
740  * Move the menus around on the screen, to test mvwin().
741  */
742 static void
743 move_menus(MENU * current, int by_y, int by_x)
744 {
745     if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
746         erase();
747         wnoutrefresh(stdscr);
748         move_menu(mpFile, current, by_y, by_x);
749         move_menu(mpSelect, current, by_y, by_x);
750 #ifdef TRACE
751         move_menu(mpTrace, current, by_y, by_x);
752 #endif
753         doupdate();
754     }
755 }
756
757 #ifdef KEY_RESIZE
758 static void
759 resize_menu(MENU ** menu)
760 {
761 #if 0
762     WINDOW *win = menu_win(*menu);
763     WINDOW *sub = menu_sub(*menu);
764 #endif
765     (void) menu;
766 }
767
768 static void
769 resize_menus(MENU * current)
770 {
771     (void) current;
772
773     werase(status);
774     wnoutrefresh(status);
775     wresize(status, 1, COLS);
776     mvwin(status, LINES - 1, 0);
777
778     resize_menu(&mpBanner);
779     resize_menu(&mpFile);
780     resize_menu(&mpSelect);
781 #ifdef TRACE
782     resize_menu(&mpTrace);
783 #endif
784 }
785 #endif
786
787 static void
788 show_status(int ch, MENU * menu)
789 {
790     wmove(status, 0, 0);
791     wprintw(status, "key %s, menu %d, mark %s, match %s",
792             keyname(ch),
793             menu_number(),
794             menu_mark(menu),
795             menu_pattern(menu));
796     wclrtoeol(status);
797     wrefresh(status);
798 }
799
800 static void
801 perform_menus(void)
802 {
803     MENU *this_menu;
804     MENU *last_menu = mpFile;
805     int code = E_UNKNOWN_COMMAND;
806     int cmd;
807     int ch = ERR;
808
809 #ifdef NCURSES_MOUSE_VERSION
810     mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
811 #endif
812
813     menu_display(last_menu);
814
815     for (;;) {
816
817         if (ch != ERR)
818             show_status(ch, last_menu);
819
820         ch = menu_getc(mpBanner);
821
822         /*
823          * Provide for moving the menu around in the screen using shifted
824          * cursor keys.
825          */
826         switch (ch) {
827         case KEY_SF:
828             move_menus(last_menu, 1, 0);
829             continue;
830         case KEY_SR:
831             move_menus(last_menu, -1, 0);
832             continue;
833         case KEY_SLEFT:
834             move_menus(last_menu, 0, -1);
835             continue;
836         case KEY_SRIGHT:
837             move_menus(last_menu, 0, 1);
838             continue;
839 #ifdef KEY_RESIZE
840         case KEY_RESIZE:
841             resize_menus(last_menu);
842             continue;
843 #endif
844         }
845         cmd = menu_virtualize(ch);
846
847         switch (cmd) {
848             /*
849              * The banner menu acts solely to select one of the other menus.
850              * Move between its items, wrapping at the left/right limits.
851              */
852         case REQ_LEFT_ITEM:
853         case REQ_RIGHT_ITEM:
854             code = menu_driver(mpBanner, cmd);
855             if (code == E_REQUEST_DENIED) {
856                 if (menu_number() > 0)
857                     code = menu_driver(mpBanner, REQ_FIRST_ITEM);
858                 else
859                     code = menu_driver(mpBanner, REQ_LAST_ITEM);
860             }
861             break;
862         default:
863             switch (menu_number()) {
864             case eFile:
865                 code = perform_file_menu(cmd);
866                 break;
867             case eSelect:
868                 code = perform_select_menu(cmd);
869                 break;
870 #ifdef TRACE
871             case eTrace:
872                 code = perform_trace_menu(cmd);
873                 break;
874 #endif
875             }
876
877 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
878             if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
879                 (void) menu_getc(mpBanner);
880                 code = menu_driver(mpBanner, cmd);
881                 if (code == E_REQUEST_DENIED) {
882                     MEVENT event;
883                     if (menu_getc(mpBanner) == KEY_MOUSE)
884                         getmouse(&event);       /* give up */
885                 }
886             }
887 #endif
888
889             break;
890         }
891
892         if (code == E_OK) {
893             this_menu = current_menu();
894             if (this_menu != last_menu) {
895                 move(1, 0);
896                 clrtobot();
897                 box(menu_win(this_menu), 0, 0);
898                 refresh();
899
900                 /* force the current menu to appear */
901                 menu_display(this_menu);
902
903                 last_menu = this_menu;
904             }
905         }
906         wrefresh(menu_win(last_menu));
907         if (code == E_UNKNOWN_COMMAND
908             || code == E_NOT_POSTED) {
909             ITEM *item = current_item(last_menu);
910             MENU_DATA *td = (MENU_DATA *) item_userptr(item);
911             td->func((int) td->mask);
912         }
913         if (code == E_REQUEST_DENIED)
914             beep();
915         continue;
916     }
917 }
918
919 static void
920 destroy_menus(void)
921 {
922     menu_destroy(mpFile);
923     menu_destroy(mpSelect);
924 #ifdef TRACE
925     menu_destroy(mpTrace);
926 #endif
927     menu_destroy(mpBanner);
928 }
929
930 #if HAVE_RIPOFFLINE
931 static int
932 rip_footer(WINDOW *win, int cols)
933 {
934     wbkgd(win, A_REVERSE);
935     werase(win);
936     wmove(win, 0, 0);
937     wprintw(win, "footer: %d columns", cols);
938     wnoutrefresh(win);
939     return OK;
940 }
941
942 static int
943 rip_header(WINDOW *win, int cols)
944 {
945     wbkgd(win, A_REVERSE);
946     werase(win);
947     wmove(win, 0, 0);
948     wprintw(win, "header: %d columns", cols);
949     wnoutrefresh(win);
950     return OK;
951 }
952 #endif /* HAVE_RIPOFFLINE */
953
954 static void
955 call_files(int code)
956 {
957     switch (code) {
958     case 0:
959         destroy_menus();
960         endwin();
961         printf("DONE!\n");
962         ExitProgram(EXIT_SUCCESS);
963     }
964 }
965
966 static void
967 usage(void)
968 {
969     static const char *const tbl[] =
970     {
971         "Usage: demo_menus [options] [menu-file]"
972         ,""
973         ,"Options:"
974 #if HAVE_RIPOFFLINE
975         ,"  -f       rip-off footer line (can repeat)"
976         ,"  -h       rip-off header line (can repeat)"
977 #endif
978 #ifdef TRACE
979         ,"  -t mask  specify default trace-level (may toggle with ^T)"
980 #endif
981     };
982     size_t n;
983     for (n = 0; n < SIZEOF(tbl); n++)
984         fprintf(stderr, "%s\n", tbl[n]);
985     ExitProgram(EXIT_FAILURE);
986 }
987
988 int
989 main(int argc, char *argv[])
990 {
991     int c;
992
993     setlocale(LC_ALL, "");
994
995     while ((c = getopt(argc, argv, "fht:")) != -1) {
996         switch (c) {
997 #if HAVE_RIPOFFLINE
998         case 'f':
999             ripoffline(-1, rip_footer);
1000             break;
1001         case 'h':
1002             ripoffline(1, rip_header);
1003             break;
1004 #endif /* HAVE_RIPOFFLINE */
1005 #ifdef TRACE
1006         case 't':
1007             trace((unsigned) strtoul(optarg, 0, 0));
1008             break;
1009 #endif
1010         default:
1011             usage();
1012         }
1013     }
1014
1015     initscr();
1016     noraw();
1017     cbreak();
1018     noecho();
1019
1020     if (has_colors()) {
1021         start_color();
1022         init_pair(1, COLOR_RED, COLOR_BLACK);
1023         init_pair(2, COLOR_BLUE, COLOR_WHITE);
1024     }
1025     status = newwin(3, COLS, LINES - 3, 0);
1026     build_menus(argc > 1 ? argv[1] : 0);
1027     perform_menus();
1028     destroy_menus();
1029
1030     endwin();
1031     ExitProgram(EXIT_SUCCESS);
1032 }
1033 #else
1034 int
1035 main(void)
1036 {
1037     printf("This program requires the curses menu library\n");
1038     ExitProgram(EXIT_FAILURE);
1039 }
1040 #endif