-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookedCtrl.cpp
More file actions
1595 lines (1465 loc) · 50.1 KB
/
HookedCtrl.cpp
File metadata and controls
1595 lines (1465 loc) · 50.1 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
#include "stdafx.h"
#include "HookedCtrl.h"
const int NumFontItems = 36;
// 0 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
int FontSizesPt[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 34, 38, 42, 46, 50, 54, 60, 64, 68, 72, 76, 80, 86, 92, 96};
int FontSizesPx[] = {1, 3, 4, 5, 7, 8, 9, 11, 12, 13, 15, 16, 19, 22, 24, 26, 29, 32, 35, 37, 40, 45, 51, 56, 61, 67, 72, 80, 85, 91, 96, 101, 107, 115, 123, 128};
HFONT FontHandles[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int FontAt = 11;
int HookedCtrl::GetFontPtFromHeight(int sz)
{
int thesz = abs(sz);
int besti = 0;
int bestdist = 999999;
for (int i = NumFontItems - 1; i >= 0; i--)
{
int dist = abs(FontSizesPx[i] - thesz);
if (dist < bestdist)
{
besti = i;
bestdist = dist;
}
}
return FontSizesPt[besti];
}
int HookedCtrl::GetFontHeightFromPt(int sz)
{
int thesz = abs(sz);
int besti = 0;
int bestdist = 999999;
for (int i = NumFontItems - 1; i >= 0; i--)
{
int dist = abs(FontSizesPt[i] - thesz);
if (dist < bestdist)
{
besti = i;
bestdist = dist;
}
}
return FontSizesPx[besti];
}
int HookedCtrl::GetFontSize(int pt)
{
HFONT hFont = (HFONT)DirectSend(WM_GETFONT, 0, 0);
LOGFONT fontAttributes = { 0 };
GetObject(hFont, sizeof(fontAttributes), &fontAttributes);
if (pt) return GetFontPtFromHeight(abs(fontAttributes.lfHeight));
return abs(fontAttributes.lfHeight);
}
int HookedCtrl::SetFontSize(int pt, int sz)
{
int Height = 0;
if (pt)
{
Height = GetFontHeightFromPt(sz);
}
else
{
int ValPt = GetFontPtFromHeight(sz);
Height = GetFontHeightFromPt(ValPt);
}
//Get the font
HFONT hFont = (HFONT)DirectSend(WM_GETFONT, 0, 0);
LOGFONT fontAttributes = { 0 };
GetObject(hFont, sizeof(fontAttributes), &fontAttributes);
//fontAttributes.lfWeight = FW_BOLD;
fontAttributes.lfHeight = -Height;
HFONT NewFont = CreateFontIndirect(&fontAttributes);
if (NewFont)
{
//remember the default font, this new font is managed by the control
HFONT OldEBSFont = m_OEBS.font;
SendMessage(m_hWnd, WM_SETFONT, (WPARAM)NewFont, 1);
//restore the old font
m_OEBS.font = OldEBSFont;
}
if (pt) return GetFontPtFromHeight(Height);
else return Height;
}
int HookedCtrl::ZoomFont(int direction, int rettype = 0)
{
LOGFONT fontAttributes = { 0 };
HFONT hFont = (HFONT)DirectSend(WM_GETFONT, 0, 0);
GetObject(hFont, sizeof(fontAttributes), &fontAttributes);
//fontAttributes.lfWeight = FW_BOLD;
long Height = abs(fontAttributes.lfHeight);
if (direction == 1)
{
for (int i = 0; i < NumFontItems; i++)
{
if (FontSizesPx[i] > Height || i == NumFontItems-1)
{
Height = FontSizesPx[i];
break;
}
}
}
else
{
for (int i = NumFontItems-1; i >=0 ; i--)
{
if (FontSizesPx[i] < Height || i==0)
{
Height = FontSizesPx[i];
break;
}
}
}
int ret = SetFontSize(0, Height);
if (rettype) return GetFontPtFromHeight(ret);
else return ret;
}
#define VK_A 0x41
#define VK_C 0x43
#define VK_F 0x46
#define VK_K 0x4B
#define VK_R 0x52
#define VK_V 0x56
#define VK_X 0x58
#define VK_Y 0x59
#define VK_Z 0x5A
#define VK_SQUAREBRACKET 0xDD
LRESULT HookedCtrl::DoWordList()
{
//Find the word...
HLOCAL hMem = (HLOCAL)DirectSend(EM_GETHANDLE, 0, 0);
TCHAR *Text = NULL;
if (hMem)
{
TCHAR *Text = (TCHAR*)LocalLock(hMem);
if (Text)
{
//...at the cursor
LONG CaretPos = m_SelEnd;
LONG selstart = m_SelEnd;
LONG selend = m_SelEnd;
while (CaretPos-1 >=0 && !m_Language->WORDLISTSPLIT.Valid(Text[CaretPos-1]))
{
CaretPos--;
}
selstart = CaretPos;
m_WLAltWordStart = CaretPos;
wstring Alternative = L"";
while (!m_Language->WORDLISTSPLIT.Valid(Text[CaretPos]))
{
if (Text[CaretPos] == '.')
{
Alternative = Text[CaretPos];
m_WLAltWordStart = CaretPos;
}
else if (Alternative.length())
{
Alternative += Text[CaretPos];
}
CaretPos++;
}
selend = CaretPos;
wstring text = GetSelText(selstart, selend);
m_WLWordStart = selstart;
m_WLWordEnd = selend;
LRESULT Pos = SafePosFromChar(selstart, 0, 1);
short PosX = LOWORD(Pos);
short PosY = HIWORD(Pos);
m_WordListDlg.SetWordList(m_WordList);
m_WordListDlg.SetDispatch(m_hWnd, 0);
RECT r; GetClientRect(m_hWnd, &r);
short InvertedOffset = (short)r.bottom - PosY;
if (m_WordListDlg.m_hDlg == NULL) //for some reason it wont show if it was never shown before, so call twice if the window handle is null (nevere created), too tired to figure it out
m_WordListDlg.Show(SW_SHOWNA, PosX, PosY + (short)m_textMetrics.tmHeight, (short)m_textMetrics.tmAveCharWidth * 24, (short)m_textMetrics.tmHeight * 8, InvertedOffset);
m_WordListDlg.Show(SW_SHOWNA, PosX, PosY + (short)m_textMetrics.tmHeight, (short)m_textMetrics.tmAveCharWidth * 24, (short)m_textMetrics.tmHeight * 8, InvertedOffset);
m_WordListDlg.RefreshListContent(text.c_str(), 1, Alternative);
LocalUnlock(hMem);
}
}
return 0l;
}
LRESULT HookedCtrl::DoWM_KEYDOWN(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//What key?
switch (wParam)
{
case VK_LEFT: return CaretMoveChar(-1, KEY_STATE_DOWN(VK_CONTROL), KEY_STATE_DOWN(VK_SHIFT));
case VK_RIGHT: return CaretMoveChar(1, KEY_STATE_DOWN(VK_CONTROL), KEY_STATE_DOWN(VK_SHIFT));
case VK_UP:
{
if (m_WordListDlg.IsVisible())
{
SetActiveWindow(m_WordListDlg.m_hDlg);
HWND hList = GetDlgItem(m_WordListDlg.m_hDlg, IDC_WORDLIST);
SetFocus(hList);
PostMessage(hList, message, wParam, lParam);
return 0l;
}
else
{
return CaretMoveLine(-1, KEY_STATE_DOWN(VK_CONTROL), KEY_STATE_DOWN(VK_SHIFT));
}
break;
}
case VK_DOWN: {
if (m_WordListDlg.IsVisible())
{
SetActiveWindow(m_WordListDlg.m_hDlg);
HWND hList = GetDlgItem(m_WordListDlg.m_hDlg, IDC_WORDLIST);
SetFocus(hList);
PostMessage(hList, message, wParam, lParam);
return 0l;
}
else
{
return CaretMoveLine(1, KEY_STATE_DOWN(VK_CONTROL), KEY_STATE_DOWN(VK_SHIFT));
}
break;
}
case VK_RETURN:
{
if (m_WordListDlg.IsVisible())
{
SetActiveWindow(m_WordListDlg.m_hDlg);
HWND hList = GetDlgItem(m_WordListDlg.m_hDlg, IDC_WORDLIST);
SetFocus(hList);
PostMessage(hList, message, wParam, lParam);
return 0l;
}
break;
}
case VK_HOME: return CaretMoveEndOfLine(-1, KEY_STATE_DOWN(VK_CONTROL), KEY_STATE_DOWN(VK_SHIFT));
case VK_END: return CaretMoveEndOfLine(1, KEY_STATE_DOWN(VK_CONTROL), KEY_STATE_DOWN(VK_SHIFT));
case VK_PRIOR: return PageScroll(-1, KEY_STATE_DOWN(VK_CONTROL), KEY_STATE_DOWN(VK_SHIFT));
case VK_NEXT: return PageScroll(1, KEY_STATE_DOWN(VK_CONTROL), KEY_STATE_DOWN(VK_SHIFT));
case VK_DELETE: {
if(KEY_STATE_DOWN(VK_SHIFT) && KEY_STATE_DOWN(VK_CONTROL) == 0)
return DoCutKey(1);
else
return DeleteChar(1, KEY_STATE_DOWN(VK_CONTROL), message, wParam, lParam);
}
case VK_INSERT: {
if (KEY_STATE_DOWN(VK_SHIFT) == 0 && KEY_STATE_DOWN(VK_CONTROL))
return DoCopyKey(1);
else if (KEY_STATE_DOWN(VK_SHIFT) && KEY_STATE_DOWN(VK_CONTROL) == 0)
return DoPasteKey(1);
else
return DoInsertKey();
}
case VK_BACK: return DeleteChar(-1, KEY_STATE_DOWN(VK_CONTROL), WM_CHAR, wParam, lParam);
case VK_TAB: return DoTabChar(KEY_STATE_DOWN(VK_SHIFT), WM_CHAR, wParam, lParam);
case VK_K: return DoBlockCommentKey(KEY_STATE_DOWN(VK_CONTROL), KEY_STATE_DOWN(VK_SHIFT));
case VK_Z: return DoUndoKey(KEY_STATE_DOWN(VK_CONTROL));
case VK_Y: return DoRedoKey(KEY_STATE_DOWN(VK_CONTROL));
case VK_C: return DoCopyKey(KEY_STATE_DOWN(VK_CONTROL));
case VK_V: return DoPasteKey(KEY_STATE_DOWN(VK_CONTROL));
case VK_X: return DoCutKey(KEY_STATE_DOWN(VK_CONTROL));
case VK_R:
case VK_F:
{
if (KEY_STATE_DOWN(VK_CONTROL) == 0) return 1L;
m_SearchDlg.Hide();
m_SearchDlg.Show();
m_WordListDlg.Hide();
m_WordListDlg.Show();
PostMessage(m_SearchDlg.m_hDlg, UM_INTERNAL_DOSEARCH, KEY_STATE_DOWN(VK_R), 0);
break;
}
case VK_F3:
{
wstring t;
if(m_SearchDlg.IsVisible() || m_SelStart == m_SelEnd)
t = m_SearchDlg.GetSearchText();
SearchText((KEY_STATE_DOWN(VK_SHIFT)) ? -1 : 1,
(m_SearchDlg.m_SearchCaseBut.State),
(m_SearchDlg.m_SearchWordBut.State),
t);
m_SearchDlg.Show(SW_SHOWNA);
break;
//return SendMessage(hWnd, UM_SEARCHSEL, KEY_STATE_DOWN(VK_SHIFT), (m_SearchDlg.m_SearchCaseBut.State) | (m_SearchDlg.m_SearchWordBut.State * 2));
}
case VK_A:
{
if (KEY_STATE_DOWN(VK_CONTROL) == 0) return 1L;
SendMessage(hWnd, EM_SETSEL, 0, -1); return 0L;
}
case VK_ESCAPE:
{
if (m_WordListDlg.IsVisible())
{
m_WordListDlg.Hide();
}
else if (m_SearchDlg.IsVisible())
{
m_SearchDlg.Hide();
}
else
{
SendMessage(hWnd, EM_SETSEL, -1, 0);
}
return 0l;
}
case VK_SQUAREBRACKET:
{
if (KEY_STATE_DOWN(VK_CONTROL) == 0) return 1L;
SendMessage(hWnd, UM_MATCHBRACKET, 0, -1);
return 0L;
}
case VK_SPACE:
{
if (KEY_STATE_DOWN(VK_CONTROL)) return DoWordList();
return 0L;
}
}
return 0l;
}
LRESULT HookedCtrl::DoWM_USER(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//User messages used to set and get setting and access feature for the control.
//The control sends these messages to itself as well when using these exposed features
switch (message)
{
case UM_BLOCKINDENT: return BlockIndent(wParam);
case UM_BLOCKCOMMENT: return BlockComment(wParam);
case UM_REDO: return EditRedo();
case UM_CANREDO: return CanRedo();
case UM_SETINSERTMODE: return SetInsertMode(wParam);
case UM_GETINSERTMODE: return GetInsertMode();
case UM_CANSEARCHSEL: return CanSearchText();
case UM_SEARCHSEL: return SearchText((wParam==1) ? 1 : -1,
((lParam & UMSS_CS) == UMSS_CS) ? 1 : 0,
((lParam & UMSS_WW) == UMSS_WW) ? 1 : 0);
case UM_GETLINEMARGIN: return GetLineMargin();
case UM_SETLINEMARGIN: return SetLineMargin(wParam);
case UM_CHARFROMPOS64k: return SafeCharFromPos((short)wParam, (short)lParam);
case UM_MATCHBRACKET: return MatchBracket(KEY_STATE_DOWN(VK_SHIFT));
case UM_SETNOHIDESEL: return DoUM_SETNOHIDESEL(wParam);
case UM_GETNOHIDESEL: return (GetWindowLongPtr(m_hWnd, GWL_STYLE) & ES_NOHIDESEL) == ES_NOHIDESEL;
case UM_INTERNAL_GETEBHOOK: return (LRESULT)this;
case UM_SETFONTSIZE: return (LRESULT)SetFontSize((int)wParam, (int)lParam);
case UM_GETFONTSIZE: return GetFontSize((int)wParam);
case UM_ZOOMFONT: return (LRESULT)ZoomFont((int)wParam, (int)lParam);
case UM_PERFORMAUTOINDENT: return (LRESULT)DoIndent((int)wParam);
case UM_SHOWSEARCHREPLACE: {
if (lParam)
{
m_SearchDlg.Hide();
m_SearchDlg.Show();
PostMessage(m_SearchDlg.m_hDlg, UM_INTERNAL_DOSEARCH, wParam, 0);
}
else
{
m_SearchDlg.Hide();
}
return 1l;
}
case UM_GETSCROLLBAR:
{
if(wParam)
return (GetWindowLongPtr(hWnd, GWL_STYLE) & (WS_HSCROLL | ES_AUTOHSCROLL)) == (WS_HSCROLL | ES_AUTOHSCROLL);
else
return (GetWindowLongPtr(hWnd, GWL_STYLE) & (WS_VSCROLL | ES_AUTOVSCROLL)) == (WS_VSCROLL | ES_AUTOVSCROLL);
}
case UM_SETSCROLLBAR:
{
if (wParam)
{
LONG_PTR s = GetWindowLongPtr(m_hWnd, GWL_STYLE);
if (lParam)
s |= (WS_HSCROLL | ES_AUTOHSCROLL);
else
s ^= (WS_HSCROLL | ES_AUTOHSCROLL);
SetWindowLongPtr(m_hWnd, GWL_STYLE, (LONG_PTR)s);
}
else
{
LONG_PTR s = GetWindowLongPtr(m_hWnd, GWL_STYLE);
if (lParam)
s |= (WS_VSCROLL | ES_AUTOVSCROLL);
else
s ^= (WS_VSCROLL | ES_AUTOVSCROLL);
SetWindowLongPtr(m_hWnd, GWL_STYLE, (LONG_PTR)s);
}
RedrawWindow(hWnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_ERASENOW);
SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE);
return 0l;
}
case UM_GETWORDWRAP:
{
return (GetWindowLongPtr(hWnd, GWL_STYLE) & (ES_AUTOHSCROLL)) != (ES_AUTOHSCROLL);
}
case UM_SETWORDWRAP:
{
LONG_PTR s = GetWindowLongPtr(m_hWnd, GWL_STYLE);
if (wParam==0)
s |= (ES_AUTOHSCROLL);
else
s ^= (ES_AUTOHSCROLL);
SetWindowLongPtr(m_hWnd, GWL_STYLE, (LONG_PTR)s);
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE);
SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE);
return 0l;
}
case UM_INTERNAL_CANCELED:
{
SetFocus(m_hWnd); //refocus on the text box
return 0l;
}
case UM_INTERNAL_WORDSELECTED:
{
//list box tells the dialog box that a selection was made. passing the list handle and the linked control idc
HWND ListhWnd = (HWND)lParam;
if (SendMessage(ListhWnd, LB_GETSELCOUNT, 0, 0))
{
LRESULT Sel = SendMessage(ListhWnd, LB_GETCURSEL, 0, 0);
if (Sel != LB_ERR)
{
RedrawStop();
m_ScrollCaretEnabled = false;
wstringex *Word = (wstringex *)SendMessage(ListhWnd, LB_GETITEMDATA, Sel, 0);
if (Word->length())
{
LONG selstart = (LONG)m_WLWordStart;
if(Word->c_str()[0] == '.') selstart = (LONG)m_WLAltWordStart;
SendMessage(hWnd, EM_SETSEL, selstart, (LONG)m_WLWordEnd);
RedrawResume();
m_ScrollCaretEnabled = true;
AddUndoableContent(Word->c_str());
}
}
}
PostMessage(m_hWnd, UM_INTERNAL_REFOCUS, 0, 0);
//SetFocus(m_hWnd); //refocus on the text box
return 0l;
}
case UM_INTERNAL_REFOCUS:
{
SetFocus(m_hWnd);
return 0l;
}
}
//ok, possibly a edit box user message
return CallOrigWindowProc(hWnd, message, wParam, lParam);
}
LRESULT HookedCtrl::DoWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
//caught functionality either add to the behaviour or are implmented to prevent flicker for the standard behaviour
case WM_PAINT: return DoWM_PAINT(hWnd, message, wParam, lParam);
case WM_ERASEBKGND: return DoWM_ERASEBKGND(hWnd, message, wParam, lParam);
case WM_CHAR: return DoWM_CHAR(hWnd, message, wParam, lParam);
case WM_SIZE: return DoWM_SIZE(hWnd, message, wParam, lParam);
case WM_SIZING: return DoWM_SIZE(hWnd, message, wParam, lParam);
case EM_SETSEL: return DoEM_SETSEL(hWnd, message, wParam, lParam);
case EM_GETSEL: return DoEM_GETSEL(hWnd, message, wParam, lParam);
case WM_LBUTTONDOWN:return DoWM_LBUTTONDOWN(hWnd, message, wParam, lParam);
case WM_LBUTTONUP: return DoWM_LBUTTONUP(hWnd, message, wParam, lParam);
case WM_MOUSEMOVE: return DoWM_MOUSEMOVE(hWnd, message, wParam, lParam);
case WM_TIMER: return DoWM_TIMER(hWnd, message, wParam, lParam);
case WM_KILLFOCUS: return DoWM_KILLFOCUS(hWnd, message, wParam, lParam);
case WM_KEYDOWN: return DoWM_KEYDOWN(hWnd, message, wParam, lParam);
case EM_SCROLLCARET:return DoEM_SCROLLCARET(hWnd, message, wParam, lParam);
case WM_SETFOCUS: return DoWM_SETFOCUS(hWnd, message, wParam, lParam);
case EM_SCROLL: return DoWM_SCROLL(hWnd, message, wParam, lParam);
case WM_VSCROLL: return DoWM_SCROLL(hWnd, message, wParam, lParam);
case WM_HSCROLL: return DoWM_SCROLL(hWnd, message, wParam, lParam);
case EM_LINESCROLL: return DoWM_SCROLL(hWnd, message, wParam, lParam);
case WM_MOUSEWHEEL: return DoWM_MOUSEWHEEL(hWnd, message, wParam, lParam);
case WM_SETCURSOR: return DoWM_SETCURSOR(hWnd, message, wParam, lParam);
case WM_NCMOUSEMOVE: {this->SetTheCursor(m_Arrow); return 0l; }
case WM_LBUTTONDBLCLK: return DoWM_LBUTTONDBLCLK(hWnd, message, wParam, lParam);
case EM_UNDO:
case WM_UNDO: return EditUndo();
case EM_CANUNDO: return CanUndo();
case WM_COPY: return DoWM_COPY(hWnd, message, wParam, lParam);
case WM_PASTE: return DoWM_PASTE(hWnd, message, wParam, lParam);
case WM_CUT: return DoWM_CUT(hWnd, message, wParam, lParam);
case WM_CLEAR: { AddUndoableContent(L""); return 0l; }
case EM_SETREADONLY: return DoEM_SETREADONLY(hWnd, message, wParam, lParam);
case EM_EMPTYUNDOBUFFER: return DoEM_EMPTYUNDOBUFFER(hWnd, message, wParam, lParam);
case WM_SETTEXT: return DoWM_SETTEXT(hWnd, message, wParam, lParam);
case EM_REPLACESEL: return DoEM_REPLACESEL(hWnd, message, wParam, lParam);
case EM_FMTLINES: return 0l;
case WM_SETFONT: return DoWM_SETFONT(hWnd, message, wParam, lParam);
}
//is it a user message used for specific features
if (message >= WM_USER && message <= 0x7FFF) return DoWM_USER(hWnd, message, wParam, lParam);
//default to original function
return CallOrigWindowProc(hWnd, message, wParam, lParam);
};
LRESULT HookedCtrl::DoWM_CHAR(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (m_IsReadOnly) return 0l;
//control + any key results in wierd character being caught. like ctlr + 1 sends char 0
if (KEY_STATE_DOWN(VK_CONTROL)) return 0l;
//Character input event, basically typing text
if (wParam == 13)
{
if (!m_WordListDlg.IsVisible())
{
TCHAR CharSent[] = { 13, 10, 0 };
this->AddUndoableContent(CharSent,0,0, message, wParam, lParam);
}
}
else if (wParam >= 32) {
TCHAR CharSent[] = { (TCHAR)wParam, 0 };
if (m_InsertMode == 0 && m_SelEnd == m_SelStart)
{
m_ScrollCaretEnabled = false;
RedrawStop();
wstring t = GetSelText(m_SelStart, m_SelStart + 1);
if (t.length())
{
if(t.c_str()[0] != '\r')
SendMessage(hWnd, EM_SETSEL, m_SelStart, m_SelStart + 1);
}
RedrawResume();
m_ScrollCaretEnabled = true;
}
this->AddUndoableContent(CharSent, 0, 0, message, wParam, lParam);
}
return 0l;
}
LRESULT HookedCtrl::DoWM_SIZE(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//On resize, prevent flicker.
RedrawStop();
LRESULT r = CallOrigWindowProc(hWnd, message, wParam, lParam);
RedrawResume();
m_SearchDlg.UpdatePosition();
m_WordListDlg.UpdatePosition();
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE);
UpdateWindow(m_hWnd);
return r;
};
LRESULT HookedCtrl::DoEM_SETSEL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//On setsel, prevent flicker and remember the selection, so I always have a valid m_SelEnd, which will be the caret position
RedrawStop();
//this->InvalidateSelectRegion();
LRESULT r = CallOrigWindowProc(hWnd, message, wParam, lParam);
//remember
auto oldstart = m_SelStart;
auto oldend = m_SelEnd;
//If the start is 0 and the end is 1, all the text in the edit control is selected.If the start is 1, any current selection is deselected.
if ((LONG)wParam == -1 || (LONG)lParam == -1)
{
DirectSend(EM_GETSEL, (WPARAM)&m_SelStart, (LPARAM) &m_SelEnd);
}
else
{
m_SelStart = (LONG)wParam;
m_SelEnd = (LONG)lParam;
}
//check if edit box is trying to swap sel end and sel start on us. so sel end stays the cursor
if (oldstart == m_SelEnd && oldend == m_SelStart) swap(m_SelStart, m_SelEnd);
RedrawResume();
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
//UpdateWindow(m_hWnd);
return r;
}
LRESULT HookedCtrl::DoEM_GETSEL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//Initially used. May need it later, but now basically it's the original function
return CallOrigWindowProc(hWnd, message, wParam, lParam);
}
LRESULT HookedCtrl::DoWM_KILLFOCUS(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//Kill focus, prevent flicker
RedrawStop();
CaretOn = false;
LRESULT r = CallOrigWindowProc(hWnd, message, wParam, lParam);
RedrawResume();
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
//UpdateWindow(m_hWnd);
return r;
}
LRESULT HookedCtrl::DoWM_CUT(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (m_IsReadOnly) return 0l;
DirectSend(WM_COPY, 0, 0);
this->AddUndoableContent(L"",true);
return 0l;
}
LRESULT HookedCtrl::DoWM_COPY(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return DirectSend(WM_COPY, 0, 0);
}
LRESULT HookedCtrl::DoWM_PASTE(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (m_IsReadOnly) return 0l;
//TODO, Complete implementation
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return 0l;
if (!OpenClipboard(m_hWnd)) return 0l;
HGLOBAL hglb;
TCHAR* lptstr;
hglb = GetClipboardData(CF_UNICODETEXT);
if (hglb != NULL)
{
lptstr = (TCHAR*)GlobalLock(hglb);
if (lptstr != NULL)
{
// Call the application-defined ReplaceSelection
// function to insert the text and repaint the
// window.
this->AddUndoableContent(lptstr, true);
GlobalUnlock(hglb);
}
}
CloseClipboard();
return 0l;
}
LRESULT HookedCtrl::DoWM_LBUTTONDBLCLK(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//short xPos = GET_X_LPARAM(lParam);
//short yPos = GET_Y_LPARAM(lParam);
if (m_CurrentCursor == m_IBeam)
{
SetFocus(hWnd);
SetCapture(hWnd);
TurnCaretOn();
m_WordSelectMode = true;
//Whole word double click. use the word select function for iitial word
RedrawStop();
m_ScrollCaretEnabled = false;
//Dont grab the character, use the one we got on lbutton down
//auto pos = SafeCharFromPos(LOWORD(lParam), HIWORD(lParam));
//m_OldMouseChar = (LONG)pos;
//m_SelEnd = pos;
float mx = LOWORD(lParam);
float cx = LOWORD(DirectSend(EM_POSFROMCHAR,m_SelEnd,0));
//I spend a lot of tine in the little tweak that matter.
//choose the direction of the select word based on caret x vs mouse x, solving the select on the left bias
if (mx >= cx)
{
CaretMoveWord(1, KEY_STATE_DOWN(VK_SHIFT));
CaretMoveWord(-1, 1);
}
else
{
CaretMoveWord(-1, KEY_STATE_DOWN(VK_SHIFT));
CaretMoveWord(1, 1);
}
if (KEY_STATE_DOWN(VK_SHIFT) == 0)
{
m_LineStartSelStart = m_SelStart;
m_LineStartSelEnd = m_SelEnd;
}
RedrawResume();
m_ScrollCaretEnabled = true;
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
UpdateWindow(m_hWnd);
}
return 0l;
}
LRESULT HookedCtrl::DoWM_LBUTTONDOWN(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
SetFocus(hWnd);
SetCapture(hWnd);
TurnCaretOn();
//short xPos = GET_X_LPARAM(lParam);
//short yPos = GET_Y_LPARAM(lParam);
if (m_CurrentCursor == m_IBeam)
{
//Regular mode Select the clicked character for sel start and sel end
RedrawStop();
auto pos = SafeCharFromPos(LOWORD(lParam), HIWORD(lParam));
m_OldMouseChar = (LONG)pos;
bool DoWordListSelect = false;
if (KEY_STATE_DOWN(VK_SHIFT))
SendMessage(hWnd, EM_SETSEL, m_SelStart, pos);
else
{
SendMessage(hWnd, EM_SETSEL, pos, pos);
DoWordListSelect = true;
}
m_LineStartSelStart = m_SelEnd; //not a typo
m_LineStartSelEnd = m_SelEnd;
RedrawResume();
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);
}
else if (m_CurrentCursor == m_RightArrow)
{
//Line number clicked mode, select the line by using the Home/End code
RedrawStop();
m_ScrollCaretEnabled = false;
auto pos = SafeCharFromPos(LOWORD(lParam), HIWORD(lParam));
m_OldMouseChar = (LONG)pos;
m_SelEnd = pos;
CaretMoveEndOfLine(-1, 0, KEY_STATE_DOWN(VK_SHIFT));
CaretMoveEndOfLine(1, 0, 1);
if (KEY_STATE_DOWN(VK_SHIFT) == 0)
{
m_LineStartSelStart = m_SelStart;
m_LineStartSelEnd = m_SelEnd;
}
RedrawResume();
//scroll to left edge
DirectSend(WM_HSCROLL, MAKEWPARAM(SB_LEFT, 0), 0);
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
m_ScrollCaretEnabled =true;
//UpdateWindow(m_hWnd);
}
return 0l;
}
LRESULT HookedCtrl::DoWM_LBUTTONUP(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//Done mode down action, release capture and refresh
if (GetCapture() == hWnd) ReleaseCapture();
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE);
m_WordSelectMode = false;
//UpdateWindow(m_hWnd);
return 0l;
}
LRESULT HookedCtrl::DoWM_SETCURSOR(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (m_SearchDlg.IsVisible()) return 0l;
if (m_WordListDlg.IsVisible()) return 0l;
//My set cursor, set the cursor to active one
SetCursor(m_CurrentCursor);
return 1L;
}
LRESULT HookedCtrl::DoWM_MOUSEMOVE(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//Mouse hover, set the mouse cursor
if (GetCapture() != hWnd)
{
short x = LOWORD(lParam);
short y = HIWORD(lParam);
if(m_SearchDlg.IsPtOverDlg({x,y})) return 0l;
if (m_WordListDlg.IsPtOverDlg({ x,y })) return 0l;
RECT ClientRect; GetClientRect(hWnd, &ClientRect);
if (x < m_Margin) //over line number margin, use my right arrow
SetTheCursor(m_RightArrow);
else if (PtInRect(&ClientRect, { x,y }))
SetTheCursor(m_IBeam);
else
SetTheCursor(m_Arrow);
}
else if ((wParam & MK_LBUTTON) == MK_LBUTTON)
{
//TODO Left Right Scrolling
//Dragging the mouse
RECT ClientRect; GetClientRect(hWnd, &ClientRect);
bool Scrolled = true;
short xPos = GET_X_LPARAM(lParam);
short yPos = GET_Y_LPARAM(lParam);
RedrawStop();
//scroll if dragging outsite by amount according to distance from edge and relative size of the text
if (yPos < -m_textMetrics.tmHeight / 2)
{
LONG OffSet = max(1, -yPos / m_textMetrics.tmHeight); //scroll by distance from edge
repeat(OffSet)
{
DirectSend(WM_VSCROLL, MAKEWPARAM(SB_LINEUP, 0), 0);
}
Scrolled = true;
}
else if (yPos > ClientRect.bottom + m_textMetrics.tmHeight / 2) //scroll by n distance from edge
{
LONG OffSet = max(1, (yPos - ClientRect.bottom) / m_textMetrics.tmHeight);
repeat(OffSet)
{
DirectSend(WM_VSCROLL, MAKEWPARAM(SB_LINEDOWN, 0), 0);
}
Scrolled = true;
}
//cap the coord for character finding so it's always valid coord inside rect
xPos = (short)max(m_Margin + 1, min(xPos, ClientRect.right - 1));
yPos = (short)max(1, min(yPos, ClientRect.bottom - m_textMetrics.tmHeight)); // bottom may have half skipped lines, this ensures coord is on the last line
//get the pos of the caharcter under mouse
auto pos = SafeCharFromPos(xPos,yPos);
//if it's not the same as initial or last move
if ((LONG) pos != m_OldMouseChar)
{
if (m_CurrentCursor == m_IBeam && m_WordSelectMode == false) //is selecting from text window
{
//just set the end pos to character under mouse
if (m_SelEnd != pos)
{
SendMessage(hWnd, EM_SETSEL, m_SelStart, pos);
RedrawResume();
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE);
//UpdateWindow(m_hWnd);
}
}
else if (m_CurrentCursor == m_IBeam && m_WordSelectMode == true) //if word selectiong mode
{
//With the character under mouse, set as caret position select and perform home/end operation
//to select the next word
m_ScrollCaretEnabled = false; //disable the ScrollToCaret code because we will hug the left side
//This crazy code looks at the 4 possible options for the next character selection,
//using the word search function from the mouse position and then looking back from the end of the word
//and doing this in both directions results in 4 options possibile, even if in most cases some may be the same
//Not going to explain it because that would only cause your brain to tilt. This code ivolved from the commented out
//code still seen which worked most the time but was causing weirdism in cases with conflicting stop chars
//////////////////////////
//Options looking right and back
m_SelEnd = pos;
CaretMoveWord(1, 1);
LONG Option1 = m_SelEnd;
CaretMoveWord(-1, 1);
LONG Option2 = m_SelEnd;
//Options looking left and back
m_SelEnd = pos;
CaretMoveWord(-1, 1);
LONG Option3 = m_SelEnd;
CaretMoveWord(1, 1);
LONG Option4 = m_SelEnd;
//and a quick 4 option sort to figure out what option is closest to the character under the mouse
if (abs(pos - Option3) > abs(pos - Option4)) //swap 4 and 4 if needed
{
Option3 = Option4;
}
if (abs(pos - Option1) > abs(pos - Option2)) //swap 1 and 2 if needed
{
Option1 = Option2;
}
if (abs(pos - Option1) > abs(pos - Option3)) //swap 1 and 3, resulting in 1 as closest option
{
Option1 = Option3;
}
m_SelEnd = Option1;
////////////////////
{
//m_SelEnd = pos
if (pos <= (m_LineStartSelStart + m_LineStartSelEnd) / 2) //home end operation differs if selecting upwards and downwards from initial position
{
m_SelStart = m_LineStartSelEnd;
if (m_SelEnd == m_LineStartSelEnd) m_SelEnd = m_LineStartSelStart;
//CaretMoveWord(1, 1);
//CaretMoveWord(-1, 1);
}
else
{
m_SelStart = m_LineStartSelStart;
if (m_SelEnd == m_LineStartSelStart) m_SelEnd = m_LineStartSelEnd;
//CaretMoveWord(-1, 1);
//CaretMoveWord(1, 1);
}
SendMessage(hWnd, EM_SETSEL, m_SelStart, m_SelEnd);
}
RedrawResume();
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE);
m_ScrollCaretEnabled = true;
//hack, if the prev char is a tab, force a OldChar to stay what it was because the select gets stuck a wee bit from it's position being too far
//if (m_CaretChar != '\t' && m_CaretPrevChar == '\t') pos = m_OldMouseChar;
//UpdateWindow(m_hWnd);
}
else if (m_CurrentCursor == m_RightArrow) //if selectiong from line number bar
{
//With the character under mouse, set as caret position select and perform home/end operation
m_ScrollCaretEnabled = false; //disable the ScrollToCaret code because we will hug the left side
m_SelEnd = pos;
if (pos < m_LineStartSelStart) //home end operation differs if selecting upwards and downwards from initial position
{
m_SelStart = m_LineStartSelEnd;
CaretMoveEndOfLine(1, 0, 1);
CaretMoveEndOfLine(-1, 0, 1);
}
else
{
m_SelStart = m_LineStartSelStart;
CaretMoveEndOfLine(-1, 0, 1);
CaretMoveEndOfLine(1, 0, 1);
}
RedrawResume();
DirectSend(WM_HSCROLL, MAKEWPARAM(SB_LEFT, 0), 0);
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE);
m_ScrollCaretEnabled = true;
//UpdateWindow(m_hWnd);
}
}
m_OldMouseChar = (LONG) pos;
RedrawResume(); //just in case we did not branch to a resume drawing branch
if (Scrolled) //we did some scrolling
{
UpdateWindow(m_hWnd); //(a) when this happens the mouse move will keep posting...
}
if (Scrolled && OldMousePos == lParam) //if scrolling and not wiggling the mouse for faster scrolling
{
//delay
Sleep(100); //(a)...which allows controlling the scroll speed without using sleep instead of a timer, weird hack discovered by accident
}
OldMousePos = lParam;
}
return 0;
}
LRESULT HookedCtrl::DoWM_TIMER(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//because the caret would stop blinking and disapear after 3 blinks... in case you are wondering why I took over this control
if (wParam == m_CaretTimer)
{
if (GetFocus() == hWnd)
{
//Fucking caret fuck you I'll draw you myself
//Later: I decided to leave that comment in there :)
//Turns out the default caret uses the back color of the control as caret color
//and does xor with this color with the text box image... long story short, when the caret would be Off on the dark skin
//it would appear ON on the dark skin... and when ON it would disapear....
//... and the work around for this falls in the hands of the user of the edit box which is asking for trouble
//so the cursor is now rendered in the draw, it costs a little more but I have full control of it, which is required
//to match the color settingS defined in the language file
//On Hook the caret is destroyed and on unhook it is recreated. carret functions still work though I replaced a few behaviors
CaretOn = !CaretOn;
if (m_NumCaretTicks>0) CaretOn = true;
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);
//UpdateWindow(m_hWnd);
//if (CaretOn || m_NumCaretTicks >0)
// CaretOn
//HideCaret(hWnd);
//else
//ShowCaret(hWnd);
//SetCaretBlinkTime(500);
}
else if (CaretOn)
{
CaretOn = false;
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE);