-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqmenu.c
239 lines (198 loc) · 5.09 KB
/
qmenu.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
/*
* QuickMenu VidMgr demonstration application.
*
* Written in June 1996 by Andrew Clarke and released to the public domain.
*
* Currently assumes 80x25 video output, but may be rewritten to
* recognise other video dimensions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vidmgr.h"
#include "vioimage.h"
#define maxx vm_getscreenwidth()
#define maxy vm_getscreenheight()
#define PROG "QuickMenu"
#define VERSION "1.2"
#define MAXITEMS 12
typedef struct
{
char desc[40];
char cmd[250];
}
MENUDATA;
static VIOIMAGE sysimage;
static MENUDATA menudata[MAXITEMS];
static int menuitems, curritem, olditem;
int qmenuLoadMenu(char *fnm)
{
FILE *fp;
int item, line;
fp = fopen(fnm, "r");
if (fp == NULL)
{
perror(fnm);
return 0;
}
item = 0;
line = 1;
while (item < MAXITEMS && !feof(fp))
{
char *p;
if (fgets(menudata[item].desc, 40, fp) == NULL)
{
break;
}
p = strrchr(menudata[item].desc, '\n');
if (p != NULL)
{
*p = '\0';
}
if (*menudata[item].desc == '\0')
{
printf("%s(%d): Error! Expected description but found ''\n", fnm, line);
return 0;
}
line++;
if (fgets(menudata[item].cmd, 250, fp) == NULL)
{
printf("%s(%d): Error! Expected command but reached end of file\n", fnm, line);
return 0;
}
p = strrchr(menudata[item].cmd, '\n');
if (p != NULL)
{
*p = '\0';
}
if (*menudata[item].cmd == '\0')
{
printf("%s(%d): Error! Expected command but found ''\n", fnm, line);
return 0;
}
line++;
item++;
}
fclose(fp);
menuitems = item;
return 1;
}
void qmenuRun(void)
{
int done, ch;
FILE *ofp;
char qmtmpfnm[12];
#if defined(OS2)
strcpy(qmtmpfnm, "$qmtemp.cmd");
#elif defined(EMX)
if (_osmode == DOS_MODE)
{
strcpy(qmtmpfnm, "$qmtemp.bat");
}
else
{
strcpy(qmtmpfnm, "$qmtemp.cmd");
}
#else
strcpy(qmtmpfnm, "$qmtemp.bat");
#endif
vm_attrib(24, (char) (7 + curritem), 56, (char) (7 + curritem), vm_mkcolor(WHITE, BLACK));
done = 0;
while (!done)
{
if (curritem != olditem)
{
vm_attrib(24, (char) (7 + olditem), 56, (char) (7 + olditem), vm_mkcolor(BLACK, LIGHTGRAY));
vm_attrib(24, (char) (7 + curritem), 56, (char) (7 + curritem), vm_mkcolor(WHITE, BLACK));
}
while (!vm_kbhit())
{
/* nada */
}
ch = vm_getch();
switch (ch)
{
case 0x3b00: /* F1 */
/* nada */
break;
case 0x4800: /* up arrow */
olditem = curritem;
if (curritem != 0)
{
curritem--;
}
break;
case 0x5000: /* down arrow */
olditem = curritem;
if (curritem != menuitems - 1)
{
curritem++;
}
break;
case 0x4700: /* Home */
olditem = curritem;
curritem = 0;
break;
case 0x4f00: /* End */
olditem = curritem;
curritem = menuitems - 1;
break;
case 0x001b: /* Escape */
case 0x3d00: /* F3 */
case 0x4400: /* F10 */
case 0x6b00: /* Alt+F4 */
case 0x2d00: /* Alt+X */
remove(qmtmpfnm);
done = 1;
break;
case 0x000d: /* Enter */
ofp = fopen(qmtmpfnm, "w");
fputs("@echo off\n", ofp);
fputs(menudata[curritem].cmd, ofp);
fputs("\n", ofp);
fputs("qm\n", ofp);
fclose(ofp);
done = 1;
break;
default:
break;
}
}
}
void qmenuTerm(void)
{
vioImageRestore(&sysimage, 1, 1);
vioImageTerm(&sysimage);
vm_gotoxy(vm_startup.xpos, vm_startup.ypos);
vm_done();
}
void qmenuInit(void)
{
int item;
printf("\n" PROG " " VERSION "; Written in June 1996 by Andrew Clarke.\n");
printf("Released to the public domain.\n");
vm_init();
vioImageDefaults(&sysimage);
vioImageInit(&sysimage, maxx, maxy);
vioImageSave(&sysimage, 1, 1);
if (qmenuLoadMenu("qmenu.mnu") != 1)
{
vm_done();
exit(EXIT_FAILURE);
}
vm_setcursorstyle(CURSORHIDE);
vm_attrib(22, 6, 62, 21, vm_mkcolor(LIGHTGRAY, BLACK));
vm_paintclearbox(20, 5, 60, 20, vm_mkcolor(BLACK, LIGHTGRAY));
vm_frame(23, 6, 57, 19, vm_mkcolor(BLACK, LIGHTGRAY), vm_frame_double);
for (item = 0; item < menuitems; item++)
{
vm_puts(25, (char) (7 + item), menudata[item].desc);
}
}
int main(void)
{
qmenuInit();
qmenuRun();
qmenuTerm();
return 0;
}