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
|
/* -*- c -*- ------------------------------------------------------------- *
*
* Copyright 2004 Murali Krishnan Ganapathy - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 53 Temple Place Ste 330,
* Bostom MA 02111-1307, USA; either version 2 of the License, or
* (at your option) any later version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
/* This program can be compiled for DOS with the OpenWatcom compiler
* (http://www.openwatcom.org/):
*
* wcl -3 -osx -mt getargs.c
*/
#include "biosio.h"
#include "string.h"
#include "menu.h"
// Structures
// Declare a menusystem here
static t_menusystem menusystem;
/* Basic Menu routines */
void drawbox(char top, char left, char bot, char right,char attr, char page)
{
char x;
// Top border
gotoxy(top,left,page);
cprint(TOPLEFT,attr,1,page);
gotoxy(top,left+1,page);
cprint(TOP,attr,right-left,page);
gotoxy(top,right,page);
cprint(TOPRIGHT,attr,1,page);
// Bottom border
gotoxy(bot,left,page);
cprint(BOTLEFT,attr,1,page);
gotoxy(bot,left+1,page);
cprint(BOT,attr,right-left,page);
gotoxy(bot,right,page);
cprint(BOTRIGHT,attr,1,page);
// Left & right borders
for (x=top+1; x < bot; x++)
{
gotoxy(x,left,page);
cprint(LEFT,attr,1,page);
gotoxy(x,right,page);
cprint(RIGHT,attr,1,page);
}
}
void printmenu(t_menu * menu, int curr, char top, char left)
{
int x;
int numitems,menuwidth;
t_menusystem *ms;
char attr;
ms = & menusystem;
numitems = menu->numitems;
menuwidth = menu->menuwidth+2;
clearwindow(top,left-1,top+numitems+1,left+menuwidth+1,ms->menupage,ms->fillchar,ms->shadowattr);
drawbox(top-1,left-2,top+numitems,left+menuwidth,ms->normalattr,ms->menupage);
// Menu title
x = (menuwidth - strlen(menu->title) - 1) >> 1;
gotoxy(top-1,left+x,ms->menupage);
csprint(menu->title);
for (x=0; x < numitems; x++)
{
gotoxy(top+x,left-1,ms->menupage);
if (menu->items[x].action == OPT_INACTIVE)
{
attr = (x==curr? ms->revinactattr : ms->inactattr);
} else {
attr = (x==curr ? ms->reverseattr : ms->normalattr);
}
cprint(ms->spacechar,attr,menuwidth+1,ms->menupage);
gotoxy(top+x,left,ms->menupage);
csprint(menu->items[x].item);
gotoxy(top+x,left+menuwidth-1,ms->menupage); // Last char if any
switch (menu->items[x].action)
{
case OPT_SUBMENU:
cprint(SUBMENUCHAR,attr,1,ms->menupage);
break;
case OPT_CHECKBOX:
cprint( (menu->items[x].itemdata.checked ? CHECKED : UNCHECKED),attr,1,ms->menupage);
break;
}
}
if (menusystem.handler) menusystem.handler(&menusystem,menu->items+curr);
}
void cleanupmenu(t_menu *menu, char top,char left)
{
t_menusystem *ms = &menusystem;
clearwindow(top,left-1,top+menu->numitems+1,left+menu->menuwidth+3,ms->menupage,ms->fillchar,ms->fillattr); // Clear the shadow
clearwindow(top-1,left-2,top+menu->numitems,left+menu->menuwidth+2,ms->menupage,ms->fillchar,ms->fillattr); // clear the main window
}
/* Handle one menu */
t_menuitem * getmenuoption( t_menu *menu, char top, char left, char startopt)
// Return item chosen or NULL if ESC was hit.
{
int curr;
char asc,scan;
char numitems;
t_menusystem *ms;
t_menuitem *ci; // Current item
ms = & menusystem;
numitems = menu->numitems;
// Setup status line
gotoxy(ms->minrow+ms->statline,ms->mincol,ms->menupage);
cprint(ms->spacechar,ms->reverseattr,ms->numcols,ms->menupage);
// Initialise current menu item
curr = startopt;
gotoxy(ms->minrow+ms->statline,ms->mincol,ms->menupage);
cprint(ms->spacechar,ms->statusattr,ms->numcols,1);
gotoxy(ms->minrow+ms->statline,ms->mincol,ms->menupage);
csprint(menu->items[curr].status);
while (1) // Forever
{
printmenu(menu,curr,top,left);
ci = &(menu->items[curr]);
asc = inputc(&scan);
switch (scan)
{
case HOMEKEY:
curr = 0;
break;
case ENDKEY:
curr = numitems -1;
break;
case PAGEDN:
curr += 5;
break;
case PAGEUP:
curr -= 5;
break;
case UPARROW:
curr --;
break;
case DNARROW:
curr++;
break;
case LTARROW:
case ESCAPE:
return NULL;
break;
case ENTERA:
case RTARROW:
case ENTERB:
if (ci->action == OPT_INACTIVE) break;
if (ci->action == OPT_CHECKBOX) break;
if (ci->action == OPT_EXITMENU) return NULL; // As if we hit Esc
return ci;
break;
case SPACEKEY:
if (ci->action != OPT_CHECKBOX) break;
ci->itemdata.checked = !ci->itemdata.checked;
// Call handler to see it anything needs to be done
if (ci->handler != NULL) ci->handler(&menusystem,ci);
break;
}
// Adjust within range
if (curr < 0) curr=0;
if (curr >= numitems) curr = numitems -1;
// Update status line
gotoxy(ms->minrow+ms->statline,ms->mincol,ms->menupage);
cprint(ms->spacechar,ms->statusattr,ms->numcols,ms->menupage);
csprint(menu->items[curr].status);
}
return NULL; // Should never come here
}
/* Handle the entire system of menu's. */
t_menuitem * runmenusystem(char top, char left, int currmenu)
/*
* currmenu
* Which menu should be currently displayed
* top,left
* What is the position of the top,left corner of the menu
*
* Return Value:
* Returns a pointer to the final item chosen, or NULL if nothing chosen.
*/
{
t_menu *cmenu;
t_menusystem *ms = &menusystem;
t_menuitem *opt,*choice;
int numitems;
char startopt;
startopt = 0;
startover:
cmenu = (menusystem.menus+currmenu);
numitems = menusystem.menus[currmenu].numitems;
opt = getmenuoption(cmenu,top,left,startopt);
if (opt == NULL)
{
// User hit Esc
cleanupmenu(cmenu,top,left);
return NULL;
}
if (opt->action != OPT_SUBMENU) // We are done with the menu system
{
cleanupmenu(cmenu,top,left);
return opt; // parent cleanup other menus
}
if (opt->itemdata.submenunum >= menusystem.nummenus) // This is Bad....
{
gotoxy(12,12,ms->menupage); // Middle of screen
csprint("Invalid submenu requested. Ask administrator to correct this.");
cleanupmenu(cmenu,top,left);
return NULL; // Pretend user hit esc
}
// Call recursively for submenu
// Position the submenu below the current item,
// covering half the current window (horizontally)
choice = runmenusystem(top+opt->index+2, left+3+(cmenu->menuwidth >> 1), opt->itemdata.submenunum);
if (choice==NULL) // User hit Esc in submenu
{
// Startover
startopt = opt->index;
goto startover;
}
else
{
cleanupmenu(cmenu,top,left);
return choice;
}
}
/* User Callable functions */
t_menuitem * showmenus(char startmenu)
{
t_menuitem *rv;
t_menusystem *ms;
char oldpage,tpos;
ms = & menusystem;
// Setup screen for menusystem
oldpage = getdisppage();
setdisppage(ms->menupage);
cls();
clearwindow(ms->minrow,ms->mincol,ms->maxrow,ms->maxcol,ms->menupage,ms->fillchar,ms->fillattr);
tpos = (ms->numcols - strlen(menusystem.title) - 1) >> 1; // To center it on line
gotoxy(ms->minrow,ms->mincol,ms->menupage);
cprint(ms->tfillchar,ms->titleattr,ms->numcols,ms->menupage);
gotoxy(ms->minrow,ms->mincol+tpos,ms->menupage);
csprint(menusystem.title);
cursoroff(); // Doesn't seem to work?
// Go
rv = runmenusystem(ms->minrow+MENUROW, ms->mincol+MENUCOL, startmenu);
// Hide the garbage we left on the screen
cursoron();
if (oldpage == ms->menupage) cls(); else setdisppage(oldpage);
// Return user choice
return rv;
}
void init_menusystem(const char *title)
{
menusystem.nummenus = 0;
if (title == NULL)
strcpy(menusystem.title,TITLESTR);
else strcpy(menusystem.title,title);
menusystem.normalattr = NORMALATTR;
menusystem.reverseattr= REVERSEATTR;
menusystem.inactattr = INACTATTR;
menusystem.revinactattr = REVINACTATTR;
menusystem.statusattr = STATUSATTR;
menusystem.statline = STATLINE;
menusystem.tfillchar= TFILLCHAR;
menusystem.titleattr= TITLEATTR;
menusystem.fillchar = FILLCHAR;
menusystem.fillattr = FILLATTR;
menusystem.spacechar= SPACECHAR;
menusystem.shadowattr = SHADOWATTR;
menusystem.menupage = MENUPAGE; // Usually no need to change this at all
menusystem.handler = NULL; // No handler function
// Figure out the size of the screen we are in now.
// By default we use the whole screen for our menu
menusystem.minrow = menusystem.mincol = 0;
menusystem.numcols = getnumcols();
menusystem.numrows = getnumrows();
menusystem.maxcol = menusystem.numcols - 1;
menusystem.maxrow = menusystem.numrows - 1;
}
void set_normal_attr(char normal, char selected, char inactivenormal, char inactiveselected)
{
if (normal != 0xFF) menusystem.normalattr = normal;
if (selected != 0xFF) menusystem.reverseattr = selected;
if (inactivenormal != 0xFF) menusystem.inactattr = inactivenormal;
if (inactiveselected != 0xFF) menusystem.revinactattr = inactiveselected;
}
void set_status_info(char statusattr, char statline)
{
if (statusattr != 0xFF) menusystem.statusattr = statusattr;
// statline is relative to minrow
if (statline >= menusystem.numrows) statline = menusystem.numrows - 1;
menusystem.statline = statline; // relative to ms->minrow, 0 based
}
void set_title_info(char tfillchar, char titleattr)
{
if (tfillchar != 0xFF) menusystem.tfillchar = tfillchar;
if (titleattr != 0xFF) menusystem.titleattr = titleattr;
}
void set_misc_info(char fillchar, char fillattr,char spacechar, char shadowattr)
{
if (fillchar != 0xFF) menusystem.fillchar = fillchar;
if (fillattr != 0xFF) menusystem.fillattr = fillattr;
if (spacechar != 0xFF) menusystem.spacechar = spacechar;
if (shadowattr!= 0xFF) menusystem.shadowattr= shadowattr;
}
void set_window_size(char top, char left, char bot, char right) // Set the window which menusystem should use
{
t_menusystem *ms;
char nr,nc;
if ((top > bot) || (left > right)) return; // Sorry no change will happen here
nr = getnumrows();
nc = getnumcols();
if (bot >= nr) bot = nr-1;
if (right >= nc) right = nc-1;
ms = &menusystem;
ms->minrow = top;
ms->mincol = left;
ms->maxrow = bot;
ms->maxcol = right;
ms->numcols = right - left + 1;
ms->numrows = bot - top + 1;
if (ms->statline >= ms->numrows) ms->statline = ms->numrows - 1; // Clip statline if need be
}
void reg_handler( t_menusystem_handler handler)
{
menusystem.handler = handler;
}
void unreg_handler()
{
menusystem.handler = NULL;
}
int add_menu(const char *title) // Create a new menu and return its position
{
char num = menusystem.nummenus;
t_menu *m;
if (num >= MAXMENUS) return -1;
m = &(menusystem.menus[(int)num]);
m->numitems = 0;
if (title)
{
if (strlen(title) > MENULEN - 2)
strcpy(m->title," TITLE TOO LONG ");
else strcpy(m->title,title);
}
else strcpy(m->title,"");
m ->menuwidth = strlen(m->title);
menusystem.nummenus += 1;
return menusystem.nummenus - 1;
}
t_menuitem * add_item(const char *item, const char *status, t_action action, const char *data, char itemdata) // Add item to the "current" menu
{
t_menuitem *mi;
t_menu *m;
m = &(menusystem.menus[menusystem.nummenus-1]);
mi = &(m->items[(int)m->numitems]);
mi->handler = NULL; // No handler
if (item) {
if (strlen(item) > MENULEN - 2) {
strcpy(mi->item,"ITEM TOO LONG");
} else {
strcpy(mi->item,item);
if (strlen(item) > m->menuwidth) m->menuwidth = strlen(item);
}
} else strcpy(mi->item,"");
if (status) {
if (strlen(status) > STATLEN - 2) {
strcpy(mi->status,"STATUS STRING TOO LONG");
} else {
strcpy(mi->status,status);
}
} else strcpy(mi->status,"");
mi->action = action;
if (data) {
if (strlen(data) > ACTIONLEN - 2) {
strcpy(mi->data,"ACTION STRING LONG");
} else {
strcpy(mi->data,data); // This is only null terminated
}
} else strcpy(mi->data,"");
switch (action)
{
case OPT_SUBMENU:
mi->itemdata.submenunum = itemdata;
break;
case OPT_CHECKBOX:
mi->itemdata.checked = itemdata;
break;
case OPT_RADIOBTN:
mi->itemdata.choice = itemdata;
break;
}
mi->index = m->numitems++;
mi->parindex = menusystem.nummenus-1;
return mi;
}
|