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
|
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include "machine_type.h"
#include "menu.h"
#include "output.h"
#include "input.h"
void clearMenu(tMenu* Menu)
{
Menu->menuitemnum=0;
Menu->menuitemactive=0;
}
void newMenuItem(tMenu* Menu,char* text,tUInt16 y,tUInt16 x,char hotkey,tBool active,tInt8* itemnum)
{
memcpy(Menu->MenuItems[Menu->menuitemnum].menutext,text,64);
Menu->MenuItems[Menu->menuitemnum].x=x;
Menu->MenuItems[Menu->menuitemnum].y=y;
if (hotkey>='a' && hotkey<='z') hotkey-=32; // make the hotkey uppercase
Menu->MenuItems[Menu->menuitemnum].hotkey=hotkey;
if (active) Menu->menuitemactive=Menu->menuitemnum;
*itemnum=Menu->menuitemnum;
Menu->menuitemnum++;
}
void printMenu(tOutput* output,tMenu* Menu,tUInt16 offsy,tUInt16 offsx)
{
int i;
int j;
for (i=0;i<Menu->menuitemnum;i++)
{
setcolor(output,(i==Menu->menuitemactive)?COLOR_MENUACTIVE:COLOR_MENUNORMAL);
mvwprintw(output->win,offsy+Menu->MenuItems[i].y,offsx+Menu->MenuItems[i].x,"%s",Menu->MenuItems[i].menutext);
if (Menu->MenuItems[i].hotkey)
{
for (j=0;j<strlen(Menu->MenuItems[i].menutext);j++)
{
if (Menu->MenuItems[i].menutext[j]==Menu->MenuItems[i].hotkey || (Menu->MenuItems[i].hotkey>='A' && Menu->MenuItems[i].hotkey<='Z' && Menu->MenuItems[i].menutext[j]-32==Menu->MenuItems[i].hotkey))
{
setcolor(output,(i==Menu->menuitemactive)?COLOR_MENUHOTKEYACTIVE:COLOR_MENUHOTKEY);
mvwprintw(output->win,offsy+Menu->MenuItems[i].y,offsx+Menu->MenuItems[i].x+j,"%c",Menu->MenuItems[i].menutext[j]);
j=65;
}
}
}
}
}
void MenuMoveLeft(tMenu* Menu)
{
tUInt16 actx;
tUInt16 acty;
tInt16 diffx;
tInt16 diffy;
tInt16 mindiffx;
tInt16 mindiffy;
tUInt16 minact;
int i;
actx=Menu->MenuItems[Menu->menuitemactive].x;
acty=Menu->MenuItems[Menu->menuitemactive].y;
mindiffx=COLS;
mindiffy=LINES;
minact=Menu->menuitemactive;
for (i=0;i<Menu->menuitemnum;i++)
if (i!=Menu->menuitemactive)
{
diffx=actx-Menu->MenuItems[i].x;
diffy=acty-Menu->MenuItems[i].y;
if (diffy<0) diffy=-diffy;
if (mindiffy>diffy)
{
mindiffy=diffy;
mindiffx=COLS;
}
if (mindiffy==diffy)
{
if (mindiffx>=diffx && diffx>0)
{
mindiffx=diffx;
minact=i;
}
}
}
Menu->menuitemactive=minact;
}
void MenuMoveRight(tMenu* Menu)
{
tUInt16 actx;
tUInt16 acty;
tInt16 diffx;
tInt16 diffy;
tInt16 mindiffx;
tInt16 mindiffy;
tUInt16 minact;
int i;
actx=Menu->MenuItems[Menu->menuitemactive].x;
acty=Menu->MenuItems[Menu->menuitemactive].y;
mindiffx=COLS;
mindiffy=LINES;
minact=Menu->menuitemactive;
for (i=0;i<Menu->menuitemnum;i++)
if (i!=Menu->menuitemactive)
{
diffx=Menu->MenuItems[i].x-actx;
diffy=acty-Menu->MenuItems[i].y;
if (diffy<0) diffy=-diffy;
if (mindiffy>diffy)
{
mindiffy=diffy;
mindiffx=COLS;
}
if (mindiffy==diffy)
{
if (mindiffx>=diffx && diffx>0)
{
mindiffx=diffx;
minact=i;
}
}
}
Menu->menuitemactive=minact;
}
void MenuMoveUp(tMenu* Menu)
{
tUInt16 actx;
tUInt16 acty;
tInt16 diffx;
tInt16 diffy;
tInt16 mindiffx;
tInt16 mindiffy;
tUInt16 minact;
int i;
actx=Menu->MenuItems[Menu->menuitemactive].x;
acty=Menu->MenuItems[Menu->menuitemactive].y;
mindiffx=COLS;
mindiffy=LINES;
minact=Menu->menuitemactive;
for (i=0;i<Menu->menuitemnum;i++)
if (i!=Menu->menuitemactive)
{
diffx=Menu->MenuItems[i].x-actx;
diffy=acty-Menu->MenuItems[i].y;
if (diffx<0) diffx=-diffx;
if (mindiffx>diffx)
{
mindiffx=diffx;
mindiffy=LINES;
}
if (mindiffx==diffx)
{
if (mindiffy>=diffy && diffy>0)
{
mindiffy=diffy;
minact=i;
}
}
}
Menu->menuitemactive=minact;
}
void MenuMoveDown(tMenu* Menu)
{
tUInt16 actx;
tUInt16 acty;
tInt16 diffx;
tInt16 diffy;
tInt16 mindiffx;
tInt16 mindiffy;
tUInt16 minact;
int i;
actx=Menu->MenuItems[Menu->menuitemactive].x;
acty=Menu->MenuItems[Menu->menuitemactive].y;
mindiffx=COLS;
mindiffy=LINES;
minact=Menu->menuitemactive;
for (i=0;i<Menu->menuitemnum;i++)
if (i!=Menu->menuitemactive)
{
diffx=Menu->MenuItems[i].x-actx;
diffy=Menu->MenuItems[i].y-acty;
if (diffx<0) diffx=-diffx;
if (mindiffx>diffx)
{
mindiffx=diffx;
mindiffy=LINES;
}
if (mindiffx==diffx)
{
if (mindiffy>=diffy && diffy>0)
{
mindiffy=diffy;
minact=i;
}
}
}
Menu->menuitemactive=minact;
}
void MenuSetActiveItem(tMenu* Menu,tInt8 itemnum)
{
Menu->menuitemactive=itemnum;
}
tInt8 MenuInteract(tOutput* output,tMenu* Menu,tInt16 offsy,tInt16 offsx)
{
tInt16 ch;
int i;
tInt8 retval;
ch=0;
printMenu(output,Menu,offsy,offsx);
while (ch!=KEYENTER)
{
ch=getkey((tKeyTab*)output->pKeyTab,0);
switch(ch)
{
case KEYLEFT: MenuMoveLeft(Menu); break;
case KEYRIGHT: MenuMoveRight(Menu); break;
case KEYUP: MenuMoveUp(Menu); break;
case KEYDOWN: MenuMoveDown(Menu); break;
case KEYTAB: Menu->menuitemactive=(Menu->menuitemactive+1)%Menu->menuitemnum;break;
case KEYENTER: retval=Menu->menuitemactive; break;
default:
if (ch>=' ' && ch<127)
{
if (ch>='a' && ch<='z') ch-=32;
for (i=0;i<Menu->menuitemnum;i++)
{
if (Menu->MenuItems[i].hotkey==ch)
{
Menu->menuitemactive=i;
ch=KEYENTER;
}
}
}
break;
}
printMenu(output,Menu,offsy,offsx);
}
return Menu->menuitemactive;
}
|