2 * $Id: demo_menus.c,v 1.13 2005/10/01 15:54:31 tom Exp $
4 * Demonstrate a variety of functions from the menu library.
5 * Thomas Dickey - 2005/4/9
25 menu_request_by_name -
48 #include <test.priv.h>
54 #include <sys/types.h>
57 #ifdef NCURSES_VERSION
59 static unsigned save_trace = TRACE_ORDINARY | TRACE_CALLS;
60 extern unsigned _nc_tracing;
78 static MENU *mpBanner;
80 static MENU *mpSelect;
83 #define strdup my_strdup
87 char *p = (char *) malloc(strlen(s) + 1);
92 #endif /* not HAVE_STRDUP */
94 /* Common function to allow ^T to toggle trace-mode in the middle of a test
95 * so that trace-files can be made smaller.
102 while ((c = wgetch(win)) == CTRL('T')) {
104 save_trace = _nc_tracing;
105 _tracef("TOGGLE-TRACING OFF");
108 _nc_tracing = save_trace;
112 _tracef("TOGGLE-TRACING ON");
119 #define Getchar() wGetchar(stdscr)
122 menu_virtualize(int c)
126 if (c == '\n' || c == KEY_EXIT)
127 result = (MAX_COMMAND + 1);
129 result = (REQ_SCR_ULINE);
131 result = (REQ_SCR_DLINE);
132 else if (c == 'b' || c == KEY_NPAGE)
133 result = (REQ_SCR_UPAGE);
134 else if (c == 'f' || c == KEY_PPAGE)
135 result = (REQ_SCR_DPAGE);
136 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
137 result = (REQ_LEFT_ITEM);
138 else if (c == 'n' || c == KEY_DOWN)
139 result = (REQ_NEXT_ITEM);
140 else if (c == 'p' || c == KEY_UP)
141 result = (REQ_PREV_ITEM);
142 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
143 result = (REQ_RIGHT_ITEM);
145 result = (REQ_TOGGLE_ITEM);
157 return wGetchar(menu_win(m));
161 menu_offset(MenuNo number)
165 if ((int) number >= 0) {
166 int spc_desc, spc_rows, spc_cols;
168 #ifdef NCURSES_VERSION
169 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
174 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
175 result = number * (mpBanner->itemlen + spc_rows);
181 menu_create(ITEM ** items, int count, int ncols, MenuNo number)
186 int y = ((int) number >= 0) ? MENU_Y : 0;
187 int x = menu_offset(number);
188 int margin = (y == MENU_Y) ? 1 : 0;
189 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
190 int maxrow = (count + 1) / ncols;
192 if ((maxrow + y) >= (LINES - 4))
193 maxrow = LINES - 4 - y;
195 result = new_menu(items);
198 set_menu_fore(result, COLOR_PAIR(1));
199 set_menu_back(result, COLOR_PAIR(2));
202 set_menu_format(result, maxrow, maxcol);
203 scale_menu(result, &mrows, &mcols);
205 if (mcols + (2 * margin + x) >= COLS)
206 mcols = COLS - (2 * margin + x);
209 if (number == eTrace)
210 menu_opts_off(result, O_ONEVALUE);
212 menu_opts_on(result, O_ONEVALUE);
215 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
216 set_menu_win(result, menuwin);
217 keypad(menuwin, TRUE);
221 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
229 menu_destroy(MENU * m)
238 count = item_count(m);
244 _tracef("freeing item %d:%d", ip - menu_items(m), count);
252 /* force the given menu to appear */
254 menu_display(MENU * m)
256 touchwin(menu_win(m));
257 wrefresh(menu_win(m));
260 /*****************************************************************************/
263 build_file_menu(MenuNo number)
265 static const char *labels[] =
270 static ITEM *items[SIZEOF(labels)];
275 for (ap = labels; *ap; ap++)
276 *ip++ = new_item(*ap, "");
279 mpFile = menu_create(items, SIZEOF(labels) - 1, 1, number);
283 perform_file_menu(int cmd)
285 return menu_driver(mpFile, cmd);
288 /*****************************************************************************/
291 build_select_menu(MenuNo number, char *filename)
293 static const char *labels[] =
306 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
307 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
318 if (stat(filename, &sb) == 0
319 && (sb.st_mode & S_IFMT) == S_IFREG
320 && sb.st_size != 0) {
321 unsigned size = sb.st_size;
323 char *blob = malloc(size + 1);
324 const char **list = (const char **) calloc(sizeof(*list), size + 1);
326 items = (ITEM **) calloc(sizeof(ITEM *), size + 1);
327 if (blob != 0 && list != 0) {
328 FILE *fp = fopen(filename, "r");
330 if (fread(blob, sizeof(char), size, fp) == size) {
332 for (j = k = 0; j < size; ++j) {
334 list[k++] = blob + j;
337 if (blob[j] == '\n') {
339 if (k > 0 && *list[k - 1] == '\0')
342 } else if (blob[j] == '\t') {
343 blob[j] = ' '; /* menu items are printable */
356 count = SIZEOF(labels) - 1;
357 items = (ITEM **) calloc(count + 1, sizeof(*items));
363 *ip++ = new_item(*ap++, "");
366 mpSelect = menu_create(items, (int) count, 1, number);
370 perform_select_menu(int cmd)
372 return menu_driver(mpSelect, cmd);
375 /*****************************************************************************/
378 #define T_TBL(name) { #name, name }
384 T_TBL(TRACE_DISABLE),
389 T_TBL(TRACE_CHARPUT),
390 T_TBL(TRACE_ORDINARY),
392 T_TBL(TRACE_VIRTPUT),
397 T_TBL(TRACE_DATABASE),
399 T_TBL(TRACE_MAXIMUM),
406 build_trace_menu(MenuNo number)
408 static ITEM *items[SIZEOF(t_tbl)];
413 for (n = 0; t_tbl[n].name != 0; n++)
414 *ip++ = new_item(t_tbl[n].name, "");
417 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
421 tracetrace(unsigned tlevel)
428 for (n = 0; t_tbl[n].name != 0; n++)
429 need += strlen(t_tbl[n].name) + 2;
430 buf = (char *) malloc(need);
432 sprintf(buf, "0x%02x = {", tlevel);
434 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
436 for (n = 1; t_tbl[n].name != 0; n++)
437 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
438 strcat(buf, t_tbl[n].name);
442 if (buf[strlen(buf) - 2] == ',')
443 buf[strlen(buf) - 2] = '\0';
444 return (strcat(buf, "}"));
447 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
451 update_trace_menu(MENU * m)
455 bool changed = FALSE;
457 items = menu_items(m);
461 for (p = items + 1; *p != 0; p++)
462 if (item_value(*p)) {
463 set_item_value(*p, FALSE);
472 perform_trace_menu(int cmd)
473 /* interactively set the trace level */
479 for (ip = menu_items(mpTrace); *ip; ip++) {
480 unsigned mask = t_tbl[item_index(*ip)].mask;
482 set_item_value(*ip, _nc_tracing == 0);
483 else if ((mask & _nc_tracing) == mask)
484 set_item_value(*ip, TRUE);
487 result = menu_driver(mpTrace, cmd);
489 if (result == E_OK) {
490 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
492 for (ip = menu_items(mpTrace); *ip; ip++) {
494 newtrace |= t_tbl[item_index(*ip)].mask;
497 _tracef("trace level interactively set to %s", tracetrace(_nc_tracing));
499 (void) mvprintw(LINES - 2, 0,
500 "Trace level is %s\n", tracetrace(_nc_tracing));
508 /*****************************************************************************/
513 return item_index(current_item(mpBanner));
521 switch (menu_number()) {
541 build_menus(char *filename)
543 static const char *labels[] =
552 static ITEM *items[SIZEOF(labels)];
557 for (ap = labels; *ap; ap++)
558 *ip++ = new_item(*ap, "");
561 mpBanner = menu_create(items, SIZEOF(labels) - 1, SIZEOF(labels) - 1, eUnknown);
562 set_menu_mark(mpBanner, ">");
564 build_file_menu(eFile);
565 build_select_menu(eSelect, filename);
567 build_trace_menu(eTrace);
575 MENU *last_menu = mpFile;
576 int code = E_UNKNOWN_COMMAND, cmd, ch;
578 #ifdef NCURSES_MOUSE_VERSION
579 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
582 menu_display(last_menu);
585 ch = menu_getc(mpBanner);
586 cmd = menu_virtualize(ch);
590 * The banner menu acts solely to select one of the other menus.
591 * Move between its items, wrapping at the left/right limits.
595 code = menu_driver(mpBanner, cmd);
596 if (code == E_REQUEST_DENIED) {
597 if (menu_number() > 0)
598 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
600 code = menu_driver(mpBanner, REQ_LAST_ITEM);
604 switch (menu_number()) {
606 code = perform_file_menu(cmd);
609 code = perform_select_menu(cmd);
613 code = perform_trace_menu(cmd);
618 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
619 code = menu_driver(mpBanner, cmd);
626 this_menu = current_menu();
627 if (this_menu != last_menu) {
630 box(menu_win(this_menu), 0, 0);
633 /* force the current menu to appear */
634 menu_display(this_menu);
636 last_menu = this_menu;
639 wrefresh(menu_win(last_menu));
640 if (code == E_UNKNOWN_COMMAND
641 || code == E_NOT_POSTED) {
642 if (menu_number() == eFile)
646 if (code == E_REQUEST_DENIED)
651 #ifdef NCURSES_MOUSE_VERSION
652 mousemask(0, (mmask_t *) 0);
659 menu_destroy(mpFile);
660 menu_destroy(mpSelect);
662 menu_destroy(mpTrace);
664 menu_destroy(mpBanner);
668 main(int argc, char *argv[])
670 setlocale(LC_ALL, "");
679 init_pair(1, COLOR_RED, COLOR_BLACK);
680 init_pair(2, COLOR_BLUE, COLOR_WHITE);
682 build_menus(argc > 1 ? argv[1] : 0);
693 printf("This program requires the curses menu library\n");
694 ExitProgram(EXIT_FAILURE);