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