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