blob: 7b0bf30316e558f6aa6b88559feb6b3e092b1548 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2022,2023 Thomas E. Dickey *
3 * Copyright 2003-2016,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29/*
micky3879b9f5e72025-07-08 18:04:53 -040030 * $Id: demo_menus.c,v 1.80 2023/05/27 20:13:10 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010031 *
32 * Demonstrate a variety of functions from the menu library.
33 * Thomas Dickey - 2005/4/9
34 */
35/*
36item_description -
37item_init -
38item_opts -
39item_opts_off -
40item_opts_on -
41item_term -
42item_visible -
43menu_back -
44menu_fore -
45menu_format -
46menu_grey -
47menu_init -
48menu_opts -
49menu_pad -
50menu_request_by_name -
51menu_request_name -
52menu_term -
53menu_userptr -
54set_current_item -
55set_item_opts -
56set_menu_grey -
57set_menu_items -
58set_menu_opts -
59set_menu_pad -
60set_menu_pattern -
61set_menu_spacing -
62set_menu_userptr -
63set_top_row -
64top_row -
65*/
66
67#include <test.priv.h>
68
69#if USE_LIBMENU
70
71#include <menu.h>
72
73#include <sys/types.h>
74#include <sys/stat.h>
75
76#ifdef NCURSES_VERSION
77#ifdef TRACE
78static unsigned save_trace = TRACE_ORDINARY | TRACE_CALLS;
Steve Kondikae271bc2015-11-15 02:50:53 +010079static MENU *mpTrace;
80#endif
Steve Kondikae271bc2015-11-15 02:50:53 +010081#endif
82
83typedef enum {
84 eBanner = -1
85 ,eFile
86 ,eSelect
87#ifdef TRACE
88 ,eTrace
89#endif
90 ,eMAX
91} MenuNo;
92
93#define okMenuNo(n) (((n) > eBanner) && ((n) < eMAX))
94
95#define MENU_Y 1
96
97typedef struct {
98 NCURSES_CONST char *name;
99 void (*func) (int);
100 unsigned mask;
101} MENU_DATA;
102
103static void call_files(int);
104
105static MENU *mpBanner;
106static MENU *mpFile;
107static MENU *mpSelect;
108
109static WINDOW *status;
110
111static bool loaded_file = FALSE;
112
113static char empty[1];
114
micky3879b9f5e72025-07-08 18:04:53 -0400115#ifdef TRACE
116static GCC_NORETURN void failed(const char *s);
117
118static void
119failed(const char *s)
120{
121 perror(s);
122 endwin();
123 ExitProgram(EXIT_FAILURE);
124}
125#endif
126
Steve Kondikae271bc2015-11-15 02:50:53 +0100127/* Common function to allow ^T to toggle trace-mode in the middle of a test
128 * so that trace-files can be made smaller.
129 */
130static int
131wGetchar(WINDOW *win)
132{
133 int c;
134#ifdef TRACE
135 while ((c = wgetch(win)) == CTRL('T')) {
136 if (_nc_tracing) {
137 save_trace = _nc_tracing;
138 Trace(("TOGGLE-TRACING OFF"));
139 _nc_tracing = 0;
140 } else {
141 _nc_tracing = save_trace;
142 }
micky3879b9f5e72025-07-08 18:04:53 -0400143 curses_trace(_nc_tracing);
Steve Kondikae271bc2015-11-15 02:50:53 +0100144 if (_nc_tracing)
145 Trace(("TOGGLE-TRACING ON"));
146 }
147#else
148 c = wgetch(win);
149#endif
150 return c;
151}
152#define Getchar() wGetchar(stdscr)
153
154static int
155menu_virtualize(int c)
156{
157 int result;
158
159 if (c == '\n' || c == KEY_EXIT)
160 result = (MAX_COMMAND + 1);
161 else if (c == 'u')
162 result = (REQ_SCR_ULINE);
163 else if (c == 'd')
164 result = (REQ_SCR_DLINE);
165 else if (c == 'b' || c == KEY_NPAGE)
166 result = (REQ_SCR_UPAGE);
167 else if (c == 'f' || c == KEY_PPAGE)
168 result = (REQ_SCR_DPAGE);
169 else if (c == 'l' || c == KEY_LEFT || c == KEY_BTAB)
170 result = (REQ_LEFT_ITEM);
171 else if (c == 'n' || c == KEY_DOWN)
172 result = (REQ_NEXT_ITEM);
173 else if (c == 'p' || c == KEY_UP)
174 result = (REQ_PREV_ITEM);
175 else if (c == 'r' || c == KEY_RIGHT || c == '\t')
176 result = (REQ_RIGHT_ITEM);
177 else if (c == ' ')
178 result = (REQ_TOGGLE_ITEM);
179 else {
180 if (c != KEY_MOUSE)
181 beep();
182 result = (c);
183 }
184 return result;
185}
186
187static int
188menu_getc(MENU * m)
189{
190 return wGetchar(menu_win(m));
191}
192
193static int
194menu_offset(MenuNo number)
195{
196 int result = 0;
197
198 if (okMenuNo(number)) {
micky3879b9f5e72025-07-08 18:04:53 -0400199 int spc_rows;
Steve Kondikae271bc2015-11-15 02:50:53 +0100200#ifdef NCURSES_VERSION
micky3879b9f5e72025-07-08 18:04:53 -0400201 int spc_desc, spc_cols;
Steve Kondikae271bc2015-11-15 02:50:53 +0100202 menu_spacing(mpBanner, &spc_desc, &spc_rows, &spc_cols);
203#else
204 spc_rows = 0;
205#endif
206
207 /* FIXME: MENU.itemlen seems the only way to get actual width of items */
208 result = (number - (eBanner + 1)) * (menu_itemwidth(mpBanner) + spc_rows);
209 }
210 return result;
211}
212
213static void
214my_menu_init(MENU * menu)
215{
216 Trace(("called MenuHook my_menu_init"));
217 mvwprintw(status, 2, 0, "menu_init %p", (void *) menu);
218 wclrtoeol(status);
219 wrefresh(status);
220}
221
222static void
223my_menu_term(MENU * menu)
224{
225 Trace(("called MenuHook my_menu_term"));
226 mvwprintw(status, 2, 0, "menu_term %p", (void *) menu);
227 wclrtoeol(status);
228 wrefresh(status);
229}
230
231static void
232my_item_init(MENU * menu)
233{
234 ITEM *item = current_item(menu);
235 const char *name = item_name(item);
236
237 Trace(("called MenuHook my_item_init (%s)", name));
238 mvwprintw(status, 2, 0, "item_init %s", name);
239 wclrtoeol(status);
240 wrefresh(status);
241}
242
243static void
244my_item_term(MENU * menu)
245{
246 ITEM *item = current_item(menu);
247 const char *name = item_name(item);
248
249 Trace(("called MenuHook my_item_term (%s)", name));
250 mvwprintw(status, 2, 0, "item_term %s", name);
251 wclrtoeol(status);
252 wrefresh(status);
253}
254
255static MENU *
256menu_create(ITEM ** items, int count, int ncols, MenuNo number)
257{
258 MENU *result;
259 WINDOW *menuwin;
260 int mrows, mcols;
261 int y = okMenuNo(number) ? MENU_Y : 0;
262 int x = menu_offset(number);
263 int margin = (y == MENU_Y) ? 1 : 0;
264 int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
265 int maxrow = (count + 1) / ncols;
266
267 if ((maxrow + y) >= (LINES - 4))
268 maxrow = LINES - 4 - y;
269
270 result = new_menu(items);
271
272 if (has_colors()) {
273 set_menu_fore(result, (chtype) COLOR_PAIR(1));
274 set_menu_back(result, (chtype) COLOR_PAIR(2));
275 }
276
277 set_menu_format(result, maxrow, maxcol);
278 scale_menu(result, &mrows, &mcols);
279
280 if (mcols + (2 * margin + x) >= COLS)
281 mcols = COLS - (2 * margin + x);
282
Steve Kondikae271bc2015-11-15 02:50:53 +0100283 menuwin = newwin(mrows + (2 * margin), mcols + (2 * margin), y, x);
284 set_menu_win(result, menuwin);
285 keypad(menuwin, TRUE);
286 if (margin)
287 box(menuwin, 0, 0);
288
289 set_menu_sub(result, derwin(menuwin, mrows, mcols, margin, margin));
290
micky3879b9f5e72025-07-08 18:04:53 -0400291#ifdef TRACE
292 if (number == eTrace)
293 menu_opts_off(result, O_ONEVALUE);
294 else
295 menu_opts_on(result, O_ONEVALUE);
296#endif
297#if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
298 menu_opts_on(result, O_MOUSE_MENU);
299#endif
300
Steve Kondikae271bc2015-11-15 02:50:53 +0100301 post_menu(result);
302
303 set_menu_init(result, my_menu_init);
304 set_menu_term(result, my_menu_term);
305 set_item_init(result, my_item_init);
306 set_item_term(result, my_item_term);
307 return result;
308}
309
310static void
micky3879b9f5e72025-07-08 18:04:53 -0400311menu_destroy(MENU * m, int itemsToo)
Steve Kondikae271bc2015-11-15 02:50:53 +0100312{
Steve Kondikae271bc2015-11-15 02:50:53 +0100313 Trace(("menu_destroy %p", (void *) m));
314 if (m != 0) {
315 ITEM **items = menu_items(m);
316 const char *blob = 0;
micky3879b9f5e72025-07-08 18:04:53 -0400317 int count;
Steve Kondikae271bc2015-11-15 02:50:53 +0100318
319 count = item_count(m);
320 Trace(("menu_destroy %p count %d", (void *) m, count));
321 if ((count > 0) && (m == mpSelect)) {
322 blob = item_name(*items);
323 }
324
325 unpost_menu(m);
326 free_menu(m);
327
328 /* free the extra data allocated in build_select_menu() */
329 if ((count > 0) && (m == mpSelect)) {
330 if (blob && loaded_file) {
331 Trace(("freeing blob %p", blob));
332 free((void *) blob);
333 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100334 }
micky3879b9f5e72025-07-08 18:04:53 -0400335 if (count > 0 && itemsToo) {
336 if (itemsToo & 1) {
337 ITEM **ip = items;
338 if (ip != 0) {
339 while (*ip)
340 free_item(*ip++);
341 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100342 }
micky3879b9f5e72025-07-08 18:04:53 -0400343 if (itemsToo & 2)
344 free(items);
Steve Kondikae271bc2015-11-15 02:50:53 +0100345 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100346 }
347}
348
349/* force the given menu to appear */
350static void
351menu_display(MENU * m)
352{
353 touchwin(menu_win(m));
354 wrefresh(menu_win(m));
355}
356
357/*****************************************************************************/
358
359static void
360build_file_menu(MenuNo number)
361{
362 static MENU_DATA table[] =
363 {
364 {"Exit", call_files, 0},
365 {(char *) 0, 0, 0}
366 };
367 static ITEM *items[SIZEOF(table)];
368
369 ITEM **ip = items;
370 int n;
371
372 for (n = 0; table[n].name != 0; ++n) {
373 *ip = new_item(table[n].name, empty);
374 set_item_userptr(*ip, (void *) &table[n]);
375 ++ip;
376 }
377 *ip = (ITEM *) 0;
378
379 mpFile = menu_create(items, SIZEOF(table) - 1, 1, number);
380}
381
382static int
383perform_file_menu(int cmd)
384{
385 return menu_driver(mpFile, cmd);
386}
387
388/*****************************************************************************/
389
390static void
391call_select(int code)
392{
393 (void) code;
394 Trace(("Selected item %d", code));
395}
396
397static void
398build_select_menu(MenuNo number, char *filename)
399{
400#define MY_DATA(name) { name, call_select, 0 }
401 static MENU_DATA table[] =
402 {
403 MY_DATA("Lions"),
404 MY_DATA("Tigers"),
405 MY_DATA("Bears"),
406 MY_DATA("(Oh my!)"),
407 MY_DATA("Newts"),
408 MY_DATA("Platypi"),
409 MY_DATA("Lemurs"),
410 MY_DATA("(Oh really?!)"),
411 MY_DATA("Leopards"),
412 MY_DATA("Panthers"),
413 MY_DATA("Pumas"),
414 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
415 MY_DATA("Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs"),
416 {(char *) 0, 0, 0}
417 };
418 static ITEM **items;
419
420 ITEM **ip;
421 MENU_DATA *ap = 0;
422 MENU_DATA *myList = 0;
423 int i;
424 size_t count = 0;
425
426 if (filename != 0) {
427 struct stat sb;
428 if (stat(filename, &sb) == 0
429 && (sb.st_mode & S_IFMT) == S_IFREG
430 && sb.st_size != 0) {
431 size_t size = (size_t) sb.st_size;
Steve Kondikae271bc2015-11-15 02:50:53 +0100432 char *blob = typeMalloc(char, size + 1);
433 MENU_DATA *list = typeCalloc(MENU_DATA, size + 1);
434
435 items = typeCalloc(ITEM *, size + 1);
436 Trace(("build_select_menu blob=%p, items=%p",
437 (void *) blob,
438 (void *) items));
439 if (blob != 0 && list != 0) {
440 FILE *fp = fopen(filename, "r");
441 if (fp != 0) {
442 if (fread(blob, sizeof(char), size, fp) == size) {
443 bool mark = TRUE;
micky3879b9f5e72025-07-08 18:04:53 -0400444 unsigned j, k;
Steve Kondikae271bc2015-11-15 02:50:53 +0100445 for (j = k = 0; j < size; ++j) {
446 if (mark) {
447 list[k++].name = blob + j;
448 mark = FALSE;
449 }
450 if (blob[j] == '\n') {
451 blob[j] = '\0';
452 if (k > 0 && *list[k - 1].name == '\0')
453 --k;
454 mark = TRUE;
455 } else if (blob[j] == '\t') {
456 blob[j] = ' '; /* menu items are printable */
457 }
458 }
459 list[k].name = 0;
460 count = k;
461 ap = myList = list;
462 }
463 fclose(fp);
464 }
465 loaded_file = TRUE;
466 }
467 if (ap == 0)
468 free(items);
469 }
470 }
471 if (ap == 0) {
472 count = SIZEOF(table) - 1;
473 items = typeCalloc(ITEM *, count + 1);
474 ap = table;
475 }
476
477 ip = items;
478 for (i = 0; ap[i].name != 0; ++i) {
479 ap[i].func = call_select;
480 ap[i].mask = (unsigned) i;
481 *ip = new_item(ap[i].name, empty);
482 set_item_userptr(*ip, (void *) &table[i]);
483 ++ip;
484 }
485 *ip = 0;
486
487 mpSelect = menu_create(items, (int) count, 1, number);
488 if (myList != 0)
489 free(myList);
490}
491
492static int
493perform_select_menu(int cmd)
494{
495 return menu_driver(mpSelect, cmd);
496}
497
498/*****************************************************************************/
499
500#ifdef TRACE
501
502static void
503call_trace(int code)
504{
505 (void) code;
506 Trace(("Updating trace mask %d", code));
507}
508
509#define T_TBL(name) { #name, call_trace, name }
510static MENU_DATA t_tbl[] =
511{
512
513 T_TBL(TRACE_DISABLE),
514 T_TBL(TRACE_TIMES),
515 T_TBL(TRACE_TPUTS),
516 T_TBL(TRACE_UPDATE),
517 T_TBL(TRACE_MOVE),
518 T_TBL(TRACE_CHARPUT),
519 T_TBL(TRACE_ORDINARY),
520 T_TBL(TRACE_CALLS),
521 T_TBL(TRACE_VIRTPUT),
522 T_TBL(TRACE_IEVENT),
523 T_TBL(TRACE_BITS),
524 T_TBL(TRACE_ICALLS),
525 T_TBL(TRACE_CCALLS),
526 T_TBL(TRACE_DATABASE),
527 T_TBL(TRACE_ATTRS),
528 T_TBL(TRACE_MAXIMUM),
529 {
530 (char *) 0, 0, 0
531 }
532};
533
534static void
535build_trace_menu(MenuNo number)
536{
537 static ITEM *items[SIZEOF(t_tbl)];
538
539 ITEM **ip = items;
540 int n;
541
542 for (n = 0; t_tbl[n].name != 0; n++) {
543 *ip = new_item(t_tbl[n].name, empty);
544 set_item_userptr(*ip, (void *) &t_tbl[n]);
545 ++ip;
546 }
547 *ip = (ITEM *) 0;
548
549 mpTrace = menu_create(items, SIZEOF(t_tbl) - 1, 2, number);
550}
551
552static char *
553tracetrace(unsigned tlevel)
554{
555 static char *buf;
micky3879b9f5e72025-07-08 18:04:53 -0400556 static size_t need = 12;
Steve Kondikae271bc2015-11-15 02:50:53 +0100557 int n;
558
559 if (buf == 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100560 for (n = 0; t_tbl[n].name != 0; n++)
561 need += strlen(t_tbl[n].name) + 2;
562 buf = typeMalloc(char, need);
micky3879b9f5e72025-07-08 18:04:53 -0400563 if (!buf)
564 failed("tracetrace");
Steve Kondikae271bc2015-11-15 02:50:53 +0100565 }
micky3879b9f5e72025-07-08 18:04:53 -0400566 _nc_SPRINTF(buf, _nc_SLIMIT(need) "0x%02x = {", tlevel);
Steve Kondikae271bc2015-11-15 02:50:53 +0100567 if (tlevel == 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400568 _nc_STRCAT(buf, t_tbl[0].name ? t_tbl[0].name : "", need);
569 _nc_STRCAT(buf, ", ", need);
Steve Kondikae271bc2015-11-15 02:50:53 +0100570 } else {
571 for (n = 1; t_tbl[n].name != 0; n++)
572 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
micky3879b9f5e72025-07-08 18:04:53 -0400573 _nc_STRCAT(buf, t_tbl[n].name, need);
574 _nc_STRCAT(buf, ", ", need);
Steve Kondikae271bc2015-11-15 02:50:53 +0100575 }
576 }
577 if (buf[strlen(buf) - 2] == ',')
578 buf[strlen(buf) - 2] = '\0';
micky3879b9f5e72025-07-08 18:04:53 -0400579 _nc_STRCAT(buf, "}", need);
580 return buf;
Steve Kondikae271bc2015-11-15 02:50:53 +0100581}
582
583/* fake a dynamically reconfigurable menu using the 0th entry to deselect
584 * the others
585 */
586static bool
587update_trace_menu(MENU * m)
588{
589 ITEM **items;
micky3879b9f5e72025-07-08 18:04:53 -0400590 ITEM *i;
Steve Kondikae271bc2015-11-15 02:50:53 +0100591 bool changed = FALSE;
592
593 items = menu_items(m);
594 i = current_item(m);
595 if (i == items[0]) {
596 if (item_value(i)) {
micky3879b9f5e72025-07-08 18:04:53 -0400597 ITEM **p;
Steve Kondikae271bc2015-11-15 02:50:53 +0100598 for (p = items + 1; *p != 0; p++)
599 if (item_value(*p)) {
600 set_item_value(*p, FALSE);
601 changed = TRUE;
602 }
603 }
604 }
605 return changed;
606}
607
608static int
609perform_trace_menu(int cmd)
610/* interactively set the trace level */
611{
612 ITEM **ip;
Steve Kondikae271bc2015-11-15 02:50:53 +0100613 int result;
614
615 for (ip = menu_items(mpTrace); *ip; ip++) {
616 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
617 unsigned mask = td->mask;
618 if (mask == 0)
619 set_item_value(*ip, _nc_tracing == 0);
620 else if ((mask & _nc_tracing) == mask)
621 set_item_value(*ip, TRUE);
622 }
623
624 result = menu_driver(mpTrace, cmd);
625
626 if (result == E_OK) {
627 if (update_trace_menu(mpTrace) || cmd == REQ_TOGGLE_ITEM) {
micky3879b9f5e72025-07-08 18:04:53 -0400628 unsigned newtrace = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100629 for (ip = menu_items(mpTrace); *ip; ip++) {
630 if (item_value(*ip)) {
631 MENU_DATA *td = (MENU_DATA *) item_userptr(*ip);
632 newtrace |= td->mask;
633 }
634 }
micky3879b9f5e72025-07-08 18:04:53 -0400635 curses_trace(newtrace);
Steve Kondikae271bc2015-11-15 02:50:53 +0100636 Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
637
638 MvWPrintw(status, 1, 0,
639 "Trace level is %s\n", tracetrace(_nc_tracing));
640 wrefresh(status);
641 }
642 }
643 return result;
644}
645#endif /* TRACE */
646
647/*****************************************************************************/
648
649static int
650menu_number(void)
651{
652 return item_index(current_item(mpBanner)) - (eBanner + 1);
653}
654
655static MENU *
656current_menu(void)
657{
658 MENU *result;
659
660 switch (menu_number()) {
661 case eFile:
662 result = mpFile;
663 break;
664 case eSelect:
665 result = mpSelect;
666 break;
667#ifdef TRACE
668 case eTrace:
669 result = mpTrace;
670 break;
671#endif
672 default:
673 result = 0;
674 break;
675 }
676 return result;
677}
678
679static void
680call_menus(int code)
681{
682 (void) code;
683 Trace(("Activated menu %d\n", code));
684}
685
686static void
687build_menus(char *filename)
688{
689 static MENU_DATA table[] =
690 {
691 {"File", call_menus, 0},
692 {"Select", call_menus, 1},
693#ifdef TRACE
694 {"Trace", call_menus, 2},
695#endif
696 {(char *) 0, 0, 0}
697 };
698 static ITEM *items[SIZEOF(table)];
699
700 ITEM **ip = items;
701 int n;
702
703 for (n = 0; table[n].name != 0; ++n) {
704 *ip = new_item(table[n].name, empty);
705 set_item_userptr(*ip, (void *) &table[n]);
706 ++ip;
707 }
708 *ip = (ITEM *) 0;
709
710 mpBanner = menu_create(items, SIZEOF(table) - 1, SIZEOF(table) - 1, eBanner);
711 set_menu_mark(mpBanner, ">");
712
713 build_file_menu(eFile);
714 build_select_menu(eSelect, filename);
715#ifdef TRACE
716 build_trace_menu(eTrace);
717#endif
718}
719
720static int
721move_menu(MENU * menu, MENU * current, int by_y, int by_x)
722{
723 WINDOW *top_win = menu_win(menu);
724 WINDOW *sub_win = menu_sub(menu);
725 int y0, x0;
726 int y1, x1;
727 int result;
728
729 getbegyx(top_win, y0, x0);
730 y0 += by_y;
731 x0 += by_x;
732
733 getbegyx(sub_win, y1, x1);
734 y1 += by_y;
735 x1 += by_x;
736
737 if ((result = mvwin(top_win, y0, x0)) != ERR) {
738#if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
739 sub_win->_begy = y1;
740 sub_win->_begx = x1;
741#else
742 mvwin(sub_win, y1, x1);
743#endif
744 if (menu == current) {
745 touchwin(top_win);
746 wnoutrefresh(top_win);
747 }
748 }
749 return result;
750}
751
752/*
753 * Move the menus around on the screen, to test mvwin().
754 */
755static void
756move_menus(MENU * current, int by_y, int by_x)
757{
758 if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
759 erase();
760 wnoutrefresh(stdscr);
761 move_menu(mpFile, current, by_y, by_x);
762 move_menu(mpSelect, current, by_y, by_x);
763#ifdef TRACE
764 move_menu(mpTrace, current, by_y, by_x);
765#endif
766 doupdate();
767 }
768}
769
micky3879b9f5e72025-07-08 18:04:53 -0400770#if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
771static void
772resize_menu(MENU ** menu)
773{
774#if 0
775 WINDOW *win = menu_win(*menu);
776 WINDOW *sub = menu_sub(*menu);
777#endif
778 (void) menu;
779}
780
781static void
782resize_menus(MENU * current)
783{
784 (void) current;
785
786 werase(status);
787 wnoutrefresh(status);
788 wresize(status, 1, COLS);
789 mvwin(status, LINES - 1, 0);
790
791 resize_menu(&mpBanner);
792 resize_menu(&mpFile);
793 resize_menu(&mpSelect);
794#ifdef TRACE
795 resize_menu(&mpTrace);
796#endif
797}
798#endif /* defined(KEY_RESIZE) && NCURSES_EXT_FUNCS */
799
Steve Kondikae271bc2015-11-15 02:50:53 +0100800static void
801show_status(int ch, MENU * menu)
802{
803 wmove(status, 0, 0);
804 wprintw(status, "key %s, menu %d, mark %s, match %s",
805 keyname(ch),
806 menu_number(),
807 menu_mark(menu),
808 menu_pattern(menu));
809 wclrtoeol(status);
810 wrefresh(status);
811}
812
813static void
814perform_menus(void)
815{
816 MENU *this_menu;
817 MENU *last_menu = mpFile;
818 int code = E_UNKNOWN_COMMAND;
819 int cmd;
820 int ch = ERR;
821
822#ifdef NCURSES_MOUSE_VERSION
micky3879b9f5e72025-07-08 18:04:53 -0400823 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
Steve Kondikae271bc2015-11-15 02:50:53 +0100824#endif
825
826 menu_display(last_menu);
827
828 for (;;) {
829
830 if (ch != ERR)
831 show_status(ch, last_menu);
832
833 ch = menu_getc(mpBanner);
834
835 /*
836 * Provide for moving the menu around in the screen using shifted
837 * cursor keys.
838 */
839 switch (ch) {
840 case KEY_SF:
841 move_menus(last_menu, 1, 0);
842 continue;
843 case KEY_SR:
844 move_menus(last_menu, -1, 0);
845 continue;
846 case KEY_SLEFT:
847 move_menus(last_menu, 0, -1);
848 continue;
849 case KEY_SRIGHT:
850 move_menus(last_menu, 0, 1);
851 continue;
micky3879b9f5e72025-07-08 18:04:53 -0400852#if defined(KEY_RESIZE) && NCURSES_EXT_FUNCS
853 case KEY_RESIZE:
854 resize_menus(last_menu);
855 continue;
856#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100857 }
858 cmd = menu_virtualize(ch);
859
860 switch (cmd) {
861 /*
862 * The banner menu acts solely to select one of the other menus.
863 * Move between its items, wrapping at the left/right limits.
864 */
865 case REQ_LEFT_ITEM:
866 case REQ_RIGHT_ITEM:
867 code = menu_driver(mpBanner, cmd);
868 if (code == E_REQUEST_DENIED) {
869 if (menu_number() > 0)
870 code = menu_driver(mpBanner, REQ_FIRST_ITEM);
871 else
872 code = menu_driver(mpBanner, REQ_LAST_ITEM);
873 }
874 break;
875 default:
876 switch (menu_number()) {
877 case eFile:
878 code = perform_file_menu(cmd);
879 break;
880 case eSelect:
881 code = perform_select_menu(cmd);
882 break;
883#ifdef TRACE
884 case eTrace:
885 code = perform_trace_menu(cmd);
886 break;
887#endif
888 }
889
micky3879b9f5e72025-07-08 18:04:53 -0400890#if defined(NCURSES_MOUSE_VERSION) && defined(O_MOUSE_MENU)
Steve Kondikae271bc2015-11-15 02:50:53 +0100891 if ((code == E_REQUEST_DENIED) && (cmd == KEY_MOUSE)) {
micky3879b9f5e72025-07-08 18:04:53 -0400892 (void) menu_getc(mpBanner);
Steve Kondikae271bc2015-11-15 02:50:53 +0100893 code = menu_driver(mpBanner, cmd);
micky3879b9f5e72025-07-08 18:04:53 -0400894 if (code == E_REQUEST_DENIED) {
895 MEVENT event;
896 if (menu_getc(mpBanner) == KEY_MOUSE)
897 getmouse(&event); /* give up */
898 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100899 }
micky3879b9f5e72025-07-08 18:04:53 -0400900#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100901
902 break;
903 }
904
905 if (code == E_OK) {
906 this_menu = current_menu();
907 if (this_menu != last_menu) {
908 move(1, 0);
909 clrtobot();
910 box(menu_win(this_menu), 0, 0);
911 refresh();
912
913 /* force the current menu to appear */
914 menu_display(this_menu);
915
916 last_menu = this_menu;
917 }
918 }
919 wrefresh(menu_win(last_menu));
920 if (code == E_UNKNOWN_COMMAND
921 || code == E_NOT_POSTED) {
922 ITEM *item = current_item(last_menu);
923 MENU_DATA *td = (MENU_DATA *) item_userptr(item);
924 td->func((int) td->mask);
925 }
926 if (code == E_REQUEST_DENIED)
927 beep();
Steve Kondikae271bc2015-11-15 02:50:53 +0100928 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100929}
930
931static void
932destroy_menus(void)
933{
micky3879b9f5e72025-07-08 18:04:53 -0400934 menu_destroy(mpFile, 1);
935 menu_destroy(mpSelect, 3);
Steve Kondikae271bc2015-11-15 02:50:53 +0100936#ifdef TRACE
micky3879b9f5e72025-07-08 18:04:53 -0400937 menu_destroy(mpTrace, 1);
Steve Kondikae271bc2015-11-15 02:50:53 +0100938#endif
micky3879b9f5e72025-07-08 18:04:53 -0400939 menu_destroy(mpBanner, 1);
Steve Kondikae271bc2015-11-15 02:50:53 +0100940}
941
942#if HAVE_RIPOFFLINE
943static int
944rip_footer(WINDOW *win, int cols)
945{
946 wbkgd(win, A_REVERSE);
947 werase(win);
948 wmove(win, 0, 0);
949 wprintw(win, "footer: %d columns", cols);
950 wnoutrefresh(win);
951 return OK;
952}
953
954static int
955rip_header(WINDOW *win, int cols)
956{
957 wbkgd(win, A_REVERSE);
958 werase(win);
959 wmove(win, 0, 0);
960 wprintw(win, "header: %d columns", cols);
961 wnoutrefresh(win);
962 return OK;
963}
964#endif /* HAVE_RIPOFFLINE */
965
966static void
967call_files(int code)
968{
969 switch (code) {
970 case 0:
971 destroy_menus();
972 endwin();
973 printf("DONE!\n");
974 ExitProgram(EXIT_SUCCESS);
975 }
976}
977
978static void
micky3879b9f5e72025-07-08 18:04:53 -0400979usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +0100980{
981 static const char *const tbl[] =
982 {
983 "Usage: demo_menus [options] [menu-file]"
984 ,""
micky3879b9f5e72025-07-08 18:04:53 -0400985 ,USAGE_COMMON
Steve Kondikae271bc2015-11-15 02:50:53 +0100986 ,"Options:"
987#if HAVE_RIPOFFLINE
micky3879b9f5e72025-07-08 18:04:53 -0400988 ," -F rip-off footer line (can repeat)"
989 ," -H rip-off header line (can repeat)"
Steve Kondikae271bc2015-11-15 02:50:53 +0100990#endif
991#ifdef TRACE
micky3879b9f5e72025-07-08 18:04:53 -0400992 ," -t mask specify default trace-level (may toggle with ^T)"
Steve Kondikae271bc2015-11-15 02:50:53 +0100993#endif
994 };
995 size_t n;
996 for (n = 0; n < SIZEOF(tbl); n++)
997 fprintf(stderr, "%s\n", tbl[n]);
micky3879b9f5e72025-07-08 18:04:53 -0400998 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100999}
micky3879b9f5e72025-07-08 18:04:53 -04001000/* *INDENT-OFF* */
1001VERSION_COMMON()
1002/* *INDENT-ON* */
Steve Kondikae271bc2015-11-15 02:50:53 +01001003
1004int
1005main(int argc, char *argv[])
1006{
micky3879b9f5e72025-07-08 18:04:53 -04001007 int ch;
Steve Kondikae271bc2015-11-15 02:50:53 +01001008
1009 setlocale(LC_ALL, "");
micky3879b9f5e72025-07-08 18:04:53 -04001010 START_TRACE();
Steve Kondikae271bc2015-11-15 02:50:53 +01001011
micky3879b9f5e72025-07-08 18:04:53 -04001012 while ((ch = getopt(argc, argv, OPTS_COMMON "FHt:")) != -1) {
1013 switch (ch) {
Steve Kondikae271bc2015-11-15 02:50:53 +01001014#if HAVE_RIPOFFLINE
micky3879b9f5e72025-07-08 18:04:53 -04001015 case 'F':
Steve Kondikae271bc2015-11-15 02:50:53 +01001016 ripoffline(-1, rip_footer);
1017 break;
micky3879b9f5e72025-07-08 18:04:53 -04001018 case 'H':
Steve Kondikae271bc2015-11-15 02:50:53 +01001019 ripoffline(1, rip_header);
1020 break;
1021#endif /* HAVE_RIPOFFLINE */
1022#ifdef TRACE
1023 case 't':
micky3879b9f5e72025-07-08 18:04:53 -04001024 curses_trace((unsigned) strtoul(optarg, 0, 0));
Steve Kondikae271bc2015-11-15 02:50:53 +01001025 break;
1026#endif
micky3879b9f5e72025-07-08 18:04:53 -04001027 case OPTS_VERSION:
1028 show_version(argv);
1029 ExitProgram(EXIT_SUCCESS);
Steve Kondikae271bc2015-11-15 02:50:53 +01001030 default:
micky3879b9f5e72025-07-08 18:04:53 -04001031 usage(ch == OPTS_USAGE);
1032 /* NOTREACHED */
Steve Kondikae271bc2015-11-15 02:50:53 +01001033 }
1034 }
1035
1036 initscr();
1037 noraw();
1038 cbreak();
1039 noecho();
1040
1041 if (has_colors()) {
1042 start_color();
1043 init_pair(1, COLOR_RED, COLOR_BLACK);
1044 init_pair(2, COLOR_BLUE, COLOR_WHITE);
1045 }
1046 status = newwin(3, COLS, LINES - 3, 0);
1047 build_menus(argc > 1 ? argv[1] : 0);
1048 perform_menus();
1049 destroy_menus();
1050
1051 endwin();
1052 ExitProgram(EXIT_SUCCESS);
1053}
1054#else
1055int
1056main(void)
1057{
1058 printf("This program requires the curses menu library\n");
1059 ExitProgram(EXIT_FAILURE);
1060}
1061#endif