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.68 2019/08/17 21:45:32 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;
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);
596 for (p = items + 1; *p != 0; p++)
597 if (item_value(*p)) {
598 set_item_value(*p, FALSE);
607 perform_trace_menu(int cmd)
608 /* interactively set the trace level */
613 for (ip = menu_items(mpTrace); *ip; ip++) {
614 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
615 unsigned mask = td->mask;
617 set_item_value(*ip, _nc_tracing == 0);
618 else if ((mask & _nc_tracing) == mask)
619 set_item_value(*ip, TRUE);
622 result = menu_driver(mpTrace, cmd);
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;
634 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
636 MvWPrintw(status, 1, 0,
637 "Trace level is %s\n", tracetrace(_nc_tracing));
645 /*****************************************************************************/
650 return item_index(current_item(mpBanner)) - (eBanner + 1);
658 switch (menu_number()) {
681 Trace(("Activated menu %d\n", code));
685 build_menus(char *filename)
687 static MENU_DATA table[] =
689 {"File", call_menus, 0},
690 {"Select", call_menus, 1},
692 {"Trace", call_menus, 2},
696 static ITEM *items[SIZEOF(table)];
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]);
708 mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
709 set_menu_mark(mpBanner, ">");
711 build_file_menu(eFile);
712 build_select_menu(eSelect, filename);
714 build_trace_menu(eTrace);
719 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
721 WINDOW *top_win = menu_win(menu);
722 WINDOW *sub_win = menu_sub(menu);
727 getbegyx(top_win, y0, x0);
731 getbegyx(sub_win, y1, x1);
735 if ((result = mvwin(top_win, y0, x0)) != ERR) {
736 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
740 mvwin(sub_win, y1, x1);
742 if (menu == current) {
744 wnoutrefresh(top_win);
751 * Move the menus around on the screen, to test mvwin().
754 move_menus(MENU * current, int by_y, int by_x)
756 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
758 wnoutrefresh(stdscr);
759 move_menu(mpFile, current, by_y, by_x);
760 move_menu(mpSelect, current, by_y, by_x);
762 move_menu(mpTrace, current, by_y, by_x);
768 #if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
770 resize_menu(MENU ** menu)
773 WINDOW *win = menu_win(*menu);
774 WINDOW *sub = menu_sub(*menu);
780 resize_menus(MENU * current)
785 wnoutrefresh(status);
786 wresize(status, 1, COLS);
787 mvwin(status, LINES - 1, 0);
789 resize_menu(&mpBanner);
790 resize_menu(&mpFile);
791 resize_menu(&mpSelect);
793 resize_menu(&mpTrace);
796 #endif /* defined(KEY_RESIZE) && NCURSES_EXT_FUNCS */
799 show_status(int ch, MENU * menu)
802 wprintw(status, "key %s, menu %d, mark %s, match %s",
815 MENU *last_menu = mpFile;
816 int code = E_UNKNOWN_COMMAND;
820 #ifdef NCURSES_MOUSE_VERSION
821 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
824 menu_display(last_menu);
829 show_status(ch, last_menu);
831 ch = menu_getc(mpBanner);
834 * Provide for moving the menu around in the screen using shifted
839 move_menus(last_menu, 1, 0);
842 move_menus(last_menu, -1, 0);
845 move_menus(last_menu, 0, -1);
848 move_menus(last_menu, 0, 1);
850 #if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
852 resize_menus(last_menu);
856 cmd = menu_virtualize(ch);
860 * The banner menu acts solely to select one of the other menus.
861 * Move between its items, wrapping at the left/right limits.
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);
870 code = menu_driver(mpBanner, REQ_LAST_ITEM);
874 switch (menu_number()) {
876 code = perform_file_menu(cmd);
879 code = perform_select_menu(cmd);
883 code = perform_trace_menu(cmd);
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) {
894 if (menu_getc(mpBanner) == KEY_MOUSE)
895 getmouse(&event); /* give up */
904 this_menu = current_menu();
905 if (this_menu != last_menu) {
908 box(menu_win(this_menu), 0, 0);
911 /* force the current menu to appear */
912 menu_display(this_menu);
914 last_menu = this_menu;
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);
924 if (code == E_REQUEST_DENIED)
933 menu_destroy(mpFile);
934 menu_destroy(mpSelect);
936 menu_destroy(mpTrace);
938 menu_destroy(mpBanner);
943 rip_footer(WINDOW *win, int cols)
945 wbkgd(win, A_REVERSE);
948 wprintw(win, "footer: %d columns", cols);
954 rip_header(WINDOW *win, int cols)
956 wbkgd(win, A_REVERSE);
959 wprintw(win, "header: %d columns", cols);
963 #endif /* HAVE_RIPOFFLINE */
973 ExitProgram(EXIT_SUCCESS);
980 static const char *const tbl[] =
982 "Usage: demo_menus [options] [menu-file]"
986 ," -f rip-off footer line (can repeat)"
987 ," -h rip-off header line (can repeat)"
990 ," -t mask specify default trace-level (may toggle with ^T)"
994 for (n = 0; n < SIZEOF(tbl); n++)
995 fprintf(stderr, "%s\n", tbl[n]);
996 ExitProgram(EXIT_FAILURE);
1000 main(int argc, char *argv[])
1004 setlocale(LC_ALL, "");
1006 while ((c = getopt(argc, argv, "fht:")) != -1) {
1010 ripoffline(-1, rip_footer);
1013 ripoffline(1, rip_header);
1015 #endif /* HAVE_RIPOFFLINE */
1018 trace((unsigned) strtoul(optarg, 0, 0));
1033 init_pair(1, COLOR_RED, COLOR_BLACK);
1034 init_pair(2, COLOR_BLUE, COLOR_WHITE);
1036 status = newwin(3, COLS, LINES - 3, 0);
1037 build_menus(argc > 1 ? argv[1] : 0);
1042 ExitProgram(EXIT_SUCCESS);
1048 printf("This program requires the curses menu library\n");
1049 ExitProgram(EXIT_FAILURE);