1 /****************************************************************************
2 * Copyright (c) 2005-2015,2016 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.59 2016/03/27 00:02:01 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;
78 extern unsigned _nc_tracing;
95 #define okMenuNo(n) (((n) > eBanner) && ((n) < eMAX))
100 NCURSES_CONST char *name;
105 static void call_files(int);
107 static MENU *mpBanner;
109 static MENU *mpSelect;
111 static WINDOW *status;
113 static bool loaded_file = FALSE;
115 static char empty[1];
117 /* Common function to allow ^T to toggle trace-mode in the middle of a test
118 * so that trace-files can be made smaller.
121 wGetchar(WINDOW *win)
125 while ((c = wgetch(win)) == CTRL('T')) {
127 save_trace = _nc_tracing;
128 Trace(("TOGGLE-TRACING OFF"));
131 _nc_tracing = save_trace;
135 Trace(("TOGGLE-TRACING ON"));
142 #define Getchar() wGetchar(stdscr)
145 menu_virtualize(int c)
149 if (c == '\n' || c == KEY_EXIT)
150 result = (MAX_COMMAND + 1);
152 result = (REQ_SCR_ULINE);
154 result = (REQ_SCR_DLINE);
155 else if (c == 'b' || c == KEY_NPAGE)
156 result = (REQ_SCR_UPAGE);
157 else if (c == 'f' || c == KEY_PPAGE)
158 result = (REQ_SCR_DPAGE);
159 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
160 result = (REQ_LEFT_ITEM);
161 else if (c == 'n' || c == KEY_DOWN)
162 result = (REQ_NEXT_ITEM);
163 else if (c == 'p' || c == KEY_UP)
164 result = (REQ_PREV_ITEM);
165 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
166 result = (REQ_RIGHT_ITEM);
168 result = (REQ_TOGGLE_ITEM);
180 return wGetchar(menu_win(m));
184 menu_offset(MenuNo number)
188 if (okMenuNo(number)) {
189 int spc_desc, spc_rows, spc_cols;
191 #ifdef NCURSES_VERSION
192 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
197 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
198 result = (number - (eBanner + 1)) * (menu_itemwidth(mpBanner) + spc_rows);
204 my_menu_init(MENU * menu)
206 Trace(("called MenuHook my_menu_init"));
207 mvwprintw(status, 2, 0, "menu_init %p", (void *) menu);
213 my_menu_term(MENU * menu)
215 Trace(("called MenuHook my_menu_term"));
216 mvwprintw(status, 2, 0, "menu_term %p", (void *) menu);
222 my_item_init(MENU * menu)
224 ITEM *item = current_item(menu);
225 const char *name = item_name(item);
227 Trace(("called MenuHook my_item_init (%s)", name));
228 mvwprintw(status, 2, 0, "item_init %s", name);
234 my_item_term(MENU * menu)
236 ITEM *item = current_item(menu);
237 const char *name = item_name(item);
239 Trace(("called MenuHook my_item_term (%s)", name));
240 mvwprintw(status, 2, 0, "item_term %s", name);
246 menu_create(ITEM ** items, int count, int ncols, MenuNo number)
251 int y = okMenuNo(number) ? MENU_Y : 0;
252 int x = menu_offset(number);
253 int margin = (y == MENU_Y) ? 1 : 0;
254 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
255 int maxrow = (count + 1) / ncols;
257 if ((maxrow + y) >= (LINES - 4))
258 maxrow = LINES - 4 - y;
260 result = new_menu(items);
263 set_menu_fore(result, (chtype) COLOR_PAIR(1));
264 set_menu_back(result, (chtype) COLOR_PAIR(2));
267 set_menu_format(result, maxrow, maxcol);
268 scale_menu(result, &mrows, &mcols);
270 if (mcols + (2 * margin + x) >= COLS)
271 mcols = COLS - (2 * margin + x);
273 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
274 set_menu_win(result, menuwin);
275 keypad(menuwin, TRUE);
279 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
282 if (number == eTrace)
283 menu_opts_off(result, O_ONEVALUE);
285 menu_opts_on(result, O_ONEVALUE);
287 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
288 menu_opts_on(result, O_MOUSE_MENU);
293 set_menu_init(result, my_menu_init);
294 set_menu_term(result, my_menu_term);
295 set_item_init(result, my_item_init);
296 set_item_term(result, my_item_term);
301 menu_destroy(MENU * m)
305 Trace(("menu_destroy %p", (void *) m));
307 ITEM **items = menu_items(m);
308 const char *blob = 0;
310 count = item_count(m);
311 Trace(("menu_destroy %p count %d", (void *) m, count));
312 if ((count > 0) && (m == mpSelect)) {
313 blob = item_name(*items);
319 /* free the extra data allocated in build_select_menu() */
320 if ((count > 0) && (m == mpSelect)) {
321 if (blob && loaded_file) {
322 Trace(("freeing blob %p", blob));
329 if ((count > 0) && (m == mpTrace)) {
340 /* force the given menu to appear */
342 menu_display(MENU * m)
344 touchwin(menu_win(m));
345 wrefresh(menu_win(m));
348 /*****************************************************************************/
351 build_file_menu(MenuNo number)
353 static MENU_DATA table[] =
355 {"Exit", call_files, 0},
358 static ITEM *items[SIZEOF(table)];
363 for (n = 0; table[n].name != 0; ++n) {
364 *ip = new_item(table[n].name, empty);
365 set_item_userptr(*ip, (void *) &table[n]);
370 mpFile = menu_create(items, SIZEOF(table) - 1, 1, number);
374 perform_file_menu(int cmd)
376 return menu_driver(mpFile, cmd);
379 /*****************************************************************************/
382 call_select(int code)
385 Trace(("Selected item %d", code));
389 build_select_menu(MenuNo number, char *filename)
391 #define MY_DATA(name) { name, call_select, 0 }
392 static MENU_DATA table[] =
401 MY_DATA("(Oh really?!)"),
405 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
406 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
413 MENU_DATA *myList = 0;
419 if (stat(filename, &sb) == 0
420 && (sb.st_mode & S_IFMT) == S_IFREG
421 && sb.st_size != 0) {
422 size_t size = (size_t) sb.st_size;
424 char *blob = typeMalloc(char, size + 1);
425 MENU_DATA *list = typeCalloc(MENU_DATA, size + 1);
427 items = typeCalloc(ITEM *, size + 1);
428 Trace(("build_select_menu blob=%p, items=%p",
431 if (blob != 0 && list != 0) {
432 FILE *fp = fopen(filename, "r");
434 if (fread(blob, sizeof(char), size, fp) == size) {
436 for (j = k = 0; j < size; ++j) {
438 list[k++].name = blob + j;
441 if (blob[j] == '\n') {
443 if (k > 0 && *list[k - 1].name == '\0')
446 } else if (blob[j] == '\t') {
447 blob[j] = ' '; /* menu items are printable */
463 count = SIZEOF(table) - 1;
464 items = typeCalloc(ITEM *, count + 1);
469 for (i = 0; ap[i].name != 0; ++i) {
470 ap[i].func = call_select;
471 ap[i].mask = (unsigned) i;
472 *ip = new_item(ap[i].name, empty);
473 set_item_userptr(*ip, (void *) &table[i]);
478 mpSelect = menu_create(items, (int) count, 1, number);
484 perform_select_menu(int cmd)
486 return menu_driver(mpSelect, cmd);
489 /*****************************************************************************/
497 Trace(("Updating trace mask %d", code));
500 #define T_TBL(name) { #name, call_trace, name }
501 static MENU_DATA t_tbl[] =
504 T_TBL(TRACE_DISABLE),
509 T_TBL(TRACE_CHARPUT),
510 T_TBL(TRACE_ORDINARY),
512 T_TBL(TRACE_VIRTPUT),
517 T_TBL(TRACE_DATABASE),
519 T_TBL(TRACE_MAXIMUM),
526 build_trace_menu(MenuNo number)
528 static ITEM *items[SIZEOF(t_tbl)];
533 for (n = 0; t_tbl[n].name != 0; n++) {
534 *ip = new_item(t_tbl[n].name, empty);
535 set_item_userptr(*ip, (void *) &t_tbl[n]);
540 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
544 tracetrace(unsigned tlevel)
551 for (n = 0; t_tbl[n].name != 0; n++)
552 need += strlen(t_tbl[n].name) + 2;
553 buf = typeMalloc(char, need);
555 sprintf(buf, "0x%02x = {", tlevel);
557 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
559 for (n = 1; t_tbl[n].name != 0; n++)
560 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
561 strcat(buf, t_tbl[n].name);
565 if (buf[strlen(buf) - 2] == ',')
566 buf[strlen(buf) - 2] = '\0';
567 return (strcat(buf, "}"));
570 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
574 update_trace_menu(MENU * m)
578 bool changed = FALSE;
580 items = menu_items(m);
584 for (p = items + 1; *p != 0; p++)
585 if (item_value(*p)) {
586 set_item_value(*p, FALSE);
595 perform_trace_menu(int cmd)
596 /* interactively set the trace level */
602 for (ip = menu_items(mpTrace); *ip; ip++) {
603 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
604 unsigned mask = td->mask;
606 set_item_value(*ip, _nc_tracing == 0);
607 else if ((mask & _nc_tracing) == mask)
608 set_item_value(*ip, TRUE);
611 result = menu_driver(mpTrace, cmd);
613 if (result == E_OK) {
614 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
616 for (ip = menu_items(mpTrace); *ip; ip++) {
617 if (item_value(*ip)) {
618 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
619 newtrace |= td->mask;
623 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
625 MvWPrintw(status, 1, 0,
626 "Trace level is %s\n", tracetrace(_nc_tracing));
634 /*****************************************************************************/
639 return item_index(current_item(mpBanner)) - (eBanner + 1);
647 switch (menu_number()) {
670 Trace(("Activated menu %d\n", code));
674 build_menus(char *filename)
676 static MENU_DATA table[] =
678 {"File", call_menus, 0},
679 {"Select", call_menus, 1},
681 {"Trace", call_menus, 2},
685 static ITEM *items[SIZEOF(table)];
690 for (n = 0; table[n].name != 0; ++n) {
691 *ip = new_item(table[n].name, empty);
692 set_item_userptr(*ip, (void *) &table[n]);
697 mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
698 set_menu_mark(mpBanner, ">");
700 build_file_menu(eFile);
701 build_select_menu(eSelect, filename);
703 build_trace_menu(eTrace);
708 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
710 WINDOW *top_win = menu_win(menu);
711 WINDOW *sub_win = menu_sub(menu);
716 getbegyx(top_win, y0, x0);
720 getbegyx(sub_win, y1, x1);
724 if ((result = mvwin(top_win, y0, x0)) != ERR) {
725 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
729 mvwin(sub_win, y1, x1);
731 if (menu == current) {
733 wnoutrefresh(top_win);
740 * Move the menus around on the screen, to test mvwin().
743 move_menus(MENU * current, int by_y, int by_x)
745 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
747 wnoutrefresh(stdscr);
748 move_menu(mpFile, current, by_y, by_x);
749 move_menu(mpSelect, current, by_y, by_x);
751 move_menu(mpTrace, current, by_y, by_x);
759 resize_menu(MENU ** menu)
762 WINDOW *win = menu_win(*menu);
763 WINDOW *sub = menu_sub(*menu);
769 resize_menus(MENU * current)
774 wnoutrefresh(status);
775 wresize(status, 1, COLS);
776 mvwin(status, LINES - 1, 0);
778 resize_menu(&mpBanner);
779 resize_menu(&mpFile);
780 resize_menu(&mpSelect);
782 resize_menu(&mpTrace);
788 show_status(int ch, MENU * menu)
791 wprintw(status, "key %s, menu %d, mark %s, match %s",
804 MENU *last_menu = mpFile;
805 int code = E_UNKNOWN_COMMAND;
809 #ifdef NCURSES_MOUSE_VERSION
810 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
813 menu_display(last_menu);
818 show_status(ch, last_menu);
820 ch = menu_getc(mpBanner);
823 * Provide for moving the menu around in the screen using shifted
828 move_menus(last_menu, 1, 0);
831 move_menus(last_menu, -1, 0);
834 move_menus(last_menu, 0, -1);
837 move_menus(last_menu, 0, 1);
841 resize_menus(last_menu);
845 cmd = menu_virtualize(ch);
849 * The banner menu acts solely to select one of the other menus.
850 * Move between its items, wrapping at the left/right limits.
854 code = menu_driver(mpBanner, cmd);
855 if (code == E_REQUEST_DENIED) {
856 if (menu_number() > 0)
857 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
859 code = menu_driver(mpBanner, REQ_LAST_ITEM);
863 switch (menu_number()) {
865 code = perform_file_menu(cmd);
868 code = perform_select_menu(cmd);
872 code = perform_trace_menu(cmd);
877 #if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
878 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
879 (void) menu_getc(mpBanner);
880 code = menu_driver(mpBanner, cmd);
881 if (code == E_REQUEST_DENIED) {
883 if (menu_getc(mpBanner) == KEY_MOUSE)
884 getmouse(&event); /* give up */
893 this_menu = current_menu();
894 if (this_menu != last_menu) {
897 box(menu_win(this_menu), 0, 0);
900 /* force the current menu to appear */
901 menu_display(this_menu);
903 last_menu = this_menu;
906 wrefresh(menu_win(last_menu));
907 if (code == E_UNKNOWN_COMMAND
908 || code == E_NOT_POSTED) {
909 ITEM *item = current_item(last_menu);
910 MENU_DATA *td = (MENU_DATA *) item_userptr(item);
911 td->func((int) td->mask);
913 if (code == E_REQUEST_DENIED)
922 menu_destroy(mpFile);
923 menu_destroy(mpSelect);
925 menu_destroy(mpTrace);
927 menu_destroy(mpBanner);
932 rip_footer(WINDOW *win, int cols)
934 wbkgd(win, A_REVERSE);
937 wprintw(win, "footer: %d columns", cols);
943 rip_header(WINDOW *win, int cols)
945 wbkgd(win, A_REVERSE);
948 wprintw(win, "header: %d columns", cols);
952 #endif /* HAVE_RIPOFFLINE */
962 ExitProgram(EXIT_SUCCESS);
969 static const char *const tbl[] =
971 "Usage: demo_menus [options] [menu-file]"
975 ," -f rip-off footer line (can repeat)"
976 ," -h rip-off header line (can repeat)"
979 ," -t mask specify default trace-level (may toggle with ^T)"
983 for (n = 0; n < SIZEOF(tbl); n++)
984 fprintf(stderr, "%s\n", tbl[n]);
985 ExitProgram(EXIT_FAILURE);
989 main(int argc, char *argv[])
993 setlocale(LC_ALL, "");
995 while ((c = getopt(argc, argv, "fht:")) != -1) {
999 ripoffline(-1, rip_footer);
1002 ripoffline(1, rip_header);
1004 #endif /* HAVE_RIPOFFLINE */
1007 trace((unsigned) strtoul(optarg, 0, 0));
1022 init_pair(1, COLOR_RED, COLOR_BLACK);
1023 init_pair(2, COLOR_BLUE, COLOR_WHITE);
1025 status = newwin(3, COLS, LINES - 3, 0);
1026 build_menus(argc > 1 ? argv[1] : 0);
1031 ExitProgram(EXIT_SUCCESS);
1037 printf("This program requires the curses menu library\n");
1038 ExitProgram(EXIT_FAILURE);