1 /* $NetBSD
: menu_sys.def
,v
1.59 2012/03/06 16:55:18 mbalmer 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. The name of Piermont Information Systems Inc. may not be used to endorse
18 * or promote products derived from this software without specific prior
21 * THIS SOFTWARE IS PROVIDED
BY PIERMONT INFORMATION SYSTEMS
INC. ``AS IS
''
22 * AND ANY EXPRESS
OR IMPLIED WARRANTIES
, INCLUDING
, BUT
NOT LIMITED
TO, THE
23 * IMPLIED WARRANTIES
OF MERCHANTABILITY
AND FITNESS
FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.
IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS
INC. BE
25 * LIABLE
FOR ANY DIRECT
, INDIRECT
, INCIDENTAL
, SPECIAL
, EXEMPLARY
, OR
26 * CONSEQUENTIAL
DAMAGES (INCLUDING
, BUT
NOT LIMITED
TO, PROCUREMENT
OF
27 * SUBSTITUTE GOODS
OR SERVICES
; LOSS
OF USE
, DATA
, OR PROFITS
; OR BUSINESS
28 * INTERRUPTION
) HOWEVER CAUSED
AND ON ANY THEORY
OF LIABILITY
, WHETHER
IN
29 * CONTRACT
, STRICT LIABILITY
, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE
)
30 * ARISING
IN ANY WAY OUT
OF THE USE
OF THIS SOFTWARE
, EVEN
IF ADVISED
OF
31 * THE POSSIBILITY
OF SUCH DAMAGE.
35 /* menu_sys.defs
-- Menu system standard routines.
*/
40 #define REQ_EXECUTE
1000
41 #define REQ_NEXT_ITEM
1001
42 #define REQ_PREV_ITEM
1002
43 #define REQ_REDISPLAY
1003
44 #define REQ_SCROLLDOWN
1004
45 #define REQ_SCROLLUP
1005
49 #define
MAX(x
,y
) ((x
)>(y
)?
(x
):(y
))
50 #define
MIN(x
,y
) ((x
)<(y
)?
(x
):(y
))
52 /* Initialization state.
*/
53 static int __menu_init
= 0;
54 static int max_lines
= 0, max_cols
= 0;
56 static const char
*scrolltext
= " <: page up, >: page down";
60 static int num_menus
= 0;
61 #define DYN_INIT_NUM
32
62 static menudesc
**menu_list
;
63 #define
MENUS(n
) (*(menu_list[n]))
65 #define MENUS(n) (menu_def[n])
68 /* prototypes for in here! */
69 static void init_menu(menudesc *m);
70 static char opt_ch(menudesc *m, int op_no);
71 static void draw_menu(menudesc *m, void *arg);
72 static void process_help(menudesc *m);
73 static void process_req(menudesc *m, void *arg, int req);
74 static int menucmd(WINDOW *w);
80 /* menu system processing routines */
81 #define mbeep() (void)fputc('\a', stderr)
94 case '\016': /* Control-P */
97 case '\020': /* Control-N */
100 case '\014': /* Control-L */
101 return REQ_REDISPLAY;
103 case '\010': /* Control-H (backspace) */
107 case '\026': /* Control-V */
112 return REQ_SCROLLDOWN;
115 case '\033': /* esc-v is scroll down */
120 ch = 0; /* zap char so we beep */
132 init_menu(menudesc *m)
135 int hadd, wadd, exithadd;
138 const char *title, *tp, *ep;
144 hadd = ((m->mopt & MC_NOBOX) ? 0 : 2);
145 wadd = ((m->mopt & MC_NOBOX) ? 2 : 4);
146 if (!(m->mopt & MC_NOSHORTCUT))
149 if (m->title && *(title = MSG_XLAT(m->title)) != 0) {
150 /* Allow multiple line titles */
151 for (tp = title; (ep = strchr(tp, '\n')); tp = ep + 1) {
165 exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
169 /* put menu box below message text */
177 /* Calculate h? h == number of visible options. */
179 m->h = m->numopts + exithadd;
181 if (m->h > max_lines - y - hadd) {
182 /* Not enough space for all the options */
183 if (m->h <= 4 || !(m->mopt & (MC_SCROLL | MC_ALWAYS_SCROLL))) {
184 /* move menu up screen */
185 y = max_lines - hadd - m->h;
189 m->h = max_lines - y - hadd;
192 if (m->h < m->numopts + exithadd || m->mopt & MC_ALWAYS_SCROLL) {
193 /* We need to add the scroll text...
194 * The used to be a check for MC_SCROLL here, but it is
195 * fairly pointless - you just don't want the program
196 * to exit on this sort of error.
200 (void)fprintf(stderr,
201 "Window too short (m->h %d, m->numopts %d, exithadd %d, y %d, max_lines %d, hadd %d) for menu \"%.30s\"\n",
202 m->h, m->numopts, exithadd, y, max_lines, hadd,
207 m->h = MIN(m->h, max_lines - y - hadd);
208 i = strlen(scrolltext);
215 for (i = 0; i < m->numopts; i++) {
216 tp = MSG_XLAT(m->opts[i].opt_name);
225 /* check and adjust for screen fit */
226 if (w + wadd > max_cols) {
228 (void)fprintf(stderr,
229 "Screen too narrow (%d + %d > %d) for menu \"%s\"\n",
230 w, wadd, max_cols, title);
236 x = (max_cols - (w + wadd)) / 2; /* center */
237 else if (x + w + wadd > max_cols)
238 x = max_cols - (w + wadd); /* right align */
241 /* Center - rather than top */
242 y = (max_lines - hadd - m->h) / 2;
245 /* Get the windows. */
246 m->mw = newwin(m->h + hadd, w + wadd, y, x);
250 (void)fprintf(stderr,
251 "Could not create window (%d + %d, %d + %d, %d, %d) for menu \"%s\"\n",
252 m->h, hadd, w, wadd, y, x, title);
255 keypad(m->mw, TRUE); /* enable multi-key assembling for win */
257 /* XXX is it even worth doing this right? */
259 wbkgd(m->mw, COLOR_PAIR(1));
260 wattrset(m->mw, COLOR_PAIR(1));
263 if (m->mopt & MC_SUBMENU) {
264 /* Keep a copy of what is on the screen under the window */
265 m->sv_mw = newwin(m->h + hadd, w + wadd, y, x);
267 * cursrc contains post-doupdate() data, not post-refresh()
268 * data so we must call doupdate to ensure we save the
269 * correct data. Avoids PR 26660.
273 overwrite(curscr, m->sv_mw);
278 opt_ch(menudesc *m, int op_no)
282 if (op_no == m->numopts)
290 c = 'A' + op_no - 25;
295 draw_menu_line(menudesc *m, int opt, int cury, void *arg, const char *text)
297 int hasbox = m->mopt & MC_NOBOX ? 0 : 1;
299 if (m->cursel == opt) {
300 mvwaddstr(m->mw, cury, hasbox, ">");
303 mvwaddstr(m->mw, cury, hasbox, " ");
304 if (!(m->mopt & MC_NOSHORTCUT))
305 wprintw(m->mw, "%c: ", opt_ch(m, opt));
307 if (!text && m->draw_line)
308 m->draw_line(m, opt, arg);
310 waddstr(m->mw, MSG_XLAT(text));
311 if (m->cursel == opt)
316 draw_menu(menudesc *m, void *arg)
319 int hasbox, cury, maxy;
321 int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
324 hasbox = (m->mopt & MC_NOBOX ? 0 : 1);
326 /* Clear the window */
331 for (tp = MSG_XLAT(m->title); ; tp = ep + 1) {
332 ep = strchr(tp , '\n');
333 mvwaddnstr(m->mw, tadd++, hasbox + 1, tp,
335 if (ep == NULL || *ep == 0)
342 maxy = getmaxy(m->mw) - hasbox;
343 if (m->numopts + hasexit > m->h)
344 /* allow for scroll text */
347 if (m->cursel == -1) {
348 m->cursel = m->numopts;
349 if (m->h <= m->numopts)
350 m->topline = m->numopts + 1 - m->h;
353 while (m->cursel >= m->topline + m->h)
354 m->topline = MIN(m->topline + m->h,
355 m->numopts + hasexit - m->h);
356 while (m->cursel < m->topline)
357 m->topline = MAX(0, m->topline - m->h);
359 for (opt = m->topline; opt < m->numopts; opt++) {
362 draw_menu_line(m, opt, cury++, arg, m->opts[opt].opt_name);
365 /* Add the exit option. */
366 if (!(m->mopt & MC_NOEXITOPT)) {
368 draw_menu_line(m, m->numopts, cury++, arg, m->exitstr);
373 /* Add the scroll line */
374 if (opt != m->numopts || m->topline != 0)
375 mvwaddstr(m->mw, cury, hasbox, scrolltext);
378 if (!(m->mopt & MC_NOBOX))
381 wmove(m->mw, tadd + m->cursel - m->topline, hasbox);
388 process_help(menudesc *m)
390 const char *help = m->helpstr;
402 sv_curscr = newwin(getmaxy(curscr), getmaxx(curscr), 0, 0);
408 * Save screen contents so we can restore before returning.
409 * cursrc contains post-doupdate() data, not post-refresh()
410 * data so we must call doupdate to ensure we save the
411 * correct data. Avoids PR 26660.
414 overwrite(curscr, sv_curscr);
417 help = MSG_XLAT(help);
418 /* Display the help information. */
420 if (lineoff < curoff) {
421 help = MSG_XLAT(m->helpstr);
424 while (*help && curoff < lineoff) {
431 mvwaddstr(stdscr, 0, 0,
432 "Help: exit: x, page up: u <, page down: d >");
433 mvwaddstr(stdscr, 2, 0, help);
438 winin = wgetch(stdscr);
440 winin = tolower(winin);
449 lineoff -= max_lines - 2;
459 lineoff += max_lines - 2;
474 } while (winin != 'q');
476 /* Restore the original screen contents */
478 wnoutrefresh(sv_curscr);
481 /* Some code thinks that wrefresh(stdout) is a good idea... */
486 process_req(menudesc *m, void *arg, int req)
489 int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
500 if (ch >= m->numopts + hasexit) {
504 if (hasexit && ch == m->numopts)
506 if (!(m->opts[ch].opt_flags & OPT_IGNORE))
510 if (m->cursel >= m->topline + m->h)
511 m->topline = m->cursel - m->h + 1;
522 if (!(m->opts[ch].opt_flags & OPT_IGNORE))
526 if (m->cursel < m->topline)
527 m->topline = m->cursel;
540 if (m->cursel == 0) {
544 m->topline = MAX(0, m->topline - m->h);
545 m->cursel = MAX(0, m->cursel - m->h);
550 if (m->cursel >= m->numopts + hasexit - 1) {
554 m->topline = MIN(m->topline + m->h,
555 MAX(m->numopts + hasexit - m->h, 0));
556 m->cursel = MIN(m->numopts + hasexit - 1, m->cursel + m->h);
562 if (ch == 'x' && hasexit) {
563 m->cursel = m->numopts;
566 if (m->mopt & MC_NOSHORTCUT) {
578 if (ch < 0 || ch >= m->numopts) {
582 if (m->opts[ch].opt_flags & OPT_IGNORE) {
600 #ifdef USER_MENU_INIT
605 if (initscr() == NULL)
611 /* XXX Should be configurable but it almost isn't worth it. */
614 init_pair(1, COLOR_WHITE, COLOR_BLUE);
616 attrset(COLOR_PAIR(1));
619 max_lines = getmaxy(stdscr);
620 max_cols = getmaxx(stdscr);
621 keypad(stdscr, TRUE);
623 num_menus = DYN_INIT_NUM;
624 while (num_menus < DYN_MENU_START)
626 menu_list = malloc(sizeof *menu_list * num_menus);
627 if (menu_list == NULL)
629 (void)memset(menu_list, 0, sizeof *menu_list * num_menus);
630 for (i = 0; i < DYN_MENU_START; i++)
631 menu_list[i] = &menu_def[i];
639 process_menu(int num, void *arg)
653 if (num < 0 || num >= num_menus)
659 /* Default to select option 0 and display from 0 */
661 if ((m->mopt & (MC_DFLTEXIT | MC_NOEXITOPT)) == MC_DFLTEXIT)
668 /* I'm not sure this is needed with netbsd's curses */
670 /* Process the display action */
672 (*m->post_act)(m, arg);
677 while ((req = menucmd(m->mw)) != REQ_EXECUTE)
678 process_req(m, arg, req);
681 if (!(m->mopt & MC_NOCLEAR)) {
684 overwrite(m->sv_mw, m->mw);
688 /* Process the items */
689 if (sel >= m->numopts)
694 if (opt->opt_flags & OPT_IGNORE)
696 if (opt->opt_flags & OPT_ENDWIN)
698 if (opt->opt_action && (*opt->opt_action)(m, arg))
701 if (opt->opt_menu != -1) {
702 if (!(opt->opt_flags & OPT_SUB)) {
706 overwrite(m->sv_mw, m->mw);
716 process_menu(opt->opt_menu, arg);
718 if (opt->opt_flags & OPT_EXIT)
722 if (m->mopt & MC_NOCLEAR) {
725 overwrite(m->sv_mw, m->mw);
729 /* Process the exit action */
731 (*m->exit_act)(m, arg);
742 set_menu_numopts(int menu, int numopts)
745 MENUS(menu).numopts = numopts;
748 \f/* Control L is end of standard routines, remaining only for dynamic. */
750 /* Beginning of routines for dynamic menus. */
756 int sz = sizeof *menu_list * num_menus;
758 temp = realloc(menu_list, sz * 2);
761 (void)memset(temp + num_menus, 0, sz);
769 new_menu(const char *title, menu_ent *opts, int numopts,
770 int x, int y, int h, int w, int mopt,
771 void (*post_act)(menudesc *, void *),
772 void (*draw_line)(menudesc *, int, void *),
773 void (*exit_act)(menudesc *, void *),
774 const char
*help
, const char
*exit_str
)
779 /* Find free menu entry.
*/
780 for (ix
= DYN_MENU_START
; ; ix
++) {
781 if (ix
>= num_menus
&& !double_menus())
785 m
= calloc(sizeof
*m
, 1);
791 if (!(m
->mopt
& MC_VALID
))
798 m
->numopts
= numopts
;
803 m
->mopt
= mopt | MC_VALID
;
804 m
->post_act
= post_act
;
805 m
->draw_line
= draw_line
;
806 m
->exit_act
= exit_act
;
808 m
->exitstr
= exit_str ? exit_str
: "Exit";
814 free_menu(int menu_no
)
818 if (menu_no
< 0 || menu_no
>= num_menus
)
821 m
= menu_list
[menu_no
];
822 if (!(m
->mopt
& MC_VALID
))
826 memset(m
, 0, sizeof
*m
);