-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patheditbox.c
1131 lines (1126 loc) · 33.2 KB
/
editbox.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ------------- editbox.c ------------ */
#include "dflat.h"
#define EditBufLen(wnd) (isMultiLine(wnd) ? EDITLEN : ENTRYLEN)
#define SetLinePointer(wnd, ln) (wnd->CurrLine = ln)
#define Ch(c) ((c)&0x7f)
#define isWhite(c) (Ch(c)==' '||Ch(c)=='\n'||Ch(c)=='\f'||Ch(c)=='\t')
/* ---------- local prototypes ----------- */
static void SaveDeletedText(WINDOW, char *, int);
static void Forward(WINDOW);
static void Backward(WINDOW);
static void End(WINDOW);
static void Home(WINDOW);
static void Downward(WINDOW);
static void Upward(WINDOW);
static void StickEnd(WINDOW);
static void NextWord(WINDOW);
static void PrevWord(WINDOW);
static void ModTextPointers(WINDOW, int, int);
static void SetAnchor(WINDOW, int, int);
/* -------- local variables -------- */
static BOOL KeyBoardMarking, ButtonDown;
static BOOL TextMarking;
static int ButtonX, ButtonY;
static int PrevY = -1;
/* ----------- CREATE_WINDOW Message ---------- */
static int CreateWindowMsg(WINDOW wnd)
{
int rtn = BaseWndProc(EDITBOX, wnd, CREATE_WINDOW, 0, 0);
wnd->MaxTextLength = MAXTEXTLEN+1;
wnd->textlen = EditBufLen(wnd);
wnd->InsertMode = TRUE;
if (isMultiLine(wnd))
wnd->WordWrapMode = TRUE;
SendMessage(wnd, CLEARTEXT, 0, 0);
return rtn;
}
/* ----------- SETTEXT Message ---------- */
static int SetTextMsg(WINDOW wnd, PARAM p1)
{
int rtn = FALSE;
if (strlen((char *)p1) <= wnd->MaxTextLength) {
rtn = BaseWndProc(EDITBOX, wnd, SETTEXT, p1, 0);
wnd->TextChanged = FALSE;
}
return rtn;
}
/* ----------- CLEARTEXT Message ------------ */
static int ClearTextMsg(WINDOW wnd)
{
int rtn = BaseWndProc(EDITBOX, wnd, CLEARTEXT, 0, 0);
unsigned blen = EditBufLen(wnd)+2;
wnd->text = DFrealloc(wnd->text, blen);
memset(wnd->text, 0, blen);
wnd->wlines = 0;
wnd->CurrLine = 0;
wnd->CurrCol = 0;
wnd->WndRow = 0;
wnd->wleft = 0;
wnd->wtop = 0;
wnd->textwidth = 0;
wnd->TextChanged = FALSE;
return rtn;
}
/* ----------- ADDTEXT Message ---------- */
static int AddTextMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
int rtn = FALSE;
if (strlen((char *)p1)+wnd->textlen <= wnd->MaxTextLength) {
rtn = BaseWndProc(EDITBOX, wnd, ADDTEXT, p1, p2);
if (rtn != FALSE) {
if (!isMultiLine(wnd)) {
wnd->CurrLine = 0;
wnd->CurrCol = strlen((char *)p1);
if (wnd->CurrCol >= ClientWidth(wnd)) {
wnd->wleft = wnd->CurrCol-ClientWidth(wnd);
wnd->CurrCol -= wnd->wleft;
}
wnd->BlkEndCol = wnd->CurrCol;
SendMessage(wnd, KEYBOARD_CURSOR,
WndCol, wnd->WndRow);
}
}
}
return rtn;
}
/* ----------- GETTEXT Message ---------- */
static int GetTextMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
char *cp1 = (char *)p1;
char *cp2 = wnd->text;
if (cp2 != NULL) {
while (p2-- && *cp2 && *cp2 != '\n')
*cp1++ = *cp2++;
*cp1 = '\0';
return TRUE;
}
return FALSE;
}
/* ----------- SETTEXTLENGTH Message ---------- */
static int SetTextLengthMsg(WINDOW wnd, unsigned int len)
{
if (++len < MAXTEXTLEN) {
wnd->MaxTextLength = len;
if (len < wnd->textlen) {
wnd->text=DFrealloc(wnd->text, len+2);
wnd->textlen = len;
*((wnd->text)+len) = '\0';
*((wnd->text)+len+1) = '\0';
BuildTextPointers(wnd);
}
return TRUE;
}
return FALSE;
}
/* ----------- KEYBOARD_CURSOR Message ---------- */
static void KeyboardCursorMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
wnd->CurrCol = (int)p1 + wnd->wleft;
wnd->WndRow = (int)p2;
wnd->CurrLine = (int)p2 + wnd->wtop;
if (wnd == inFocus) {
if (CharInView(wnd, (int)p1, (int)p2))
SendMessage(NULL, SHOW_CURSOR,
(wnd->InsertMode && !TextMarking), 0);
else
SendMessage(NULL, HIDE_CURSOR, 0, 0);
}
}
/* ----------- SIZE Message ---------- */
int SizeMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
int rtn = BaseWndProc(EDITBOX, wnd, SIZE, p1, p2);
if (WndCol > ClientWidth(wnd)-1)
wnd->CurrCol = ClientWidth(wnd)-1 + wnd->wleft;
if (wnd->WndRow > ClientHeight(wnd)-1) {
wnd->WndRow = ClientHeight(wnd)-1;
SetLinePointer(wnd, wnd->WndRow+wnd->wtop);
}
SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
return rtn;
}
/* ----------- SCROLL Message ---------- */
static int ScrollMsg(WINDOW wnd, PARAM p1)
{
int rtn = FALSE;
if (isMultiLine(wnd)) {
rtn = BaseWndProc(EDITBOX,wnd,SCROLL,p1,0);
if (rtn != FALSE) {
if (p1) {
/* -------- scrolling up --------- */
if (wnd->WndRow == 0) {
wnd->CurrLine++;
StickEnd(wnd);
}
else
--wnd->WndRow;
}
else {
/* -------- scrolling down --------- */
if (wnd->WndRow == ClientHeight(wnd)-1) {
if (wnd->CurrLine > 0)
--wnd->CurrLine;
StickEnd(wnd);
}
else
wnd->WndRow++;
}
SendMessage(wnd,KEYBOARD_CURSOR,WndCol,wnd->WndRow);
}
}
return rtn;
}
/* ----------- HORIZSCROLL Message ---------- */
static int HorizScrollMsg(WINDOW wnd, PARAM p1)
{
int rtn = FALSE;
char *currchar = CurrChar;
if (!(p1 &&
wnd->CurrCol == wnd->wleft && *currchar == '\n')) {
rtn = BaseWndProc(EDITBOX, wnd, HORIZSCROLL, p1, 0);
if (rtn != FALSE) {
if (wnd->CurrCol < wnd->wleft)
wnd->CurrCol++;
else if (WndCol == ClientWidth(wnd))
--wnd->CurrCol;
SendMessage(wnd,KEYBOARD_CURSOR,WndCol,wnd->WndRow);
}
}
return rtn;
}
/* ----------- SCROLLPAGE Message ---------- */
static int ScrollPageMsg(WINDOW wnd, PARAM p1)
{
int rtn = FALSE;
if (isMultiLine(wnd)) {
rtn = BaseWndProc(EDITBOX, wnd, SCROLLPAGE, p1, 0);
SetLinePointer(wnd, wnd->wtop+wnd->WndRow);
StickEnd(wnd);
SendMessage(wnd, KEYBOARD_CURSOR,WndCol, wnd->WndRow);
}
return rtn;
}
/* ----------- HORIZSCROLLPAGE Message ---------- */
static int HorizPageMsg(WINDOW wnd, PARAM p1)
{
int rtn = BaseWndProc(EDITBOX, wnd, HORIZPAGE, p1, 0);
if ((int) p1 == FALSE) {
if (wnd->CurrCol > wnd->wleft+ClientWidth(wnd)-1)
wnd->CurrCol = wnd->wleft+ClientWidth(wnd)-1;
}
else if (wnd->CurrCol < wnd->wleft)
wnd->CurrCol = wnd->wleft;
SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
return rtn;
}
/* ----- Extend the marked block to the new x,y position ---- */
static void ExtendBlock(WINDOW wnd, int x, int y)
{
int bbl, bel;
int ptop = min(wnd->BlkBegLine, wnd->BlkEndLine);
int pbot = max(wnd->BlkBegLine, wnd->BlkEndLine);
char *lp = TextLine(wnd, wnd->wtop+y);
int len = (int) (strchr(lp, '\n') - lp);
x = max(0, min(x, len));
y = max(0, y);
wnd->BlkEndCol = min(len, x+wnd->wleft);
wnd->BlkEndLine = y+wnd->wtop;
bbl = min(wnd->BlkBegLine, wnd->BlkEndLine);
bel = max(wnd->BlkBegLine, wnd->BlkEndLine);
while (ptop < bbl) {
WriteTextLine(wnd, NULL, ptop, FALSE);
ptop++;
}
for (y = bbl; y <= bel; y++)
WriteTextLine(wnd, NULL, y, FALSE);
while (pbot > bel) {
WriteTextLine(wnd, NULL, pbot, FALSE);
--pbot;
}
}
/* ----------- LEFT_BUTTON Message ---------- */
static int LeftButtonMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
int MouseX = (int) p1 - GetClientLeft(wnd);
int MouseY = (int) p2 - GetClientTop(wnd);
RECT rc = ClientRect(wnd);
char *lp;
int len;
if (KeyBoardMarking)
return TRUE;
if (WindowMoving || WindowSizing)
return FALSE;
if (TextMarking) {
if (!InsideRect(p1, p2, rc)) {
int x = MouseX, y = MouseY;
int dir;
MESSAGE msg = 0;
if ((int)p2 == GetTop(wnd))
y++, dir = FALSE, msg = SCROLL;
else if ((int)p2 == GetBottom(wnd))
--y, dir = TRUE, msg = SCROLL;
else if ((int)p1 == GetLeft(wnd))
--x, dir = FALSE, msg = HORIZSCROLL;
else if ((int)p1 == GetRight(wnd))
x++, dir = TRUE, msg = HORIZSCROLL;
if (msg != 0) {
if (SendMessage(wnd, msg, dir, 0))
ExtendBlock(wnd, x, y);
SendMessage(wnd, PAINT, 0, 0);
}
}
return TRUE;
}
if (!InsideRect(p1, p2, rc))
return FALSE;
if (TextBlockMarked(wnd)) {
ClearTextBlock(wnd);
SendMessage(wnd, PAINT, 0, 0);
}
if (wnd->wlines) {
if (MouseY > wnd->wlines-1)
return TRUE;
lp = TextLine(wnd, MouseY+wnd->wtop);
len = (int) (strchr(lp, '\n') - lp);
MouseX = min(MouseX, len);
if (MouseX < wnd->wleft) {
MouseX = 0;
SendMessage(wnd, KEYBOARD, HOME, 0);
}
ButtonDown = TRUE;
ButtonX = MouseX;
ButtonY = MouseY;
}
else
MouseX = MouseY = 0;
wnd->WndRow = MouseY;
SetLinePointer(wnd, MouseY+wnd->wtop);
if (isMultiLine(wnd) ||
(!TextBlockMarked(wnd)
&& MouseX+wnd->wleft < strlen(wnd->text)))
wnd->CurrCol = MouseX+wnd->wleft;
SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
return TRUE;
}
/* ----------- MOUSE_MOVED Message ---------- */
static int MouseMovedMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
int MouseX = (int) p1 - GetClientLeft(wnd);
int MouseY = (int) p2 - GetClientTop(wnd);
RECT rc = ClientRect(wnd);
if (!InsideRect(p1, p2, rc))
return FALSE;
if (MouseY > wnd->wlines-1)
return FALSE;
if (ButtonDown) {
SetAnchor(wnd, ButtonX+wnd->wleft, ButtonY+wnd->wtop);
TextMarking = TRUE;
rc = WindowRect(wnd);
SendMessage(NULL,MOUSE_TRAVEL,(PARAM) &rc, 0);
ButtonDown = FALSE;
}
if (TextMarking && !(WindowMoving || WindowSizing)) {
ExtendBlock(wnd, MouseX, MouseY);
return TRUE;
}
return FALSE;
}
static void StopMarking(WINDOW wnd)
{
TextMarking = FALSE;
if (wnd->BlkBegLine > wnd->BlkEndLine) {
swap(wnd->BlkBegLine, wnd->BlkEndLine);
swap(wnd->BlkBegCol, wnd->BlkEndCol);
}
if (wnd->BlkBegLine == wnd->BlkEndLine &&
wnd->BlkBegCol > wnd->BlkEndCol)
swap(wnd->BlkBegCol, wnd->BlkEndCol);
}
/* ----------- BUTTON_RELEASED Message ---------- */
static int ButtonReleasedMsg(WINDOW wnd)
{
ButtonDown = FALSE;
if (TextMarking && !(WindowMoving || WindowSizing)) {
/* release the mouse ouside the edit box */
SendMessage(NULL, MOUSE_TRAVEL, 0, 0);
StopMarking(wnd);
return TRUE;
}
PrevY = -1;
return FALSE;
}
/* ---- Process text block keys for multiline text box ---- */
static void DoMultiLines(WINDOW wnd, int c, PARAM p2)
{
if (!KeyBoardMarking) {
if ((int)p2 & (LEFTSHIFT | RIGHTSHIFT)) {
switch (c) {
case HOME:
case CTRL_HOME:
case CTRL_BS:
case PGUP:
case CTRL_PGUP:
case UP:
case BS:
case END:
case CTRL_END:
case PGDN:
case CTRL_PGDN:
case DN:
case FWD:
case CTRL_FWD:
KeyBoardMarking = TextMarking = TRUE;
SetAnchor(wnd, wnd->CurrCol, wnd->CurrLine);
break;
default:
break;
}
}
}
}
/* ---------- page/scroll keys ----------- */
static int DoScrolling(WINDOW wnd, int c, PARAM p2)
{
switch (c) {
case PGUP:
case PGDN:
if (isMultiLine(wnd))
BaseWndProc(EDITBOX, wnd, KEYBOARD, c, p2);
break;
case CTRL_PGUP:
case CTRL_PGDN:
BaseWndProc(EDITBOX, wnd, KEYBOARD, c, p2);
break;
case HOME:
Home(wnd);
break;
case END:
End(wnd);
break;
case CTRL_FWD:
NextWord(wnd);
break;
case CTRL_BS:
PrevWord(wnd);
break;
case CTRL_HOME:
if (isMultiLine(wnd)) {
SendMessage(wnd, SCROLLDOC, TRUE, 0);
wnd->CurrLine = 0;
wnd->WndRow = 0;
}
Home(wnd);
break;
case CTRL_END:
if (isMultiLine(wnd) &&
wnd->WndRow+wnd->wtop+1 < wnd->wlines
&& wnd->wlines > 0) {
SendMessage(wnd, SCROLLDOC, FALSE, 0);
SetLinePointer(wnd, wnd->wlines-1);
wnd->WndRow =
min(ClientHeight(wnd)-1, wnd->wlines-1);
Home(wnd);
}
End(wnd);
break;
case UP:
if (isMultiLine(wnd))
Upward(wnd);
break;
case DN:
if (isMultiLine(wnd))
Downward(wnd);
break;
case FWD:
Forward(wnd);
break;
case BS:
Backward(wnd);
break;
default:
return FALSE;
}
if (!KeyBoardMarking && TextBlockMarked(wnd)) {
ClearTextBlock(wnd);
SendMessage(wnd, PAINT, 0, 0);
}
SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
return TRUE;
}
/* -------------- Del key ---------------- */
static void DelKey(WINDOW wnd)
{
char *currchar = CurrChar;
int repaint = *currchar == '\n';
if (TextBlockMarked(wnd)) {
SendMessage(wnd, COMMAND, ID_DELETETEXT, 0);
SendMessage(wnd, PAINT, 0, 0);
return;
}
if (isMultiLine(wnd) && *currchar == '\n' && *(currchar+1) == '\0')
return;
strcpy(currchar, currchar+1);
if (repaint) {
BuildTextPointers(wnd);
SendMessage(wnd, PAINT, 0, 0);
}
else {
ModTextPointers(wnd, wnd->CurrLine+1, -1);
WriteTextLine(wnd, NULL, wnd->WndRow+wnd->wtop, FALSE);
}
wnd->TextChanged = TRUE;
}
/* ------------ Tab key ------------ */
static void TabKey(WINDOW wnd, PARAM p2)
{
if (isMultiLine(wnd)) {
int insmd = wnd->InsertMode;
do {
char *cc = CurrChar+1;
if (!insmd && *cc == '\0')
break;
if (wnd->textlen == wnd->MaxTextLength)
break;
SendMessage(wnd,KEYBOARD,insmd ? ' ' : FWD,0);
} while (wnd->CurrCol % cfg.Tabs);
}
else
PostMessage(GetParent(wnd), KEYBOARD, '\t', p2);
}
/* ------------ Shift+Tab key ------------ */
static void ShiftTabKey(WINDOW wnd, PARAM p2)
{
if (isMultiLine(wnd)) {
do {
if (CurrChar == GetText(wnd))
break;
SendMessage(wnd,KEYBOARD,BS,0);
} while (wnd->CurrCol % cfg.Tabs);
}
else
PostMessage(GetParent(wnd), KEYBOARD, SHIFT_HT, p2);
}
/* --------- All displayable typed keys ------------- */
static void KeyTyped(WINDOW wnd, int c)
{
char *currchar = CurrChar;
if ((c != '\n' && c < ' ') || (c & 0x1000))
/* ---- not recognized by editor --- */
return;
if (!isMultiLine(wnd) && TextBlockMarked(wnd)) {
SendMessage(wnd, CLEARTEXT, 0, 0);
currchar = CurrChar;
}
/* ---- test typing at end of text ---- */
if (currchar == wnd->text+wnd->MaxTextLength) {
/* ---- typing at the end of maximum buffer ---- */
beep();
return;
}
if (*currchar == '\0') {
/* --- insert a newline at end of text --- */
*currchar = '\n';
*(currchar+1) = '\0';
BuildTextPointers(wnd);
}
/* --- displayable char or newline --- */
if (c == '\n' || wnd->InsertMode || *currchar == '\n') {
/* ------ inserting the keyed character ------ */
if (wnd->text[wnd->textlen-1] != '\0') {
/* --- the current text buffer is full --- */
if (wnd->textlen == wnd->MaxTextLength) {
/* --- text buffer is at maximum size --- */
beep();
return;
}
/* ---- increase the text buffer size ---- */
wnd->textlen += GROWLENGTH;
/* --- but not above maximum size --- */
if (wnd->textlen > wnd->MaxTextLength)
wnd->textlen = wnd->MaxTextLength;
wnd->text = DFrealloc(wnd->text, wnd->textlen+2);
wnd->text[wnd->textlen-1] = '\0';
currchar = CurrChar;
}
memmove(currchar+1, currchar, strlen(currchar)+1);
ModTextPointers(wnd, wnd->CurrLine+1, 1);
if (isMultiLine(wnd) && wnd->wlines > 1)
wnd->textwidth = max(wnd->textwidth,
(int) (TextLine(wnd, wnd->CurrLine+1)-
TextLine(wnd, wnd->CurrLine)));
else
wnd->textwidth = max(wnd->textwidth,
strlen(wnd->text));
WriteTextLine(wnd, NULL,
wnd->wtop+wnd->WndRow, FALSE);
}
/* ----- put the char in the buffer ----- */
*currchar = c;
wnd->TextChanged = TRUE;
if (c == '\n') {
wnd->wleft = 0;
BuildTextPointers(wnd);
End(wnd);
Forward(wnd);
SendMessage(wnd, PAINT, 0, 0);
return;
}
/* ---------- test end of window --------- */
if (WndCol == ClientWidth(wnd)-1) {
if (!isMultiLine(wnd)) {
if (!(currchar == wnd->text+wnd->MaxTextLength-2))
SendMessage(wnd, HORIZSCROLL, TRUE, 0);
}
else {
char *cp = currchar;
while (*cp != ' ' && cp != TextLine(wnd, wnd->CurrLine))
--cp;
if (cp == TextLine(wnd, wnd->CurrLine) ||
!wnd->WordWrapMode)
SendMessage(wnd, HORIZSCROLL, TRUE, 0);
else {
int dif = 0;
if (c != ' ') {
dif = (int) (currchar - cp);
wnd->CurrCol -= dif;
SendMessage(wnd, KEYBOARD, DEL, 0);
--dif;
}
SendMessage(wnd, KEYBOARD, '\n', 0);
currchar = CurrChar;
wnd->CurrCol = dif;
if (c == ' ')
return;
}
}
}
/* ------ display the character ------ */
SetStandardColor(wnd);
if (wnd->protect)
c = '*';
PutWindowChar(wnd, c, WndCol, wnd->WndRow);
/* ----- advance the pointers ------ */
wnd->CurrCol++;
}
/* ------------ screen changing key strokes ------------- */
static void DoKeyStroke(WINDOW wnd, int c, PARAM p2)
{
switch (c) {
case RUBOUT:
if (wnd->CurrCol == 0 && wnd->CurrLine == 0)
break;
SendMessage(wnd, KEYBOARD, BS, 0);
SendMessage(wnd, KEYBOARD, DEL, 0);
break;
case DEL:
DelKey(wnd);
break;
case SHIFT_HT:
ShiftTabKey(wnd, p2);
break;
case '\t':
TabKey(wnd, p2);
break;
case '\r':
if (!isMultiLine(wnd)) {
PostMessage(GetParent(wnd), KEYBOARD, c, p2);
break;
}
c = '\n';
default:
if (TextBlockMarked(wnd)) {
SendMessage(wnd, COMMAND, ID_DELETETEXT, 0);
SendMessage(wnd, PAINT, 0, 0);
}
KeyTyped(wnd, c);
break;
}
}
/* ----------- KEYBOARD Message ---------- */
static int KeyboardMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
int c = (int) p1;
if (WindowMoving || WindowSizing || ((int)p2 & ALTKEY))
return FALSE;
switch (c) {
/* --- these keys get processed by lower classes --- */
case ESC:
case F1:
case F2:
case F3:
case F4:
case F5:
case F6:
case F7:
case F8:
case F9:
case F10:
case INS:
case SHIFT_INS:
case SHIFT_DEL:
return FALSE;
/* --- these keys get processed here --- */
case CTRL_FWD:
case CTRL_BS:
case CTRL_HOME:
case CTRL_END:
case CTRL_PGUP:
case CTRL_PGDN:
break;
default:
/* other ctrl keys get processed by lower classes */
if ((int)p2 & CTRLKEY)
return FALSE;
/* --- all other keys get processed here --- */
break;
}
DoMultiLines(wnd, c, p2);
if (DoScrolling(wnd, c, p2)) {
if (KeyBoardMarking)
ExtendBlock(wnd, WndCol, wnd->WndRow);
}
else if (!TestAttribute(wnd, READONLY)) {
DoKeyStroke(wnd, c, p2);
SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
}
else if (c == '\t')
PostMessage(GetParent(wnd), KEYBOARD, '\t', p2);
else
beep();
return TRUE;
}
/* ----------- SHIFT_CHANGED Message ---------- */
static void ShiftChangedMsg(WINDOW wnd, PARAM p1)
{
if (!((int)p1 & (LEFTSHIFT | RIGHTSHIFT)) &&
KeyBoardMarking) {
StopMarking(wnd);
KeyBoardMarking = FALSE;
}
}
/* ----------- ID_DELETETEXT Command ---------- */
static void DeleteTextCmd(WINDOW wnd)
{
if (TextBlockMarked(wnd)) {
char *bbl=TextLine(wnd,wnd->BlkBegLine)+wnd->BlkBegCol;
char *bel=TextLine(wnd,wnd->BlkEndLine)+wnd->BlkEndCol;
int len = (int) (bel - bbl);
SaveDeletedText(wnd, bbl, len);
wnd->TextChanged = TRUE;
strcpy(bbl, bel);
wnd->CurrLine = TextLineNumber(wnd, bbl-wnd->BlkBegCol);
wnd->CurrCol = wnd->BlkBegCol;
wnd->WndRow = wnd->BlkBegLine - wnd->wtop;
if (wnd->WndRow < 0) {
wnd->wtop = wnd->BlkBegLine;
wnd->WndRow = 0;
}
SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
ClearTextBlock(wnd);
BuildTextPointers(wnd);
}
}
/* ----------- ID_CLEAR Command ---------- */
static void ClearCmd(WINDOW wnd)
{
if (TextBlockMarked(wnd)) {
char *bbl=TextLine(wnd,wnd->BlkBegLine)+wnd->BlkBegCol;
char *bel=TextLine(wnd,wnd->BlkEndLine)+wnd->BlkEndCol;
int len = (int) (bel - bbl);
SaveDeletedText(wnd, bbl, len);
wnd->CurrLine = TextLineNumber(wnd, bbl);
wnd->CurrCol = wnd->BlkBegCol;
wnd->WndRow = wnd->BlkBegLine - wnd->wtop;
if (wnd->WndRow < 0) {
wnd->WndRow = 0;
wnd->wtop = wnd->BlkBegLine;
}
/* ------ change all text lines in block to \n ----- */
while (bbl < bel) {
char *cp = strchr(bbl, '\n');
if (cp > bel)
cp = bel;
strcpy(bbl, cp);
bel -= (int) (cp - bbl);
bbl++;
}
ClearTextBlock(wnd);
BuildTextPointers(wnd);
SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
wnd->TextChanged = TRUE;
}
}
/* ----------- ID_UNDO Command ---------- */
static void UndoCmd(WINDOW wnd)
{
if (wnd->DeletedText != NULL) {
PasteText(wnd, wnd->DeletedText, wnd->DeletedLength);
free(wnd->DeletedText);
wnd->DeletedText = NULL;
wnd->DeletedLength = 0;
SendMessage(wnd, PAINT, 0, 0);
}
}
/* ----------- ID_PARAGRAPH Command ---------- */
static void ParagraphCmd(WINDOW wnd)
{
int bc, fl;
char *bl, *bbl, *bel, *bb;
ClearTextBlock(wnd);
/* ---- forming paragraph from cursor position --- */
fl = wnd->wtop + wnd->WndRow;
bbl = bel = bl = TextLine(wnd, wnd->CurrLine);
if ((bc = wnd->CurrCol) >= ClientWidth(wnd))
bc = 0;
Home(wnd);
/* ---- locate the end of the paragraph ---- */
while (*bel) {
int blank = TRUE;
char *bll = bel;
/* --- blank line marks end of paragraph --- */
while (*bel && *bel != '\n') {
if (*bel != ' ')
blank = FALSE;
bel++;
}
if (blank) {
bel = bll;
break;
}
if (*bel)
bel++;
}
if (bel == bbl) {
SendMessage(wnd, KEYBOARD, DN, 0);
return;
}
if (*bel == '\0')
--bel;
if (*bel == '\n')
--bel;
/* --- change all newlines in block to spaces --- */
while (CurrChar < bel) {
if (*CurrChar == '\n') {
*CurrChar = ' ';
wnd->CurrLine++;
wnd->CurrCol = 0;
}
else
wnd->CurrCol++;
}
/* ---- insert newlines at new margin boundaries ---- */
bb = bbl;
while (bbl < bel) {
bbl++;
if ((int)(bbl - bb) == ClientWidth(wnd)-1) {
while (*bbl != ' ' && bbl > bb)
--bbl;
if (*bbl != ' ') {
bbl = strchr(bbl, ' ');
if (bbl == NULL || bbl >= bel)
break;
}
*bbl = '\n';
bb = bbl+1;
}
}
BuildTextPointers(wnd);
/* --- put cursor back at beginning --- */
wnd->CurrLine = TextLineNumber(wnd, bl);
wnd->CurrCol = bc;
if (fl < wnd->wtop)
wnd->wtop = fl;
wnd->WndRow = fl - wnd->wtop;
SendMessage(wnd, PAINT, 0, 0);
SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
wnd->TextChanged = TRUE;
BuildTextPointers(wnd);
}
/* ----------- COMMAND Message ---------- */
static int CommandMsg(WINDOW wnd, PARAM p1)
{
switch ((int)p1) {
case ID_SEARCH:
SearchText(wnd);
return TRUE;
case ID_REPLACE:
ReplaceText(wnd);
return TRUE;
case ID_SEARCHNEXT:
SearchNext(wnd);
return TRUE;
case ID_CUT:
CopyToClipboard(wnd);
SendMessage(wnd, COMMAND, ID_DELETETEXT, 0);
SendMessage(wnd, PAINT, 0, 0);
return TRUE;
case ID_COPY:
CopyToClipboard(wnd);
ClearTextBlock(wnd);
SendMessage(wnd, PAINT, 0, 0);
return TRUE;
case ID_PASTE:
PasteFromClipboard(wnd);
SendMessage(wnd, PAINT, 0, 0);
return TRUE;
case ID_DELETETEXT:
DeleteTextCmd(wnd);
SendMessage(wnd, PAINT, 0, 0);
return TRUE;
case ID_CLEAR:
ClearCmd(wnd);
SendMessage(wnd, PAINT, 0, 0);
return TRUE;
case ID_UNDO:
UndoCmd(wnd);
SendMessage(wnd, PAINT, 0, 0);
return TRUE;
case ID_PARAGRAPH:
ParagraphCmd(wnd);
SendMessage(wnd, PAINT, 0, 0);
return TRUE;
default:
break;
}
return FALSE;
}
/* ---------- CLOSE_WINDOW Message ----------- */
static int CloseWindowMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
int rtn;
SendMessage(NULL, HIDE_CURSOR, 0, 0);
if (wnd->DeletedText != NULL)
free(wnd->DeletedText);
rtn = BaseWndProc(EDITBOX, wnd, CLOSE_WINDOW, p1, p2);
if (wnd->text != NULL) {
free(wnd->text);
wnd->text = NULL;
}
return rtn;
}
/* ------- Window processing module for EDITBOX class ------ */
int EditBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
int rtn;
switch (msg) {
case CREATE_WINDOW:
return CreateWindowMsg(wnd);
case ADDTEXT:
return AddTextMsg(wnd, p1, p2);
case SETTEXT:
return SetTextMsg(wnd, p1);
case CLEARTEXT:
return ClearTextMsg(wnd);
case GETTEXT:
return GetTextMsg(wnd, p1, p2);
case SETTEXTLENGTH:
return SetTextLengthMsg(wnd, (unsigned) p1);
case KEYBOARD_CURSOR:
KeyboardCursorMsg(wnd, p1, p2);
return TRUE;
case SETFOCUS:
if (!(int)p1)
SendMessage(NULL, HIDE_CURSOR, 0, 0);
case PAINT:
case MOVE:
rtn = BaseWndProc(EDITBOX, wnd, msg, p1, p2);
SendMessage(wnd,KEYBOARD_CURSOR,WndCol,wnd->WndRow);
return rtn;
case SIZE:
return SizeMsg(wnd, p1, p2);
case SCROLL:
return ScrollMsg(wnd, p1);
case HORIZSCROLL:
return HorizScrollMsg(wnd, p1);
case SCROLLPAGE:
return ScrollPageMsg(wnd, p1);
case HORIZPAGE:
return HorizPageMsg(wnd, p1);
case LEFT_BUTTON:
if (LeftButtonMsg(wnd, p1, p2))
return TRUE;
break;
case MOUSE_MOVED:
if (MouseMovedMsg(wnd, p1, p2))
return TRUE;
break;
case BUTTON_RELEASED:
if (ButtonReleasedMsg(wnd))
return TRUE;
break;
case KEYBOARD:
if (KeyboardMsg(wnd, p1, p2))
return TRUE;
break;
case SHIFT_CHANGED:
ShiftChangedMsg(wnd, p1);
break;
case COMMAND:
if (CommandMsg(wnd, p1))
return TRUE;
break;
case CLOSE_WINDOW:
return CloseWindowMsg(wnd, p1, p2);
default:
break;
}
return BaseWndProc(EDITBOX, wnd, msg, p1, p2);
}
/* ------ save deleted text for the Undo command ------ */
static void SaveDeletedText(WINDOW wnd, char *bbl, int len)
{
wnd->DeletedLength = len;
wnd->DeletedText=DFrealloc(wnd->DeletedText,len);
memmove(wnd->DeletedText, bbl, len);
}
/* ---- cursor right key: right one character position ---- */
static void Forward(WINDOW wnd)
{
char *cc = CurrChar+1;
if (*cc == '\0')
return;
if (*CurrChar == '\n') {
Home(wnd);
Downward(wnd);
}
else {
wnd->CurrCol++;
if (WndCol == ClientWidth(wnd))
SendMessage(wnd, HORIZSCROLL, TRUE, 0);
}
}
/* ----- stick the moving cursor to the end of the line ---- */
static void StickEnd(WINDOW wnd)
{
char *cp = TextLine(wnd, wnd->CurrLine);