-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseWindow.cpp
659 lines (574 loc) · 17.4 KB
/
BaseWindow.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
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
#include "Private.h"
#include "Globals.h"
#include "BaseWindow.h"
#define idTimer_UIObject 39772
//+---------------------------------------------------------------------------
//
// CBaseWindow::ctor
//
//----------------------------------------------------------------------------
CBaseWindow::CBaseWindow()
{
_wndHandle = nullptr;
_pParentWnd = nullptr;
_pUIWnd = nullptr;
_pTimerUIObj = nullptr;
_pUIObjCapture = nullptr;
_enableVirtualWnd = TRUE;
_visibleVirtualWnd = TRUE;
_RectOfVirtualWnd.left = 0;
_RectOfVirtualWnd.top = 0;
_RectOfVirtualWnd.right = 0;
_RectOfVirtualWnd.bottom = 0;
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::dtor
//
//----------------------------------------------------------------------------
CBaseWindow::~CBaseWindow()
{
_SetThis(_wndHandle, nullptr);
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_InitWindowClass
//
//----------------------------------------------------------------------------
/* static */
BOOL CBaseWindow::_InitWindowClass(_In_ LPCWSTR lpwszClassName, _Out_ ATOM *patom)
{
WNDCLASS wc;
wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_IME;
wc.lpfnWndProc = CBaseWindow::_WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = Global::dllInstanceHandle;
wc.hIcon = nullptr;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wc.lpszMenuName = nullptr;
wc.lpszClassName = lpwszClassName;
*patom = RegisterClass(&wc);
return (*patom != 0);
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_UninitClass
//
//----------------------------------------------------------------------------
/* static */
void CBaseWindow::_UninitWindowClass(ATOM atom)
{
if (atom != 0)
{
UnregisterClass((LPCTSTR)atom, Global::dllInstanceHandle);
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_Create
//
//----------------------------------------------------------------------------
BOOL CBaseWindow::_Create(ATOM atom, DWORD dwExStyle, DWORD dwStyle, _In_opt_ CBaseWindow *pParentWnd, int wndWidth,
int wndHeight, _In_opt_ HWND parentWndHandle)
{
_pParentWnd = pParentWnd;
if (atom != 0)
{
// create real window
_wndHandle = CreateWindowEx(dwExStyle, (LPCTSTR)atom, NULL, dwStyle, 0, 0, wndWidth, wndHeight,
_pParentWnd ? _pParentWnd->_GetWnd() : parentWndHandle, // parentWndHandle
NULL, Global::dllInstanceHandle,
this); // lpParam
if (!_wndHandle)
{
return FALSE;
}
}
return TRUE;
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_Destroy
//
//----------------------------------------------------------------------------
void CBaseWindow::_Destroy()
{
if (_wndHandle != nullptr)
{
DestroyWindow(_wndHandle);
_wndHandle = nullptr;
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_Move
//
//----------------------------------------------------------------------------
void CBaseWindow::_Move(int x, int y)
{
if (_wndHandle != nullptr)
{
SetWindowPos(_wndHandle, 0, x, y, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
}
else
{
int dx = x - _RectOfVirtualWnd.left;
int dy = y - _RectOfVirtualWnd.top;
_RectOfVirtualWnd.left += dx;
_RectOfVirtualWnd.top += dy;
_RectOfVirtualWnd.right += dx;
_RectOfVirtualWnd.bottom += dy;
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_Resize
//
//----------------------------------------------------------------------------
void CBaseWindow::_Resize(int x, int y, int cx, int cy)
{
if (_wndHandle != nullptr)
{
MoveWindow(_wndHandle, x, y, cx, cy, TRUE);
}
else
{
_RectOfVirtualWnd.left = x;
_RectOfVirtualWnd.top = y;
_RectOfVirtualWnd.right = x + cx;
_RectOfVirtualWnd.bottom = y + cy;
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_Show
//
//----------------------------------------------------------------------------
void CBaseWindow::_Show(BOOL isShowWnd)
{
if (_wndHandle != nullptr)
{
if (isShowWnd)
{
ShowWindow(_wndHandle, SW_SHOWNA);
}
else
{
ShowWindow(_wndHandle, SW_HIDE);
}
}
else
{
_visibleVirtualWnd = isShowWnd;
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_IsWindowVisible
//
//----------------------------------------------------------------------------
BOOL CBaseWindow::_IsWindowVisible()
{
if (_wndHandle != nullptr)
{
return IsWindowVisible(_wndHandle);
}
else
{
return _visibleVirtualWnd;
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_Enable
//
//----------------------------------------------------------------------------
void CBaseWindow::_Enable(BOOL enableWindowReceiveInput)
{
if (_wndHandle != nullptr)
{
EnableWindow(_wndHandle, enableWindowReceiveInput);
}
else
{
_enableVirtualWnd = enableWindowReceiveInput;
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_IsEnabled
//
//----------------------------------------------------------------------------
BOOL CBaseWindow::_IsEnabled()
{
if (_wndHandle != nullptr)
{
return IsWindowEnabled(_wndHandle);
}
else
{
return _enableVirtualWnd;
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_InvalidateRect
//
//----------------------------------------------------------------------------
void CBaseWindow::_InvalidateRect()
{
if (_wndHandle != nullptr)
{
InvalidateRect(_wndHandle, NULL, TRUE);
}
else
{
CBaseWindow *pobj = _pParentWnd;
while (pobj != nullptr)
{
if (pobj->_wndHandle)
{
InvalidateRect(pobj->_wndHandle, &_RectOfVirtualWnd, TRUE);
break;
}
pobj = pobj->_pParentWnd;
}
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_GetWindowRect
//
//----------------------------------------------------------------------------
BOOL CBaseWindow::_GetWindowRect(_Inout_ LPRECT lpRect)
{
if (_wndHandle != nullptr)
{
return GetWindowRect(_wndHandle, lpRect);
}
else
{
*lpRect = _RectOfVirtualWnd;
return TRUE;
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_GetClientRect
//
//----------------------------------------------------------------------------
BOOL CBaseWindow::_GetClientRect(_Inout_ LPRECT lpRect)
{
if (_wndHandle != nullptr)
{
return GetClientRect(_wndHandle, lpRect);
}
else
{
*lpRect = _RectOfVirtualWnd;
return TRUE;
}
}
//+---------------------------------------------------------------------------
//
// _GetWindowExtent
//
//----------------------------------------------------------------------------
HRESULT CBaseWindow::_GetWindowExtent(_In_ const RECT *prcTextExtent, _In_opt_ RECT *prcCandidateExtent,
_Inout_ POINT *pptCandidate)
{
RECT rcWorkArea = {0, 0, 0, 0};
// Get work area
GetWorkAreaFromPoint(*(LPPOINT)&prcTextExtent->left, &rcWorkArea);
// Calc candidate window extent
if (prcCandidateExtent)
{
CalcFitPointAroundTextExtent(prcTextExtent, &rcWorkArea, prcCandidateExtent, pptCandidate);
}
return S_OK;
}
//+---------------------------------------------------------------------------
//
// CalcFitPointAroundTextExtent
//
//----------------------------------------------------------------------------
void CBaseWindow::CalcFitPointAroundTextExtent(_In_ const RECT *prcTextExtent, _In_ const RECT *prcWorkArea,
_In_ const RECT *prcWindow, _Out_ POINT *ppt)
{
RECT rcTargetWindow[2];
DWORD dwFlags[2];
// set rcTargetWindow[0] which rectangle attached on bottom side of text extent
rcTargetWindow[0] = *prcWindow;
OffsetRect(&rcTargetWindow[0], prcTextExtent->left, prcTextExtent->bottom);
// set rcTargetWindow[1] which rectangle attached on top side of text extent
rcTargetWindow[1] = *prcWindow;
OffsetRect(&rcTargetWindow[1], prcTextExtent->left, prcTextExtent->top - (prcWindow->bottom - prcWindow->top));
//
// check target rectangle fit in workarea
//
for (DWORD i = 0; i < ARRAYSIZE(rcTargetWindow); i++)
{
dwFlags[i] = RectInRect(prcWorkArea, &rcTargetWindow[i]);
// generally, rcTargetWindow[0] or rcTargetWindow[1] have neither of these flags
// but in the case where window is just complete way off the screen e.g. user
// starts input, then drag window way off screen, both of these will be true
if ((dwFlags[i] & RECT_OVER_TOP) || (dwFlags[i] & RECT_OVER_BOTTOM))
{
continue;
}
if (dwFlags[i] == RECT_INSIDE)
{
*ppt = *(POINT *)&rcTargetWindow[i].left;
return;
}
else if ((dwFlags[i] & RECT_OVER_LEFT) != 0)
{
ppt->x = 0;
ppt->y = rcTargetWindow[i].top;
return;
}
else if ((dwFlags[i] & RECT_OVER_RIGHT) != 0)
{
ppt->x = prcWorkArea->right - (prcWindow->right - prcWindow->left);
ppt->y = rcTargetWindow[i].top;
return;
}
}
// if both rcTargetWindow have RECT_OVER_TOP or RECT_OVER_BOTTOM,
// then "dock" the window to top or bottom of working area.
if ((dwFlags[0] & RECT_OVER_TOP) != 0)
{
ppt->y = 0;
}
else
{
ppt->y = prcWorkArea->bottom - (prcWindow->bottom - prcWindow->top);
}
// dock to left/right edge if RECT_OVER_LEFT or RECT_OVER_RIGHT.
// else just stay where we are
if ((dwFlags[0] & RECT_OVER_LEFT) != 0)
{
ppt->x = 0;
}
else if ((dwFlags[0] & RECT_OVER_RIGHT) != 0)
{
ppt->x = prcWorkArea->right - (prcWindow->right - prcWindow->left);
}
else
{
ppt->x = prcTextExtent->left;
}
return;
}
//+---------------------------------------------------------------------------
//
// RectInRect
//
//----------------------------------------------------------------------------
DWORD CBaseWindow::RectInRect(_In_ const RECT *prcLimit, _In_ const RECT *prcTarget)
{
DWORD dwFlags = 0;
// Check if prcTarget is entirely inside prcLimit
if (prcLimit->left <= prcTarget->left && prcTarget->right <= prcLimit->right && prcLimit->top <= prcTarget->top &&
prcTarget->bottom <= prcLimit->bottom)
{
return RECT_INSIDE;
}
// Check horizontal range. Target can be
// - wider than the limit (assert here since it should never happen)
// - entirely outside the limit (RECT_OVERLEFT or RECT_OVERRIGHT)
// - partially inside the limit (RECT_OVERLEFT or RECT_OVERRIGHT)
if (prcTarget->left < prcLimit->left && prcTarget->right > prcLimit->right)
{
assert(FALSE);
dwFlags |= RECT_TOO_WIDE;
}
else if (prcTarget->left < prcLimit->left)
{
dwFlags |= RECT_OVER_LEFT;
}
else if (prcTarget->right > prcLimit->right)
{
dwFlags |= RECT_OVER_RIGHT;
}
// Check vertical range. Target can be
// - taller than the limit (assert here since it should never happen)
// - entirely outside the limit (RECT_OVERTOP or RECT_OVERBOTTOM)
// - partially inside the limit (RECT_OVERTOP or RECT_OVERBOTTOM)
if (prcTarget->top < prcLimit->top && prcTarget->bottom > prcLimit->bottom)
{
assert(FALSE);
dwFlags |= RECT_TOO_TALL;
}
else if (prcTarget->top < prcLimit->top)
{
dwFlags |= RECT_OVER_TOP;
}
else if (prcTarget->bottom > prcLimit->bottom)
{
dwFlags |= RECT_OVER_BOTTOM;
}
return dwFlags;
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_NotifyCommand
//
//----------------------------------------------------------------------------
LRESULT CBaseWindow::_NotifyCommand(UINT uMsg, DWORD dwSB, int nPos)
{
CBaseWindow *pUIWnd = _GetUIWnd();
if (pUIWnd && pUIWnd->_GetWnd())
{
WPARAM wParam = MAKEWPARAM(dwSB, nPos);
if (pUIWnd->_GetWnd())
{
return SendMessage(pUIWnd->_GetWnd(), uMsg, wParam, (LPARAM)0);
}
}
return 0;
}
//+---------------------------------------------------------------------------
//
// _SetCaptureObject
//
//----------------------------------------------------------------------------
void CBaseWindow::_SetCaptureObject(_In_opt_ CBaseWindow *pUIObj)
{
CBaseWindow *pUIWnd = _GetTopmostUIWnd();
if (nullptr == pUIWnd)
{
return;
}
pUIWnd->_pUIObjCapture = pUIObj;
if (pUIObj != nullptr)
{
SetCapture(pUIWnd->_GetWnd());
}
else
{
ReleaseCapture();
}
}
//+---------------------------------------------------------------------------
//
// _SetTimerObject
//
//----------------------------------------------------------------------------
void CBaseWindow::_SetTimerObject(_In_opt_ CBaseWindow *pUIObj, UINT uElapse)
{
CBaseWindow *pUIWnd = _GetTopmostUIWnd();
if (nullptr == pUIWnd)
{
return;
}
pUIWnd->_pTimerUIObj = pUIObj;
if (pUIObj != nullptr)
{
SetTimer(pUIWnd->_GetWnd(), idTimer_UIObject, uElapse, NULL);
}
else
{
KillTimer(pUIWnd->_GetWnd(), idTimer_UIObject);
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::_WindowProc
//
// CBaseWindow window proc.
//----------------------------------------------------------------------------
/* static */
LRESULT CALLBACK CBaseWindow::_WindowProc(_In_ HWND wndHandle, UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
_SetThis(wndHandle, ((CREATESTRUCT *)lParam)->lpCreateParams);
}
CBaseWindow *pv = _GetThis(wndHandle);
if (!pv)
{
return DefWindowProc(wndHandle, uMsg, wParam, lParam);
}
if (uMsg == WM_TIMER)
{
switch (wParam)
{
case idTimer_UIObject:
if (pv->_GetTimerObject() != nullptr)
{
pv->_GetTimerObject()->_OnTimer();
}
break;
}
return 0;
}
else
{
return pv->_WindowProcCallback(wndHandle, uMsg, wParam, lParam);
}
}
//+---------------------------------------------------------------------------
//
// CBaseWindow::GetWorkAreaFromPoint
//
//----------------------------------------------------------------------------
void CBaseWindow::GetWorkAreaFromPoint(_In_ const POINT &ptPoint, _Out_ LPRECT lprcWorkArea)
{
if (lprcWorkArea == nullptr)
{
return;
}
lprcWorkArea->left = 0;
lprcWorkArea->top = 0;
lprcWorkArea->right = 0;
lprcWorkArea->bottom = 0;
HMONITOR hMonitor = MonitorFromPoint(ptPoint, MONITOR_DEFAULTTONEAREST);
if (hMonitor)
{
MONITORINFO MonitorInfo = {0};
MonitorInfo.cbSize = sizeof(MONITORINFO);
if (GetMonitorInfo(hMonitor, &MonitorInfo))
{
*lprcWorkArea = MonitorInfo.rcWork;
return;
}
}
SystemParametersInfo(SPI_GETWORKAREA, 0, lprcWorkArea, 0);
return;
}
BOOL CBaseWindow::_IsTimer()
{
CBaseWindow *pobj = _GetTopmostUIWnd();
if (pobj != nullptr)
{
return (pobj->_pTimerUIObj != nullptr);
}
return FALSE;
}
BOOL CBaseWindow::_IsCapture()
{
CBaseWindow *pobj = _GetTopmostUIWnd();
if (pobj != nullptr)
{
return (pobj->_pUIObjCapture != nullptr);
}
return FALSE;
}
CBaseWindow *CBaseWindow::_GetTopmostUIWnd()
{
CBaseWindow *pobj = this;
while (pobj->_pParentWnd != nullptr)
{
pobj = pobj->_pParentWnd;
}
return pobj->_pUIWnd;
}