-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
WinGui.cpp
4172 lines (3616 loc) · 120 KB
/
WinGui.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
/* Copyright 2022 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */
#include "utils/BaseUtil.h"
#include "utils/ScopedWin.h"
#include "utils/BitManip.h"
#include "utils/WinUtil.h"
#include "utils/Dpi.h"
#include "utils/WinDynCalls.h"
#include "wingui/UIModels.h"
#include "wingui/Layout.h"
#include "wingui/WinGui.h"
#include "Theme.h"
#include "webview2.h"
#include "utils/Log.h"
Kind kindWnd = "wnd";
static LONG gSubclassId = 0;
UINT_PTR NextSubclassId() {
LONG res = InterlockedIncrement(&gSubclassId);
return (UINT_PTR)res;
}
#define WIN_MESSAGES(V) \
V(WM_CREATE) \
V(WM_DESTROY) \
V(WM_MOVE) \
V(WM_SIZE) \
V(WM_ACTIVATE) \
V(WM_SETFOCUS) \
V(WM_KILLFOCUS) \
V(WM_ENABLE) \
V(WM_SETREDRAW) \
V(WM_SETTEXT) \
V(WM_GETTEXT) \
V(WM_GETTEXTLENGTH) \
V(WM_PAINT) \
V(WM_CLOSE) \
V(WM_QUERYENDSESSION) \
V(WM_QUERYOPEN) \
V(WM_ENDSESSION) \
V(WM_QUIT) \
V(WM_ERASEBKGND) \
V(WM_SYSCOLORCHANGE) \
V(WM_SHOWWINDOW) \
V(WM_WININICHANGE) \
V(WM_SETTINGCHANGE) \
V(WM_DEVMODECHANGE) \
V(WM_ACTIVATEAPP) \
V(WM_FONTCHANGE) \
V(WM_TIMECHANGE) \
V(WM_CANCELMODE) \
V(WM_SETCURSOR) \
V(WM_MOUSEACTIVATE) \
V(WM_CHILDACTIVATE) \
V(WM_QUEUESYNC) \
V(WM_GETMINMAXINFO) \
V(WM_PAINTICON) \
V(WM_ICONERASEBKGND) \
V(WM_NEXTDLGCTL) \
V(WM_SPOOLERSTATUS) \
V(WM_DRAWITEM) \
V(WM_MEASUREITEM) \
V(WM_DELETEITEM) \
V(WM_VKEYTOITEM) \
V(WM_CHARTOITEM) \
V(WM_SETFONT) \
V(WM_GETFONT) \
V(WM_SETHOTKEY) \
V(WM_GETHOTKEY) \
V(WM_QUERYDRAGICON) \
V(WM_COMPAREITEM) \
V(WM_GETOBJECT) \
V(WM_COMPACTING) \
V(WM_COMMNOTIFY) \
V(WM_WINDOWPOSCHANGING) \
V(WM_WINDOWPOSCHANGED) \
V(WM_POWER) \
V(WM_COPYDATA) \
V(WM_CANCELJOURNAL) \
V(WM_NOTIFY) \
V(WM_INPUTLANGCHANGEREQUEST) \
V(WM_INPUTLANGCHANGE) \
V(WM_TCARD) \
V(WM_HELP) \
V(WM_USERCHANGED) \
V(WM_NOTIFYFORMAT) \
V(WM_CONTEXTMENU) \
V(WM_STYLECHANGING) \
V(WM_STYLECHANGED) \
V(WM_DISPLAYCHANGE) \
V(WM_GETICON) \
V(WM_SETICON) \
V(WM_NCCREATE) \
V(WM_NCDESTROY) \
V(WM_NCCALCSIZE) \
V(WM_NCHITTEST) \
V(WM_NCPAINT) \
V(WM_NCACTIVATE) \
V(WM_GETDLGCODE) \
V(WM_MOUSEMOVE) \
V(WM_LBUTTONDOWN) \
V(WM_LBUTTONUP) \
V(WM_LBUTTONDBLCLK) \
V(WM_RBUTTONDOWN) \
V(WM_RBUTTONUP) \
V(WM_RBUTTONDBLCLK) \
V(WM_MBUTTONDOWN) \
V(WM_MBUTTONUP) \
V(WM_MBUTTONDBLCLK) \
V(WM_MOUSEWHEEL) \
V(WM_XBUTTONDOWN) \
V(WM_XBUTTONUP) \
V(WM_XBUTTONDBLCLK) \
V(WM_MOUSEHWHEEL) \
V(WM_PARENTNOTIFY) \
V(WM_ENTERMENULOOP) \
V(WM_EXITMENULOOP) \
V(WM_NEXTMENU) \
V(WM_SIZING) \
V(WM_CAPTURECHANGED) \
V(WM_MOVING) \
V(TCM_GETITEMCOUNT) \
V(TCM_GETITEMW) \
V(TCM_SETITEMW) \
V(TCM_INSERTITEMW) \
V(TCM_DELETEITEM) \
V(TCM_DELETEALLITEMS) \
V(TCM_GETITEMRECT) \
V(TCM_GETCURSEL) \
V(TCM_SETCURSEL) \
V(TCM_HITTEST) \
V(TCM_SETITEMEXTRA) \
V(TCM_ADJUSTRECT) \
V(TCM_SETITEMSIZE) \
V(TCM_REMOVEIMAGE) \
V(TCM_SETPADDING) \
V(TCM_GETROWCOUNT) \
V(TCM_GETTOOLTIPS) \
V(TCM_SETTOOLTIPS) \
V(TCM_GETCURFOCUS) \
V(TCM_SETCURFOCUS) \
V(TCM_SETMINTABWIDTH) \
V(TCM_DESELECTALL) \
V(TCM_HIGHLIGHTITEM)
#define MSG_ID(id) id,
UINT gWinMessageIDs[] = {WIN_MESSAGES(MSG_ID)};
#undef MSG_ID
#define MSG_NAME(id) #id "\0"
SeqStrings gWinMessageNames = WIN_MESSAGES(MSG_NAME) "\0";
#undef MSG_NAME
const char* WinMsgName(UINT msg) {
int n = dimof(gWinMessageIDs);
for (int i = 0; i < n; i++) {
UINT m = gWinMessageIDs[i];
if (m == msg) {
return seqstrings::IdxToStr(gWinMessageNames, i);
}
}
char* s = str::Format("0x%x", (int)msg);
char* res = str::DupTemp(s);
str::Free(s);
return res;
}
// TODO:
// - if layout is set, do layout on WM_SIZE using LayoutToSize
// window_map.h / window_map.cpp
struct WindowToHwnd {
Wnd* window = nullptr;
HWND hwnd = nullptr;
};
Vec<WindowToHwnd> gWindowToHwndMap;
static Wnd* WindowMapGetWindow(HWND hwnd) {
for (auto& el : gWindowToHwndMap) {
if (el.hwnd == hwnd) {
return el.window;
}
}
return nullptr;
}
static void WindowMapAdd(HWND hwnd, Wnd* w) {
if (!hwnd || (WindowMapGetWindow(hwnd) != nullptr)) {
return;
}
WindowToHwnd el = {w, hwnd};
gWindowToHwndMap.Append(el);
}
/*
static bool WindowMapRemove(HWND hwnd) {
int n = gWindowToHwndMap.isize();
for (int i = 0; i < n; i++) {
auto&& el = gWindowToHwndMap[i];
if (el.hwnd == hwnd) {
gWindowToHwndMap.RemoveAtFast(i);
return true;
}
}
return false;
}
*/
static bool WindowMapRemove(Wnd* w) {
int n = gWindowToHwndMap.isize();
for (int i = 0; i < n; i++) {
auto&& el = gWindowToHwndMap[i];
if (el.window == w) {
gWindowToHwndMap.RemoveAtFast(i);
return true;
}
}
return false;
}
//- Taskbar.cpp
const DWORD WM_TASKBARCALLBACK = WM_APP + 0x15;
const DWORD WM_TASKBARCREATED = ::RegisterWindowMessage(L"TaskbarCreated");
const DWORD WM_TASKBARBUTTONCREATED = ::RegisterWindowMessage(L"TaskbarButtonCreated");
//- Window.h / Window.cpp
const WCHAR* kDefaultClassName = L"SumatraWgDefaultWinClass";
static LRESULT CALLBACK StaticWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
Wnd* window = WindowMapGetWindow(hwnd);
if (msg == WM_NCCREATE) {
CREATESTRUCT* cs = (CREATESTRUCT*)(lparam);
CrashIf(window);
window = (Wnd*)(cs->lpCreateParams);
window->hwnd = hwnd;
WindowMapAdd(hwnd, window);
}
if (window) {
return window->WndProc(hwnd, msg, wparam, lparam);
} else {
return ::DefWindowProc(hwnd, msg, wparam, lparam);
}
}
static LRESULT CALLBACK StaticWindowProcSubclassed(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, UINT_PTR subclassId,
DWORD_PTR data) {
return StaticWindowProc(hwnd, msg, wp, lp);
}
Wnd::Wnd() {
// instance = GetModuleHandleW(nullptr);
kind = kindWnd;
}
Wnd::~Wnd() {
Destroy();
delete layout;
DeleteBrush(backgroundColorBrush);
}
Kind Wnd::GetKind() {
return kind;
}
void Wnd::SetText(const WCHAR* s) {
CrashIf(!hwnd);
HwndSetText(hwnd, s);
HwndInvalidate(hwnd); // TODO: move inside HwndSetText()?
}
void Wnd::SetText(const char* s) {
if (!s) {
s = "";
}
HwndSetText(hwnd, s);
HwndInvalidate(hwnd); // TODO: move inside HwndSetText()?
}
TempStr Wnd::GetTextTemp() {
char* s = HwndGetTextTemp(hwnd);
return s;
}
void Wnd::SetVisibility(Visibility newVisibility) {
CrashIf(!hwnd);
visibility = newVisibility;
bool isVisible = IsVisible();
// TODO: a different way to determine if is top level vs. child window?
if (GetParent(hwnd) == nullptr) {
::ShowWindow(hwnd, isVisible ? SW_SHOW : SW_HIDE);
} else {
BOOL bIsVisible = toBOOL(isVisible);
SetWindowStyle(hwnd, WS_VISIBLE, bIsVisible);
}
}
Visibility Wnd::GetVisibility() {
return visibility;
#if 0
if (GetParent(hwnd) == nullptr) {
// TODO: what to do for top-level window?
CrashMe();
return true;
}
bool isVisible = IsWindowStyleSet(hwnd, WS_VISIBLE);
return isVisible;
#endif
}
void Wnd::SetIsVisible(bool isVisible) {
SetVisibility(isVisible ? Visibility::Visible : Visibility::Collapse);
}
bool Wnd::IsVisible() const {
return visibility == Visibility::Visible;
}
void Wnd::Destroy() {
HwndDestroyWindowSafe(&hwnd);
UnSubclass();
Cleanup();
}
LRESULT Wnd::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
return WndProcDefault(hwnd, msg, wparam, lparam);
}
// This function is called when a window is attached to Wnd.
// Override it to automatically perform tasks when the window is attached.
// Note: Window controls are attached.
void Wnd::OnAttach() {
}
// Override this to handle WM_COMMAND messages
bool Wnd::OnCommand(WPARAM wparam, LPARAM lparam) {
// UINT id = LOWORD(wparam);
// switch (id)
// {
// case IDM_FILE_NEW:
// OnFileNew();
// return true; // return TRUE for handled commands
// }
// return false for unhandled commands
return false;
}
// Called during window creation. Override this functions to perform tasks
// such as creating child windows.
int Wnd::OnCreate(CREATESTRUCT*) {
// This function is called when a WM_CREATE message is received
// Override it to automatically perform tasks during window creation.
// Return 0 to continue creating the window.
// Note: Window controls don't call OnCreate. They are sublcassed (attached)
// after their window is created.
/*
LOGFONT logfont;
::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(logfont), &logfont);
font = ::CreateFontIndirect(&logfont);
::SendMessage(hwnd, WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE);
*/
return 0;
}
// This function is called when a window is destroyed.
// Override it to do additional tasks, such as ending the application
// with PostQuitMessage.
void Wnd::OnDestroy() {
}
// Called when the background of the window's client area needs to be erased.
// Override this function in your derived class to perform drawing tasks.
// Return Value: Return FALSE to also permit default erasure of the background
// Return TRUE to prevent default erasure of the background
bool Wnd::OnEraseBkgnd(HDC) {
return false;
}
// Called in response to WM_CLOSE, before the window is destroyed.
// Override this function to suppress destroying the window.
// WM_CLOSE is sent by SendMessage(WM_CLOSE, 0, 0) or by clicking X
// in the top right corner.
// Child windows don't receive WM_CLOSE unless they are closed using
// the Close function.
void Wnd::OnClose() {
Destroy();
}
void Wnd::OnContextMenu(Point ptScreen) {
if (!onContextMenu) {
return;
}
// https://docs.microsoft.com/en-us/windows/win32/menurc/wm-contextmenu
ContextMenuEvent ev;
ev.w = this;
ev.mouseScreen = ptScreen;
POINT ptW = ToPOINT(ptScreen);
if (ptScreen.x != -1) {
MapWindowPoints(HWND_DESKTOP, hwnd, &ptW, 1);
}
ev.mouseWindow.x = ptW.x;
ev.mouseWindow.y = ptW.y;
onContextMenu(&ev);
}
void Wnd::OnDropFiles(HDROP drop_info) {
}
void Wnd::OnGetMinMaxInfo(MINMAXINFO* mmi) {
}
LRESULT Wnd::OnMouseEvent(UINT msg, WPARAM wparam, LPARAM lparam) {
return -1;
}
void Wnd::OnMove(LPPOINTS pts) {
}
// Processes notification (WM_NOTIFY) messages from a child window.
LRESULT Wnd::OnNotify(int controlId, NMHDR* nmh) {
// You can use either OnNotifyReflect or OnNotify to handle notifications
// Override OnNotifyReflect to handle notifications in the CWnd class that
// generated the notification. OR
// Override OnNotify to handle notifications in the PARENT of the CWnd class
// that generated the notification.
// Your overriding function should look like this ...
// LPNMHDR pHeader = reinterpret_cast<LPNMHDR>(lparam);
// switch (pHeader->code)
// {
// Handle your notifications from the CHILD window here
// Return the value recommended by the Windows API documentation.
// For many notifications, the return value doesn't matter, but for some it does.
// }
// return 0 for unhandled notifications
// The framework will call SetWindowLongPtr(DWLP_MSGRESULT, result) for dialogs.
return 0;
}
// Processes the notification (WM_NOTIFY) messages in the child window that originated them.
LRESULT Wnd::OnNotifyReflect(WPARAM, LPARAM) {
// Override OnNotifyReflect to handle notifications in the CWnd class that
// generated the notification.
// Your overriding function should look like this ...
// LPNMHDR pHeader = reinterpret_cast<LPNMHDR>(lparam);
// switch (pHeader->code)
// {
// Handle your notifications from this window here
// Return the value recommended by the Windows API documentation.
// }
// Return 0 for unhandled notifications.
// The framework will call SetWindowLongPtr(DWLP_MSGRESULT, result) for dialogs.
return 0;
}
void Wnd::OnPaint(HDC hdc, PAINTSTRUCT* ps) {
auto bgBrush = backgroundColorBrush;
if (bgBrush != nullptr) {
FillRect(hdc, &ps->rcPaint, bgBrush);
}
}
void Wnd::OnSize(UINT msg, UINT type, SIZE size) {
}
void Wnd::OnTaskbarCallback(UINT msg, LPARAM lparam) {
}
void Wnd::OnTimer(UINT_PTR event_id) {
}
void Wnd::OnWindowPosChanging(WINDOWPOS* window_pos) {
}
// This function processes those special messages sent by some older controls,
// and reflects them back to the originating CWnd object.
// Override this function in your derived class to handle these special messages:
// WM_COMMAND, WM_CTLCOLORBTN, WM_CTLCOLOREDIT, WM_CTLCOLORDLG, WM_CTLCOLORLISTBOX,
// WM_CTLCOLORSCROLLBAR, WM_CTLCOLORSTATIC, WM_CHARTOITEM, WM_VKEYTOITEM,
// WM_HSCROLL, WM_VSCROLL, WM_DRAWITEM, WM_MEASUREITEM, WM_DELETEITEM,
// WM_COMPAREITEM, WM_PARENTNOTIFY.
LRESULT Wnd::OnMessageReflect(UINT, WPARAM, LPARAM) {
// This function processes those special messages (see above) sent
// by some older controls, and reflects them back to the originating CWnd object.
// Override this function in your derived class to handle these special messages.
// Your overriding function should look like this ...
// switch (msg)
// {
// Handle your reflected messages here
// }
// return 0 for unhandled messages
return 0;
}
Size Wnd::GetIdealSize() {
return {};
}
Size Wnd::Layout(const Constraints bc) {
dbglayoutf("WindowBase::Layout() %s ", GetKind());
LogConstraints(bc, "\n");
auto hinset = insets.left + insets.right;
auto vinset = insets.top + insets.bottom;
auto innerConstraints = bc.Inset(hinset, vinset);
int dx = MinIntrinsicWidth(0);
int dy = MinIntrinsicHeight(0);
childSize = innerConstraints.Constrain(Size{dx, dy});
auto res = Size{
childSize.dx + hinset,
childSize.dy + vinset,
};
return res;
}
int Wnd::MinIntrinsicHeight(int) {
#if 0
auto vinset = insets.top + insets.bottom;
Size s = GetIdealSize();
return s.dy + vinset;
#else
Size s = GetIdealSize();
return s.dy;
#endif
}
int Wnd::MinIntrinsicWidth(int) {
#if 0
auto hinset = insets.left + insets.right;
Size s = GetIdealSize();
return s.dx + hinset;
#else
Size s = GetIdealSize();
return s.dx;
#endif
}
void Wnd::Close() {
CrashIf(!::IsWindow(hwnd));
PostMessageW(hwnd, WM_CLOSE, 0, 0);
}
void Wnd::SetPos(RECT* r) {
::MoveWindow(hwnd, r);
}
void Wnd::SetBounds(Rect bounds) {
dbglayoutf("WindowBaseLayout:SetBounds() %s %d,%d - %d, %d\n", GetKind(), bounds.x, bounds.y, bounds.dx, bounds.dy);
lastBounds = bounds;
bounds.x += insets.left;
bounds.y += insets.top;
bounds.dx -= (insets.right + insets.left);
bounds.dy -= (insets.bottom + insets.top);
auto r = RectToRECT(bounds);
::MoveWindow(hwnd, &r);
// TODO: optimize if doesn't change position
::InvalidateRect(hwnd, nullptr, TRUE);
}
// A function used internally to call OnMessageReflect. Don't call or override this function.
LRESULT Wnd::MessageReflect(UINT msg, WPARAM wparam, LPARAM lparam) {
HWND wnd = 0;
switch (msg) {
case WM_COMMAND:
case WM_CTLCOLORBTN:
case WM_CTLCOLOREDIT:
case WM_CTLCOLORDLG:
case WM_CTLCOLORLISTBOX:
case WM_CTLCOLORSCROLLBAR:
case WM_CTLCOLORSTATIC:
case WM_CHARTOITEM:
case WM_VKEYTOITEM:
case WM_HSCROLL:
case WM_VSCROLL:
wnd = reinterpret_cast<HWND>(lparam);
break;
case WM_DRAWITEM:
case WM_MEASUREITEM:
case WM_DELETEITEM:
case WM_COMPAREITEM:
wnd = GetDlgItem(hwnd, static_cast<int>(wparam));
break;
case WM_PARENTNOTIFY:
switch (LOWORD(wparam)) {
case WM_CREATE:
case WM_DESTROY:
wnd = reinterpret_cast<HWND>(lparam);
break;
}
}
if (!wnd) {
return 0;
}
Wnd* pWnd = WindowMapGetWindow(wnd);
if (pWnd != nullptr) {
auto res = pWnd->OnMessageReflect(msg, wparam, lparam);
return res;
}
return 0;
}
// for interop with windows not wrapped in Wnd, run this at the beginning of message loop
LRESULT TryReflectMessages(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
// hwnd is a parent of control sending WM_NOTIFY message
switch (msg) {
case WM_COMMAND: {
// Reflect this message if it's from a control.
Wnd* pWnd = WindowMapGetWindow(reinterpret_cast<HWND>(lparam));
bool didHandle = false;
if (pWnd != nullptr) {
didHandle = pWnd->OnCommand(wparam, lparam);
}
if (didHandle) {
return 1;
}
} break; // Note: Some MDI commands require default processing.
case WM_NOTIFY: {
// Do notification reflection if message came from a child window.
// Restricting OnNotifyReflect to child windows avoids double handling.
NMHDR* hdr = reinterpret_cast<LPNMHDR>(lparam);
HWND from = hdr->hwndFrom;
Wnd* wndFrom = WindowMapGetWindow(from);
if (!wndFrom) {
return 0;
}
if (hwnd == GetParent(wndFrom->hwnd)) {
return wndFrom->OnNotifyReflect(wparam, lparam);
}
}
// A set of messages to be reflected back to the control that generated them.
case WM_CTLCOLORBTN:
case WM_CTLCOLOREDIT:
case WM_CTLCOLORDLG:
case WM_CTLCOLORLISTBOX:
case WM_CTLCOLORSCROLLBAR:
case WM_CTLCOLORSTATIC:
case WM_DRAWITEM:
case WM_MEASUREITEM:
case WM_DELETEITEM:
case WM_COMPAREITEM:
case WM_CHARTOITEM:
case WM_VKEYTOITEM:
case WM_HSCROLL:
case WM_VSCROLL:
case WM_PARENTNOTIFY: {
Wnd* pWnd = WindowMapGetWindow(reinterpret_cast<HWND>(lparam));
LRESULT result = 0;
if (pWnd != nullptr) {
result = pWnd->MessageReflect(msg, wparam, lparam);
}
if (result != 0) {
return result; // Message processed so return.
}
} break; // Do default processing when message not already processed.
}
return 0;
}
LRESULT Wnd::WndProcDefault(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
LRESULT result = 0;
switch (msg) {
case WM_CLOSE: {
OnClose();
return 0;
}
// windows don't support WM_GETFONT / WM_SETFONT
// only controls do. not sure if we won't interfere
// with control handling
// TODO: maybe when font is nullptr, ask the original proc
case WM_GETFONT: {
return (LRESULT)font;
}
case WM_SETFONT: {
font = (HFONT)wparam;
return 0;
}
case WM_COMMAND: {
// Reflect this message if it's from a control.
Wnd* pWnd = WindowMapGetWindow(reinterpret_cast<HWND>(lparam));
bool didHandle = false;
if (pWnd != nullptr) {
didHandle = pWnd->OnCommand(wparam, lparam);
}
// Handle user commands.
if (!didHandle) {
didHandle = OnCommand(wparam, lparam);
}
if (didHandle) {
return 0;
}
} break; // Note: Some MDI commands require default processing.
case WM_CREATE: {
OnCreate((CREATESTRUCT*)lparam);
break;
}
case WM_DESTROY: {
OnDestroy();
break; // Note: Some controls require default processing.
}
case WM_NOTIFY: {
// Do notification reflection if message came from a child window.
// Restricting OnNotifyReflect to child windows avoids double handling.
NMHDR* hdr = reinterpret_cast<NMHDR*>(lparam);
HWND from = hdr->hwndFrom;
Wnd* wndFrom = WindowMapGetWindow(from);
if (wndFrom != nullptr) {
if (::GetParent(from) == this->hwnd) {
result = wndFrom->OnNotifyReflect(wparam, lparam);
}
}
// Handle user notifications
if (result == 0) {
result = OnNotify((int)wparam, (NMHDR*)lparam);
}
if (result != 0) {
return result;
}
break;
}
case WM_PAINT: {
if (subclassId) {
// Allow window controls to do their default drawing.
return FinalWindowProc(msg, wparam, lparam);
}
if (::GetUpdateRect(hwnd, nullptr, FALSE)) {
PAINTSTRUCT ps;
HDC hdc = ::BeginPaint(hwnd, &ps);
OnPaint(hdc, &ps);
::EndPaint(hwnd, &ps);
} else {
// TODO: for now those are the same because LabelWithCloseWnd::OnPaint
// assumes ps is provided (and maybe others)
PAINTSTRUCT ps;
HDC hdc = ::BeginPaint(hwnd, &ps);
OnPaint(hdc, &ps);
::EndPaint(hwnd, &ps);
#if 0
HDC hdc = ::GetDC(hwnd);
OnPaint(hdc, nullptr);
::ReleaseDC(hwnd, hdc);
#endif
}
// No more drawing required
return 0;
}
case WM_ERASEBKGND: {
HDC dc = (HDC)(wparam);
BOOL preventErasure;
preventErasure = OnEraseBkgnd(dc);
if (preventErasure)
return TRUE;
} break;
// A set of messages to be reflected back to the control that generated them.
case WM_CTLCOLORBTN:
case WM_CTLCOLOREDIT:
case WM_CTLCOLORDLG:
case WM_CTLCOLORLISTBOX:
case WM_CTLCOLORSCROLLBAR:
case WM_CTLCOLORSTATIC:
case WM_DRAWITEM:
case WM_MEASUREITEM:
case WM_DELETEITEM:
case WM_COMPAREITEM:
case WM_CHARTOITEM:
case WM_VKEYTOITEM:
case WM_HSCROLL:
case WM_VSCROLL:
case WM_PARENTNOTIFY: {
result = MessageReflect(msg, wparam, lparam);
if (result != 0)
return result; // Message processed so return.
} break; // Do default processing when message not already processed.
case WM_DROPFILES: {
OnDropFiles(reinterpret_cast<HDROP>(wparam));
break;
}
case WM_ENTERSIZEMOVE:
case WM_EXITSIZEMOVE: {
SIZE size = {0};
OnSize(msg, 0, size);
break;
}
case WM_GETMINMAXINFO: {
OnGetMinMaxInfo(reinterpret_cast<MINMAXINFO*>(lparam));
break;
}
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
case WM_MOUSEACTIVATE:
case WM_MOUSEHOVER:
case WM_MOUSEHWHEEL:
case WM_MOUSELEAVE:
case WM_MOUSEMOVE:
case WM_MOUSEWHEEL: {
LRESULT lResult = OnMouseEvent(msg, wparam, lparam);
if (lResult != -1)
return lResult;
break;
}
case WM_MOVE: {
POINTS pts = MAKEPOINTS(lparam);
OnMove(&pts);
break;
}
case WM_CONTEXTMENU: {
Point ptScreen = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)};
// Note: HWND in wparam might be a child window
OnContextMenu(ptScreen);
break;
}
case WM_SIZE: {
SIZE size = {LOWORD(lparam), HIWORD(lparam)};
OnSize(msg, static_cast<UINT>(wparam), size);
break;
}
case WM_TIMER: {
OnTimer(static_cast<UINT>(wparam));
break;
}
case WM_WINDOWPOSCHANGING: {
OnWindowPosChanging(reinterpret_cast<LPWINDOWPOS>(lparam));
break;
}
default: {
if (msg == WM_TASKBARCREATED || msg == WM_TASKBARBUTTONCREATED || msg == WM_TASKBARCALLBACK) {
OnTaskbarCallback(msg, lparam);
return 0;
}
break;
}
}
// Now hand all messages to the default procedure.
return FinalWindowProc(msg, wparam, lparam);
}
LRESULT Wnd::FinalWindowProc(UINT msg, WPARAM wparam, LPARAM lparam) {
if (subclassId) {
return ::DefSubclassProc(hwnd, msg, wparam, lparam);
} else {
// TODO: also DefSubclassProc?
return ::DefWindowProc(hwnd, msg, wparam, lparam);
}
}
bool Wnd::PreTranslateMessage(MSG& msg) {
return false;
}
void Wnd::Attach(HWND hwnd) {
CrashIf(!IsWindow(hwnd));
CrashIf(WindowMapGetWindow(hwnd));
this->hwnd = hwnd;
Subclass();
OnAttach();
}
// Attaches a CWnd object to a dialog item.
void Wnd::AttachDlgItem(UINT id, HWND parent) {
CrashIf(!::IsWindow(parent));
HWND wnd = ::GetDlgItem(parent, id);
Attach(wnd);
}
HWND Wnd::Detach() {
UnSubclass();
HWND wnd = hwnd;
WindowMapRemove(this);
hwnd = nullptr;
return wnd;
}
void Wnd::Cleanup() {
WindowMapRemove(this);
hwnd = nullptr;
subclassId = 0;
}
static void WndRegisterClass(const WCHAR* className) {
WNDCLASSEX wc = {};
wc.cbSize = sizeof(wc);
HINSTANCE inst = GetInstance();
BOOL ok = ::GetClassInfoExW(inst, className, &wc);
if (ok) {
return;
}
wc = {};
wc.cbSize = sizeof(wc);
wc.style = CS_DBLCLKS;
wc.hInstance = inst;
wc.lpszClassName = className;
wc.lpfnWndProc = StaticWindowProc;
wc.hCursor = ::LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = reinterpret_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
ATOM atom = ::RegisterClassExW(&wc);
CrashIf(!atom);
}
HWND Wnd::CreateControl(const CreateControlArgs& args) {
CrashIf(!args.className);
// TODO: validate that className is one of the known controls?
font = args.font;
if (!font) {
// TODO: need this?
font = GetDefaultGuiFont();
}
DWORD style = args.style;
if (args.parent) {
style |= WS_CHILD;
}
if (args.visible) {
style |= WS_VISIBLE;
} else {
style &= ~WS_VISIBLE;
}
DWORD exStyle = args.exStyle;
const WCHAR* className = args.className;
int x = args.pos.x;
int y = args.pos.y;
int dx = args.pos.dx;
int dy = args.pos.dy;
HWND parent = args.parent;
HMENU id = args.ctrlId;
HINSTANCE inst = GetInstance();
void* createParams = this;
hwnd = ::CreateWindowExW(exStyle, className, L"", style, x, y, dx, dy, parent, id, inst, createParams);
HwndSetFont(hwnd, font);
CrashIf(!hwnd);
// TODO: validate that
Subclass();
OnAttach();
// prevWindowProc(hwnd, WM_SETFONT, (WPARAM)f, 0);
// HwndSetFont(hwnd, f);
if (args.text) {