/
menu.c
746 lines (630 loc) · 16.8 KB
/
menu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/* $NetBSD: menu.c,v 1.16 2003/03/09 01:08:48 lukem Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn (blymn@baea.com.au, brett_lymn@yahoo.com.au)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: menu.c,v 1.16 2003/03/09 01:08:48 lukem Exp $");
#include <ctype.h>
#include <menu.h>
#include <string.h>
#include <stdlib.h>
#include "internals.h"
MENU _menui_default_menu = {
16, /* number of item rows that will fit in window */
1, /* number of columns of items that will fit in window */
0, /* number of rows of items we have */
0, /* number of columns of items we have */
0, /* current cursor row */
0, /* current cursor column */
{NULL, 0}, /* mark string */
{NULL, 0}, /* unmark string */
O_ONEVALUE, /* menu options */
NULL, /* the pattern buffer */
0, /* length of pattern buffer */
0, /* the length of matched buffer */
0, /* is the menu posted? */
A_REVERSE, /* menu foreground */
A_NORMAL, /* menu background */
A_UNDERLINE, /* unselectable menu item */
' ', /* filler between name and description */
NULL, /* user defined pointer */
0, /* top row of menu */
0, /* widest item in the menu */
0, /* the width of a menu column */
0, /* number of items attached to the menu */
NULL, /* items in the menu */
0, /* current menu item */
0, /* currently in a hook function */
NULL, /* function called when menu posted */
NULL, /* function called when menu is unposted */
NULL, /* function called when current item changes */
NULL, /* function called when current item changes */
NULL, /* the menu window */
NULL, /* the menu subwindow */
NULL, /* the window to write to */
};
/*
* Set the menu mark character
*/
int
set_menu_mark(MENU *m, char *mark)
{
MENU *menu = m;
if (m == NULL) menu = &_menui_default_menu;
/* if there was an old mark string, free it first */
if (menu->mark.string != NULL) free(menu->mark.string);
if ((menu->mark.string = (char *) malloc(strlen(mark) + 1)) == NULL)
return E_SYSTEM_ERROR;
strcpy(menu->mark.string, mark);
menu->mark.length = strlen(mark);
/* max item size may have changed - recalculate. */
_menui_max_item_size(menu);
return E_OK;
}
/*
* Return the menu mark string for the menu.
*/
char *
menu_mark(MENU *menu)
{
if (menu == NULL)
return _menui_default_menu.mark.string;
else
return menu->mark.string;
}
/*
* Set the menu unmark character
*/
int
set_menu_unmark(MENU *m, char *mark)
{
MENU *menu = m;
if (m == NULL) menu = &_menui_default_menu;
/* if there was an old mark string, free it first */
if (menu->unmark.string != NULL) free(menu->unmark.string);
if ((menu->unmark.string = (char *) malloc(strlen(mark) + 1)) == NULL)
return E_SYSTEM_ERROR;
strcpy(menu->unmark.string, mark);
menu->unmark.length = strlen(mark);
/* max item size may have changed - recalculate. */
_menui_max_item_size(menu);
return E_OK;
}
/*
* Return the menu unmark string for the menu.
*/
char *
menu_unmark(menu)
MENU *menu;
{
if (menu == NULL)
return _menui_default_menu.unmark.string;
else
return menu->unmark.string;
}
/*
* Set the menu window to the window passed.
*/
int
set_menu_win(MENU *menu, WINDOW *win)
{
if (menu == NULL) {
_menui_default_menu.menu_win = win;
_menui_default_menu.scrwin = win;
} else {
if (menu->posted == TRUE) {
return E_POSTED;
} else {
menu->menu_win = win;
menu->scrwin = win;
}
}
return E_OK;
}
/*
* Return the pointer to the menu window
*/
WINDOW *
menu_win(MENU *menu)
{
if (menu == NULL)
return _menui_default_menu.menu_win;
else
return menu->menu_win;
}
/*
* Set the menu subwindow for the menu.
*/
int
set_menu_sub(menu, sub)
MENU *menu;
WINDOW *sub;
{
if (menu == NULL) {
_menui_default_menu.menu_subwin = sub;
_menui_default_menu.scrwin = sub;
} else {
if (menu->posted == TRUE)
return E_POSTED;
menu->menu_subwin = sub;
menu->scrwin = sub;
}
return E_OK;
}
/*
* Return the subwindow pointer for the menu
*/
WINDOW *
menu_sub(MENU *menu)
{
if (menu == NULL)
return _menui_default_menu.menu_subwin;
else
return menu->menu_subwin;
}
/*
* Set the maximum number of rows and columns of items that may be displayed.
*/
int
set_menu_format(MENU *param_menu, int rows, int cols)
{
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
menu->rows = rows;
menu->cols = cols;
if (menu->items != NULL)
/* recalculate the item neighbours */
return _menui_stitch_items(menu);
return E_OK;
}
/*
* Return the max number of rows and cols that may be displayed.
*/
void
menu_format(MENU *param_menu, int *rows, int *cols)
{
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
*rows = menu->rows;
*cols = menu->cols;
}
/*
* Set the user defined function to call when a menu is posted.
*/
int
set_menu_init(MENU *menu, Menu_Hook func)
{
if (menu == NULL)
_menui_default_menu.menu_init = func;
else
menu->menu_init = func;
return E_OK;
}
/*
* Return the pointer to the menu init function.
*/
Menu_Hook
menu_init(MENU *menu)
{
if (menu == NULL)
return _menui_default_menu.menu_init;
else
return menu->menu_init;
}
/*
* Set the user defined function called when a menu is unposted.
*/
int
set_menu_term(MENU *menu, Menu_Hook func)
{
if (menu == NULL)
_menui_default_menu.menu_term = func;
else
menu->menu_term = func;
return E_OK;
}
/*
* Return the user defined menu termination function pointer.
*/
Menu_Hook
menu_term(MENU *menu)
{
if (menu == NULL)
return _menui_default_menu.menu_term;
else
return menu->menu_term;
}
/*
* Return the current menu options set.
*/
OPTIONS
menu_opts(MENU *menu)
{
if (menu == NULL)
return _menui_default_menu.opts;
else
return menu->opts;
}
/*
* Set the menu options to the given options.
*/
int
set_menu_opts(MENU *param_menu, OPTIONS opts)
{
int i, seen;
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
OPTIONS old_opts = menu->opts;
menu->opts = opts;
/*
* If the radio option is selected then make sure only one
* item is actually selected in the items.
*/
if (((opts & O_RADIO) == O_RADIO) && (menu->items != NULL) &&
(menu->items[0] != NULL)) {
seen = 0;
for (i = 0; i < menu->item_count; i++) {
if (menu->items[i]->selected == 1) {
if (seen == 0) {
seen = 1;
} else {
menu->items[i]->selected = 0;
}
}
}
/* if none selected, select the first item */
if (seen == 0)
menu->items[0]->selected = 1;
}
if ((menu->opts & O_ROWMAJOR) != (old_opts & O_ROWMAJOR))
/* changed menu layout - need to recalc neighbours */
_menui_stitch_items(menu);
return E_OK;
}
/*
* Turn on the options in menu given by opts.
*/
int
menu_opts_on(MENU *param_menu, OPTIONS opts)
{
int i, seen;
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
OPTIONS old_opts = menu->opts;
menu->opts |= opts;
/*
* If the radio option is selected then make sure only one
* item is actually selected in the items.
*/
if (((opts & O_RADIO) == O_RADIO) && (menu->items != NULL) &&
(menu->items[0] != NULL)) {
seen = 0;
for (i = 0; i < menu->item_count; i++) {
if (menu->items[i]->selected == 1) {
if (seen == 0) {
seen = 1;
} else {
menu->items[i]->selected = 0;
}
}
}
/* if none selected then select the top item */
if (seen == 0)
menu->items[0]->selected = 1;
}
if ((menu->items != NULL) &&
(menu->opts & O_ROWMAJOR) != (old_opts & O_ROWMAJOR))
/* changed menu layout - need to recalc neighbours */
_menui_stitch_items(menu);
return E_OK;
}
/*
* Turn off the menu options given in opts.
*/
int
menu_opts_off(MENU *param_menu, OPTIONS opts)
{
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
OPTIONS old_opts = menu->opts;
menu->opts &= ~(opts);
if ((menu->items != NULL ) &&
(menu->opts & O_ROWMAJOR) != (old_opts & O_ROWMAJOR))
/* changed menu layout - need to recalc neighbours */
_menui_stitch_items(menu);
return E_OK;
}
/*
* Return the menu pattern buffer.
*/
char *
menu_pattern(MENU *menu)
{
if (menu == NULL)
return _menui_default_menu.pattern;
else
return menu->pattern;
}
/*
* Set the menu pattern buffer to pat and attempt to match the pattern in
* the item list.
*/
int
set_menu_pattern(MENU *param_menu, char *pat)
{
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
char *p = pat;
/* check pattern is all printable characters */
while (*p)
if (!isprint((unsigned char) *p++)) return E_BAD_ARGUMENT;
if ((menu->pattern = (char *) realloc(menu->pattern,
sizeof(char) * strlen(pat) + 1)) == NULL)
return E_SYSTEM_ERROR;
strcpy(menu->pattern, pat);
menu->plen = strlen(pat);
/* search item list for pat here */
return _menui_match_items(menu, MATCH_FORWARD, &menu->cur_item);
}
/*
* Allocate a new menu structure and fill it in.
*/
MENU *
new_menu(ITEM **items)
{
MENU *the_menu;
if ((the_menu = (MENU *)malloc(sizeof(MENU))) == NULL)
return NULL;
/* copy the defaults */
(void)memcpy(the_menu, &_menui_default_menu, sizeof(MENU));
/* set a default window if none already set. */
if (the_menu->menu_win == NULL)
the_menu->scrwin = stdscr;
/* make a private copy of the mark string */
if (_menui_default_menu.mark.string != NULL) {
if ((the_menu->mark.string =
(char *) malloc((unsigned) _menui_default_menu.mark.length + 1))
== NULL) {
free(the_menu);
return NULL;
}
strlcpy(the_menu->mark.string, _menui_default_menu.mark.string,
(unsigned) _menui_default_menu.mark.length + 1);
}
/* make a private copy of the unmark string too */
if (_menui_default_menu.unmark.string != NULL) {
if ((the_menu->unmark.string =
(char *) malloc((unsigned) _menui_default_menu.unmark.length + 1))
== NULL) {
free(the_menu);
return NULL;
}
strlcpy(the_menu->unmark.string,
_menui_default_menu.unmark.string,
(unsigned) _menui_default_menu.unmark.length+ 1 );
}
/* now attach the items, if any */
if (items != NULL) {
if(set_menu_items(the_menu, items) < 0) {
if (the_menu->mark.string != NULL)
free(the_menu->mark.string);
if (the_menu->unmark.string != NULL)
free(the_menu->unmark.string);
free(the_menu);
return NULL;
}
}
return the_menu;
}
/*
* Free up storage allocated to the menu object and destroy it.
*/
int
free_menu(MENU *menu)
{
int i;
if (menu == NULL)
return E_BAD_ARGUMENT;
if (menu->posted != 0)
return E_POSTED;
if (menu->pattern != NULL)
free(menu->pattern);
if (menu->mark.string != NULL)
free(menu->mark.string);
if (menu->items != NULL) {
/* disconnect the items from this menu */
for (i = 0; i < menu->item_count; i++) {
menu->items[i]->parent = NULL;
}
}
free(menu);
return E_OK;
}
/*
* Calculate the minimum window size for the menu.
*/
int
scale_menu(MENU *param_menu, int *rows, int *cols)
{
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
if (menu->items == NULL)
return E_BAD_ARGUMENT;
/* calculate the max item size */
_menui_max_item_size(menu);
*rows = menu->rows;
*cols = menu->cols * menu->max_item_width;
/*
* allow for spacing between columns...
*/
*cols += (menu->cols - 1);
return E_OK;
}
/*
* Set the menu item list to the one given.
*/
int
set_menu_items(MENU *param_menu, ITEM **items)
{
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
int i, new_count = 0, sel_count = 0;
/* don't change if menu is posted */
if (menu->posted == 1)
return E_POSTED;
/* count the new items and validate none are connected already */
while (items[new_count] != NULL) {
if ((items[new_count]->parent != NULL) &&
(items[new_count]->parent != menu))
return E_CONNECTED;
if (items[new_count]->selected == 1)
sel_count++;
new_count++;
}
/*
* don't allow multiple selected items if menu is radio
* button style.
*/
if (((menu->opts & O_RADIO) == O_RADIO) &&
(sel_count > 1))
return E_BAD_ARGUMENT;
/* if there were items connected then disconnect them. */
if (menu->items != NULL) {
for (i = 0; i < menu->item_count; i++) {
menu->items[i]->parent = NULL;
menu->items[i]->index = -1;
}
}
menu->item_count = new_count;
/* connect the new items to the menu */
for (i = 0; i < new_count; i++) {
items[i]->parent = menu;
items[i]->index = i;
}
menu->items = items;
menu->cur_item = 0; /* reset current item just in case */
menu->top_row = 0; /* and the top row too */
if (menu->pattern != NULL) { /* and the pattern buffer....sigh */
free(menu->pattern);
menu->plen = 0;
menu->match_len = 0;
}
/*
* make sure at least one item is selected on a radio
* button style menu.
*/
if (((menu->opts & O_RADIO) == O_RADIO) && (sel_count == 0))
menu->items[0]->selected = 1;
_menui_stitch_items(menu); /* recalculate the item neighbours */
return E_OK;
}
/*
* Return the pointer to the menu items array.
*/
ITEM **
menu_items(MENU *menu)
{
if (menu == NULL)
return _menui_default_menu.items;
else
return menu->items;
}
/*
* Return the count of items connected to the menu
*/
int
item_count(MENU *menu)
{
if (menu == NULL)
return _menui_default_menu.item_count;
else
return menu->item_count;
}
/*
* Set the menu top row to be the given row. The current item becomes the
* leftmost item on that row in the menu.
*/
int
set_top_row(MENU *param_menu, int row)
{
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
int i, cur_item, state = E_SYSTEM_ERROR;
if (row > menu->item_rows)
return E_BAD_ARGUMENT;
if (menu->items == NULL)
return E_NOT_CONNECTED;
if (menu->in_init == 1)
return E_BAD_STATE;
cur_item = 0;
for (i = 0; i < menu->item_count; i++) {
/* search for first item that matches row - this will be
the current item. */
if (row == menu->items[i]->row) {
cur_item = i;
state = E_OK;
break; /* found what we want - no need to go further */
}
}
menu->in_init = 1; /* just in case we call the init/term routines */
if (menu->posted == 1) {
if (menu->menu_term != NULL)
menu->menu_term(menu);
if (menu->item_term != NULL)
menu->item_term(menu);
}
menu->cur_item = cur_item;
menu->top_row = row;
if (menu->posted == 1) {
if (menu->menu_init != NULL)
menu->menu_init(menu);
if (menu->item_init != NULL)
menu->item_init(menu);
}
menu->in_init = 0;
/* this should always be E_OK unless we are really screwed up */
return state;
}
/*
* Return the current top row number.
*/
int
top_row(MENU *param_menu)
{
MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
if (menu->items == NULL)
return E_NOT_CONNECTED;
return menu->top_row;
}
/*
* Position the cursor at the correct place in the menu.
*
*/
int
pos_menu_cursor(MENU *menu)
{
int movx, maxmark;
if (menu == NULL)
return E_BAD_ARGUMENT;
maxmark = max(menu->mark.length, menu->unmark.length);
movx = maxmark + (menu->items[menu->cur_item]->col
* (menu->col_width + 1));
if (menu->match_len > 0)
movx += menu->match_len - 1;
wmove(menu->scrwin,
menu->items[menu->cur_item]->row - menu->top_row, movx);
return E_OK;
}