Repository navigation
Expand file tree
/
Copy pathmenu.c
More file actions
309 lines (263 loc) · 8.78 KB
/
Copy pathmenu.c
File metadata and controls
309 lines (263 loc) · 8.78 KB
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
/* -----------------------------------------------------------
* Program: MENU.C
* Authors: Bradley L. Jones
* Gregory L. Guntle
* adjusted for use with CatView by Peter van der Woude
* Purpose: Manages a menu on the screen
*
* Enter with: * row, col - for positioning menu
* * box_type - SINGLE_BOX, DOUBLE_BOX or NOBOX
* * array of ptrs to the menu items to display
* array is setup this way:
* char *array_name[10] = { "menu item",
* "keys" }; etc.
* * nbr_items - number of menu items (incl keys)
* * exit_keys - special keys for exit menu (F3, etc.)
* * address to return menu item selected in
* * arrow flag - should R/L arrows be used
* * do_shadow - should shadow be displayed?
*
* Returns: The exit key and modifies one parm to hold the
* selected item.
*
* Note(s): The cursor is turned off. Calling function
* must turn it back on if it is needed.
*=======================================================*/
#include "colortbl.h"
#include "tyac.h"
#include <conio.h>
#include <string.h>
/* ================================================= *
* External Global variables defined in MAIN *
* ================================================= */
extern struct color_table ct;
/* ================================================= *
* Global variables for other functions in this file *
* ================================================= */
int row, col;
int nbr;
char **menu_ptr;
char *EXIT_KEYS;
int nbr_exit_keys;
/* ================================================= *
* Function declarations used in this file *
* ================================================= */
int DisplayMenuEx(int, int, int, char **,
int, char *, int *, int,
int);
void rewrite_menu_items(int, int);
int check_menu_keys(char);
extern MenuHighlight;
extern TOTAL_CATALOGS;
/* ----------------------------------------------- *
* DISPLAY_MENU: *
* *
* This function does the real chore in handling *
* menus. *
* ----------------------------------------------- */
#pragma argsused
int DisplayMenuEx(int srow, int scol, int box_type,
char **menu, int nbr_items, char *exit_keys,
int *sel, int arr_flg, int do_shadow) {
int i = 0;
int menu_pos; /* Maintaining menu selections */
int old_menu_pos;
int loop_exit = FALSE;
int ch; /* Character pressed */
int max_len = 0; /* max string length in array */
int temp_len = 0;
int key_found; /* Indicate if key is exit keys */
char *ptr_to_key; /* Holds comp val for mtchng keys */
/*--------------------------------------------------*
* Set up global variables - other functions to use *
*--------------------------------------------------*/
/* Set Global ptr to menu items */
menu_ptr = menu;
row = srow;
col = scol;
EXIT_KEYS = exit_keys;
nbr = nbr_items;
/* number of exit keys */
nbr_exit_keys = strlen(EXIT_KEYS);
/* ----------------------------- */
/* Calculate string lengths */
/* ----------------------------- */
while (i < (nbr / 2)) {
temp_len = strlen(*(menu + (i * 2)));
if (temp_len > max_len) {
max_len = temp_len;
}
i++;
}
nbr = nbr_items / 2; /* Exclude keys of selection */
/* ------------------------------ */
/* If Box is needed Draw now */
/* ------------------------------ */
if (box_type != 0) {
if (do_shadow == SHADOW) {
grid(row, row + nbr + 1, col - 3, col + max_len,
ct.shdw_fcol, ct.bg_bcol, 2);
}
box(row - 1, row + nbr, col - 2, col + max_len + 1,
box_type, FILL_BOX, ct.abar_fcol, ct.abar_bcol);
}
/* ------------------------------ */
/* Display Menu */
/* ------------------------------ */
/* Display menu */
for (i = 0; i < nbr; i++) {
write_string(*(menu + (i * 2)),
ct.menu_fcol, ct.menu_bcol, row + i, col);
}
/* Highlight first menu item */
if (MenuHighlight)
write_string(*(menu), ct.menu_high_fcol,
ct.menu_high_bcol, row, col);
/* ------------------------------------------------------ */
_setcursortype(_NOCURSOR); /* Turn off cursor */
old_menu_pos = 1; /* Track selection prior */
menu_pos = 1; /* Track current sel */
while (loop_exit == FALSE) {
if ((ch = getch()) == 0) {
/* Scan code so read next byte */
ch = getch();
switch (ch) {
case HOME: /* goto to TOP of menu */
menu_pos = 1;
rewrite_menu_items(menu_pos,
old_menu_pos);
old_menu_pos = menu_pos;
break;
case END: /* goto LAST menu item */
menu_pos = nbr;
rewrite_menu_items(menu_pos,
old_menu_pos);
old_menu_pos = menu_pos;
break;
case LT_ARROW:
case RT_ARROW:
loop_exit = TRUE;
menu_pos = 1;
rewrite_menu_items(-1, old_menu_pos);
old_menu_pos = 1;
break;
case DN_ARROW: /* Move DOWN one menu selection */
menu_pos++;
if (menu_pos > nbr)
menu_pos = 1;
rewrite_menu_items(menu_pos,
old_menu_pos);
old_menu_pos = menu_pos;
break;
case UP_ARROW: /* Move UP one menu selection */
menu_pos--;
if (menu_pos < 1) /* At end ? */
menu_pos = nbr;
rewrite_menu_items(menu_pos,
old_menu_pos);
old_menu_pos = menu_pos;
break;
default:
loop_exit = check_menu_keys(ch);
if (loop_exit == FALSE) {
/* key a valid exit key ?*/
boop();
}
break;
} /* end of switch */
} /* end of if */
else {
switch (ch) /* test for other special keys */
{
case CR_KEY:
loop_exit = TRUE;
break;
case ESC_KEY: /* is ESC_KEY an exit key? */
i = 0;
while (i < nbr_exit_keys && !loop_exit) {
if (ch == EXIT_KEYS[i++]) {
loop_exit = TRUE;
}
}
if (!loop_exit) {
boop();
}
break;
default: /* Search thru valid keys on Menu items */
i = 0;
key_found = FALSE;
while (i < nbr && !key_found) {
ptr_to_key = strchr(*(menu + (i * 2) + 1), ch);
if (!ptr_to_key) {
/* Not found - look at next one */
i++;
} else {
/* found key - exit */
key_found = TRUE;
}
}
if (!key_found) {
boop();
} else {
/* Found letter - position menu sel */
menu_pos = i + 1;
rewrite_menu_items(menu_pos,
old_menu_pos);
old_menu_pos = menu_pos;
}
break;
} /* end of switch */
} /* end of else */
} /* end of while loop */
*sel = menu_pos;
return (ch);
} /* end of subroutine display_menu */
/* ----------------------------------------------- *
* function: rewrite_menu_items() *
* *
* This subroutine redisplays the menu items. *
* The previous selection in NORMAL colors *
* and the new selections in HIGHLIGHTED colors. *
* ----------------------------------------------- */
void rewrite_menu_items(int new_pos, int old_pos) {
/* rewrite last selection - normal colors */
write_string(*(menu_ptr + ((old_pos - 1) * 2)),
ct.menu_fcol, ct.menu_bcol,
row + old_pos - 1, col);
if (new_pos == -1 && TOTAL_CATALOGS > 20) /* if left or right arrow and menu full */
{
new_pos = 1;
MenuHighlight = FALSE;
}
/* Now rewrite new one w/selections color */
else /* left or right arrow but menu not full */
{
if (new_pos == -1) {
new_pos = 1;
MenuHighlight = TRUE;
}
write_string(*(menu_ptr + ((new_pos - 1) * 2)),
ct.menu_high_fcol, ct.menu_high_bcol,
row + new_pos - 1, col);
}
}
/* ----------------------------------------------- *
* function: check_menu_keys() *
* *
* This subroutine checks the key pressed against *
* a list of keys that can end the procedure. *
* It receives the key pressed and returns TRUE *
* if key is in the list, else FALSE if not in *
* list. *
* ----------------------------------------------- */
int check_menu_keys(char key_pressed) {
/* return a true or false to return_code */
int return_code = FALSE;
int loop_ctr = 0;
while (loop_ctr < nbr_exit_keys && !return_code) {
if (key_pressed == EXIT_KEYS[loop_ctr++]) {
return_code = TRUE;
}
}
return (return_code);
}