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