-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookedCtrl.h
More file actions
1954 lines (1775 loc) · 56 KB
/
HookedCtrl.h
File metadata and controls
1954 lines (1775 loc) · 56 KB
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
#pragma once
#include "Parser.h"
#include "resource.h"
#define CLAMPEB(v) max(0,min(m_TextLen,v))
#define CLAMPEB2(v) max(0,min(m_TextLen-1,v))
class EditAction
{
public:
size_t m_OSelStart;
size_t m_OSelEnd;
wstring m_OrigSelText;
size_t m_NSelStart;
size_t m_NSelEnd;
wstring m_NewText;
bool m_RememberNewSize;
EditAction(size_t OSelStart, size_t OSelEnd, wstring OldSelText, size_t NSelStart, size_t NSelEnd, wstring NewText, bool RememberNewSize)
{
m_OSelStart = OSelStart;
m_OSelEnd = OSelEnd;
m_OrigSelText = OldSelText;
m_NSelStart = NSelStart;
m_NSelEnd = NSelEnd;
m_NewText = NewText;
m_RememberNewSize = RememberNewSize;
}
};
typedef stack<EditAction*> EditActions;
extern HINSTANCE ghDllHandle;
typedef struct EbSettings{ //edit box settings. some values default to my settings
WNDPROC WndProc = NULL;
short TabStop = 16;
LPARAM Margins = 0;
UINT BlinkTime = 0;
HBRUSH BkBrush = 0;
HFONT font = 0;
int TextHeight = 0;
} EBSETTINGS;
typedef struct ColorSpecs
{
COLORREF DlgBkColor = 0;
HBRUSH DlgBkColorBrush = NULL;
COLORREF CtrlTextColor = 0;
COLORREF CtrlBkColor = 0;
HBRUSH CtrlBkColorBrush = NULL;
COLORREF CtrlButColor = 0;
HBRUSH CtrlButColorBrush = NULL;
COLORREF CtrlButTextColor = 0;
HBRUSH CtrlButTextColorBrush = NULL;
COLORREF CtrlButHoverColor = 0;
HBRUSH CtrlButHoverColorBrush = NULL;
COLORREF CtrlButDownColor = 0;
HBRUSH CtrlButDownColorBrush = NULL;
} COLORSPECS;
class HookedCtrl
{
public: //yeah, I'm bad with privacy, this aint a corporate project
HWND m_hWnd = NULL;
EBSETTINGS m_OEBS;
EBSETTINGS m_NEBS;
Language *m_Language = NULL;
ColorSpec *m_ColorSpec = NULL;
bool m_DrawLineNumbers = true;
EditActions m_UndoStack;
EditActions m_RedoStack;
int LineNoTextGap = 4;
LONG m_SelStart = 0;
LONG m_SelEnd = 0;
int m_StopRedraw = 0;
int m_CaretTimer = -1;
bool CaretOn = false;
int m_NumCaretTicks = 0;
LONG m_TextLen = 0;
TEXTMETRIC m_textMetrics;
TCHAR m_CaretChar = 0;
TCHAR m_CaretPrevChar = 0;
bool m_IsWordWrap = false;
short m_Margin = 0;
HCURSOR m_RightArrow;
HCURSOR m_Arrow;
HCURSOR m_IBeam;
HCURSOR m_CurrentCursor;
bool m_ScrollCaretEnabled = true;
LONG m_LineStartSelStart = 0;
LONG m_LineStartSelEnd = 0;
LPARAM OldMousePos = 0;
bool m_WordSelectMode = false;
LONG m_OldMouseChar = 0;
LRESULT m_InsertMode = 1;
bool m_IsReadOnly = false;
bool m_DontHideSelection = false;
SearchDialog m_SearchDlg;
WordListDialog m_WordListDlg;
COLORSPECS m_Colors;
KERNINGPAIR* m_KerningPairs = NULL;
int m_NumKerningPairs = 0;
WORDLIST m_WordList;
HLOCAL m_hColorBytes = NULL;
LONG m_ColorByteAllocSize = 0;
Parser m_Parser;
size_t m_WLWordStart = 0;
size_t m_WLWordEnd = 0;
size_t m_WLAltWordStart = 0;
public:
HookedCtrl(HWND hWnd) {
m_SearchDlg.Init(hWnd);
m_WordListDlg.Init(hWnd);
/*
m_WordList.push_back(L"Word1");
m_WordList.push_back(L"Word2");
m_WordList.push_back(L"Word3");
m_WordList.push_back(L"Word4");
m_WordList.push_back(L"Word5");
m_WordList.push_back(L"Word6");
m_WordList.push_back(L"Word7");
m_WordList.push_back(L"Word8");
m_WordList.push_back(L"Word9");
m_WordList.push_back(L"Word10");
m_WordList.push_back(L"Word11");
m_WordList.push_back(L"Word12");
m_WordListDlg.SetWordList(m_WordList);
*/
m_Parser.SetHookedCtrl(this);
m_ColorByteAllocSize = 65536;
m_hColorBytes = LocalAlloc(LHND, sizeof(BYTE) * m_ColorByteAllocSize);
m_RightArrow = LoadCursor(ghDllHandle, MAKEINTRESOURCE(IDC_RIGHTPOINTER));
m_Arrow = LoadCursor(NULL, IDC_ARROW);
m_IBeam = LoadCursor(NULL, IDC_IBEAM);
m_CurrentCursor = m_IBeam;
m_hWnd = hWnd;
};
~HookedCtrl() {
OnUnHook();
m_hColorBytes = LocalFree(m_hColorBytes);
};
LRESULT DoIndent(int Style)
{
wstring t = m_Parser.DoIndent((HLOCAL)DirectSend(EM_GETHANDLE, 0, 0), m_TextLen, Style);
//SetWindowText(m_hWnd, t->c_str());
LONG endsel = m_SelEnd;
RedrawStop();
m_ScrollCaretEnabled = false;
SendMessage(m_hWnd, EM_SETSEL, 0, -1);
AddUndoableContent(t);
SendMessage(m_hWnd, EM_SETSEL, endsel, endsel);
RedrawResume();
m_ScrollCaretEnabled = true;
CaretMoveEndOfLine(-1, 0, 0);
return 0l;
}
//Color bytes manips
void ResizeColorBytes(DWORD EBTextLen)
{
//this functions grows or shrinks the color bytes according to the passed edit box text size. a 64k buffer growth is used so to limit the amount of re-allocs
DWORD AllocatedSize = m_ColorByteAllocSize;
//shrink down if eb text is less than what is alocated
while (AllocatedSize > EBTextLen) AllocatedSize -= 65536;
//grow if eb text is more or equal to what is allocated, this allows for the extra 64k buffer
while (AllocatedSize <= EBTextLen) AllocatedSize += 65536;
//if the resulting amount is not what is currently allocated, grow or shrink
if (AllocatedSize != m_ColorByteAllocSize)
{
if (NULL != LocalReAlloc(m_hColorBytes, sizeof(BYTE) * AllocatedSize, LHND))
{
//failed, memory was not re-alocated, stick to initial values
m_ColorByteAllocSize = AllocatedSize;
}
}
}
//Main Window Procedure stuff
void RestoreWindowProc() { SetWindowLongPtr(m_hWnd, GWLP_WNDPROC, (LONG_PTR)m_OEBS.WndProc); };
void StoreWindowProcs(WNDPROC origWndProc, WNDPROC newWndProc) { m_OEBS.WndProc = origWndProc; m_NEBS.WndProc = newWndProc; };
LRESULT DirectSend(UINT message, WPARAM wParam, LPARAM lParam) { return CallWindowProc(m_OEBS.WndProc, m_hWnd, message, wParam, lParam); };
LRESULT CallOrigWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { return CallWindowProc(m_OEBS.WndProc, hWnd, message, wParam, lParam); };
LRESULT DoWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
//Message Handlers
LRESULT DoWM_PAINT(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_ERASEBKGND(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_USER(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_CHAR(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_SIZE(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoEM_SETSEL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoEM_GETSEL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_PASTE(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_LBUTTONDOWN(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_LBUTTONUP(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_MOUSEMOVE(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_TIMER(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_KILLFOCUS(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_KEYDOWN(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoEM_SCROLLCARET(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_SETFOCUS(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_SCROLL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_SETCURSOR(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_LBUTTONDBLCLK(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_COPY(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_CUT(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_MOUSEWHEEL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoWM_SETFONT(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT DoEM_SETREADONLY(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
RedrawStop();
LRESULT r = CallOrigWindowProc(hWnd, message, wParam, lParam);
RedrawResume();
m_IsReadOnly = (GetWindowLongPtr(hWnd, GWL_STYLE) & ES_READONLY) == ES_READONLY;
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
return r;
}
LRESULT DoUM_SETNOHIDESEL(WPARAM wParam)
{
LONG_PTR s = GetWindowLongPtr(m_hWnd, GWL_STYLE);
if (wParam)
{
s |= ES_NOHIDESEL;
}
else
{
s ^= ES_NOHIDESEL;
}
SetWindowLongPtr(m_hWnd, GWL_STYLE, (LONG_PTR)s);
m_DontHideSelection = (GetWindowLongPtr(m_hWnd, GWL_STYLE) & ES_NOHIDESEL) == ES_NOHIDESEL;
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
SetWindowPos(m_hWnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE);
return 0l;
}
LRESULT DoWM_SETTEXT(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
RedrawStop();
LRESULT r = CallOrigWindowProc(hWnd, message, wParam, lParam);
FreeUndoRedo();
DirectSend(EM_GETSEL, (WPARAM)&m_SelStart, (LPARAM)&m_SelEnd);
RedrawResume();
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
return r;
}
LRESULT DoEM_REPLACESEL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
this->AddUndoableContent((TCHAR*)lParam,true);
if(wParam == 0) FreeUndoRedo();
return 0L;
}
void FreeUndoRedo()
{
//clean up the redo and undo stack
while (!m_RedoStack.empty())
{
EditAction *t = m_RedoStack.top();
m_RedoStack.pop();
delete t;
}
while (!m_UndoStack.empty())
{
EditAction *t = m_UndoStack.top();
m_UndoStack.pop();
delete t;
}
}
LRESULT DoEM_EMPTYUNDOBUFFER(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
FreeUndoRedo();
return CallOrigWindowProc(hWnd, message, wParam, lParam);
}
//Drawing
void RedrawStop()
{
//Call to cancel drawing, added a stack type ability
m_StopRedraw++;
DirectSend(WM_SETREDRAW, m_StopRedraw == 0, 0);
}
void RedrawResume()
{
//Call to resume drawing, added a stack type ability
m_StopRedraw = max(0, m_StopRedraw-1);
DirectSend(WM_SETREDRAW, m_StopRedraw == 0, 0);
}
void Paint(HWND hWnd, HDC hDC);
//Language
void SetLanguage(Language *Language) {
m_Language = Language;
m_WordList.clear();
if (m_Language != NULL)
{
for (const auto& elem : m_Language->KEYWORDS.m_Words) {
wstringex *t = new wstringex();
t->assign(elem);
t->type = COLORDEREF::KEYWORDCOLOR;
m_WordList.push_back(*t);
}
for (const auto& elem : m_Language->LITERALS.m_Words) {
wstringex *t = new wstringex();
t->assign(elem);
t->type = COLORDEREF::LITERALCOLOR;
m_WordList.push_back(*t);
}
for (const auto& elem : m_Language->FUNCTIONS.m_Words) {
wstringex *t = new wstringex();
t->assign(elem);
t->type = COLORDEREF::FUNCTIONCOLOR;
m_WordList.push_back(*t);
}
for (const auto& elem : m_Language->STATICCLASS.m_Words) {
wstringex *t = new wstringex();
t->assign(elem);
t->type = COLORDEREF::STATICCLASSCOLOR;
m_WordList.push_back(*t);
}
for (const auto& elem : m_Language->CLASSFUNCS.m_Words) {
wstringex *t = new wstringex();
t->assign(elem);
t->type = COLORDEREF::CLASSFUNCCOLOR;
m_WordList.push_back(*t);
}
}
};
Language * GetLanguage() { return m_Language; };
//Color Specs
void SetColorSpec(ColorSpec *ColSpec) {
m_ColorSpec = ColSpec;
};
ColorSpec * GetColorSpec() { return m_ColorSpec; };
//Tab Stops
void SaveTabStops() { m_OEBS.TabStop = 32; };
void SetTabStops() { int ts[1]; ts[0] = m_NEBS.TabStop; DirectSend(EM_SETTABSTOPS, 1, (LPARAM)ts); };
void RestoreTabStops() { int ts[1]; ts[0] = m_OEBS.TabStop; DirectSend(EM_SETTABSTOPS, 1, (LPARAM)ts); };
//Margins
void SaveMargins() { m_OEBS.Margins = DirectSend(EM_GETMARGINS, 0, 0); };
void SaveFont() { m_OEBS.font = (HFONT)DirectSend(WM_GETFONT, 0, 0); };
void RestoreFont()
{
HFONT CurrentFont = (HFONT)DirectSend(WM_GETFONT, 0, 0);
DirectSend(WM_SETFONT, (WPARAM)m_OEBS.font, 0);
if (CurrentFont != m_OEBS.font) DeleteObject(CurrentFont);
};
//return true if changed so we know a redraw was sent so we can cancel drawing
bool SetMargins(int left, int right) { m_NEBS.Margins = MAKELPARAM(left, right); if (DirectSend(EM_GETMARGINS, 0, 0) != m_NEBS.Margins) { DirectSend(EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, m_NEBS.Margins); return true; } return false; };
bool SetLeftMarginTextWidth(wstring MarginText, HDC hdc) { SIZE sz; GetTextExtentPoint32(hdc,MarginText.c_str(),(int)MarginText.length(),&sz ); return SetMargins(sz.cx + LineNoTextGap * 2, 0); };
WORD GetLeftMargin() { return LOWORD(DirectSend(EM_GETMARGINS, 0, 0)); };
void RestoreMargins() { DirectSend(EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, m_OEBS.Margins); };
//Caret/ Cursor
void SetTheCursor(HCURSOR cur)
{
//used to change the cursor
if (GetCursor() != cur) //if already set, prevent cursor freakout, do not call
{
m_CurrentCursor = cur;
SetCursor(m_CurrentCursor);
}
}
void TurnCaretOn(int NumTicks = 1)
{
//turns the caret visiility on. NumTicks specifies how long it remain full on before blinking again
m_NumCaretTicks = NumTicks;
CaretOn = true;
//ShowCaret(m_hWnd);
}
void SelectNextChar(int direction)
{
//this function safely selects one character for replacement. usually either 1 char or the \r\n EOL
//correct any between \r\n issue the caret will appear at the end of the line still to the user, so got back before the \r\n
if (direction == 1 && m_CaretChar == '\n')
m_SelEnd -= 1;
else if (direction == -1 && m_CaretPrevChar == '\r')
m_SelEnd -= 1;
m_SelStart = m_SelEnd;
if (direction == 1 && m_CaretChar == '\r')
m_SelEnd += 2;
else if (direction == -1 && m_CaretPrevChar == '\n')
m_SelEnd -= 2;
else
m_SelEnd += direction;
SendMessage(m_hWnd, EM_SETSEL, (WPARAM)m_SelStart, (LPARAM)m_SelEnd);
}
void CleanWord(wstring &t)
{
t.erase(std::remove(t.begin(), t.end(), L'\n'), t.end());
t.erase(std::remove(t.begin(), t.end(), L'\r'), t.end());
}
//Selection
wstring GetSelText(LONG ss = -1, LONG se = -1)
{
//get the selection text
wstring retString;
DWORD SelStart = ss,
SelEnd = se;
if(ss == -1)
DirectSend(EM_GETSEL, (WPARAM) &SelStart, (LPARAM) &SelEnd);
if (SelStart > SelEnd) swap(SelStart, SelEnd);
//create a string directly from the memory handle
HLOCAL hMem = (HLOCAL)DirectSend(EM_GETHANDLE, 0, 0);
TCHAR *Text = NULL;
if (hMem)
{
TCHAR *Text = (TCHAR*)LocalLock(hMem);
if (Text)
{
//by assigning a rterminating null tempararely to the end of select
TCHAR OldChar = Text[SelEnd];
Text[SelEnd] = 0;
//and relying on the wstring TCHAR* asign
retString = (Text + SelStart);
//place back the character that was there
Text[SelEnd] = OldChar;
LocalUnlock(hMem);
}
}
return retString;
}
void AddUndoableContent(wstring Text, bool RememberNewSize = false, bool SelectNewText = false, UINT message = 0, WPARAM WMCHAR = 0, LPARAM WMCHARLPARAM = 0)
{
//all the message, wparam craziness is because the code used to blindly call REMPLACESEL which is very slow on huge code when editing at top. so instead, when
//possible, we send the WM_CHAR message and params, which the edit box is faster to process the inserts with. there is still some slowdown for enter and shift tab
//and backspse on the first character in the line which I cant resolve... the slowdowns are almost always the same as in the plain jane edit box...
//...I spent about 3 weeks trying to resolve the slowdowns with huge code, we are talking about tens of thousand of lines of code here where the slowdown happens,
//a scenario that is unlikely, but being a massive anal retentive programmer, I was being thorough to have it working in that scenario.
if (m_IsReadOnly) return;
m_Parser.SetChangeRegion(m_SelStart, m_SelEnd);
//this function is called to change the content of the text box. it also records the action for undo...
m_ScrollCaretEnabled = false;
RedrawStop();
//clean up the redo stack
while (!m_RedoStack.empty())
{
EditAction *t = m_RedoStack.top();
m_RedoStack.pop();
delete t;
}
//get what was selected
wstring OldText = this->GetSelText();
//get the selection range
DWORD start = m_SelStart, end = m_SelEnd;
//DirectSend(EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
//add the info to the undo stack
LONG NewStart = min(start, end);
LONG NewEnd = NewStart + (LONG) Text.length();
m_UndoStack.push(new EditAction(start, end, OldText, NewStart, NewEnd, Text, RememberNewSize));
//perform the replace
if (!message)
{
DirectSend(EM_REPLACESEL, FALSE, (LPARAM)Text.c_str());
}
else // if wm_char message is passed...
{
//DirectSend(WM_KEYDOWN, WMCHAR, WMCHARLPARAM);
DirectSend(message, WMCHAR, WMCHARLPARAM);
//DirectSend(WM_KEYUP, WMCHAR, WMCHARLPARAM);
}
//move caret to the end of the new content
if (start > end) swap(start, end);
end = start + (DWORD)Text.length();
if (!SelectNewText) start = end;
SendMessage(m_hWnd, EM_SETSEL, (WPARAM)start, (LPARAM)end);
RedrawResume();
m_ScrollCaretEnabled = true;
SendMessage(m_hWnd, EM_SCROLLCARET, 0, 0);
TurnCaretOn();
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
m_TextLen = (LONG)DirectSend(WM_GETTEXTLENGTH, 0, 0);
m_Parser.SetChangeRegion(m_SelStart, m_SelEnd);
if (m_WordListDlg.IsVisible() && m_WordListDlg.m_hParent == m_hWnd)
{
//if (Text != L" " && Text != L"\t")
{
DoWordList();
}
//else
//{
// m_WordListDlg.Hide();
//}
}
//UpdateWindow(m_hWnd);
}
bool CanUndo()
{
return (LRESULT)!m_UndoStack.empty();
}
bool EditUndo() {
if (m_IsReadOnly) return false;
//TODO, retest
if (CanUndo())
{
TurnCaretOn();
EditAction *t = m_UndoStack.top();
RedrawStop();
SendMessage(m_hWnd, EM_SETSEL, t->m_NSelStart, t->m_NSelEnd);
m_Parser.SetChangeRegion(m_SelStart, m_SelEnd);
if (SendMessage(m_hWnd, EM_SCROLLCARET, 0, 0)) //do not do it until we see it, if scrolled, bail
{
RedrawResume();
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
return true;
}
DirectSend(EM_REPLACESEL, FALSE, (LPARAM)t->m_OrigSelText.c_str());
SendMessage(m_hWnd, EM_SETSEL, t->m_OSelStart, t->m_OSelEnd);
RedrawResume();
m_Parser.SetChangeRegion(m_SelStart, m_SelEnd);
SendMessage(m_hWnd, EM_SCROLLCARET, 0,0);
m_UndoStack.pop();
m_RedoStack.push(t);
return true;
}
else
{
TurnCaretOn();
SendMessage(m_hWnd, EM_SETSEL, m_SelEnd, m_SelEnd);
}
return false;
};
LRESULT GetLineMargin()
{
return (LRESULT)m_DrawLineNumbers;
}
LRESULT SetLineMargin(WPARAM mode)
{
LRESULT Old = m_DrawLineNumbers;
m_DrawLineNumbers = mode;
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
return Old;
}
LRESULT GetInsertMode()
{
return (LRESULT) m_InsertMode;
}
LRESULT SetInsertMode(WPARAM mode)
{
if (m_IsReadOnly) return -1l;
LRESULT Old = m_InsertMode;
TurnCaretOn();
m_InsertMode = mode;
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
return Old;
}
bool CanRedo()
{
return (LRESULT)!m_RedoStack.empty();
}
bool EditRedo() {
if (m_IsReadOnly) return 0l;
//TODO: retest
if (CanRedo())
{
TurnCaretOn();
EditAction *t = m_RedoStack.top();
RedrawStop();
SendMessage(m_hWnd, EM_SETSEL, t->m_OSelStart, t->m_OSelEnd);
m_Parser.SetChangeRegion(m_SelStart, m_SelEnd);
if (SendMessage(m_hWnd, EM_SCROLLCARET, 0, 0)) //do not do it until we see it, if scrolled, bail
{
RedrawResume();
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
return true;
}
DirectSend(EM_REPLACESEL, FALSE, (LPARAM)t->m_NewText.c_str());
if(t->m_RememberNewSize)
SendMessage(m_hWnd, EM_SETSEL, t->m_NSelStart, t->m_NSelEnd);
else
SendMessage(m_hWnd, EM_SETSEL, t->m_NSelEnd, t->m_NSelEnd);
RedrawResume();
m_Parser.SetChangeRegion(m_SelStart, m_SelEnd);
SendMessage(m_hWnd, EM_SCROLLCARET, 0, 0);
m_RedoStack.pop();
m_UndoStack.push(t);
return true;
}
else
{
TurnCaretOn();
SendMessage(m_hWnd, EM_SETSEL, m_SelEnd, m_SelEnd);
}
return false;
};
//Font based functions
int GetFontSize(int pt);
int GetFontHeightFromPt(int sz);
int GetFontPtFromHeight(int sz);
int SetFontSize(int pt, int sz);
int ZoomFont(int direction, int rettype);
//For fonts that are not monospace we need to deal with the kerning
void FreeKerningPairs()
{
//Free the kerning array, this happenes when the font changes and when the controle is unhooked
if (m_KerningPairs != NULL)
{
delete[] m_KerningPairs;
m_KerningPairs = NULL;
m_NumKerningPairs = 0;
}
}
//Get the kerning reference array
void AllocKerningPairs(HDC hDC)
{
if (m_KerningPairs == NULL)
{
//how many kerning pairs are there for the font?
m_NumKerningPairs = GetKerningPairs(hDC, 0, NULL);
if (m_NumKerningPairs > 0)
{
//alloc array and store kering pairs
m_KerningPairs = new KERNINGPAIR[m_NumKerningPairs];
GetKerningPairs(hDC, m_NumKerningPairs, m_KerningPairs);
}
}
}
//check is a character combination has kerning info
int GetKerning(HDC hDC, TCHAR Char1, TCHAR Char2)
{
//look through the kerning reference array, find the mathing pair and return the kerning
int ret = 0;
if (m_NumKerningPairs > 0)
{
for (int i = 0; i < m_NumKerningPairs; i++)
{
if (((m_KerningPairs + i)->wFirst == Char1) && ((m_KerningPairs + i)->wSecond == Char2))
{
ret = (m_KerningPairs + i)->iKernAmount;
break;
}
}
}
return ret;
}
//Keyboard Handling/Caret/Line Movement
short GetNextXCoord(HDC DrawDC, short xCoord, LONG CharPos, TCHAR Char, TCHAR Char2)
{
//this function gets the next x based on the current x position and the character to jump over, and the next character to draw
if (Char != '\t') //if the character is not a tab, calculate it's width
{
//get the character width
int w1;
GetCharWidth32(DrawDC, Char, Char, &w1);
//add to the xpostion andsubtract/add the kerning space between the 2 characters if any
return xCoord + w1 + GetKerning(DrawDC, Char, Char2);
}
else //MUCH slower, if it's a tab, get the position of the next character after it relying on pos from char which is super slow in a loop
{
//hack, if tab and end of file, add \r\n so we dont confuse the caret system and so EM_POSFROMCHAR will succeed
if (CharPos == m_TextLen-1)
{
LONG OldSelStart = m_SelStart;
LONG OldSelEnd = m_SelEnd;
RedrawStop();
m_ScrollCaretEnabled = false;
SendMessage(m_hWnd, EM_SETSEL, CharPos + 1, CharPos + 1);
DirectSend(EM_REPLACESEL, 0, (LPARAM)L"\r\n");
SendMessage(m_hWnd, EM_SETSEL, OldSelStart, OldSelEnd);
m_ScrollCaretEnabled = true;
m_TextLen = (LONG)DirectSend(WM_GETTEXTLENGTH, 0, 0);
RedrawResume();
}
return LOWORD(DirectSend(EM_POSFROMCHAR, CLAMPEB(CharPos+1), 0));
}
}
LONG SafeCharFromPos(short x, short y)
{
LRESULT data = (DirectSend(EM_CHARFROMPOS, 0, MAKELPARAM(x, y)));
LONG Char = LOWORD(data);
LONG Line = HIWORD(data);
LONG Limit = (LONG) DirectSend(EM_GETLIMITTEXT, 0, 0);
if (Limit < 65051 && Limit > 0)
{
return Char; //safe until 64k
}
//if it failed the char will be smaller than the first char on the line (the line is valid for far longer)
//but not more than 64k lines... so here I use top + y to get the line
Line = (LONG)DirectSend(EM_GETFIRSTVISIBLELINE, 0, 0) + y/this->m_textMetrics.tmHeight;
LONG CharAtLine = (LONG)DirectSend(EM_LINEINDEX, min(Line, max(0,DirectSend(EM_GETLINECOUNT,0,0)-1)), 0);
if (CharAtLine <= Char)
{
return Char; //looks Ok
}
//so we need to loop until we find the character, from the first char on the line found
Char = CharAtLine;
HLOCAL hMem = (HLOCAL)DirectSend(EM_GETHANDLE, 0, 0);
if (hMem == NULL) return Char;
TCHAR *Text = (TCHAR*)LocalLock(hMem);
if (Text == NULL) return Char;
short thisX;
LONG RetChar = Char;
bool SafeToCheckAhead = false;
short RetCharX = LOWORD(DirectSend(EM_POSFROMCHAR, Char, 0));
//loop through line chars until the x position of the charcter crosses the x coord passed
while(1)
{
thisX = LOWORD(DirectSend(EM_POSFROMCHAR, Char, 0));
if (thisX > x)
{
SafeToCheckAhead = true;
break;
}
else if (Text[Char] == '\r') //quit at newline
{
RetChar = Char;
break;
}
else if (Text[Char] == 0) //quit at null
{
RetChar = Char;
break;
}
else if (Char == Limit) //end of box
{
RetChar = Char;
break;
}
else
{
RetCharX = thisX;
RetChar = Char;
}
Char++;
}
//if we did not hit the end, we are between chars, see if the left char (RetChar as RetCharX) is closer than the one on the right Char as thisX
if (SafeToCheckAhead)
{
if (abs(x - RetCharX) > abs(x - thisX))
{
RetChar = Char;
}
}
LocalUnlock(hMem);
return RetChar;
}
LPARAM SafePosFromChar(LONG CharPos, short ExtraXOff = 0, int ReturnEOLPlusNL = 0)
{
if (m_TextLen == 0) return MAKELPARAM(m_Margin, 0);
else if (CharPos < m_TextLen) return DirectSend(EM_POSFROMCHAR, CharPos, 0);
else
{
LPARAM pos = DirectSend(EM_POSFROMCHAR, m_TextLen - 1, 0);
short x = LOWORD(pos),
y = HIWORD(pos);
bool HasNL = false;
if (ReturnEOLPlusNL == 1)
{
HLOCAL hMem = (HLOCAL)DirectSend(EM_GETHANDLE, 0, 0);
if (hMem != NULL)
{
TCHAR *Text = (TCHAR*)LocalLock(hMem);
if (Text != NULL)
{
if (Text[m_TextLen - 1] == '\n') HasNL = true;
LocalUnlock(hMem);
}
}
}
if(!HasNL)
return MAKELPARAM(x + m_textMetrics.tmAveCharWidth + ExtraXOff, y);
else
return MAKELPARAM(m_Margin + ExtraXOff, y + m_textMetrics.tmHeight);
}
}
bool HitLimit(LONG Pos) { return (Pos == 0 || Pos == m_TextLen); };
LRESULT CaretMoveWord(int direction, int Select, int whitecharonly = 0) {
//Left right word select.
TurnCaretOn();
//sanity check on text data
if (m_TextLen == 0) return 0l;
if (direction == -1 && m_SelEnd == 0) return 0l;
else if (direction == 1 && m_SelEnd == (m_TextLen)) return 0l;
HLOCAL hMem = (HLOCAL)DirectSend(EM_GETHANDLE, 0, 0);
if (hMem == NULL) return 0l;
TCHAR *Text = (TCHAR*)LocalLock(hMem);
if (Text == NULL) return 0l;
//is it a new line? we need to skip over that
if (direction == 1 && m_CaretChar == '\r')
m_SelEnd += 2;
else if (direction == -1 && m_CaretPrevChar == '\n')
m_SelEnd -= 2;
else //if(!HitLimit(m_SelEnd))
{
if (direction == 1 && m_CaretChar == '\n') //between \r\n
m_SelEnd += 1;
else if (direction == -1 && m_CaretPrevChar == '\r') //between \r\n
m_SelEnd -= 1;
//Depending on the type of character we are on, loop until it's not that type of character we are on.
ByteLookup LookUp = this->m_Language->SELECTBLOCKERS; //default to selection blockers
LONG peek = 0;
if (direction == -1) peek = -1;
TCHAR InitialChar = Text[m_SelEnd + peek];
if (whitecharonly)
{
LookUp = this->m_Language->WORDLISTSPLIT;
}
else if (LookUp.Valid(InitialChar))
{
LookUp = this->m_Language->SELECTREVERSEBLOCKERS; //unless we are on selection blockers
}
//loop until we are not on the same type of character
bool Done = false;
TCHAR Char = 0;
//We can skip the first char yeah?
m_SelEnd = CLAMPEB(m_SelEnd + direction);
do
{
//we hit the end, break
if (HitLimit(m_SelEnd))
break;
//read the character
Char = Text[m_SelEnd + peek];
//we hit a char blocker, done
if (LookUp.Valid(Char))
{
//but not if it's the same character, so to skip over tabtabtab or == or // or spacespacespace
if (InitialChar != Char)
break;
}
//next position
m_SelEnd = CLAMPEB(m_SelEnd + direction);
InitialChar = Char; //remeber last character
} while (true);
//bugfix
//fix the selection that may include a half selected newline (\r\n), that is text\r or \ntext is selected when it should not include either newline chars
if (whitecharonly)
{
wstring t = this->GetSelText(m_SelStart, m_SelEnd);
if (t != L"")
{
if (t.c_str()[0] == '\n')
{
if (m_SelStart < m_SelEnd)
{
m_SelStart++;
}
else
{
m_SelEnd++;
}
}
if (t.c_str()[t.length() - 1] == '\r')
{
if (m_SelStart > m_SelEnd)
{
m_SelStart--;
}
else
{
m_SelEnd--;
}
}
}
}
}
LocalUnlock(hMem);
RedrawStop();
bool DoWordListSelect = false;
if (Select) SendMessage(m_hWnd, EM_SETSEL, m_SelStart, m_SelEnd);
else
{
SendMessage(m_hWnd, EM_SETSEL, m_SelEnd, m_SelEnd);
DoWordListSelect = true;
}
RedrawResume();
//move to caret
SendMessage(m_hWnd, EM_SCROLLCARET, 0, 0);
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
if (DoWordListSelect)
{
if (m_WordListDlg.IsVisible(true) && whitecharonly == 0)
{
DoWordList();
}
}
//UpdateWindow(m_hWnd);
return 0l;
}
LRESULT CaretMoveChar(int direction, int control, int Select) {
if (control) return CaretMoveWord(direction, Select);
//move by one character left or right, skip over \r\n
TurnCaretOn();
int moveamount = direction;
if (moveamount == 1)
moveamount *= (m_CaretChar == '\r') ? 2 : 1;
else if (moveamount == -1)
moveamount *= (m_CaretPrevChar == '\n') ? 2 : 1;
m_SelEnd = CLAMPEB(m_SelEnd + moveamount);
//Call set sel, no drawing
RedrawStop();
bool DoWordListSelect = false;
if (Select) SendMessage(m_hWnd, EM_SETSEL, m_SelStart, m_SelEnd);
else
{
SendMessage(m_hWnd, EM_SETSEL, m_SelEnd, m_SelEnd);
DoWordListSelect = true;
}
RedrawResume();
//move to caret
SendMessage(m_hWnd, EM_SCROLLCARET, 0, 0);
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
if (DoWordListSelect)
{
if (m_WordListDlg.IsVisible(true))
{
DoWordList();
}
}
//UpdateWindow(m_hWnd);
return 0;
}
LRESULT CaretMoveLine(int direction, int control, int Select) {