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