1 /****************************************************************************
2 * Copyright (c) 2005-2011,2012 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.37 2012/11/18 00:18:54 tom Exp $
31 * Demonstrate a variety of functions from the menu library.
32 * Thomas Dickey - 2005/4/9
50 menu_request_by_name -
73 #include <test.priv.h>
79 #include <sys/types.h>
82 #ifdef NCURSES_VERSION
84 static unsigned save_trace = TRACE_ORDINARY | TRACE_CALLS;
85 extern unsigned _nc_tracing;
102 #define okMenuNo(n) (((n) > eBanner) && ((n) < eMAX))
106 static MENU *mpBanner;
108 static MENU *mpSelect;
110 static bool loaded_file = FALSE;
112 /* Common function to allow ^T to toggle trace-mode in the middle of a test
113 * so that trace-files can be made smaller.
116 wGetchar(WINDOW *win)
120 while ((c = wgetch(win)) == CTRL('T')) {
122 save_trace = _nc_tracing;
123 Trace(("TOGGLE-TRACING OFF"));
126 _nc_tracing = save_trace;
130 Trace(("TOGGLE-TRACING ON"));
137 #define Getchar() wGetchar(stdscr)
140 menu_virtualize(int c)
144 if (c == '\n' || c == KEY_EXIT)
145 result = (MAX_COMMAND + 1);
147 result = (REQ_SCR_ULINE);
149 result = (REQ_SCR_DLINE);
150 else if (c == 'b' || c == KEY_NPAGE)
151 result = (REQ_SCR_UPAGE);
152 else if (c == 'f' || c == KEY_PPAGE)
153 result = (REQ_SCR_DPAGE);
154 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
155 result = (REQ_LEFT_ITEM);
156 else if (c == 'n' || c == KEY_DOWN)
157 result = (REQ_NEXT_ITEM);
158 else if (c == 'p' || c == KEY_UP)
159 result = (REQ_PREV_ITEM);
160 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
161 result = (REQ_RIGHT_ITEM);
163 result = (REQ_TOGGLE_ITEM);
175 return wGetchar(menu_win(m));
179 menu_offset(MenuNo number)
183 if (okMenuNo(number)) {
184 int spc_desc, spc_rows, spc_cols;
186 #ifdef NCURSES_VERSION
187 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
192 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
193 result = (number - (eBanner + 1)) * (menu_itemwidth(mpBanner) + spc_rows);
199 menu_create(ITEM ** items, int count, int ncols, MenuNo number)
204 int y = okMenuNo(number) ? MENU_Y : 0;
205 int x = menu_offset(number);
206 int margin = (y == MENU_Y) ? 1 : 0;
207 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
208 int maxrow = (count + 1) / ncols;
210 if ((maxrow + y) >= (LINES - 4))
211 maxrow = LINES - 4 - y;
213 result = new_menu(items);
216 set_menu_fore(result, (chtype) COLOR_PAIR(1));
217 set_menu_back(result, (chtype) COLOR_PAIR(2));
220 set_menu_format(result, maxrow, maxcol);
221 scale_menu(result, &mrows, &mcols);
223 if (mcols + (2 * margin + x) >= COLS)
224 mcols = COLS - (2 * margin + x);
227 if (number == eTrace)
228 menu_opts_off(result, O_ONEVALUE);
230 menu_opts_on(result, O_ONEVALUE);
233 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
234 set_menu_win(result, menuwin);
235 keypad(menuwin, TRUE);
239 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
247 menu_destroy(MENU * m)
251 Trace(("menu_destroy %p", (void *) m));
253 ITEM **items = menu_items(m);
254 const char *blob = 0;
256 count = item_count(m);
257 Trace(("menu_destroy %p count %d", (void *) m, count));
258 if ((count > 0) && (m == mpSelect)) {
259 blob = item_name(*items);
265 /* free the extra data allocated in build_select_menu() */
266 if ((count > 0) && (m == mpSelect)) {
267 if (blob && loaded_file) {
268 Trace(("freeing blob %p", blob));
275 if ((count > 0) && (m == mpTrace)) {
286 /* force the given menu to appear */
288 menu_display(MENU * m)
290 touchwin(menu_win(m));
291 wrefresh(menu_win(m));
294 /*****************************************************************************/
297 build_file_menu(MenuNo number)
299 static CONST_MENUS char *labels[] =
304 static ITEM *items[SIZEOF(labels)];
307 CONST_MENUS char **ap;
309 for (ap = labels; *ap; ap++)
310 *ip++ = new_item(*ap, "");
313 mpFile = menu_create(items, SIZEOF(labels) - 1, 1, number);
317 perform_file_menu(int cmd)
319 return menu_driver(mpFile, cmd);
322 /*****************************************************************************/
325 build_select_menu(MenuNo number, char *filename)
327 static CONST_MENUS char *labels[] =
340 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
341 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
347 CONST_MENUS char **ap = 0;
348 CONST_MENUS char **myList = 0;
353 if (stat(filename, &sb) == 0
354 && (sb.st_mode & S_IFMT) == S_IFREG
355 && sb.st_size != 0) {
356 size_t size = (size_t) sb.st_size;
358 char *blob = typeMalloc(char, size + 1);
359 CONST_MENUS char **list = typeCalloc(CONST_MENUS char *, size + 1);
361 items = typeCalloc(ITEM *, size + 1);
362 Trace(("build_select_menu blob=%p, items=%p",
365 if (blob != 0 && list != 0) {
366 FILE *fp = fopen(filename, "r");
368 if (fread(blob, sizeof(char), size, fp) == size) {
370 for (j = k = 0; j < size; ++j) {
372 list[k++] = blob + j;
375 if (blob[j] == '\n') {
377 if (k > 0 && *list[k - 1] == '\0')
380 } else if (blob[j] == '\t') {
381 blob[j] = ' '; /* menu items are printable */
397 count = SIZEOF(labels) - 1;
398 items = typeCalloc(ITEM *, count + 1);
404 *ip++ = new_item(*ap++, "");
407 mpSelect = menu_create(items, (int) count, 1, number);
413 perform_select_menu(int cmd)
415 return menu_driver(mpSelect, cmd);
418 /*****************************************************************************/
421 #define T_TBL(name) { #name, name }
427 T_TBL(TRACE_DISABLE),
432 T_TBL(TRACE_CHARPUT),
433 T_TBL(TRACE_ORDINARY),
435 T_TBL(TRACE_VIRTPUT),
440 T_TBL(TRACE_DATABASE),
442 T_TBL(TRACE_MAXIMUM),
449 build_trace_menu(MenuNo number)
451 static ITEM *items[SIZEOF(t_tbl)];
456 for (n = 0; t_tbl[n].name != 0; n++)
457 *ip++ = new_item(t_tbl[n].name, "");
460 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
464 tracetrace(unsigned tlevel)
471 for (n = 0; t_tbl[n].name != 0; n++)
472 need += strlen(t_tbl[n].name) + 2;
473 buf = typeMalloc(char, need);
475 sprintf(buf, "0x%02x = {", tlevel);
477 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
479 for (n = 1; t_tbl[n].name != 0; n++)
480 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
481 strcat(buf, t_tbl[n].name);
485 if (buf[strlen(buf) - 2] == ',')
486 buf[strlen(buf) - 2] = '\0';
487 return (strcat(buf, "}"));
490 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
494 update_trace_menu(MENU * m)
498 bool changed = FALSE;
500 items = menu_items(m);
504 for (p = items + 1; *p != 0; p++)
505 if (item_value(*p)) {
506 set_item_value(*p, FALSE);
515 perform_trace_menu(int cmd)
516 /* interactively set the trace level */
522 for (ip = menu_items(mpTrace); *ip; ip++) {
523 unsigned mask = t_tbl[item_index(*ip)].mask;
525 set_item_value(*ip, _nc_tracing == 0);
526 else if ((mask & _nc_tracing) == mask)
527 set_item_value(*ip, TRUE);
530 result = menu_driver(mpTrace, cmd);
532 if (result == E_OK) {
533 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
535 for (ip = menu_items(mpTrace); *ip; ip++) {
537 newtrace |= t_tbl[item_index(*ip)].mask;
540 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
542 MvPrintw(LINES - 2, 0,
543 "Trace level is %s\n", tracetrace(_nc_tracing));
551 /*****************************************************************************/
556 return item_index(current_item(mpBanner)) - (eBanner + 1);
564 switch (menu_number()) {
584 build_menus(char *filename)
586 static CONST_MENUS char *labels[] =
595 static ITEM *items[SIZEOF(labels)];
598 CONST_MENUS char **ap;
600 for (ap = labels; *ap; ap++)
601 *ip++ = new_item(*ap, "");
604 mpBanner = menu_create(items, SIZEOF(labels) - 1, SIZEOF(labels) - 1, eBanner);
605 set_menu_mark(mpBanner, ">");
607 build_file_menu(eFile);
608 build_select_menu(eSelect, filename);
610 build_trace_menu(eTrace);
615 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
617 WINDOW *top_win = menu_win(menu);
618 WINDOW *sub_win = menu_sub(menu);
623 getbegyx(top_win, y0, x0);
627 getbegyx(sub_win, y1, x1);
631 if ((result = mvwin(top_win, y0, x0)) != ERR) {
632 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
636 mvwin(sub_win, y1, x1);
638 if (menu == current) {
640 wnoutrefresh(top_win);
647 * Move the menus around on the screen, to test mvwin().
650 move_menus(MENU * current, int by_y, int by_x)
652 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
654 wnoutrefresh(stdscr);
655 move_menu(mpFile, current, by_y, by_x);
656 move_menu(mpSelect, current, by_y, by_x);
658 move_menu(mpTrace, current, by_y, by_x);
665 show_status(int ch, MENU * menu)
668 printw("key %s, menu %d, mark %s, match %s",
681 MENU *last_menu = mpFile;
682 int code = E_UNKNOWN_COMMAND;
686 #ifdef NCURSES_MOUSE_VERSION
687 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
690 menu_display(last_menu);
695 show_status(ch, last_menu);
697 ch = menu_getc(mpBanner);
700 * Provide for moving the menu around in the screen using shifted
705 move_menus(last_menu, 1, 0);
708 move_menus(last_menu, -1, 0);
711 move_menus(last_menu, 0, -1);
714 move_menus(last_menu, 0, 1);
717 cmd = menu_virtualize(ch);
721 * The banner menu acts solely to select one of the other menus.
722 * Move between its items, wrapping at the left/right limits.
726 code = menu_driver(mpBanner, cmd);
727 if (code == E_REQUEST_DENIED) {
728 if (menu_number() > 0)
729 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
731 code = menu_driver(mpBanner, REQ_LAST_ITEM);
735 switch (menu_number()) {
737 code = perform_file_menu(cmd);
740 code = perform_select_menu(cmd);
744 code = perform_trace_menu(cmd);
749 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
750 code = menu_driver(mpBanner, cmd);
757 this_menu = current_menu();
758 if (this_menu != last_menu) {
761 box(menu_win(this_menu), 0, 0);
764 /* force the current menu to appear */
765 menu_display(this_menu);
767 last_menu = this_menu;
770 wrefresh(menu_win(last_menu));
771 if (code == E_UNKNOWN_COMMAND
772 || code == E_NOT_POSTED) {
773 if (menu_number() == eFile)
777 if (code == E_REQUEST_DENIED)
782 #ifdef NCURSES_MOUSE_VERSION
783 mousemask(0, (mmask_t *) 0);
790 menu_destroy(mpFile);
791 menu_destroy(mpSelect);
793 menu_destroy(mpTrace);
795 menu_destroy(mpBanner);
800 rip_footer(WINDOW *win, int cols)
802 wbkgd(win, A_REVERSE);
805 wprintw(win, "footer: %d columns", cols);
811 rip_header(WINDOW *win, int cols)
813 wbkgd(win, A_REVERSE);
816 wprintw(win, "header: %d columns", cols);
820 #endif /* HAVE_RIPOFFLINE */
825 static const char *const tbl[] =
827 "Usage: demo_menus [options] [menu-file]"
831 ," -f rip-off footer line (can repeat)"
832 ," -h rip-off header line (can repeat)"
835 ," -t mask specify default trace-level (may toggle with ^T)"
839 for (n = 0; n < SIZEOF(tbl); n++)
840 fprintf(stderr, "%s\n", tbl[n]);
841 ExitProgram(EXIT_FAILURE);
845 main(int argc, char *argv[])
849 setlocale(LC_ALL, "");
851 while ((c = getopt(argc, argv, "a:de:fhmp:s:t:")) != -1) {
855 ripoffline(-1, rip_footer);
858 ripoffline(1, rip_header);
860 #endif /* HAVE_RIPOFFLINE */
863 trace((unsigned) strtoul(optarg, 0, 0));
878 init_pair(1, COLOR_RED, COLOR_BLACK);
879 init_pair(2, COLOR_BLUE, COLOR_WHITE);
881 build_menus(argc > 1 ? argv[1] : 0);
886 ExitProgram(EXIT_SUCCESS);
892 printf("This program requires the curses menu library\n");
893 ExitProgram(EXIT_FAILURE);