This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathcef_window.cpp
More file actions
365 lines (299 loc) · 10.6 KB
/
cef_window.cpp
File metadata and controls
365 lines (299 loc) · 10.6 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
/*
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "cef_window.h"
//DEFINES
//HiDPI The default logical DPI when scaling is applied in windows. see. https://msdn.microsoft.com/en-us/library/ms701681(v=vs.85).aspx
#define DEFAULT_WINDOWS_DPI 96
// Externals
extern HINSTANCE hInst;
// Constants
static const wchar_t gCefClientWindowPropName[] = L"CefClientWindowPtr";
// Global Hook Data
struct HookData {
HookData()
{
this->Reset();
}
void Reset()
{
mhHook = NULL;
mWindow = NULL;
}
HHOOK mhHook;
cef_window* mWindow;
} gHookData;
//cef_window --
cef_window::cef_window(void) :
mWnd(NULL),
mSuperWndProc(NULL)
{
}
cef_window::~cef_window(void)
{
}
// The CBT HookProc will catch a window being
// created so that we can subclass it and
// dispatch WM_NCCREATE messages to the window object
static LRESULT CALLBACK _HookProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code != HCBT_CREATEWND)
return CallNextHookEx(gHookData.mhHook, code, wParam, lParam);
LPCREATESTRUCT lpcs = ((LPCBT_CREATEWND)lParam)->lpcs;
HHOOK nextHook = gHookData.mhHook;
if (lpcs->lpCreateParams && lpcs->lpCreateParams == (LPVOID)gHookData.mWindow)
{
HWND hWnd = (HWND)wParam;
gHookData.mWindow->SubclassWindow(hWnd);
// Rest the hook data here since we've already hooked this window
// this allows for other windows to be created in the WM_CREATE handlers
// of subclassed windows
gHookData.mWindow = 0;
}
return CallNextHookEx(nextHook, code, wParam, lParam);
}
// Enables Hooking
static void _HookWindowCreate(cef_window* window)
{
// can only hook one creation at a time
if (gHookData.mhHook || gHookData.mWindow)
return;
gHookData.mhHook = ::SetWindowsHookEx(WH_CBT, _HookProc, NULL, ::GetCurrentThreadId());
gHookData.mWindow = window;
}
// Disabled Hooking
static void _UnHookWindowCreate()
{
::UnhookWindowsHookEx(gHookData.mhHook);
gHookData.Reset();
}
// Window Proc which receives messages from subclassed windows
static LRESULT CALLBACK _WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
cef_window* window = (cef_window*)::GetProp(hWnd, ::gCefClientWindowPropName);
if (window)
{
return window->WindowProc(message, wParam, lParam);
}
else
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
// Subclass
bool cef_window::SubclassWindow(HWND hWnd)
{
if (::GetProp(hWnd, ::gCefClientWindowPropName) != NULL)
return false;
mWnd = hWnd;
mSuperWndProc = from_cpp20::bit_cast<WNDPROC>(SetWindowLongPtr(GWLP_WNDPROC, (LONG_PTR)&_WindowProc));
SetProp(::gCefClientWindowPropName, (HANDLE)this);
return true;
}
// CefCreateWindow -- used to wrap the window create process
// so that hooking and unhooking are done correctly
static HWND _CefCreateWindow(LPCTSTR szClassname, LPCTSTR szWindowTitle, DWORD dwStyles, int x, int y, int width, int height, HWND hWndParent, HMENU hMenu, cef_window* window)
{
_HookWindowCreate(window);
HWND result = CreateWindow(szClassname, szWindowTitle,
dwStyles, x, y, width, height, hWndParent, hMenu, hInst, (LPVOID)window);
_UnHookWindowCreate();
return result;
}
// Create -- Creates a window
BOOL cef_window::Create(LPCTSTR szClassname, LPCTSTR szWindowTitle, DWORD dwStyles, int x, int y, int width, int height, cef_window* parent/*=NULL*/, cef_menu* menu/*=NULL*/)
{
HWND hWndParent = parent ? parent->mWnd : NULL;
HMENU hMenu = NULL;
HWND hWndThis = ::_CefCreateWindow(szClassname, szWindowTitle,
dwStyles, x, y, width, height, hWndParent, hMenu, this);
return hWndThis && hWndThis == mWnd;
}
// Calls the window procedure for a subclassed window
// or, if just wrapping an HWND,
// -- sends the message to the window's registered wndproc
LRESULT cef_window::DefaultWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (mSuperWndProc)
{
// If we were subclassed then we have a super window proc
return ::CallWindowProc(mSuperWndProc, mWnd, message, wParam, lParam);
}
else
{
// otherwise just let the system deal with it.
return ::DefWindowProc(mWnd, message, wParam, lParam);
}
}
// WM_NCDESTROY handler
// cleansup the data and unsubclasses the window
// calls PostNcDestroy
BOOL cef_window::HandleNcDestroy()
{
WNDPROC superWndProc = from_cpp20::bit_cast<WNDPROC>(GetWindowLongPtr(GWLP_WNDPROC));
RemoveProp(::gCefClientWindowPropName);
DefaultWindowProc(WM_NCDESTROY, 0, 0);
if ((from_cpp20::bit_cast<WNDPROC>(GetWindowLongPtr(GWLP_WNDPROC)) == superWndProc) && (mSuperWndProc != NULL))
SetWindowLongPtr(GWLP_WNDPROC, reinterpret_cast<INT_PTR>(mSuperWndProc));
mSuperWndProc = NULL;
PostNcDestroy();
return TRUE;
}
BOOL cef_window::HandleCreate()
{
return FALSE;
}
// PostNcDestroy base implementaiton
void cef_window::PostNcDestroy()
{
mWnd = NULL;
}
// Window Message Dispatching
LRESULT cef_window::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_NCDESTROY:
if (HandleNcDestroy())
return 0L;
break;
}
LRESULT lr = DefaultWindowProc(message, wParam, lParam);
if (message == WM_NCDESTROY)
{
PostNcDestroy();
}
return lr;
}
// ScreenToNonClient or ScreenToWindow.
// Converts screen coordinates into window coordinates
// (0,0) is the top left corner of the window
void cef_window::ScreenToNonClient(LPRECT r) const
{
if (r) {
WINDOWINFO wi ;
::ZeroMemory (&wi, sizeof (wi)) ;
wi.cbSize = sizeof (wi) ;
GetWindowInfo (&wi) ;
int height = ::RectHeight(*r);
int width = ::RectWidth(*r);
r->top = r->top - wi.rcWindow.top;
r->left = r->left - wi.rcWindow.left;
r->bottom = r->top + height;
r->right = r->left + width;
}
}
// Computes the client rect relative to the Window Rect
// Used to compute the clipping region, etc...
void cef_window::ComputeLogicalClientRect(RECT& rectClient) const
{
WINDOWINFO wi ;
::ZeroMemory (&wi, sizeof (wi)) ;
wi.cbSize = sizeof (wi) ;
GetWindowInfo (&wi) ;
::CopyRect(&rectClient, &wi.rcClient);
int height = ::RectHeight(wi.rcClient);
int width = ::RectWidth(wi.rcClient);
rectClient.top = wi.rcClient.top - wi.rcWindow.top;
rectClient.left = wi.rcClient.left - wi.rcWindow.left;
rectClient.bottom = rectClient.top + height;
rectClient.right = rectClient.left + width;
}
// Retrieves the window rect in logical coordinates
void cef_window::ComputeLogicalWindowRect (RECT& rectWindow) const
{
RECT wr;
GetWindowRect (&wr);
::SetRectEmpty(&rectWindow);
rectWindow.bottom = ::RectHeight(wr);
rectWindow.right = ::RectWidth(wr);
}
// Converts NonClient (window) coordinates to screen coordinates
void cef_window::NonClientToScreen(LPRECT r) const
{
if (r) {
RECT wr;
GetWindowRect (&wr);
r->top += wr.top;
r->left += wr.left;
r->bottom += wr.top;
r->right += wr.left;
}
}
// Basic ScreenToClient implementation
void cef_window::ScreenToClient(LPRECT lpRect) const
{
if (lpRect) {
::ScreenToClient(mWnd, (LPPOINT)lpRect);
::ScreenToClient(mWnd, ((LPPOINT)lpRect)+1);
if (GetExStyle() & WS_EX_LAYOUTRTL)
::RectSwapLeftRight(*lpRect);
}
}
// Basic ClientToScreen implementation
void cef_window::ClientToScreen(LPRECT lpRect) const
{
if (lpRect) {
::ClientToScreen(mWnd, (LPPOINT)lpRect);
::ClientToScreen(mWnd, ((LPPOINT)lpRect)+1);
if (GetExStyle() & WS_EX_LAYOUTRTL)
::RectSwapLeftRight(*lpRect);
}
}
// Turns mouse tracking on or off
// we track non-client mouse events when the user moves the
// mouse over one of our buttons. We want a WM_NCMOUSELEAVE
// message so we can redraw the buttons in the non-hovered
// state this method tells windows we want the WM_NCMOUSELEAVE
// message. It can tell it that we don't care anymore too,
// which is nice...
BOOL cef_window::TrackNonClientMouseEvents(bool track/*=true*/)
{
TRACKMOUSEEVENT tme ;
::ZeroMemory(&tme, sizeof (tme)) ;
tme.cbSize = sizeof (tme) ;
tme.dwFlags = TME_QUERY ;
tme.hwndTrack = mWnd ;
::TrackMouseEvent (&tme) ;
/// i.e. if we're currently tracking and the caller
// wanted to track or if we're not currently tracking and
// the caller wanted to turn off tracking then just bail
if (((tme.dwFlags & TME_LEAVE) == TME_LEAVE) == track)
return FALSE; // nothing to do...
tme.dwFlags = TME_LEAVE|TME_NONCLIENT;
if (!track)
tme.dwFlags |= TME_CANCEL;
// The previous call to TrackMouseEvent destroys the hwndTrack
// and cbSize members so we have to re-initialize them.
tme.hwndTrack = mWnd ;
tme.cbSize = sizeof (tme) ;
return ::TrackMouseEvent (&tme);
}
//Get the Horizontal DPI scaling factor.
UINT cef_window::GetDPIScalingX() const
{
HDC dc = GetDC();
float lpx = dc ? GetDeviceCaps(dc,LOGPIXELSX):DEFAULT_WINDOWS_DPI ;
//scale factor as it would look in a default(96dpi) screen. the default will be always 96 logical DPI when scaling is applied in windows.
//see. https://msdn.microsoft.com/en-us/library/ms701681(v=vs.85).aspx
ReleaseDC(dc);
return (lpx/DEFAULT_WINDOWS_DPI)*100;
}