Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.36
      1  1.36       dsl /*	$NetBSD: menu_sys.def,v 1.36 2003/06/03 08:49:53 dsl 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.36       dsl static void init_menu(struct menudesc *m);
     72  1.36       dsl static char opt_ch(int op_no);
     73  1.36       dsl static void draw_menu(struct menudesc *m);
     74  1.36       dsl static void process_help(struct menudesc *m, int num);
     75  1.36       dsl static void process_req(struct menudesc *m, int num, int req);
     76  1.36       dsl static int menucmd(WINDOW *w);
     77   1.1      phil 
     78   1.1      phil #ifndef NULL
     79  1.36       dsl #define NULL 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.35       dsl 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.31       dsl 		case '\016':  /* Control-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.36       dsl 			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.31       dsl 		case KEY_LEFT:
    108   1.9      phil 			return REQ_SCROLLUP;
    109  1.31       dsl 		case '\026':  /* Control-V */
    110   1.9      phil 		case '>':
    111  1.11      phil 		case ' ':
    112  1.29     blymn 		case KEY_NPAGE:
    113  1.31       dsl 		case KEY_RIGHT:
    114   1.9      phil 			return REQ_SCROLLDOWN;
    115   1.9      phil 		case '?':
    116   1.9      phil 			return REQ_HELP;
    117  1.29     blymn 		case '\033': /* esc-v is scroll down */
    118  1.29     blymn 			ch = wgetch(w);
    119  1.29     blymn 			if (ch == 'v')
    120  1.29     blymn 				return REQ_SCROLLUP;
    121  1.29     blymn 			else
    122  1.29     blymn 				ch = 0; /* zap char so we beep */
    123   1.1      phil 		}
    124   1.1      phil 
    125   1.9      phil 		if (isalpha(ch))
    126  1.36       dsl 			return ch;
    127   1.1      phil 
    128   1.1      phil 		mbeep();
    129   1.1      phil 		wrefresh(w);
    130   1.1      phil 	}
    131   1.1      phil }
    132   1.1      phil 
    133  1.23   hubertf static void
    134  1.35       dsl init_menu(struct menudesc *m)
    135   1.1      phil {
    136  1.18       cgd 	int wmax;
    137  1.18       cgd 	int hadd, wadd, exithadd;
    138  1.14      phil 	int i;
    139   1.1      phil 
    140  1.18       cgd 	hadd = ((m->mopt & MC_NOBOX) ? 0 : 2);
    141  1.18       cgd 	wadd = ((m->mopt & MC_NOBOX) ? 2 : 4);
    142  1.18       cgd 
    143  1.18       cgd 	hadd += strlen(m->title) != 0 ? 2 : 0;
    144  1.18       cgd 	exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
    145  1.18       cgd 
    146  1.18       cgd 	wmax = strlen(m->title);
    147   1.1      phil 
    148   1.7      phil 	/* Calculate h? h == number of visible options. */
    149   1.1      phil 	if (m->h == 0) {
    150  1.18       cgd 		m->h = m->numopts + exithadd;
    151  1.36       dsl 		if (m->h + m->y + hadd >= max_lines && (m->mopt & MC_SCROLL)) {
    152  1.36       dsl 			hadd++;
    153  1.36       dsl 			m->h = max_lines - m->y - hadd;
    154  1.36       dsl 		}
    155   1.1      phil 	}
    156   1.7      phil 
    157   1.7      phil 	/* Check window heights and set scrolling */
    158  1.18       cgd 	if (m->h < m->numopts + exithadd) {
    159  1.14      phil 		if (!(m->mopt & MC_SCROLL) || m->h < 3) {
    160   1.7      phil 			endwin();
    161  1.35       dsl 			(void)fprintf(stderr,
    162  1.17       cgd 				"Window too short for menu \"%s\"\n",
    163   1.9      phil 				m->title);
    164   1.7      phil 			exit(1);
    165   1.7      phil 		}
    166   1.7      phil 	} else
    167  1.14      phil 		m->mopt &= ~MC_SCROLL;
    168   1.1      phil 
    169   1.9      phil 	/* check for screen fit */
    170  1.18       cgd 	if (m->y + m->h + hadd > max_lines) {
    171   1.9      phil 		endwin();
    172  1.35       dsl 		(void)fprintf(stderr,
    173  1.17       cgd 			"Screen too short for menu \"%s\"\n", m->title);
    174   1.9      phil 		exit(1);
    175   1.9      phil 
    176   1.9      phil 	}
    177   1.9      phil 
    178   1.1      phil 	/* Calculate w? */
    179   1.1      phil 	if (m->w == 0) {
    180  1.14      phil 		if (m->mopt & MC_SCROLL)
    181  1.18       cgd 			wmax = MAX(wmax,strlen(scrolltext));
    182  1.35       dsl 		for (i = 0; i < m->numopts; i++)
    183  1.35       dsl 			wmax = MAX(wmax, strlen(m->opts[i].opt_name) + 3);
    184  1.18       cgd 		m->w = wmax;
    185   1.1      phil 	}
    186   1.1      phil 
    187  1.17       cgd 	/* check and adjust for screen fit */
    188  1.18       cgd 	if (m->w + wadd > max_cols) {
    189  1.17       cgd 		endwin();
    190  1.35       dsl 		(void)fprintf(stderr,
    191  1.17       cgd 			"Screen too narrow for menu \"%s\"\n", m->title);
    192  1.17       cgd 		exit(1);
    193  1.17       cgd 
    194  1.17       cgd 	}
    195  1.20       cgd 	if (m->x == -1)
    196  1.20       cgd 		m->x = (max_cols - (m->w + wadd)) / 2;	/* center */
    197  1.20       cgd 	else if (m->x + m->w + wadd > max_cols)
    198  1.18       cgd 		m->x = max_cols - (m->w + wadd);
    199  1.17       cgd 
    200   1.1      phil 	/* Get the windows. */
    201  1.36       dsl 	m->mw = newwin(m->h + hadd, m->w + wadd, m->y, m->x);
    202  1.29     blymn 	keypad(m->mw, TRUE); /* enable multi-key assembling for win */
    203   1.1      phil 
    204   1.1      phil 	if (m->mw == NULL) {
    205   1.1      phil 		endwin();
    206  1.35       dsl 		(void)fprintf(stderr,
    207  1.18       cgd 			"Could not create window for menu \"%s\"\n", m->title);
    208   1.1      phil 		exit(1);
    209  1.26     perry 	}
    210  1.26     perry 
    211  1.26     perry 	/* XXX is it even worth doing this right? */
    212  1.26     perry 	if (has_colors()) {
    213  1.26     perry 		wbkgd(m->mw, COLOR_PAIR(1));
    214  1.26     perry 		wattrset(m->mw, COLOR_PAIR(1));
    215  1.26     perry 	}
    216   1.1      phil }
    217   1.1      phil 
    218  1.23   hubertf static char
    219  1.35       dsl opt_ch(int op_no)
    220  1.14      phil {
    221  1.14      phil 	char c;
    222  1.14      phil 	if (op_no < 25) {
    223  1.14      phil 		c = 'a' + op_no;
    224  1.35       dsl 		if (c >= 'x')
    225  1.35       dsl 			c++;
    226  1.14      phil 	} else
    227  1.14      phil 		c = 'A' + op_no - 25;
    228  1.35       dsl 	return c;
    229  1.14      phil }
    230  1.14      phil 
    231  1.23   hubertf static void
    232  1.36       dsl draw_menu_line(struct menudesc *m, int i, int cury, char opt, const char *text)
    233  1.36       dsl {
    234  1.36       dsl 	int hasbox = m->mopt & MC_NOBOX ? 0 : 1;
    235  1.36       dsl 
    236  1.36       dsl 	if (m->cursel == i) {
    237  1.36       dsl 		mvwaddstr(m->mw, cury, hasbox, ">");
    238  1.36       dsl 		wstandout(m->mw);
    239  1.36       dsl 	} else
    240  1.36       dsl 		mvwaddstr(m->mw, cury, hasbox, " ");
    241  1.36       dsl 	if (!(m->mopt & MC_NOSHORTCUT))
    242  1.36       dsl 		wprintw(m->mw, "%c: ", opt);
    243  1.36       dsl 	waddstr(m->mw, text);
    244  1.36       dsl 	if (m->cursel == i)
    245  1.36       dsl 		wstandend(m->mw);
    246  1.36       dsl }
    247  1.36       dsl 
    248  1.36       dsl static void
    249  1.36       dsl draw_menu(struct menudesc *m)
    250   1.1      phil {
    251   1.1      phil 	int i;
    252  1.36       dsl 	int hasbox, cury, maxy;
    253   1.1      phil 	int tadd;
    254   1.1      phil 
    255  1.14      phil 	if (m->mopt & MC_NOBOX) {
    256   1.1      phil 		cury = 0;
    257   1.7      phil 		maxy = m->h;
    258   1.1      phil 		hasbox = 0;
    259   1.1      phil 	} else {
    260   1.1      phil 		cury = 1;
    261  1.35       dsl 		maxy = m->h + 1;
    262   1.1      phil 		hasbox = 1;
    263   1.1      phil 	}
    264   1.1      phil 
    265   1.9      phil 	/* Clear the window */
    266  1.35       dsl 	wclear(m->mw);
    267   1.9      phil 
    268   1.1      phil 	tadd = strlen(m->title) ? 2 : 0;
    269   1.1      phil 
    270   1.1      phil 	if (tadd) {
    271  1.36       dsl 		mvwaddstr(m->mw, hasbox, hasbox, " ");
    272  1.36       dsl 		mvwaddstr(m->mw, hasbox, hasbox + 1, m->title);
    273  1.36       dsl 		cury += tadd;
    274  1.36       dsl 		maxy += tadd;
    275   1.1      phil 	}
    276   1.1      phil 
    277  1.36       dsl 	for (i = m->topline; i < m->numopts; i++) {
    278  1.36       dsl 		if (cury >= maxy)
    279  1.36       dsl 			break;
    280  1.36       dsl 		draw_menu_line(m, i, cury++, opt_ch(i), m->opts[i].opt_name);
    281   1.1      phil 	}
    282   1.7      phil 
    283   1.7      phil 	/* Add the exit option. */
    284  1.36       dsl 	if (!(m->mopt & MC_NOEXITOPT) && cury < maxy)
    285  1.36       dsl 		draw_menu_line(m, m->numopts, cury++, 'x', m->exitstr);
    286   1.7      phil 
    287   1.7      phil 	/* Add the scroll line */
    288  1.36       dsl 	if (m->mopt & MC_SCROLL)
    289  1.35       dsl 		mvwaddstr(m->mw, cury, hasbox, scrolltext);
    290   1.7      phil 
    291   1.7      phil 	/* Add the box. */
    292  1.14      phil 	if (!(m->mopt & MC_NOBOX))
    293  1.26     perry 		box(m->mw, 0, 0);
    294   1.1      phil 
    295  1.36       dsl 	wmove(m->mw, tadd + hasbox + m->cursel - m->topline, hasbox);
    296  1.36       dsl 	wrefresh(m->mw);
    297   1.1      phil }
    298   1.1      phil 
    299  1.23   hubertf static void
    300  1.30  christos /*ARGSUSED*/
    301  1.35       dsl process_help(struct menudesc *m, int num)
    302   1.5      phil {
    303  1.34       dsl 	const char *help = m->helpstr;
    304   1.7      phil 	int lineoff = 0;
    305   1.5      phil 	int curoff = 0;
    306   1.5      phil 	int again;
    307   1.7      phil 	int winin;
    308   1.5      phil 
    309   1.6      phil 	/* Is there help? */
    310   1.6      phil 	if (!help) {
    311   1.6      phil 		mbeep();
    312   1.6      phil 		return;
    313   1.6      phil 	}
    314   1.6      phil 
    315   1.6      phil 	/* Display the help information. */
    316   1.7      phil 	do {
    317   1.5      phil 		if (lineoff < curoff) {
    318   1.5      phil 			help = m->helpstr;
    319   1.5      phil 			curoff = 0;
    320   1.5      phil 		}
    321   1.5      phil 		while (*help && curoff < lineoff) {
    322   1.5      phil 			if (*help == '\n')
    323   1.5      phil 				curoff++;
    324   1.5      phil 			help++;
    325   1.5      phil 		}
    326   1.5      phil 
    327   1.5      phil 		wclear(stdscr);
    328  1.35       dsl 		mvwaddstr(stdscr, 0, 0,
    329   1.9      phil 			"Help: exit: x,  page up: u <, page down: d >");
    330  1.35       dsl 		mvwaddstr(stdscr, 2, 0, help);
    331  1.35       dsl 		wmove(stdscr, 1, 0);
    332  1.36       dsl 		wrefresh(stdscr);
    333   1.5      phil 
    334   1.5      phil 		do {
    335  1.29     blymn 			winin = wgetch(stdscr);
    336  1.29     blymn 			if (winin < KEY_MIN)
    337   1.9      phil 				winin = tolower(winin);
    338   1.5      phil 			again = 0;
    339   1.5      phil 			switch (winin) {
    340   1.5      phil 				case '<':
    341   1.5      phil 				case 'u':
    342  1.29     blymn 				case KEY_UP:
    343  1.29     blymn 				case KEY_LEFT:
    344  1.29     blymn 				case KEY_PPAGE:
    345   1.5      phil 					if (lineoff)
    346   1.5      phil 						lineoff -= max_lines - 2;
    347   1.7      phil 					else
    348   1.7      phil 						again = 1;
    349   1.5      phil 					break;
    350   1.5      phil 				case '>':
    351   1.5      phil 				case 'd':
    352  1.29     blymn 				case KEY_DOWN:
    353  1.29     blymn 				case KEY_RIGHT:
    354  1.29     blymn 				case KEY_NPAGE:
    355   1.5      phil 					if (*help)
    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.10      phil 				case 'q':
    361  1.10      phil 					break;
    362   1.9      phil 				case 'x':
    363  1.10      phil 					winin = 'q';
    364   1.5      phil 					break;
    365   1.5      phil 				default:
    366   1.5      phil 					again = 1;
    367   1.5      phil 			}
    368   1.9      phil 			if (again)
    369   1.9      phil 				mbeep();
    370   1.5      phil 		} while (again);
    371   1.5      phil 	} while (winin != 'q');
    372   1.5      phil 
    373   1.5      phil 	/* Restore current menu */
    374   1.5      phil 	wclear(stdscr);
    375   1.5      phil 	wrefresh(stdscr);
    376  1.14      phil 	if (m->post_act)
    377  1.14      phil 		(*m->post_act)();
    378   1.5      phil }
    379   1.5      phil 
    380  1.23   hubertf static void
    381  1.35       dsl process_req(struct menudesc *m, int num, int req)
    382   1.1      phil {
    383   1.1      phil 	int ch;
    384  1.35       dsl 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
    385   1.1      phil 
    386  1.35       dsl 	switch(req) {
    387  1.35       dsl 
    388  1.35       dsl 	case REQ_EXECUTE:
    389   1.1      phil 		return;
    390   1.7      phil 
    391  1.35       dsl 	case REQ_NEXT_ITEM:
    392  1.35       dsl 		if (m->cursel >= m->numopts + hasexit - 1) {
    393   1.1      phil 			mbeep();
    394  1.35       dsl 			return;
    395  1.35       dsl 		}
    396  1.35       dsl 		m->cursel++;
    397  1.36       dsl 		if (m->mopt & MC_SCROLL && m->cursel >= m->topline + m->h)
    398  1.35       dsl 			m->topline += 1;
    399  1.35       dsl 		break;
    400   1.7      phil 
    401  1.35       dsl 	case REQ_PREV_ITEM:
    402  1.35       dsl 		if (m->cursel <= 0) {
    403   1.1      phil 			mbeep();
    404  1.35       dsl 			return;
    405  1.35       dsl 		}
    406  1.35       dsl 		m->cursel--;
    407  1.35       dsl 		if (m->cursel < m->topline)
    408  1.35       dsl 			m->topline -= 1;
    409  1.35       dsl 		break;
    410   1.3      phil 
    411  1.35       dsl 	case REQ_REDISPLAY:
    412   1.3      phil 		wclear(stdscr);
    413   1.3      phil 		wrefresh(stdscr);
    414  1.14      phil 		if (m->post_act)
    415  1.14      phil 			(*m->post_act)();
    416  1.35       dsl 		break;
    417   1.3      phil 
    418  1.35       dsl 	case REQ_HELP:
    419  1.35       dsl 		process_help(m, num);
    420  1.35       dsl 		break;
    421   1.7      phil 
    422  1.35       dsl 	case REQ_SCROLLUP:
    423  1.35       dsl 		if (m->cursel == 0) {
    424   1.7      phil 			mbeep();
    425  1.35       dsl 			return;
    426   1.7      phil 		}
    427  1.36       dsl 		m->topline = MAX(0, m->topline - m->h);
    428  1.36       dsl 		m->cursel = MAX(0, m->cursel - m->h);
    429  1.35       dsl 		wclear(m->mw);
    430  1.35       dsl 		break;
    431   1.7      phil 
    432  1.35       dsl 	case REQ_SCROLLDOWN:
    433  1.35       dsl 		if (m->cursel >= m->numopts + hasexit - 1) {
    434   1.7      phil 			mbeep();
    435  1.35       dsl 			return;
    436   1.7      phil 		}
    437  1.36       dsl 		m->topline = MIN(m->topline + m->h,
    438  1.36       dsl 				 m->numopts + hasexit - m->h);
    439  1.36       dsl 		m->cursel = MIN(m->numopts + hasexit - 1, m->cursel + m->h);
    440  1.35       dsl 		wclear(m->mw);
    441  1.35       dsl 		break;
    442   1.7      phil 
    443  1.35       dsl 	default:
    444   1.9      phil 		ch = req;
    445   1.7      phil 		if (ch == 'x' && hasexit) {
    446   1.1      phil 			m->cursel = m->numopts;
    447  1.35       dsl 			break;
    448  1.35       dsl 		}
    449  1.35       dsl 		if (m->mopt & MC_NOSHORTCUT) {
    450  1.35       dsl 			mbeep();
    451  1.35       dsl 			return;
    452  1.35       dsl 		}
    453  1.35       dsl 		if (ch > 'z')
    454  1.35       dsl 			ch = 255;
    455  1.35       dsl 		if (ch >= 'a') {
    456  1.35       dsl 			if (ch > 'x')
    457  1.35       dsl 				ch--;
    458  1.35       dsl 			ch = ch - 'a';
    459  1.35       dsl 		} else
    460  1.35       dsl 			ch = 25 + ch - 'A';
    461  1.35       dsl 		if (ch < 0 || ch >= m->numopts) {
    462  1.35       dsl 			mbeep();
    463  1.35       dsl 			return;
    464  1.35       dsl 		}
    465  1.35       dsl 		m->cursel = ch;
    466   1.1      phil 	}
    467   1.9      phil 
    468  1.36       dsl 	while (m->cursel >= m->topline + m->h)
    469  1.36       dsl 		m->topline = MIN(m->topline + m->h,
    470  1.36       dsl 				 m->numopts + hasexit - m->h);
    471  1.36       dsl 	while (m->cursel < m->topline)
    472  1.36       dsl 		m->topline = MAX(0, m->topline - m->h);
    473   1.9      phil 
    474  1.36       dsl 	draw_menu(m);
    475   1.1      phil }
    476   1.1      phil 
    477  1.23   hubertf int
    478  1.35       dsl menu_init(void)
    479  1.17       cgd {
    480  1.17       cgd 
    481  1.17       cgd 	if (__menu_init)
    482  1.17       cgd 		return 0;
    483  1.33       dsl 
    484  1.33       dsl #ifdef	USER_MENU_INIT
    485  1.33       dsl 	if (USER_MENU_INIT)
    486  1.33       dsl 		return 1;
    487  1.33       dsl #endif
    488  1.17       cgd 
    489  1.17       cgd 	if (initscr() == NULL)
    490  1.17       cgd 		return 1;
    491  1.17       cgd 
    492  1.17       cgd 	cbreak();
    493  1.17       cgd 	noecho();
    494  1.26     perry 
    495  1.26     perry 	/* XXX Should be configurable but it almost isn't worth it. */
    496  1.26     perry 	if (has_colors()) {
    497  1.26     perry 		start_color();
    498  1.26     perry 		init_pair(1, COLOR_WHITE, COLOR_BLUE);
    499  1.26     perry 		bkgd(COLOR_PAIR(1));
    500  1.26     perry 		attrset(COLOR_PAIR(1));
    501  1.26     perry 	}
    502  1.26     perry 
    503  1.17       cgd 	max_lines = getmaxy(stdscr);
    504  1.17       cgd 	max_cols = getmaxx(stdscr);
    505  1.29     blymn 	keypad(stdscr, TRUE);
    506  1.17       cgd #ifdef DYNAMIC_MENUS
    507  1.17       cgd 	num_menus = DYN_INIT_NUM;
    508  1.17       cgd 	while (num_menus < DYN_MENU_START)
    509  1.17       cgd 		num_menus *= 2;
    510  1.35       dsl 	menus = malloc(sizeof(menudesc) * num_menus);
    511  1.17       cgd 	if (menus == NULL)
    512  1.17       cgd 		return 2;
    513  1.35       dsl 	(void)memset(menus, 0, sizeof(menudesc) * num_menus);
    514  1.36       dsl 	(void)memcpy(menus, menu_def, sizeof(menudesc) * DYN_MENU_START);
    515  1.36       dsl 	num_avail = num_menus - DYN_MENU_START;
    516  1.17       cgd #endif
    517  1.17       cgd 
    518  1.17       cgd 	__menu_init = 1;
    519  1.36       dsl 	return 0;
    520  1.17       cgd }
    521  1.17       cgd 
    522  1.23   hubertf void
    523  1.35       dsl process_menu(int num)
    524   1.1      phil {
    525   1.1      phil 	int sel = 0;
    526   1.1      phil 	int req, done;
    527   1.1      phil 	int last_num;
    528   1.1      phil 
    529  1.14      phil 	struct menudesc *m;
    530  1.14      phil 
    531  1.14      phil 	m = &menus[num];
    532   1.1      phil 
    533   1.1      phil 	done = FALSE;
    534   1.1      phil 
    535   1.1      phil 	/* Initialize? */
    536  1.17       cgd 	if (menu_init()) {
    537  1.17       cgd 		__menu_initerror();
    538  1.17       cgd 		return;
    539   1.1      phil 	}
    540  1.17       cgd 
    541   1.1      phil 	if (__m_endwin) {
    542   1.7      phil      		wclear(stdscr);
    543   1.1      phil 		wrefresh(stdscr);
    544   1.1      phil 		__m_endwin = 0;
    545   1.1      phil 	}
    546   1.1      phil 	if (m->mw == NULL)
    547  1.36       dsl 		init_menu(m);
    548   1.1      phil 
    549   1.7      phil 	/* Always preselect option 0 and display from 0! */
    550   1.1      phil 	m->cursel = 0;
    551   1.7      phil 	m->topline = 0;
    552   1.1      phil 
    553   1.1      phil 	while (!done) {
    554   1.1      phil 		last_num = num;
    555   1.1      phil 		if (__m_endwin) {
    556   1.1      phil 			wclear(stdscr);
    557   1.1      phil 			wrefresh(stdscr);
    558   1.1      phil 			__m_endwin = 0;
    559   1.1      phil 		}
    560   1.1      phil 		/* Process the display action */
    561  1.14      phil 		if (m->post_act)
    562  1.14      phil 			(*m->post_act)();
    563  1.36       dsl 		draw_menu(m);
    564   1.1      phil 
    565  1.36       dsl 		while ((req = menucmd(m->mw)) != REQ_EXECUTE)
    566  1.36       dsl 			process_req(m, num, req);
    567   1.1      phil 
    568   1.1      phil 		sel = m->cursel;
    569  1.35       dsl 		wclear(m->mw);
    570  1.35       dsl 		wrefresh(m->mw);
    571   1.1      phil 
    572   1.1      phil 		/* Process the items */
    573  1.14      phil 		if (sel < m->numopts) {
    574  1.14      phil 			if (m->opts[sel].opt_flags & OPT_ENDWIN) {
    575  1.14      phil 				endwin();
    576  1.14      phil 				__m_endwin = 1;
    577  1.14      phil 			}
    578  1.14      phil 			if (m->opts[sel].opt_action)
    579  1.23   hubertf 				done = (*m->opts[sel].opt_action)(m);
    580  1.14      phil 			if (m->opts[sel].opt_menu != -1) {
    581  1.14      phil 				if (m->opts[sel].opt_flags & OPT_SUB)
    582  1.36       dsl 					process_menu(m->opts[sel].opt_menu);
    583  1.14      phil 				else
    584  1.14      phil 					num = m->opts[sel].opt_menu;
    585  1.14      phil 			}
    586  1.15      phil 
    587  1.15      phil                         if (m->opts[sel].opt_flags & OPT_EXIT)
    588  1.15      phil                                 done = TRUE;
    589  1.15      phil 
    590  1.14      phil 		} else
    591   1.1      phil 			done = TRUE;
    592   1.1      phil 
    593   1.1      phil 		/* Reselect m just in case */
    594   1.1      phil 		if (num != last_num) {
    595   1.1      phil 			m = &menus[num];
    596  1.14      phil 
    597   1.1      phil 			/* Initialize? */
    598   1.1      phil 			if (m->mw == NULL)
    599  1.36       dsl 				init_menu(m);
    600  1.14      phil 			if (m->post_act)
    601  1.14      phil 				(*m->post_act)();
    602   1.1      phil 		}
    603   1.1      phil 	}
    604   1.1      phil 
    605   1.1      phil 	/* Process the exit action */
    606  1.14      phil 	if (m->exit_act)
    607  1.14      phil 		(*m->exit_act)();
    608   1.1      phil }
    609  1.13      phil 
    610  1.13      phil /* Control L is end of standard routines, remaining only for dynamic. */
    612  1.13      phil 
    613  1.13      phil /* Beginning of routines for dynamic menus. */
    614  1.24      phil 
    615  1.35       dsl static int
    616  1.14      phil double_menus(void)
    617  1.14      phil {
    618  1.14      phil 	menudesc *temp;
    619  1.35       dsl 
    620  1.14      phil 	temp  = malloc(sizeof(menudesc) * num_menus * 2);
    621  1.14      phil 	if (temp == NULL)
    622  1.35       dsl 		return 0;
    623  1.35       dsl 	(void) memset(temp, 0, sizeof(menudesc) * num_menus * 2);
    624  1.35       dsl 	(void) memcpy(temp, menus, sizeof(menudesc) * num_menus);
    625  1.14      phil 	free(menus);
    626  1.14      phil 	menus = temp;
    627  1.14      phil 	num_avail = num_menus;
    628  1.14      phil 	num_menus *= 2;
    629  1.14      phil 
    630  1.14      phil 	return 1;
    631  1.14      phil }
    632  1.23   hubertf 
    633  1.35       dsl int
    634  1.24      phil new_menu(char * title, menu_ent * opts, int numopts,
    635  1.25      phil 	int x, int y, int h, int w, int mopt,
    636  1.14      phil 	void (*post_act)(void), void (*exit_act)(void), char * help)
    637  1.14      phil {
    638   1.7      phil 	int ix;
    639  1.14      phil 
    640  1.14      phil 	/* Check for free menu entry. */
    641  1.35       dsl 	if (num_avail == 0)
    642  1.14      phil 		if (!double_menus())
    643  1.14      phil 			return -1;
    644  1.14      phil 
    645  1.14      phil 	/* Find free menu entry. */
    646  1.14      phil 	for (ix = DYN_MENU_START; ix < num_menus && menus[ix].mopt & MC_VALID;
    647  1.14      phil 		ix++) /* do  nothing */;
    648  1.14      phil 
    649  1.14      phil 	/* if ix == num_menus ... panic */
    650  1.14      phil 
    651  1.24      phil 	/* Set Entries */
    652  1.14      phil 	menus[ix].title = title ? title : "";
    653  1.14      phil 	menus[ix].opts = opts;
    654  1.14      phil 	menus[ix].numopts = numopts;
    655  1.14      phil 	menus[ix].x = x;
    656  1.14      phil 	menus[ix].y = y;
    657  1.14      phil 	menus[ix].h = h;
    658  1.14      phil 	menus[ix].w = w;
    659  1.14      phil 	menus[ix].mopt = mopt | MC_VALID;
    660  1.14      phil 	menus[ix].post_act = post_act;
    661  1.14      phil 	menus[ix].exit_act = exit_act;
    662  1.21       cgd 	menus[ix].helpstr  = help;
    663  1.14      phil 	menus[ix].exitstr  = "Exit";
    664  1.35       dsl 
    665  1.14      phil 	init_menu(&menus[ix]);
    666  1.14      phil 
    667  1.14      phil 	return ix;
    668  1.14      phil }
    669  1.23   hubertf 
    670  1.35       dsl void
    671  1.14      phil free_menu(int menu_no)
    672  1.14      phil {
    673  1.14      phil 	if (menu_no < num_menus) {
    674  1.14      phil 		menus[menu_no].mopt &= ~MC_VALID;
    675  1.35       dsl 		if (menus[menu_no].mw != NULL)
    676  1.14      phil 			delwin(menus[menu_no].mw);
    677  1.14      phil 	}
    678                 }
    679