File: menurt.c

package info (click to toggle)
iptraf 3.0.0-8.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,928 kB
  • ctags: 1,229
  • sloc: ansic: 12,563; sh: 151; makefile: 137; awk: 1
file content (296 lines) | stat: -rw-r--r-- 6,813 bytes parent folder | download | duplicates (4)
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
/***

menurt.c- ncurses-based menu definition module
Written by Gerard Paul Java
Copyright (c) Gerard Paul Java 1997, 1998

This program is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License in the included COPYING file for
details.

***/

#include <curses.h>
#include <panel.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "menurt.h"
#include "winops.h"
#include "labels.h"

/* initialize menu system */

void tx_initmenu(struct MENU *menu, int y1, int x1, int y2, int x2,
    int borderattr, int normalattr, int highattr,
    int barnormalattr, int barhighattr, int descattr)
{
    menu->itemlist = NULL;
    menu->itemcount = 0;
    strcpy(menu->shortcuts, "");
    menu->x1 = x1;
    menu->y1 = y1;
    menu->x2 = x2;
    menu->y2 = y2;
    menu->menuwin = newwin(y1, x1, y2, x2);
    menu->menupanel = new_panel(menu->menuwin);
    menu->menu_maxx = x1 - 2;

    keypad(menu->menuwin, 1);
    meta(menu->menuwin, 1);
    noecho();
    wtimeout(menu->menuwin, -1);	/* block until input */
    notimeout(menu->menuwin, 0);	/* disable Esc timer */
    nonl();
    cbreak();
    
    menu->borderattr = borderattr;
    menu->normalattr = normalattr;
    menu->highattr = highattr;
    menu->barnormalattr = barnormalattr;
    menu->barhighattr = barhighattr;
    menu->descriptionattr = descattr;
}

/* add menu item */

void tx_additem(struct MENU *menu, char *item, char *desc)
{
    struct ITEM *tnode;
    char cur_option[OPTIONSTRLEN_MAX];
    char thekey[2];

    if (menu->itemcount >= 25)
	return;

    tnode = malloc(sizeof(struct ITEM));

    if (item != NULL) {
	strcpy(tnode->option, item);
	strcpy(tnode->desc, desc);
	tnode->itemtype = REGULARITEM;

	strcpy(cur_option, item);
	strtok(cur_option, "^");
	strcpy(thekey, strtok(NULL, "^"));
	thekey[0] = toupper(thekey[0]);
	strcat(menu->shortcuts, thekey);
    } else {
	tnode->itemtype = SEPARATOR;
	strcat(menu->shortcuts, "^");	/* mark shortcut position for seps */
    }

    if (menu->itemlist == NULL) {
	menu->itemlist = tnode;
    } else {
	menu->lastitem->next = tnode;
	tnode->prev = menu->lastitem;
    }

    menu->itemlist->prev = tnode;
    menu->lastitem = tnode;
    tnode->next = menu->itemlist;
    menu->itemcount++;
}

/* show each individual item */

void tx_showitem(struct MENU *menu, struct ITEM *itemptr, int selected)
{
    int hiattr = 0;
    int loattr = 0;
    int ctr;
    char curoption[OPTIONSTRLEN_MAX];
    char padding[OPTIONSTRLEN_MAX];

    if (itemptr->itemtype == REGULARITEM) {
	switch (selected) {
	case NOTSELECTED:
	    hiattr = menu->highattr;
	    loattr = menu->normalattr;
	    break;
	case SELECTED:
	    hiattr = menu->barhighattr;
	    loattr = menu->barnormalattr;
	    break;
	}

	strcpy(curoption, itemptr->option);

	wattrset(menu->menuwin, loattr);
	wprintw(menu->menuwin, "%s", strtok(curoption, "^"));
	wattrset(menu->menuwin, hiattr);
	wprintw(menu->menuwin, "%s", strtok((char *) NULL, "^"));
	wattrset(menu->menuwin, loattr);
	wprintw(menu->menuwin, "%s", strtok((char *) NULL, "^"));

	strcpy(padding, "");

	for (ctr = strlen(itemptr->option); ctr <= menu->x1 - 1; ctr++)
	    strcat(padding, " ");

	wprintw(menu->menuwin, "%s", padding);
    } else {
	wattrset(menu->menuwin, menu->borderattr);
	whline(menu->menuwin, ACS_HLINE, menu->menu_maxx);
    }

    update_panels();
    doupdate();
}

/* repeatedly calls tx_showitem to display individual items */

void tx_showmenu(struct MENU *menu)
{
    struct ITEM *itemptr;	/* points to each item in turn */
    int ctr = 1;		/* counts each item */

    wattrset(menu->menuwin, menu->borderattr);	/* set to bg+/b */
    tx_colorwin(menu->menuwin);	/* color window */
    tx_box(menu->menuwin, ACS_VLINE, ACS_HLINE);	/* draw border */

    itemptr = menu->itemlist;	/* point to start */

    wattrset(menu->menuwin, menu->normalattr);

    do {			/* display items */
	wmove(menu->menuwin, ctr, 1);
	tx_showitem(menu, itemptr, NOTSELECTED);	/* show items, initially unselected */
	ctr++;
	itemptr = itemptr->next;
    } while (ctr <= menu->itemcount);

    update_panels();
    doupdate();
}

void menumoveto(struct MENU *menu, struct ITEM **itemptr, unsigned int row)
{
    struct ITEM *tnode;
    unsigned int i;

    tnode = menu->itemlist;
    for (i = 1; i < row; i++)
	tnode = tnode->next;

    *itemptr = tnode;
}

/* 
 * Actually do the menu operation after all the initialization
 */

void tx_operatemenu(struct MENU *menu, int *position, int *aborted)
{
    struct ITEM *itemptr;
    int row = *position;
    int exitloop = 0;
    int ch;
    char *keyptr;

    tx_menukeyhelp(menu->normalattr, menu->highattr);
    *aborted = 0;
    menumoveto(menu, &itemptr, row);

    menu->descwin = newwin(1, COLS, LINES - 2, 0);
    menu->descpanel = new_panel(menu->descwin);

    do {
	wmove(menu->menuwin, row, 1);
	tx_showitem(menu, itemptr, SELECTED);

	/*
	 * Print item description
	 */

	wattrset(menu->descwin, menu->descriptionattr);
	tx_colorwin(menu->descwin);
	wmove(menu->descwin, 0, 0);
	wprintw(menu->descwin, " %s", itemptr->desc);
	update_panels();
	doupdate();

	wmove(menu->menuwin, row, 2);
	ch = wgetch(menu->menuwin);
	wmove(menu->menuwin, row, 1);
	tx_showitem(menu, itemptr, NOTSELECTED);

	switch (ch) {
	case KEY_UP:
	    if (row == 1)
		row = menu->itemcount;
	    else
		row--;

	    itemptr = itemptr->prev;

	    if (itemptr->itemtype == SEPARATOR) {
		row--;
		itemptr = itemptr->prev;
	    }
	    break;
	case KEY_DOWN:
	    if (row == menu->itemcount)
		row = 1;
	    else
		row++;

	    itemptr = itemptr->next;
	    if (itemptr->itemtype == SEPARATOR) {
		row++;
		itemptr = itemptr->next;
	    }
	    break;
	case 12:
	    tx_refresh_screen();
	    break;
	case 13:
	    exitloop = 1;
	    break;
	    /* case 27: exitloop = 1;*aborted = 1;row=menu->itemcount;break; */
	case '^':
	    break;		/* ignore caret key */
	default:
	    keyptr = strchr(menu->shortcuts, toupper(ch));
	    if ((keyptr != NULL)
		&& keyptr - menu->shortcuts < menu->itemcount) {
		row = keyptr - menu->shortcuts + 1;
		exitloop = 1;
	    }
	}
    } while (!(exitloop));

    *position = row;		/* position of executed option is in *position */
    del_panel(menu->descpanel);
    delwin(menu->descwin);
    update_panels();
    doupdate();
}


void tx_destroymenu(struct MENU *menu)
{
    struct ITEM *tnode;
    struct ITEM *tnextnode;

    if (menu->itemlist != NULL) {
	tnode = menu->itemlist;
	tnextnode = menu->itemlist->next;

	tnode->prev->next = NULL;

	while (tnode != NULL) {
	    free(tnode);
	    tnode = tnextnode;

	    if (tnextnode != NULL)
		tnextnode = tnextnode->next;
	}
    }
    del_panel(menu->menupanel);
    delwin(menu->menuwin);
    update_panels();
    doupdate();
}