1 /****************************************************************************
2 * Copyright (c) 2005-2007,2008 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.23 2008/02/23 23:06:49 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;
111 #define strdup my_strdup
115 char *p = (char *) malloc(strlen(s) + 1);
120 #endif /* not HAVE_STRDUP */
122 /* Common function to allow ^T to toggle trace-mode in the middle of a test
123 * so that trace-files can be made smaller.
126 wGetchar(WINDOW *win)
130 while ((c = wgetch(win)) == CTRL('T')) {
132 save_trace = _nc_tracing;
133 Trace(("TOGGLE-TRACING OFF"));
136 _nc_tracing = save_trace;
140 Trace(("TOGGLE-TRACING ON"));
147 #define Getchar() wGetchar(stdscr)
150 menu_virtualize(int c)
154 if (c == '\n' || c == KEY_EXIT)
155 result = (MAX_COMMAND + 1);
157 result = (REQ_SCR_ULINE);
159 result = (REQ_SCR_DLINE);
160 else if (c == 'b' || c == KEY_NPAGE)
161 result = (REQ_SCR_UPAGE);
162 else if (c == 'f' || c == KEY_PPAGE)
163 result = (REQ_SCR_DPAGE);
164 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
165 result = (REQ_LEFT_ITEM);
166 else if (c == 'n' || c == KEY_DOWN)
167 result = (REQ_NEXT_ITEM);
168 else if (c == 'p' || c == KEY_UP)
169 result = (REQ_PREV_ITEM);
170 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
171 result = (REQ_RIGHT_ITEM);
173 result = (REQ_TOGGLE_ITEM);
185 return wGetchar(menu_win(m));
189 menu_offset(MenuNo number)
193 if (okMenuNo(number)) {
194 int spc_desc, spc_rows, spc_cols;
196 #ifdef NCURSES_VERSION
197 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
202 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
203 result = (number - (eBanner + 1)) * (mpBanner->itemlen + spc_rows);
209 menu_create(ITEM ** items, int count, int ncols, MenuNo number)
214 int y = okMenuNo(number) ? MENU_Y : 0;
215 int x = menu_offset(number);
216 int margin = (y == MENU_Y) ? 1 : 0;
217 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
218 int maxrow = (count + 1) / ncols;
220 if ((maxrow + y) >= (LINES - 4))
221 maxrow = LINES - 4 - y;
223 result = new_menu(items);
226 set_menu_fore(result, COLOR_PAIR(1));
227 set_menu_back(result, COLOR_PAIR(2));
230 set_menu_format(result, maxrow, maxcol);
231 scale_menu(result, &mrows, &mcols);
233 if (mcols + (2 * margin + x) >= COLS)
234 mcols = COLS - (2 * margin + x);
237 if (number == eTrace)
238 menu_opts_off(result, O_ONEVALUE);
240 menu_opts_on(result, O_ONEVALUE);
243 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
244 set_menu_win(result, menuwin);
245 keypad(menuwin, TRUE);
249 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
257 menu_destroy(MENU * m)
267 count = item_count(m);
273 Trace(("freeing item %d:%d", ip - menu_items(m), count));
281 /* force the given menu to appear */
283 menu_display(MENU * m)
285 touchwin(menu_win(m));
286 wrefresh(menu_win(m));
289 /*****************************************************************************/
292 build_file_menu(MenuNo number)
294 static CONST_MENUS char *labels[] =
299 static ITEM *items[SIZEOF(labels)];
302 CONST_MENUS char **ap;
304 for (ap = labels; *ap; ap++)
305 *ip++ = new_item(*ap, "");
308 mpFile = menu_create(items, SIZEOF(labels) - 1, 1, number);
312 perform_file_menu(int cmd)
314 return menu_driver(mpFile, cmd);
317 /*****************************************************************************/
320 build_select_menu(MenuNo number, char *filename)
322 static CONST_MENUS char *labels[] =
335 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
336 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
342 CONST_MENUS char **ap = 0;
347 if (stat(filename, &sb) == 0
348 && (sb.st_mode & S_IFMT) == S_IFREG
349 && sb.st_size != 0) {
350 unsigned size = sb.st_size;
352 char *blob = malloc(size + 1);
353 CONST_MENUS char **list =
354 (CONST_MENUS char **) calloc(sizeof(*list), size + 1);
356 items = (ITEM **) calloc(sizeof(ITEM *), size + 1);
357 if (blob != 0 && list != 0) {
358 FILE *fp = fopen(filename, "r");
360 if (fread(blob, sizeof(char), size, fp) == size) {
362 for (j = k = 0; j < size; ++j) {
364 list[k++] = blob + j;
367 if (blob[j] == '\n') {
369 if (k > 0 && *list[k - 1] == '\0')
372 } else if (blob[j] == '\t') {
373 blob[j] = ' '; /* menu items are printable */
386 count = SIZEOF(labels) - 1;
387 items = (ITEM **) calloc(count + 1, sizeof(*items));
393 *ip++ = new_item(*ap++, "");
396 mpSelect = menu_create(items, (int) count, 1, number);
400 perform_select_menu(int cmd)
402 return menu_driver(mpSelect, cmd);
405 /*****************************************************************************/
408 #define T_TBL(name) { #name, name }
414 T_TBL(TRACE_DISABLE),
419 T_TBL(TRACE_CHARPUT),
420 T_TBL(TRACE_ORDINARY),
422 T_TBL(TRACE_VIRTPUT),
427 T_TBL(TRACE_DATABASE),
429 T_TBL(TRACE_MAXIMUM),
436 build_trace_menu(MenuNo number)
438 static ITEM *items[SIZEOF(t_tbl)];
443 for (n = 0; t_tbl[n].name != 0; n++)
444 *ip++ = new_item(t_tbl[n].name, "");
447 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
451 tracetrace(unsigned tlevel)
458 for (n = 0; t_tbl[n].name != 0; n++)
459 need += strlen(t_tbl[n].name) + 2;
460 buf = (char *) malloc(need);
462 sprintf(buf, "0x%02x = {", tlevel);
464 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
466 for (n = 1; t_tbl[n].name != 0; n++)
467 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
468 strcat(buf, t_tbl[n].name);
472 if (buf[strlen(buf) - 2] == ',')
473 buf[strlen(buf) - 2] = '\0';
474 return (strcat(buf, "}"));
477 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
481 update_trace_menu(MENU * m)
485 bool changed = FALSE;
487 items = menu_items(m);
491 for (p = items + 1; *p != 0; p++)
492 if (item_value(*p)) {
493 set_item_value(*p, FALSE);
502 perform_trace_menu(int cmd)
503 /* interactively set the trace level */
509 for (ip = menu_items(mpTrace); *ip; ip++) {
510 unsigned mask = t_tbl[item_index(*ip)].mask;
512 set_item_value(*ip, _nc_tracing == 0);
513 else if ((mask & _nc_tracing) == mask)
514 set_item_value(*ip, TRUE);
517 result = menu_driver(mpTrace, cmd);
519 if (result == E_OK) {
520 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
522 for (ip = menu_items(mpTrace); *ip; ip++) {
524 newtrace |= t_tbl[item_index(*ip)].mask;
527 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
529 (void) mvprintw(LINES - 2, 0,
530 "Trace level is %s\n", tracetrace(_nc_tracing));
538 /*****************************************************************************/
543 return item_index(current_item(mpBanner)) - (eBanner + 1);
551 switch (menu_number()) {
571 build_menus(char *filename)
573 static CONST_MENUS char *labels[] =
582 static ITEM *items[SIZEOF(labels)];
585 CONST_MENUS char **ap;
587 for (ap = labels; *ap; ap++)
588 *ip++ = new_item(*ap, "");
591 mpBanner = menu_create(items, SIZEOF(labels) - 1, SIZEOF(labels) - 1, eBanner);
592 set_menu_mark(mpBanner, ">");
594 build_file_menu(eFile);
595 build_select_menu(eSelect, filename);
597 build_trace_menu(eTrace);
602 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
604 WINDOW *top_win = menu_win(menu);
605 WINDOW *sub_win = menu_sub(menu);
610 getbegyx(top_win, y0, x0);
614 getbegyx(sub_win, y1, x1);
618 if ((result = mvwin(top_win, y0, x0)) != ERR) {
619 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
623 mvwin(sub_win, y1, x1);
625 if (menu == current) {
627 wnoutrefresh(top_win);
634 * Move the menus around on the screen, to test mvwin().
637 move_menus(MENU * current, int by_y, int by_x)
639 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
641 wnoutrefresh(stdscr);
642 move_menu(mpFile, current, by_y, by_x);
643 move_menu(mpSelect, current, by_y, by_x);
645 move_menu(mpTrace, current, by_y, by_x);
652 show_status(int ch, MENU * menu)
655 printw("key %s, menu %d, mark %s, match %s",
668 MENU *last_menu = mpFile;
669 int code = E_UNKNOWN_COMMAND;
673 #ifdef NCURSES_MOUSE_VERSION
674 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
677 menu_display(last_menu);
682 show_status(ch, last_menu);
684 ch = menu_getc(mpBanner);
687 * Provide for moving the menu around in the screen using shifted
692 move_menus(last_menu, 1, 0);
695 move_menus(last_menu, -1, 0);
698 move_menus(last_menu, 0, -1);
701 move_menus(last_menu, 0, 1);
704 cmd = menu_virtualize(ch);
708 * The banner menu acts solely to select one of the other menus.
709 * Move between its items, wrapping at the left/right limits.
713 code = menu_driver(mpBanner, cmd);
714 if (code == E_REQUEST_DENIED) {
715 if (menu_number() > 0)
716 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
718 code = menu_driver(mpBanner, REQ_LAST_ITEM);
722 switch (menu_number()) {
724 code = perform_file_menu(cmd);
727 code = perform_select_menu(cmd);
731 code = perform_trace_menu(cmd);
736 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
737 code = menu_driver(mpBanner, cmd);
744 this_menu = current_menu();
745 if (this_menu != last_menu) {
748 box(menu_win(this_menu), 0, 0);
751 /* force the current menu to appear */
752 menu_display(this_menu);
754 last_menu = this_menu;
757 wrefresh(menu_win(last_menu));
758 if (code == E_UNKNOWN_COMMAND
759 || code == E_NOT_POSTED) {
760 if (menu_number() == eFile)
764 if (code == E_REQUEST_DENIED)
769 #ifdef NCURSES_MOUSE_VERSION
770 mousemask(0, (mmask_t *) 0);
777 menu_destroy(mpFile);
778 menu_destroy(mpSelect);
780 menu_destroy(mpTrace);
782 menu_destroy(mpBanner);
787 rip_footer(WINDOW *win, int cols)
789 wbkgd(win, A_REVERSE);
792 wprintw(win, "footer: %d columns", cols);
798 rip_header(WINDOW *win, int cols)
800 wbkgd(win, A_REVERSE);
803 wprintw(win, "header: %d columns", cols);
807 #endif /* HAVE_RIPOFFLINE */
812 static const char *const tbl[] =
814 "Usage: demo_menus [options]"
818 ," -f rip-off footer line (can repeat)"
819 ," -h rip-off header line (can repeat)"
822 ," -t mask specify default trace-level (may toggle with ^T)"
826 for (n = 0; n < SIZEOF(tbl); n++)
827 fprintf(stderr, "%s\n", tbl[n]);
828 ExitProgram(EXIT_FAILURE);
832 main(int argc, char *argv[])
836 setlocale(LC_ALL, "");
838 while ((c = getopt(argc, argv, "a:de:fhmp:s:t:")) != -1) {
842 ripoffline(-1, rip_footer);
845 ripoffline(1, rip_header);
847 #endif /* HAVE_RIPOFFLINE */
850 trace(strtoul(optarg, 0, 0));
865 init_pair(1, COLOR_RED, COLOR_BLACK);
866 init_pair(2, COLOR_BLUE, COLOR_WHITE);
868 build_menus(argc > 1 ? argv[1] : 0);
873 ExitProgram(EXIT_SUCCESS);
879 printf("This program requires the curses menu library\n");
880 ExitProgram(EXIT_FAILURE);