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