forked from alexfru/dflat20
-
Notifications
You must be signed in to change notification settings - Fork 2
/
popdown.c
396 lines (378 loc) · 11.7 KB
/
popdown.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
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
/* ------------- popdown.c ----------- */
#include "dflat.h"
static int SelectionWidth(struct PopDown *);
static int py = -1;
int CurrentMenuSelection;
/* ------------ CREATE_WINDOW Message ------------- */
static int CreateWindowMsg(WINDOW wnd)
{
int rtn, adj;
ClearAttribute(wnd, HASTITLEBAR |
VSCROLLBAR |
MOVEABLE |
SIZEABLE |
HSCROLLBAR);
/* ------ adjust to keep popdown on screen ----- */
adj = SCREENHEIGHT-1-wnd->rc.bt;
if (adj < 0) {
wnd->rc.tp += adj;
wnd->rc.bt += adj;
}
adj = SCREENWIDTH-1-wnd->rc.rt;
if (adj < 0) {
wnd->rc.lf += adj;
wnd->rc.rt += adj;
}
rtn = BaseWndProc(POPDOWNMENU, wnd, CREATE_WINDOW, 0, 0);
SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
SendMessage(NULL, SAVE_CURSOR, 0, 0);
SendMessage(NULL, HIDE_CURSOR, 0, 0);
wnd->oldFocus = inFocus;
inFocus = wnd;
return rtn;
}
/* --------- LEFT_BUTTON Message --------- */
static void LeftButtonMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
int my = (int) p2 - GetTop(wnd);
if (InsideRect(p1, p2, ClientRect(wnd))) {
if (my != py) {
SendMessage(wnd, LB_SELECTION,
(PARAM) wnd->wtop+my-1, TRUE);
py = my;
}
}
else if ((int)p2 == GetTop(GetParent(wnd)))
if (GetClass(GetParent(wnd)) == MENUBAR)
PostMessage(GetParent(wnd), LEFT_BUTTON, p1, p2);
}
/* -------- BUTTON_RELEASED Message -------- */
static BOOL ButtonReleasedMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
py = -1;
if (InsideRect((int)p1, (int)p2, ClientRect(wnd))) {
int sel = (int)p2 - GetClientTop(wnd);
if (*TextLine(wnd, sel) != LINE)
SendMessage(wnd, LB_CHOOSE, wnd->selection, 0);
}
else {
WINDOW pwnd = GetParent(wnd);
if (GetClass(pwnd) == MENUBAR && (int)p2==GetTop(pwnd))
return FALSE;
if ((int)p1 == GetLeft(pwnd)+2)
return FALSE;
SendMessage(wnd, CLOSE_WINDOW, 0, 0);
return TRUE;
}
return FALSE;
}
/* --------- PAINT Message -------- */
static void PaintMsg(WINDOW wnd)
{
int wd;
unsigned char sep[MAXPOPWIDTH], *cp = sep;
unsigned char sel[MAXPOPWIDTH];
struct PopDown *ActivePopDown;
struct PopDown *pd1;
ActivePopDown = pd1 = wnd->mnu->Selections;
wd = MenuWidth(ActivePopDown)-2;
while (wd--)
*cp++ = LINE;
*cp = '\0';
SendMessage(wnd, CLEARTEXT, 0, 0);
wnd->selection = wnd->mnu->Selection;
while (pd1->SelectionTitle != NULL) {
if (*pd1->SelectionTitle == LINE)
SendMessage(wnd, ADDTEXT, (PARAM) sep, 0);
else {
int len;
memset(sel, '\0', sizeof sel);
if (pd1->Attrib & INACTIVE)
/* ------ inactive menu selection ----- */
sprintf(sel, "%c%c%c",
CHANGECOLOR,
wnd->WindowColors [HILITE_COLOR] [FG]|0x80,
wnd->WindowColors [STD_COLOR] [BG]|0x80);
strcat(sel, " ");
if (pd1->Attrib & CHECKED)
/* ---- paint the toggle checkmark ---- */
sel[strlen(sel)-1] = CHECKMARK;
len=CopyCommand(sel+strlen(sel),pd1->SelectionTitle,
pd1->Attrib & INACTIVE,
wnd->WindowColors [STD_COLOR] [BG]);
if (pd1->Accelerator) {
/* ---- paint accelerator key ---- */
int i;
int wd1 = 2+SelectionWidth(ActivePopDown) -
strlen(pd1->SelectionTitle);
int key = pd1->Accelerator;
if (key > 0 && key < 27) {
/* --- CTRL+ key --- */
while (wd1--)
strcat(sel, " ");
sprintf(sel+strlen(sel), "[Ctrl+%c]", key-1+'A');
}
else {
for (i = 0; keys[i].keylabel; i++) {
if (keys[i].keycode == key) {
while (wd1--)
strcat(sel, " ");
sprintf(sel+strlen(sel), "[%s]",
keys[i].keylabel);
break;
}
}
}
}
if (pd1->Attrib & CASCADED) {
/* ---- paint cascaded menu token ---- */
if (!pd1->Accelerator) {
wd = MenuWidth(ActivePopDown)-len+1;
while (wd--)
strcat(sel, " ");
}
sel[strlen(sel)-1] = CASCADEPOINTER;
}
else
strcat(sel, " ");
strcat(sel, " ");
sel[strlen(sel)-1] = RESETCOLOR;
SendMessage(wnd, ADDTEXT, (PARAM) sel, 0);
}
pd1++;
}
}
/* ---------- BORDER Message ----------- */
static int BorderMsg(WINDOW wnd)
{
int i, rtn = TRUE;
WINDOW currFocus;
if (wnd->mnu != NULL) {
currFocus = inFocus;
inFocus = NULL;
rtn = BaseWndProc(POPDOWNMENU, wnd, BORDER, 0, 0);
inFocus = currFocus;
for (i = 0; i < ClientHeight(wnd); i++) {
if (*TextLine(wnd, i) == LINE) {
wputch(wnd, LEDGE, 0, i+1);
wputch(wnd, REDGE, WindowWidth(wnd)-1, i+1);
}
}
}
return rtn;
}
/* -------------- LB_CHOOSE Message -------------- */
static void LBChooseMsg(WINDOW wnd, PARAM p1)
{
struct PopDown *ActivePopDown = wnd->mnu->Selections;
if (ActivePopDown != NULL) {
int *attr = &(ActivePopDown+(int)p1)->Attrib;
wnd->mnu->Selection = (int)p1;
if (!(*attr & INACTIVE)) {
WINDOW pwnd = GetParent(wnd);
if (*attr & TOGGLE)
*attr ^= CHECKED;
if (pwnd != NULL) {
CurrentMenuSelection = p1;
PostMessage(pwnd, COMMAND,
(ActivePopDown+(int)p1)->ActionId, 0); /* p2 was p1 */
}
}
else
beep();
}
}
/* ---------- KEYBOARD Message --------- */
static BOOL KeyboardMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
struct PopDown *ActivePopDown = wnd->mnu->Selections;
if (wnd->mnu != NULL) {
if (ActivePopDown != NULL) {
int c = (int)p1;
int sel = 0;
int a;
struct PopDown *pd = ActivePopDown;
if ((c & ~0x7F) == 0) // FIXME unicode
c = tolower(c);
a = AltConvert(c);
while (pd->SelectionTitle != NULL) {
char *cp = strchr(pd->SelectionTitle,
SHORTCUTCHAR);
if (cp) {
int sc = tolower(*(cp+1));
if ((cp && sc == c) ||
(a && sc == a) ||
pd->Accelerator == c) {
PostMessage(wnd, LB_SELECTION, sel, 0);
PostMessage(wnd, LB_CHOOSE, sel, TRUE);
return TRUE;
}
}
pd++, sel++;
}
}
}
switch ((int)p1) {
case F1:
if (ActivePopDown == NULL)
SendMessage(GetParent(wnd), KEYBOARD, p1, p2);
else
#ifdef INCLUDE_HELP
DisplayHelp(wnd,
(ActivePopDown+wnd->selection)->help);
return TRUE;
#endif
case ESC:
SendMessage(wnd, CLOSE_WINDOW, 0, 0);
return TRUE;
case FWD:
case BS:
if (GetClass(GetParent(wnd)) == MENUBAR)
PostMessage(GetParent(wnd), KEYBOARD, p1, p2);
return TRUE;
case UP:
if (wnd->selection == 0) {
if (wnd->wlines == ClientHeight(wnd)) {
PostMessage(wnd, LB_SELECTION,
wnd->wlines-1, FALSE);
return TRUE;
}
}
break;
case DN:
if (wnd->selection == wnd->wlines-1) {
if (wnd->wlines == ClientHeight(wnd)) {
PostMessage(wnd, LB_SELECTION, 0, FALSE);
return TRUE;
}
}
break;
case HOME:
case END:
case '\r':
break;
default:
return TRUE;
}
return FALSE;
}
/* ----------- CLOSE_WINDOW Message ---------- */
static int CloseWindowMsg(WINDOW wnd)
{
int rtn;
WINDOW pwnd = GetParent(wnd);
SendMessage(wnd, RELEASE_MOUSE, 0, 0);
SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
SendMessage(NULL, RESTORE_CURSOR, 0, 0);
inFocus = wnd->oldFocus;
rtn = BaseWndProc(POPDOWNMENU, wnd, CLOSE_WINDOW, 0, 0);
SendMessage(pwnd, CLOSE_POPDOWN, 0, 0);
return rtn;
}
/* - Window processing module for POPDOWNMENU window class - */
int PopDownProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
switch (msg) {
case CREATE_WINDOW:
return CreateWindowMsg(wnd);
case LEFT_BUTTON:
LeftButtonMsg(wnd, p1, p2);
return FALSE;
case DOUBLE_CLICK:
return TRUE;
case LB_SELECTION:
if (*TextLine(wnd, (int)p1) == LINE)
return TRUE;
wnd->mnu->Selection = (int)p1;
break;
case BUTTON_RELEASED:
if (ButtonReleasedMsg(wnd, p1, p2))
return TRUE;
break;
case BUILD_SELECTIONS:
wnd->mnu = (void *) p1;
wnd->selection = wnd->mnu->Selection;
break;
case PAINT:
if (wnd->mnu == NULL)
return TRUE;
PaintMsg(wnd);
break;
case BORDER:
return BorderMsg(wnd);
case LB_CHOOSE:
LBChooseMsg(wnd, p1);
return TRUE;
case KEYBOARD:
if (KeyboardMsg(wnd, p1, p2))
return TRUE;
break;
case CLOSE_WINDOW:
return CloseWindowMsg(wnd);
default:
break;
}
return BaseWndProc(POPDOWNMENU, wnd, msg, p1, p2);
}
/* --------- compute menu height -------- */
int MenuHeight(struct PopDown *pd)
{
int ht = 0;
while (pd[ht].SelectionTitle != NULL)
ht++;
return ht+2;
}
/* --------- compute menu width -------- */
int MenuWidth(struct PopDown *pd)
{
int wd = 0, i;
int len = 0;
wd = SelectionWidth(pd);
while (pd->SelectionTitle != NULL) {
if (pd->Accelerator) {
for (i = 0; keys[i].keylabel; i++)
if (keys[i].keycode == pd->Accelerator) {
len = max(len, 2+strlen(keys[i].keylabel));
break;
}
}
if (pd->Attrib & CASCADED)
len = max(len, 2);
pd++;
}
return wd+5+len;
}
/* ---- compute the maximum selection width in a menu ---- */
static int SelectionWidth(struct PopDown *pd)
{
int wd = 0;
while (pd->SelectionTitle != NULL) {
int len = strlen(pd->SelectionTitle)-1;
wd = max(wd, len);
pd++;
}
return wd;
}
/* ----- copy a menu command to a display buffer ---- */
int CopyCommand(unsigned char *dest, unsigned char *src,
int skipcolor, int bg)
{
unsigned char *d = dest;
while (*src && *src != '\n') {
if (*src == SHORTCUTCHAR) {
src++;
if (!skipcolor) {
*dest++ = CHANGECOLOR;
*dest++ = cfg.clr[POPDOWNMENU]
[HILITE_COLOR] [BG] | 0x80;
*dest++ = bg | 0x80;
*dest++ = *src++;
*dest++ = RESETCOLOR;
}
}
else
*dest++ = *src++;
}
return (int) (dest - d);
}