forked from thennequin/EasyWindow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasyWindowWin32.cpp
865 lines (776 loc) · 25.2 KB
/
EasyWindowWin32.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
#if defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)
#include "EasyWindow.h"
#include <windows.h>
#define EW_OVERRIDE override
class EasyWindowWin32 : public EasyWindow
{
static const char* const c_pClassName;
public:
EasyWindowWin32(const char* pTitle, int iWidth, int iHeight, bool bClientSize, EasyWindow* pParent, EWindowStyle eStyle, EWindowFlags eFlags)
: m_bSizing(false)
, m_iActiveHitMousePosition(HTNOWHERE)
, m_eCursor(EasyWindow::E_CURSOR_ARROW)
{
m_bKeyDownAlt[0] = false;
m_bKeyDownAlt[1] = false;
m_bKeyDownCtrl[0] = false;
m_bKeyDownCtrl[1] = false;
m_bKeyDownShift[0] = false;
m_bKeyDownShift[1] = false;
m_iLastEvents = 0;
if (!s_bClassInitialized)
{
s_bClassInitialized = true;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)EasyWindowWin32::Proc;
//wc.hInstance = hInstance;
wc.hInstance = GetModuleHandle(NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = c_pClassName;
RegisterClassEx(&wc);
for (int i = 0; i < c_iMaxKeys; ++i)
s_iTranslateKeys[i] = KEY_NONE;
s_iTranslateKeys[VK_ESCAPE] = EKey::KEY_ESC;
s_iTranslateKeys[VK_RETURN] = EKey::KEY_RETURN;
s_iTranslateKeys[VK_TAB] = EKey::KEY_TAB;
s_iTranslateKeys[VK_BACK] = EKey::KEY_BACKSPACE;
s_iTranslateKeys[VK_SPACE] = EKey::KEY_SPACE;
s_iTranslateKeys[VK_UP] = EKey::KEY_UP;
s_iTranslateKeys[VK_DOWN] = EKey::KEY_DOWN;
s_iTranslateKeys[VK_LEFT] = EKey::KEY_LEFT;
s_iTranslateKeys[VK_RIGHT] = EKey::KEY_RIGHT;
s_iTranslateKeys[VK_INSERT] = EKey::KEY_INSERT;
s_iTranslateKeys[VK_DELETE] = EKey::KEY_DELETE;
s_iTranslateKeys[VK_HOME] = EKey::KEY_HOME;
s_iTranslateKeys[VK_END] = EKey::KEY_END;
s_iTranslateKeys[VK_PRIOR] = EKey::KEY_PAGEUP;
s_iTranslateKeys[VK_NEXT] = EKey::KEY_PAGEDOWN;
s_iTranslateKeys[VK_SNAPSHOT] = EKey::KEY_PRINT;
s_iTranslateKeys[VK_OEM_PLUS] = EKey::KEY_PLUS;
s_iTranslateKeys[VK_OEM_MINUS] = EKey::KEY_MINUS;
s_iTranslateKeys[VK_OEM_4] = EKey::KEY_LEFTBRACKET;
s_iTranslateKeys[VK_OEM_6] = EKey::KEY_RIGHTBRACKET;
s_iTranslateKeys[VK_OEM_1] = EKey::KEY_SEMICOLON;
s_iTranslateKeys[VK_OEM_7] = EKey::KEY_QUOTE;
s_iTranslateKeys[VK_OEM_COMMA] = EKey::KEY_COMMA;
s_iTranslateKeys[VK_OEM_PERIOD] = EKey::KEY_PERIOD;
s_iTranslateKeys[VK_DECIMAL] = EKey::KEY_PERIOD;
s_iTranslateKeys[VK_OEM_2] = EKey::KEY_SLASH;
s_iTranslateKeys[VK_OEM_5] = EKey::KEY_BACKSLASH;
s_iTranslateKeys[VK_OEM_3] = EKey::KEY_TILDE;
s_iTranslateKeys[VK_F1] = EKey::KEY_F1;
s_iTranslateKeys[VK_F2] = EKey::KEY_F2;
s_iTranslateKeys[VK_F3] = EKey::KEY_F3;
s_iTranslateKeys[VK_F4] = EKey::KEY_F4;
s_iTranslateKeys[VK_F5] = EKey::KEY_F5;
s_iTranslateKeys[VK_F6] = EKey::KEY_F6;
s_iTranslateKeys[VK_F7] = EKey::KEY_F7;
s_iTranslateKeys[VK_F8] = EKey::KEY_F8;
s_iTranslateKeys[VK_F9] = EKey::KEY_F9;
s_iTranslateKeys[VK_F10] = EKey::KEY_F10;
s_iTranslateKeys[VK_F11] = EKey::KEY_F11;
s_iTranslateKeys[VK_F12] = EKey::KEY_F12;
s_iTranslateKeys[VK_NUMPAD0] = EKey::KEY_NUMPAD0;
s_iTranslateKeys[VK_NUMPAD1] = EKey::KEY_NUMPAD1;
s_iTranslateKeys[VK_NUMPAD2] = EKey::KEY_NUMPAD2;
s_iTranslateKeys[VK_NUMPAD3] = EKey::KEY_NUMPAD3;
s_iTranslateKeys[VK_NUMPAD4] = EKey::KEY_NUMPAD4;
s_iTranslateKeys[VK_NUMPAD5] = EKey::KEY_NUMPAD5;
s_iTranslateKeys[VK_NUMPAD6] = EKey::KEY_NUMPAD6;
s_iTranslateKeys[VK_NUMPAD7] = EKey::KEY_NUMPAD7;
s_iTranslateKeys[VK_NUMPAD8] = EKey::KEY_NUMPAD8;
s_iTranslateKeys[VK_NUMPAD9] = EKey::KEY_NUMPAD9;
s_iTranslateKeys['0'] = EKey::KEY_0;
s_iTranslateKeys['1'] = EKey::KEY_1;
s_iTranslateKeys['2'] = EKey::KEY_2;
s_iTranslateKeys['3'] = EKey::KEY_3;
s_iTranslateKeys['4'] = EKey::KEY_4;
s_iTranslateKeys['5'] = EKey::KEY_5;
s_iTranslateKeys['6'] = EKey::KEY_6;
s_iTranslateKeys['7'] = EKey::KEY_7;
s_iTranslateKeys['8'] = EKey::KEY_8;
s_iTranslateKeys['9'] = EKey::KEY_9;
s_iTranslateKeys['A'] = EKey::KEY_A;
s_iTranslateKeys['B'] = EKey::KEY_B;
s_iTranslateKeys['C'] = EKey::KEY_C;
s_iTranslateKeys['D'] = EKey::KEY_D;
s_iTranslateKeys['E'] = EKey::KEY_E;
s_iTranslateKeys['F'] = EKey::KEY_F;
s_iTranslateKeys['G'] = EKey::KEY_G;
s_iTranslateKeys['H'] = EKey::KEY_H;
s_iTranslateKeys['I'] = EKey::KEY_I;
s_iTranslateKeys['J'] = EKey::KEY_J;
s_iTranslateKeys['K'] = EKey::KEY_K;
s_iTranslateKeys['L'] = EKey::KEY_L;
s_iTranslateKeys['M'] = EKey::KEY_M;
s_iTranslateKeys['N'] = EKey::KEY_N;
s_iTranslateKeys['O'] = EKey::KEY_O;
s_iTranslateKeys['P'] = EKey::KEY_P;
s_iTranslateKeys['Q'] = EKey::KEY_Q;
s_iTranslateKeys['R'] = EKey::KEY_R;
s_iTranslateKeys['S'] = EKey::KEY_S;
s_iTranslateKeys['T'] = EKey::KEY_T;
s_iTranslateKeys['U'] = EKey::KEY_U;
s_iTranslateKeys['V'] = EKey::KEY_V;
s_iTranslateKeys['W'] = EKey::KEY_W;
s_iTranslateKeys['X'] = EKey::KEY_X;
s_iTranslateKeys['Y'] = EKey::KEY_Y;
s_iTranslateKeys['Z'] = EKey::KEY_Z;
s_iTranslateKeys[VK_LMENU] = EKey::KEY_LEFTALT;
s_iTranslateKeys[VK_RMENU] = EKey::KEY_RIGHTALT;
s_iTranslateKeys[VK_LCONTROL] = EKey::KEY_LEFTCTRL;
s_iTranslateKeys[VK_RCONTROL] = EKey::KEY_RIGHTCTRL;
s_iTranslateKeys[VK_LSHIFT] = EKey::KEY_LEFTSHIFT;
s_iTranslateKeys[VK_RSHIFT] = EKey::KEY_RIGHTSHIFT;
s_iTranslateKeys[VK_LWIN] = EKey::KEY_LEFTMETA;
s_iTranslateKeys[VK_RWIN] = EKey::KEY_RIGHTMETA;
}
DWORD iWindowStyle = WS_OVERLAPPEDWINDOW; // E_NORMAL
DWORD iWindowExStyle = 0;
if (eStyle == E_STYLE_BORDERLESS)
{
iWindowStyle = WS_OVERLAPPED | WS_SYSMENU;
}
else if (eStyle == E_STYLE_BORDERLESS_RESIZABLE)
{
iWindowStyle = WS_OVERLAPPED | WS_SYSMENU | WS_SIZEBOX | WS_MAXIMIZEBOX | WS_MINIMIZEBOX;
}
else if (eStyle == E_STYLE_POPUP)
{
iWindowStyle = WS_POPUP;
}
if ((eFlags & E_FLAG_OWN_DC) != 0)
{
iWindowStyle |= CS_OWNDC;
}
if ((eFlags & E_FLAG_ACCEPT_FILES_DROP) != 0)
{
iWindowExStyle |= WS_EX_ACCEPTFILES;
}
m_bBorderless = eStyle == E_STYLE_BORDERLESS || eStyle == E_STYLE_BORDERLESS_RESIZABLE;
m_bManualSizing = eStyle == E_STYLE_BORDERLESS_RESIZABLE;
m_bCatchAlt = (eFlags & E_FLAG_CATCH_ALT_KEY) != 0;
m_bCaptureMouseOnClick = ( eFlags & E_FLAG_ALWAYS_CAPTURE_MOUSE_ON_CLICK ) != 0;
RECT wr = { 0, 0, iWidth, iHeight };
if (bClientSize)
{
AdjustWindowRect(&wr, iWindowStyle, FALSE);
}
m_pHandle = CreateWindowEx(
iWindowExStyle,
"EasyWindowWin32",
pTitle != NULL ? pTitle : "",
iWindowStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
wr.right - wr.left,
wr.bottom - wr.top,
pParent != NULL ? ((EasyWindowWin32*)pParent)->m_pHandle : NULL,
NULL,
GetModuleHandle(NULL),
NULL);
if (m_bManualSizing)
{
SetRectEmpty(&m_oBorderThickness);
if (iWindowStyle & WS_THICKFRAME)
{
AdjustWindowRectEx(&m_oBorderThickness, iWindowStyle & ~WS_CAPTION, FALSE, NULL);
m_oBorderThickness.left *= -1;
m_oBorderThickness.top *= -1;
m_oBorderThickness.left += 1;
m_oBorderThickness.top += 1;
m_oBorderThickness.right += 1;
m_oBorderThickness.bottom += 1;
}
else if (iWindowStyle & WS_BORDER)
{
SetRect(&m_oBorderThickness, 1, 1, 1, 1);
}
}
SetWindowLongPtr(m_pHandle, GWLP_USERDATA, (LONG_PTR)this);
SetWindowPos(m_pHandle, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
}
virtual ~EasyWindowWin32()
{
if (m_pHandle != NULL)
{
SetWindowLongPtr(m_pHandle, GWLP_USERDATA, (LONG_PTR)NULL);
bool bIsActive = GetActiveWindow() == m_pHandle;
HWND hOwner = GetWindow(m_pHandle, GW_OWNER);
DestroyWindow(m_pHandle);
if (bIsActive && hOwner != NULL)
{
// HACK : Force active window because on destroy the owner window can be moved in background
SetActiveWindow(hOwner);
}
}
}
virtual bool Update() EW_OVERRIDE
{
if (IsWindow(m_pHandle) == FALSE)
return false;
MSG oMsg;
int iCount = 0;
m_iLastEvents = 0;
while (iCount < 10 && PeekMessage(&oMsg, m_pHandle, 0, 0, PM_NOREMOVE)) // Max 10 messages
{
if (GetMessage(&oMsg, m_pHandle, 0, 0))
{
TranslateMessage(&oMsg);
DispatchMessage(&oMsg);
if (m_bSizing)
break;
}
++iCount;
}
return true;
}
virtual EEventFlags GetLastEvents()
{
return m_iLastEvents;
}
virtual void Show(bool bShow) EW_OVERRIDE
{
ShowWindow(m_pHandle, bShow ? SW_SHOW : SW_HIDE);
}
virtual bool IsFocused() const EW_OVERRIDE
{
return GetForegroundWindow() == m_pHandle;
}
virtual void SetFocused() EW_OVERRIDE
{
SetForegroundWindow(m_pHandle);
}
virtual void SetSize(int iWidth, int iHeight, bool bClientSize) EW_OVERRIDE
{
RECT oRect = { 0, 0, iWidth, iHeight };
if (bClientSize && m_bBorderless == false)
{
DWORD iWindowStyle = GetWindowLong(m_pHandle, GWL_STYLE);
AdjustWindowRect(&oRect, iWindowStyle, FALSE);
}
SetWindowPos(m_pHandle, NULL, 0, 0, oRect.right - oRect.left, oRect.bottom - oRect.top, SWP_NOMOVE);
}
virtual void SetPosition(int iPosX, int iPosY, bool bClientPos) EW_OVERRIDE
{
RECT oRect = { iPosX, iPosY, 0, 0 };
if (bClientPos)
{
POINT oPoint = { 0, 0 };
ClientToScreen( m_pHandle, &oPoint );
RECT oWinRect;
GetWindowRect(m_pHandle, &oWinRect);
oRect.left += oPoint.x - oWinRect.left;
oRect.top += oPoint.y - oWinRect.top;
}
SetWindowPos(m_pHandle, NULL, oRect.left, oRect.top, 0, 0, SWP_NOSIZE);
}
virtual void SetMaximized(bool bMaximized) EW_OVERRIDE
{
if (bMaximized)
ShowWindow(m_pHandle, SW_MAXIMIZE);
else
ShowWindow(m_pHandle, SW_RESTORE);
}
virtual void SetMinimized(bool bMinimized) EW_OVERRIDE
{
if (bMinimized)
ShowWindow(m_pHandle, SW_MINIMIZE);
else if(IsIconic(m_pHandle))
ShowWindow(m_pHandle, SW_RESTORE);
}
virtual void SetTitle(const char* pTitle) EW_OVERRIDE
{
SetWindowText(m_pHandle, pTitle);
}
virtual void SetAlpha(unsigned char iAlpha) EW_OVERRIDE
{
if (iAlpha < 255)
{
SetWindowLong(m_pHandle, GWL_EXSTYLE, GetWindowLong(m_pHandle, GWL_EXSTYLE) | WS_EX_LAYERED);
}
else
{
SetWindowLong(m_pHandle, GWL_EXSTYLE, GetWindowLong(m_pHandle, GWL_EXSTYLE) & ~WS_EX_LAYERED);
}
SetLayeredWindowAttributes(m_pHandle, RGB(0, 0, 0), iAlpha, LWA_ALPHA);
}
virtual void SetCursor(ECursor eCursor) EW_OVERRIDE
{
m_eCursor = eCursor;
}
virtual void GetSize(int* iWidth, int* iHeight) const EW_OVERRIDE
{
RECT oRect;
GetWindowRect(m_pHandle, &oRect);
*iWidth = (int)(oRect.right - oRect.left);
*iHeight = (int)(oRect.bottom - oRect.top);
}
virtual void GetClientSize(int* iWidth, int* iHeight) const EW_OVERRIDE
{
RECT oRect;
GetClientRect(m_pHandle, &oRect);
*iWidth = (int)(oRect.right - oRect.left);
*iHeight = (int)(oRect.bottom - oRect.top);
}
virtual void GetPosition(int* iX, int* iY) const EW_OVERRIDE
{
RECT oRectClient;
GetWindowRect(m_pHandle, &oRectClient);
*iX = (int)oRectClient.left;
*iY = (int)oRectClient.top;
}
virtual void GetClientPosition(int* iX, int* iY) const EW_OVERRIDE
{
RECT oRectClient;
GetClientRect(m_pHandle, &oRectClient);
ClientToScreen(m_pHandle, reinterpret_cast<POINT*>(&oRectClient.left)); // convert top-left
*iX = (int)oRectClient.left;
*iY = (int)oRectClient.top;
}
virtual bool IsMaximized() const EW_OVERRIDE
{
return IsZoomed(m_pHandle) == TRUE;
}
virtual bool IsMinimized() const EW_OVERRIDE
{
return IsIconic(m_pHandle) == TRUE;
}
virtual bool IsKeyCtrlDown() EW_OVERRIDE
{
return (GetKeyState(VK_CONTROL) & 0x8000) != 0;
}
virtual bool IsKeyAltDown() EW_OVERRIDE
{
return (GetKeyState(VK_MENU) & 0x8000) != 0;
}
virtual bool IsKeyShiftDown()
{
return (GetKeyState(VK_SHIFT) & 0x8000) != 0;
}
virtual void* GetHandle() EW_OVERRIDE
{
return (void*)m_pHandle;
}
protected:
HWND m_pHandle;
bool m_bBorderless;
bool m_bManualSizing;
RECT m_oBorderThickness;
bool m_bSizing;
WPARAM m_iActiveHitMousePosition;
LONG_PTR m_iSizingMode;
ECursor m_eCursor;
bool m_bCatchAlt;
bool m_bKeyDownAlt[2];
bool m_bKeyDownCtrl[2];
bool m_bKeyDownShift[2];
bool m_bCaptureMouseOnClick;
EEventFlags m_iLastEvents;
static bool s_bClassInitialized;
static const int c_iMaxKeys = 256;
static EKey s_iTranslateKeys[c_iMaxKeys];
static LRESULT CALLBACK Proc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
EasyWindowWin32* pThis = (EasyWindowWin32*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
if (pThis != NULL)
{
switch (iMsg)
{
case WM_ERASEBKGND:
return 1;
break;
case WM_PAINT:
{
pThis->m_iLastEvents |= E_EVENT_PAINT;
PAINTSTRUCT ps;
BeginPaint( hWnd, &ps );
EndPaint( hWnd, &ps );
return 1;
break;
}
case WM_NCPAINT:
//return 0;
break;
case WM_SIZE:
pThis->m_iLastEvents |= E_EVENT_SIZED;
pThis->OnSize(pThis, LOWORD(lParam), HIWORD(lParam));
break;
case WM_MOVE:
pThis->m_iLastEvents |= E_EVENT_MOVED;
pThis->OnMove(pThis, LOWORD(lParam), HIWORD(lParam));
break;
case WM_SETFOCUS:
pThis->m_iLastEvents |= E_EVENT_FOCUSED;
pThis->OnFocus(pThis, true);
break;
case WM_KILLFOCUS:
pThis->m_iLastEvents |= E_EVENT_UNFOCUSED;
pThis->OnFocus(pThis, false);
break;
case WM_CLOSE:
pThis->m_iLastEvents |= E_EVENT_CLOSED;
if (pThis->OnClose(pThis))
return 1;
break;
case WM_LBUTTONDOWN:
if (!pThis->m_bSizing)
{
pThis->m_iLastEvents |= E_EVENT_MOUSE_BUTTON_DOWN;
if (pThis->m_bCaptureMouseOnClick)
SetCapture(hWnd);
pThis->OnMouseButton(pThis, 0, true);
}
break;
case WM_LBUTTONUP:
if (pThis->m_bSizing)
{
ReleaseCapture();
pThis->m_bSizing = false;
}
else
{
pThis->m_iLastEvents |= E_EVENT_MOUSE_BUTTON_UP;
if (pThis->m_bCaptureMouseOnClick)
ReleaseCapture();
pThis->OnMouseButton(pThis, 0, false);
}
break;
case WM_RBUTTONDOWN:
pThis->m_iLastEvents |= E_EVENT_MOUSE_BUTTON_DOWN;
pThis->OnMouseButton(pThis, 1, true);
break;
case WM_RBUTTONUP:
pThis->m_iLastEvents |= E_EVENT_MOUSE_BUTTON_UP;
pThis->OnMouseButton(pThis, 1, false);
break;
case WM_MBUTTONDOWN:
pThis->m_iLastEvents |= E_EVENT_MOUSE_BUTTON_DOWN;
pThis->OnMouseButton(pThis, 2, true);
break;
case WM_MBUTTONUP:
pThis->m_iLastEvents |= E_EVENT_MOUSE_BUTTON_UP;
pThis->OnMouseButton(pThis, 2, false);
break;
case WM_XBUTTONDOWN:
pThis->m_iLastEvents |= E_EVENT_MOUSE_BUTTON_DOWN;
pThis->OnMouseButton(pThis, 2 + GET_XBUTTON_WPARAM(wParam), true);
break;
case WM_XBUTTONUP:
pThis->m_iLastEvents |= E_EVENT_MOUSE_BUTTON_UP;
pThis->OnMouseButton(pThis, 2 + GET_XBUTTON_WPARAM(wParam), false);
break;
case WM_MOUSEMOVE:
case WM_NCMOUSEMOVE:
{
/*if (!m_bMouseTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_hWnd;
if (TrackMouseEvent(&tme))
{
m_bMouseTracking = true;
}
}*/
POINT oCursorPos;
GetCursorPos(&oCursorPos);
if (pThis->m_bSizing)
{
RECT oRect;
GetWindowRect(pThis->m_pHandle, &oRect);
switch (pThis->m_iSizingMode)
{
case HTLEFT:
oRect.left = oCursorPos.x;
break;
case HTTOPLEFT:
oRect.top = oCursorPos.y;
oRect.left = oCursorPos.x;
break;
case HTBOTTOMLEFT:
oRect.bottom = oCursorPos.y;
oRect.left = oCursorPos.x;
break;
case HTRIGHT:
oRect.right = oCursorPos.x;
break;
case HTTOPRIGHT:
oRect.top = oCursorPos.y;
oRect.right = oCursorPos.x;
break;
case HTBOTTOMRIGHT:
oRect.bottom = oCursorPos.y;
oRect.right = oCursorPos.x;
break;
case HTTOP:
oRect.top = oCursorPos.y;
break;
case HTBOTTOM:
oRect.bottom = oCursorPos.y;
break;
}
SetWindowPos(pThis->m_pHandle, NULL, oRect.left, oRect.top, oRect.right - oRect.left, oRect.bottom - oRect.top, 0);
}
else
{
pThis->m_iLastEvents |= E_EVENT_MOUSE_MOVED;
POINT oTopLeft = { 0, 0 };
ClientToScreen(pThis->m_pHandle, &oTopLeft); // Convert top-left client position to screen position
pThis->OnMouseMove(pThis, oCursorPos.x - oTopLeft.x, oCursorPos.y - oTopLeft.y);
}
break;
}
case WM_MOUSEWHEEL:
pThis->m_iLastEvents |= E_EVENT_MOUSE_WHEEL;
pThis->OnMouseWheel(pThis, GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA);
break;
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
if (pThis->m_bCatchAlt == false)
break;
case WM_KEYDOWN:
case WM_KEYUP:
{
bool bDown = (iMsg == WM_SYSKEYDOWN) || (iMsg == WM_KEYDOWN);
if (wParam >= 0 && wParam < c_iMaxKeys && (bDown == false || (HIWORD(lParam) & KF_REPEAT) == 0))
{
pThis->m_iLastEvents |= bDown ? E_EVENT_KEY_DOWN : E_EVENT_KEY_UP;
EKey eKey = (EKey)s_iTranslateKeys[wParam];
if (wParam == VK_SHIFT)
{
if (bDown && pThis->m_bKeyDownShift[0] != bDown && pThis->m_bKeyDownShift[1] != bDown)
pThis->OnKey(pThis, KEY_SHIFT, bDown);
UINT iScancode = (lParam & 0x00ff0000) >> 16;
UINT iNewVK = MapVirtualKey(iScancode, MAPVK_VSC_TO_VK_EX);
eKey = (EKey)s_iTranslateKeys[iNewVK];
pThis->m_bKeyDownShift[(int)(eKey == KEY_RIGHTSHIFT)] = bDown;
if (!bDown && pThis->m_bKeyDownShift[0] == bDown && pThis->m_bKeyDownShift[1] == bDown)
pThis->OnKey(pThis, KEY_SHIFT, bDown);
}
else if (wParam == VK_CONTROL)
{
if (bDown && pThis->m_bKeyDownCtrl[0] != bDown && pThis->m_bKeyDownCtrl[1] != bDown)
pThis->OnKey(pThis, KEY_CTRL, bDown);
bool bExtended = (HIWORD(lParam) & KF_EXTENDED) != 0;
eKey = bExtended ? KEY_RIGHTCTRL : KEY_LEFTCTRL;
pThis->m_bKeyDownCtrl[(int)(eKey == KEY_RIGHTCTRL)] = bDown;
if (!bDown && pThis->m_bKeyDownCtrl[0] == bDown && pThis->m_bKeyDownCtrl[1] == bDown)
pThis->OnKey(pThis, KEY_CTRL, bDown);
}
else if (wParam == VK_MENU)
{
if (bDown && pThis->m_bKeyDownAlt[0] != bDown && pThis->m_bKeyDownAlt[1] != bDown)
pThis->OnKey(pThis, KEY_ALT, bDown);
bool bExtended = (HIWORD(lParam) & KF_EXTENDED) != 0;
eKey = bExtended ? KEY_RIGHTALT : KEY_LEFTALT;
pThis->m_bKeyDownAlt[(int)(eKey == KEY_RIGHTALT)] = bDown;
if (!bDown && pThis->m_bKeyDownAlt[0] == bDown && pThis->m_bKeyDownAlt[1] == bDown)
pThis->OnKey(pThis, KEY_ALT, bDown);
}
pThis->OnKey(pThis, eKey, bDown);
}
return 1;
break;
}
case WM_CHAR:
pThis->m_iLastEvents |= E_EVENT_CHAR;
pThis->OnChar(pThis, wParam);
break;
case WM_NCCALCSIZE:
if (pThis->m_bManualSizing && wParam == TRUE && lParam != NULL)
{
NCCALCSIZE_PARAMS* pSizeParams = (NCCALCSIZE_PARAMS*)lParam;
if (pSizeParams != NULL && IsZoomed(hWnd))
{
pSizeParams->rgrc[0].left += pThis->m_oBorderThickness.left;
pSizeParams->rgrc[0].top += pThis->m_oBorderThickness.top;
pSizeParams->rgrc[0].right -= pThis->m_oBorderThickness.right;
pSizeParams->rgrc[0].bottom -= pThis->m_oBorderThickness.bottom;
}
return 0;
}
break;
case WM_NCHITTEST:
if (pThis->m_bBorderless && pThis->BorderlessHoveredArea.IsSet())
{
switch (pThis->BorderlessHoveredArea(pThis, (signed short)lParam, (signed short)(lParam >> 16)))
{
break; case E_HOVEREDAREA_MENU:
return HTMENU;
break; case E_HOVEREDAREA_CAPTION:
return HTCAPTION;
break; case E_HOVEREDAREA_MINIMIZE:
return HTMINBUTTON;
break; case E_HOVEREDAREA_MAXIMIZE:
return HTMAXBUTTON;
break; case E_HOVEREDAREA_CLOSE:
return HTCLOSE;
}
}
if (pThis->m_bManualSizing)
{
RECT oWindowRect;
const RECT& oBorder = pThis->m_oBorderThickness;
GetWindowRect(pThis->m_pHandle, &oWindowRect);
// Cast DWORD to short to recover signed value
int iAbsoluteX = (short)LOWORD(lParam);
int iAbsoluteY = (short)HIWORD(lParam);
int iX = iAbsoluteX - oWindowRect.left;
int iY = iAbsoluteY - oWindowRect.top;
if (iX < oBorder.left && iY < oBorder.top)
return HTTOPLEFT;
else if (iX > oWindowRect.right - oWindowRect.left - oBorder.right && iY < oBorder.top)
return HTTOPRIGHT;
else if (iX > oWindowRect.right - oWindowRect.left - oBorder.right && iY > oWindowRect.bottom - oWindowRect.top - oBorder.bottom)
return HTBOTTOMRIGHT;
else if (iX < oBorder.left && iY > oWindowRect.bottom - oWindowRect.top - oBorder.bottom)
return HTBOTTOMLEFT;
else if (iX < oBorder.left)
return HTLEFT;
else if (iY < oBorder.top)
return HTTOP;
else if (iX > oWindowRect.right - oWindowRect.left - oBorder.right)
return HTRIGHT;
else if (iY > oWindowRect.bottom - oWindowRect.top - oBorder.bottom)
return HTBOTTOM;
else
return HTCLIENT;
}
break;
case WM_NCLBUTTONDOWN:
if (pThis->m_bManualSizing)
{
switch (wParam)
{
case HTLEFT:
case HTTOPLEFT:
case HTBOTTOMLEFT:
case HTRIGHT:
case HTTOPRIGHT:
case HTBOTTOMRIGHT:
case HTTOP:
case HTBOTTOM:
SetCapture(hWnd);
pThis->m_bSizing = true;
pThis->m_iSizingMode = wParam;
return 0;
}
}
pThis->m_iActiveHitMousePosition = wParam;
if (pThis->m_bBorderless && (wParam == HTMINBUTTON || wParam == HTMAXBUTTON || wParam == HTCLOSE))
{
// Skip deafult system behaviour to skip buttons drawing in bordeless mode
return 0;
}
break;
case WM_NCLBUTTONUP:
{
if (pThis->m_bSizing)
{
ReleaseCapture();
pThis->m_bSizing = false;
}
WPARAM iActiveHitMousePotition = pThis->m_iActiveHitMousePosition;
pThis->m_iActiveHitMousePosition = HTNOWHERE;
// Self manage buttons
if (iActiveHitMousePotition == wParam)
{
if (wParam == HTMINBUTTON)
{
ShowWindow(hWnd, SW_MINIMIZE);
return 0;
}
else if (wParam == HTMAXBUTTON)
{
WINDOWPLACEMENT oWindowPlacement;
GetWindowPlacement(hWnd, &oWindowPlacement);
ShowWindow(hWnd, oWindowPlacement.showCmd == SW_MAXIMIZE ? SW_RESTORE : SW_MAXIMIZE);
return 0;
}
else if (wParam == HTCLOSE)
{
SendMessage(hWnd, WM_CLOSE, 0, 0);
return 0;
}
}
break;
}
case WM_SETCURSOR:
if (LOWORD(lParam) == HTCLIENT) // Inside window
{
switch (pThis->m_eCursor)
{
case E_CURSOR_NONE:
::SetCursor(NULL);
break;
case E_CURSOR_ARROW:
::SetCursor(LoadCursor(NULL, IDC_ARROW));
break;
case E_CURSOR_TEXT_INPUT: // When hovering over InputText, etc.
::SetCursor(LoadCursor(NULL, IDC_IBEAM));
break;
case E_CURSOR_HAND: // Unused
::SetCursor(LoadCursor(NULL, IDC_HAND));
break;
case E_CURSOR_RESIZE_NS: // Unused
::SetCursor(LoadCursor(NULL, IDC_SIZENS));
break;
case E_CURSOR_RESIZE_EW: // When hovering over a column
::SetCursor(LoadCursor(NULL, IDC_SIZEWE));
break;
case E_CURSOR_RESIZE_NESW: // Unused
::SetCursor(LoadCursor(NULL, IDC_SIZENESW));
break;
case E_CURSOR_RESIZE_NWSE: // When hovering over the bottom-right corner of a window
::SetCursor(LoadCursor(NULL, IDC_SIZENWSE));
break;
}
return 1;
}
break;
case WM_DROPFILES:
{
pThis->m_iLastEvents |= E_EVENT_FILES_DROPPED;
HDROP hDrop = (HDROP)wParam;
int iFileCount = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
char** pFiles = (char**)malloc(sizeof(char**) * iFileCount);
for (int i = 0; i < iFileCount; ++i)
{
pFiles[i] = (char*)malloc(sizeof(char) * MAX_PATH);
DragQueryFile(hDrop, i, pFiles[i], MAX_PATH);
}
POINT oDropPoint;
DragQueryPoint(hDrop, &oDropPoint);
DragFinish(hDrop);
DropFiles oFiles;
oFiles.iCount = iFileCount;
oFiles.pFiles = pFiles;
oFiles.oPosition.x = oDropPoint.x;
oFiles.oPosition.y = oDropPoint.y;
pThis->OnDropFiles(pThis, oFiles);
for (int i = 0; i < iFileCount; ++i)
{
free(pFiles[i]);
}
free(pFiles);
return 1;
}
}
}
return DefWindowProc(hWnd, iMsg, wParam, lParam);
}
};
const char* const EasyWindowWin32::c_pClassName = "EasyWindowWin32";
bool EasyWindowWin32::s_bClassInitialized = false;
EasyWindowWin32::EKey EasyWindowWin32::s_iTranslateKeys[c_iMaxKeys];
EasyWindow* EasyWindow::Create(const char* pTitle, int iWidth, int iHeight, bool bClientSize, EasyWindow* pParent, EWindowStyle eStyle, EWindowFlags eFlags)
{
return new EasyWindowWin32(pTitle, iWidth, iHeight, bClientSize, pParent, eStyle, eFlags);
}
#endif //_WIN32 || _WIN64 || _MSC_VER