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