Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.29.2.1
      1  1.29.2.1       jmc /*	$NetBSD: menu_sys.def,v 1.29.2.1 2003/12/24 04:33:36 jmc Exp $	*/
      2       1.1      phil 
      3       1.1      phil /*
      4       1.1      phil  * Copyright 1997 Piermont Information Systems Inc.
      5       1.1      phil  * All rights reserved.
      6       1.1      phil  *
      7       1.1      phil  * Written by Philip A. Nelson for Piermont Information Systems Inc.
      8       1.1      phil  *
      9       1.1      phil  * Redistribution and use in source and binary forms, with or without
     10       1.1      phil  * modification, are permitted provided that the following conditions
     11       1.1      phil  * are met:
     12       1.1      phil  * 1. Redistributions of source code must retain the above copyright
     13       1.1      phil  *    notice, this list of conditions and the following disclaimer.
     14       1.1      phil  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1      phil  *    notice, this list of conditions and the following disclaimer in the
     16       1.1      phil  *    documentation and/or other materials provided with the distribution.
     17       1.1      phil  * 3. All advertising materials mentioning features or use of this software
     18       1.1      phil  *    must display the following acknowledgement:
     19       1.1      phil  *      This product includes software develooped for the NetBSD Project by
     20       1.1      phil  *      Piermont Information Systems Inc.
     21       1.1      phil  * 4. The name of Piermont Information Systems Inc. may not be used to endorse
     22       1.1      phil  *    or promote products derived from this software without specific prior
     23       1.1      phil  *    written permission.
     24       1.1      phil  *
     25       1.1      phil  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     26       1.1      phil  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27       1.1      phil  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28       1.1      phil  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     29       1.1      phil  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30       1.1      phil  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31       1.1      phil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32       1.1      phil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33       1.1      phil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34       1.1      phil  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     35       1.1      phil  * THE POSSIBILITY OF SUCH DAMAGE.
     36       1.1      phil  *
     37       1.1      phil  */
     38       1.1      phil 
     39       1.1      phil /* menu_sys.defs -- Menu system standard routines. */
     40       1.1      phil 
     41       1.1      phil #include <string.h>
     42       1.1      phil #include <ctype.h>
     43       1.1      phil 
     44       1.3      phil #define REQ_EXECUTE    1000
     45       1.3      phil #define REQ_NEXT_ITEM  1001
     46       1.3      phil #define REQ_PREV_ITEM  1002
     47       1.3      phil #define REQ_REDISPLAY  1003
     48       1.9      phil #define REQ_SCROLLDOWN 1004
     49       1.9      phil #define REQ_SCROLLUP   1005
     50       1.9      phil #define REQ_HELP       1006
     51       1.9      phil 
     52       1.9      phil /* Macros */
     53       1.1      phil #define MAX(x,y) ((x)>(y)?(x):(y))
     54       1.7      phil #define MIN(x,y) ((x)<(y)?(x):(y))
     55       1.1      phil 
     56       1.1      phil /* Initialization state. */
     57       1.1      phil static int __menu_init = 0;
     58       1.1      phil int __m_endwin = 0;
     59      1.17       cgd static int max_lines = 0, max_cols = 0;
     60       1.7      phil static char *scrolltext = " <: page up, >: page down";
     61       1.1      phil 
     62      1.14      phil static menudesc *menus = menu_def;
     63      1.14      phil 
     64      1.14      phil #ifdef DYNAMIC_MENUS
     65      1.14      phil static int num_menus  = 0;
     66      1.14      phil static int num_avail  = 0;
     67      1.14      phil #define DYN_INIT_NUM 32
     68      1.14      phil #endif
     69      1.14      phil 
     70       1.1      phil /* prototypes for in here! */
     71       1.1      phil static void init_menu (struct menudesc *m);
     72      1.14      phil static char opt_ch (int op_no);
     73       1.1      phil static void post_menu (struct menudesc *m);
     74       1.5      phil static void process_help (struct menudesc *m, int num);
     75       1.4      phil static void process_req (struct menudesc *m, int num, int req);
     76       1.1      phil static int menucmd (WINDOW *w);
     77       1.1      phil 
     78       1.1      phil #ifndef NULL
     79       1.1      phil #define NULL (void *)0
     80       1.1      phil #endif
     81       1.1      phil 
     82       1.1      phil /* menu system processing routines */
     83      1.27  christos #define mbeep() (void)fputc('\a', stderr)
     84       1.1      phil 
     85      1.23   hubertf static int
     86      1.23   hubertf menucmd (WINDOW *w)
     87       1.1      phil {
     88       1.1      phil 	int ch;
     89       1.1      phil 
     90       1.1      phil 	while (TRUE) {
     91      1.29     blymn 		ch = wgetch(w);
     92       1.1      phil 
     93       1.1      phil 		switch (ch) {
     94       1.1      phil 		case '\n':
     95       1.1      phil 			return REQ_EXECUTE;
     96      1.11      phil 		case '\016':  /* Contnrol-P */
     97      1.29     blymn 		case KEY_DOWN:
     98       1.1      phil 			return REQ_NEXT_ITEM;
     99      1.11      phil 		case '\020':  /* Control-N */
    100      1.29     blymn 		case KEY_UP:
    101       1.1      phil 			return REQ_PREV_ITEM;
    102      1.11      phil 		case '\014':  /* Control-L */
    103       1.3      phil 		        return REQ_REDISPLAY;
    104       1.9      phil 		case '<':
    105      1.11      phil 		case '\010':  /* Control-H (backspace) */
    106      1.29     blymn 		case KEY_PPAGE:
    107       1.9      phil 			return REQ_SCROLLUP;
    108      1.29     blymn 		case '\026':
    109       1.9      phil 		case '>':
    110      1.11      phil 		case ' ':
    111      1.29     blymn 		case KEY_NPAGE:
    112       1.9      phil 			return REQ_SCROLLDOWN;
    113       1.9      phil 		case '?':
    114       1.9      phil 			return REQ_HELP;
    115      1.29     blymn 		case '\033': /* esc-v is scroll down */
    116      1.29     blymn 			ch = wgetch(w);
    117      1.29     blymn 			if (ch == 'v')
    118      1.29     blymn 				return REQ_SCROLLUP;
    119      1.29     blymn 			else
    120      1.29     blymn 				ch = 0; /* zap char so we beep */
    121       1.1      phil 		}
    122       1.1      phil 
    123       1.9      phil 		if (isalpha(ch))
    124       1.1      phil 			return (ch);
    125       1.1      phil 
    126       1.1      phil 		mbeep();
    127       1.1      phil 		wrefresh(w);
    128       1.1      phil 	}
    129       1.1      phil }
    130       1.1      phil 
    131      1.23   hubertf static void
    132      1.23   hubertf init_menu (struct menudesc *m)
    133       1.1      phil {
    134      1.18       cgd 	int wmax;
    135      1.18       cgd 	int hadd, wadd, exithadd;
    136      1.14      phil 	int i;
    137       1.1      phil 
    138      1.18       cgd 	hadd = ((m->mopt & MC_NOBOX) ? 0 : 2);
    139      1.18       cgd 	wadd = ((m->mopt & MC_NOBOX) ? 2 : 4);
    140      1.18       cgd 
    141      1.18       cgd 	hadd += strlen(m->title) != 0 ? 2 : 0;
    142      1.18       cgd 	exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
    143      1.18       cgd 
    144      1.18       cgd 	wmax = strlen(m->title);
    145       1.1      phil 
    146       1.7      phil 	/* Calculate h? h == number of visible options. */
    147       1.1      phil 	if (m->h == 0) {
    148      1.18       cgd 		m->h = m->numopts + exithadd;
    149      1.18       cgd 		if (m->h + m->y + hadd >= max_lines && (m->mopt & MC_SCROLL))
    150      1.18       cgd 			m->h = max_lines - m->y - hadd ;
    151       1.1      phil 	}
    152       1.7      phil 
    153       1.7      phil 	/* Check window heights and set scrolling */
    154      1.18       cgd 	if (m->h < m->numopts + exithadd) {
    155      1.14      phil 		if (!(m->mopt & MC_SCROLL) || m->h < 3) {
    156       1.7      phil 			endwin();
    157       1.7      phil 			(void) fprintf (stderr,
    158      1.17       cgd 				"Window too short for menu \"%s\"\n",
    159       1.9      phil 				m->title);
    160       1.7      phil 			exit(1);
    161       1.7      phil 		}
    162       1.7      phil 	} else
    163      1.14      phil 		m->mopt &= ~MC_SCROLL;
    164       1.1      phil 
    165       1.9      phil 	/* check for screen fit */
    166      1.18       cgd 	if (m->y + m->h + hadd > max_lines) {
    167       1.9      phil 		endwin();
    168       1.9      phil 		(void) fprintf (stderr,
    169      1.17       cgd 			"Screen too short for menu \"%s\"\n", m->title);
    170       1.9      phil 		exit(1);
    171       1.9      phil 
    172       1.9      phil 	}
    173       1.9      phil 
    174       1.1      phil 	/* Calculate w? */
    175       1.1      phil 	if (m->w == 0) {
    176      1.14      phil 		if (m->mopt & MC_SCROLL)
    177      1.18       cgd 			wmax = MAX(wmax,strlen(scrolltext));
    178      1.14      phil 		for (i=0; i < m->numopts; i++ )
    179      1.18       cgd 			wmax = MAX(wmax,strlen(m->opts[i].opt_name)+3);
    180      1.18       cgd 		m->w = wmax;
    181       1.1      phil 	}
    182       1.1      phil 
    183      1.17       cgd 	/* check and adjust for screen fit */
    184      1.18       cgd 	if (m->w + wadd > max_cols) {
    185      1.17       cgd 		endwin();
    186      1.17       cgd 		(void) fprintf (stderr,
    187      1.17       cgd 			"Screen too narrow for menu \"%s\"\n", m->title);
    188      1.17       cgd 		exit(1);
    189      1.17       cgd 
    190      1.17       cgd 	}
    191      1.20       cgd 	if (m->x == -1)
    192      1.20       cgd 		m->x = (max_cols - (m->w + wadd)) / 2;	/* center */
    193      1.20       cgd 	else if (m->x + m->w + wadd > max_cols)
    194      1.18       cgd 		m->x = max_cols - (m->w + wadd);
    195      1.17       cgd 
    196       1.1      phil 	/* Get the windows. */
    197      1.18       cgd 	m->mw = newwin(m->h+hadd, m->w+wadd, m->y, m->x);
    198      1.29     blymn 	keypad(m->mw, TRUE); /* enable multi-key assembling for win */
    199       1.1      phil 
    200       1.1      phil 	if (m->mw == NULL) {
    201       1.1      phil 		endwin();
    202       1.1      phil 		(void) fprintf (stderr,
    203      1.18       cgd 			"Could not create window for menu \"%s\"\n", m->title);
    204       1.1      phil 		exit(1);
    205      1.26     perry 	}
    206      1.26     perry 
    207      1.26     perry 	/* XXX is it even worth doing this right? */
    208      1.26     perry 	if (has_colors()) {
    209      1.26     perry 		wbkgd(m->mw, COLOR_PAIR(1));
    210      1.26     perry 		wattrset(m->mw, COLOR_PAIR(1));
    211      1.26     perry 	}
    212       1.1      phil }
    213       1.1      phil 
    214      1.23   hubertf static char
    215      1.23   hubertf opt_ch (int op_no)
    216      1.14      phil {
    217      1.14      phil 	char c;
    218      1.14      phil 	if (op_no < 25) {
    219      1.14      phil 		c = 'a' + op_no;
    220      1.14      phil 		if (c >= 'x') c++;
    221      1.14      phil 	} else
    222      1.14      phil 		c = 'A' + op_no - 25;
    223      1.14      phil 	return (char) c;
    224      1.14      phil }
    225      1.14      phil 
    226      1.23   hubertf static void
    227      1.23   hubertf post_menu (struct menudesc *m)
    228       1.1      phil {
    229       1.1      phil 	int i;
    230       1.7      phil 	int hasbox, cury, maxy, selrow, lastopt;
    231       1.1      phil 	int tadd;
    232      1.14      phil 	char optstr[5];
    233       1.1      phil 
    234      1.14      phil 	if (m->mopt & MC_NOBOX) {
    235       1.1      phil 		cury = 0;
    236       1.7      phil 		maxy = m->h;
    237       1.1      phil 		hasbox = 0;
    238       1.1      phil 	} else {
    239       1.1      phil 		cury = 1;
    240       1.7      phil 		maxy = m->h+1;
    241       1.1      phil 		hasbox = 1;
    242       1.1      phil 	}
    243       1.1      phil 
    244       1.9      phil 	/* Clear the window */
    245       1.9      phil 	wclear (m->mw);
    246       1.9      phil 
    247       1.1      phil 	tadd = strlen(m->title) ? 2 : 0;
    248       1.1      phil 
    249       1.1      phil 	if (tadd) {
    250      1.18       cgd 		mvwaddstr(m->mw, cury, cury, " ");
    251      1.18       cgd 		mvwaddstr(m->mw, cury, cury + 1, m->title);
    252       1.1      phil 		cury += 2;
    253       1.7      phil 		maxy += 2;
    254       1.1      phil 	}
    255       1.1      phil 
    256       1.7      phil 	/* Set defaults, calculate lastopt. */
    257       1.7      phil 	selrow = -1;
    258      1.14      phil 	if (m->mopt & MC_SCROLL) {
    259       1.7      phil 		lastopt = MIN(m->numopts, m->topline+m->h-1);
    260       1.7      phil 		maxy -= 1;
    261       1.7      phil 	} else
    262       1.7      phil 		lastopt = m->numopts;
    263       1.7      phil 
    264       1.7      phil 	for (i=m->topline; i<lastopt; i++, cury++) {
    265       1.1      phil 		if (m->cursel == i) {
    266       1.1      phil 			mvwaddstr (m->mw, cury, hasbox, ">");
    267       1.1      phil 			wstandout(m->mw);
    268       1.7      phil 			selrow = cury;
    269       1.1      phil 		} else
    270       1.1      phil 			mvwaddstr (m->mw, cury, hasbox, " ");
    271      1.24      phil 		if (!(m->mopt & MC_NOSHORTCUT)) {
    272      1.23   hubertf 			(void) sprintf (optstr, "%c: ", opt_ch(i));
    273      1.24      phil 		    	waddstr (m->mw, optstr);
    274      1.23   hubertf 		}
    275      1.14      phil 		waddstr (m->mw, m->opts[i].opt_name);
    276       1.1      phil 		if (m->cursel == i)
    277       1.1      phil 			wstandend(m->mw);
    278       1.1      phil 	}
    279       1.7      phil 
    280       1.7      phil 	/* Add the exit option. */
    281      1.14      phil 	if (!(m->mopt & MC_NOEXITOPT) && cury < maxy) {
    282       1.7      phil 		if (m->cursel >= m->numopts) {
    283       1.7      phil 			mvwaddstr (m->mw, cury, hasbox, ">");
    284       1.7      phil 			wstandout(m->mw);
    285       1.7      phil 			selrow = cury;
    286       1.7      phil 		} else
    287       1.7      phil 			mvwaddstr (m->mw, cury, hasbox, " ");
    288      1.24      phil 		if (!(m->mopt & MC_NOSHORTCUT))
    289      1.23   hubertf 			waddstr (m->mw, "x: ");
    290      1.19       cgd 		waddstr (m->mw, m->exitstr);
    291       1.7      phil 		if (m->cursel >= m->numopts)
    292       1.7      phil 			wstandend(m->mw);
    293       1.7      phil 		cury++;
    294       1.7      phil 	}
    295       1.7      phil 
    296       1.7      phil 	/* Add the scroll line */
    297      1.14      phil 	if (m->mopt & MC_SCROLL) {
    298       1.7      phil 		mvwaddstr (m->mw, cury, hasbox, scrolltext);
    299       1.7      phil 		if (selrow < 0)
    300       1.7      phil 			selrow = cury;
    301       1.7      phil 	}
    302       1.7      phil 
    303       1.7      phil 	/* Add the box. */
    304      1.14      phil 	if (!(m->mopt & MC_NOBOX))
    305      1.26     perry 		box(m->mw, 0, 0);
    306       1.1      phil 
    307       1.7      phil 	wmove(m->mw, selrow, hasbox);
    308       1.1      phil }
    309       1.1      phil 
    310      1.23   hubertf static void
    311      1.23   hubertf process_help (struct menudesc *m, int num)
    312       1.5      phil {
    313       1.5      phil 	char *help = m->helpstr;
    314       1.7      phil 	int lineoff = 0;
    315       1.5      phil 	int curoff = 0;
    316       1.5      phil 	int again;
    317       1.7      phil 	int winin;
    318       1.5      phil 
    319       1.6      phil 	/* Is there help? */
    320       1.6      phil 	if (!help) {
    321       1.6      phil 		mbeep();
    322       1.6      phil 		return;
    323       1.6      phil 	}
    324       1.6      phil 
    325       1.6      phil 	/* Display the help information. */
    326       1.7      phil 	do {
    327       1.5      phil 		if (lineoff < curoff) {
    328       1.5      phil 			help = m->helpstr;
    329       1.5      phil 			curoff = 0;
    330       1.5      phil 		}
    331       1.5      phil 		while (*help && curoff < lineoff) {
    332       1.5      phil 			if (*help == '\n')
    333       1.5      phil 				curoff++;
    334       1.5      phil 			help++;
    335       1.5      phil 		}
    336       1.5      phil 
    337       1.5      phil 		wclear(stdscr);
    338       1.5      phil 		mvwaddstr (stdscr, 0, 0,
    339       1.9      phil 			"Help: exit: x,  page up: u <, page down: d >");
    340       1.5      phil 		mvwaddstr (stdscr, 2, 0, help);
    341       1.5      phil 		wmove (stdscr, 1, 0);
    342       1.5      phil 	  	wrefresh(stdscr);
    343       1.5      phil 
    344       1.5      phil 		do {
    345      1.29     blymn 			winin = wgetch(stdscr);
    346      1.29     blymn 			if (winin < KEY_MIN)
    347       1.9      phil 				winin = tolower(winin);
    348       1.5      phil 			again = 0;
    349       1.5      phil 			switch (winin) {
    350       1.5      phil 				case '<':
    351       1.5      phil 				case 'u':
    352      1.29     blymn 				case KEY_UP:
    353      1.29     blymn 				case KEY_LEFT:
    354      1.29     blymn 				case KEY_PPAGE:
    355       1.5      phil 					if (lineoff)
    356       1.5      phil 						lineoff -= max_lines - 2;
    357       1.7      phil 					else
    358       1.7      phil 						again = 1;
    359       1.5      phil 					break;
    360       1.5      phil 				case '>':
    361       1.5      phil 				case 'd':
    362      1.29     blymn 				case KEY_DOWN:
    363      1.29     blymn 				case KEY_RIGHT:
    364      1.29     blymn 				case KEY_NPAGE:
    365       1.5      phil 					if (*help)
    366       1.5      phil 						lineoff += max_lines - 2;
    367       1.7      phil 					else
    368       1.7      phil 						again = 1;
    369       1.5      phil 					break;
    370      1.10      phil 				case 'q':
    371      1.10      phil 					break;
    372       1.9      phil 				case 'x':
    373      1.10      phil 					winin = 'q';
    374       1.5      phil 					break;
    375       1.5      phil 				default:
    376       1.5      phil 					again = 1;
    377       1.5      phil 			}
    378       1.9      phil 			if (again)
    379       1.9      phil 				mbeep();
    380       1.5      phil 		} while (again);
    381       1.5      phil 	} while (winin != 'q');
    382       1.5      phil 
    383       1.5      phil 	/* Restore current menu */
    384       1.5      phil 	wclear(stdscr);
    385       1.5      phil 	wrefresh(stdscr);
    386      1.14      phil 	if (m->post_act)
    387      1.14      phil 		(*m->post_act)();
    388       1.5      phil }
    389       1.5      phil 
    390      1.23   hubertf static void
    391      1.23   hubertf process_req (struct menudesc *m, int num, int req)
    392       1.1      phil {
    393       1.1      phil 	int ch;
    394      1.14      phil 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1 );
    395       1.7      phil 	int refresh = 0;
    396       1.9      phil 	int scroll_sel = 0;
    397       1.1      phil 
    398       1.1      phil 	if (req == REQ_EXECUTE)
    399       1.1      phil 		return;
    400       1.7      phil 
    401       1.1      phil 	else if (req == REQ_NEXT_ITEM) {
    402       1.7      phil 		if (m->cursel < m->numopts + hasexit - 1) {
    403       1.1      phil 			m->cursel++;
    404       1.9      phil 			scroll_sel = 1;
    405       1.7      phil 			refresh = 1;
    406      1.14      phil 			if (m->mopt & MC_SCROLL &&
    407      1.12      phil 			    m->cursel >= m->topline + m->h -1 )
    408      1.11      phil 				m->topline += 1;
    409       1.7      phil 		} else
    410       1.1      phil 			mbeep();
    411       1.7      phil 
    412       1.1      phil 	} else if (req == REQ_PREV_ITEM) {
    413       1.7      phil 		if (m->cursel > 0) {
    414       1.1      phil 			m->cursel--;
    415       1.9      phil 			scroll_sel = 1;
    416       1.7      phil 			refresh = 1;
    417      1.11      phil 			if (m->cursel < m->topline )
    418      1.11      phil 				m->topline -= 1;
    419       1.7      phil 		} else
    420       1.1      phil 			mbeep();
    421       1.3      phil 
    422       1.3      phil 	} else if (req == REQ_REDISPLAY) {
    423       1.3      phil 		wclear(stdscr);
    424       1.3      phil 		wrefresh(stdscr);
    425      1.14      phil 		if (m->post_act)
    426      1.14      phil 			(*m->post_act)();
    427       1.7      phil 		refresh = 1;
    428       1.3      phil 
    429       1.9      phil 	} else if (req == REQ_HELP) {
    430       1.5      phil 		process_help (m, num);
    431       1.7      phil 		refresh = 1;
    432       1.7      phil 
    433       1.9      phil 	} else if (req == REQ_SCROLLUP) {
    434      1.14      phil 		if (!(m->mopt & MC_SCROLL))
    435       1.9      phil 			mbeep();
    436       1.9      phil 		else if (m->topline == 0)
    437       1.7      phil 			mbeep();
    438       1.7      phil 		else {
    439      1.14      phil 			m->topline = MAX(0,m->topline-m->h+1);
    440      1.23   hubertf 			m->cursel = MAX(0, m->cursel-m->h+1);
    441       1.7      phil 			wclear (m->mw);
    442       1.7      phil 			refresh = 1;
    443       1.7      phil 		}
    444       1.7      phil 
    445       1.9      phil 	} else if (req == REQ_SCROLLDOWN) {
    446      1.14      phil 		if (!(m->mopt & MC_SCROLL))
    447       1.9      phil 			mbeep();
    448      1.13      phil 		else if (m->topline + m->h - 1 >= m->numopts + hasexit)
    449       1.7      phil 			mbeep();
    450       1.7      phil 		else {
    451      1.13      phil 			m->topline = MIN(m->topline+m->h-1,
    452      1.13      phil 					 m->numopts+hasexit-m->h+1);
    453      1.23   hubertf 			m->cursel = MIN(m->numopts-1, m->cursel+m->h-1);
    454       1.7      phil 			wclear (m->mw);
    455       1.7      phil 			refresh = 1;
    456       1.7      phil 		}
    457       1.7      phil 
    458       1.1      phil 	} else {
    459       1.9      phil 		ch = req;
    460       1.7      phil 		if (ch == 'x' && hasexit) {
    461       1.1      phil 			m->cursel = m->numopts;
    462       1.9      phil 			scroll_sel = 1;
    463       1.7      phil 			refresh = 1;
    464      1.24      phil 		} else
    465      1.24      phil 			if (!(m->mopt & MC_NOSHORTCUT)) {
    466      1.23   hubertf 				if (ch > 'z')
    467      1.23   hubertf 					ch = 255;
    468      1.23   hubertf 				if (ch >= 'a') {
    469      1.23   hubertf 					if (ch > 'x') ch--;
    470      1.24      phil 				   		ch = ch - 'a';
    471      1.23   hubertf 				} else
    472      1.23   hubertf 					ch = 25 + ch - 'A';
    473      1.23   hubertf 				if (ch < 0 || ch >= m->numopts)
    474      1.23   hubertf 					mbeep();
    475      1.23   hubertf 				else {
    476      1.23   hubertf 					m->cursel = ch;
    477      1.23   hubertf 					scroll_sel = 1;
    478      1.23   hubertf 					refresh = 1;
    479      1.23   hubertf 				}
    480      1.24      phil 			} else
    481      1.24      phil 				mbeep();
    482       1.1      phil 	}
    483       1.9      phil 
    484      1.14      phil 	if (m->mopt & MC_SCROLL && scroll_sel) {
    485       1.9      phil 		while (m->cursel >= m->topline + m->h -1 )
    486      1.13      phil 			m->topline = MIN(m->topline+m->h-1,
    487      1.13      phil 					 m->numopts+hasexit-m->h+1);
    488       1.9      phil 		while (m->cursel < m->topline)
    489      1.13      phil 			m->topline = MAX(0,m->topline-m->h+1);
    490       1.9      phil 	}
    491       1.9      phil 
    492       1.7      phil 	if (refresh) {
    493       1.7      phil 		post_menu (m);
    494       1.7      phil 		wrefresh (m->mw);
    495       1.1      phil 	}
    496       1.1      phil }
    497       1.1      phil 
    498      1.23   hubertf int
    499      1.23   hubertf menu_init (void)
    500      1.17       cgd {
    501      1.17       cgd 
    502      1.17       cgd 	if (__menu_init)
    503      1.17       cgd 		return 0;
    504      1.17       cgd 
    505      1.17       cgd 	if (initscr() == NULL)
    506      1.17       cgd 		return 1;
    507      1.17       cgd 
    508      1.17       cgd 	cbreak();
    509      1.17       cgd 	noecho();
    510      1.26     perry 
    511      1.26     perry 	/* XXX Should be configurable but it almost isn't worth it. */
    512      1.26     perry 	if (has_colors()) {
    513      1.26     perry 		start_color();
    514      1.26     perry 		init_pair(1, COLOR_WHITE, COLOR_BLUE);
    515      1.26     perry 		bkgd(COLOR_PAIR(1));
    516      1.26     perry 		attrset(COLOR_PAIR(1));
    517      1.26     perry 	}
    518      1.26     perry 
    519      1.17       cgd 	max_lines = getmaxy(stdscr);
    520      1.17       cgd 	max_cols = getmaxx(stdscr);
    521      1.29     blymn 	keypad(stdscr, TRUE);
    522      1.17       cgd #ifdef DYNAMIC_MENUS
    523      1.17       cgd 	num_menus = DYN_INIT_NUM;
    524      1.17       cgd 	while (num_menus < DYN_MENU_START)
    525      1.17       cgd 		num_menus *= 2;
    526      1.17       cgd 	menus = (menudesc *) malloc(sizeof(menudesc)*num_menus);
    527      1.17       cgd 	if (menus == NULL)
    528      1.17       cgd 		return 2;
    529      1.17       cgd 	(void) memset ((void *)menus, 0, sizeof(menudesc)*num_menus);
    530      1.17       cgd 	(void) memcpy ((void *)menus, (void *)menu_def,
    531      1.17       cgd 		sizeof(menudesc)*DYN_MENU_START);
    532      1.17       cgd 	 num_avail = num_menus - DYN_MENU_START;
    533      1.17       cgd #endif
    534      1.17       cgd 
    535      1.17       cgd 	__menu_init = 1;
    536      1.17       cgd 	return (0);
    537      1.17       cgd }
    538      1.17       cgd 
    539      1.23   hubertf void
    540      1.23   hubertf process_menu (int num)
    541       1.1      phil {
    542       1.1      phil 	int sel = 0;
    543       1.1      phil 	int req, done;
    544       1.1      phil 	int last_num;
    545       1.1      phil 
    546      1.14      phil 	struct menudesc *m;
    547      1.14      phil 
    548      1.14      phil 	m = &menus[num];
    549       1.1      phil 
    550       1.1      phil 	done = FALSE;
    551       1.1      phil 
    552       1.1      phil 	/* Initialize? */
    553      1.17       cgd 	if (menu_init()) {
    554      1.17       cgd 		__menu_initerror();
    555      1.17       cgd 		return;
    556       1.1      phil 	}
    557      1.17       cgd 
    558       1.1      phil 	if (__m_endwin) {
    559       1.7      phil      		wclear(stdscr);
    560       1.1      phil 		wrefresh(stdscr);
    561       1.1      phil 		__m_endwin = 0;
    562       1.1      phil 	}
    563       1.1      phil 	if (m->mw == NULL)
    564       1.1      phil 		init_menu (m);
    565       1.1      phil 
    566       1.7      phil 	/* Always preselect option 0 and display from 0! */
    567       1.1      phil 	m->cursel = 0;
    568       1.7      phil 	m->topline = 0;
    569       1.1      phil 
    570       1.1      phil 	while (!done) {
    571       1.1      phil 		last_num = num;
    572       1.1      phil 		if (__m_endwin) {
    573       1.1      phil 			wclear(stdscr);
    574       1.1      phil 			wrefresh(stdscr);
    575       1.1      phil 			__m_endwin = 0;
    576       1.1      phil 		}
    577       1.1      phil 		/* Process the display action */
    578      1.14      phil 		if (m->post_act)
    579      1.14      phil 			(*m->post_act)();
    580       1.1      phil 		post_menu (m);
    581       1.1      phil 		wrefresh (m->mw);
    582       1.1      phil 
    583       1.1      phil 		while ((req = menucmd (m->mw)) != REQ_EXECUTE)
    584       1.4      phil 			process_req (m, num, req);
    585       1.1      phil 
    586       1.1      phil 		sel = m->cursel;
    587       1.1      phil 		wclear (m->mw);
    588       1.1      phil 		wrefresh (m->mw);
    589       1.1      phil 
    590       1.1      phil 		/* Process the items */
    591      1.14      phil 		if (sel < m->numopts) {
    592      1.14      phil 			if (m->opts[sel].opt_flags & OPT_ENDWIN) {
    593      1.14      phil 				endwin();
    594      1.14      phil 				__m_endwin = 1;
    595      1.14      phil 			}
    596      1.14      phil 			if (m->opts[sel].opt_action)
    597      1.23   hubertf 				done = (*m->opts[sel].opt_action)(m);
    598  1.29.2.1       jmc 
    599  1.29.2.1       jmc 			/*
    600  1.29.2.1       jmc 			 * The opt_action function may allocate new menus,
    601  1.29.2.1       jmc 			 * which may make m a stale pointer into the old
    602  1.29.2.1       jmc 			 * menus array. So re-target it.
    603  1.29.2.1       jmc 			 */
    604  1.29.2.1       jmc 			m = &menus[num];
    605  1.29.2.1       jmc 
    606      1.14      phil 			if (m->opts[sel].opt_menu != -1) {
    607      1.14      phil 				if (m->opts[sel].opt_flags & OPT_SUB)
    608      1.14      phil 					process_menu (m->opts[sel].opt_menu);
    609      1.14      phil 				else
    610      1.14      phil 					num = m->opts[sel].opt_menu;
    611      1.14      phil 			}
    612      1.15      phil 
    613      1.15      phil                         if (m->opts[sel].opt_flags & OPT_EXIT)
    614      1.15      phil                                 done = TRUE;
    615      1.15      phil 
    616      1.14      phil 		} else
    617       1.1      phil 			done = TRUE;
    618       1.1      phil 
    619       1.1      phil 		/* Reselect m just in case */
    620       1.1      phil 		if (num != last_num) {
    621       1.1      phil 			m = &menus[num];
    622      1.14      phil 
    623       1.1      phil 			/* Initialize? */
    624       1.1      phil 			if (m->mw == NULL)
    625       1.1      phil 				init_menu (m);
    626      1.14      phil 			if (m->post_act)
    627      1.14      phil 				(*m->post_act)();
    628       1.1      phil 		}
    629       1.1      phil 	}
    630       1.1      phil 
    631       1.1      phil 	/* Process the exit action */
    632      1.14      phil 	if (m->exit_act)
    633      1.14      phil 		(*m->exit_act)();
    634       1.1      phil }
    635      1.13      phil 
    636      1.13      phil /* Control L is end of standard routines, remaining only for dynamic. */
    638      1.13      phil 
    639      1.13      phil /* Beginning of routines for dynamic menus. */
    640      1.14      phil 
    641      1.14      phil /* local prototypes */
    642      1.14      phil static int double_menus (void);
    643      1.24      phil 
    644      1.23   hubertf static int
    645      1.14      phil double_menus (void)
    646      1.14      phil {
    647      1.14      phil 	menudesc *temp;
    648      1.14      phil 
    649      1.14      phil 	temp  = (menudesc *) malloc(sizeof(menudesc)*num_menus*2);
    650      1.14      phil 	if (temp == NULL)
    651      1.14      phil 		return 0;
    652      1.14      phil 	(void) memset ((void *)temp, 0,
    653      1.14      phil 		sizeof(menudesc)*num_menus*2);
    654      1.14      phil 	(void) memcpy ((void *)temp, (void *)menus,
    655      1.14      phil 		sizeof(menudesc)*num_menus);
    656      1.14      phil 	free (menus);
    657      1.14      phil 	menus = temp;
    658      1.14      phil 	num_avail = num_menus;
    659      1.14      phil 	num_menus *= 2;
    660      1.14      phil 
    661      1.14      phil 	return 1;
    662      1.14      phil }
    663      1.23   hubertf 
    664      1.23   hubertf int
    665      1.24      phil new_menu (char * title, menu_ent * opts, int numopts,
    666      1.25      phil 	int x, int y, int h, int w, int mopt,
    667      1.14      phil 	void (*post_act)(void), void (*exit_act)(void), char * help)
    668      1.14      phil {
    669       1.7      phil 	int ix;
    670      1.14      phil 
    671      1.14      phil 	/* Check for free menu entry. */
    672      1.14      phil 	if (num_avail == 0)
    673      1.14      phil 		if (!double_menus ())
    674      1.14      phil 			return -1;
    675      1.14      phil 
    676      1.14      phil 	/* Find free menu entry. */
    677      1.14      phil 	for (ix = DYN_MENU_START; ix < num_menus && menus[ix].mopt & MC_VALID;
    678      1.14      phil 		ix++) /* do  nothing */;
    679      1.14      phil 
    680      1.14      phil 	/* if ix == num_menus ... panic */
    681      1.14      phil 
    682      1.24      phil 	/* Set Entries */
    683      1.14      phil 	menus[ix].title = title ? title : "";
    684      1.14      phil 	menus[ix].opts = opts;
    685      1.14      phil 	menus[ix].numopts = numopts;
    686      1.14      phil 	menus[ix].x = x;
    687      1.14      phil 	menus[ix].y = y;
    688      1.14      phil 	menus[ix].h = h;
    689      1.14      phil 	menus[ix].w = w;
    690      1.14      phil 	menus[ix].mopt = mopt | MC_VALID;
    691      1.14      phil 	menus[ix].post_act = post_act;
    692      1.14      phil 	menus[ix].exit_act = exit_act;
    693      1.21       cgd 	menus[ix].helpstr  = help;
    694      1.14      phil 	menus[ix].exitstr  = "Exit";
    695      1.14      phil 
    696      1.14      phil 	init_menu (&menus[ix]);
    697      1.14      phil 
    698      1.14      phil 	return ix;
    699      1.14      phil }
    700      1.23   hubertf 
    701      1.23   hubertf void
    702      1.14      phil free_menu (int menu_no)
    703      1.14      phil {
    704      1.14      phil 	if (menu_no < num_menus) {
    705      1.14      phil 		menus[menu_no].mopt &= ~MC_VALID;
    706      1.14      phil 		if (menus[menu_no].mw != NULL)
    707      1.14      phil 			delwin (menus[menu_no].mw);
    708      1.14      phil 	}
    709                     }
    710