-
Notifications
You must be signed in to change notification settings - Fork 3
/
RadEdit.cpp
1505 lines (1317 loc) · 45 KB
/
RadEdit.cpp
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
#include "stdafx.h"
#include "RadEdit.h"
#include "Text.h"
#include <windowsx.h>
#include <intsafe.h>
#include "RadEditResource.h" // For IDR_RADEDIT
// TODO
// http://winapi.freetechsecrets.com/win32/WIN32Edit_Control_Default_Message_Pro.htm
// Undo
// Check parent notifications EN_*
// Support IME
// Support word wrap
// Support middle mouse click scroll mode
// Support RTL
// ES_LEFT, ES_CENTER, ES_RIGHT
// ES_UPPERCASE, ES_LOWERCASE
// ES_AUTOVSCROLL, ES_AUTOHSCROLL not set
// ES_OEMCONVERT CharToOem
// ES_WANTRETURN
// ES_NUMBER
// NOT Implemented:
// limit text
// single line
// word wrap
// cue banner
// ES_PASSWORD
// balloon tool tip
#define USE_UNISCRIBE
#ifdef USE_UNISCRIBE
#include <usp10.h>
#define UTF8_CHARSET 254
#endif
namespace
{
inline LONG GetWidth(const RECT& r)
{
return r.right - r.left;
}
inline LONG GetHeight(const RECT& r)
{
return r.bottom - r.top;
}
inline SIZE ToSize(DWORD s)
{
return { LOWORD(s), HIWORD(s) };
}
inline POINT ToPoint(LPARAM p)
{
return { GET_X_LPARAM(p), GET_Y_LPARAM(p) };
}
inline LRESULT ToLResult(POINT p)
{
return MAKELPARAM(p.x, p.y);
}
BOOL HasStyle(HWND hWnd, int HasStyle)
{
int Style = GetWindowStyle(hWnd);
return (Style & HasStyle) == HasStyle;
}
int MyGetScrollPos(HWND hWnd, int nBar)
{
SCROLLINFO info = { sizeof(SCROLLINFO), SIF_POS };
ENSURE(GetScrollInfo(hWnd, nBar, &info));
return info.nPos;
}
UINT MyGetScrollPage(HWND hWnd, int nBar)
{
SCROLLINFO info = { sizeof(SCROLLINFO), SIF_PAGE };
ENSURE(GetScrollInfo(hWnd, nBar, &info));
return info.nPage;
}
#ifndef USE_UNISCRIBE
int GetTabbedTextExtentEx(_In_ HDC hDC,
_In_reads_(chCount) LPCWSTR lpString,
_In_ int chCount,
_In_ int nTabPositions,
_In_reads_opt_(nTabPositions) INT *lpnTabStopPositions,
int x)
{
#if 0
for (int i = 0; i < chCount; ++i)
{
SIZE s = ToSize(GetTabbedTextExtent(hDC, lpString, i, nTabPositions, lpnTabStopPositions));
if (s.cx >= x)
return i;
}
return chCount;
#else
int l = 0;
int r = chCount - 1;
while (l < r)
{
int m = (l + r) / 2; // TODO This can't be in the middle of a sequence
SIZE s = ToSize(GetTabbedTextExtent(hDC, lpString, m, nTabPositions, lpnTabStopPositions));
if (s.cx < x)
l = GetNextChar(lpString, m);
else // if (s.cx >= x)
r = GetPrevChar(lpString, m);
}
return l;
#endif
}
#endif
void NotifyParent(HWND hWnd, int code)
{
//SendMessage(GetParent(hWnd), WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(hWnd), code), (LPARAM) hWnd);
FORWARD_WM_COMMAND(GetParent(hWnd), GetWindowID(hWnd), hWnd, code, SNDMSG);
}
}
class RadEdit;
LRESULT CALLBACK RadEditWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
ATOM RegisterRadEdit(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = RadEditWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(RadEdit*);
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = WC_RADEDIT;
return RegisterClass(&wc);
}
class RadEdit
{
private:
HFONT m_hFont = NULL;
TEXTMETRIC m_TextMetrics = {};
HLOCAL m_hText = NULL;
BOOL m_bModify = FALSE;
int m_nMarginLeft = EC_USEFONTINFO;
int m_nMarginRight = EC_USEFONTINFO;
int m_nTabStops = 0;
INT m_rgTabStops[256];
DWORD m_nSelStart = 0;
DWORD m_nSelEnd = 0;
BOOL m_bSelectWord = FALSE;
EDITWORDBREAKPROC m_pEditWordBreakProc = nullptr;
int LineHeight() const { return m_TextMetrics.tmHeight; }
int AveCharWidth() const { return m_TextMetrics.tmAveCharWidth; }
int MarginLeft() const { return m_nMarginLeft == EC_USEFONTINFO ? m_TextMetrics.tmOverhang : m_nMarginLeft; }
int MarginRight() const { return m_nMarginRight == EC_USEFONTINFO ? m_TextMetrics.tmOverhang : m_nMarginRight; }
void CalcScrollBars(HWND hWnd) const;
SIZE CalcTextSize(HDC hDC, PCWSTR buffer, int nCount) const;
void MoveCaret(HWND hWnd) const;
void Draw(HWND hWnd, HDC hDC, LPCRECT pr) const;
void ReplaceSel(HWND hWnd, PCWSTR pText, BOOL bStoreUndo);
DWORD GetFirstVisibleLine(HWND hWnd) const;
DWORD GetFirstVisibleCol(HWND hWnd) const;
#ifdef USE_UNISCRIBE
HRESULT ScriptStringAnalyse(HDC hDC, PCWSTR buffer, int nCount, _Outptr_result_buffer_(1) SCRIPT_STRING_ANALYSIS* pssa) const
{
const int glyhs = 2 * nCount + 16;
SCRIPT_TABDEF tabdef = {};
tabdef.cTabStops = m_nTabStops;
tabdef.iScale = 4;
tabdef.pTabStops = const_cast<LPINT>(m_rgTabStops);
tabdef.iTabOrigin = 0;
return ::ScriptStringAnalyse(hDC, buffer, nCount, glyhs, -1, SSA_FALLBACK | SSA_GLYPHS | SSA_LINK | SSA_TAB, 0, nullptr, nullptr, nullptr, &tabdef, nullptr, pssa);
}
#endif
public:
DWORD GetCursor() const { return m_nSelEnd; }
BOOL OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct);
void OnDestroy(HWND hWnd);
BOOL OnEraseBkgnd(HWND hWnd, HDC hDC);
void OnPaint(HWND hWnd);
void OnSize(HWND hWnd, UINT state, int cx, int cy);
void OnScroll(HWND hWnd, UINT nSBCode, HWND hScrollBar, UINT nBar) const;
void OnVScroll(HWND hWnd, HWND hWndCtl, UINT code, int /*pos*/) { OnScroll(hWnd, code, hWndCtl, SB_VERT); NotifyParent(hWnd, EN_VSCROLL); }
void OnHScroll(HWND hWnd, HWND hWndCtl, UINT code, int /*pos*/) { OnScroll(hWnd, code, hWndCtl, SB_HORZ); NotifyParent(hWnd, EN_HSCROLL); }
void OnSetFocus(HWND hWnd, HWND hWndOldFocus);
void OnKillFocus(HWND hWnd, HWND hWndNewFocus);
void OnSetFont(HWND hWnd, HFONT hFont, BOOL bRedraw);
HFONT OnGetFont(HWND hWnd) const;
void OnKey(HWND hWnd, UINT vk, BOOL fDown, int cRepeat, UINT flags);
void OnChar(HWND hWnd, TCHAR ch, int cRepeat);
void OnLButtonDown(HWND hWnd, BOOL fDoubleClick, int x, int y, UINT keyFlags);
void OnLButtonUp(HWND hWnd, int x, int y, UINT keyFlags);
void OnMouseMove(HWND hWnd, int x, int y, UINT keyFlags);
void OnMouseWheel(HWND hWnd, int xPos, int yPos, int zDelta, UINT fwKeys);
void OnContextMenu(HWND hwnd, HWND hwndContext, UINT xPos, UINT yPos);
INT OnGetTextLength(HWND hWnd);
INT OnGetText(HWND hWnd, int cchTextMax, LPTSTR lpszText);
BOOL OnSetText(HWND hWnd, LPCTSTR lpszText);
void OnCut(HWND hWnd);
void OnCopy(HWND hWnd);
void OnPaste(HWND hWnd);
void OnClear(HWND hWnd);
void OnSetRedraw(HWND hwnd, BOOL fRedraw);
UINT OnGetDlgCode(HWND hWnd, LPMSG pMsg);
LRESULT OnGetSel(HWND hWnd, LPDWORD pSelStart, LPDWORD pSelEnd) const;
void OnSetSel(HWND hWnd, DWORD nSelStart, DWORD nSelEnd);
LRESULT OnScroll(HWND hWnd, UINT nSBCode);
LRESULT OnLineScroll(HWND hWnd, int nHScroll, int nVScroll);
void OnScrollCaret(HWND hWnd);
BOOL OnGetModify(HWND hWnd) const;
void OnSetModify(HWND hWnd, BOOL bModify);
DWORD OnGetLineCount(HWND hWnd) const;
DWORD OnLineIndex(HWND hWnd, int nLine) const;
void OnSetHandle(HWND hWnd, HLOCAL hText);
HLOCAL OnGetHandle(HWND hWnd) const;
DWORD OnGetThumb(HWND hWnd) const;
DWORD OnLineLength(HWND hWnd, int nIndex) const;
void OnReplaceSel(HWND hWnd, PCWSTR pText, BOOL bStoreUndo);
int OnGetLine(HWND hWnd, int nLine, PTCHAR copy, DWORD nBufSize) const;
DWORD OnLineFromChar(HWND hWnd, int nCharIndex) const;
BOOL OnSetTabStops(HWND hWnd, LPINT rgTabStops, int nTabStops);
DWORD OnGetFirstVisibleLine(HWND hWnd) const;
BOOL OnSetReadOnly(HWND hWnd, BOOL bReadOnly);
EDITWORDBREAKPROC OnGetWordBreakProc(HWND hWnd) const;
void OnSetWordBreakProc(HWND hWnd, EDITWORDBREAKPROC pEWP);
void OnSetMargins(HWND hWnd, UINT nFlags, UINT nLeftMargin, UINT nRightMargin);
LRESULT OnGetMargins(HWND hWnd) const;
POINT OnPosFromChar(HWND hWnd, DWORD nChar) const;
LRESULT OnCharFromPos(HWND hWnd, POINT pos) const;
LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
};
void RadEdit::CalcScrollBars(HWND hWnd) const
{
SIZE s = {};
PCWSTR const buffer = (PCWSTR) LocalLock(m_hText);
int nIndex = 0;
while (nIndex >= 0)
{
int nNext = GetNextLine(buffer, nIndex);
if (nNext >= 0)
{
int x = nNext - nIndex - 1;
if (x > s.cx)
s.cx = x;
nIndex = nNext;
}
else
{
int x = (int) wcslen(buffer + nIndex);
if (x > s.cx)
s.cx = x;
nIndex = -1;
}
++s.cy;
}
LocalUnlock(m_hText);
RECT cr = {};
GetClientRect(hWnd, &cr);
cr.left += MarginLeft();
cr.right -= MarginRight();
SCROLLINFO vinfo = { sizeof(SCROLLINFO), SIF_ALL }, hinfo = { sizeof(SCROLLINFO), SIF_ALL };
ENSURE(GetScrollInfo(hWnd, SB_VERT, &vinfo));
ENSURE(GetScrollInfo(hWnd, SB_HORZ, &hinfo));
vinfo.fMask |= SIF_DISABLENOSCROLL;
hinfo.fMask |= SIF_DISABLENOSCROLL;
vinfo.nMax = s.cy - 1;
vinfo.nPage = GetHeight(cr) / LineHeight() - 1;
hinfo.nMax = s.cx;
hinfo.nPage = GetWidth(cr) / AveCharWidth() - 1;
SetScrollInfo(hWnd, SB_VERT, &vinfo, TRUE);
SetScrollInfo(hWnd, SB_HORZ, &hinfo, TRUE);
}
SIZE RadEdit::CalcTextSize(HDC hDC, PCWSTR buffer, int nCount) const
{
#ifdef USE_UNISCRIBE
HRESULT hr;
SCRIPT_STRING_ANALYSIS ssa = {};
hr = ScriptStringAnalyse(hDC, buffer, nCount, &ssa);
if (SUCCEEDED(hr))
{
const SIZE Size = *ScriptString_pSize(ssa);
hr = ScriptStringFree(&ssa);
return Size;
}
else
return { 0, 0 };
#else
return ToSize(GetTabbedTextExtent(hDC, buffer, nCount, m_nTabStops, const_cast<LPINT>(m_rgTabStops)));
#endif
}
void RadEdit::MoveCaret(HWND hWnd) const
{
POINT p = EditPosFromChar(hWnd, m_nSelEnd);
SetCaretPos(p.x, p.y);
}
void RadEdit::Draw(HWND hWnd, HDC hDC, LPCRECT pr) const
{
HFONT of = SelectFont(hDC, m_hFont);
BOOL bShowSel = GetFocus() == hWnd || HasStyle(hWnd, ES_NOHIDESEL);
RECT rClip;
GetClientRect(hWnd, &rClip);
rClip.left += MarginLeft();
rClip.right -= MarginRight();
IntersectClipRect(hDC, rClip.left, rClip.top, rClip.right, rClip.bottom);
DWORD nSelStart, nSelEnd;
OnGetSel(hWnd, &nSelStart, &nSelEnd);
POINT p = { -(int) GetFirstVisibleCol(hWnd) * AveCharWidth() + MarginLeft(), 0 };
LPCWSTR const buffer = (LPCWSTR) LocalLock(m_hText);
const int nFirstLine = GetFirstVisibleLine(hWnd);
const int nStartLine = pr->top / LineHeight();
p.y += nStartLine * LineHeight();
int nIndex = TextLineIndex(m_hText, nFirstLine + nStartLine);
while (nIndex >= 0)
{
const int nLineEnd = GetLineEnd(buffer, nIndex);
const int nCount = nLineEnd - nIndex;
#ifdef USE_UNISCRIBE
// TODO Create the clipping rect
HRESULT hr;
SCRIPT_STRING_ANALYSIS ssa = {};
hr = ScriptStringAnalyse(hDC, buffer + nIndex, nCount, &ssa);
if (SUCCEEDED(hr))
{
const DWORD nSelLineStart = bShowSel ? min(max((int) nSelStart, nIndex), nIndex + nCount) - nIndex : 0;
const DWORD nSelLineEnd = bShowSel ? min(max((int) nSelEnd, nIndex), nIndex + nCount) - nIndex : 0;
hr = ScriptStringOut(ssa, p.x, p.y, ETO_OPAQUE, nullptr, nSelLineStart, nSelLineEnd, FALSE);
//const SIZE* pSize = ScriptString_pSize(ssa);
hr = ScriptStringFree(&ssa);
}
#else
TabbedTextOutW(hDC, p.x, p.y, buffer + nIndex, nCount, m_nTabStops, m_rgTabStops, MarginLeft());
if (bShowSel)
{
const DWORD nSelLineStart = min(max((int) nSelStart, nIndex), nIndex + nCount);
const DWORD nSelLineEnd = min(max((int) nSelEnd, nIndex), nIndex + nCount);
if (nSelLineStart != nSelLineEnd)
{
int save = SaveDC(hDC);
SIZE sBegin(CalcTextSize(hDC, buffer + nIndex, nSelLineStart - nIndex));
SIZE sEnd(CalcTextSize(hDC, buffer + nIndex, nSelLineEnd - nIndex));
IntersectClipRect(hDC, p.x + sBegin.cx, p.y, p.x + sEnd.cx, p.y + LineHeight());
SetBkColor(hDC, GetSysColor(COLOR_HIGHLIGHT));
SetTextColor(hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
TabbedTextOutW(hDC, p.x, p.y, buffer + nIndex, nCount, m_nTabStops, m_rgTabStops, MarginLeft());
RestoreDC(hDC, save);
}
}
#endif
nIndex = GetNextLine(buffer, nLineEnd);
p.y += LineHeight();
if ((p.y + m_TextMetrics.tmHeight) > pr->bottom)
break;
}
LocalUnlock(m_hText);
SelectFont(hDC, of);
}
void RadEdit::ReplaceSel(HWND hWnd, PCWSTR pText, BOOL /*bStoreUndo*/)
{
// TODO bStoreUndo
if (!HasStyle(hWnd, ES_READONLY))
{
DWORD nSelStart, nSelEnd;
OnGetSel(hWnd, &nSelStart, &nSelEnd);
if (nSelStart != nSelEnd || !IsEmpty(pText))
{
if (HLOCAL hTextNew = TextReplace(m_hText, nSelStart, nSelEnd, pText))
{
m_hText = hTextNew;
m_bModify = TRUE;
OnSetSel(hWnd, nSelEnd, nSelEnd);
InvalidateRect(hWnd, nullptr, TRUE);
NotifyParent(hWnd, EN_CHANGE);
}
else
NotifyParent(hWnd, EN_ERRSPACE);
}
}
}
DWORD RadEdit::GetFirstVisibleLine(HWND hWnd) const
{
return MyGetScrollPos(hWnd, SB_VERT);
}
DWORD RadEdit::GetFirstVisibleCol(HWND hWnd) const
{
return MyGetScrollPos(hWnd, SB_HORZ);
}
UINT DoScroll(UINT nSBCode, const SCROLLINFO& info)
{
int curpos = info.nPos;
// Determine the new position of scroll box.
switch (nSBCode)
{
case SB_LEFT: // Scroll to far left.
curpos = info.nMin;
break;
case SB_RIGHT: // Scroll to far right.
curpos = info.nMax;
break;
case SB_ENDSCROLL: // End scroll.
break;
case SB_LINELEFT: // Scroll left.
if (curpos > info.nMin)
curpos--;
break;
case SB_LINERIGHT: // Scroll right.
if (curpos < info.nMax)
curpos++;
break;
case SB_PAGELEFT: // Scroll one page left.
if (curpos > info.nMin)
//curpos = max(info.nMin, curpos - MulDiv(info.nPage, 9, 10));
curpos = max(info.nMin, curpos - (int) info.nPage);
break;
case SB_PAGERIGHT: // Scroll one page right.
if (curpos < info.nMax)
//curpos = min(info.nMax, curpos + MulDiv(info.nPage, 9, 10));
curpos = min(info.nMax, curpos + (int) info.nPage);
break;
case SB_THUMBPOSITION: // Scroll to absolute position. nTrackPos is the position
curpos = info.nTrackPos; // of the scroll box at the end of the drag operation.
break;
case SB_THUMBTRACK: // Drag scroll box to specified position. nTrackPos is the
curpos = info.nTrackPos; // position that the scroll box has been dragged to.
break;
}
return curpos;
}
BOOL RadEdit::OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
lpCreateStruct;
ASSERT(lpCreateStruct->style & WS_VSCROLL);
ASSERT(lpCreateStruct->style & WS_HSCROLL);
ASSERT(lpCreateStruct->style & ES_AUTOVSCROLL);
ASSERT(lpCreateStruct->style & ES_AUTOHSCROLL);
ASSERT(lpCreateStruct->style & ES_MULTILINE);
m_hText = LocalAlloc(LHND, sizeof(WCHAR));
OnSetFont(hWnd, NULL, FALSE);
return TRUE;
}
void RadEdit::OnDestroy(HWND /*hWnd*/)
{
LocalFree(m_hText);
}
BOOL RadEdit::OnEraseBkgnd(HWND /*hWnd*/, HDC /*hDC*/)
{
return FALSE;
}
void RadEdit::OnPaint(HWND hWnd)
{
PAINTSTRUCT ps = {};
HDC hDC = BeginPaint(hWnd, &ps);
RECT cr = {};
GetClientRect(hWnd, &cr);
HDC hBmpDC = CreateCompatibleDC(hDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDC, GetWidth(cr), GetHeight(cr));
HBITMAP hOldBmp = SelectBitmap(hBmpDC, hBmp);
HBRUSH hBgBrush = HasStyle(hWnd, ES_READONLY)
? FORWARD_WM_CTLCOLORSTATIC(GetParent(hWnd), hDC, hWnd, SNDMSG)
: FORWARD_WM_CTLCOLOREDIT(GetParent(hWnd), hDC, hWnd, SNDMSG);
DeleteBrush(hBgBrush); // TODO Where to use brush
DefWindowProc(hWnd, WM_ERASEBKGND, (WPARAM) hBmpDC, 0);
Draw(hWnd, hBmpDC, &ps.rcPaint);
BitBlt(hDC, 0, 0, GetWidth(cr), GetHeight(cr), hBmpDC, 0, 0, SRCCOPY);
SelectBitmap(hBmpDC, hOldBmp);
DeleteBitmap(hBmp);
DeleteDC(hBmpDC);
EndPaint(hWnd, &ps);
}
void RadEdit::OnSize(HWND hWnd, UINT /*state*/, int /*cx*/, int /*cy*/)
{
CalcScrollBars(hWnd);
}
void RadEdit::OnScroll(HWND hWnd, UINT nSBCode, HWND hScrollBar, UINT nBar) const
{
if (hScrollBar == NULL)
hScrollBar = hWnd;
else
nBar = SB_CTL;
SCROLLINFO info = { sizeof(SCROLLINFO), SIF_ALL };
ENSURE(GetScrollInfo(hScrollBar, nBar, &info));
info.nPos = DoScroll(nSBCode, info);
SetScrollInfo(hScrollBar, nBar, &info, TRUE);
MoveCaret(hWnd);
InvalidateRect(hWnd, nullptr, TRUE);
}
void RadEdit::OnSetFocus(HWND hWnd, HWND /*hWndOldFocus*/)
{
CreateCaret(hWnd, NULL, 1, m_TextMetrics.tmHeight);
MoveCaret(hWnd);
ShowCaret(hWnd);
InvalidateRect(hWnd, NULL, TRUE);
NotifyParent(hWnd, EN_SETFOCUS);
}
void RadEdit::OnKillFocus(HWND hWnd, HWND /*hWndNewFocus*/)
{
DestroyCaret();
InvalidateRect(hWnd, NULL, TRUE);
NotifyParent(hWnd, EN_KILLFOCUS);
}
void RadEdit::OnSetFont(HWND hWnd, HFONT hFont, BOOL bRedraw)
{
if (hFont == NULL)
hFont = GetStockFont(SYSTEM_FONT);
const int ocs = AveCharWidth();
m_hFont = hFont;
{
HDC hDC = GetWindowDC(hWnd);
HFONT of = SelectFont(hDC, m_hFont);
GetTextMetrics(hDC, &m_TextMetrics);
SelectFont(hDC, of);
ReleaseDC(hWnd, hDC);
}
{
const int cs = AveCharWidth();
for (int i = 0; i < m_nTabStops; ++i)
m_rgTabStops[i] = MulDiv(m_rgTabStops[i], cs, ocs);
}
if (bRedraw)
InvalidateRect(hWnd, nullptr, TRUE);
}
HFONT RadEdit::OnGetFont(HWND /*hWnd*/) const
{
return m_hFont;
}
void RadEdit::OnKey(HWND hWnd, UINT vk, BOOL fDown, int /*cRepeat*/, UINT /*flags*/)
{
if (!fDown)
return;
const bool bShift = (GetKeyState(VK_SHIFT) & KF_UP) != 0;
const bool bCtrl = (GetKeyState(VK_CONTROL) & KF_UP) != 0;
const bool bAlt = (GetKeyState(VK_MENU) & KF_UP) != 0;
switch (vk)
{
case VK_LEFT:
if (!bAlt)
{
int nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
nSelEnd = GetPrevChar(m_hText, nSelEnd);
if (bCtrl && nSelEnd >= 0)
{
EDITWORDBREAKPROC ewb = m_pEditWordBreakProc != nullptr ? m_pEditWordBreakProc : DefaultEditWordBreakProc;
nSelEnd = MoveWord(ewb, WB_LEFT, m_hText, nSelEnd);
}
if (nSelEnd >= 0)
{
if (!bShift)
nSelStart = nSelEnd;
OnSetSel(hWnd, nSelStart, nSelEnd);
OnScrollCaret(hWnd);
}
}
break;
case VK_RIGHT:
if (!bAlt)
{
int nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
nSelEnd = GetNextChar(m_hText, nSelEnd);
if (bCtrl && nSelEnd >= 0)
{
EDITWORDBREAKPROC ewb = m_pEditWordBreakProc != nullptr ? m_pEditWordBreakProc : DefaultEditWordBreakProc;
nSelEnd = MoveWord(ewb, WB_RIGHT, m_hText, nSelEnd);
}
if (nSelEnd >= 0)
{
if (!bShift)
nSelStart = nSelEnd;
OnSetSel(hWnd, nSelStart, nSelEnd);
OnScrollCaret(hWnd);
}
}
break;
case VK_UP:
if (!bAlt && !bCtrl)
{
int nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
POINT pos(EditPosFromChar(hWnd, nSelEnd));
pos.y -= LineHeight();
nSelEnd = EditCharFromPos(hWnd, pos);
if (!bShift)
nSelStart = nSelEnd;
OnSetSel(hWnd, nSelStart, nSelEnd);
OnScrollCaret(hWnd);
}
break;
case VK_DOWN:
if (!bAlt && !bCtrl)
{
int nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
POINT pos(EditPosFromChar(hWnd, nSelEnd));
pos.y += LineHeight();
nSelEnd = EditCharFromPos(hWnd, pos);
if (!bShift)
nSelStart = nSelEnd;
OnSetSel(hWnd, nSelStart, nSelEnd);
OnScrollCaret(hWnd);
}
break;
case VK_HOME:
if (!bAlt)
{
DWORD nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
if (bCtrl)
nSelEnd = 0;
else
nSelEnd = TextLineStart(m_hText, nSelEnd);
if (nSelEnd >= 0)
{
if (!bShift)
nSelStart = nSelEnd;
OnSetSel(hWnd, nSelStart, nSelEnd);
OnScrollCaret(hWnd);
}
}
break;
case VK_END:
if (!bAlt)
{
DWORD nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
if (bCtrl)
nSelEnd = TextLength(m_hText);
else
nSelEnd = TextLineEnd(m_hText, nSelEnd);
if (nSelEnd >= 0)
{
if (!bShift)
nSelStart = nSelEnd;
OnSetSel(hWnd, nSelStart, nSelEnd);
OnScrollCaret(hWnd);
}
}
break;
case VK_NEXT:
if (!bAlt)
{
int nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
POINT pos(EditPosFromChar(hWnd, nSelEnd));
OnScroll(hWnd, SB_PAGEDOWN, NULL, SB_VERT);
nSelEnd = EditCharFromPos(hWnd, pos);
if (!bShift)
nSelStart = nSelEnd;
OnSetSel(hWnd, nSelStart, nSelEnd);
OnScrollCaret(hWnd);
}
break;
case VK_PRIOR:
if (!bAlt)
{
int nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
POINT pos(EditPosFromChar(hWnd, nSelEnd));
OnScroll(hWnd, SB_PAGEUP, NULL, SB_VERT);
nSelEnd = EditCharFromPos(hWnd, pos);
if (!bShift)
nSelStart = nSelEnd;
OnSetSel(hWnd, nSelStart, nSelEnd);
OnScrollCaret(hWnd);
}
break;
case VK_DELETE:
if (bShift)
OnCut(hWnd);
else
{
if (m_nSelEnd == m_nSelStart)
{
int nSelEnd = GetNextChar(m_hText, m_nSelEnd);
if (nSelEnd >= 0)
m_nSelEnd = nSelEnd;
}
if (m_nSelEnd != m_nSelStart)
{
WCHAR r[] = _T("");
ReplaceSel(hWnd, r, TRUE);
}
}
break;
case VK_INSERT:
if (bCtrl)
OnCopy(hWnd);
if (bShift)
OnPaste(hWnd);
break;
case _T('X'):
if (bCtrl)
OnCut(hWnd);
break;
case _T('C'):
if (bCtrl)
OnCopy(hWnd);
break;
case _T('V'):
if (bCtrl)
OnPaste(hWnd);
break;
}
}
void RadEdit::OnChar(HWND hWnd, TCHAR ch, int /*cRepeat*/)
{
switch (ch)
{
default:
if (iswprint((TCHAR) ch))
{
WCHAR r[] = { (WCHAR) ch, L'\0' };
ReplaceSel(hWnd, r, TRUE);
}
break;
case VK_RETURN:
{
WCHAR r[] = _T("\r\n");
ReplaceSel(hWnd, r, TRUE);
}
break;
case VK_TAB:
{
WCHAR r[] = _T("\t");
ReplaceSel(hWnd, r, TRUE);
}
break;
case VK_BACK:
if (m_nSelEnd == m_nSelStart)
{
int nSelEnd = GetPrevChar(m_hText, m_nSelEnd);
if (nSelEnd >= 0)
m_nSelEnd = nSelEnd;
}
if (m_nSelEnd != m_nSelStart)
{
WCHAR r[] = _T("");
ReplaceSel(hWnd, r, TRUE);
}
break;
}
}
void RadEdit::OnLButtonDown(HWND hWnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
{
POINT point = { x, y };
DWORD nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
//OnGetSel(hWnd, &nSelStart, &nSelEnd);
nSelEnd = EditCharFromPos(hWnd, point);
m_bSelectWord = fDoubleClick;
if (fDoubleClick)
{
EDITWORDBREAKPROC ewb = &DefaultEditWordBreakProc;
if (!(keyFlags & MK_SHIFT))
nSelStart = MoveWord(ewb, WB_LEFT, m_hText, nSelEnd);
nSelEnd = MoveWord(ewb, WB_RIGHT, m_hText, nSelEnd);
m_bSelectWord = TRUE;
}
else if (!(keyFlags & MK_SHIFT))
nSelStart = nSelEnd;
OnSetSel(hWnd, nSelStart, nSelEnd);
SetCapture(hWnd);
}
void RadEdit::OnLButtonUp(HWND /*hWnd*/, int x, int y, UINT /*keyFlags*/)
{
POINT point = { x, y };
ReleaseCapture();
}
void RadEdit::OnMouseMove(HWND hWnd, int x, int y, UINT /*keyFlags*/)
{
POINT point = { x, y };
if (GetCapture() == hWnd)
{
DWORD nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
//OnGetSel(hWnd, &nSelStart, &nSelEnd);
nSelEnd = EditCharFromPos(hWnd, point);
if (m_bSelectWord)
{
EDITWORDBREAKPROC ewb = &DefaultEditWordBreakProc;
nSelEnd = MoveWord(ewb, WB_RIGHT, m_hText, nSelEnd);
}
OnSetSel(hWnd, nSelStart, nSelEnd);
OnScrollCaret(hWnd);
}
}
void RadEdit::OnMouseWheel(HWND hWnd, int /*xPos*/, int /*yPos*/, int zDelta, UINT fwKeys)
{
if ((fwKeys & (MK_CONTROL | MK_SHIFT)) == 0)
{
int nLines = MulDiv(zDelta, -3, WHEEL_DELTA);
Edit_Scroll(hWnd, nLines, 0);
}
}
void RadEdit::OnContextMenu(HWND hWnd, HWND /*hWndContext*/, UINT xPos, UINT yPos)
{
HMODULE hInst = GetWindowInstance(hWnd);
HMENU hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_RADEDIT));
DWORD nSelStart = m_nSelStart, nSelEnd = m_nSelEnd;
//OnGetSel(hWnd, &nSelStart, &nSelEnd);
BOOL bSelected = nSelStart != nSelEnd;
EnableMenuItem(hMenu, WM_CUT, MF_BYCOMMAND | (bSelected ? MF_ENABLED : MF_DISABLED));
EnableMenuItem(hMenu, WM_COPY, MF_BYCOMMAND | (bSelected ? MF_ENABLED : MF_DISABLED));
EnableMenuItem(hMenu, WM_CLEAR, MF_BYCOMMAND | (bSelected ? MF_ENABLED : MF_DISABLED));
int cmd = TrackPopupMenu(GetSubMenu(hMenu, 0), TPM_RETURNCMD | TPM_RIGHTBUTTON, xPos, yPos, 0, hWnd, nullptr);
if (cmd > 0)
SendMessage(hWnd, cmd, 0, -1);
DestroyMenu(hMenu);
}
INT RadEdit::OnGetTextLength(HWND /*hWnd*/)
{
return TextLength(m_hText);
}
INT RadEdit::OnGetText(HWND /*hWnd*/, int cchTextMax, LPTSTR lpszText)
{
PCWSTR const buffer = (PCWSTR) LocalLock(m_hText);
StrCpyN(lpszText, buffer, cchTextMax);
LocalUnlock(m_hText);
return (INT) wcslen(lpszText); // TODO capture size when copying
}
BOOL RadEdit::OnSetText(HWND /*hWnd*/, LPCTSTR lpszText)
{
HLOCAL hTextNew = TextCreate(lpszText);
if (hTextNew != NULL)
{
LocalFree(m_hText);
m_hText = hTextNew;
return TRUE;
}
else
return FALSE;
}
void RadEdit::OnCut(HWND hWnd)
{
OnCopy(hWnd);
ReplaceSel(hWnd, _T(""), TRUE);
}
void RadEdit::OnCopy(HWND hWnd)
{
DWORD nSelStart, nSelEnd;
OnGetSel(hWnd, &nSelStart, &nSelEnd);
if (nSelStart < nSelEnd)
{
DWORD nLength = nSelEnd - nSelStart;
HGLOBAL hClip = GlobalAlloc(GMEM_MOVEABLE, (nLength + 1) * sizeof(WCHAR));
PWSTR const clip = (PWSTR) GlobalLock(hClip);
PCWSTR const buffer = (PCWSTR) LocalLock(m_hText);
memcpy(clip, buffer + nSelStart, nLength * sizeof(WCHAR));
clip[nLength] = L'\0';
LocalUnlock(m_hText);
GlobalUnlock(hClip);
if (OpenClipboard(hWnd))
{
ENSURE(SetClipboardData(CF_UNICODETEXT, hClip));
CloseClipboard();
}
}
}
void RadEdit::OnPaste(HWND hWnd)
{
if (OpenClipboard(hWnd))
{
HGLOBAL hClip = GetClipboardData(CF_UNICODETEXT);
if (hClip != NULL)
{
PCWSTR const buffer = (PCWSTR) GlobalLock(hClip);
ReplaceSel(hWnd, buffer, TRUE);
GlobalUnlock(hClip);
}
CloseClipboard();
}
}
void RadEdit::OnClear(HWND hWnd)
{
ReplaceSel(hWnd, L"", TRUE);
}
void RadEdit::OnSetRedraw(HWND hWnd, BOOL fRedraw)
{
// TODO Capture rcPaint during WM_PAINT to reapply here
FORWARD_WM_SETREDRAW(hWnd, fRedraw, DefWindowProc);
if (fRedraw)
RedrawWindow(hWnd, NULL, NULL, RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
//InvalidateRect(hWnd, NULL, TRUE);
}
UINT RadEdit::OnGetDlgCode(HWND /*hWnd*/, LPMSG /*pMsg*/)
{
return DLGC_HASSETSEL | DLGC_WANTALLKEYS | DLGC_WANTARROWS | DLGC_WANTCHARS | DLGC_WANTTAB;
}
LRESULT RadEdit::OnGetSel(HWND /*hWnd*/, LPDWORD pSelStart, LPDWORD pSelEnd) const
{
DWORD nStart = min(m_nSelStart, m_nSelEnd);
DWORD nEnd = max(m_nSelStart, m_nSelEnd);