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
****************************************************************************/
2
* $Id: demo_menus.c,v 1.13 2005/10/01 15:54:31 tom Exp $
29
* $Id: demo_menus.c,v 1.28 2008/08/23 20:31:54 tom Exp $
4
31
* Demonstrate a variety of functions from the menu library.
5
32
* Thomas Dickey - 2005/4/9
102
132
while ((c = wgetch(win)) == CTRL('T')) {
103
133
if (_nc_tracing) {
104
134
save_trace = _nc_tracing;
105
_tracef("TOGGLE-TRACING OFF");
135
Trace(("TOGGLE-TRACING OFF"));
108
138
_nc_tracing = save_trace;
110
140
trace(_nc_tracing);
112
_tracef("TOGGLE-TRACING ON");
142
Trace(("TOGGLE-TRACING ON"));
185
215
int mrows, mcols;
186
int y = ((int) number >= 0) ? MENU_Y : 0;
216
int y = okMenuNo(number) ? MENU_Y : 0;
187
217
int x = menu_offset(number);
188
218
int margin = (y == MENU_Y) ? 1 : 0;
189
219
int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
229
259
menu_destroy(MENU * m)
263
Trace(("menu_destroy %p", m));
265
ITEM **items = menu_items(m);
266
const char *blob = 0;
238
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);
244
_tracef("freeing item %d:%d", ip - menu_items(m), count);
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)) {
318
362
if (stat(filename, &sb) == 0
319
363
&& (sb.st_mode & S_IFMT) == S_IFREG
320
364
&& sb.st_size != 0) {
321
unsigned size = sb.st_size;
365
size_t size = (size_t) sb.st_size;
323
char *blob = malloc(size + 1);
324
const char **list = (const char **) calloc(sizeof(*list), size + 1);
367
char *blob = typeMalloc(char, size + 1);
368
CONST_MENUS char **list = typeCalloc(CONST_MENUS char *, size + 1);
326
items = (ITEM **) calloc(sizeof(ITEM *), size + 1);
370
items = typeCalloc(ITEM *, size + 1);
371
Trace(("build_select_menu blob=%p, items=%p", blob, items));
327
372
if (blob != 0 && list != 0) {
328
373
FILE *fp = fopen(filename, "r");
427
475
size_t need = 12;
428
476
for (n = 0; t_tbl[n].name != 0; n++)
429
477
need += strlen(t_tbl[n].name) + 2;
430
buf = (char *) malloc(need);
478
buf = typeMalloc(char, need);
432
480
sprintf(buf, "0x%02x = {", tlevel);
433
481
if (tlevel == 0) {
494
542
newtrace |= t_tbl[item_index(*ip)].mask;
497
_tracef("trace level interactively set to %s", tracetrace(_nc_tracing));
545
Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
499
547
(void) mvprintw(LINES - 2, 0,
500
548
"Trace level is %s\n", tracetrace(_nc_tracing));
552
600
static ITEM *items[SIZEOF(labels)];
554
602
ITEM **ip = items;
603
CONST_MENUS char **ap;
557
605
for (ap = labels; *ap; ap++)
558
606
*ip++ = new_item(*ap, "");
559
607
*ip = (ITEM *) 0;
561
mpBanner = menu_create(items, SIZEOF(labels) - 1, SIZEOF(labels) - 1, eUnknown);
609
mpBanner = menu_create(items, SIZEOF(labels) - 1, SIZEOF(labels) - 1, eBanner);
562
610
set_menu_mark(mpBanner, ">");
564
612
build_file_menu(eFile);
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",
572
683
perform_menus(void)
575
686
MENU *last_menu = mpFile;
576
int code = E_UNKNOWN_COMMAND, cmd, ch;
687
int code = E_UNKNOWN_COMMAND;
578
691
#ifdef NCURSES_MOUSE_VERSION
579
692
mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
582
695
menu_display(last_menu);
700
show_status(ch, last_menu);
585
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);
586
722
cmd = menu_virtualize(ch);
664
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);
668
850
main(int argc, char *argv[])
670
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));