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