1 /****************************************************************************
2 * Copyright (c) 2005-2012,2013 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.52 2013/09/28 21:55:02 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))
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 /* Common function to allow ^T to toggle trace-mode in the middle of a test
116 * so that trace-files can be made smaller.
119 wGetchar(WINDOW *win)
123 while ((c = wgetch(win)) == CTRL('T')) {
125 save_trace = _nc_tracing;
126 Trace(("TOGGLE-TRACING OFF"));
129 _nc_tracing = save_trace;
133 Trace(("TOGGLE-TRACING ON"));
140 #define Getchar() wGetchar(stdscr)
143 menu_virtualize(int c)
147 if (c == '\n' || c == KEY_EXIT)
148 result = (MAX_COMMAND + 1);
150 result = (REQ_SCR_ULINE);
152 result = (REQ_SCR_DLINE);
153 else if (c == 'b' || c == KEY_NPAGE)
154 result = (REQ_SCR_UPAGE);
155 else if (c == 'f' || c == KEY_PPAGE)
156 result = (REQ_SCR_DPAGE);
157 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
158 result = (REQ_LEFT_ITEM);
159 else if (c == 'n' || c == KEY_DOWN)
160 result = (REQ_NEXT_ITEM);
161 else if (c == 'p' || c == KEY_UP)
162 result = (REQ_PREV_ITEM);
163 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
164 result = (REQ_RIGHT_ITEM);
166 result = (REQ_TOGGLE_ITEM);
178 return wGetchar(menu_win(m));
182 menu_offset(MenuNo number)
186 if (okMenuNo(number)) {
187 int spc_desc, spc_rows, spc_cols;
189 #ifdef NCURSES_VERSION
190 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
195 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
196 result = (number - (eBanner + 1)) * (menu_itemwidth(mpBanner) + spc_rows);
202 my_menu_init(MENU * menu)
204 Trace(("called MenuHook my_menu_init"));
205 mvwprintw(status, 2, 0, "menu_init %p", (void *) menu);
211 my_menu_term(MENU * menu)
213 Trace(("called MenuHook my_menu_term"));
214 mvwprintw(status, 2, 0, "menu_term %p", (void *) menu);
220 my_item_init(MENU * menu)
222 ITEM *item = current_item(menu);
223 const char *name = item_name(item);
225 Trace(("called MenuHook my_item_init (%s)", name));
226 mvwprintw(status, 2, 0, "item_init %s", name);
232 my_item_term(MENU * menu)
234 ITEM *item = current_item(menu);
235 const char *name = item_name(item);
237 Trace(("called MenuHook my_item_term (%s)", name));
238 mvwprintw(status, 2, 0, "item_term %s", name);
244 menu_create(ITEM ** items, int count, int ncols, MenuNo number)
249 int y = okMenuNo(number) ? MENU_Y : 0;
250 int x = menu_offset(number);
251 int margin = (y == MENU_Y) ? 1 : 0;
252 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
253 int maxrow = (count + 1) / ncols;
255 if ((maxrow + y) >= (LINES - 4))
256 maxrow = LINES - 4 - y;
258 result = new_menu(items);
261 set_menu_fore(result, (chtype) COLOR_PAIR(1));
262 set_menu_back(result, (chtype) COLOR_PAIR(2));
265 set_menu_format(result, maxrow, maxcol);
266 scale_menu(result, &mrows, &mcols);
268 if (mcols + (2 * margin + x) >= COLS)
269 mcols = COLS - (2 * margin + x);
272 if (number == eTrace)
273 menu_opts_off(result, O_ONEVALUE);
275 menu_opts_on(result, O_ONEVALUE);
278 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
279 set_menu_win(result, menuwin);
280 keypad(menuwin, TRUE);
284 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
288 set_menu_init(result, my_menu_init);
289 set_menu_term(result, my_menu_term);
290 set_item_init(result, my_item_init);
291 set_item_term(result, my_item_term);
296 menu_destroy(MENU * m)
300 Trace(("menu_destroy %p", (void *) m));
302 ITEM **items = menu_items(m);
303 const char *blob = 0;
305 count = item_count(m);
306 Trace(("menu_destroy %p count %d", (void *) m, count));
307 if ((count > 0) && (m == mpSelect)) {
308 blob = item_name(*items);
314 /* free the extra data allocated in build_select_menu() */
315 if ((count > 0) && (m == mpSelect)) {
316 if (blob && loaded_file) {
317 Trace(("freeing blob %p", blob));
324 if ((count > 0) && (m == mpTrace)) {
335 /* force the given menu to appear */
337 menu_display(MENU * m)
339 touchwin(menu_win(m));
340 wrefresh(menu_win(m));
343 /*****************************************************************************/
346 build_file_menu(MenuNo number)
348 static MENU_DATA table[] =
350 {"Exit", call_files, 0},
353 static ITEM *items[SIZEOF(table)];
358 for (n = 0; table[n].name != 0; ++n) {
359 *ip = new_item(table[n].name, "");
360 set_item_userptr(*ip, &table[n]);
365 mpFile = menu_create(items, SIZEOF(table) - 1, 1, number);
369 perform_file_menu(int cmd)
371 return menu_driver(mpFile, cmd);
374 /*****************************************************************************/
377 call_select(int code)
380 Trace(("Selected item %d", code));
384 build_select_menu(MenuNo number, char *filename)
386 #define MY_DATA(name) { name, call_select, 0 }
387 static MENU_DATA table[] =
396 MY_DATA("(Oh really?!)"),
400 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
401 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
408 MENU_DATA *myList = 0;
414 if (stat(filename, &sb) == 0
415 && (sb.st_mode & S_IFMT) == S_IFREG
416 && sb.st_size != 0) {
417 size_t size = (size_t) sb.st_size;
419 char *blob = typeMalloc(char, size + 1);
420 MENU_DATA *list = typeCalloc(MENU_DATA, size + 1);
422 items = typeCalloc(ITEM *, size + 1);
423 Trace(("build_select_menu blob=%p, items=%p",
426 if (blob != 0 && list != 0) {
427 FILE *fp = fopen(filename, "r");
429 if (fread(blob, sizeof(char), size, fp) == size) {
431 for (j = k = 0; j < size; ++j) {
433 list[k++].name = blob + j;
436 if (blob[j] == '\n') {
438 if (k > 0 && *list[k - 1].name == '\0')
441 } else if (blob[j] == '\t') {
442 blob[j] = ' '; /* menu items are printable */
458 count = SIZEOF(table) - 1;
459 items = typeCalloc(ITEM *, count + 1);
464 for (i = 0; ap[i].name != 0; ++i) {
465 ap[i].func = call_select;
466 ap[i].mask = (unsigned) i;
467 *ip = new_item(ap[i].name, "");
468 set_item_userptr(*ip, &table[i]);
473 mpSelect = menu_create(items, (int) count, 1, number);
479 perform_select_menu(int cmd)
481 return menu_driver(mpSelect, cmd);
484 /*****************************************************************************/
492 Trace(("Updating trace mask %d", code));
495 #define T_TBL(name) { #name, call_trace, name }
496 static MENU_DATA t_tbl[] =
499 T_TBL(TRACE_DISABLE),
504 T_TBL(TRACE_CHARPUT),
505 T_TBL(TRACE_ORDINARY),
507 T_TBL(TRACE_VIRTPUT),
512 T_TBL(TRACE_DATABASE),
514 T_TBL(TRACE_MAXIMUM),
521 build_trace_menu(MenuNo number)
523 static ITEM *items[SIZEOF(t_tbl)];
528 for (n = 0; t_tbl[n].name != 0; n++) {
529 *ip = new_item(t_tbl[n].name, "");
530 set_item_userptr(*ip, &t_tbl[n]);
535 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
539 tracetrace(unsigned tlevel)
546 for (n = 0; t_tbl[n].name != 0; n++)
547 need += strlen(t_tbl[n].name) + 2;
548 buf = typeMalloc(char, need);
550 sprintf(buf, "0x%02x = {", tlevel);
552 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
554 for (n = 1; t_tbl[n].name != 0; n++)
555 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
556 strcat(buf, t_tbl[n].name);
560 if (buf[strlen(buf) - 2] == ',')
561 buf[strlen(buf) - 2] = '\0';
562 return (strcat(buf, "}"));
565 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
569 update_trace_menu(MENU * m)
573 bool changed = FALSE;
575 items = menu_items(m);
579 for (p = items + 1; *p != 0; p++)
580 if (item_value(*p)) {
581 set_item_value(*p, FALSE);
590 perform_trace_menu(int cmd)
591 /* interactively set the trace level */
597 for (ip = menu_items(mpTrace); *ip; ip++) {
598 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
599 unsigned mask = td->mask;
601 set_item_value(*ip, _nc_tracing == 0);
602 else if ((mask & _nc_tracing) == mask)
603 set_item_value(*ip, TRUE);
606 result = menu_driver(mpTrace, cmd);
608 if (result == E_OK) {
609 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
611 for (ip = menu_items(mpTrace); *ip; ip++) {
612 if (item_value(*ip)) {
613 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
614 newtrace |= td->mask;
618 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
620 MvWPrintw(status, 1, 0,
621 "Trace level is %s\n", tracetrace(_nc_tracing));
629 /*****************************************************************************/
634 return item_index(current_item(mpBanner)) - (eBanner + 1);
642 switch (menu_number()) {
665 Trace(("Activated menu %d\n", code));
669 build_menus(char *filename)
671 static MENU_DATA table[] =
673 {"File", call_menus, 0},
674 {"Select", call_menus, 1},
676 {"Trace", call_menus, 2},
680 static ITEM *items[SIZEOF(table)];
685 for (n = 0; table[n].name != 0; ++n) {
686 *ip = new_item(table[n].name, "");
687 set_item_userptr(*ip, &table[n]);
692 mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
693 set_menu_mark(mpBanner, ">");
695 build_file_menu(eFile);
696 build_select_menu(eSelect, filename);
698 build_trace_menu(eTrace);
703 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
705 WINDOW *top_win = menu_win(menu);
706 WINDOW *sub_win = menu_sub(menu);
711 getbegyx(top_win, y0, x0);
715 getbegyx(sub_win, y1, x1);
719 if ((result = mvwin(top_win, y0, x0)) != ERR) {
720 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
724 mvwin(sub_win, y1, x1);
726 if (menu == current) {
728 wnoutrefresh(top_win);
735 * Move the menus around on the screen, to test mvwin().
738 move_menus(MENU * current, int by_y, int by_x)
740 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
742 wnoutrefresh(stdscr);
743 move_menu(mpFile, current, by_y, by_x);
744 move_menu(mpSelect, current, by_y, by_x);
746 move_menu(mpTrace, current, by_y, by_x);
753 show_status(int ch, MENU * menu)
756 wprintw(status, "key %s, menu %d, mark %s, match %s",
769 MENU *last_menu = mpFile;
770 int code = E_UNKNOWN_COMMAND;
774 #ifdef NCURSES_MOUSE_VERSION
775 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
778 menu_display(last_menu);
783 show_status(ch, last_menu);
785 ch = menu_getc(mpBanner);
788 * Provide for moving the menu around in the screen using shifted
793 move_menus(last_menu, 1, 0);
796 move_menus(last_menu, -1, 0);
799 move_menus(last_menu, 0, -1);
802 move_menus(last_menu, 0, 1);
805 cmd = menu_virtualize(ch);
809 * The banner menu acts solely to select one of the other menus.
810 * Move between its items, wrapping at the left/right limits.
814 code = menu_driver(mpBanner, cmd);
815 if (code == E_REQUEST_DENIED) {
816 if (menu_number() > 0)
817 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
819 code = menu_driver(mpBanner, REQ_LAST_ITEM);
823 switch (menu_number()) {
825 code = perform_file_menu(cmd);
828 code = perform_select_menu(cmd);
832 code = perform_trace_menu(cmd);
837 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
838 code = menu_driver(mpBanner, cmd);
845 this_menu = current_menu();
846 if (this_menu != last_menu) {
849 box(menu_win(this_menu), 0, 0);
852 /* force the current menu to appear */
853 menu_display(this_menu);
855 last_menu = this_menu;
858 wrefresh(menu_win(last_menu));
859 if (code == E_UNKNOWN_COMMAND
860 || code == E_NOT_POSTED) {
861 ITEM *item = current_item(last_menu);
862 MENU_DATA *td = (MENU_DATA *) item_userptr(item);
863 td->func((int) td->mask);
865 if (code == E_REQUEST_DENIED)
870 #ifdef NCURSES_MOUSE_VERSION
871 mousemask(0, (mmask_t *) 0);
878 menu_destroy(mpFile);
879 menu_destroy(mpSelect);
881 menu_destroy(mpTrace);
883 menu_destroy(mpBanner);
888 rip_footer(WINDOW *win, int cols)
890 wbkgd(win, A_REVERSE);
893 wprintw(win, "footer: %d columns", cols);
899 rip_header(WINDOW *win, int cols)
901 wbkgd(win, A_REVERSE);
904 wprintw(win, "header: %d columns", cols);
908 #endif /* HAVE_RIPOFFLINE */
918 ExitProgram(EXIT_SUCCESS);
925 static const char *const tbl[] =
927 "Usage: demo_menus [options] [menu-file]"
931 ," -f rip-off footer line (can repeat)"
932 ," -h rip-off header line (can repeat)"
935 ," -t mask specify default trace-level (may toggle with ^T)"
939 for (n = 0; n < SIZEOF(tbl); n++)
940 fprintf(stderr, "%s\n", tbl[n]);
941 ExitProgram(EXIT_FAILURE);
945 main(int argc, char *argv[])
949 setlocale(LC_ALL, "");
951 while ((c = getopt(argc, argv, "a:de:fhmp:s:t:")) != -1) {
955 ripoffline(-1, rip_footer);
958 ripoffline(1, rip_header);
960 #endif /* HAVE_RIPOFFLINE */
963 trace((unsigned) strtoul(optarg, 0, 0));
978 init_pair(1, COLOR_RED, COLOR_BLACK);
979 init_pair(2, COLOR_BLUE, COLOR_WHITE);
981 status = newwin(3, COLS, LINES - 3, 0);
982 build_menus(argc > 1 ? argv[1] : 0);
987 ExitProgram(EXIT_SUCCESS);
993 printf("This program requires the curses menu library\n");
994 ExitProgram(EXIT_FAILURE);