-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-window.cc
More file actions
106 lines (73 loc) · 2.28 KB
/
Copy pathbase-window.cc
File metadata and controls
106 lines (73 loc) · 2.28 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
// base-window.cc
// Code for `base-window` module.
// See license.txt for copyright and terms of use.
#include "base-window.h" // this module
#include "winapi-util.h" // winapiDie, CreateWindowExWArgs
#include <cassert> // assert
wchar_t const *BaseWindow::s_windowClassName = L"Base Window Class";
bool BaseWindow::s_windowClassRegistered = false;
BaseWindow::~BaseWindow()
{}
BaseWindow::BaseWindow()
: m_hwnd(nullptr)
{}
void BaseWindow::registerWindowClassIfNecessary()
{
if (s_windowClassRegistered) {
return;
}
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = s_windowClassName;
// TODO: This should be adjustable.
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
if (RegisterClass(&wc) == 0) {
winapiDie(L"RegisterClass");
}
s_windowClassRegistered = true;
}
void BaseWindow::createWindow(CreateWindowExWArgs const &origCW)
{
assert(!m_hwnd);
registerWindowClassIfNecessary();
CreateWindowExWArgs cw(origCW);
cw.m_lpClassName = s_windowClassName;
cw.m_lpParam = this;
HWND hwnd = cw.createWindow();
if (!hwnd) {
winapiDie(L"CreateWindowExW");
}
// `m_hwnd` should be set in `WindowProc`.
assert(hwnd == m_hwnd);
}
/*static*/ LRESULT CALLBACK BaseWindow::WindowProc(
HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
BaseWindow *pThis = nullptr;
if (uMsg == WM_NCCREATE) {
// Get the `BaseWindow` instance from `CREATESTRUCT` and save it in
// `GWLP_USERDATA`.
CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
pThis = (BaseWindow*)(pCreate->lpCreateParams);
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
// The `WM_NCCREATE` message is delivered synchronously before
// `CreateWindowExW` returns, so we need to set the handle here.
pThis->m_hwnd = hwnd;
}
else {
// Get the `BaseWindow` instance from `GWLP_USERDATA`.
pThis = (BaseWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
}
if (pThis) {
return pThis->handleMessage(uMsg, wParam, lParam);
}
else {
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
LRESULT BaseWindow::handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
}
// EOF