]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_menus.c
ncurses 6.1 - patch 20190817
[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.68 2019/08/17 21:45:32 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, **p;
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             for (p = items + 1; *p != 0; p++)
597                 if (item_value(*p)) {
598                     set_item_value(*p, FALSE);
599                     changed = TRUE;
600                 }
601         }
602     }
603     return changed;
604 }
605
606 static int
607 perform_trace_menu(int cmd)
608 /* interactively set the trace level */
609 {
610     ITEM **ip;
611     int result;
612
613     for (ip = menu_items(mpTrace); *ip; ip++) {
614         MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
615         unsigned mask = td->mask;
616         if (mask == 0)
617             set_item_value(*ip, _nc_tracing == 0);
618         else if ((mask & _nc_tracing) == mask)
619             set_item_value(*ip, TRUE);
620     }
621
622     result = menu_driver(mpTrace, cmd);
623
624     if (result == E_OK) {
625         if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
626             unsigned newtrace = 0;
627             for (ip = menu_items(mpTrace); *ip; ip++) {
628                 if (item_value(*ip)) {
629                     MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
630                     newtrace |= td->mask;
631                 }
632             }
633             trace(newtrace);
634             Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
635
636             MvWPrintw(status, 1, 0,
637                       "Trace level is %s\n", tracetrace(_nc_tracing));
638             wrefresh(status);
639         }
640     }
641     return result;
642 }
643 #endif /* TRACE */
644
645 /*****************************************************************************/
646
647 static int
648 menu_number(void)
649 {
650     return item_index(current_item(mpBanner)) - (eBanner + 1);
651 }
652
653 static MENU *
654 current_menu(void)
655 {
656     MENU *result;
657
658     switch (menu_number()) {
659     case eFile:
660         result = mpFile;
661         break;
662     case eSelect:
663         result = mpSelect;
664         break;
665 #ifdef TRACE
666     case eTrace:
667         result = mpTrace;
668         break;
669 #endif
670     default:
671         result = 0;
672         break;
673     }
674     return result;
675 }
676
677 static void
678 call_menus(int code)
679 {
680     (void) code;
681     Trace(("Activated menu %d\n", code));
682 }
683
684 static void
685 build_menus(char *filename)
686 {
687     static MENU_DATA table[] =
688     {
689         {"File", call_menus, 0},
690         {"Select", call_menus, 1},
691 #ifdef TRACE
692         {"Trace", call_menus, 2},
693 #endif
694         {(char *) 0, 0, 0}
695     };
696     static ITEM *items[SIZEOF(table)];
697
698     ITEM **ip = items;
699     int n;
700
701     for (n = 0; table[n].name != 0; ++n) {
702         *ip = new_item(table[n].name, empty);
703         set_item_userptr(*ip, (void *) &table[n]);
704         ++ip;
705     }
706     *ip = (ITEM *) 0;
707
708     mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
709     set_menu_mark(mpBanner, ">");
710
711     build_file_menu(eFile);
712     build_select_menu(eSelect, filename);
713 #ifdef TRACE
714     build_trace_menu(eTrace);
715 #endif
716 }
717
718 static int
719 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
720 {
721     WINDOW *top_win = menu_win(menu);
722     WINDOW *sub_win = menu_sub(menu);
723     int y0, x0;
724     int y1, x1;
725     int result;
726
727     getbegyx(top_win, y0, x0);
728     y0 += by_y;
729     x0 += by_x;
730
731     getbegyx(sub_win, y1, x1);
732     y1 += by_y;
733     x1 += by_x;
734
735     if ((result = mvwin(top_win, y0, x0)) != ERR) {
736 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
737         sub_win->_begy = y1;
738         sub_win->_begx = x1;
739 #else
740         mvwin(sub_win, y1, x1);
741 #endif
742         if (menu == current) {
743             touchwin(top_win);
744             wnoutrefresh(top_win);
745         }
746     }
747     return result;
748 }
749
750 /*
751  * Move the menus around on the screen, to test mvwin().
752  */
753 static void
754 move_menus(MENU * current, int by_y, int by_x)
755 {
756     if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
757         erase();
758         wnoutrefresh(stdscr);
759         move_menu(mpFile, current, by_y, by_x);
760         move_menu(mpSelect, current, by_y, by_x);
761 #ifdef TRACE
762         move_menu(mpTrace, current, by_y, by_x);
763 #endif
764         doupdate();
765     }
766 }
767
768 #if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
769 static void
770 resize_menu(MENU ** menu)
771 {
772 #if 0
773     WINDOW *win = menu_win(*menu);
774     WINDOW *sub = menu_sub(*menu);
775 #endif
776     (void) menu;
777 }
778
779 static void
780 resize_menus(MENU * current)
781 {
782     (void) current;
783
784     werase(status);
785     wnoutrefresh(status);
786     wresize(status, 1, COLS);
787     mvwin(status, LINES - 1, 0);
788
789     resize_menu(&mpBanner);
790     resize_menu(&mpFile);
791     resize_menu(&mpSelect);
792 #ifdef TRACE
793     resize_menu(&mpTrace);
794 #endif
795 }
796 #endif /* defined(KEY_RESIZE) && NCURSES_EXT_FUNCS */
797
798 static void
799 show_status(int ch, MENU * menu)
800 {
801     wmove(status, 0, 0);
802     wprintw(status, "key %s, menu %d, mark %s, match %s",
803             keyname(ch),
804             menu_number(),
805             menu_mark(menu),
806             menu_pattern(menu));
807     wclrtoeol(status);
808     wrefresh(status);
809 }
810
811 static void
812 perform_menus(void)
813 {
814     MENU *this_menu;
815     MENU *last_menu = mpFile;
816     int code = E_UNKNOWN_COMMAND;
817     int cmd;
818     int ch = ERR;
819
820 #ifdef NCURSES_MOUSE_VERSION
821     mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
822 #endif
823
824     menu_display(last_menu);
825
826     for (;;) {
827
828         if (ch != ERR)
829             show_status(ch, last_menu);
830
831         ch = menu_getc(mpBanner);
832
833         /*
834          * Provide for moving the menu around in the screen using shifted
835          * cursor keys.
836          */
837         switch (ch) {
838         case KEY_SF:
839             move_menus(last_menu, 1, 0);
840             continue;
841         case KEY_SR:
842             move_menus(last_menu, -1, 0);
843             continue;
844         case KEY_SLEFT:
845             move_menus(last_menu, 0, -1);
846             continue;
847         case KEY_SRIGHT:
848             move_menus(last_menu, 0, 1);
849             continue;
850 #if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
851         case KEY_RESIZE:
852             resize_menus(last_menu);
853             continue;
854 #endif
855         }
856         cmd = menu_virtualize(ch);
857
858         switch (cmd) {
859             /*
860              * The banner menu acts solely to select one of the other menus.
861              * Move between its items, wrapping at the left/right limits.
862              */
863         case REQ_LEFT_ITEM:
864         case REQ_RIGHT_ITEM:
865             code = menu_driver(mpBanner, cmd);
866             if (code == E_REQUEST_DENIED) {
867                 if (menu_number() > 0)
868                     code = menu_driver(mpBanner, REQ_FIRST_ITEM);
869                 else
870                     code = menu_driver(mpBanner, REQ_LAST_ITEM);
871             }
872             break;
873         default:
874             switch (menu_number()) {
875             case eFile:
876                 code = perform_file_menu(cmd);
877                 break;
878             case eSelect:
879                 code = perform_select_menu(cmd);
880                 break;
881 #ifdef TRACE
882             case eTrace:
883                 code = perform_trace_menu(cmd);
884                 break;
885 #endif
886             }
887
888 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
889             if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
890                 (void) menu_getc(mpBanner);
891                 code = menu_driver(mpBanner, cmd);
892                 if (code == E_REQUEST_DENIED) {
893                     MEVENT event;
894                     if (menu_getc(mpBanner) == KEY_MOUSE)
895                         getmouse(&event);       /* give up */
896                 }
897             }
898 #endif
899
900             break;
901         }
902
903         if (code == E_OK) {
904             this_menu = current_menu();
905             if (this_menu != last_menu) {
906                 move(1, 0);
907                 clrtobot();
908                 box(menu_win(this_menu), 0, 0);
909                 refresh();
910
911                 /* force the current menu to appear */
912                 menu_display(this_menu);
913
914                 last_menu = this_menu;
915             }
916         }
917         wrefresh(menu_win(last_menu));
918         if (code == E_UNKNOWN_COMMAND
919             || code == E_NOT_POSTED) {
920             ITEM *item = current_item(last_menu);
921             MENU_DATA *td = (MENU_DATA *) item_userptr(item);
922             td->func((int) td->mask);
923         }
924         if (code == E_REQUEST_DENIED)
925             beep();
926         continue;
927     }
928 }
929
930 static void
931 destroy_menus(void)
932 {
933     menu_destroy(mpFile);
934     menu_destroy(mpSelect);
935 #ifdef TRACE
936     menu_destroy(mpTrace);
937 #endif
938     menu_destroy(mpBanner);
939 }
940
941 #if HAVE_RIPOFFLINE
942 static int
943 rip_footer(WINDOW *win, int cols)
944 {
945     wbkgd(win, A_REVERSE);
946     werase(win);
947     wmove(win, 0, 0);
948     wprintw(win, "footer: %d columns", cols);
949     wnoutrefresh(win);
950     return OK;
951 }
952
953 static int
954 rip_header(WINDOW *win, int cols)
955 {
956     wbkgd(win, A_REVERSE);
957     werase(win);
958     wmove(win, 0, 0);
959     wprintw(win, "header: %d columns", cols);
960     wnoutrefresh(win);
961     return OK;
962 }
963 #endif /* HAVE_RIPOFFLINE */
964
965 static void
966 call_files(int code)
967 {
968     switch (code) {
969     case 0:
970         destroy_menus();
971         endwin();
972         printf("DONE!\n");
973         ExitProgram(EXIT_SUCCESS);
974     }
975 }
976
977 static void
978 usage(void)
979 {
980     static const char *const tbl[] =
981     {
982         "Usage: demo_menus [options] [menu-file]"
983         ,""
984         ,"Options:"
985 #if HAVE_RIPOFFLINE
986         ,"  -f       rip-off footer line (can repeat)"
987         ,"  -h       rip-off header line (can repeat)"
988 #endif
989 #ifdef TRACE
990         ,"  -t mask  specify default trace-level (may toggle with ^T)"
991 #endif
992     };
993     size_t n;
994     for (n = 0; n < SIZEOF(tbl); n++)
995         fprintf(stderr, "%s\n", tbl[n]);
996     ExitProgram(EXIT_FAILURE);
997 }
998
999 int
1000 main(int argc, char *argv[])
1001 {
1002     int c;
1003
1004     setlocale(LC_ALL, "");
1005
1006     while ((c = getopt(argc, argv, "fht:")) != -1) {
1007         switch (c) {
1008 #if HAVE_RIPOFFLINE
1009         case 'f':
1010             ripoffline(-1, rip_footer);
1011             break;
1012         case 'h':
1013             ripoffline(1, rip_header);
1014             break;
1015 #endif /* HAVE_RIPOFFLINE */
1016 #ifdef TRACE
1017         case 't':
1018             trace((unsigned) strtoul(optarg, 0, 0));
1019             break;
1020 #endif
1021         default:
1022             usage();
1023         }
1024     }
1025
1026     initscr();
1027     noraw();
1028     cbreak();
1029     noecho();
1030
1031     if (has_colors()) {
1032         start_color();
1033         init_pair(1, COLOR_RED, COLOR_BLACK);
1034         init_pair(2, COLOR_BLUE, COLOR_WHITE);
1035     }
1036     status = newwin(3, COLS, LINES - 3, 0);
1037     build_menus(argc > 1 ? argv[1] : 0);
1038     perform_menus();
1039     destroy_menus();
1040
1041     endwin();
1042     ExitProgram(EXIT_SUCCESS);
1043 }
1044 #else
1045 int
1046 main(void)
1047 {
1048     printf("This program requires the curses menu library\n");
1049     ExitProgram(EXIT_FAILURE);
1050 }
1051 #endif