forked from alexfru/dflat20
-
Notifications
You must be signed in to change notification settings - Fork 2
/
listbox.c
470 lines (442 loc) · 13 KB
/
listbox.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* ------------- listbox.c ------------ */
#include "dflat.h"
#ifdef INCLUDE_EXTENDEDSELECTIONS
static int ExtendSelections(WINDOW, int, int);
static void TestExtended(WINDOW, PARAM);
static void ClearAllSelections(WINDOW);
static void SetSelection(WINDOW, int);
static void FlipSelection(WINDOW, int);
static void ClearSelection(WINDOW, int);
#else
#define TestExtended(w,p) /**/
#endif
static void near ChangeSelection(WINDOW, int, int);
static void near WriteSelection(WINDOW, int, int, RECT *);
static BOOL SelectionInWindow(WINDOW, int);
static int py = -1; /* the previous y mouse coordinate */
#ifdef INCLUDE_EXTENDEDSELECTIONS
/* --------- SHIFT_F8 Key ------------ */
static void AddModeKey(WINDOW wnd)
{
if (isMultiLine(wnd)) {
wnd->AddMode ^= TRUE;
SendMessage(GetParent(wnd), ADDSTATUS,
wnd->AddMode ? ((PARAM) "Add Mode") : 0, 0);
}
}
#endif
/* --------- UP (Up Arrow) Key ------------ */
static void UpKey(WINDOW wnd, PARAM p2)
{
if (wnd->selection > 0) {
if (wnd->selection == wnd->wtop) {
BaseWndProc(LISTBOX, wnd, KEYBOARD, UP, p2);
PostMessage(wnd, LB_SELECTION, wnd->selection-1,
isMultiLine(wnd) ? p2 : FALSE);
}
else {
int newsel = wnd->selection-1;
if (wnd->wlines == ClientHeight(wnd))
while (*TextLine(wnd, newsel) == LINE)
--newsel;
PostMessage(wnd, LB_SELECTION, newsel,
#ifdef INCLUDE_EXTENDEDSELECTIONS
isMultiLine(wnd) ? p2 :
#endif
FALSE);
}
}
}
/* --------- DN (Down Arrow) Key ------------ */
static void DnKey(WINDOW wnd, PARAM p2)
{
if (wnd->selection < wnd->wlines-1) {
if (wnd->selection == wnd->wtop+ClientHeight(wnd)-1) {
BaseWndProc(LISTBOX, wnd, KEYBOARD, DN, p2);
PostMessage(wnd, LB_SELECTION, wnd->selection+1,
isMultiLine(wnd) ? p2 : FALSE);
}
else {
int newsel = wnd->selection+1;
if (wnd->wlines == ClientHeight(wnd))
while (*TextLine(wnd, newsel) == LINE)
newsel++;
PostMessage(wnd, LB_SELECTION, newsel,
#ifdef INCLUDE_EXTENDEDSELECTIONS
isMultiLine(wnd) ? p2 :
#endif
FALSE);
}
}
}
/* --------- HOME and PGUP Keys ------------ */
static void HomePgUpKey(WINDOW wnd, PARAM p1, PARAM p2)
{
BaseWndProc(LISTBOX, wnd, KEYBOARD, p1, p2);
PostMessage(wnd, LB_SELECTION, wnd->wtop,
#ifdef INCLUDE_EXTENDEDSELECTIONS
isMultiLine(wnd) ? p2 :
#endif
FALSE);
}
/* --------- END and PGDN Keys ------------ */
static void EndPgDnKey(WINDOW wnd, PARAM p1, PARAM p2)
{
int bot;
BaseWndProc(LISTBOX, wnd, KEYBOARD, p1, p2);
bot = wnd->wtop+ClientHeight(wnd)-1;
if (bot > wnd->wlines-1)
bot = wnd->wlines-1;
PostMessage(wnd, LB_SELECTION, bot,
#ifdef INCLUDE_EXTENDEDSELECTIONS
isMultiLine(wnd) ? p2 :
#endif
FALSE);
}
#ifdef INCLUDE_EXTENDEDSELECTIONS
/* --------- Space Bar Key ------------ */
static void SpacebarKey(WINDOW wnd, PARAM p2)
{
if (isMultiLine(wnd)) {
int sel = SendMessage(wnd, LB_CURRENTSELECTION, 0, 0);
if (sel != -1) {
if (wnd->AddMode)
FlipSelection(wnd, sel);
if (ItemSelected(wnd, sel)) {
if (!((int) p2 & (LEFTSHIFT | RIGHTSHIFT)))
wnd->AnchorPoint = sel;
ExtendSelections(wnd, sel, (int) p2);
}
else
wnd->AnchorPoint = -1;
SendMessage(wnd, PAINT, 0, 0);
}
}
}
#endif
/* --------- Enter ('\r') Key ------------ */
static void EnterKey(WINDOW wnd)
{
if (wnd->selection != -1) {
SendMessage(wnd, LB_SELECTION, wnd->selection, TRUE);
SendMessage(wnd, LB_CHOOSE, wnd->selection, 0);
}
}
/* --------- All Other Key Presses ------------ */
static void KeyPress(WINDOW wnd, PARAM p1, PARAM p2)
{
int sel = wnd->selection+1;
while (sel < wnd->wlines) {
char *cp = TextLine(wnd, sel);
if (cp == NULL)
break;
#ifdef INCLUDE_EXTENDEDSELECTIONS
if (isMultiLine(wnd))
cp++;
#endif
if (tolower(*cp) == (int)p1) {
SendMessage(wnd, LB_SELECTION, sel,
isMultiLine(wnd) ? p2 : FALSE);
if (!SelectionInWindow(wnd, sel)) {
wnd->wtop = sel-ClientHeight(wnd)+1;
SendMessage(wnd, PAINT, 0, 0);
}
break;
}
sel++;
}
}
/* --------- KEYBOARD Message ------------ */
static int KeyboardMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
switch ((int) p1) {
#ifdef INCLUDE_EXTENDEDSELECTIONS
case SHIFT_F8:
AddModeKey(wnd);
return TRUE;
#endif
case UP:
TestExtended(wnd, p2);
UpKey(wnd, p2);
return TRUE;
case DN:
TestExtended(wnd, p2);
DnKey(wnd, p2);
return TRUE;
case PGUP:
case HOME:
TestExtended(wnd, p2);
HomePgUpKey(wnd, p1, p2);
return TRUE;
case PGDN:
case END:
TestExtended(wnd, p2);
EndPgDnKey(wnd, p1, p2);
return TRUE;
#ifdef INCLUDE_EXTENDEDSELECTIONS
case ' ':
SpacebarKey(wnd, p2);
break;
#endif
case '\r':
EnterKey(wnd);
return TRUE;
default:
KeyPress(wnd, p1, p2);
break;
}
return FALSE;
}
/* ------- LEFT_BUTTON Message -------- */
static int LeftButtonMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
int my = (int) p2 - GetTop(wnd);
if (my >= wnd->wlines-wnd->wtop)
my = wnd->wlines - wnd->wtop;
if (!InsideRect(p1, p2, ClientRect(wnd)))
return FALSE;
if (wnd->wlines && my != py) {
int sel = wnd->wtop+my-1;
#ifdef INCLUDE_EXTENDEDSELECTIONS
int sh = getshift();
if (!(sh & (LEFTSHIFT | RIGHTSHIFT))) {
if (!(sh & CTRLKEY))
ClearAllSelections(wnd);
wnd->AnchorPoint = sel;
SendMessage(wnd, PAINT, 0, 0);
}
#endif
SendMessage(wnd, LB_SELECTION, sel, TRUE);
py = my;
}
return TRUE;
}
/* ------------- DOUBLE_CLICK Message ------------ */
static int DoubleClickMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
if (WindowMoving || WindowSizing)
return FALSE;
if (wnd->wlines) {
RECT rc = ClientRect(wnd);
BaseWndProc(LISTBOX, wnd, DOUBLE_CLICK, p1, p2);
if (InsideRect(p1, p2, rc))
SendMessage(wnd, LB_CHOOSE, wnd->selection, 0);
}
return TRUE;
}
/* ------------ ADDTEXT Message -------------- */
static int AddTextMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
int rtn = BaseWndProc(LISTBOX, wnd, ADDTEXT, p1, p2);
if (wnd->selection == -1)
SendMessage(wnd, LB_SETSELECTION, 0, 0);
#ifdef INCLUDE_EXTENDEDSELECTIONS
if (*(char *)p1 == LISTSELECTOR)
wnd->SelectCount++;
#endif
return rtn;
}
/* --------- GETTEXT Message ------------ */
static void GetTextMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
if ((int)p2 != -1) {
char *cp1 = (char *)p1;
char *cp2 = TextLine(wnd, (int)p2);
while (cp2 && *cp2 && *cp2 != '\n')
*cp1++ = *cp2++;
*cp1 = '\0';
}
}
/* --------- LISTBOX Window Processing Module ------------ */
int ListBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
switch (msg) {
case CREATE_WINDOW:
BaseWndProc(LISTBOX, wnd, msg, p1, p2);
wnd->selection = -1;
#ifdef INCLUDE_EXTENDEDSELECTIONS
wnd->AnchorPoint = -1;
#endif
return TRUE;
case KEYBOARD:
if (WindowMoving || WindowSizing)
break;
if (KeyboardMsg(wnd, p1, p2))
return TRUE;
break;
case LEFT_BUTTON:
if (LeftButtonMsg(wnd, p1, p2) == TRUE)
return TRUE;
break;
case DOUBLE_CLICK:
if (DoubleClickMsg(wnd, p1, p2))
return TRUE;
break;
case BUTTON_RELEASED:
if (WindowMoving || WindowSizing || VSliding)
break;
py = -1;
return TRUE;
case ADDTEXT:
return AddTextMsg(wnd, p1, p2);
case LB_GETTEXT:
GetTextMsg(wnd, p1, p2);
return TRUE;
case CLEARTEXT:
wnd->selection = -1;
#ifdef INCLUDE_EXTENDEDSELECTIONS
wnd->AnchorPoint = -1;
#endif
wnd->SelectCount = 0;
break;
case PAINT:
BaseWndProc(LISTBOX, wnd, msg, p1, p2);
WriteSelection(wnd, wnd->selection, TRUE, (RECT *)p1);
return TRUE;
case SETFOCUS:
BaseWndProc(LISTBOX, wnd, msg, p1, p2);
if ((int)p1)
WriteSelection(wnd, wnd->selection, TRUE, NULL);
return TRUE;
case SCROLL:
case HORIZSCROLL:
case SCROLLPAGE:
case HORIZPAGE:
case SCROLLDOC:
BaseWndProc(LISTBOX, wnd, msg, p1, p2);
WriteSelection(wnd,wnd->selection,TRUE,NULL);
return TRUE;
case LB_CHOOSE:
SendMessage(GetParent(wnd), LB_CHOOSE, p1, p2);
return TRUE;
case LB_SELECTION:
ChangeSelection(wnd, (int) p1, (int) p2);
SendMessage(GetParent(wnd), LB_SELECTION,
wnd->selection, 0);
return TRUE;
case LB_CURRENTSELECTION:
return wnd->selection;
case LB_SETSELECTION:
ChangeSelection(wnd, (int) p1, 0);
return TRUE;
#ifdef INCLUDE_EXTENDEDSELECTIONS
case CLOSE_WINDOW:
if (isMultiLine(wnd) && wnd->AddMode) {
wnd->AddMode = FALSE;
SendMessage(GetParent(wnd), ADDSTATUS, 0, 0);
}
break;
#endif
default:
break;
}
return BaseWndProc(LISTBOX, wnd, msg, p1, p2);
}
static BOOL SelectionInWindow(WINDOW wnd, int sel)
{
return (wnd->wlines && sel >= wnd->wtop &&
sel < wnd->wtop+ClientHeight(wnd));
}
static void near WriteSelection(WINDOW wnd, int sel,
int reverse, RECT *rc)
{
if (isVisible(wnd))
if (SelectionInWindow(wnd, sel))
WriteTextLine(wnd, rc, sel, reverse);
}
#ifdef INCLUDE_EXTENDEDSELECTIONS
/* ----- Test for extended selections in the listbox ----- */
static void TestExtended(WINDOW wnd, PARAM p2)
{
if (isMultiLine(wnd) && !wnd->AddMode &&
!((int) p2 & (LEFTSHIFT | RIGHTSHIFT))) {
if (wnd->SelectCount > 1) {
ClearAllSelections(wnd);
SendMessage(wnd, PAINT, 0, 0);
}
}
}
/* ----- Clear selections in the listbox ----- */
static void ClearAllSelections(WINDOW wnd)
{
if (isMultiLine(wnd) && wnd->SelectCount > 0) {
int sel;
for (sel = 0; sel < wnd->wlines; sel++)
ClearSelection(wnd, sel);
}
}
/* ----- Invert a selection in the listbox ----- */
static void FlipSelection(WINDOW wnd, int sel)
{
if (isMultiLine(wnd)) {
if (ItemSelected(wnd, sel))
ClearSelection(wnd, sel);
else
SetSelection(wnd, sel);
}
}
static int ExtendSelections(WINDOW wnd, int sel, int shift)
{
if (shift & (LEFTSHIFT | RIGHTSHIFT) &&
wnd->AnchorPoint != -1) {
int i = sel;
int j = wnd->AnchorPoint;
int rtn;
if (j > i)
swap(i,j);
rtn = i - j;
while (j <= i)
SetSelection(wnd, j++);
return rtn;
}
return 0;
}
static void SetSelection(WINDOW wnd, int sel)
{
if (isMultiLine(wnd) && !ItemSelected(wnd, sel)) {
char *lp = TextLine(wnd, sel);
*lp = LISTSELECTOR;
wnd->SelectCount++;
}
}
static void ClearSelection(WINDOW wnd, int sel)
{
if (isMultiLine(wnd) && ItemSelected(wnd, sel)) {
char *lp = TextLine(wnd, sel);
*lp = ' ';
--wnd->SelectCount;
}
}
BOOL ItemSelected(WINDOW wnd, int sel)
{
if (sel != -1 && isMultiLine(wnd) && sel < wnd->wlines) {
char *cp = TextLine(wnd, sel);
return (int)((*cp) & 255) == LISTSELECTOR;
}
return FALSE;
}
#endif
static void near ChangeSelection(WINDOW wnd,int sel,int shift)
{
if (sel != wnd->selection) {
#ifdef INCLUDE_EXTENDEDSELECTIONS
if (sel != -1 && isMultiLine(wnd)) {
int sels;
if (!wnd->AddMode)
ClearAllSelections(wnd);
sels = ExtendSelections(wnd, sel, shift);
if (sels > 1)
SendMessage(wnd, PAINT, 0, 0);
if (sels == 0 && !wnd->AddMode) {
ClearSelection(wnd, wnd->selection);
SetSelection(wnd, sel);
wnd->AnchorPoint = sel;
}
}
#endif
WriteSelection(wnd, wnd->selection, FALSE, NULL);
wnd->selection = sel;
if (sel != -1)
WriteSelection(wnd, sel, TRUE, NULL);
}
}