menu_sys.def revision 1.7
1/*	$NetBSD: menu_sys.def,v 1.7 1998/06/25 09:58:57 phil 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
49#define KEYPAD_DOWN_ARROW 256
50#define KEYPAD_UP_ARROW   257
51
52#define MAX(x,y) ((x)>(y)?(x):(y))
53#define MIN(x,y) ((x)<(y)?(x):(y))
54
55/* Initialization state. */
56static int __menu_init = 0;
57int __m_endwin = 0;
58static int max_lines = 0;
59static char *scrolltext = " <: page up, >: page down";
60
61/* prototypes for in here! */
62
63static void init_menu (struct menudesc *m);
64static void post_menu (struct menudesc *m);
65static void process_help (struct menudesc *m, int num);
66static void process_req (struct menudesc *m, int num, int req);
67static void mbeep (void);
68static int menucmd (WINDOW *w);
69
70#ifndef NULL
71#define NULL (void *)0
72#endif
73
74/* menu system processing routines */
75
76static void mbeep (void)
77{
78	fprintf (stderr,"\a");
79}
80
81static int mgetch(WINDOW *w)
82{
83	static char buf[20];
84	static int  num = 0;
85
86	int i, ret;
87
88	for (i=0; i< strlen(KD); i++) {
89		if (i >= num)
90			buf[num++] = wgetch(w);
91		if (buf[i] != KD[i])
92			break;
93	}
94	if (i == strlen(KD)) {
95		num = 0;
96		return KEYPAD_DOWN_ARROW;
97	}		
98
99	for (i=0; i< strlen(KU); i++) {
100		if (i >= num)
101			buf[num++] = wgetch(w);
102		if (buf[i] != KU[i])
103			break;
104	}
105	if (i == strlen(KU)) {
106		num = 0;
107		return KEYPAD_UP_ARROW;
108	}		
109
110	ret = buf[0];
111	for (i = 0; i < strlen(buf); i++)
112		buf[i] = buf[i+1];
113	num--;
114	return ret;
115}
116
117static int menucmd (WINDOW *w)
118{
119	int ch;
120
121	while (TRUE) {
122		ch = mgetch(w);
123		
124		switch (ch) {
125		case '\n':
126			return REQ_EXECUTE;
127		case '\016':
128		case KEYPAD_DOWN_ARROW:
129			return REQ_NEXT_ITEM;
130		case '\020':
131		case KEYPAD_UP_ARROW:
132			return REQ_PREV_ITEM;
133		case '\014':
134		        return REQ_REDISPLAY;
135		}
136		
137		if (isalpha(ch) || ch == '?' || ch == '<' || ch == '>')
138			return (ch);
139
140		mbeep();
141		wrefresh(w);
142	}
143}
144
145static void init_menu (struct menudesc *m)
146{
147	int max;
148	char **p;
149	int add, exitadd;
150
151	add = ((m->mopt & NOBOX) ? 2 : 4);
152	exitadd = ((m->mopt & NOEXITOPT) ? 0 : 1);
153	max = strlen(m->title);
154
155	/* Calculate h? h == number of visible options. */
156	if (m->h == 0) {
157		m->h = m->numopts + exitadd;
158		if (m->h + m->y + add >= max_lines && (m->mopt & SCROLL))
159			m->h = max_lines - m->y - add - 1;
160	}
161
162	/* Check window heights and set scrolling */
163	if (m->h < m->numopts + exitadd) {
164		if (m->mopt & SCROLL) {
165			if (m->h < 3) {
166				endwin();
167				(void) fprintf (stderr,
168					"Window too small for menu \"%s\"\n",
169			 		m->title);
170				exit(1);
171			}
172		} else {
173			endwin();
174			(void) fprintf (stderr,
175				"Menu too big for menu \"%s\"\n",
176			 	m->title);
177			exit(1);
178		}
179	} else
180		m->mopt &= ~SCROLL;
181
182	/* Calculate w? */
183	if (m->w == 0) {
184		p = m->opts;
185		if (m->mopt & SCROLL)
186			max = strlen(scrolltext);
187		while (*p) {
188			max = MAX(max,strlen(*p));
189			p++;
190		}
191		m->w = max;
192	}
193
194	/* Get the windows. */
195	m->mw = newwin(m->h+add, m->w+add, m->y, m->x);
196
197	if (m->mw == NULL) {
198		endwin();
199		(void) fprintf (stderr,
200			"Could not create window for window with title "
201			" \"%s\"\n", m->title);
202		exit(1);
203	} 
204}
205
206static void post_menu (struct menudesc *m)
207{
208	int i;
209	int hasbox, cury, maxy, selrow, lastopt;
210	int tadd;
211	
212	if (m->mopt & NOBOX) {
213		cury = 0;
214		maxy = m->h;
215		hasbox = 0;
216	} else {
217		cury = 1;
218		maxy = m->h+1;
219		hasbox = 1;
220	}
221
222	tadd = strlen(m->title) ? 2 : 0;
223
224	if (tadd) {
225		mvwaddstr(m->mw, cury, cury, m->title);
226		cury += 2;
227		maxy += 2;
228	}
229
230	/* Set defaults, calculate lastopt. */
231	selrow = -1;
232	if (m->mopt & SCROLL) {
233		lastopt = MIN(m->numopts, m->topline+m->h-1);
234		maxy -= 1;
235	} else
236		lastopt = m->numopts;
237
238	for (i=m->topline; i<lastopt; i++, cury++) {
239		if (m->cursel == i) {
240			mvwaddstr (m->mw, cury, hasbox, ">");
241			wstandout(m->mw);
242			selrow = cury;
243		} else
244			mvwaddstr (m->mw, cury, hasbox, " ");
245		waddstr (m->mw, m->opts[i]);
246		if (m->cursel == i)
247			wstandend(m->mw);
248		wclrtoeol(m->mw);
249	}
250
251	/* Add the exit option. */
252	if (!(m->mopt & NOEXITOPT) && cury < maxy) {
253		if (m->cursel >= m->numopts) {
254			mvwaddstr (m->mw, cury, hasbox, ">");
255			wstandout(m->mw);
256			selrow = cury;
257		} else
258			mvwaddstr (m->mw, cury, hasbox, " ");
259		waddstr (m->mw, "x: Exit");
260		if (m->cursel >= m->numopts)
261			wstandend(m->mw);
262		wclrtoeol(m->mw);
263		cury++;
264	}
265
266	/* Add the scroll line */
267	if (m->mopt & SCROLL) {
268		mvwaddstr (m->mw, cury, hasbox, scrolltext);
269		if (selrow < 0)
270			selrow = cury;
271	}
272
273	/* Add the box. */
274	if (!(m->mopt & NOBOX))
275		box(m->mw, '*', '*');
276
277	wmove(m->mw, selrow, hasbox);
278}
279
280static void process_help (struct menudesc *m, int num)
281{
282	char *help = m->helpstr;
283	int lineoff = 0;
284	int curoff = 0;
285	int again;
286	int winin;
287
288	/* Is there help? */
289	if (!help) {
290		mbeep();
291		return;
292	}
293
294	/* Display the help information. */
295	do {
296		if (lineoff < curoff) {
297			help = m->helpstr;
298			curoff = 0;
299		}
300		while (*help && curoff < lineoff) {
301			if (*help == '\n')
302				curoff++;
303			help++;
304		}
305	
306		wclear(stdscr);
307		mvwaddstr (stdscr, 0, 0, 
308			"Help: exit: q,  page up: u <, page down: d >");
309		mvwaddstr (stdscr, 2, 0, help);
310		wmove (stdscr, 1, 0);
311	  	wrefresh(stdscr);
312
313		do {
314			winin = mgetch(stdscr);
315			winin = tolower(winin);
316			again = 0;
317			switch (winin) {
318				case '<':
319				case 'u':
320				case KEYPAD_UP_ARROW:
321					if (lineoff)
322						lineoff -= max_lines - 2;
323					else
324						again = 1;
325					break;
326				case '>':
327				case 'd':
328				case KEYPAD_DOWN_ARROW:
329					if (*help)
330						lineoff += max_lines - 2;
331					else
332						again = 1;
333					break;
334				case 'q':
335					break;
336				default:
337					again = 1;
338			}
339		} while (again);
340	} while (winin != 'q');
341
342	/* Restore current menu */    
343	wclear(stdscr);
344	wrefresh(stdscr);
345	process_item (&num, -2);
346}
347
348static void process_req (struct menudesc *m, int num, int req)
349{
350	int ch;
351	int hasexit = (m->mopt & NOEXITOPT ? 0 : 1 );
352	int refresh = 0;
353
354	if (req == REQ_EXECUTE)
355		return;
356
357	else if (req == REQ_NEXT_ITEM) {
358		if (m->cursel < m->numopts + hasexit - 1) {
359			m->cursel++;
360			refresh = 1;
361		} else
362			mbeep();
363
364	} else if (req == REQ_PREV_ITEM) {
365		if (m->cursel > 0) {
366			m->cursel--;
367			refresh = 1;
368		} else
369			mbeep();
370
371	} else if (req == REQ_REDISPLAY) {
372		wclear(stdscr);
373		wrefresh(stdscr);
374		process_item (&num, -2);
375		refresh = 1;
376
377	} else if (req == '?') {
378		process_help (m, num);
379		refresh = 1;
380
381	} else if (req == '<') {
382		if (m->topline == 0)
383			mbeep();
384		else {
385			m->topline -= m->h-1;
386			wclear (m->mw);
387			refresh = 1;
388		}
389
390	} else if (req == '>') {
391		if (m->topline + m->h - 1 > m->numopts + hasexit)
392			mbeep();
393		else {
394			m->topline += m->h-1;
395			wclear (m->mw);
396			refresh = 1;
397		}
398
399	} else {
400		ch = tolower (req);
401		if (ch == 'x' && hasexit) {
402			m->cursel = m->numopts;
403			refresh = 1;
404		} else {
405			ch = ch - 'a';
406			if (ch < 0 || ch >= m->numopts)
407				mbeep();
408			else {
409				m->cursel = ch;
410				refresh = 1;
411			}
412		}
413	}
414	if (refresh) {
415		post_menu (m);
416		wrefresh (m->mw);
417	}
418}
419
420void process_menu (int num)
421{
422	int sel = 0;
423	int req, done;
424	int last_num;
425
426	struct menudesc *m = &menus[num];
427
428	done = FALSE;
429
430	/* Initialize? */
431	if (!__menu_init) {
432		if (initscr() == NULL) {
433			__menu_initerror();
434			return;
435		}
436		cbreak();
437		noecho();
438		max_lines = stdscr->maxy;
439		__menu_init = 1;
440	}
441	if (__m_endwin) {
442     		wclear(stdscr);
443		wrefresh(stdscr);
444		__m_endwin = 0;
445	}
446	if (m->mw == NULL)
447		init_menu (m);
448
449	/* Always preselect option 0 and display from 0! */
450	m->cursel = 0;
451	m->topline = 0;
452
453	while (!done) {
454		last_num = num;
455		if (__m_endwin) {
456			wclear(stdscr);
457			wrefresh(stdscr);
458			__m_endwin = 0;
459		}
460		/* Process the display action */
461		process_item (&num, -2);
462		post_menu (m);
463		wrefresh (m->mw);
464
465		while ((req = menucmd (m->mw)) != REQ_EXECUTE)
466			process_req (m, num, req);
467
468		sel = m->cursel;
469		wclear (m->mw);
470		wrefresh (m->mw);
471
472		/* Process the items */
473		if (sel < m->numopts)
474			done = process_item (&num, sel);
475		else
476			done = TRUE;
477
478		/* Reselect m just in case */
479		if (num != last_num) {
480			m = &menus[num];
481			/* Initialize? */
482			if (m->mw == NULL)
483				init_menu (m);
484			process_item (&num, -2);
485		}
486	}
487
488	/* Process the exit action */
489	process_item (&num, -1);
490}
491
492