-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultWindowProc.h
289 lines (253 loc) · 10.2 KB
/
DefaultWindowProc.h
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
#pragma once
#include <afx.h>
#include <functional>
#include <list>
#include <unordered_map>
/*
* DefaultWindowProc manager, allow to add callbacks on window messages
*
* Usage:
DefaultWindowProc::OnWindowMessage(button, WM_NCDESTROY, [](HWND hWnd, WPARAM wParam, LPARAM lParam, LRESULT& result)
{
OutputDebugString(L"On destroy button");
}, this);
*/
class DefaultWindowProc
{
public:
/// Callback for handling message from window, set result if you want to interrupt handling message by other handlers
typedef std::function<void(HWND hWnd, WPARAM wParam, LPARAM lParam, LRESULT& result)> Callback;
/// Add callback on window message
static void OnWindowMessage(const CWnd& targetWindow, UINT message, Callback&& callback, const LPVOID handler)
{
ENSURE(::IsWindow(targetWindow));
ENSURE(!!handler);
DefaultWindowProc& instance = Instance();
if (!instance.TryAttachToWindowProc(targetWindow))
return;
auto windowInfoIt = instance.m_windowsInfo.find(static_cast<HWND>(targetWindow));
ASSERT(windowInfoIt != instance.m_windowsInfo.end());
windowInfoIt->second.callbacksByMessages[message].emplace(handler, std::move(callback));
}
/// Call default window process of the window
static LRESULT CallDefaultWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
DefaultWindowProc& instance = Instance();
if (auto it = instance.m_windowsInfo.find(hWnd); it != instance.m_windowsInfo.end())
return it->second.defaultWindowProc(hWnd, message, wParam, lParam);
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
/// Remove callback from window message
static void RemoveCallback(const CWnd& targetWindow, UINT message, const LPVOID handler)
{
DefaultWindowProc& instance = Instance();
auto windowInfoIt = instance.m_windowsInfo.find(static_cast<HWND>(targetWindow));
if (windowInfoIt == instance.m_windowsInfo.end())
{
// no callbacks for target window
return;
}
auto messagesCallbacksIt = windowInfoIt->second.callbacksByMessages.find(message);
if (messagesCallbacksIt == windowInfoIt->second.callbacksByMessages.end())
{
// no callbacks for this message
ASSERT(FALSE);
return;
}
auto handlerCallbackIt = messagesCallbacksIt->second.find(handler);
if (handlerCallbackIt == messagesCallbacksIt->second.end())
{
// this handler hasn`t subscribed to events yet
ASSERT(FALSE);
return;
}
// clean callbacks
messagesCallbacksIt->second.erase(handlerCallbackIt);
if (messagesCallbacksIt->second.empty())
{
windowInfoIt->second.callbacksByMessages.erase(messagesCallbacksIt);
if (windowInfoIt->second.callbacksByMessages.empty() && windowInfoIt->second.anyMessagesCallbacks.empty())
{
// restore default window proc
::SetWindowLongPtr(targetWindow, GWLP_WNDPROC, (LONG_PTR)windowInfoIt->second.defaultWindowProc);
instance.m_windowsInfo.erase(windowInfoIt);
}
}
}
/// Callback for handling message from window, set result if you want to interrupt handling message by other handlers
typedef std::function<void(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT& result)> AnyMessageCallback;
/// Add callback on window message
static void OnAnyWindowMessage(const CWnd& targetWindow, AnyMessageCallback&& callback, const LPVOID handler)
{
ENSURE(::IsWindow(targetWindow));
ENSURE(!!handler);
DefaultWindowProc& instance = Instance();
instance.TryAttachToWindowProc(targetWindow);
auto windowInfoIt = instance.m_windowsInfo.find(static_cast<HWND>(targetWindow));
ASSERT(windowInfoIt != instance.m_windowsInfo.end());
windowInfoIt->second.anyMessagesCallbacks.emplace(handler, std::move(callback));
}
/// Add callback on window message
static void RemoveAnyMessageCallback(const CWnd& targetWindow, const LPVOID handler)
{
DefaultWindowProc& instance = Instance();
auto windowInfoIt = instance.m_windowsInfo.find(static_cast<HWND>(targetWindow));
if (windowInfoIt == instance.m_windowsInfo.end())
{
// no callbacks for target window
return;
}
auto handlerCallbackIt = windowInfoIt->second.anyMessagesCallbacks.find(handler);
if (handlerCallbackIt == windowInfoIt->second.anyMessagesCallbacks.end())
{
// this handler hasn`t been subscribed to events yet
ASSERT(FALSE);
return;
}
// clean callbacks
windowInfoIt->second.anyMessagesCallbacks.erase(handlerCallbackIt);
if (windowInfoIt->second.callbacksByMessages.empty() && windowInfoIt->second.anyMessagesCallbacks.empty())
{
// restore default window proc
::SetWindowLongPtr(targetWindow, GWLP_WNDPROC, (LONG_PTR)windowInfoIt->second.defaultWindowProc);
instance.m_windowsInfo.erase(windowInfoIt);
}
}
private:
// allow creating only from instance
DefaultWindowProc() = default;
~DefaultWindowProc()
{
ASSERT(m_windowsInfo.empty());
for (auto&&[hWnd, windowInfo] : m_windowsInfo)
{
::SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)windowInfo.defaultWindowProc);
}
}
DefaultWindowProc(const DefaultWindowProc&) = delete;
DefaultWindowProc& operator=(const DefaultWindowProc&) = delete;
private:
// get anchor manager instance
static DefaultWindowProc& Instance()
{
static DefaultWindowProc s;
return s;
}
// checking if we already attached to window def window proc, if no - attach
bool TryAttachToWindowProc(HWND hWnd)
{
if (m_windowsInfo.find(hWnd) == m_windowsInfo.end())
{
auto windowPros = (WNDPROC)::SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)WindowProc);
if (windowPros == nullptr)
{
ASSERT(!"Failed to change window proc");
return false;
}
m_windowsInfo.emplace(hWnd, WindowInfo(windowPros));
}
return true;
}
// if we don`t need to handle def window proc but we do - detach
void CheckNecessityToHandleDefProc(HWND hWnd)
{
if (const auto it = m_windowsInfo.find(hWnd); it != m_windowsInfo.end() &&
it->second.callbacksByMessages.empty() && it->second.anyMessagesCallbacks.empty())
{
::SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)it->second.defaultWindowProc);
m_windowsInfo.erase(it);
}
}
// proxy window proc
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DefaultWindowProc& instance = Instance();
auto windowInfoIt = instance.m_windowsInfo.find(hWnd);
if (windowInfoIt == instance.m_windowsInfo.end())
{
ASSERT(FALSE);
return S_FALSE;
}
#ifdef DEBUG // debugging helper
CString str;
if (uMsg != WM_GETTEXT && uMsg != WM_GETTEXTLENGTH)
{
if (::IsWindow(hWnd))
CWnd::FromHandle(hWnd)->GetWindowText(str);
else
ASSERT(FALSE);
}
#endif
WNDPROC defaultWindowProc = windowInfoIt->second.defaultWindowProc;
LRESULT result = 0;
for (auto&& [handler, callback] : windowInfoIt->second.anyMessagesCallbacks)
{
callback(hWnd, uMsg, wParam, lParam, result);
// callback might unsubscribe itself
windowInfoIt = instance.m_windowsInfo.find(hWnd);
if (windowInfoIt == instance.m_windowsInfo.end())
{
if (result || !::IsWindow(hWnd))
return 1;
return ::CallWindowProc(defaultWindowProc, hWnd, uMsg, wParam, lParam);
}
if (result)
{
if (uMsg == WM_NCDESTROY)
break;
return result;
}
}
if (!result)
{
const CallbacksForMessage& callbacks = windowInfoIt->second.callbacksByMessages;
const auto messageCallbackIt = callbacks.find(uMsg);
if (messageCallbackIt != callbacks.end())
{
for (auto&& [handler, callback] : messageCallbackIt->second)
{
callback(hWnd, wParam, lParam, result);
// callback might unsubscribe itself
windowInfoIt = instance.m_windowsInfo.find(hWnd);
if (windowInfoIt == instance.m_windowsInfo.end())
{
if (result || !::IsWindow(hWnd))
return 1;
return ::CallWindowProc(defaultWindowProc, hWnd, uMsg, wParam, lParam);
}
if (result)
{
if (uMsg == WM_NCDESTROY)
break;
return result;
}
}
}
}
switch (uMsg)
{
case WM_NCDESTROY:
// restore def window proc
::SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)defaultWindowProc);
// don't use
instance.m_windowsInfo.erase(windowInfoIt);
break;
default:
break;
}
return ::CallWindowProc(defaultWindowProc, hWnd, uMsg, wParam, lParam);
}
private:
typedef std::unordered_map<LPVOID, AnyMessageCallback> AnyMessageCallbackByHandler;
typedef std::unordered_map<LPVOID, Callback> CallbackByHandler;
typedef std::unordered_map<UINT, CallbackByHandler> CallbacksForMessage;
struct WindowInfo
{
explicit WindowInfo(WNDPROC proc) noexcept : defaultWindowProc(proc) {}
WNDPROC defaultWindowProc;
AnyMessageCallbackByHandler anyMessagesCallbacks;
CallbacksForMessage callbacksByMessages;
};
// list of handling windows and their information
std::unordered_map<HWND, WindowInfo> m_windowsInfo;
};