1 /* $NetBSD
: menu_sys.def
,v
1.57 2004/09/17 18:27:28 wrstuden Exp $
*/
4 * Copyright
1997 Piermont Information Systems Inc.
7 * Written by Philip A. Nelson for Piermont Information Systems Inc.
9 * Redistribution and use in source and binary forms
, with or without
10 * modification
, are permitted provided that the following conditions
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
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.
39 /* menu_sys.defs
-- Menu system standard routines.
*/
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
53 #define
MAX(x
,y
) ((x
)>(y
)?
(x
):(y
))
54 #define
MIN(x
,y
) ((x
)<(y
)?
(x
):(y
))
56 /* Initialization state.
*/
57 static int __menu_init
= 0;
58 static int max_lines
= 0, max_cols
= 0;
60 static const char
*scrolltext
= " <: page up, >: page down";
64 static int num_menus
= 0;
65 #define DYN_INIT_NUM
32
66 static menudesc
**menu_list
;
67 #define
MENUS(n
) (*(menu_list[n]))
69 #define MENUS(n) (menu_def[n])
72 /* prototypes for in here! */
73 static void init_menu(menudesc *m);
74 static char opt_ch(menudesc *m, int op_no);
75 static void draw_menu(menudesc *m, void *arg);
76 static void process_help(menudesc *m);
77 static void process_req(menudesc *m, void *arg, int req);
78 static int menucmd(WINDOW *w);
84 /* menu system processing routines */
85 #define mbeep() (void)fputc('\a', stderr)
98 case '\016': /* Control-P */
100 return REQ_NEXT_ITEM;
101 case '\020': /* Control-N */
103 return REQ_PREV_ITEM;
104 case '\014': /* Control-L */
105 return REQ_REDISPLAY;
107 case '\010': /* Control-H (backspace) */
111 case '\026': /* Control-V */
116 return REQ_SCROLLDOWN;
119 case '\033': /* esc-v is scroll down */
124 ch = 0; /* zap char so we beep */
136 init_menu(menudesc *m)
139 int hadd, wadd, exithadd;
142 const char *title, *tp, *ep;
148 hadd = ((m->mopt & MC_NOBOX) ? 0 : 2);
149 wadd = ((m->mopt & MC_NOBOX) ? 2 : 4);
150 if (!(m->mopt & MC_NOSHORTCUT))
153 if (m->title && *(title = MSG_XLAT(m->title)) != 0) {
154 /* Allow multiple line titles */
155 for (tp = title; (ep = strchr(tp, '\n')); tp = ep + 1) {
169 exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
173 /* put menu box below message text */
181 /* Calculate h? h == number of visible options. */
183 m->h = m->numopts + exithadd;
185 if (m->h > max_lines - y - hadd) {
186 /* Not enough space for all the options */
187 if (m->h <= 4 || !(m->mopt & (MC_SCROLL | MC_ALWAYS_SCROLL))) {
188 /* move menu up screen */
189 y = max_lines - hadd - m->h;
193 m->h = max_lines - y - hadd;
196 if (m->h < m->numopts + exithadd || m->mopt & MC_ALWAYS_SCROLL) {
197 /* We need to add the scroll text...
198 * The used to be a check for MC_SCROLL here, but it is
199 * fairly pointless - you just don't want the program
200 * to exit on this sort of error.
204 (void)fprintf(stderr,
205 "Window too short (m->h %d, m->numopts %d, exithadd %d, y %d, max_lines %d, hadd %d) for menu \"%.30s\"\n",
206 m->h, m->numopts, exithadd, y, max_lines, hadd,
211 m->h = MIN(m->h, max_lines - y - hadd);
212 i = strlen(scrolltext);
219 for (i = 0; i < m->numopts; i++) {
220 tp = MSG_XLAT(m->opts[i].opt_name);
229 /* check and adjust for screen fit */
230 if (w + wadd > max_cols) {
232 (void)fprintf(stderr,
233 "Screen too narrow (%d + %d > %d) for menu \"%s\"\n",
234 w, wadd, max_cols, title);
240 x = (max_cols - (w + wadd)) / 2; /* center */
241 else if (x + w + wadd > max_cols)
242 x = max_cols - (w + wadd); /* right align */
245 /* Center - rather than top */
246 y = (max_lines - hadd - m->h) / 2;
249 /* Get the windows. */
250 m->mw = newwin(m->h + hadd, w + wadd, y, x);
254 (void)fprintf(stderr,
255 "Could not create window (%d + %d, %d + %d, %d, %d) for menu \"%s\"\n",
256 m->h, hadd, w, wadd, y, x, title);
259 keypad(m->mw, TRUE); /* enable multi-key assembling for win */
261 /* XXX is it even worth doing this right? */
263 wbkgd(m->mw, COLOR_PAIR(1));
264 wattrset(m->mw, COLOR_PAIR(1));
267 if (m->mopt & MC_SUBMENU) {
268 /* Keep a copy of what is on the screen under the window */
269 m->sv_mw = newwin(m->h + hadd, w + wadd, y, x);
271 * cursrc contains post-doupdate() data, not post-refresh()
272 * data so we must call doupdate to ensure we save the
273 * correct data. Avoids PR 26660.
277 overwrite(curscr, m->sv_mw);
282 opt_ch(menudesc *m, int op_no)
286 if (op_no == m->numopts)
294 c = 'A' + op_no - 25;
299 draw_menu_line(menudesc *m, int opt, int cury, void *arg, const char *text)
301 int hasbox = m->mopt & MC_NOBOX ? 0 : 1;
303 if (m->cursel == opt) {
304 mvwaddstr(m->mw, cury, hasbox, ">");
307 mvwaddstr(m->mw, cury, hasbox, " ");
308 if (!(m->mopt & MC_NOSHORTCUT))
309 wprintw(m->mw, "%c: ", opt_ch(m, opt));
311 if (!text && m->draw_line)
312 m->draw_line(m, opt, arg);
314 waddstr(m->mw, MSG_XLAT(text));
315 if (m->cursel == opt)
320 draw_menu(menudesc *m, void *arg)
323 int hasbox, cury, maxy;
325 int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
328 hasbox = (m->mopt & MC_NOBOX ? 0 : 1);
330 /* Clear the window */
335 for (tp = MSG_XLAT(m->title); ; tp = ep + 1) {
336 ep = strchr(tp , '\n');
337 mvwaddnstr(m->mw, tadd++, hasbox + 1, tp,
339 if (ep == NULL || *ep == 0)
346 maxy = getmaxy(m->mw) - hasbox;
347 if (m->numopts + hasexit > m->h)
348 /* allow for scroll text */
351 if (m->cursel == -1) {
352 m->cursel = m->numopts;
353 if (m->h <= m->numopts)
354 m->topline = m->numopts + 1 - m->h;
357 while (m->cursel >= m->topline + m->h)
358 m->topline = MIN(m->topline + m->h,
359 m->numopts + hasexit - m->h);
360 while (m->cursel < m->topline)
361 m->topline = MAX(0, m->topline - m->h);
363 for (opt = m->topline; opt < m->numopts; opt++) {
366 draw_menu_line(m, opt, cury++, arg, m->opts[opt].opt_name);
369 /* Add the exit option. */
370 if (!(m->mopt & MC_NOEXITOPT)) {
372 draw_menu_line(m, m->numopts, cury++, arg, m->exitstr);
377 /* Add the scroll line */
378 if (opt != m->numopts || m->topline != 0)
379 mvwaddstr(m->mw, cury, hasbox, scrolltext);
382 if (!(m->mopt & MC_NOBOX))
385 wmove(m->mw, tadd + m->cursel - m->topline, hasbox);
392 process_help(menudesc *m)
394 const char *help = m->helpstr;
406 sv_curscr = newwin(getmaxy(curscr), getmaxx(curscr), 0, 0);
412 * Save screen contents so we can restore before returning.
413 * cursrc contains post-doupdate() data, not post-refresh()
414 * data so we must call doupdate to ensure we save the
415 * correct data. Avoids PR 26660.
418 overwrite(curscr, sv_curscr);
421 help = MSG_XLAT(help);
422 /* Display the help information. */
424 if (lineoff < curoff) {
425 help = MSG_XLAT(m->helpstr);
428 while (*help && curoff < lineoff) {
435 mvwaddstr(stdscr, 0, 0,
436 "Help: exit: x, page up: u <, page down: d >");
437 mvwaddstr(stdscr, 2, 0, help);
442 winin = wgetch(stdscr);
444 winin = tolower(winin);
453 lineoff -= max_lines - 2;
463 lineoff += max_lines - 2;
478 } while (winin != 'q');
480 /* Restore the original screen contents */
482 wnoutrefresh(sv_curscr);
485 /* Some code thinks that wrefresh(stdout) is a good idea... */
490 process_req(menudesc *m, void *arg, int req)
493 int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
504 if (ch >= m->numopts + hasexit) {
508 if (hasexit && ch == m->numopts)
510 if (!(m->opts[ch].opt_flags & OPT_IGNORE))
514 if (m->cursel >= m->topline + m->h)
515 m->topline = m->cursel - m->h + 1;
526 if (!(m->opts[ch].opt_flags & OPT_IGNORE))
530 if (m->cursel < m->topline)
531 m->topline = m->cursel;
544 if (m->cursel == 0) {
548 m->topline = MAX(0, m->topline - m->h);
549 m->cursel = MAX(0, m->cursel - m->h);
554 if (m->cursel >= m->numopts + hasexit - 1) {
558 m->topline = MIN(m->topline + m->h,
559 MAX(m->numopts + hasexit - m->h, 0));
560 m->cursel = MIN(m->numopts + hasexit - 1, m->cursel + m->h);
566 if (ch == 'x' && hasexit) {
567 m->cursel = m->numopts;
570 if (m->mopt & MC_NOSHORTCUT) {
582 if (ch < 0 || ch >= m->numopts) {
586 if (m->opts[ch].opt_flags & OPT_IGNORE) {
604 #ifdef USER_MENU_INIT
609 if (initscr() == NULL)
615 /* XXX Should be configurable but it almost isn't worth it. */
618 init_pair(1, COLOR_WHITE, COLOR_BLUE);
620 attrset(COLOR_PAIR(1));
623 max_lines = getmaxy(stdscr);
624 max_cols = getmaxx(stdscr);
625 keypad(stdscr, TRUE);
627 num_menus = DYN_INIT_NUM;
628 while (num_menus < DYN_MENU_START)
630 menu_list = malloc(sizeof *menu_list * num_menus);
631 if (menu_list == NULL)
633 (void)memset(menu_list, 0, sizeof *menu_list * num_menus);
634 for (i = 0; i < DYN_MENU_START; i++)
635 menu_list[i] = &menu_def[i];
643 process_menu(int num, void *arg)
657 if (num < 0 || num >= num_menus)
663 /* Default to select option 0 and display from 0 */
665 if ((m->mopt & (MC_DFLTEXIT | MC_NOEXITOPT)) == MC_DFLTEXIT)
672 /* I'm not sure this is needed with netbsd's curses */
674 /* Process the display action */
676 (*m->post_act)(m, arg);
681 while ((req = menucmd(m->mw)) != REQ_EXECUTE)
682 process_req(m, arg, req);
685 if (!(m->mopt & MC_NOCLEAR)) {
688 overwrite(m->sv_mw, m->mw);
692 /* Process the items */
693 if (sel >= m->numopts)
698 if (opt->opt_flags & OPT_IGNORE)
700 if (opt->opt_flags & OPT_ENDWIN)
702 if (opt->opt_action && (*opt->opt_action)(m, arg))
705 if (opt->opt_menu != -1) {
706 if (!(opt->opt_flags & OPT_SUB)) {
710 overwrite(m->sv_mw, m->mw);
720 process_menu(opt->opt_menu, arg);
722 if (opt->opt_flags & OPT_EXIT)
726 if (m->mopt & MC_NOCLEAR) {
729 overwrite(m->sv_mw, m->mw);
733 /* Process the exit action */
735 (*m->exit_act)(m, arg);
746 set_menu_numopts(int menu, int numopts)
749 MENUS(menu).numopts = numopts;
752 \f/* Control L is end of standard routines, remaining only for dynamic. */
754 /* Beginning of routines for dynamic menus. */
760 int sz = sizeof *menu_list * num_menus;
762 temp = realloc(menu_list, sz * 2);
765 (void)memset(temp + num_menus, 0, sz);
773 new_menu(const char *title, menu_ent *opts, int numopts,
774 int x, int y, int h, int w, int mopt,
775 void (*post_act)(menudesc *, void *),
776 void (*draw_line)(menudesc *, int, void *),
777 void (*exit_act)(menudesc *, void *),
778 const char
*help
, const char
*exit_str
)
783 /* Find free menu entry.
*/
784 for (ix
= DYN_MENU_START
; ; ix
++) {
785 if (ix
>= num_menus
&& !double_menus())
789 m
= calloc(sizeof
*m
, 1);
795 if (!(m
->mopt
& MC_VALID
))
802 m
->numopts
= numopts
;
807 m
->mopt
= mopt | MC_VALID
;
808 m
->post_act
= post_act
;
809 m
->draw_line
= draw_line
;
810 m
->exit_act
= exit_act
;
812 m
->exitstr
= exit_str ? exit_str
: "Exit";
818 free_menu(int menu_no
)
822 if (menu_no
< 0 || menu_no
>= num_menus
)
825 m
= menu_list
[menu_no
];
826 if (!(m
->mopt
& MC_VALID
))
830 memset(m
, 0, sizeof
*m
);