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.36 2012/11/03 19:27:01 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));
274 if ((count > 0) && (m == mpTrace)) {
283 /* force the given menu to appear */
285 menu_display(MENU * m)
287 touchwin(menu_win(m));
288 wrefresh(menu_win(m));
291 /*****************************************************************************/
294 build_file_menu(MenuNo number)
296 static CONST_MENUS char *labels[] =
301 static ITEM *items[SIZEOF(labels)];
304 CONST_MENUS char **ap;
306 for (ap = labels; *ap; ap++)
307 *ip++ = new_item(*ap, "");
310 mpFile = menu_create(items, SIZEOF(labels) - 1, 1, number);
314 perform_file_menu(int cmd)
316 return menu_driver(mpFile, cmd);
319 /*****************************************************************************/
322 build_select_menu(MenuNo number, char *filename)
324 static CONST_MENUS char *labels[] =
337 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
338 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
344 CONST_MENUS char **ap = 0;
345 CONST_MENUS char **myList = 0;
350 if (stat(filename, &sb) == 0
351 && (sb.st_mode & S_IFMT) == S_IFREG
352 && sb.st_size != 0) {
353 size_t size = (size_t) sb.st_size;
355 char *blob = typeMalloc(char, size + 1);
356 CONST_MENUS char **list = typeCalloc(CONST_MENUS char *, size + 1);
358 items = typeCalloc(ITEM *, size + 1);
359 Trace(("build_select_menu blob=%p, items=%p",
362 if (blob != 0 && list != 0) {
363 FILE *fp = fopen(filename, "r");
365 if (fread(blob, sizeof(char), size, fp) == size) {
367 for (j = k = 0; j < size; ++j) {
369 list[k++] = blob + j;
372 if (blob[j] == '\n') {
374 if (k > 0 && *list[k - 1] == '\0')
377 } else if (blob[j] == '\t') {
378 blob[j] = ' '; /* menu items are printable */
392 count = SIZEOF(labels) - 1;
393 items = typeCalloc(ITEM *, count + 1);
399 *ip++ = new_item(*ap++, "");
402 mpSelect = menu_create(items, (int) count, 1, number);
408 perform_select_menu(int cmd)
410 return menu_driver(mpSelect, cmd);
413 /*****************************************************************************/
416 #define T_TBL(name) { #name, name }
422 T_TBL(TRACE_DISABLE),
427 T_TBL(TRACE_CHARPUT),
428 T_TBL(TRACE_ORDINARY),
430 T_TBL(TRACE_VIRTPUT),
435 T_TBL(TRACE_DATABASE),
437 T_TBL(TRACE_MAXIMUM),
444 build_trace_menu(MenuNo number)
446 static ITEM *items[SIZEOF(t_tbl)];
451 for (n = 0; t_tbl[n].name != 0; n++)
452 *ip++ = new_item(t_tbl[n].name, "");
455 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
459 tracetrace(unsigned tlevel)
466 for (n = 0; t_tbl[n].name != 0; n++)
467 need += strlen(t_tbl[n].name) + 2;
468 buf = typeMalloc(char, need);
470 sprintf(buf, "0x%02x = {", tlevel);
472 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
474 for (n = 1; t_tbl[n].name != 0; n++)
475 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
476 strcat(buf, t_tbl[n].name);
480 if (buf[strlen(buf) - 2] == ',')
481 buf[strlen(buf) - 2] = '\0';
482 return (strcat(buf, "}"));
485 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
489 update_trace_menu(MENU * m)
493 bool changed = FALSE;
495 items = menu_items(m);
499 for (p = items + 1; *p != 0; p++)
500 if (item_value(*p)) {
501 set_item_value(*p, FALSE);
510 perform_trace_menu(int cmd)
511 /* interactively set the trace level */
517 for (ip = menu_items(mpTrace); *ip; ip++) {
518 unsigned mask = t_tbl[item_index(*ip)].mask;
520 set_item_value(*ip, _nc_tracing == 0);
521 else if ((mask & _nc_tracing) == mask)
522 set_item_value(*ip, TRUE);
525 result = menu_driver(mpTrace, cmd);
527 if (result == E_OK) {
528 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
530 for (ip = menu_items(mpTrace); *ip; ip++) {
532 newtrace |= t_tbl[item_index(*ip)].mask;
535 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
537 MvPrintw(LINES - 2, 0,
538 "Trace level is %s\n", tracetrace(_nc_tracing));
546 /*****************************************************************************/
551 return item_index(current_item(mpBanner)) - (eBanner + 1);
559 switch (menu_number()) {
579 build_menus(char *filename)
581 static CONST_MENUS char *labels[] =
590 static ITEM *items[SIZEOF(labels)];
593 CONST_MENUS char **ap;
595 for (ap = labels; *ap; ap++)
596 *ip++ = new_item(*ap, "");
599 mpBanner = menu_create(items, SIZEOF(labels) - 1, SIZEOF(labels) - 1, eBanner);
600 set_menu_mark(mpBanner, ">");
602 build_file_menu(eFile);
603 build_select_menu(eSelect, filename);
605 build_trace_menu(eTrace);
610 move_menu(MENU * menu, MENU * current, int by_y, int by_x)
612 WINDOW *top_win = menu_win(menu);
613 WINDOW *sub_win = menu_sub(menu);
618 getbegyx(top_win, y0, x0);
622 getbegyx(sub_win, y1, x1);
626 if ((result = mvwin(top_win, y0, x0)) != ERR) {
627 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
631 mvwin(sub_win, y1, x1);
633 if (menu == current) {
635 wnoutrefresh(top_win);
642 * Move the menus around on the screen, to test mvwin().
645 move_menus(MENU * current, int by_y, int by_x)
647 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
649 wnoutrefresh(stdscr);
650 move_menu(mpFile, current, by_y, by_x);
651 move_menu(mpSelect, current, by_y, by_x);
653 move_menu(mpTrace, current, by_y, by_x);
660 show_status(int ch, MENU * menu)
663 printw("key %s, menu %d, mark %s, match %s",
676 MENU *last_menu = mpFile;
677 int code = E_UNKNOWN_COMMAND;
681 #ifdef NCURSES_MOUSE_VERSION
682 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
685 menu_display(last_menu);
690 show_status(ch, last_menu);
692 ch = menu_getc(mpBanner);
695 * Provide for moving the menu around in the screen using shifted
700 move_menus(last_menu, 1, 0);
703 move_menus(last_menu, -1, 0);
706 move_menus(last_menu, 0, -1);
709 move_menus(last_menu, 0, 1);
712 cmd = menu_virtualize(ch);
716 * The banner menu acts solely to select one of the other menus.
717 * Move between its items, wrapping at the left/right limits.
721 code = menu_driver(mpBanner, cmd);
722 if (code == E_REQUEST_DENIED) {
723 if (menu_number() > 0)
724 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
726 code = menu_driver(mpBanner, REQ_LAST_ITEM);
730 switch (menu_number()) {
732 code = perform_file_menu(cmd);
735 code = perform_select_menu(cmd);
739 code = perform_trace_menu(cmd);
744 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
745 code = menu_driver(mpBanner, cmd);
752 this_menu = current_menu();
753 if (this_menu != last_menu) {
756 box(menu_win(this_menu), 0, 0);
759 /* force the current menu to appear */
760 menu_display(this_menu);
762 last_menu = this_menu;
765 wrefresh(menu_win(last_menu));
766 if (code == E_UNKNOWN_COMMAND
767 || code == E_NOT_POSTED) {
768 if (menu_number() == eFile)
772 if (code == E_REQUEST_DENIED)
777 #ifdef NCURSES_MOUSE_VERSION
778 mousemask(0, (mmask_t *) 0);
785 menu_destroy(mpFile);
786 menu_destroy(mpSelect);
788 menu_destroy(mpTrace);
790 menu_destroy(mpBanner);
795 rip_footer(WINDOW *win, int cols)
797 wbkgd(win, A_REVERSE);
800 wprintw(win, "footer: %d columns", cols);
806 rip_header(WINDOW *win, int cols)
808 wbkgd(win, A_REVERSE);
811 wprintw(win, "header: %d columns", cols);
815 #endif /* HAVE_RIPOFFLINE */
820 static const char *const tbl[] =
822 "Usage: demo_menus [options] [menu-file]"
826 ," -f rip-off footer line (can repeat)"
827 ," -h rip-off header line (can repeat)"
830 ," -t mask specify default trace-level (may toggle with ^T)"
834 for (n = 0; n < SIZEOF(tbl); n++)
835 fprintf(stderr, "%s\n", tbl[n]);
836 ExitProgram(EXIT_FAILURE);
840 main(int argc, char *argv[])
844 setlocale(LC_ALL, "");
846 while ((c = getopt(argc, argv, "a:de:fhmp:s:t:")) != -1) {
850 ripoffline(-1, rip_footer);
853 ripoffline(1, rip_header);
855 #endif /* HAVE_RIPOFFLINE */
858 trace((unsigned) strtoul(optarg, 0, 0));
873 init_pair(1, COLOR_RED, COLOR_BLACK);
874 init_pair(2, COLOR_BLUE, COLOR_WHITE);
876 build_menus(argc > 1 ? argv[1] : 0);
881 ExitProgram(EXIT_SUCCESS);
887 printf("This program requires the curses menu library\n");
888 ExitProgram(EXIT_FAILURE);