Sync with trunk head
[reactos.git] / lib / atl / atlwin.h
1 /*
2 * ReactOS ATL
3 *
4 * Copyright 2009 Andrew Hill <ash77@reactos.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #pragma once
22
23 #ifdef __GNUC__
24 #define GCCU(x) x __attribute__((unused))
25 #define Unused(x)
26 #else
27 #define GCCU(x)
28 #define Unused(x) (x);
29 #endif // __GNUC__
30
31 #ifdef SetWindowLongPtr
32 #undef SetWindowLongPtr
33 inline LONG_PTR SetWindowLongPtr(HWND hWnd, int nIndex, LONG_PTR dwNewLong)
34 {
35 return SetWindowLong(hWnd, nIndex, (LONG)dwNewLong);
36 }
37 #endif
38
39 #ifdef GetWindowLongPtr
40 #undef GetWindowLongPtr
41 inline LONG_PTR GetWindowLongPtr(HWND hWnd, int nIndex)
42 {
43 return (LONG_PTR)GetWindowLong(hWnd, nIndex);
44 }
45 #endif
46
47 namespace ATL
48 {
49
50 struct _ATL_WNDCLASSINFOW;
51 typedef _ATL_WNDCLASSINFOW CWndClassInfo;
52
53 template <DWORD t_dwStyle = 0, DWORD t_dwExStyle = 0>
54 class CWinTraits
55 {
56 public:
57 static DWORD GetWndStyle(DWORD dwStyle)
58 {
59 if (dwStyle == 0)
60 return t_dwStyle;
61 return dwStyle;
62 }
63
64 static DWORD GetWndExStyle(DWORD dwExStyle)
65 {
66 if (dwExStyle == 0)
67 return t_dwExStyle;
68 return dwExStyle;
69 }
70 };
71
72 typedef CWinTraits<WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0> CControlWinTraits;
73 typedef CWinTraits<WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE> CFrameWinTraits;
74 typedef CWinTraits<WS_OVERLAPPEDWINDOW | WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WS_EX_MDICHILD> CMDIChildWinTraits;
75
76 template <DWORD t_dwStyle = 0, DWORD t_dwExStyle = 0, class TWinTraits = CControlWinTraits>
77 class CWinTraitsOR
78 {
79 public:
80 static DWORD GetWndStyle(DWORD dwStyle)
81 {
82 return dwStyle | t_dwStyle | TWinTraits::GetWndStyle(dwStyle);
83 }
84
85 static DWORD GetWndExStyle(DWORD dwExStyle)
86 {
87 return dwExStyle | t_dwExStyle | TWinTraits::GetWndExStyle(dwExStyle);
88 }
89 };
90
91 class _U_MENUorID
92 {
93 public:
94 HMENU m_hMenu;
95 public:
96 _U_MENUorID(HMENU hMenu)
97 {
98 m_hMenu = hMenu;
99 }
100
101 _U_MENUorID(UINT nID)
102 {
103 m_hMenu = (HMENU)(UINT_PTR)nID;
104 }
105 };
106
107 class _U_RECT
108 {
109 public:
110 LPRECT m_lpRect;
111 public:
112 _U_RECT(LPRECT lpRect)
113 {
114 m_lpRect = lpRect;
115 }
116
117 _U_RECT(RECT &rc)
118 {
119 m_lpRect = &rc;
120 }
121 };
122
123 struct _ATL_MSG : public MSG
124 {
125 public:
126 BOOL bHandled;
127 public:
128 _ATL_MSG(HWND hWnd, UINT uMsg, WPARAM wParamIn, LPARAM lParamIn, BOOL bHandledIn = TRUE)
129 {
130 hwnd = hWnd;
131 message = uMsg;
132 wParam = wParamIn;
133 lParam = lParamIn;
134 time = 0;
135 pt.x = 0;
136 pt.y = 0;
137 bHandled = bHandledIn;
138 }
139 };
140
141 #if defined(_M_IX86)
142
143 #pragma pack(push,1)
144 struct thunkCode
145 {
146 DWORD m_mov;
147 DWORD m_this;
148 BYTE m_jmp;
149 DWORD m_relproc;
150 };
151 #pragma pack(pop)
152
153 class CWndProcThunk
154 {
155 public:
156 thunkCode m_thunk;
157 _AtlCreateWndData cd;
158 public:
159 BOOL Init(WNDPROC proc, void *pThis)
160 {
161 m_thunk.m_mov = 0x042444C7;
162 m_thunk.m_this = PtrToUlong(pThis);
163 m_thunk.m_jmp = 0xe9;
164 m_thunk.m_relproc = DWORD(reinterpret_cast<char *>(proc) - (reinterpret_cast<char *>(this) + sizeof(thunkCode)));
165 return TRUE;
166 }
167
168 WNDPROC GetWNDPROC()
169 {
170 return reinterpret_cast<WNDPROC>(&m_thunk);
171 }
172 };
173
174 #elif _AMD64_ //WARNING: NOT VERIFIED
175 #pragma pack(push,1)
176 struct thunkCode
177 {
178 DWORD_PTR m_mov;
179 DWORD_PTR m_this;
180 BYTE m_jmp;
181 DWORD_PTR m_relproc;
182 };
183 #pragma pack(pop)
184
185 class CWndProcThunk
186 {
187 public:
188 thunkCode m_thunk;
189 _AtlCreateWndData cd;
190 public:
191 BOOL Init(WNDPROC proc, void *pThis)
192 {
193 m_thunk.m_mov = 0xffff8000042444C7LL;
194 m_thunk.m_this = (DWORD_PTR)pThis;
195 m_thunk.m_jmp = 0xe9;
196 m_thunk.m_relproc = DWORD_PTR(reinterpret_cast<char *>(proc) - (reinterpret_cast<char *>(this) + sizeof(thunkCode)));
197 return TRUE;
198 }
199
200 WNDPROC GetWNDPROC()
201 {
202 return reinterpret_cast<WNDPROC>(&m_thunk);
203 }
204 };
205 #else
206 #error ARCH not supported
207 #endif
208
209 class CMessageMap
210 {
211 public:
212 virtual BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT &lResult, DWORD dwMsgMapID) = 0;
213 };
214
215 class CWindow
216 {
217 public:
218 HWND m_hWnd;
219 static RECT rcDefault;
220 public:
221 CWindow(HWND hWnd = NULL)
222 {
223 m_hWnd = hWnd;
224 }
225
226 operator HWND() const
227 {
228 return m_hWnd;
229 }
230
231 static LPCTSTR GetWndClassName()
232 {
233 return NULL;
234 }
235
236 HDC BeginPaint(LPPAINTSTRUCT lpPaint)
237 {
238 ATLASSERT(::IsWindow(m_hWnd));
239 return ::BeginPaint(m_hWnd, lpPaint);
240 }
241
242 BOOL DestroyWindow()
243 {
244 ATLASSERT(::IsWindow(m_hWnd));
245
246 if (!::DestroyWindow(m_hWnd))
247 return FALSE;
248
249 m_hWnd = NULL;
250 return TRUE;
251 }
252
253 void EndPaint(LPPAINTSTRUCT lpPaint)
254 {
255 ATLASSERT(::IsWindow(m_hWnd));
256 ::EndPaint(m_hWnd, lpPaint);
257 }
258
259 BOOL GetClientRect(LPRECT lpRect) const
260 {
261 ATLASSERT(::IsWindow(m_hWnd));
262 return ::GetClientRect(m_hWnd, lpRect);
263 }
264
265 CWindow GetParent() const
266 {
267 ATLASSERT(::IsWindow(m_hWnd));
268 return CWindow(::GetParent(m_hWnd));
269 }
270
271 BOOL Invalidate(BOOL bErase = TRUE)
272 {
273 ATLASSERT(::IsWindow(m_hWnd));
274 return ::InvalidateRect(m_hWnd, NULL, bErase);
275 }
276
277 BOOL InvalidateRect(LPCRECT lpRect, BOOL bErase = TRUE)
278 {
279 ATLASSERT(::IsWindow(m_hWnd));
280 return ::InvalidateRect(m_hWnd, lpRect, bErase);
281 }
282
283 BOOL IsWindow() const
284 {
285 return ::IsWindow(m_hWnd);
286 }
287
288 BOOL KillTimer(UINT_PTR nIDEvent)
289 {
290 ATLASSERT(::IsWindow(m_hWnd));
291 return ::KillTimer(m_hWnd, nIDEvent);
292 }
293
294 BOOL LockWindowUpdate(BOOL bLock = TRUE)
295 {
296 ATLASSERT(::IsWindow(m_hWnd));
297 if (bLock)
298 return ::LockWindowUpdate(m_hWnd);
299 return ::LockWindowUpdate(NULL);
300 }
301
302 BOOL ScreenToClient(LPPOINT lpPoint) const
303 {
304 ATLASSERT(::IsWindow(m_hWnd));
305 return ::ScreenToClient(m_hWnd, lpPoint);
306 }
307
308 LRESULT SendMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
309 {
310 ATLASSERT(::IsWindow(m_hWnd));
311 return ::SendMessage(m_hWnd, message, wParam, lParam);
312 }
313
314 static LRESULT SendMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
315 {
316 ATLASSERT(::IsWindow(hWnd));
317 return ::SendMessage(hWnd, message, wParam, lParam);
318 }
319
320 HWND SetCapture()
321 {
322 ATLASSERT(::IsWindow(m_hWnd));
323 return ::SetCapture(m_hWnd);
324 }
325
326 HWND SetFocus()
327 {
328 ATLASSERT(::IsWindow(m_hWnd));
329 return ::SetFocus(m_hWnd);
330 }
331
332 UINT_PTR SetTimer(UINT_PTR nIDEvent, UINT nElapse, void (CALLBACK *lpfnTimer)(HWND, UINT, UINT_PTR, DWORD) = NULL)
333 {
334 ATLASSERT(::IsWindow(m_hWnd));
335 return ::SetTimer(m_hWnd, nIDEvent, nElapse, reinterpret_cast<TIMERPROC>(lpfnTimer));
336 }
337
338 BOOL SetWindowPos(HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags)
339 {
340 ATLASSERT(::IsWindow(m_hWnd));
341 return ::SetWindowPos(m_hWnd, hWndInsertAfter, x, y, cx, cy, nFlags);
342 }
343
344 BOOL SetWindowText(LPCTSTR lpszString)
345 {
346 ATLASSERT(::IsWindow(m_hWnd));
347 return ::SetWindowText(m_hWnd, lpszString);
348 }
349
350 BOOL ShowWindow(int nCmdShow)
351 {
352 ATLASSERT(::IsWindow(m_hWnd));
353 return ::ShowWindow(m_hWnd, nCmdShow);
354 }
355
356 };
357
358 _declspec(selectany) RECT CWindow::rcDefault = { CW_USEDEFAULT, CW_USEDEFAULT, 0, 0 };
359
360 template <class TBase = CWindow, class TWinTraits = CControlWinTraits>
361 class CWindowImplBaseT : public TBase, public CMessageMap
362 {
363 public:
364 enum { WINSTATE_DESTROYED = 0x00000001 };
365 DWORD m_dwState;
366 const _ATL_MSG *m_pCurrentMsg;
367 CWndProcThunk m_thunk;
368 WNDPROC m_pfnSuperWindowProc;
369 public:
370 CWindowImplBaseT()
371 {
372 m_dwState = 0;
373 m_pCurrentMsg = NULL;
374 m_pfnSuperWindowProc = ::DefWindowProc;
375 }
376
377 virtual void OnFinalMessage(HWND /* hWnd */)
378 {
379 }
380
381 BOOL SubclassWindow(HWND hWnd)
382 {
383 CWindowImplBaseT<TBase, TWinTraits> *pThis;
384 WNDPROC newWindowProc;
385 WNDPROC oldWindowProc;
386 BOOL result;
387
388 ATLASSERT(m_hWnd == NULL);
389 ATLASSERT(::IsWindow(hWnd));
390
391 pThis = reinterpret_cast<CWindowImplBaseT<TBase, TWinTraits>*>(this);
392
393 result = m_thunk.Init(GetWindowProc(), this);
394 if (result == FALSE)
395 return FALSE;
396 newWindowProc = m_thunk.GetWNDPROC();
397 oldWindowProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(newWindowProc)));
398 if (oldWindowProc == NULL)
399 return FALSE;
400 m_pfnSuperWindowProc = oldWindowProc;
401 pThis->m_hWnd = hWnd;
402 return TRUE;
403 }
404
405 virtual WNDPROC GetWindowProc()
406 {
407 return WindowProc;
408 }
409
410 static DWORD GetWndStyle(DWORD dwStyle)
411 {
412 return TWinTraits::GetWndStyle(dwStyle);
413 }
414
415 static DWORD GetWndExStyle(DWORD dwExStyle)
416 {
417 return TWinTraits::GetWndExStyle(dwExStyle);
418 }
419
420 LRESULT DefWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
421 {
422 CWindowImplBaseT<TBase, TWinTraits> *pThis;
423
424 pThis = reinterpret_cast<CWindowImplBaseT<TBase, TWinTraits> *>(this);
425 return ::CallWindowProc(m_pfnSuperWindowProc, pThis->m_hWnd, uMsg, wParam, lParam);
426 }
427
428 static LRESULT CALLBACK StartWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
429 {
430 CWindowImplBaseT<TBase, TWinTraits> *pThis;
431 WNDPROC newWindowProc;
432 WNDPROC GCCU(pOldProc);
433
434 pThis = reinterpret_cast<CWindowImplBaseT<TBase, TWinTraits> *>(_AtlWinModule.ExtractCreateWndData());
435 ATLASSERT(pThis != NULL);
436 if (pThis == NULL)
437 return 0;
438 pThis->m_thunk.Init(pThis->GetWindowProc(), pThis);
439 newWindowProc = pThis->m_thunk.GetWNDPROC();
440 pOldProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(newWindowProc)));
441 Unused(pOldProc); // TODO: should generate trace message if overwriting another subclass
442 pThis->m_hWnd = hWnd;
443 return newWindowProc(hWnd, uMsg, wParam, lParam);
444 }
445
446 static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
447 {
448 CWindowImplBaseT<TBase, TWinTraits> *pThis = reinterpret_cast<CWindowImplBaseT< TBase, TWinTraits> *>(hWnd);
449 _ATL_MSG msg(pThis->m_hWnd, uMsg, wParam, lParam);
450 LRESULT lResult;
451 const _ATL_MSG *previousMessage;
452 BOOL handled;
453 LONG_PTR saveWindowProc;
454
455 ATLASSERT(pThis != NULL && (pThis->m_dwState & WINSTATE_DESTROYED) == 0 && pThis->m_hWnd != NULL);
456 if (pThis == NULL || (pThis->m_dwState & WINSTATE_DESTROYED) != 0 || pThis->m_hWnd == NULL)
457 return 0;
458
459 hWnd = pThis->m_hWnd;
460 previousMessage = pThis->m_pCurrentMsg;
461 pThis->m_pCurrentMsg = &msg;
462
463 handled = pThis->ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult, 0);
464 ATLASSERT(pThis->m_pCurrentMsg == &msg);
465
466 if (handled == FALSE)
467 {
468 if (uMsg == WM_NCDESTROY)
469 {
470 saveWindowProc = ::GetWindowLongPtr(hWnd, GWLP_WNDPROC);
471 lResult = pThis->DefWindowProc(uMsg, wParam, lParam);
472 if (pThis->m_pfnSuperWindowProc != ::DefWindowProc && saveWindowProc == ::GetWindowLongPtr(hWnd, GWLP_WNDPROC))
473 ::SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(pThis->m_pfnSuperWindowProc));
474 pThis->m_dwState |= WINSTATE_DESTROYED;
475 }
476 else
477 lResult = pThis->DefWindowProc(uMsg, wParam, lParam);
478 }
479 ATLASSERT(pThis->m_pCurrentMsg == &msg);
480 pThis->m_pCurrentMsg = previousMessage;
481 if (previousMessage == NULL && (pThis->m_dwState & WINSTATE_DESTROYED) != 0)
482 {
483 pThis->m_dwState &= ~WINSTATE_DESTROYED;
484 pThis->m_hWnd = NULL;
485 pThis->OnFinalMessage(hWnd);
486 }
487 return lResult;
488 }
489
490 HWND Create(HWND hWndParent, _U_RECT rect, LPCTSTR szWindowName, DWORD dwStyle, DWORD dwExStyle,
491 _U_MENUorID MenuOrID, ATOM atom, LPVOID lpCreateParam)
492 {
493 HWND hWnd;
494
495 ATLASSERT(m_hWnd == NULL);
496 ATLASSERT(atom != 0);
497 if (atom == 0)
498 return NULL;
499 if (m_thunk.Init(NULL, NULL) == FALSE)
500 {
501 SetLastError(ERROR_OUTOFMEMORY);
502 return NULL;
503 }
504
505 _AtlWinModule.AddCreateWndData(&m_thunk.cd, this);
506 if (MenuOrID.m_hMenu == NULL && (dwStyle & WS_CHILD) != 0)
507 MenuOrID.m_hMenu = (HMENU)(UINT_PTR)this;
508 if (rect.m_lpRect == NULL)
509 rect.m_lpRect = &TBase::rcDefault;
510 hWnd = ::CreateWindowEx(dwExStyle, reinterpret_cast<LPCWSTR>(MAKEINTATOM(atom)), szWindowName, dwStyle, rect.m_lpRect->left,
511 rect.m_lpRect->top, rect.m_lpRect->right - rect.m_lpRect->left, rect.m_lpRect->bottom - rect.m_lpRect->top,
512 hWndParent, MenuOrID.m_hMenu, _AtlBaseModule.GetModuleInstance(), lpCreateParam);
513
514 ATLASSERT(m_hWnd == hWnd);
515
516 return hWnd;
517 }
518 };
519
520 template <class T, class TBase = CWindow, class TWinTraits = CControlWinTraits>
521 class CWindowImpl : public CWindowImplBaseT<TBase, TWinTraits>
522 {
523 public:
524 static LPCTSTR GetWndCaption()
525 {
526 return NULL;
527 }
528
529 HWND Create(HWND hWndParent, _U_RECT rect = NULL, LPCTSTR szWindowName = NULL, DWORD dwStyle = 0,
530 DWORD dwExStyle = 0, _U_MENUorID MenuOrID = 0U, LPVOID lpCreateParam = NULL)
531 {
532 CWindowImplBaseT<TBase, TWinTraits> *pThis;
533 ATOM atom;
534
535 ATLASSERT(m_hWnd == NULL);
536 pThis = reinterpret_cast<CWindowImplBaseT<TBase, TWinTraits>*>(this);
537
538 if (T::GetWndClassInfo().m_lpszOrigName == NULL)
539 T::GetWndClassInfo().m_lpszOrigName = pThis->GetWndClassName();
540 atom = T::GetWndClassInfo().Register(&pThis->m_pfnSuperWindowProc);
541
542 if (szWindowName == NULL)
543 szWindowName = T::GetWndCaption();
544 dwStyle = T::GetWndStyle(dwStyle);
545 dwExStyle = T::GetWndExStyle(dwExStyle);
546
547 return CWindowImplBaseT<TBase, TWinTraits>::Create(hWndParent, rect, szWindowName, dwStyle,
548 dwExStyle, MenuOrID, atom, lpCreateParam);
549 }
550 };
551
552 template <class TBase = CWindow, class TWinTraits = CControlWinTraits>
553 class CContainedWindowT : public TBase
554 {
555 public:
556 CWndProcThunk m_thunk;
557 LPCTSTR m_lpszClassName;
558 WNDPROC m_pfnSuperWindowProc;
559 CMessageMap *m_pObject;
560 DWORD m_dwMsgMapID;
561 const _ATL_MSG *m_pCurrentMsg;
562 public:
563 CContainedWindowT(CMessageMap *pObject, DWORD dwMsgMapID = 0)
564 {
565 m_lpszClassName = TBase::GetWndClassName();
566 m_pfnSuperWindowProc = ::DefWindowProc;
567 m_pObject = pObject;
568 m_dwMsgMapID = dwMsgMapID;
569 m_pCurrentMsg = NULL;
570 }
571
572 CContainedWindowT(LPTSTR lpszClassName, CMessageMap *pObject, DWORD dwMsgMapID = 0)
573 {
574 m_lpszClassName = lpszClassName;
575 m_pfnSuperWindowProc = ::DefWindowProc;
576 m_pObject = pObject;
577 m_dwMsgMapID = dwMsgMapID;
578 m_pCurrentMsg = NULL;
579 }
580
581 LRESULT DefWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
582 {
583 CWindowImplBaseT<TBase, TWinTraits> *pThis;
584
585 pThis = reinterpret_cast<CWindowImplBaseT<TBase, TWinTraits> *>(this);
586 return ::CallWindowProc(m_pfnSuperWindowProc, pThis->m_hWnd, uMsg, wParam, lParam);
587 }
588
589 BOOL SubclassWindow(HWND hWnd)
590 {
591 CContainedWindowT<TBase> *pThis;
592 WNDPROC newWindowProc;
593 WNDPROC oldWindowProc;
594 BOOL result;
595
596 ATLASSERT(m_hWnd == NULL);
597 ATLASSERT(::IsWindow(hWnd));
598
599 pThis = reinterpret_cast<CContainedWindowT<TBase> *>(this);
600
601 result = m_thunk.Init(WindowProc, pThis);
602 if (result == FALSE)
603 return FALSE;
604 newWindowProc = m_thunk.GetWNDPROC();
605 oldWindowProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(newWindowProc)));
606 if (oldWindowProc == NULL)
607 return FALSE;
608 m_pfnSuperWindowProc = oldWindowProc;
609 pThis->m_hWnd = hWnd;
610 return TRUE;
611 }
612
613 static LRESULT CALLBACK StartWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
614 {
615 CContainedWindowT<TBase> *pThis;
616 WNDPROC newWindowProc;
617 WNDPROC GCCU(pOldProc);
618
619 pThis = reinterpret_cast<CContainedWindowT<TBase> *>(_AtlWinModule.ExtractCreateWndData());
620 ATLASSERT(pThis != NULL);
621 if (pThis == NULL)
622 return 0;
623 pThis->m_thunk.Init(WindowProc, pThis);
624 newWindowProc = pThis->m_thunk.GetWNDPROC();
625 pOldProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(newWindowProc)));
626 Unused(pOldProc); // TODO: should generate trace message if overwriting another subclass
627 pThis->m_hWnd = hWnd;
628 return newWindowProc(hWnd, uMsg, wParam, lParam);
629 }
630
631 static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
632 {
633 CContainedWindowT<TBase> *pThis = reinterpret_cast<CContainedWindowT<TBase> *>(hWnd);
634 _ATL_MSG msg(pThis->m_hWnd, uMsg, wParam, lParam);
635 LRESULT lResult;
636 const _ATL_MSG *previousMessage;
637 BOOL handled;
638 LONG_PTR saveWindowProc;
639
640 ATLASSERT(pThis != NULL && pThis->m_hWnd != NULL && pThis->m_pObject != NULL);
641 if (pThis == NULL || pThis->m_hWnd == NULL || pThis->m_pObject == NULL)
642 return 0;
643
644 hWnd = pThis->m_hWnd;
645 previousMessage = pThis->m_pCurrentMsg;
646 pThis->m_pCurrentMsg = &msg;
647
648 handled = pThis->m_pObject->ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult, pThis->m_dwMsgMapID);
649 ATLASSERT(pThis->m_pCurrentMsg == &msg);
650
651 pThis->m_pCurrentMsg = previousMessage;
652 if (handled == FALSE)
653 {
654 if (uMsg == WM_NCDESTROY)
655 {
656 saveWindowProc = ::GetWindowLongPtr(hWnd, GWLP_WNDPROC);
657 lResult = pThis->DefWindowProc(uMsg, wParam, lParam);
658 if (pThis->m_pfnSuperWindowProc != ::DefWindowProc && saveWindowProc == ::GetWindowLongPtr(hWnd, GWLP_WNDPROC))
659 ::SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(pThis->m_pfnSuperWindowProc));
660 pThis->m_hWnd = NULL;
661 }
662 else
663 lResult = pThis->DefWindowProc(uMsg, wParam, lParam);
664 }
665 return lResult;
666 }
667
668 };
669 typedef CContainedWindowT<CWindow> CContainedWindow;
670
671 #define BEGIN_MSG_MAP(theClass) \
672 public: \
673 BOOL ProcessWindowMessage(HWND GCCU(hWnd), UINT GCCU(uMsg), WPARAM GCCU(wParam), LPARAM GCCU(lParam), LRESULT &GCCU(lResult), DWORD dwMsgMapID = 0) \
674 { \
675 BOOL GCCU(bHandled) = TRUE; \
676 Unused(hWnd); \
677 Unused(uMsg); \
678 Unused(wParam); \
679 Unused(lParam); \
680 Unused(lResult); \
681 Unused(bHandled); \
682 switch(dwMsgMapID) \
683 { \
684 case 0:
685
686 #define END_MSG_MAP() \
687 break; \
688 default: \
689 ATLASSERT(FALSE); \
690 break; \
691 } \
692 return FALSE; \
693 }
694
695 #define MESSAGE_HANDLER(msg, func) \
696 if (uMsg == msg) \
697 { \
698 bHandled = TRUE; \
699 lResult = func(uMsg, wParam, lParam, bHandled); \
700 if (bHandled) \
701 return TRUE; \
702 }
703
704 #define MESSAGE_RANGE_HANDLER(msgFirst, msgLast, func) \
705 if (uMsg >= msgFirst && uMsg <= msgLast) \
706 { \
707 bHandled = TRUE; \
708 lResult = func(uMsg, wParam, lParam, bHandled); \
709 if (bHandled) \
710 return TRUE; \
711 }
712
713 #define COMMAND_ID_HANDLER(id, func) \
714 if (uMsg == WM_COMMAND && id == LOWORD(wParam)) \
715 { \
716 bHandled = TRUE; \
717 lResult = func(HIWORD(wParam), LOWORD(wParam), (HWND)lParam, bHandled); \
718 if (bHandled) \
719 return TRUE; \
720 }
721
722 #define COMMAND_RANGE_HANDLER(idFirst, idLast, func) \
723 if (uMsg == WM_COMMAND && LOWORD(wParam) >= idFirst && LOWORD(wParam) <= idLast) \
724 { \
725 bHandled = TRUE; \
726 lResult = func(HIWORD(wParam), LOWORD(wParam), (HWND)lParam, bHandled); \
727 if (bHandled) \
728 return TRUE; \
729 }
730
731 #define NOTIFY_CODE_HANDLER(cd, func) \
732 if(uMsg == WM_NOTIFY && cd == ((LPNMHDR)lParam)->code) \
733 { \
734 bHandled = TRUE; \
735 lResult = func((int)wParam, (LPNMHDR)lParam, bHandled); \
736 if (bHandled) \
737 return TRUE; \
738 }
739
740 #define NOTIFY_HANDLER(id, cd, func) \
741 if(uMsg == WM_NOTIFY && id == ((LPNMHDR)lParam)->idFrom && cd == ((LPNMHDR)lParam)->code) \
742 { \
743 bHandled = TRUE; \
744 lResult = func((int)wParam, (LPNMHDR)lParam, bHandled); \
745 if (bHandled) \
746 return TRUE; \
747 }
748
749 #define DECLARE_WND_CLASS_EX(WndClassName, style, bkgnd) \
750 static ATL::CWndClassInfo& GetWndClassInfo() \
751 { \
752 static ATL::CWndClassInfo wc = \
753 { \
754 { sizeof(WNDCLASSEX), style, StartWindowProc, \
755 0, 0, NULL, NULL, NULL, (HBRUSH)(bkgnd + 1), NULL, WndClassName, NULL }, \
756 NULL, NULL, IDC_ARROW, TRUE, 0, _T("") \
757 }; \
758 return wc; \
759 }
760
761 struct _ATL_WNDCLASSINFOW
762 {
763 WNDCLASSEXW m_wc;
764 LPCWSTR m_lpszOrigName;
765 WNDPROC pWndProc;
766 LPCWSTR m_lpszCursorID;
767 BOOL m_bSystemCursor;
768 ATOM m_atom;
769 WCHAR m_szAutoName[5 + sizeof(void *)];
770
771 ATOM Register(WNDPROC *p)
772 {
773 if (m_atom == 0)
774 m_atom = RegisterClassEx(&m_wc);
775 return m_atom;
776 }
777 };
778
779 }; // namespace ATL