menu_sys.def revision 1.52 1 1.52 dsl /* $NetBSD: menu_sys.def,v 1.52 2003/12/21 21:42:48 dsl Exp $ */
2 1.1 phil
3 1.1 phil /*
4 1.1 phil * Copyright 1997 Piermont Information Systems Inc.
5 1.1 phil * All rights reserved.
6 1.1 phil *
7 1.1 phil * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 1.1 phil *
9 1.1 phil * Redistribution and use in source and binary forms, with or without
10 1.1 phil * modification, are permitted provided that the following conditions
11 1.1 phil * are met:
12 1.1 phil * 1. Redistributions of source code must retain the above copyright
13 1.1 phil * notice, this list of conditions and the following disclaimer.
14 1.1 phil * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 phil * notice, this list of conditions and the following disclaimer in the
16 1.1 phil * documentation and/or other materials provided with the distribution.
17 1.1 phil * 3. All advertising materials mentioning features or use of this software
18 1.1 phil * must display the following acknowledgement:
19 1.1 phil * This product includes software develooped for the NetBSD Project by
20 1.1 phil * Piermont Information Systems Inc.
21 1.1 phil * 4. The name of Piermont Information Systems Inc. may not be used to endorse
22 1.1 phil * or promote products derived from this software without specific prior
23 1.1 phil * written permission.
24 1.1 phil *
25 1.1 phil * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
26 1.1 phil * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 phil * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 phil * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
29 1.1 phil * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 phil * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 phil * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 phil * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 phil * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 phil * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 1.1 phil * THE POSSIBILITY OF SUCH DAMAGE.
36 1.1 phil *
37 1.1 phil */
38 1.1 phil
39 1.1 phil /* menu_sys.defs -- Menu system standard routines. */
40 1.1 phil
41 1.1 phil #include <string.h>
42 1.1 phil #include <ctype.h>
43 1.1 phil
44 1.3 phil #define REQ_EXECUTE 1000
45 1.3 phil #define REQ_NEXT_ITEM 1001
46 1.3 phil #define REQ_PREV_ITEM 1002
47 1.3 phil #define REQ_REDISPLAY 1003
48 1.9 phil #define REQ_SCROLLDOWN 1004
49 1.9 phil #define REQ_SCROLLUP 1005
50 1.9 phil #define REQ_HELP 1006
51 1.9 phil
52 1.9 phil /* Macros */
53 1.1 phil #define MAX(x,y) ((x)>(y)?(x):(y))
54 1.7 phil #define MIN(x,y) ((x)<(y)?(x):(y))
55 1.1 phil
56 1.1 phil /* Initialization state. */
57 1.1 phil static int __menu_init = 0;
58 1.1 phil int __m_endwin = 0;
59 1.17 cgd static int max_lines = 0, max_cols = 0;
60 1.48 dsl #ifndef scrolltext
61 1.44 dsl static const char *scrolltext = " <: page up, >: page down";
62 1.48 dsl #endif
63 1.1 phil
64 1.14 phil #ifdef DYNAMIC_MENUS
65 1.14 phil static int num_menus = 0;
66 1.14 phil #define DYN_INIT_NUM 32
67 1.52 dsl static menudesc **menu_list;
68 1.52 dsl #define MENUS(n) (*(menu_list[n]))
69 1.52 dsl #else
70 1.52 dsl #define MENUS(n) (menu_def[n])
71 1.14 phil #endif
72 1.14 phil
73 1.1 phil /* prototypes for in here! */
74 1.39 dsl static void init_menu(menudesc *m);
75 1.43 dsl static char opt_ch(menudesc *m, int op_no);
76 1.43 dsl static void draw_menu(menudesc *m, void *arg);
77 1.52 dsl static void process_help(menudesc *m);
78 1.52 dsl static void process_req(menudesc *m, void *arg, int req);
79 1.36 dsl static int menucmd(WINDOW *w);
80 1.1 phil
81 1.1 phil #ifndef NULL
82 1.36 dsl #define NULL 0
83 1.1 phil #endif
84 1.1 phil
85 1.1 phil /* menu system processing routines */
86 1.27 christos #define mbeep() (void)fputc('\a', stderr)
87 1.1 phil
88 1.23 hubertf static int
89 1.35 dsl menucmd(WINDOW *w)
90 1.1 phil {
91 1.1 phil int ch;
92 1.1 phil
93 1.1 phil while (TRUE) {
94 1.29 blymn ch = wgetch(w);
95 1.1 phil
96 1.1 phil switch (ch) {
97 1.1 phil case '\n':
98 1.1 phil return REQ_EXECUTE;
99 1.31 dsl case '\016': /* Control-P */
100 1.29 blymn case KEY_DOWN:
101 1.1 phil return REQ_NEXT_ITEM;
102 1.11 phil case '\020': /* Control-N */
103 1.29 blymn case KEY_UP:
104 1.1 phil return REQ_PREV_ITEM;
105 1.11 phil case '\014': /* Control-L */
106 1.36 dsl return REQ_REDISPLAY;
107 1.9 phil case '<':
108 1.11 phil case '\010': /* Control-H (backspace) */
109 1.29 blymn case KEY_PPAGE:
110 1.31 dsl case KEY_LEFT:
111 1.9 phil return REQ_SCROLLUP;
112 1.31 dsl case '\026': /* Control-V */
113 1.9 phil case '>':
114 1.11 phil case ' ':
115 1.29 blymn case KEY_NPAGE:
116 1.31 dsl case KEY_RIGHT:
117 1.9 phil return REQ_SCROLLDOWN;
118 1.9 phil case '?':
119 1.9 phil return REQ_HELP;
120 1.29 blymn case '\033': /* esc-v is scroll down */
121 1.29 blymn ch = wgetch(w);
122 1.29 blymn if (ch == 'v')
123 1.29 blymn return REQ_SCROLLUP;
124 1.29 blymn else
125 1.29 blymn ch = 0; /* zap char so we beep */
126 1.1 phil }
127 1.1 phil
128 1.9 phil if (isalpha(ch))
129 1.36 dsl return ch;
130 1.1 phil
131 1.1 phil mbeep();
132 1.1 phil wrefresh(w);
133 1.1 phil }
134 1.1 phil }
135 1.1 phil
136 1.23 hubertf static void
137 1.39 dsl init_menu(menudesc *m)
138 1.1 phil {
139 1.18 cgd int wmax;
140 1.18 cgd int hadd, wadd, exithadd;
141 1.14 phil int i;
142 1.50 dsl int x, y, w;
143 1.50 dsl const char *title, *tp, *ep;
144 1.1 phil
145 1.50 dsl x = m->x;
146 1.50 dsl y = m->y;
147 1.50 dsl w = m->w;
148 1.50 dsl wmax = 0;
149 1.18 cgd hadd = ((m->mopt & MC_NOBOX) ? 0 : 2);
150 1.18 cgd wadd = ((m->mopt & MC_NOBOX) ? 2 : 4);
151 1.18 cgd
152 1.40 dsl if (m->title && *(title = MSG_XLAT(m->title)) != 0) {
153 1.50 dsl /* Allow multiple line titles */
154 1.50 dsl for (tp = title; ep = strchr(tp, '\n'); tp = ep + 1) {
155 1.50 dsl i = ep - tp;
156 1.50 dsl wmax = MAX(wmax, i);
157 1.50 dsl hadd++;
158 1.50 dsl }
159 1.50 dsl hadd++;
160 1.50 dsl i = strlen(tp);
161 1.51 dsl wmax = MAX(wmax, i);
162 1.50 dsl if (i != 0)
163 1.50 dsl hadd++;
164 1.40 dsl } else {
165 1.40 dsl m->title = NULL;
166 1.40 dsl title = "untitled";
167 1.40 dsl }
168 1.18 cgd exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
169 1.18 cgd
170 1.47 dsl #ifdef MSG_DEFS_H
171 1.50 dsl if (y == -1)
172 1.50 dsl y = msg_row();
173 1.47 dsl #endif
174 1.1 phil
175 1.7 phil /* Calculate h? h == number of visible options. */
176 1.38 dsl if (m->h == 0)
177 1.18 cgd m->h = m->numopts + exithadd;
178 1.50 dsl m->h = MIN(m->h, max_lines - y - hadd);
179 1.7 phil
180 1.48 dsl if (m->h < m->numopts + exithadd || m->mopt & MC_ALWAYS_SCROLL) {
181 1.48 dsl if (!(m->mopt & (MC_SCROLL | MC_ALWAYS_SCROLL)) || m->h < 3) {
182 1.7 phil endwin();
183 1.35 dsl (void)fprintf(stderr,
184 1.17 cgd "Window too short for menu \"%s\"\n",
185 1.40 dsl title);
186 1.7 phil exit(1);
187 1.7 phil }
188 1.38 dsl hadd++;
189 1.51 dsl m->h = MIN(m->h, max_lines - y - hadd);
190 1.51 dsl i = strlen(scrolltext);
191 1.51 dsl wmax = MAX(wmax, i);
192 1.51 dsl }
193 1.9 phil
194 1.1 phil /* Calculate w? */
195 1.50 dsl if (w == 0) {
196 1.49 dsl int l;
197 1.49 dsl for (i = 0; i < m->numopts; i++) {
198 1.49 dsl l = strlen(MSG_XLAT(m->opts[i].opt_name));
199 1.49 dsl if (!(m->mopt & MC_NOSHORTCUT))
200 1.49 dsl l += 3;
201 1.49 dsl wmax = MAX(wmax, l);
202 1.49 dsl }
203 1.50 dsl w = wmax;
204 1.1 phil }
205 1.1 phil
206 1.17 cgd /* check and adjust for screen fit */
207 1.50 dsl if (w + wadd > max_cols) {
208 1.17 cgd endwin();
209 1.35 dsl (void)fprintf(stderr,
210 1.40 dsl "Screen too narrow for menu \"%s\"\n", title);
211 1.17 cgd exit(1);
212 1.17 cgd
213 1.17 cgd }
214 1.50 dsl if (x == -1)
215 1.50 dsl x = (max_cols - (w + wadd)) / 2; /* center */
216 1.50 dsl else if (x + w + wadd > max_cols)
217 1.50 dsl x = max_cols - (w + wadd); /* right align */
218 1.17 cgd
219 1.1 phil /* Get the windows. */
220 1.50 dsl m->mw = newwin(m->h + hadd, w + wadd, y, x);
221 1.1 phil
222 1.1 phil if (m->mw == NULL) {
223 1.1 phil endwin();
224 1.35 dsl (void)fprintf(stderr,
225 1.42 dsl "Could not create window (%d + %d, %d + %d, %d, %d) for menu \"%s\"\n",
226 1.50 dsl m->h, hadd, w, wadd, y, x, title);
227 1.1 phil exit(1);
228 1.26 perry }
229 1.38 dsl keypad(m->mw, TRUE); /* enable multi-key assembling for win */
230 1.26 perry
231 1.26 perry /* XXX is it even worth doing this right? */
232 1.26 perry if (has_colors()) {
233 1.26 perry wbkgd(m->mw, COLOR_PAIR(1));
234 1.26 perry wattrset(m->mw, COLOR_PAIR(1));
235 1.26 perry }
236 1.1 phil }
237 1.1 phil
238 1.23 hubertf static char
239 1.43 dsl opt_ch(menudesc *m, int op_no)
240 1.14 phil {
241 1.14 phil char c;
242 1.43 dsl
243 1.43 dsl if (op_no == m->numopts)
244 1.43 dsl return 'x';
245 1.43 dsl
246 1.14 phil if (op_no < 25) {
247 1.14 phil c = 'a' + op_no;
248 1.35 dsl if (c >= 'x')
249 1.35 dsl c++;
250 1.14 phil } else
251 1.14 phil c = 'A' + op_no - 25;
252 1.35 dsl return c;
253 1.14 phil }
254 1.14 phil
255 1.23 hubertf static void
256 1.43 dsl draw_menu_line(menudesc *m, int opt, int cury, void *arg, const char *text)
257 1.36 dsl {
258 1.36 dsl int hasbox = m->mopt & MC_NOBOX ? 0 : 1;
259 1.36 dsl
260 1.43 dsl if (m->cursel == opt) {
261 1.36 dsl mvwaddstr(m->mw, cury, hasbox, ">");
262 1.36 dsl wstandout(m->mw);
263 1.36 dsl } else
264 1.36 dsl mvwaddstr(m->mw, cury, hasbox, " ");
265 1.36 dsl if (!(m->mopt & MC_NOSHORTCUT))
266 1.43 dsl wprintw(m->mw, "%c: ", opt_ch(m, opt));
267 1.43 dsl
268 1.43 dsl if (!text && m->draw_line)
269 1.43 dsl m->draw_line(m, opt, arg);
270 1.43 dsl else
271 1.43 dsl waddstr(m->mw, MSG_XLAT(text));
272 1.43 dsl if (m->cursel == opt)
273 1.36 dsl wstandend(m->mw);
274 1.36 dsl }
275 1.36 dsl
276 1.36 dsl static void
277 1.43 dsl draw_menu(menudesc *m, void *arg)
278 1.1 phil {
279 1.38 dsl int opt;
280 1.36 dsl int hasbox, cury, maxy;
281 1.1 phil int tadd;
282 1.45 dsl int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
283 1.50 dsl const char *tp, *ep;
284 1.1 phil
285 1.50 dsl hasbox = (m->mopt & MC_NOBOX ? 0 : 1);
286 1.1 phil
287 1.9 phil /* Clear the window */
288 1.35 dsl wclear(m->mw);
289 1.9 phil
290 1.50 dsl tadd = hasbox;
291 1.40 dsl if (m->title) {
292 1.50 dsl for (tp = MSG_XLAT(m->title); ; tp = ep + 1) {
293 1.50 dsl ep = strchr(tp , '\n');
294 1.50 dsl mvwaddnstr(m->mw, tadd++, hasbox + 1, tp,
295 1.50 dsl ep ? ep - tp : -1);
296 1.50 dsl if (ep == NULL || *ep == 0)
297 1.50 dsl break;
298 1.50 dsl }
299 1.50 dsl tadd++;
300 1.50 dsl }
301 1.50 dsl
302 1.50 dsl cury = tadd;
303 1.50 dsl maxy = getmaxy(m->mw) - hasbox;
304 1.51 dsl if (m->numopts + hasexit > m->h)
305 1.51 dsl /* allow for scroll text */
306 1.51 dsl maxy--;
307 1.1 phil
308 1.39 dsl if (m->cursel == -1) {
309 1.39 dsl m->cursel = m->numopts;
310 1.39 dsl if (m->h <= m->numopts)
311 1.39 dsl m->topline = m->numopts + 1 - m->h;
312 1.39 dsl }
313 1.39 dsl
314 1.45 dsl while (m->cursel >= m->topline + m->h)
315 1.45 dsl m->topline = MIN(m->topline + m->h,
316 1.45 dsl m->numopts + hasexit - m->h);
317 1.45 dsl while (m->cursel < m->topline)
318 1.45 dsl m->topline = MAX(0, m->topline - m->h);
319 1.45 dsl
320 1.38 dsl for (opt = m->topline; opt < m->numopts; opt++) {
321 1.36 dsl if (cury >= maxy)
322 1.36 dsl break;
323 1.43 dsl draw_menu_line(m, opt, cury++, arg, m->opts[opt].opt_name);
324 1.1 phil }
325 1.7 phil
326 1.7 phil /* Add the exit option. */
327 1.38 dsl if (!(m->mopt & MC_NOEXITOPT)) {
328 1.38 dsl if (cury < maxy)
329 1.43 dsl draw_menu_line(m, m->numopts, cury++, arg, m->exitstr);
330 1.38 dsl else
331 1.38 dsl opt = 0;
332 1.38 dsl }
333 1.7 phil
334 1.7 phil /* Add the scroll line */
335 1.38 dsl if (opt != m->numopts || m->topline != 0)
336 1.35 dsl mvwaddstr(m->mw, cury, hasbox, scrolltext);
337 1.7 phil
338 1.7 phil /* Add the box. */
339 1.14 phil if (!(m->mopt & MC_NOBOX))
340 1.26 perry box(m->mw, 0, 0);
341 1.1 phil
342 1.50 dsl wmove(m->mw, tadd + m->cursel - m->topline, hasbox);
343 1.36 dsl wrefresh(m->mw);
344 1.1 phil }
345 1.1 phil
346 1.23 hubertf static void
347 1.30 christos /*ARGSUSED*/
348 1.52 dsl process_help(menudesc *m)
349 1.5 phil {
350 1.34 dsl const char *help = m->helpstr;
351 1.7 phil int lineoff = 0;
352 1.5 phil int curoff = 0;
353 1.5 phil int again;
354 1.7 phil int winin;
355 1.5 phil
356 1.6 phil /* Is there help? */
357 1.6 phil if (!help) {
358 1.6 phil mbeep();
359 1.6 phil return;
360 1.6 phil }
361 1.40 dsl help = MSG_XLAT(help);
362 1.6 phil
363 1.6 phil /* Display the help information. */
364 1.7 phil do {
365 1.5 phil if (lineoff < curoff) {
366 1.40 dsl help = MSG_XLAT(m->helpstr);
367 1.5 phil curoff = 0;
368 1.5 phil }
369 1.5 phil while (*help && curoff < lineoff) {
370 1.5 phil if (*help == '\n')
371 1.5 phil curoff++;
372 1.5 phil help++;
373 1.5 phil }
374 1.5 phil
375 1.5 phil wclear(stdscr);
376 1.35 dsl mvwaddstr(stdscr, 0, 0,
377 1.9 phil "Help: exit: x, page up: u <, page down: d >");
378 1.35 dsl mvwaddstr(stdscr, 2, 0, help);
379 1.35 dsl wmove(stdscr, 1, 0);
380 1.36 dsl wrefresh(stdscr);
381 1.5 phil
382 1.5 phil do {
383 1.29 blymn winin = wgetch(stdscr);
384 1.29 blymn if (winin < KEY_MIN)
385 1.9 phil winin = tolower(winin);
386 1.5 phil again = 0;
387 1.5 phil switch (winin) {
388 1.5 phil case '<':
389 1.5 phil case 'u':
390 1.29 blymn case KEY_UP:
391 1.29 blymn case KEY_LEFT:
392 1.29 blymn case KEY_PPAGE:
393 1.5 phil if (lineoff)
394 1.5 phil lineoff -= max_lines - 2;
395 1.7 phil else
396 1.7 phil again = 1;
397 1.5 phil break;
398 1.5 phil case '>':
399 1.5 phil case 'd':
400 1.29 blymn case KEY_DOWN:
401 1.29 blymn case KEY_RIGHT:
402 1.29 blymn case KEY_NPAGE:
403 1.5 phil if (*help)
404 1.5 phil lineoff += max_lines - 2;
405 1.7 phil else
406 1.7 phil again = 1;
407 1.5 phil break;
408 1.10 phil case 'q':
409 1.10 phil break;
410 1.9 phil case 'x':
411 1.10 phil winin = 'q';
412 1.5 phil break;
413 1.5 phil default:
414 1.5 phil again = 1;
415 1.5 phil }
416 1.9 phil if (again)
417 1.9 phil mbeep();
418 1.5 phil } while (again);
419 1.5 phil } while (winin != 'q');
420 1.5 phil }
421 1.5 phil
422 1.23 hubertf static void
423 1.52 dsl process_req(menudesc *m, void *arg, int req)
424 1.1 phil {
425 1.1 phil int ch;
426 1.35 dsl int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
427 1.1 phil
428 1.35 dsl switch(req) {
429 1.35 dsl
430 1.35 dsl case REQ_EXECUTE:
431 1.1 phil return;
432 1.7 phil
433 1.35 dsl case REQ_NEXT_ITEM:
434 1.41 dsl ch = m->cursel;
435 1.41 dsl for (;;) {
436 1.41 dsl ch++;
437 1.41 dsl if (ch >= m->numopts + hasexit) {
438 1.41 dsl mbeep();
439 1.41 dsl return;
440 1.41 dsl }
441 1.41 dsl if (hasexit && ch == m->numopts)
442 1.41 dsl break;
443 1.43 dsl if (!(m->opts[ch].opt_flags & OPT_IGNORE))
444 1.41 dsl break;
445 1.35 dsl }
446 1.41 dsl m->cursel = ch;
447 1.48 dsl if (m->cursel >= m->topline + m->h)
448 1.41 dsl m->topline = m->cursel - m->h + 1;
449 1.35 dsl break;
450 1.7 phil
451 1.35 dsl case REQ_PREV_ITEM:
452 1.41 dsl ch = m->cursel;
453 1.41 dsl for (;;) {
454 1.41 dsl if (ch <= 0) {
455 1.41 dsl mbeep();
456 1.41 dsl return;
457 1.41 dsl }
458 1.41 dsl ch--;
459 1.43 dsl if (!(m->opts[ch].opt_flags & OPT_IGNORE))
460 1.41 dsl break;
461 1.35 dsl }
462 1.41 dsl m->cursel = ch;
463 1.35 dsl if (m->cursel < m->topline)
464 1.41 dsl m->topline = m->cursel;
465 1.35 dsl break;
466 1.3 phil
467 1.39 dsl case REQ_HELP:
468 1.52 dsl process_help(m);
469 1.39 dsl /* FALLTHROUGH */
470 1.39 dsl
471 1.35 dsl case REQ_REDISPLAY:
472 1.3 phil wclear(stdscr);
473 1.3 phil wrefresh(stdscr);
474 1.14 phil if (m->post_act)
475 1.39 dsl (*m->post_act)(m, arg);
476 1.35 dsl break;
477 1.7 phil
478 1.35 dsl case REQ_SCROLLUP:
479 1.35 dsl if (m->cursel == 0) {
480 1.7 phil mbeep();
481 1.35 dsl return;
482 1.7 phil }
483 1.36 dsl m->topline = MAX(0, m->topline - m->h);
484 1.36 dsl m->cursel = MAX(0, m->cursel - m->h);
485 1.35 dsl wclear(m->mw);
486 1.35 dsl break;
487 1.7 phil
488 1.35 dsl case REQ_SCROLLDOWN:
489 1.35 dsl if (m->cursel >= m->numopts + hasexit - 1) {
490 1.7 phil mbeep();
491 1.35 dsl return;
492 1.7 phil }
493 1.36 dsl m->topline = MIN(m->topline + m->h,
494 1.38 dsl MAX(m->numopts + hasexit - m->h, 0));
495 1.36 dsl m->cursel = MIN(m->numopts + hasexit - 1, m->cursel + m->h);
496 1.35 dsl wclear(m->mw);
497 1.35 dsl break;
498 1.7 phil
499 1.35 dsl default:
500 1.9 phil ch = req;
501 1.7 phil if (ch == 'x' && hasexit) {
502 1.1 phil m->cursel = m->numopts;
503 1.35 dsl break;
504 1.35 dsl }
505 1.35 dsl if (m->mopt & MC_NOSHORTCUT) {
506 1.35 dsl mbeep();
507 1.35 dsl return;
508 1.35 dsl }
509 1.35 dsl if (ch > 'z')
510 1.35 dsl ch = 255;
511 1.35 dsl if (ch >= 'a') {
512 1.35 dsl if (ch > 'x')
513 1.35 dsl ch--;
514 1.35 dsl ch = ch - 'a';
515 1.35 dsl } else
516 1.35 dsl ch = 25 + ch - 'A';
517 1.35 dsl if (ch < 0 || ch >= m->numopts) {
518 1.41 dsl mbeep();
519 1.41 dsl return;
520 1.41 dsl }
521 1.43 dsl if (m->opts[ch].opt_flags & OPT_IGNORE) {
522 1.35 dsl mbeep();
523 1.35 dsl return;
524 1.35 dsl }
525 1.35 dsl m->cursel = ch;
526 1.1 phil }
527 1.9 phil
528 1.43 dsl draw_menu(m, arg);
529 1.1 phil }
530 1.1 phil
531 1.23 hubertf int
532 1.35 dsl menu_init(void)
533 1.17 cgd {
534 1.52 dsl int i;
535 1.17 cgd
536 1.17 cgd if (__menu_init)
537 1.17 cgd return 0;
538 1.33 dsl
539 1.33 dsl #ifdef USER_MENU_INIT
540 1.33 dsl if (USER_MENU_INIT)
541 1.33 dsl return 1;
542 1.33 dsl #endif
543 1.17 cgd
544 1.17 cgd if (initscr() == NULL)
545 1.17 cgd return 1;
546 1.17 cgd
547 1.17 cgd cbreak();
548 1.17 cgd noecho();
549 1.26 perry
550 1.26 perry /* XXX Should be configurable but it almost isn't worth it. */
551 1.26 perry if (has_colors()) {
552 1.26 perry start_color();
553 1.26 perry init_pair(1, COLOR_WHITE, COLOR_BLUE);
554 1.26 perry bkgd(COLOR_PAIR(1));
555 1.26 perry attrset(COLOR_PAIR(1));
556 1.26 perry }
557 1.26 perry
558 1.17 cgd max_lines = getmaxy(stdscr);
559 1.17 cgd max_cols = getmaxx(stdscr);
560 1.29 blymn keypad(stdscr, TRUE);
561 1.17 cgd #ifdef DYNAMIC_MENUS
562 1.17 cgd num_menus = DYN_INIT_NUM;
563 1.17 cgd while (num_menus < DYN_MENU_START)
564 1.17 cgd num_menus *= 2;
565 1.52 dsl menu_list = malloc(sizeof *menu_list * num_menus);
566 1.52 dsl if (menu_list == NULL)
567 1.17 cgd return 2;
568 1.52 dsl (void)memset(menu_list, 0, sizeof *menu_list * num_menus);
569 1.52 dsl for (i = 0; i < DYN_MENU_START; i++)
570 1.52 dsl menu_list[i] = &menu_def[i];
571 1.17 cgd #endif
572 1.17 cgd
573 1.17 cgd __menu_init = 1;
574 1.36 dsl return 0;
575 1.17 cgd }
576 1.17 cgd
577 1.23 hubertf void
578 1.37 dsl process_menu(int num, void *arg)
579 1.1 phil {
580 1.1 phil int sel = 0;
581 1.50 dsl int req;
582 1.37 dsl menu_ent *opt;
583 1.1 phil
584 1.39 dsl menudesc *m;
585 1.14 phil
586 1.52 dsl m = &MENUS(num);
587 1.1 phil
588 1.1 phil /* Initialize? */
589 1.17 cgd if (menu_init()) {
590 1.17 cgd __menu_initerror();
591 1.17 cgd return;
592 1.1 phil }
593 1.17 cgd
594 1.1 phil if (__m_endwin) {
595 1.7 phil wclear(stdscr);
596 1.1 phil wrefresh(stdscr);
597 1.1 phil __m_endwin = 0;
598 1.1 phil }
599 1.1 phil
600 1.39 dsl /* Default to select option 0 and display from 0 */
601 1.7 phil m->topline = 0;
602 1.39 dsl if ((m->mopt & (MC_DFLTEXIT | MC_NOEXITOPT)) == MC_DFLTEXIT)
603 1.39 dsl m->cursel = -1;
604 1.39 dsl else
605 1.39 dsl m->cursel = 0;
606 1.1 phil
607 1.50 dsl for (;;) {
608 1.1 phil if (__m_endwin) {
609 1.1 phil wclear(stdscr);
610 1.1 phil wrefresh(stdscr);
611 1.1 phil __m_endwin = 0;
612 1.1 phil }
613 1.1 phil /* Process the display action */
614 1.14 phil if (m->post_act)
615 1.39 dsl (*m->post_act)(m, arg);
616 1.47 dsl if (m->mw == NULL)
617 1.47 dsl init_menu(m);
618 1.43 dsl draw_menu(m, arg);
619 1.1 phil
620 1.36 dsl while ((req = menucmd(m->mw)) != REQ_EXECUTE)
621 1.52 dsl process_req(m, arg, req);
622 1.1 phil
623 1.1 phil sel = m->cursel;
624 1.38 dsl if (!(m->mopt & MC_NOCLEAR)) {
625 1.38 dsl wclear(m->mw);
626 1.38 dsl wrefresh(m->mw);
627 1.38 dsl }
628 1.1 phil
629 1.1 phil /* Process the items */
630 1.50 dsl if (sel >= m->numopts)
631 1.50 dsl /* exit option */
632 1.50 dsl break;
633 1.50 dsl
634 1.50 dsl opt = &m->opts[sel];
635 1.50 dsl if (opt->opt_flags & OPT_IGNORE)
636 1.50 dsl continue;
637 1.50 dsl if (opt->opt_flags & OPT_ENDWIN) {
638 1.50 dsl endwin();
639 1.50 dsl __m_endwin = 1;
640 1.50 dsl }
641 1.50 dsl if (opt->opt_action && (*opt->opt_action)(m, arg))
642 1.50 dsl break;
643 1.50 dsl
644 1.50 dsl if (opt->opt_menu != -1) {
645 1.50 dsl if (!(opt->opt_flags & OPT_SUB)) {
646 1.50 dsl num = opt->opt_menu;
647 1.50 dsl delwin(m->mw);
648 1.50 dsl m->mw = NULL;
649 1.52 dsl m = &MENUS(num);
650 1.43 dsl continue;
651 1.14 phil }
652 1.50 dsl process_menu(opt->opt_menu, arg);
653 1.50 dsl }
654 1.50 dsl if (opt->opt_flags & OPT_EXIT)
655 1.50 dsl break;
656 1.1 phil }
657 1.1 phil
658 1.38 dsl if (m->mopt & MC_NOCLEAR) {
659 1.38 dsl wclear(m->mw);
660 1.38 dsl wrefresh(m->mw);
661 1.38 dsl }
662 1.38 dsl
663 1.1 phil /* Process the exit action */
664 1.14 phil if (m->exit_act)
665 1.39 dsl (*m->exit_act)(m, arg);
666 1.50 dsl delwin(m->mw);
667 1.50 dsl m->mw = NULL;
668 1.1 phil }
669 1.13 phil
670 1.52 dsl
671 1.52 dsl void
672 1.52 dsl set_menu_numopts(int menu, int numopts)
673 1.52 dsl {
674 1.52 dsl
675 1.52 dsl MENUS(menu).numopts = numopts;
676 1.52 dsl }
677 1.52 dsl
678 1.13 phil /* Control L is end of standard routines, remaining only for dynamic. */
680 1.13 phil
681 1.13 phil /* Beginning of routines for dynamic menus. */
682 1.24 phil
683 1.35 dsl static int
684 1.14 phil double_menus(void)
685 1.52 dsl {
686 1.52 dsl menudesc **temp;
687 1.14 phil int sz = sizeof *menu_list * num_menus;
688 1.52 dsl
689 1.14 phil temp = realloc(menu_list, sz * 2);
690 1.14 phil if (temp == NULL)
691 1.39 dsl return 0;
692 1.52 dsl (void)memset(temp + num_menus, 0, sz);
693 1.14 phil menu_list = temp;
694 1.14 phil num_menus *= 2;
695 1.14 phil
696 1.14 phil return 1;
697 1.14 phil }
698 1.23 hubertf
699 1.38 dsl int
700 1.24 phil new_menu(const char *title, menu_ent *opts, int numopts,
701 1.39 dsl int x, int y, int h, int w, int mopt,
702 1.43 dsl void (*post_act)(menudesc *, void *),
703 1.39 dsl void (*draw_line)(menudesc *, int, void *),
704 1.44 dsl void (*exit_act)(menudesc *, void *),
705 1.14 phil const char *help, const char *exit_str)
706 1.14 phil {
707 1.39 dsl int ix;
708 1.7 phil menudesc *m;
709 1.39 dsl
710 1.39 dsl /* Find free menu entry. */
711 1.46 takemura for (ix = DYN_MENU_START; ; ix++) {
712 1.14 phil if (ix >= num_menus && !double_menus())
713 1.52 dsl return -1;
714 1.52 dsl m = menu_list[ix];
715 1.52 dsl if (m == NULL) {
716 1.52 dsl m = calloc(sizeof *m, 1);
717 1.52 dsl if (m == NULL)
718 1.52 dsl return -1;
719 1.52 dsl menu_list[ix] = m;
720 1.52 dsl break;
721 1.52 dsl }
722 1.39 dsl if (!(m->mopt & MC_VALID))
723 1.39 dsl break;
724 1.14 phil }
725 1.14 phil
726 1.40 dsl /* Set Entries */
727 1.39 dsl m->title = title;
728 1.39 dsl m->opts = opts;
729 1.39 dsl m->numopts = numopts;
730 1.39 dsl m->x = x;
731 1.39 dsl m->y = y;
732 1.39 dsl m->h = h;
733 1.39 dsl m->w = w;
734 1.39 dsl m->mopt = mopt | MC_VALID;
735 1.43 dsl m->post_act = post_act;
736 1.39 dsl m->draw_line = draw_line;
737 1.39 dsl m->exit_act = exit_act;
738 1.44 dsl m->helpstr = help;
739 1.14 phil m->exitstr = exit_str ? exit_str : "Exit";
740 1.14 phil
741 1.14 phil return ix;
742 1.14 phil }
743 1.23 hubertf
744 1.35 dsl void
745 1.14 phil free_menu(int menu_no)
746 1.39 dsl {
747 1.39 dsl menudesc *m;
748 1.39 dsl
749 1.39 dsl if (menu_no < 0 || menu_no >= num_menus)
750 1.39 dsl return;
751 1.52 dsl
752 1.50 dsl m = menu_list[menu_no];
753 1.39 dsl if (!(m->mopt & MC_VALID))
754 1.39 dsl return;
755 1.39 dsl if (m->mw != NULL)
756 1.39 dsl delwin(m->mw);
757 1.39 dsl memset(m, 0, sizeof *m);
758 }
759