1 /****************************************************************************
2 * Copyright (c) 2005-2016,2017 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.64 2017/06/24 14:21:52 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];
113 static void failed(const char *s) GCC_NORETURN;
116 failed(const char *s)
120 ExitProgram(EXIT_FAILURE);
123 /* Common function to allow ^T to toggle trace-mode in the middle of a test
124 * so that trace-files can be made smaller.
127 wGetchar(WINDOW *win)
131 while ((c = wgetch(win)) == CTRL('T')) {
133 save_trace = _nc_tracing;
134 Trace(("TOGGLE-TRACING OFF"));
137 _nc_tracing = save_trace;
141 Trace(("TOGGLE-TRACING ON"));
148 #define Getchar() wGetchar(stdscr)
151 menu_virtualize(int c)
155 if (c == '\n' || c == KEY_EXIT)
156 result = (MAX_COMMAND + 1);
158 result = (REQ_SCR_ULINE);
160 result = (REQ_SCR_DLINE);
161 else if (c == 'b' || c == KEY_NPAGE)
162 result = (REQ_SCR_UPAGE);
163 else if (c == 'f' || c == KEY_PPAGE)
164 result = (REQ_SCR_DPAGE);
165 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
166 result = (REQ_LEFT_ITEM);
167 else if (c == 'n' || c == KEY_DOWN)
168 result = (REQ_NEXT_ITEM);
169 else if (c == 'p' || c == KEY_UP)
170 result = (REQ_PREV_ITEM);
171 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
172 result = (REQ_RIGHT_ITEM);
174 result = (REQ_TOGGLE_ITEM);
186 return wGetchar(menu_win(m));
190 menu_offset(MenuNo number)
194 if (okMenuNo(number)) {
195 int spc_desc, spc_rows, spc_cols;
197 #ifdef NCURSES_VERSION
198 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
203 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
204 result = (number - (eBanner + 1)) * (menu_itemwidth(mpBanner) + spc_rows);
210 my_menu_init(MENU * menu)
212 Trace(("called MenuHook my_menu_init"));
213 mvwprintw(status, 2, 0, "menu_init %p", (void *) menu);
219 my_menu_term(MENU * menu)
221 Trace(("called MenuHook my_menu_term"));
222 mvwprintw(status, 2, 0, "menu_term %p", (void *) menu);
228 my_item_init(MENU * menu)
230 ITEM *item = current_item(menu);
231 const char *name = item_name(item);
233 Trace(("called MenuHook my_item_init (%s)", name));
234 mvwprintw(status, 2, 0, "item_init %s", name);
240 my_item_term(MENU * menu)
242 ITEM *item = current_item(menu);
243 const char *name = item_name(item);
245 Trace(("called MenuHook my_item_term (%s)", name));
246 mvwprintw(status, 2, 0, "item_term %s", name);
252 menu_create(ITEM ** items, int count, int ncols, MenuNo number)
257 int y = okMenuNo(number) ? MENU_Y : 0;
258 int x = menu_offset(number);
259 int margin = (y == MENU_Y) ? 1 : 0;
260 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
261 int maxrow = (count + 1) / ncols;
263 if ((maxrow + y) >= (LINES - 4))
264 maxrow = LINES - 4 - y;
266 result = new_menu(items);
269 set_menu_fore(result, (chtype) COLOR_PAIR(1));
270 set_menu_back(result, (chtype) COLOR_PAIR(2));
273 set_menu_format(result, maxrow, maxcol);
274 scale_menu(result, &mrows, &mcols);
276 if (mcols + (2 * margin + x) >= COLS)
277 mcols = COLS - (2 * margin + x);
279 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
280 set_menu_win(result, menuwin);
281 keypad(menuwin, TRUE);
285 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
288 if (number == eTrace)
289 menu_opts_off(result, O_ONEVALUE);
291 menu_opts_on(result, O_ONEVALUE);
293 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
294 menu_opts_on(result, O_MOUSE_MENU);
299 set_menu_init(result, my_menu_init);
300 set_menu_term(result, my_menu_term);
301 set_item_init(result, my_item_init);
302 set_item_term(result, my_item_term);
307 menu_destroy(MENU * m)
311 Trace(("menu_destroy %p", (void *) m));
313 ITEM **items = menu_items(m);
314 const char *blob = 0;
316 count = item_count(m);
317 Trace(("menu_destroy %p count %d", (void *) m, count));
318 if ((count > 0) && (m == mpSelect)) {
319 blob = item_name(*items);
325 /* free the extra data allocated in build_select_menu() */
326 if ((count > 0) && (m == mpSelect)) {
327 if (blob && loaded_file) {
328 Trace(("freeing blob %p", blob));
335 if ((count > 0) && (m == mpTrace)) {
346 /* force the given menu to appear */
348 menu_display(MENU * m)
350 touchwin(menu_win(m));
351 wrefresh(menu_win(m));
354 /*****************************************************************************/
357 build_file_menu(MenuNo number)
359 static MENU_DATA table[] =
361 {"Exit", call_files, 0},
364 static ITEM *items[SIZEOF(table)];
369 for (n = 0; table[n].name != 0; ++n) {
370 *ip = new_item(table[n].name, empty);
371 set_item_userptr(*ip, (void *) &table[n]);
376 mpFile = menu_create(items, SIZEOF(table) - 1, 1, number);
380 perform_file_menu(int cmd)
382 return menu_driver(mpFile, cmd);
385 /*****************************************************************************/
388 call_select(int code)
391 Trace(("Selected item %d", code));
395 build_select_menu(MenuNo number, char *filename)
397 #define MY_DATA(name) { name, call_select, 0 }
398 static MENU_DATA table[] =
407 MY_DATA("(Oh really?!)"),
411 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
412 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
419 MENU_DATA *myList = 0;
425 if (stat(filename, &sb) == 0
426 && (sb.st_mode & S_IFMT) == S_IFREG
427 && sb.st_size != 0) {
428 size_t size = (size_t) sb.st_size;
430 char *blob = typeMalloc(char, size + 1);
431 MENU_DATA *list = typeCalloc(MENU_DATA, size + 1);
433 items = typeCalloc(ITEM *, size + 1);
434 Trace(("build_select_menu blob=%p, items=%p",
437 if (blob != 0 && list != 0) {
438 FILE *fp = fopen(filename, "r");
440 if (fread(blob, sizeof(char), size, fp) == size) {
442 for (j = k = 0; j < size; ++j) {
444 list[k++].name = blob + j;
447 if (blob[j] == '\n') {
449 if (k > 0 && *list[k - 1].name == '\0')
452 } else if (blob[j] == '\t') {
453 blob[j] = ' '; /* menu items are printable */
469 count = SIZEOF(table) - 1;
470 items = typeCalloc(ITEM *, count + 1);
475 for (i = 0; ap[i].name != 0; ++i) {
476 ap[i].func = call_select;
477 ap[i].mask = (unsigned) i;
478 *ip = new_item(ap[i].name, empty);
479 set_item_userptr(*ip, (void *) &table[i]);
484 mpSelect = menu_create(items, (int) count, 1, number);
490 perform_select_menu(int cmd)
492 return menu_driver(mpSelect, cmd);
495 /*****************************************************************************/
503 Trace(("Updating trace mask %d", code));
506 #define T_TBL(name) { #name, call_trace, name }
507 static MENU_DATA t_tbl[] =
510 T_TBL(TRACE_DISABLE),
515 T_TBL(TRACE_CHARPUT),
516 T_TBL(TRACE_ORDINARY),
518 T_TBL(TRACE_VIRTPUT),
523 T_TBL(TRACE_DATABASE),
525 T_TBL(TRACE_MAXIMUM),
532 build_trace_menu(MenuNo number)
534 static ITEM *items[SIZEOF(t_tbl)];
539 for (n = 0; t_tbl[n].name != 0; n++) {
540 *ip = new_item(t_tbl[n].name, empty);
541 set_item_userptr(*ip, (void *) &t_tbl[n]);
546 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
550 tracetrace(unsigned tlevel)
553 static size_t need = 12;
557 for (n = 0; t_tbl[n].name != 0; n++)
558 need += strlen(t_tbl[n].name) + 2;
559 buf = typeMalloc(char, need);
561 failed("tracetrace");
563 _nc_SPRINTF(buf, _nc_SLIMIT(need) "0x%02x = {", tlevel);
565 _nc_STRCAT(buf, t_tbl[0].name, need);
566 _nc_STRCAT(buf, ", ", need);
568 for (n = 1; t_tbl[n].name != 0; n++)
569 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
570 _nc_STRCAT(buf, t_tbl[n].name, need);
571 _nc_STRCAT(buf, ", ", need);
574 if (buf[strlen(buf) - 2] == ',')
575 buf[strlen(buf) - 2] = '\0';
576 _nc_STRCAT(buf, "}", need);
580 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
584 update_trace_menu(MENU * m)
588 bool changed = FALSE;
590 items = menu_items(m);
594 for (p = items + 1; *p != 0; p++)
595 if (item_value(*p)) {
596 set_item_value(*p, FALSE);
605 perform_trace_menu(int cmd)
606 /* interactively set the trace level */
612 for (ip = menu_items(mpTrace); *ip; ip++) {
613 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
614 unsigned mask = td->mask;
616 set_item_value(*ip, _nc_tracing == 0);
617 else if ((mask & _nc_tracing) == mask)
618 set_item_value(*ip, TRUE);
621 result = menu_driver(mpTrace, cmd);
623 if (result == E_OK) {
624 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
626 for (ip = menu_items(mpTrace); *ip; ip++) {
627 if (item_value(*ip)) {
628 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
629 newtrace |= td->mask;
633 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
635 MvWPrintw(status, 1, 0,
636 "Trace level is %s\n", tracetrace(_nc_tracing));
644 /*****************************************************************************/
649 return item_index(current_item(mpBanner)) - (eBanner + 1);
657 switch (menu_number()) {
680 Trace(("Activated menu %d\n", code));
684 build_menus(char *filename)
686 static MENU_DATA table[] =
688 {"File", call_menus, 0},
689 {"Select", call_menus, 1},
691 {"Trace", call_menus, 2},
695 static ITEM *items[SIZEOF(table)];
700 for (n = 0; table[n].name != 0; ++n) {
701 *ip = new_item(table[n].name, empty);
702 set_item_userptr(*ip, (void *) &table[n]);
707 mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
708 set_menu_mark(mpBanner, ">");
710 build_file_menu(eFile);
711 build_select_menu(eSelect, filename);
713 build_trace_menu(eTrace);
718 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
720 WINDOW *top_win = menu_win(menu);
721 WINDOW *sub_win = menu_sub(menu);
726 getbegyx(top_win, y0, x0);
730 getbegyx(sub_win, y1, x1);
734 if ((result = mvwin(top_win, y0, x0)) != ERR) {
735 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
739 mvwin(sub_win, y1, x1);
741 if (menu == current) {
743 wnoutrefresh(top_win);
750 * Move the menus around on the screen, to test mvwin().
753 move_menus(MENU * current, int by_y, int by_x)
755 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
757 wnoutrefresh(stdscr);
758 move_menu(mpFile, current, by_y, by_x);
759 move_menu(mpSelect, current, by_y, by_x);
761 move_menu(mpTrace, current, by_y, by_x);
769 resize_menu(MENU ** menu)
772 WINDOW *win = menu_win(*menu);
773 WINDOW *sub = menu_sub(*menu);
779 resize_menus(MENU * current)
784 wnoutrefresh(status);
785 wresize(status, 1, COLS);
786 mvwin(status, LINES - 1, 0);
788 resize_menu(&mpBanner);
789 resize_menu(&mpFile);
790 resize_menu(&mpSelect);
792 resize_menu(&mpTrace);
798 show_status(int ch, MENU * menu)
801 wprintw(status, "key %s, menu %d, mark %s, match %s",
814 MENU *last_menu = mpFile;
815 int code = E_UNKNOWN_COMMAND;
819 #ifdef NCURSES_MOUSE_VERSION
820 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
823 menu_display(last_menu);
828 show_status(ch, last_menu);
830 ch = menu_getc(mpBanner);
833 * Provide for moving the menu around in the screen using shifted
838 move_menus(last_menu, 1, 0);
841 move_menus(last_menu, -1, 0);
844 move_menus(last_menu, 0, -1);
847 move_menus(last_menu, 0, 1);
851 resize_menus(last_menu);
855 cmd = menu_virtualize(ch);
859 * The banner menu acts solely to select one of the other menus.
860 * Move between its items, wrapping at the left/right limits.
864 code = menu_driver(mpBanner, cmd);
865 if (code == E_REQUEST_DENIED) {
866 if (menu_number() > 0)
867 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
869 code = menu_driver(mpBanner, REQ_LAST_ITEM);
873 switch (menu_number()) {
875 code = perform_file_menu(cmd);
878 code = perform_select_menu(cmd);
882 code = perform_trace_menu(cmd);
887 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
888 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
889 (void) menu_getc(mpBanner);
890 code = menu_driver(mpBanner, cmd);
891 if (code == E_REQUEST_DENIED) {
893 if (menu_getc(mpBanner) == KEY_MOUSE)
894 getmouse(&event); /* give up */
903 this_menu = current_menu();
904 if (this_menu != last_menu) {
907 box(menu_win(this_menu), 0, 0);
910 /* force the current menu to appear */
911 menu_display(this_menu);
913 last_menu = this_menu;
916 wrefresh(menu_win(last_menu));
917 if (code == E_UNKNOWN_COMMAND
918 || code == E_NOT_POSTED) {
919 ITEM *item = current_item(last_menu);
920 MENU_DATA *td = (MENU_DATA *) item_userptr(item);
921 td->func((int) td->mask);
923 if (code == E_REQUEST_DENIED)
932 menu_destroy(mpFile);
933 menu_destroy(mpSelect);
935 menu_destroy(mpTrace);
937 menu_destroy(mpBanner);
942 rip_footer(WINDOW *win, int cols)
944 wbkgd(win, A_REVERSE);
947 wprintw(win, "footer: %d columns", cols);
953 rip_header(WINDOW *win, int cols)
955 wbkgd(win, A_REVERSE);
958 wprintw(win, "header: %d columns", cols);
962 #endif /* HAVE_RIPOFFLINE */
972 ExitProgram(EXIT_SUCCESS);
979 static const char *const tbl[] =
981 "Usage: demo_menus [options] [menu-file]"
985 ," -f rip-off footer line (can repeat)"
986 ," -h rip-off header line (can repeat)"
989 ," -t mask specify default trace-level (may toggle with ^T)"
993 for (n = 0; n < SIZEOF(tbl); n++)
994 fprintf(stderr, "%s\n", tbl[n]);
995 ExitProgram(EXIT_FAILURE);
999 main(int argc, char *argv[])
1003 setlocale(LC_ALL, "");
1005 while ((c = getopt(argc, argv, "fht:")) != -1) {
1009 ripoffline(-1, rip_footer);
1012 ripoffline(1, rip_header);
1014 #endif /* HAVE_RIPOFFLINE */
1017 trace((unsigned) strtoul(optarg, 0, 0));
1032 init_pair(1, COLOR_RED, COLOR_BLACK);
1033 init_pair(2, COLOR_BLUE, COLOR_WHITE);
1035 status = newwin(3, COLS, LINES - 3, 0);
1036 build_menus(argc > 1 ? argv[1] : 0);
1041 ExitProgram(EXIT_SUCCESS);
1047 printf("This program requires the curses menu library\n");
1048 ExitProgram(EXIT_FAILURE);