Newer
Older
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
/*
* Edit control
*
* Copyright David W. Metcalfe, 1994
*
* Release 1, April 1994
*/
static char Copyright[] = "Copyright David W. Metcalfe, 1994";
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "win.h"
#include "class.h"
#include "user.h"
/*
#define DEBUG_EDIT
*/
#define NOTIFY_PARENT(hWndCntrl, wNotifyCode) \
SendMessage(GetParent(hWndCntrl), WM_COMMAND, \
GetDlgCtrlID(hWndCntrl), MAKELPARAM(hWndCntrl, wNotifyCode));
#define MAXTEXTLEN 32000 /* maximum text buffer length */
#define EDITLEN 1024 /* starting length for multi-line control */
#define ENTRYLEN 256 /* starting length for single line control */
#define GROWLENGTH 64 /* buffers grow by this much */
#define HSCROLLDIM (ClientWidth(wndPtr) / 3)
/* "line" dimension for horizontal scroll */
#define EDIT_HEAP_ALLOC(size) USER_HEAP_ALLOC(GMEM_MOVEABLE,size)
#define EDIT_HEAP_REALLOC(handle,size) USER_HEAP_REALLOC(handle,size,\
GMEM_MOVEABLE)
#define EDIT_HEAP_ADDR(handle) USER_HEAP_ADDR(handle)
#define EDIT_HEAP_FREE(handle) USER_HEAP_FREE(handle)
typedef struct
{
int wlines; /* number of lines of text */
int wtop; /* top line that is displayed */
int wleft; /* left pixel that is displayed */
unsigned int textlen; /* text buffer length */
int textwidth; /* width of longest line in pixels */
RECT fmtrc; /* rectangle in which to format text */
int txtht; /* height of text line in pixels */
HANDLE hText; /* handle to text buffer */
HANDLE hCharWidths; /* widths of chars in font */
HANDLE hTextPtrs; /* list of line offsets */
HANDLE hBlankLine; /* to fill blank lines quickly */
int CurrCol; /* current column */
int CurrLine; /* current line */
int WndCol; /* current window column */
int WndRow; /* current window row */
BOOL TextChanged; /* TRUE if text has changed */
BOOL PaintBkgd; /* paint control background */
unsigned int MaxTextLen; /* maximum text buffer length */
int SelBegLine; /* beginning line of selection */
int SelBegCol; /* beginning column of selection */
int SelEndLine; /* ending line of selection */
int SelEndCol; /* ending column of selection */
} EDITSTATE;
#define ClientWidth(wndPtr) (wndPtr->rectClient.right - \
wndPtr->rectClient.left)
#define ClientHeight(wndPtr, es) ((wndPtr->rectClient.bottom - \
wndPtr->rectClient.top) / es->txtht)
#define EditBufLen(wndPtr) (wndPtr->dwStyle & ES_MULTILINE \
? EDITLEN : ENTRYLEN)
#define CurrChar (EDIT_TextLine(hwnd, es->CurrLine) + es->CurrCol)
#define SelMarked(es) (es->SelBegLine != -1 && es->SelBegCol != -1 && \
es->SelEndLine != -1 && es->SelEndCol != -1)
/* macros to access window styles */
#define IsAutoVScroll() (wndPtr->dwStyle & ES_AUTOVSCROLL)
#define IsAutoHScroll() (wndPtr->dwStyle & ES_AUTOHSCROLL)
#define IsMultiLine() (wndPtr->dwStyle & ES_MULTILINE)
#define IsVScrollBar() (wndPtr->dwStyle & WS_VSCROLL)
#define IsHScrollBar() (wndPtr->dwStyle & WS_HSCROLL)
/* internal variables */
static BOOL TextMarking; /* TRUE if text marking in progress */
static BOOL ButtonDown; /* TRUE if left mouse button down */
static int ButtonRow; /* row in text buffer when button pressed */
static int ButtonCol; /* col in text buffer when button pressed */
static BOOL Print = FALSE;
LONG EditWndProc(HWND hWnd, WORD uMsg, WORD wParam, LONG lParam);
long EDIT_CreateMsg(HWND hwnd, LONG lParam);
void EDIT_ClearTextPointers(HWND hwnd);
void EDIT_BuildTextPointers(HWND hwnd);
void EDIT_ModTextPointers(HWND hwnd, int lineno, int var);
void EDIT_PaintMsg(HWND hwnd);
HANDLE EDIT_GetTextLine(HWND hwnd, int selection);
char *EDIT_TextLine(HWND hwnd, int sel);
int EDIT_LineLength(EDITSTATE *es, char *str, int len);
void EDIT_WriteTextLine(HWND hwnd, RECT *rc, int y);
void EDIT_WriteText(HWND hwnd, char *lp, int off, int len, int row,
int col, RECT *rc, BOOL blank, BOOL reverse);
HANDLE EDIT_GetStr(EDITSTATE *es, char *lp, int off, int len, int *diff);
void EDIT_CharMsg(HWND hwnd, WORD wParam);
void EDIT_KeyTyped(HWND hwnd, short ch);
int EDIT_CharWidth(EDITSTATE *es, short ch);
void EDIT_Forward(HWND hwnd);
void EDIT_Downward(HWND hwnd);
void EDIT_Upward(HWND hwnd);
void EDIT_Backward(HWND hwnd);
void EDIT_End(HWND hwnd);
void EDIT_Home(HWND hwnd);
void EDIT_StickEnd(HWND hwnd);
void EDIT_KeyDownMsg(HWND hwnd, WORD wParam);
void EDIT_KeyHScroll(HWND hwnd, WORD opt);
void EDIT_KeyVScrollLine(HWND hwnd, WORD opt);
void EDIT_KeyVScrollPage(HWND hwnd, WORD opt);
void EDIT_KeyVScrollDoc(HWND hwnd, WORD opt);
int EDIT_ComputeVScrollPos(HWND hwnd);
int EDIT_ComputeHScrollPos(HWND hwnd);
void EDIT_DelKey(HWND hwnd);
void EDIT_VScrollMsg(HWND hwnd, WORD wParam, LONG lParam);
void EDIT_VScrollLine(HWND hwnd, WORD opt);
void EDIT_VScrollPage(HWND hwnd, WORD opt);
void EDIT_HScrollMsg(HWND hwnd, WORD wParam, LONG lParam);
void EDIT_SizeMsg(HWND hwnd, WORD wParam, LONG lParam);
void EDIT_LButtonDownMsg(HWND hwnd, WORD wParam, LONG lParam);
void EDIT_MouseMoveMsg(HWND hwnd, WORD wParam, LONG lParam);
int EDIT_PixelToChar(HWND hwnd, int row, int *pixel);
LONG EDIT_SetTextMsg(HWND hwnd, LONG lParam);
void EDIT_ClearText(HWND hwnd);
void EDIT_SetSelMsg(HWND hwnd, LONG lParam);
void EDIT_GetLineCol(HWND hwnd, int off, int *line, int *col);
void EDIT_DeleteSel(HWND hwnd);
void EDIT_ClearSel(HWND hwnd);
int EDIT_TextLineNumber(HWND hwnd, char *lp);
void EDIT_SetAnchor(HWND hwnd, int row, int col);
void EDIT_ExtendSel(HWND hwnd, int x, int y);
void EDIT_StopMarking(HWND hwnd);
LONG EDIT_GetLineMsg(HWND hwnd, WORD wParam, LONG lParam);
LONG EDIT_GetSelMsg(HWND hwnd);
LONG EDIT_LineFromCharMsg(HWND hwnd, WORD wParam);
LONG EDIT_LineIndexMsg(HWND hwnd, WORD wParam);
void swap(int *a, int *b);
LONG EditWndProc(HWND hwnd, WORD uMsg, WORD wParam, LONG lParam)
{
LONG lResult = 0L;
HDC hdc;
char *textPtr;
int len;
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
switch (uMsg) {
case EM_CANUNDO:
/* cannot process undo message */
lResult = 0L;
break;
case EM_FMTLINES:
printf("edit: cannot process EM_FMTLINES message\n");
lResult = 0L;
break;
case EM_GETFIRSTVISIBLELINE:
lResult = es->wtop;
break;
case EM_GETHANDLE:
printf("edit: cannot process EM_GETHANDLE message\n");
break;
case EM_GETLINE:
if (IsMultiLine())
lResult = EDIT_GetLineMsg(hwnd, wParam, lParam);
else
lResult = 0L;
break;
case EM_GETLINECOUNT:
if (IsMultiLine())
lResult = es->wlines;
else
lResult = 0L;
break;
case EM_GETMODIFY:
lResult = es->TextChanged;
break;
case EM_GETPASSWORDCHAR:
printf("edit: cannot process EM_GETPASSWORDCHAR message\n");
break;
case EM_GETRECT:
GetWindowRect(hwnd, (LPRECT)lParam);
break;
case EM_GETSEL:
lResult = EDIT_GetSelMsg(hwnd);
break;
case EM_GETWORDBREAKPROC:
printf("edit: cannot process EM_GETWORDBREAKPROC message\n");
break;
case EM_LIMITTEXT:
es->MaxTextLen = wParam;
break;
case EM_LINEFROMCHAR:
lResult = EDIT_LineFromCharMsg(hwnd, wParam);
break;
case EM_LINEINDEX:
if (IsMultiLine())
lResult = EDIT_LineIndexMsg(hwnd, wParam);
else
lResult = 0L;
break;
case EM_LINELENGTH:
printf("edit: cannot process EM_LINELENGTH message\n");
break;
case EM_LINESCROLL:
printf("edit: cannot process EM_LINESCROLL message\n");
break;
case EM_REPLACESEL:
printf("edit: cannot process EM_REPLACESEL message\n");
break;
case EM_SETHANDLE:
printf("edit: cannot process EM_SETHANDLE message\n");
break;
case EM_SETMODIFY:
es->TextChanged = wParam;
break;
case EM_SETPASSWORDCHAR:
printf("edit: cannot process EM_SETPASSWORDCHAR message\n");
break;
case EM_SETREADONLY:
printf("edit: cannot process EM_SETREADONLY message\n");
break;
case EM_SETRECT:
case EM_SETRECTNP:
printf("edit: cannot process EM_SETRECT(NP) message\n");
break;
case EM_SETSEL:
HideCaret(hwnd);
EDIT_SetSelMsg(hwnd, lParam);
SetCaretPos(es->WndCol, es->WndRow * es->txtht);
ShowCaret(hwnd);
break;
case EM_SETTABSTOPS:
printf("edit: cannot process EM_SETTABSTOPS message\n");
break;
case EM_SETWORDBREAKPROC:
printf("edit: cannot process EM_SETWORDBREAKPROC message\n");
break;
case WM_CHAR:
EDIT_CharMsg(hwnd, wParam);
break;
case WM_CREATE:
lResult = EDIT_CreateMsg(hwnd, lParam);
break;
case WM_DESTROY:
EDIT_HEAP_FREE(es->hTextPtrs);
EDIT_HEAP_FREE(es->hCharWidths);
EDIT_HEAP_FREE((HANDLE)(*(wndPtr->wExtra)));
break;
case WM_ENABLE:
InvalidateRect(hwnd, NULL, FALSE);
break;
case WM_GETTEXT:
textPtr = (LPSTR)EDIT_HEAP_ADDR(es->hText);
if ((int)wParam > (len = strlen(textPtr)))
{
strcpy((char *)lParam, textPtr);
lResult = (DWORD)len;
}
else
lResult = 0L;
break;
case WM_GETTEXTLENGTH:
textPtr = (LPSTR)EDIT_HEAP_ADDR(es->hText);
lResult = (DWORD)strlen(textPtr);
break;
case WM_HSCROLL:
EDIT_HScrollMsg(hwnd, wParam, lParam);
break;
case WM_KEYDOWN:
EDIT_KeyDownMsg(hwnd, wParam);
break;
case WM_KILLFOCUS:
DestroyCaret();
NOTIFY_PARENT(hwnd, EN_KILLFOCUS);
break;
case WM_LBUTTONDOWN:
HideCaret(hwnd);
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
EDIT_LButtonDownMsg(hwnd, wParam, lParam);
SetCaretPos(es->WndCol, es->WndRow * es->txtht);
ShowCaret(hwnd);
break;
case WM_LBUTTONUP:
ButtonDown = FALSE;
if (TextMarking)
EDIT_StopMarking(hwnd);
break;
case WM_MOUSEMOVE:
HideCaret(hwnd);
EDIT_MouseMoveMsg(hwnd, wParam, lParam);
SetCaretPos(es->WndCol, es->WndRow * es->txtht);
ShowCaret(hwnd);
break;
case WM_MOVE:
lResult = 0;
break;
case WM_PAINT:
EDIT_PaintMsg(hwnd);
break;
case WM_SETFOCUS:
es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
CreateCaret(hwnd, 0, 2, es->txtht);
SetCaretPos(es->WndCol, es->WndRow * es->txtht);
ShowCaret(hwnd);
NOTIFY_PARENT(hwnd, EN_SETFOCUS);
break;
case WM_SETFONT:
break;
case WM_SETTEXT:
EDIT_SetTextMsg(hwnd, lParam);
break;
case WM_SIZE:
EDIT_SizeMsg(hwnd, wParam, lParam);
lResult = 0;
break;
case WM_VSCROLL:
EDIT_VScrollMsg(hwnd, wParam, lParam);
break;
default:
lResult = DefWindowProc(hwnd, uMsg, wParam, lParam);
break;
}
GlobalUnlock(hwnd);
return lResult;
}
/*********************************************************************
* WM_CREATE message function
*/
long EDIT_CreateMsg(HWND hwnd, LONG lParam)
{
HDC hdc;
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es;
CLASS *classPtr;
short *charWidths;
TEXTMETRIC tm;
char *text;
unsigned int *textPtrs;
/* allocate space for state variable structure */
(HANDLE)(*(wndPtr->wExtra)) =
EDIT_HEAP_ALLOC(sizeof(EDITSTATE));
es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
es->hTextPtrs = EDIT_HEAP_ALLOC(sizeof(int));
textPtrs = (unsigned int *)EDIT_HEAP_ADDR(es->hTextPtrs);
es->hCharWidths = EDIT_HEAP_ALLOC(256 * sizeof(short));
/* initialize state variable structure */
/* --- char width array */
hdc = GetDC(hwnd);
charWidths = (short *)EDIT_HEAP_ADDR(es->hCharWidths);
memset(charWidths, 0, 256 * sizeof(short));
GetCharWidth(hdc, 0, 255, charWidths);
/* --- text buffer */
es->MaxTextLen = MAXTEXTLEN + 1;
if (!(wndPtr->hText))
{
es->textlen = EditBufLen(wndPtr);
es->hText = EDIT_HEAP_ALLOC(EditBufLen(wndPtr) + 2);
text = (LPSTR)EDIT_HEAP_ADDR(es->hText);
memset(text, 0, es->textlen + 2);
EDIT_ClearTextPointers(hwnd);
}
else
{
es->hText = wndPtr->hText;
wndPtr->hText = 0;
es->textlen = GetWindowTextLength(hwnd) + 1;
EDIT_BuildTextPointers(hwnd);
}
/* --- other structure variables */
GetTextMetrics(hdc, &tm);
es->txtht = tm.tmHeight + tm.tmExternalLeading;
es->wlines = 0;
es->wtop = es->wleft = 0;
es->CurrCol = es->CurrLine = 0;
es->WndCol = es->WndRow = 0;
es->TextChanged = FALSE;
es->textwidth = 0;
es->SelBegLine = es->SelBegCol = -1;
es->SelEndLine = es->SelEndCol = -1;
/* allocate space for a line full of blanks to speed up */
/* line filling */
es->hBlankLine = EDIT_HEAP_ALLOC((ClientWidth(wndPtr) /
charWidths[32]) + 2);
text = EDIT_HEAP_ADDR(es->hBlankLine);
memset(text, ' ', (ClientWidth(wndPtr) / charWidths[32]) + 2);
/* set up text cursor for edit class */
CLASS_FindClassByName("EDIT", &classPtr);
classPtr->wc.hCursor = LoadCursor(0, IDC_IBEAM);
/* paint background on first WM_PAINT */
es->PaintBkgd = TRUE;
ReleaseDC(hwnd, hdc);
return 0L;
}
/*********************************************************************
* EDIT_ClearTextPointers
*
* Clear and initialize text line pointer array.
*/
void EDIT_ClearTextPointers(HWND hwnd)
{
unsigned int *textPtrs;
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
es->hTextPtrs = EDIT_HEAP_REALLOC(es->hTextPtrs, sizeof(int));
textPtrs = (unsigned int *)EDIT_HEAP_ADDR(es->hTextPtrs);
*textPtrs = 0;
}
/*********************************************************************
* EDIT_BuildTextPointers
*
* Build array of pointers to text lines.
*/
#define INITLINES 100
void EDIT_BuildTextPointers(HWND hwnd)
{
WND *wndPtr = WIN_FindWndPtr(hwnd);
char *text, *cp;
int incrs = INITLINES;
unsigned int off, len, temp;
EDITSTATE *es;
unsigned int *textPtrs;
short *charWidths;
es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
text = (char *)EDIT_HEAP_ADDR(es->hText);
textPtrs = (unsigned int *)EDIT_HEAP_ADDR(es->hTextPtrs);
charWidths = (short *)EDIT_HEAP_ADDR(es->hCharWidths);
es->textwidth = es->wlines = 0;
cp = text;
/* advance through text buffer */
while (*cp)
{
/* increase size of text pointer array */
if (incrs == INITLINES)
{
incrs = 0;
es->hTextPtrs = EDIT_HEAP_REALLOC(es->hTextPtrs,
(es->wlines + INITLINES) * sizeof(int));
textPtrs = (unsigned int *)EDIT_HEAP_ADDR(es->hTextPtrs);
}
off = (unsigned int)(cp - text); /* offset of beginning of line */
*(textPtrs + es->wlines) = off;
es->wlines++;
incrs++;
len = 0;
/* advance through current line */
while (*cp && *cp != '\n')
{
len += charWidths[*cp]; /* width of line in pixels */
cp++;
}
es->textwidth = max(es->textwidth, len);
if (*cp)
cp++; /* skip '\n' */
}
off = (unsigned int)(cp - text); /* offset of beginning of line */
*(textPtrs + es->wlines) = off;
}
/*********************************************************************
* EDIT_ModTextPointers
*
* Modify text pointers from a specified position.
*/
void EDIT_ModTextPointers(HWND hwnd, int lineno, int var)
{
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
unsigned int *textPtrs = (unsigned int *)EDIT_HEAP_ADDR(es->hTextPtrs);
while (lineno < es->wlines)
*(textPtrs + lineno++) += var;
}
/*********************************************************************
* WM_PAINT message function
*/
void EDIT_PaintMsg(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc;
int y;
RECT rc;
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
hdc = BeginPaint(hwnd, &ps);
rc = ps.rcPaint;
#ifdef DEBUG_EDIT
printf("WM_PAINT: rc=(%d,%d), (%d,%d)\n", rc.left, rc.top,
rc.right, rc.bottom);
#endif
if (es->PaintBkgd)
FillWindow(GetParent(hwnd), hwnd, hdc, CTLCOLOR_EDIT);
for (y = (rc.top / es->txtht); y <= (rc.bottom / es->txtht); y++)
{
if (y < es->wlines - es->wtop)
EDIT_WriteTextLine(hwnd, &rc, y + es->wtop);
}
EndPaint(hwnd, &ps);
}
/*********************************************************************
* EDIT_GetTextLine
*
* Get a copy of the text in the specified line.
*/
HANDLE EDIT_GetTextLine(HWND hwnd, int selection)
{
char *line;
HANDLE hLine;
int len = 0;
char *cp, *cp1;
#ifdef DEBUG_EDIT
printf("GetTextLine %d\n", selection);
#endif
cp = cp1 = EDIT_TextLine(hwnd, selection);
/* advance through line */
while (*cp && *cp != '\n')
{
len++;
cp++;
}
/* store selected line and return handle */
hLine = EDIT_HEAP_ALLOC(len + 6);
line = (char *)EDIT_HEAP_ADDR(hLine);
memmove(line, cp1, len);
line[len] = '\0';
return hLine;
}
/*********************************************************************
* EDIT_TextLine
*
* Return a pointer to the text in the specified line.
*/
char *EDIT_TextLine(HWND hwnd, int sel)
{
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
char *text = (char *)EDIT_HEAP_ADDR(es->hText);
unsigned int *textPtrs = (unsigned int *)EDIT_HEAP_ADDR(es->hTextPtrs);
return (text + *(textPtrs + sel));
}
/*********************************************************************
* EDIT_LineLength
*
* Return length of line _str_ of length _len_ characters in pixels.
*/
int EDIT_LineLength(EDITSTATE *es, char *str, int len)
{
int i, plen = 0;
short *charWidths = (short *)EDIT_HEAP_ADDR(es->hCharWidths);
for (i = 0; i < len; i++)
plen += charWidths[*(str + i)];
#ifdef DEBUG_EDIT
printf("EDIT_LineLength: returning %d\n", plen);
#endif
return plen;
}
/*********************************************************************
* EDIT_WriteTextLine
*
* Write the line of text at offset _y_ in text buffer to a window.
*/
void EDIT_WriteTextLine(HWND hwnd, RECT *rect, int y)
{
int len = 0;
unsigned char line[200];
HANDLE hLine;
unsigned char *lp;
int lnlen, lnlen1;
int col, off = 0;
int sbl, sel, sbc, sec;
RECT rc;
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
BOOL trunc = FALSE;
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
/* initialize rectangle if NULL, else copy */
if (rect)
CopyRect(&rc, rect);
else
GetClientRect(hwnd, &rc);
#ifdef DEBUG_EDIT
printf("WriteTextLine %d\n", y);
#endif
/* make sure y is inside the window */
if (y < es->wtop || y > (es->wtop + ClientHeight(wndPtr, es)))
{
#ifdef DEBUG_EDIT
printf("EDIT_WriteTextLine: y (%d) is not a displayed line\n", y);
#endif
return;
}
/* make sure rectangle is within window */
if (rc.left >= ClientWidth(wndPtr) - 1)
{
#ifdef DEBUG_EDIT
printf("EDIT_WriteTextLine: rc.left (%d) is greater than right edge\n",
rc.left);
#endif
return;
}
if (rc.right <= 0)
{
#ifdef DEBUG_EDIT
printf("EDIT_WriteTextLine: rc.right (%d) is less than left edge\n",
rc.right);
#endif
return;
}
if (y - es->wtop < (rc.top / es->txtht) ||
y - es->wtop > (rc.bottom / es->txtht))
{
#ifdef DEBUG_EDIT
printf("EDIT_WriteTextLine: y (%d) is outside window\n", y);
#endif
return;
}
/* get the text and length of line */
if ((hLine = EDIT_GetTextLine(hwnd, y)) == 0)
return;
lp = (unsigned char *)EDIT_HEAP_ADDR(hLine);
lnlen = EDIT_LineLength(es, lp, strlen(lp));
lnlen1 = lnlen;
/* build the line to display */
if (lnlen < es->wleft)
lnlen = 0;
else
off += es->wleft;
if (lnlen > rc.left)
{
off += rc.left;
lnlen = lnlen1 - off;
len = min(lnlen, rc.right - rc.left);
}
if (SelMarked(es))
{
sbl = es->SelBegLine;
sel = es->SelEndLine;
sbc = es->SelBegCol;
sec = es->SelEndCol;
/* put lowest marker first */
if (sbl > sel)
{
swap(&sbl, &sel);
swap(&sbc, &sec);
}
if (sbl == sel && sbc > sec)
swap(&sbc, &sec);
if (y < sbl || y > sel)
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop, rc.left, &rc,
TRUE, FALSE);
else if (y > sbl && y < sel)
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop, rc.left, &rc,
TRUE, TRUE);
else if (y == sbl)
{
col = EDIT_LineLength(es, lp, sbc);
if (col > (es->wleft + rc.left))
{
len = min(col - off, rc.right - off);
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop,
rc.left, &rc, FALSE, FALSE);
off = col;
}
if (y == sel)
{
col = EDIT_LineLength(es, lp, sec);
if (col < (es->wleft + rc.right))
{
len = min(col - off, rc.right - off);
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop,
off - es->wleft, &rc, FALSE, TRUE);
off = col;
len = min(lnlen - off, rc.right - off);
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop,
off - es->wleft, &rc, TRUE, FALSE);
}
else
{
len = min(lnlen - off, rc.right - off);
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop,
off - es->wleft, &rc, TRUE, TRUE);
}
}
else
{
len = min(lnlen - off, rc.right - off);
if (col < (es->wleft + rc.right))
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop,
off - es->wleft, &rc, TRUE, TRUE);
}
}
else if (y == sel)
{
col = EDIT_LineLength(es, lp, sec);
if (col < (es->wleft + rc.right))
{
len = min(col - off, rc.right - off);
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop,
off - es->wleft, &rc, FALSE, TRUE);
off = col;
len = min(lnlen - off, rc.right - off);
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop,
off - es->wleft, &rc, TRUE, FALSE);
}
}
}
else
EDIT_WriteText(hwnd, lp, off, len, y - es->wtop, rc.left, &rc,
TRUE, FALSE);
EDIT_HEAP_FREE(hLine);
}
/*********************************************************************
* EDIT_WriteText
*
* Write text to a window
* lp - text line
* off - offset in text line (in pixels)
* len - length from off (in pixels)
* row - line in window
* col - column in window
* rc - rectangle in which to display line
* blank - blank remainder of line?
* reverse - reverse color of line?
*/
void EDIT_WriteText(HWND hwnd, char *lp, int off, int len, int row,
int col, RECT *rc, BOOL blank, BOOL reverse)
{
HDC hdc;
HANDLE hStr;
char *str, *blanks;
int diff, num_spaces;
HRGN hrgnClip;
COLORREF oldTextColor, oldBkgdColor;
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
short *charWidths = (short *)EDIT_HEAP_ADDR(es->hCharWidths);
#ifdef DEBUG_EDIT
printf("EDIT_WriteText lp=%s, off=%d, len=%d, row=%d, col=%d, reverse=%d\n", lp, off, len, row, col, reverse);
#endif
hdc = GetDC(hwnd);
hStr = EDIT_GetStr(es, lp, off, len, &diff);
str = (char *)EDIT_HEAP_ADDR(hStr);
hrgnClip = CreateRectRgnIndirect(rc);
SelectClipRgn(hdc, hrgnClip);
SendMessage(GetParent(hwnd), WM_CTLCOLOR, (WORD)hdc,
MAKELPARAM(hwnd, CTLCOLOR_EDIT));
if (reverse)
{
oldBkgdColor = GetBkColor(hdc);
oldTextColor = GetTextColor(hdc);
SetBkColor(hdc, oldTextColor);
SetTextColor(hdc, oldBkgdColor);
}
TextOut(hdc, col - diff, row * es->txtht, str, strlen(str));
if (reverse)
{
SetBkColor(hdc, oldBkgdColor);
SetTextColor(hdc, oldTextColor);
}
/* blank out remainder of line if appropriate */
if (blank)
{
if ((rc->right - col) > len)
{
blanks = EDIT_HEAP_ADDR(es->hBlankLine);
num_spaces = (rc->right - col - len) / charWidths[32];
TextOut(hdc, col + len, row * es->txtht, blanks, num_spaces);
}
}
EDIT_HEAP_FREE(hStr);
ReleaseDC(hwnd, hdc);
}
/*********************************************************************
* EDIT_GetStr
*
* Return sub-string starting at pixel _off_ of length _len_ pixels.
* If _off_ is part way through a character, the negative offset of
* the beginning of the character is returned in _diff_, else _diff_
* will be zero.
*/
HANDLE EDIT_GetStr(EDITSTATE *es, char *lp, int off, int len, int *diff)
{
HANDLE hStr;
char *str;
int ch = 0, i = 0, j, tmp;
int ch1;
short *charWidths = (short *)EDIT_HEAP_ADDR(es->hCharWidths);
#ifdef DEBUG_EDIT
printf("EDIT_GetStr %s %d %d\n", lp, off, len);
#endif
while (i < off)
{
i += charWidths[*(lp + ch)];
ch++;
}
/* if stepped past _off_, go back a character */
if (i - off)
i -= charWidths[*(lp + --ch)];
*diff = off - i;
ch1 = ch;
while (i < len + off)
{
i += charWidths[*(lp + ch)];
ch++;
}
hStr = EDIT_HEAP_ALLOC(ch - ch1 + 3);
str = (char *)EDIT_HEAP_ADDR(hStr);
for (i = ch1, j = 0; i < ch; i++, j++)
str[j] = lp[i];
str[++j] = '\0';
#ifdef DEBUG_EDIT
printf("EDIT_GetStr: returning %s\n", str);
#endif
return hStr;
}
/*********************************************************************
* WM_CHAR message function
*/
void EDIT_CharMsg(HWND hwnd, WORD wParam)
{
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
#ifdef DEBUG_EDIT
printf("EDIT_CharMsg: wParam=%c\n", (char)wParam);
#endif
switch (wParam)
{
case '\r':
case '\n':
if (!IsMultiLine())
break;
wParam = '\n';
EDIT_KeyTyped(hwnd, wParam);
break;
default:
if (wParam >= 20 && wParam <= 126)
EDIT_KeyTyped(hwnd, wParam);
break;
}
}
/*********************************************************************
* EDIT_KeyTyped
*
* Process keystrokes that produce displayable characters.
*/
void EDIT_KeyTyped(HWND hwnd, short ch)
{
WND *wndPtr = WIN_FindWndPtr(hwnd);
EDITSTATE *es = (EDITSTATE *)EDIT_HEAP_ADDR((HANDLE)(*(wndPtr->wExtra)));
char *text = (char *)EDIT_HEAP_ADDR(es->hText);
char *currchar = CurrChar;
RECT rc;
BOOL FullPaint = FALSE;
#ifdef DEBUG_EDIT
printf("EDIT_KeyTyped: ch=%c\n", (char)ch);
#endif