1 /****************************************************************************
2 * Copyright (c) 2005-2017,2019 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: demo_menus.c,v 1.70 2019/12/07 18:06:12 tom Exp $
31 * Demonstrate a variety of functions from the menu library.
32 * Thomas Dickey - 2005/4/9
49 menu_request_by_name -
66 #include <test.priv.h>
72 #include <sys/types.h>
75 #ifdef NCURSES_VERSION
77 static unsigned save_trace = TRACE_ORDINARY | TRACE_CALLS;
92 #define okMenuNo(n) (((n) > eBanner) && ((n) < eMAX))
97 NCURSES_CONST char *name;
102 static void call_files(int);
104 static MENU *mpBanner;
106 static MENU *mpSelect;
108 static WINDOW *status;
110 static bool loaded_file = FALSE;
112 static char empty[1];
115 static void failed(const char *s) GCC_NORETURN;
118 failed(const char *s)
122 ExitProgram(EXIT_FAILURE);
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.
130 wGetchar(WINDOW *win)
134 while ((c = wgetch(win)) == CTRL('T')) {
136 save_trace = _nc_tracing;
137 Trace(("TOGGLE-TRACING OFF"));
140 _nc_tracing = save_trace;
142 curses_trace(_nc_tracing);
144 Trace(("TOGGLE-TRACING ON"));
151 #define Getchar() wGetchar(stdscr)
154 menu_virtualize(int c)
158 if (c == '\n' || c == KEY_EXIT)
159 result = (MAX_COMMAND + 1);
161 result = (REQ_SCR_ULINE);
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);
177 result = (REQ_TOGGLE_ITEM);
189 return wGetchar(menu_win(m));
193 menu_offset(MenuNo number)
197 if (okMenuNo(number)) {
199 #ifdef NCURSES_VERSION
200 int spc_desc, spc_cols;
201 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
206 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
207 result = (number - (eBanner + 1)) * (menu_itemwidth(mpBanner) + spc_rows);
213 my_menu_init(MENU * menu)
215 Trace(("called MenuHook my_menu_init"));
216 mvwprintw(status, 2, 0, "menu_init %p", (void *) menu);
222 my_menu_term(MENU * menu)
224 Trace(("called MenuHook my_menu_term"));
225 mvwprintw(status, 2, 0, "menu_term %p", (void *) menu);
231 my_item_init(MENU * menu)
233 ITEM *item = current_item(menu);
234 const char *name = item_name(item);
236 Trace(("called MenuHook my_item_init (%s)", name));
237 mvwprintw(status, 2, 0, "item_init %s", name);
243 my_item_term(MENU * menu)
245 ITEM *item = current_item(menu);
246 const char *name = item_name(item);
248 Trace(("called MenuHook my_item_term (%s)", name));
249 mvwprintw(status, 2, 0, "item_term %s", name);
255 menu_create(ITEM ** items, int count, int ncols, MenuNo number)
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;
266 if ((maxrow + y) >= (LINES - 4))
267 maxrow = LINES - 4 - y;
269 result = new_menu(items);
272 set_menu_fore(result, (chtype) COLOR_PAIR(1));
273 set_menu_back(result, (chtype) COLOR_PAIR(2));
276 set_menu_format(result, maxrow, maxcol);
277 scale_menu(result, &mrows, &mcols);
279 if (mcols + (2 * margin + x) >= COLS)
280 mcols = COLS - (2 * margin + x);
282 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
283 set_menu_win(result, menuwin);
284 keypad(menuwin, TRUE);
288 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
291 if (number == eTrace)
292 menu_opts_off(result, O_ONEVALUE);
294 menu_opts_on(result, O_ONEVALUE);
296 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
297 menu_opts_on(result, O_MOUSE_MENU);
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);
310 menu_destroy(MENU * m)
312 Trace(("menu_destroy %p", (void *) m));
314 ITEM **items = menu_items(m);
315 const char *blob = 0;
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);
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));
337 if ((count > 0) && (m == mpTrace)) {
348 /* force the given menu to appear */
350 menu_display(MENU * m)
352 touchwin(menu_win(m));
353 wrefresh(menu_win(m));
356 /*****************************************************************************/
359 build_file_menu(MenuNo number)
361 static MENU_DATA table[] =
363 {"Exit", call_files, 0},
366 static ITEM *items[SIZEOF(table)];
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]);
378 mpFile = menu_create(items, SIZEOF(table) - 1, 1, number);
382 perform_file_menu(int cmd)
384 return menu_driver(mpFile, cmd);
387 /*****************************************************************************/
390 call_select(int code)
393 Trace(("Selected item %d", code));
397 build_select_menu(MenuNo number, char *filename)
399 #define MY_DATA(name) { name, call_select, 0 }
400 static MENU_DATA table[] =
409 MY_DATA("(Oh really?!)"),
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"),
421 MENU_DATA *myList = 0;
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);
434 items = typeCalloc(ITEM *, size + 1);
435 Trace(("build_select_menu blob=%p, items=%p",
438 if (blob != 0 && list != 0) {
439 FILE *fp = fopen(filename, "r");
441 if (fread(blob, sizeof(char), size, fp) == size) {
444 for (j = k = 0; j < size; ++j) {
446 list[k++].name = blob + j;
449 if (blob[j] == '\n') {
451 if (k > 0 && *list[k - 1].name == '\0')
454 } else if (blob[j] == '\t') {
455 blob[j] = ' '; /* menu items are printable */
471 count = SIZEOF(table) - 1;
472 items = typeCalloc(ITEM *, count + 1);
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]);
486 mpSelect = menu_create(items, (int) count, 1, number);
492 perform_select_menu(int cmd)
494 return menu_driver(mpSelect, cmd);
497 /*****************************************************************************/
505 Trace(("Updating trace mask %d", code));
508 #define T_TBL(name) { #name, call_trace, name }
509 static MENU_DATA t_tbl[] =
512 T_TBL(TRACE_DISABLE),
517 T_TBL(TRACE_CHARPUT),
518 T_TBL(TRACE_ORDINARY),
520 T_TBL(TRACE_VIRTPUT),
525 T_TBL(TRACE_DATABASE),
527 T_TBL(TRACE_MAXIMUM),
534 build_trace_menu(MenuNo number)
536 static ITEM *items[SIZEOF(t_tbl)];
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]);
548 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
552 tracetrace(unsigned tlevel)
555 static size_t need = 12;
559 for (n = 0; t_tbl[n].name != 0; n++)
560 need += strlen(t_tbl[n].name) + 2;
561 buf = typeMalloc(char, need);
563 failed("tracetrace");
565 _nc_SPRINTF(buf, _nc_SLIMIT(need) "0x%02x = {", tlevel);
567 _nc_STRCAT(buf, t_tbl[0].name, need);
568 _nc_STRCAT(buf, ", ", need);
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);
576 if (buf[strlen(buf) - 2] == ',')
577 buf[strlen(buf) - 2] = '\0';
578 _nc_STRCAT(buf, "}", need);
582 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
586 update_trace_menu(MENU * m)
590 bool changed = FALSE;
592 items = menu_items(m);
597 for (p = items + 1; *p != 0; p++)
598 if (item_value(*p)) {
599 set_item_value(*p, FALSE);
608 perform_trace_menu(int cmd)
609 /* interactively set the trace level */
614 for (ip = menu_items(mpTrace); *ip; ip++) {
615 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
616 unsigned mask = td->mask;
618 set_item_value(*ip, _nc_tracing == 0);
619 else if ((mask & _nc_tracing) == mask)
620 set_item_value(*ip, TRUE);
623 result = menu_driver(mpTrace, cmd);
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;
634 curses_trace(newtrace);
635 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
637 MvWPrintw(status, 1, 0,
638 "Trace level is %s\n", tracetrace(_nc_tracing));
646 /*****************************************************************************/
651 return item_index(current_item(mpBanner)) - (eBanner + 1);
659 switch (menu_number()) {
682 Trace(("Activated menu %d\n", code));
686 build_menus(char *filename)
688 static MENU_DATA table[] =
690 {"File", call_menus, 0},
691 {"Select", call_menus, 1},
693 {"Trace", call_menus, 2},
697 static ITEM *items[SIZEOF(table)];
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]);
709 mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
710 set_menu_mark(mpBanner, ">");
712 build_file_menu(eFile);
713 build_select_menu(eSelect, filename);
715 build_trace_menu(eTrace);
720 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
722 WINDOW *top_win = menu_win(menu);
723 WINDOW *sub_win = menu_sub(menu);
728 getbegyx(top_win, y0, x0);
732 getbegyx(sub_win, y1, x1);
736 if ((result = mvwin(top_win, y0, x0)) != ERR) {
737 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
741 mvwin(sub_win, y1, x1);
743 if (menu == current) {
745 wnoutrefresh(top_win);
752 * Move the menus around on the screen, to test mvwin().
755 move_menus(MENU * current, int by_y, int by_x)
757 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
759 wnoutrefresh(stdscr);
760 move_menu(mpFile, current, by_y, by_x);
761 move_menu(mpSelect, current, by_y, by_x);
763 move_menu(mpTrace, current, by_y, by_x);
769 #if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
771 resize_menu(MENU ** menu)
774 WINDOW *win = menu_win(*menu);
775 WINDOW *sub = menu_sub(*menu);
781 resize_menus(MENU * current)
786 wnoutrefresh(status);
787 wresize(status, 1, COLS);
788 mvwin(status, LINES - 1, 0);
790 resize_menu(&mpBanner);
791 resize_menu(&mpFile);
792 resize_menu(&mpSelect);
794 resize_menu(&mpTrace);
797 #endif /* defined(KEY_RESIZE) && NCURSES_EXT_FUNCS */
800 show_status(int ch, MENU * menu)
803 wprintw(status, "key %s, menu %d, mark %s, match %s",
816 MENU *last_menu = mpFile;
817 int code = E_UNKNOWN_COMMAND;
821 #ifdef NCURSES_MOUSE_VERSION
822 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
825 menu_display(last_menu);
830 show_status(ch, last_menu);
832 ch = menu_getc(mpBanner);
835 * Provide for moving the menu around in the screen using shifted
840 move_menus(last_menu, 1, 0);
843 move_menus(last_menu, -1, 0);
846 move_menus(last_menu, 0, -1);
849 move_menus(last_menu, 0, 1);
851 #if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
853 resize_menus(last_menu);
857 cmd = menu_virtualize(ch);
861 * The banner menu acts solely to select one of the other menus.
862 * Move between its items, wrapping at the left/right limits.
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);
871 code = menu_driver(mpBanner, REQ_LAST_ITEM);
875 switch (menu_number()) {
877 code = perform_file_menu(cmd);
880 code = perform_select_menu(cmd);
884 code = perform_trace_menu(cmd);
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) {
895 if (menu_getc(mpBanner) == KEY_MOUSE)
896 getmouse(&event); /* give up */
905 this_menu = current_menu();
906 if (this_menu != last_menu) {
909 box(menu_win(this_menu), 0, 0);
912 /* force the current menu to appear */
913 menu_display(this_menu);
915 last_menu = this_menu;
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);
925 if (code == E_REQUEST_DENIED)
934 menu_destroy(mpFile);
935 menu_destroy(mpSelect);
937 menu_destroy(mpTrace);
939 menu_destroy(mpBanner);
944 rip_footer(WINDOW *win, int cols)
946 wbkgd(win, A_REVERSE);
949 wprintw(win, "footer: %d columns", cols);
955 rip_header(WINDOW *win, int cols)
957 wbkgd(win, A_REVERSE);
960 wprintw(win, "header: %d columns", cols);
964 #endif /* HAVE_RIPOFFLINE */
974 ExitProgram(EXIT_SUCCESS);
981 static const char *const tbl[] =
983 "Usage: demo_menus [options] [menu-file]"
987 ," -f rip-off footer line (can repeat)"
988 ," -h rip-off header line (can repeat)"
991 ," -t mask specify default trace-level (may toggle with ^T)"
995 for (n = 0; n < SIZEOF(tbl); n++)
996 fprintf(stderr, "%s\n", tbl[n]);
997 ExitProgram(EXIT_FAILURE);
1001 main(int argc, char *argv[])
1005 setlocale(LC_ALL, "");
1007 while ((c = getopt(argc, argv, "fht:")) != -1) {
1011 ripoffline(-1, rip_footer);
1014 ripoffline(1, rip_header);
1016 #endif /* HAVE_RIPOFFLINE */
1019 curses_trace((unsigned) strtoul(optarg, 0, 0));
1034 init_pair(1, COLOR_RED, COLOR_BLACK);
1035 init_pair(2, COLOR_BLUE, COLOR_WHITE);
1037 status = newwin(3, COLS, LINES - 3, 0);
1038 build_menus(argc > 1 ? argv[1] : 0);
1043 ExitProgram(EXIT_SUCCESS);
1049 printf("This program requires the curses menu library\n");
1050 ExitProgram(EXIT_FAILURE);