-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
211 lines (194 loc) · 4.02 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
#include "deco.h"
#include "scr.h"
#include "menu.h"
static void drawhead (int nm);
static void clrmenu (struct menu *m);
static void drawmenu (struct menu *m);
static void upmenu (struct menu *m);
static int hotkey (int c, struct menu *m);
static void initmenu ()
{
register struct menu *m;
register struct submenu *sm;
int col, w;
col = 3;
for (m=menu; m->name; ++m) {
m->len = 4 + strlen (m->name);
m->col = col;
col += m->len;
m->hgh = 0;
m->wid = 0;
for (sm=m->submenu; sm->name; ++sm) {
sm->row = m->hgh;
w = 4 + strlen (sm->name);
if (w > m->wid)
m->wid = w;
++m->hgh;
}
}
}
static void getboxes ()
{
register struct menu *m;
for (m=menu; m->name; ++m) {
if (m->box)
VFreeBox (m->box);
m->box = VGetBox (1, m->col-1, m->hgh+3, m->wid+4);
}
}
int getmenu ()
{
if (! menu[0].len)
initmenu ();
getboxes ();
VMPutString (LINES-1, 0, "\0011\16 \17 2\16 \17 3\16 \17 4\16 \17 5\16 \17 6\16 \17 7\16 \17 8\16 \17 9\16 \01710\16Quit \17\2");
for (;;) {
drawhead (nmenu);
for (;;) {
int c, k;
drawmenu (&menu[nmenu]);
hidecursor ();
VSync ();
c = KeyGet ();
switch (c) {
default:
k = hotkey (c, &menu[nmenu]);
if (k) {
clrmenu (&menu[nmenu]);
return (k);
}
VBeep ();
continue;
case cntrl (']'): /* redraw screen */
VRedraw ();
continue;
case cntrl ('M'):
clrmenu (&menu[nmenu]);
return (1);
case cntrl ('J'):
clrmenu (&menu[nmenu]);
return (2);
case cntrl ('C'):
case cntrl ('['):
case meta ('J'): /* f0 */
clrmenu (&menu[nmenu]);
return (0);
case meta ('r'): /* right */
clrmenu (&menu[nmenu]);
if (! menu[++nmenu].name)
nmenu = 0;
break;
case meta ('l'): /* left */
clrmenu (&menu[nmenu]);
if (--nmenu < 0) {
for (nmenu=0; menu[nmenu].name; ++nmenu);
--nmenu;
}
break;
case meta ('u'): /* up */
upmenu (&menu[nmenu]);
continue;
case meta ('d'): /* down */
downmenu (&menu[nmenu]);
continue;
}
break;
}
}
}
static void drawhead (int nm)
{
register i, reverse;
VSetDim ();
reverse = VStandOut ();
VMove (0, 0);
for (i=0; i<80; ++i)
VPutChar (' ');
VMove (0, 2);
for (i=0; menu[i].name; ++i) {
if (i == nm) {
VStandEnd ();
VSetBold ();
}
VPutChar (' ');
VPutChar (i==nm && !reverse ? '[' : ' ');
VPutString (menu[i].name);
VPutChar (i==nm && !reverse ? ']' : ' ');
VPutChar (' ');
if (i == nm) {
VStandOut ();
VSetDim ();
}
}
VSetNormal ();
VStandEnd ();
}
static void clrmenu (struct menu *m)
{
VUngetBox (m->box);
}
static void drawmenu (struct menu *m)
{
register i, reverse;
VClearBox (1, m->col-1, m->hgh+3, m->wid+4);
VSetBold ();
VDrawBox (1, m->col, m->hgh+2, m->wid+2);
VSetDim ();
for (i=0; m->submenu[i].name; ++i)
if (! m->submenu[i].name[0])
VHorLine (i+2, m->col+2, m->wid-2);
VSetNormal ();
for (i=0; m->submenu[i].name; ++i) {
if (! m->submenu[i].name[0])
continue;
reverse = 0;
if (i == m->nsm)
reverse = VStandOut ();
else if (! m->submenu[i].active)
VSetDim ();
VMove (2+i, m->col+1);
VPutChar (i==m->nsm && !reverse ? '[' : ' ');
VPrint ("%c %-*s", m->submenu[i].tag ? '*' : ' ',
m->wid-4, m->submenu[i].name);
VPutChar (i==m->nsm && !reverse ? ']' : ' ');
if (i == m->nsm)
VStandEnd ();
else if (! m->submenu[i].active)
VSetNormal ();
}
}
static void upmenu (struct menu *m)
{
for (;;) {
if (--m->nsm < 0) {
for (m->nsm=0; m->submenu[m->nsm].name; ++m->nsm);
--m->nsm;
}
if (m->submenu[m->nsm].active)
break;
}
}
void downmenu (struct menu *m)
{
for (;;) {
if (! m->submenu[++m->nsm].name)
m->nsm = 0;
if (m->submenu[m->nsm].active)
break;
}
}
static int hotkey (int c, struct menu *m)
{
register nsm, ret;
if (c>='A' && c<='Z') {
c += 'a' - 'A';
ret = 2;
} else
ret = 1;
for (nsm=0; m->submenu[nsm].name; ++nsm)
if (c == m->submenu[nsm].key && m->submenu[nsm].active) {
m->nsm = nsm;
return (ret);
}
return (0);
}