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.28 2008/08/23 20:31: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;
113 #define strdup my_strdup
117 char *p = typeMalloc(char, strlen(s) + 1);
122 #endif /* not HAVE_STRDUP */
124 /* Common function to allow ^T to toggle trace-mode in the middle of a test
125 * so that trace-files can be made smaller.
128 wGetchar(WINDOW *win)
132 while ((c = wgetch(win)) == CTRL('T')) {
134 save_trace = _nc_tracing;
135 Trace(("TOGGLE-TRACING OFF"));
138 _nc_tracing = save_trace;
142 Trace(("TOGGLE-TRACING ON"));
149 #define Getchar() wGetchar(stdscr)
152 menu_virtualize(int c)
156 if (c == '\n' || c == KEY_EXIT)
157 result = (MAX_COMMAND + 1);
159 result = (REQ_SCR_ULINE);
161 result = (REQ_SCR_DLINE);
162 else if (c == 'b' || c == KEY_NPAGE)
163 result = (REQ_SCR_UPAGE);
164 else if (c == 'f' || c == KEY_PPAGE)
165 result = (REQ_SCR_DPAGE);
166 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
167 result = (REQ_LEFT_ITEM);
168 else if (c == 'n' || c == KEY_DOWN)
169 result = (REQ_NEXT_ITEM);
170 else if (c == 'p' || c == KEY_UP)
171 result = (REQ_PREV_ITEM);
172 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
173 result = (REQ_RIGHT_ITEM);
175 result = (REQ_TOGGLE_ITEM);
187 return wGetchar(menu_win(m));
191 menu_offset(MenuNo number)
195 if (okMenuNo(number)) {
196 int spc_desc, spc_rows, spc_cols;
198 #ifdef NCURSES_VERSION
199 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
204 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
205 result = (number - (eBanner + 1)) * (mpBanner->itemlen + spc_rows);
211 menu_create(ITEM ** items, int count, int ncols, MenuNo number)
216 int y = okMenuNo(number) ? MENU_Y : 0;
217 int x = menu_offset(number);
218 int margin = (y == MENU_Y) ? 1 : 0;
219 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
220 int maxrow = (count + 1) / ncols;
222 if ((maxrow + y) >= (LINES - 4))
223 maxrow = LINES - 4 - y;
225 result = new_menu(items);
228 set_menu_fore(result, COLOR_PAIR(1));
229 set_menu_back(result, COLOR_PAIR(2));
232 set_menu_format(result, maxrow, maxcol);
233 scale_menu(result, &mrows, &mcols);
235 if (mcols + (2 * margin + x) >= COLS)
236 mcols = COLS - (2 * margin + x);
239 if (number == eTrace)
240 menu_opts_off(result, O_ONEVALUE);
242 menu_opts_on(result, O_ONEVALUE);
245 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
246 set_menu_win(result, menuwin);
247 keypad(menuwin, TRUE);
251 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
259 menu_destroy(MENU * m)
263 Trace(("menu_destroy %p", m));
265 ITEM **items = menu_items(m);
266 const char *blob = 0;
268 count = item_count(m);
269 Trace(("menu_destroy %p count %d", m, count));
270 if ((count > 0) && (m == mpSelect)) {
271 blob = item_name(*items);
277 /* free the extra data allocated in build_select_menu() */
278 if ((count > 0) && (m == mpSelect)) {
279 if (blob && loaded_file) {
280 Trace(("freeing blob %p", blob));
286 if ((count > 0) && (m == mpTrace)) {
295 /* force the given menu to appear */
297 menu_display(MENU * m)
299 touchwin(menu_win(m));
300 wrefresh(menu_win(m));
303 /*****************************************************************************/
306 build_file_menu(MenuNo number)
308 static CONST_MENUS char *labels[] =
313 static ITEM *items[SIZEOF(labels)];
316 CONST_MENUS char **ap;
318 for (ap = labels; *ap; ap++)
319 *ip++ = new_item(*ap, "");
322 mpFile = menu_create(items, SIZEOF(labels) - 1, 1, number);
326 perform_file_menu(int cmd)
328 return menu_driver(mpFile, cmd);
331 /*****************************************************************************/
334 build_select_menu(MenuNo number, char *filename)
336 static CONST_MENUS char *labels[] =
349 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
350 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
356 CONST_MENUS char **ap = 0;
357 CONST_MENUS char **myList = 0;
362 if (stat(filename, &sb) == 0
363 && (sb.st_mode & S_IFMT) == S_IFREG
364 && sb.st_size != 0) {
365 size_t size = (size_t) sb.st_size;
367 char *blob = typeMalloc(char, size + 1);
368 CONST_MENUS char **list = typeCalloc(CONST_MENUS char *, size + 1);
370 items = typeCalloc(ITEM *, size + 1);
371 Trace(("build_select_menu blob=%p, items=%p", blob, items));
372 if (blob != 0 && list != 0) {
373 FILE *fp = fopen(filename, "r");
375 if (fread(blob, sizeof(char), size, fp) == size) {
377 for (j = k = 0; j < size; ++j) {
379 list[k++] = blob + j;
382 if (blob[j] == '\n') {
384 if (k > 0 && *list[k - 1] == '\0')
387 } else if (blob[j] == '\t') {
388 blob[j] = ' '; /* menu items are printable */
402 count = SIZEOF(labels) - 1;
403 items = typeCalloc(ITEM *, count + 1);
409 *ip++ = new_item(*ap++, "");
412 mpSelect = menu_create(items, (int) count, 1, number);
418 perform_select_menu(int cmd)
420 return menu_driver(mpSelect, cmd);
423 /*****************************************************************************/
426 #define T_TBL(name) { #name, name }
432 T_TBL(TRACE_DISABLE),
437 T_TBL(TRACE_CHARPUT),
438 T_TBL(TRACE_ORDINARY),
440 T_TBL(TRACE_VIRTPUT),
445 T_TBL(TRACE_DATABASE),
447 T_TBL(TRACE_MAXIMUM),
454 build_trace_menu(MenuNo number)
456 static ITEM *items[SIZEOF(t_tbl)];
461 for (n = 0; t_tbl[n].name != 0; n++)
462 *ip++ = new_item(t_tbl[n].name, "");
465 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
469 tracetrace(unsigned tlevel)
476 for (n = 0; t_tbl[n].name != 0; n++)
477 need += strlen(t_tbl[n].name) + 2;
478 buf = typeMalloc(char, need);
480 sprintf(buf, "0x%02x = {", tlevel);
482 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
484 for (n = 1; t_tbl[n].name != 0; n++)
485 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
486 strcat(buf, t_tbl[n].name);
490 if (buf[strlen(buf) - 2] == ',')
491 buf[strlen(buf) - 2] = '\0';
492 return (strcat(buf, "}"));
495 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
499 update_trace_menu(MENU * m)
503 bool changed = FALSE;
505 items = menu_items(m);
509 for (p = items + 1; *p != 0; p++)
510 if (item_value(*p)) {
511 set_item_value(*p, FALSE);
520 perform_trace_menu(int cmd)
521 /* interactively set the trace level */
527 for (ip = menu_items(mpTrace); *ip; ip++) {
528 unsigned mask = t_tbl[item_index(*ip)].mask;
530 set_item_value(*ip, _nc_tracing == 0);
531 else if ((mask & _nc_tracing) == mask)
532 set_item_value(*ip, TRUE);
535 result = menu_driver(mpTrace, cmd);
537 if (result == E_OK) {
538 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
540 for (ip = menu_items(mpTrace); *ip; ip++) {
542 newtrace |= t_tbl[item_index(*ip)].mask;
545 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
547 (void) mvprintw(LINES - 2, 0,
548 "Trace level is %s\n", tracetrace(_nc_tracing));
556 /*****************************************************************************/
561 return item_index(current_item(mpBanner)) - (eBanner + 1);
569 switch (menu_number()) {
589 build_menus(char *filename)
591 static CONST_MENUS char *labels[] =
600 static ITEM *items[SIZEOF(labels)];
603 CONST_MENUS char **ap;
605 for (ap = labels; *ap; ap++)
606 *ip++ = new_item(*ap, "");
609 mpBanner = menu_create(items, SIZEOF(labels) - 1, SIZEOF(labels) - 1, eBanner);
610 set_menu_mark(mpBanner, ">");
612 build_file_menu(eFile);
613 build_select_menu(eSelect, filename);
615 build_trace_menu(eTrace);
620 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
622 WINDOW *top_win = menu_win(menu);
623 WINDOW *sub_win = menu_sub(menu);
628 getbegyx(top_win, y0, x0);
632 getbegyx(sub_win, y1, x1);
636 if ((result = mvwin(top_win, y0, x0)) != ERR) {
637 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
641 mvwin(sub_win, y1, x1);
643 if (menu == current) {
645 wnoutrefresh(top_win);
652 * Move the menus around on the screen, to test mvwin().
655 move_menus(MENU * current, int by_y, int by_x)
657 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
659 wnoutrefresh(stdscr);
660 move_menu(mpFile, current, by_y, by_x);
661 move_menu(mpSelect, current, by_y, by_x);
663 move_menu(mpTrace, current, by_y, by_x);
670 show_status(int ch, MENU * menu)
673 printw("key %s, menu %d, mark %s, match %s",
686 MENU *last_menu = mpFile;
687 int code = E_UNKNOWN_COMMAND;
691 #ifdef NCURSES_MOUSE_VERSION
692 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
695 menu_display(last_menu);
700 show_status(ch, last_menu);
702 ch = menu_getc(mpBanner);
705 * Provide for moving the menu around in the screen using shifted
710 move_menus(last_menu, 1, 0);
713 move_menus(last_menu, -1, 0);
716 move_menus(last_menu, 0, -1);
719 move_menus(last_menu, 0, 1);
722 cmd = menu_virtualize(ch);
726 * The banner menu acts solely to select one of the other menus.
727 * Move between its items, wrapping at the left/right limits.
731 code = menu_driver(mpBanner, cmd);
732 if (code == E_REQUEST_DENIED) {
733 if (menu_number() > 0)
734 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
736 code = menu_driver(mpBanner, REQ_LAST_ITEM);
740 switch (menu_number()) {
742 code = perform_file_menu(cmd);
745 code = perform_select_menu(cmd);
749 code = perform_trace_menu(cmd);
754 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
755 code = menu_driver(mpBanner, cmd);
762 this_menu = current_menu();
763 if (this_menu != last_menu) {
766 box(menu_win(this_menu), 0, 0);
769 /* force the current menu to appear */
770 menu_display(this_menu);
772 last_menu = this_menu;
775 wrefresh(menu_win(last_menu));
776 if (code == E_UNKNOWN_COMMAND
777 || code == E_NOT_POSTED) {
778 if (menu_number() == eFile)
782 if (code == E_REQUEST_DENIED)
787 #ifdef NCURSES_MOUSE_VERSION
788 mousemask(0, (mmask_t *) 0);
795 menu_destroy(mpFile);
796 menu_destroy(mpSelect);
798 menu_destroy(mpTrace);
800 menu_destroy(mpBanner);
805 rip_footer(WINDOW *win, int cols)
807 wbkgd(win, A_REVERSE);
810 wprintw(win, "footer: %d columns", cols);
816 rip_header(WINDOW *win, int cols)
818 wbkgd(win, A_REVERSE);
821 wprintw(win, "header: %d columns", cols);
825 #endif /* HAVE_RIPOFFLINE */
830 static const char *const tbl[] =
832 "Usage: demo_menus [options]"
836 ," -f rip-off footer line (can repeat)"
837 ," -h rip-off header line (can repeat)"
840 ," -t mask specify default trace-level (may toggle with ^T)"
844 for (n = 0; n < SIZEOF(tbl); n++)
845 fprintf(stderr, "%s\n", tbl[n]);
846 ExitProgram(EXIT_FAILURE);
850 main(int argc, char *argv[])
854 setlocale(LC_ALL, "");
856 while ((c = getopt(argc, argv, "a:de:fhmp:s:t:")) != -1) {
860 ripoffline(-1, rip_footer);
863 ripoffline(1, rip_header);
865 #endif /* HAVE_RIPOFFLINE */
868 trace(strtoul(optarg, 0, 0));
883 init_pair(1, COLOR_RED, COLOR_BLACK);
884 init_pair(2, COLOR_BLUE, COLOR_WHITE);
886 build_menus(argc > 1 ? argv[1] : 0);
891 ExitProgram(EXIT_SUCCESS);
897 printf("This program requires the curses menu library\n");
898 ExitProgram(EXIT_FAILURE);