1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 2003-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: demo_menus.c,v 1.71 2020/02/02 23:34:34 tom Exp $
32 * Demonstrate a variety of functions from the menu library.
33 * Thomas Dickey - 2005/4/9
50 menu_request_by_name -
67 #include <test.priv.h>
73 #include <sys/types.h>
76 #ifdef NCURSES_VERSION
78 static unsigned save_trace = TRACE_ORDINARY | TRACE_CALLS;
93 #define okMenuNo(n) (((n) > eBanner) && ((n) < eMAX))
98 NCURSES_CONST char *name;
103 static void call_files(int);
105 static MENU *mpBanner;
107 static MENU *mpSelect;
109 static WINDOW *status;
111 static bool loaded_file = FALSE;
113 static char empty[1];
116 static void failed(const char *s) GCC_NORETURN;
119 failed(const char *s)
123 ExitProgram(EXIT_FAILURE);
127 /* Common function to allow ^T to toggle trace-mode in the middle of a test
128 * so that trace-files can be made smaller.
131 wGetchar(WINDOW *win)
135 while ((c = wgetch(win)) == CTRL('T')) {
137 save_trace = _nc_tracing;
138 Trace(("TOGGLE-TRACING OFF"));
141 _nc_tracing = save_trace;
143 curses_trace(_nc_tracing);
145 Trace(("TOGGLE-TRACING ON"));
152 #define Getchar() wGetchar(stdscr)
155 menu_virtualize(int c)
159 if (c == '\n' || c == KEY_EXIT)
160 result = (MAX_COMMAND + 1);
162 result = (REQ_SCR_ULINE);
164 result = (REQ_SCR_DLINE);
165 else if (c == 'b' || c == KEY_NPAGE)
166 result = (REQ_SCR_UPAGE);
167 else if (c == 'f' || c == KEY_PPAGE)
168 result = (REQ_SCR_DPAGE);
169 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
170 result = (REQ_LEFT_ITEM);
171 else if (c == 'n' || c == KEY_DOWN)
172 result = (REQ_NEXT_ITEM);
173 else if (c == 'p' || c == KEY_UP)
174 result = (REQ_PREV_ITEM);
175 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
176 result = (REQ_RIGHT_ITEM);
178 result = (REQ_TOGGLE_ITEM);
190 return wGetchar(menu_win(m));
194 menu_offset(MenuNo number)
198 if (okMenuNo(number)) {
200 #ifdef NCURSES_VERSION
201 int spc_desc, spc_cols;
202 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
207 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
208 result = (number - (eBanner + 1)) * (menu_itemwidth(mpBanner) + spc_rows);
214 my_menu_init(MENU * menu)
216 Trace(("called MenuHook my_menu_init"));
217 mvwprintw(status, 2, 0, "menu_init %p", (void *) menu);
223 my_menu_term(MENU * menu)
225 Trace(("called MenuHook my_menu_term"));
226 mvwprintw(status, 2, 0, "menu_term %p", (void *) menu);
232 my_item_init(MENU * menu)
234 ITEM *item = current_item(menu);
235 const char *name = item_name(item);
237 Trace(("called MenuHook my_item_init (%s)", name));
238 mvwprintw(status, 2, 0, "item_init %s", name);
244 my_item_term(MENU * menu)
246 ITEM *item = current_item(menu);
247 const char *name = item_name(item);
249 Trace(("called MenuHook my_item_term (%s)", name));
250 mvwprintw(status, 2, 0, "item_term %s", name);
256 menu_create(ITEM ** items, int count, int ncols, MenuNo number)
261 int y = okMenuNo(number) ? MENU_Y : 0;
262 int x = menu_offset(number);
263 int margin = (y == MENU_Y) ? 1 : 0;
264 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
265 int maxrow = (count + 1) / ncols;
267 if ((maxrow + y) >= (LINES - 4))
268 maxrow = LINES - 4 - y;
270 result = new_menu(items);
273 set_menu_fore(result, (chtype) COLOR_PAIR(1));
274 set_menu_back(result, (chtype) COLOR_PAIR(2));
277 set_menu_format(result, maxrow, maxcol);
278 scale_menu(result, &mrows, &mcols);
280 if (mcols + (2 * margin + x) >= COLS)
281 mcols = COLS - (2 * margin + x);
283 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
284 set_menu_win(result, menuwin);
285 keypad(menuwin, TRUE);
289 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
292 if (number == eTrace)
293 menu_opts_off(result, O_ONEVALUE);
295 menu_opts_on(result, O_ONEVALUE);
297 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
298 menu_opts_on(result, O_MOUSE_MENU);
303 set_menu_init(result, my_menu_init);
304 set_menu_term(result, my_menu_term);
305 set_item_init(result, my_item_init);
306 set_item_term(result, my_item_term);
311 menu_destroy(MENU * m)
313 Trace(("menu_destroy %p", (void *) m));
315 ITEM **items = menu_items(m);
316 const char *blob = 0;
319 count = item_count(m);
320 Trace(("menu_destroy %p count %d", (void *) m, count));
321 if ((count > 0) && (m == mpSelect)) {
322 blob = item_name(*items);
328 /* free the extra data allocated in build_select_menu() */
329 if ((count > 0) && (m == mpSelect)) {
330 if (blob && loaded_file) {
331 Trace(("freeing blob %p", blob));
338 if ((count > 0) && (m == mpTrace)) {
349 /* force the given menu to appear */
351 menu_display(MENU * m)
353 touchwin(menu_win(m));
354 wrefresh(menu_win(m));
357 /*****************************************************************************/
360 build_file_menu(MenuNo number)
362 static MENU_DATA table[] =
364 {"Exit", call_files, 0},
367 static ITEM *items[SIZEOF(table)];
372 for (n = 0; table[n].name != 0; ++n) {
373 *ip = new_item(table[n].name, empty);
374 set_item_userptr(*ip, (void *) &table[n]);
379 mpFile = menu_create(items, SIZEOF(table) - 1, 1, number);
383 perform_file_menu(int cmd)
385 return menu_driver(mpFile, cmd);
388 /*****************************************************************************/
391 call_select(int code)
394 Trace(("Selected item %d", code));
398 build_select_menu(MenuNo number, char *filename)
400 #define MY_DATA(name) { name, call_select, 0 }
401 static MENU_DATA table[] =
410 MY_DATA("(Oh really?!)"),
414 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
415 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
422 MENU_DATA *myList = 0;
428 if (stat(filename, &sb) == 0
429 && (sb.st_mode & S_IFMT) == S_IFREG
430 && sb.st_size != 0) {
431 size_t size = (size_t) sb.st_size;
432 char *blob = typeMalloc(char, size + 1);
433 MENU_DATA *list = typeCalloc(MENU_DATA, size + 1);
435 items = typeCalloc(ITEM *, size + 1);
436 Trace(("build_select_menu blob=%p, items=%p",
439 if (blob != 0 && list != 0) {
440 FILE *fp = fopen(filename, "r");
442 if (fread(blob, sizeof(char), size, fp) == size) {
445 for (j = k = 0; j < size; ++j) {
447 list[k++].name = blob + j;
450 if (blob[j] == '\n') {
452 if (k > 0 && *list[k - 1].name == '\0')
455 } else if (blob[j] == '\t') {
456 blob[j] = ' '; /* menu items are printable */
472 count = SIZEOF(table) - 1;
473 items = typeCalloc(ITEM *, count + 1);
478 for (i = 0; ap[i].name != 0; ++i) {
479 ap[i].func = call_select;
480 ap[i].mask = (unsigned) i;
481 *ip = new_item(ap[i].name, empty);
482 set_item_userptr(*ip, (void *) &table[i]);
487 mpSelect = menu_create(items, (int) count, 1, number);
493 perform_select_menu(int cmd)
495 return menu_driver(mpSelect, cmd);
498 /*****************************************************************************/
506 Trace(("Updating trace mask %d", code));
509 #define T_TBL(name) { #name, call_trace, name }
510 static MENU_DATA t_tbl[] =
513 T_TBL(TRACE_DISABLE),
518 T_TBL(TRACE_CHARPUT),
519 T_TBL(TRACE_ORDINARY),
521 T_TBL(TRACE_VIRTPUT),
526 T_TBL(TRACE_DATABASE),
528 T_TBL(TRACE_MAXIMUM),
535 build_trace_menu(MenuNo number)
537 static ITEM *items[SIZEOF(t_tbl)];
542 for (n = 0; t_tbl[n].name != 0; n++) {
543 *ip = new_item(t_tbl[n].name, empty);
544 set_item_userptr(*ip, (void *) &t_tbl[n]);
549 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
553 tracetrace(unsigned tlevel)
556 static size_t need = 12;
560 for (n = 0; t_tbl[n].name != 0; n++)
561 need += strlen(t_tbl[n].name) + 2;
562 buf = typeMalloc(char, need);
564 failed("tracetrace");
566 _nc_SPRINTF(buf, _nc_SLIMIT(need) "0x%02x = {", tlevel);
568 _nc_STRCAT(buf, t_tbl[0].name, need);
569 _nc_STRCAT(buf, ", ", need);
571 for (n = 1; t_tbl[n].name != 0; n++)
572 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
573 _nc_STRCAT(buf, t_tbl[n].name, need);
574 _nc_STRCAT(buf, ", ", need);
577 if (buf[strlen(buf) - 2] == ',')
578 buf[strlen(buf) - 2] = '\0';
579 _nc_STRCAT(buf, "}", need);
583 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
587 update_trace_menu(MENU * m)
591 bool changed = FALSE;
593 items = menu_items(m);
598 for (p = items + 1; *p != 0; p++)
599 if (item_value(*p)) {
600 set_item_value(*p, FALSE);
609 perform_trace_menu(int cmd)
610 /* interactively set the trace level */
615 for (ip = menu_items(mpTrace); *ip; ip++) {
616 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
617 unsigned mask = td->mask;
619 set_item_value(*ip, _nc_tracing == 0);
620 else if ((mask & _nc_tracing) == mask)
621 set_item_value(*ip, TRUE);
624 result = menu_driver(mpTrace, cmd);
626 if (result == E_OK) {
627 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
628 unsigned newtrace = 0;
629 for (ip = menu_items(mpTrace); *ip; ip++) {
630 if (item_value(*ip)) {
631 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
632 newtrace |= td->mask;
635 curses_trace(newtrace);
636 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
638 MvWPrintw(status, 1, 0,
639 "Trace level is %s\n", tracetrace(_nc_tracing));
647 /*****************************************************************************/
652 return item_index(current_item(mpBanner)) - (eBanner + 1);
660 switch (menu_number()) {
683 Trace(("Activated menu %d\n", code));
687 build_menus(char *filename)
689 static MENU_DATA table[] =
691 {"File", call_menus, 0},
692 {"Select", call_menus, 1},
694 {"Trace", call_menus, 2},
698 static ITEM *items[SIZEOF(table)];
703 for (n = 0; table[n].name != 0; ++n) {
704 *ip = new_item(table[n].name, empty);
705 set_item_userptr(*ip, (void *) &table[n]);
710 mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
711 set_menu_mark(mpBanner, ">");
713 build_file_menu(eFile);
714 build_select_menu(eSelect, filename);
716 build_trace_menu(eTrace);
721 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
723 WINDOW *top_win = menu_win(menu);
724 WINDOW *sub_win = menu_sub(menu);
729 getbegyx(top_win, y0, x0);
733 getbegyx(sub_win, y1, x1);
737 if ((result = mvwin(top_win, y0, x0)) != ERR) {
738 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
742 mvwin(sub_win, y1, x1);
744 if (menu == current) {
746 wnoutrefresh(top_win);
753 * Move the menus around on the screen, to test mvwin().
756 move_menus(MENU * current, int by_y, int by_x)
758 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
760 wnoutrefresh(stdscr);
761 move_menu(mpFile, current, by_y, by_x);
762 move_menu(mpSelect, current, by_y, by_x);
764 move_menu(mpTrace, current, by_y, by_x);
770 #if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
772 resize_menu(MENU ** menu)
775 WINDOW *win = menu_win(*menu);
776 WINDOW *sub = menu_sub(*menu);
782 resize_menus(MENU * current)
787 wnoutrefresh(status);
788 wresize(status, 1, COLS);
789 mvwin(status, LINES - 1, 0);
791 resize_menu(&mpBanner);
792 resize_menu(&mpFile);
793 resize_menu(&mpSelect);
795 resize_menu(&mpTrace);
798 #endif /* defined(KEY_RESIZE) && NCURSES_EXT_FUNCS */
801 show_status(int ch, MENU * menu)
804 wprintw(status, "key %s, menu %d, mark %s, match %s",
817 MENU *last_menu = mpFile;
818 int code = E_UNKNOWN_COMMAND;
822 #ifdef NCURSES_MOUSE_VERSION
823 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
826 menu_display(last_menu);
831 show_status(ch, last_menu);
833 ch = menu_getc(mpBanner);
836 * Provide for moving the menu around in the screen using shifted
841 move_menus(last_menu, 1, 0);
844 move_menus(last_menu, -1, 0);
847 move_menus(last_menu, 0, -1);
850 move_menus(last_menu, 0, 1);
852 #if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
854 resize_menus(last_menu);
858 cmd = menu_virtualize(ch);
862 * The banner menu acts solely to select one of the other menus.
863 * Move between its items, wrapping at the left/right limits.
867 code = menu_driver(mpBanner, cmd);
868 if (code == E_REQUEST_DENIED) {
869 if (menu_number() > 0)
870 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
872 code = menu_driver(mpBanner, REQ_LAST_ITEM);
876 switch (menu_number()) {
878 code = perform_file_menu(cmd);
881 code = perform_select_menu(cmd);
885 code = perform_trace_menu(cmd);
890 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
891 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
892 (void) menu_getc(mpBanner);
893 code = menu_driver(mpBanner, cmd);
894 if (code == E_REQUEST_DENIED) {
896 if (menu_getc(mpBanner) == KEY_MOUSE)
897 getmouse(&event); /* give up */
906 this_menu = current_menu();
907 if (this_menu != last_menu) {
910 box(menu_win(this_menu), 0, 0);
913 /* force the current menu to appear */
914 menu_display(this_menu);
916 last_menu = this_menu;
919 wrefresh(menu_win(last_menu));
920 if (code == E_UNKNOWN_COMMAND
921 || code == E_NOT_POSTED) {
922 ITEM *item = current_item(last_menu);
923 MENU_DATA *td = (MENU_DATA *) item_userptr(item);
924 td->func((int) td->mask);
926 if (code == E_REQUEST_DENIED)
935 menu_destroy(mpFile);
936 menu_destroy(mpSelect);
938 menu_destroy(mpTrace);
940 menu_destroy(mpBanner);
945 rip_footer(WINDOW *win, int cols)
947 wbkgd(win, A_REVERSE);
950 wprintw(win, "footer: %d columns", cols);
956 rip_header(WINDOW *win, int cols)
958 wbkgd(win, A_REVERSE);
961 wprintw(win, "header: %d columns", cols);
965 #endif /* HAVE_RIPOFFLINE */
975 ExitProgram(EXIT_SUCCESS);
982 static const char *const tbl[] =
984 "Usage: demo_menus [options] [menu-file]"
988 ," -f rip-off footer line (can repeat)"
989 ," -h rip-off header line (can repeat)"
992 ," -t mask specify default trace-level (may toggle with ^T)"
996 for (n = 0; n < SIZEOF(tbl); n++)
997 fprintf(stderr, "%s\n", tbl[n]);
998 ExitProgram(EXIT_FAILURE);
1002 main(int argc, char *argv[])
1006 setlocale(LC_ALL, "");
1008 while ((c = getopt(argc, argv, "fht:")) != -1) {
1012 ripoffline(-1, rip_footer);
1015 ripoffline(1, rip_header);
1017 #endif /* HAVE_RIPOFFLINE */
1020 curses_trace((unsigned) strtoul(optarg, 0, 0));
1035 init_pair(1, COLOR_RED, COLOR_BLACK);
1036 init_pair(2, COLOR_BLUE, COLOR_WHITE);
1038 status = newwin(3, COLS, LINES - 3, 0);
1039 build_menus(argc > 1 ? argv[1] : 0);
1044 ExitProgram(EXIT_SUCCESS);
1050 printf("This program requires the curses menu library\n");
1051 ExitProgram(EXIT_FAILURE);