Add policy_templates target for GN.
[chromium-blink-merge.git] / third_party / wtl / include / atlapp.h
blob4a5e6a7064a85ab2f3715ce9781f6afe22b684d2
1 // Windows Template Library - WTL version 8.0
2 // Copyright (C) Microsoft Corporation. All rights reserved.
3 //
4 // This file is a part of the Windows Template Library.
5 // The use and distribution terms for this software are covered by the
6 // Microsoft Permissive License (Ms-PL) which can be found in the file
7 // Ms-PL.txt at the root of this distribution.
9 #ifndef __ATLAPP_H__
10 #define __ATLAPP_H__
12 #pragma once
14 #ifndef __cplusplus
15 #error ATL requires C++ compilation (use a .cpp suffix)
16 #endif
18 #ifndef __ATLBASE_H__
19 #error atlapp.h requires atlbase.h to be included first
20 #endif
22 #ifndef _WIN32_WCE
23 #if (WINVER < 0x0400)
24 #error WTL requires Windows version 4.0 or higher
25 #endif
27 #if (_WIN32_IE < 0x0300)
28 #error WTL requires IE version 3.0 or higher
29 #endif
30 #endif
32 #ifdef _ATL_NO_COMMODULE
33 #error WTL requires that _ATL_NO_COMMODULE is not defined
34 #endif // _ATL_NO_COMMODULE
36 #if defined(_WIN32_WCE) && defined(_ATL_MIN_CRT)
37 #pragma message("Warning: WTL for Windows CE doesn't use _ATL_MIN_CRT")
38 #endif // defined(_WIN32_WCE) && defined(_ATL_MIN_CRT)
40 #include <limits.h>
41 #if !defined(_ATL_MIN_CRT) && defined(_MT) && !defined(_WIN32_WCE)
42 #include <process.h> // for _beginthreadex
43 #endif
45 #if (_ATL_VER < 0x0800) && !defined(_DEBUG)
46 #include <stdio.h>
47 #endif
49 #include <commctrl.h>
50 #ifndef _WIN32_WCE
51 #pragma comment(lib, "comctl32.lib")
52 #endif // !_WIN32_WCE
54 #ifndef _WIN32_WCE
55 #include "atlres.h"
56 #else // CE specific
57 #include "atlresce.h"
58 #endif // _WIN32_WCE
60 // We need to disable this warning because of template class arguments
61 #pragma warning(disable: 4127)
64 ///////////////////////////////////////////////////////////////////////////////
65 // WTL version number
67 #define _WTL_VER 0x0800
70 ///////////////////////////////////////////////////////////////////////////////
71 // Classes in this file:
73 // CMessageFilter
74 // CIdleHandler
75 // CMessageLoop
77 // CAppModule
78 // CServerAppModule
80 // Global functions:
81 // AtlGetDefaultGuiFont()
82 // AtlCreateBoldFont()
83 // AtlInitCommonControls()
86 ///////////////////////////////////////////////////////////////////////////////
87 // Global support for Windows CE
89 #ifdef _WIN32_WCE
91 #ifndef SW_SHOWDEFAULT
92 #define SW_SHOWDEFAULT SW_SHOWNORMAL
93 #endif // !SW_SHOWDEFAULT
95 // These get's OR-ed in a constant and will have no effect.
96 // Defining them reduces the number of #ifdefs required for CE.
97 #define LR_DEFAULTSIZE 0
98 #define LR_LOADFROMFILE 0
100 #ifndef SM_CXCURSOR
101 #define SM_CXCURSOR 13
102 #endif
103 #ifndef SM_CYCURSOR
104 #define SM_CYCURSOR 14
105 #endif
107 inline BOOL IsMenu(HMENU hMenu)
109 MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
110 ::SetLastError(0);
111 BOOL bRet = ::GetMenuItemInfo(hMenu, 0, TRUE, &mii);
112 if(!bRet)
113 bRet = (::GetLastError() != ERROR_INVALID_MENU_HANDLE) ? TRUE : FALSE;
114 return bRet;
117 #if (_WIN32_WCE >= 410)
118 extern "C" void WINAPI ListView_SetItemSpacing(HWND hwndLV, int iHeight);
119 #endif // (_WIN32_WCE >= 410)
121 inline int MulDiv(IN int nNumber, IN int nNumerator, IN int nDenominator)
123 __int64 multiple = nNumber * nNumerator;
124 return static_cast<int>(multiple / nDenominator);
127 #if (_ATL_VER >= 0x0800)
129 #ifndef _WTL_KEEP_WS_OVERLAPPEDWINDOW
130 #ifdef WS_OVERLAPPEDWINDOW
131 #undef WS_OVERLAPPEDWINDOW
132 #define WS_OVERLAPPEDWINDOW 0
133 #endif // WS_OVERLAPPEDWINDOW
134 #endif // !_WTL_KEEP_WS_OVERLAPPEDWINDOW
136 #ifndef RDW_FRAME
137 #define RDW_FRAME 0
138 #endif // !RDW_FRAME
140 #ifndef WM_WINDOWPOSCHANGING
141 #define WM_WINDOWPOSCHANGING 0
142 #endif // !WM_WINDOWPOSCHANGING
144 #define FreeResource(x)
145 #define UnlockResource(x)
147 namespace ATL
149 inline HRESULT CComModule::RegisterClassObjects(DWORD /*dwClsContext*/, DWORD /*dwFlags*/) throw()
150 { return E_NOTIMPL; }
151 inline HRESULT CComModule::RevokeClassObjects() throw()
152 { return E_NOTIMPL; }
153 }; // namespace ATL
155 #ifndef lstrlenW
156 #define lstrlenW (int)ATL::lstrlenW
157 #endif // lstrlenW
159 inline int WINAPI lstrlenA(LPCSTR lpszString)
160 { return ATL::lstrlenA(lpszString); }
162 #ifdef lstrcpyn
163 #undef lstrcpyn
164 #define lstrcpyn ATL::lstrcpynW
165 #endif // lstrcpyn
167 #ifndef SetWindowLongPtrW
168 inline LONG_PTR tmp_SetWindowLongPtrW( HWND hWnd, int nIndex, LONG_PTR dwNewLong )
170 return( ::SetWindowLongW( hWnd, nIndex, LONG( dwNewLong ) ) );
172 #define SetWindowLongPtrW tmp_SetWindowLongPtrW
173 #endif
175 #ifndef GetWindowLongPtrW
176 inline LONG_PTR tmp_GetWindowLongPtrW( HWND hWnd, int nIndex )
178 return( ::GetWindowLongW( hWnd, nIndex ) );
180 #define GetWindowLongPtrW tmp_GetWindowLongPtrW
181 #endif
183 #ifndef LongToPtr
184 #define LongToPtr(x) ((void*)x)
185 #endif
187 #ifndef PtrToInt
188 #define PtrToInt( p ) ((INT)(INT_PTR) (p) )
189 #endif
191 #else // !(_ATL_VER >= 0x0800)
193 #ifdef lstrlenW
194 #undef lstrlenW
195 #define lstrlenW (int)::wcslen
196 #endif // lstrlenW
198 #define lstrlenA (int)strlen
200 #ifndef lstrcpyn
201 inline LPTSTR lstrcpyn(LPTSTR lpstrDest, LPCTSTR lpstrSrc, int nLength)
203 if(lpstrDest == NULL || lpstrSrc == NULL || nLength <= 0)
204 return NULL;
205 int nLen = __min(lstrlen(lpstrSrc), nLength - 1);
206 LPTSTR lpstrRet = (LPTSTR)memcpy(lpstrDest, lpstrSrc, nLen * sizeof(TCHAR));
207 lpstrDest[nLen] = 0;
208 return lpstrRet;
210 #endif // !lstrcpyn
212 #ifndef lstrcpynW
213 inline LPWSTR lstrcpynW(LPWSTR lpstrDest, LPCWSTR lpstrSrc, int nLength)
215 return lstrcpyn(lpstrDest, lpstrSrc, nLength); // WinCE is Unicode only
217 #endif // !lstrcpynW
219 #ifndef lstrcpynA
220 inline LPSTR lstrcpynA(LPSTR lpstrDest, LPCSTR lpstrSrc, int nLength)
222 if(lpstrDest == NULL || lpstrSrc == NULL || nLength <= 0)
223 return NULL;
224 int nLen = __min(lstrlenA(lpstrSrc), nLength - 1);
225 LPSTR lpstrRet = (LPSTR)memcpy(lpstrDest, lpstrSrc, nLen * sizeof(char));
226 lpstrDest[nLen] = 0;
227 return lpstrRet;
229 #endif // !lstrcpyn
231 #ifdef TrackPopupMenu
232 #undef TrackPopupMenu
233 #endif // TrackPopupMenu
235 #define DECLARE_WND_CLASS_EX(WndClassName, style, bkgnd) \
236 static CWndClassInfo& GetWndClassInfo() \
238 static CWndClassInfo wc = \
240 { style, StartWindowProc, \
241 0, 0, NULL, NULL, NULL, (HBRUSH)(bkgnd + 1), NULL, WndClassName }, \
242 NULL, NULL, IDC_ARROW, TRUE, 0, _T("") \
243 }; \
244 return wc; \
247 #ifndef _MAX_FNAME
248 #define _MAX_FNAME _MAX_PATH
249 #endif // _MAX_FNAME
251 #if (_WIN32_WCE < 400)
252 #define MAKEINTATOM(i) (LPTSTR)((ULONG_PTR)((WORD)(i)))
253 #endif // (_WIN32_WCE < 400)
255 #if (_WIN32_WCE < 410)
256 #define WHEEL_PAGESCROLL (UINT_MAX)
257 #define WHEEL_DELTA 120
258 #endif // (_WIN32_WCE < 410)
260 #ifdef DrawIcon
261 #undef DrawIcon
262 #endif
264 #ifndef VARCMP_LT
265 #define VARCMP_LT 0
266 #endif
267 #ifndef VARCMP_EQ
268 #define VARCMP_EQ 1
269 #endif
270 #ifndef VARCMP_GT
271 #define VARCMP_GT 2
272 #endif
273 #ifndef VARCMP_NULL
274 #define VARCMP_NULL 3
275 #endif
277 #ifndef RDW_ALLCHILDREN
278 #define RDW_ALLCHILDREN 0
279 #endif
281 #endif // !(_ATL_VER >= 0x0800)
283 #endif // _WIN32_WCE
286 ///////////////////////////////////////////////////////////////////////////////
287 // Global support for using original VC++ 6.0 headers with WTL
289 #ifndef _ATL_NO_OLD_HEADERS_WIN64
290 #if !defined(_WIN64) && (_ATL_VER < 0x0700)
292 #ifndef PSM_INSERTPAGE
293 #define PSM_INSERTPAGE (WM_USER + 119)
294 #endif // !PSM_INSERTPAGE
296 #ifndef GetClassLongPtr
297 #define GetClassLongPtrA GetClassLongA
298 #define GetClassLongPtrW GetClassLongW
299 #ifdef UNICODE
300 #define GetClassLongPtr GetClassLongPtrW
301 #else
302 #define GetClassLongPtr GetClassLongPtrA
303 #endif // !UNICODE
304 #endif // !GetClassLongPtr
306 #ifndef GCLP_HICONSM
307 #define GCLP_HICONSM (-34)
308 #endif // !GCLP_HICONSM
310 #ifndef GetWindowLongPtr
311 #define GetWindowLongPtrA GetWindowLongA
312 #define GetWindowLongPtrW GetWindowLongW
313 #ifdef UNICODE
314 #define GetWindowLongPtr GetWindowLongPtrW
315 #else
316 #define GetWindowLongPtr GetWindowLongPtrA
317 #endif // !UNICODE
318 #endif // !GetWindowLongPtr
320 #ifndef SetWindowLongPtr
321 #define SetWindowLongPtrA SetWindowLongA
322 #define SetWindowLongPtrW SetWindowLongW
323 #ifdef UNICODE
324 #define SetWindowLongPtr SetWindowLongPtrW
325 #else
326 #define SetWindowLongPtr SetWindowLongPtrA
327 #endif // !UNICODE
328 #endif // !SetWindowLongPtr
330 #ifndef GWLP_WNDPROC
331 #define GWLP_WNDPROC (-4)
332 #endif
333 #ifndef GWLP_HINSTANCE
334 #define GWLP_HINSTANCE (-6)
335 #endif
336 #ifndef GWLP_HWNDPARENT
337 #define GWLP_HWNDPARENT (-8)
338 #endif
339 #ifndef GWLP_USERDATA
340 #define GWLP_USERDATA (-21)
341 #endif
342 #ifndef GWLP_ID
343 #define GWLP_ID (-12)
344 #endif
346 #ifndef DWLP_MSGRESULT
347 #define DWLP_MSGRESULT 0
348 #endif
350 typedef long LONG_PTR;
351 typedef unsigned long ULONG_PTR;
352 typedef ULONG_PTR DWORD_PTR;
354 #ifndef HandleToUlong
355 #define HandleToUlong( h ) ((ULONG)(ULONG_PTR)(h) )
356 #endif
357 #ifndef HandleToLong
358 #define HandleToLong( h ) ((LONG)(LONG_PTR) (h) )
359 #endif
360 #ifndef LongToHandle
361 #define LongToHandle( h) ((HANDLE)(LONG_PTR) (h))
362 #endif
363 #ifndef PtrToUlong
364 #define PtrToUlong( p ) ((ULONG)(ULONG_PTR) (p) )
365 #endif
366 #ifndef PtrToLong
367 #define PtrToLong( p ) ((LONG)(LONG_PTR) (p) )
368 #endif
369 #ifndef PtrToUint
370 #define PtrToUint( p ) ((UINT)(UINT_PTR) (p) )
371 #endif
372 #ifndef PtrToInt
373 #define PtrToInt( p ) ((INT)(INT_PTR) (p) )
374 #endif
375 #ifndef PtrToUshort
376 #define PtrToUshort( p ) ((unsigned short)(ULONG_PTR)(p) )
377 #endif
378 #ifndef PtrToShort
379 #define PtrToShort( p ) ((short)(LONG_PTR)(p) )
380 #endif
381 #ifndef IntToPtr
382 #define IntToPtr( i ) ((VOID *)(INT_PTR)((int)i))
383 #endif
384 #ifndef UIntToPtr
385 #define UIntToPtr( ui ) ((VOID *)(UINT_PTR)((unsigned int)ui))
386 #endif
387 #ifndef LongToPtr
388 #define LongToPtr( l ) ((VOID *)(LONG_PTR)((long)l))
389 #endif
390 #ifndef ULongToPtr
391 #define ULongToPtr( ul ) ((VOID *)(ULONG_PTR)((unsigned long)ul))
392 #endif
394 #endif // !defined(_WIN64) && (_ATL_VER < 0x0700)
395 #endif // !_ATL_NO_OLD_HEADERS_WIN64
398 ///////////////////////////////////////////////////////////////////////////////
399 // Global support for SecureHelper functions
401 #ifndef _TRUNCATE
402 #define _TRUNCATE ((size_t)-1)
403 #endif
405 #ifndef _ERRCODE_DEFINED
406 #define _ERRCODE_DEFINED
407 typedef int errno_t;
408 #endif
410 #ifndef _SECURECRT_ERRCODE_VALUES_DEFINED
411 #define _SECURECRT_ERRCODE_VALUES_DEFINED
412 #define EINVAL 22
413 #define STRUNCATE 80
414 #endif
416 #ifndef _countof
417 #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
418 #endif
421 ///////////////////////////////////////////////////////////////////////////////
422 // Miscellaneous global support
424 // define useful macros from winuser.h
425 #ifndef IS_INTRESOURCE
426 #define IS_INTRESOURCE(_r) (((ULONG_PTR)(_r) >> 16) == 0)
427 #endif // IS_INTRESOURCE
429 // protect template members from windowsx.h macros
430 #ifdef _INC_WINDOWSX
431 #undef SubclassWindow
432 #endif // _INC_WINDOWSX
434 // define useful macros from windowsx.h
435 #ifndef GET_X_LPARAM
436 #define GET_X_LPARAM(lParam) ((int)(short)LOWORD(lParam))
437 #endif
438 #ifndef GET_Y_LPARAM
439 #define GET_Y_LPARAM(lParam) ((int)(short)HIWORD(lParam))
440 #endif
442 // Dummy structs for compiling with /CLR
443 #if (_MSC_VER >= 1300) && defined(_MANAGED)
444 __if_not_exists(_IMAGELIST::_IMAGELIST) { struct _IMAGELIST { }; }
445 __if_not_exists(_TREEITEM::_TREEITEM) { struct _TREEITEM { }; }
446 __if_not_exists(_PSP::_PSP) { struct _PSP { }; }
447 #endif
449 // Define ATLVERIFY macro for ATL3
450 #if (_ATL_VER < 0x0700)
451 #ifndef ATLVERIFY
452 #ifdef _DEBUG
453 #define ATLVERIFY(expr) ATLASSERT(expr)
454 #else
455 #define ATLVERIFY(expr) (expr)
456 #endif // DEBUG
457 #endif // ATLVERIFY
458 #endif // (_ATL_VER < 0x0700)
460 // Forward declaration for ATL3 and ATL11 fix
461 #if (((_ATL_VER < 0x0700) && defined(_ATL_DLL)) || (_ATL_VER >= 0x0B00)) && !defined(_WIN32_WCE)
462 namespace ATL { HRESULT AtlGetCommCtrlVersion(LPDWORD pdwMajor, LPDWORD pdwMinor); };
463 #endif
466 namespace WTL
469 #if (_ATL_VER >= 0x0700)
470 DECLARE_TRACE_CATEGORY(atlTraceUI);
471 #ifdef _DEBUG
472 __declspec(selectany) ATL::CTraceCategory atlTraceUI(_T("atlTraceUI"));
473 #endif // _DEBUG
474 #else // !(_ATL_VER >= 0x0700)
475 enum wtlTraceFlags
477 atlTraceUI = 0x10000000
479 #endif // !(_ATL_VER >= 0x0700)
481 // Windows version helper
482 inline bool AtlIsOldWindows()
484 OSVERSIONINFO ovi = { 0 };
485 ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
486 BOOL bRet = ::GetVersionEx(&ovi);
487 return (!bRet || !((ovi.dwMajorVersion >= 5) || (ovi.dwMajorVersion == 4 && ovi.dwMinorVersion >= 90)));
490 // default GUI font helper
491 inline HFONT AtlGetDefaultGuiFont()
493 #ifndef _WIN32_WCE
494 return (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
495 #else // CE specific
496 return (HFONT)::GetStockObject(SYSTEM_FONT);
497 #endif // _WIN32_WCE
500 // bold font helper (NOTE: Caller owns the font, and should destroy it when done using it)
501 inline HFONT AtlCreateBoldFont(HFONT hFont = NULL)
503 if(hFont == NULL)
504 hFont = AtlGetDefaultGuiFont();
505 ATLASSERT(hFont != NULL);
506 HFONT hFontBold = NULL;
507 LOGFONT lf = { 0 };
508 if(::GetObject(hFont, sizeof(LOGFONT), &lf) == sizeof(LOGFONT))
510 lf.lfWeight = FW_BOLD;
511 hFontBold = ::CreateFontIndirect(&lf);
512 ATLASSERT(hFontBold != NULL);
514 else
516 ATLASSERT(FALSE);
518 return hFontBold;
521 // Common Controls initialization helper
522 inline BOOL AtlInitCommonControls(DWORD dwFlags)
524 INITCOMMONCONTROLSEX iccx = { sizeof(INITCOMMONCONTROLSEX), dwFlags };
525 BOOL bRet = ::InitCommonControlsEx(&iccx);
526 ATLASSERT(bRet);
527 return bRet;
531 ///////////////////////////////////////////////////////////////////////////////
532 // RunTimeHelper - helper functions for Windows version and structure sizes
534 // Not for Windows CE
535 #if defined(_WIN32_WCE) && !defined(_WTL_NO_RUNTIME_STRUCT_SIZE)
536 #define _WTL_NO_RUNTIME_STRUCT_SIZE
537 #endif
539 #ifndef _WTL_NO_RUNTIME_STRUCT_SIZE
541 #ifndef _SIZEOF_STRUCT
542 #define _SIZEOF_STRUCT(structname, member) (((int)((LPBYTE)(&((structname*)0)->member) - ((LPBYTE)((structname*)0)))) + sizeof(((structname*)0)->member))
543 #endif
545 #if (_WIN32_WINNT >= 0x0600) && !defined(REBARBANDINFO_V6_SIZE)
546 #define REBARBANDINFO_V6_SIZE _SIZEOF_STRUCT(REBARBANDINFO, cxHeader)
547 #endif // (_WIN32_WINNT >= 0x0600) && !defined(REBARBANDINFO_V6_SIZE)
549 #if (_WIN32_WINNT >= 0x0600) && !defined(LVGROUP_V5_SIZE)
550 #define LVGROUP_V5_SIZE _SIZEOF_STRUCT(LVGROUP, uAlign)
551 #endif // (_WIN32_WINNT >= 0x0600) && !defined(LVGROUP_V5_SIZE)
553 #if (_WIN32_WINNT >= 0x0600) && !defined(LVTILEINFO_V5_SIZE)
554 #define LVTILEINFO_V5_SIZE _SIZEOF_STRUCT(LVTILEINFO, puColumns)
555 #endif // (_WIN32_WINNT >= 0x0600) && !defined(LVTILEINFO_V5_SIZE)
557 #if defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN) && !defined(MCHITTESTINFO_V1_SIZE)
558 #define MCHITTESTINFO_V1_SIZE _SIZEOF_STRUCT(MCHITTESTINFO, st)
559 #endif // defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN) && !defined(MCHITTESTINFO_V1_SIZE)
561 #if !defined(_WIN32_WCE) && (WINVER >= 0x0600) && !defined(NONCLIENTMETRICS_V1_SIZE)
562 #define NONCLIENTMETRICS_V1_SIZE _SIZEOF_STRUCT(NONCLIENTMETRICS, lfMessageFont)
563 #endif // !defined(_WIN32_WCE) && (WINVER >= 0x0600) && !defined(NONCLIENTMETRICS_V1_SIZE)
565 #endif // !_WTL_NO_RUNTIME_STRUCT_SIZE
567 namespace RunTimeHelper
569 #ifndef _WIN32_WCE
570 inline bool IsCommCtrl6()
572 DWORD dwMajor = 0, dwMinor = 0;
573 HRESULT hRet = ATL::AtlGetCommCtrlVersion(&dwMajor, &dwMinor);
574 return (SUCCEEDED(hRet) && (dwMajor >= 6));
577 inline bool IsVista()
579 OSVERSIONINFO ovi = { sizeof(OSVERSIONINFO) };
580 BOOL bRet = ::GetVersionEx(&ovi);
581 return ((bRet != FALSE) && (ovi.dwMajorVersion >= 6));
583 #endif // !_WIN32_WCE
585 inline int SizeOf_REBARBANDINFO()
587 int nSize = sizeof(REBARBANDINFO);
588 #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
589 if(!(IsVista() && IsCommCtrl6()))
590 nSize = REBARBANDINFO_V6_SIZE;
591 #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
592 return nSize;
595 #if (_WIN32_WINNT >= 0x501)
596 inline int SizeOf_LVGROUP()
598 int nSize = sizeof(LVGROUP);
599 #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
600 if(!IsVista())
601 nSize = LVGROUP_V5_SIZE;
602 #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
603 return nSize;
606 inline int SizeOf_LVTILEINFO()
608 int nSize = sizeof(LVTILEINFO);
609 #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
610 if(!IsVista())
611 nSize = LVTILEINFO_V5_SIZE;
612 #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
613 return nSize;
615 #endif // (_WIN32_WINNT >= 0x501)
617 inline int SizeOf_MCHITTESTINFO()
619 int nSize = sizeof(MCHITTESTINFO);
620 #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
621 if(!(IsVista() && IsCommCtrl6()))
622 nSize = MCHITTESTINFO_V1_SIZE;
623 #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
624 return nSize;
627 #ifndef _WIN32_WCE
628 inline int SizeOf_NONCLIENTMETRICS()
630 int nSize = sizeof(NONCLIENTMETRICS);
631 #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (WINVER >= 0x0600)
632 if(!IsVista())
633 nSize = NONCLIENTMETRICS_V1_SIZE;
634 #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (WINVER >= 0x0600)
635 return nSize;
637 #endif // !_WIN32_WCE
641 ///////////////////////////////////////////////////////////////////////////////
642 // ModuleHelper - helper functions for ATL3 and ATL7 module classes
644 namespace ModuleHelper
646 inline HINSTANCE GetModuleInstance()
648 #if (_ATL_VER >= 0x0700)
649 return ATL::_AtlBaseModule.GetModuleInstance();
650 #else // !(_ATL_VER >= 0x0700)
651 return ATL::_pModule->GetModuleInstance();
652 #endif // !(_ATL_VER >= 0x0700)
655 inline HINSTANCE GetResourceInstance()
657 #if (_ATL_VER >= 0x0700)
658 return ATL::_AtlBaseModule.GetResourceInstance();
659 #else // !(_ATL_VER >= 0x0700)
660 return ATL::_pModule->GetResourceInstance();
661 #endif // !(_ATL_VER >= 0x0700)
664 inline void AddCreateWndData(ATL::_AtlCreateWndData* pData, void* pObject)
666 #if (_ATL_VER >= 0x0700)
667 ATL::_AtlWinModule.AddCreateWndData(pData, pObject);
668 #else // !(_ATL_VER >= 0x0700)
669 ATL::_pModule->AddCreateWndData(pData, pObject);
670 #endif // !(_ATL_VER >= 0x0700)
673 inline void* ExtractCreateWndData()
675 #if (_ATL_VER >= 0x0700)
676 return ATL::_AtlWinModule.ExtractCreateWndData();
677 #else // !(_ATL_VER >= 0x0700)
678 return ATL::_pModule->ExtractCreateWndData();
679 #endif // !(_ATL_VER >= 0x0700)
684 ///////////////////////////////////////////////////////////////////////////////
685 // SecureHelper - helper functions for VS2005 secure CRT
687 namespace SecureHelper
689 inline void strcpyA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc)
691 #if _SECURE_ATL
692 ATL::Checked::strcpy_s(lpstrDest, cchDest, lpstrSrc);
693 #else
694 if(cchDest > (size_t)lstrlenA(lpstrSrc))
695 ATLVERIFY(lstrcpyA(lpstrDest, lpstrSrc) != NULL);
696 else
697 ATLASSERT(FALSE);
698 #endif
701 inline void strcpyW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc)
703 #if _SECURE_ATL
704 ATL::Checked::wcscpy_s(lpstrDest, cchDest, lpstrSrc);
705 #else
706 if(cchDest > (size_t)lstrlenW(lpstrSrc))
707 ATLVERIFY(lstrcpyW(lpstrDest, lpstrSrc) != NULL);
708 else
709 ATLASSERT(FALSE);
710 #endif
713 inline void strcpy_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc)
715 #ifdef _UNICODE
716 strcpyW_x(lpstrDest, cchDest, lpstrSrc);
717 #else
718 strcpyA_x(lpstrDest, cchDest, lpstrSrc);
719 #endif
722 inline errno_t strncpyA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc, size_t cchCount)
724 #if _SECURE_ATL
725 return ATL::Checked::strncpy_s(lpstrDest, cchDest, lpstrSrc, cchCount);
726 #else
727 errno_t nRet = 0;
728 if(lpstrDest == NULL || cchDest == 0 || lpstrSrc == NULL)
730 nRet = EINVAL;
732 else if(cchCount == _TRUNCATE)
734 cchCount = __min(cchDest - 1, size_t(lstrlenA(lpstrSrc)));
735 nRet = STRUNCATE;
737 else if(cchDest <= cchCount)
739 lpstrDest[0] = 0;
740 nRet = EINVAL;
742 if(nRet == 0 || nRet == STRUNCATE)
743 nRet = (lstrcpynA(lpstrDest, lpstrSrc, (int)cchCount + 1) != NULL) ? nRet : EINVAL;
744 ATLASSERT(nRet == 0 || nRet == STRUNCATE);
745 return nRet;
746 #endif
749 inline errno_t strncpyW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc, size_t cchCount)
751 #if _SECURE_ATL
752 return ATL::Checked::wcsncpy_s(lpstrDest, cchDest, lpstrSrc, cchCount);
753 #else
754 errno_t nRet = 0;
755 if(lpstrDest == NULL || cchDest == 0 || lpstrSrc == NULL)
757 nRet = EINVAL;
759 else if(cchCount == _TRUNCATE)
761 cchCount = __min(cchDest - 1, size_t(lstrlenW(lpstrSrc)));
762 nRet = STRUNCATE;
764 else if(cchDest <= cchCount)
766 lpstrDest[0] = 0;
767 nRet = EINVAL;
769 if(nRet == 0 || nRet == STRUNCATE)
770 nRet = (lstrcpynW(lpstrDest, lpstrSrc, (int)cchCount + 1) != NULL) ? nRet : EINVAL;
771 ATLASSERT(nRet == 0 || nRet == STRUNCATE);
772 return nRet;
773 #endif
776 inline errno_t strncpy_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc, size_t cchCount)
778 #ifdef _UNICODE
779 return strncpyW_x(lpstrDest, cchDest, lpstrSrc, cchCount);
780 #else
781 return strncpyA_x(lpstrDest, cchDest, lpstrSrc, cchCount);
782 #endif
785 inline void strcatA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc)
787 #if _SECURE_ATL
788 ATL::Checked::strcat_s(lpstrDest, cchDest, lpstrSrc);
789 #else
790 if(cchDest > (size_t)lstrlenA(lpstrSrc))
791 ATLVERIFY(lstrcatA(lpstrDest, lpstrSrc) != NULL);
792 else
793 ATLASSERT(FALSE);
794 #endif
797 inline void strcatW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc)
799 #if _SECURE_ATL
800 ATL::Checked::wcscat_s(lpstrDest, cchDest, lpstrSrc);
801 #else
802 if(cchDest > (size_t)lstrlenW(lpstrSrc))
803 ATLVERIFY(lstrcatW(lpstrDest, lpstrSrc) != NULL);
804 else
805 ATLASSERT(FALSE);
806 #endif
809 inline void strcat_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc)
811 #ifdef _UNICODE
812 strcatW_x(lpstrDest, cchDest, lpstrSrc);
813 #else
814 strcatA_x(lpstrDest, cchDest, lpstrSrc);
815 #endif
818 inline void memcpy_x(void* pDest, size_t cbDest, const void* pSrc, size_t cbSrc)
820 #if _SECURE_ATL
821 ATL::Checked::memcpy_s(pDest, cbDest, pSrc, cbSrc);
822 #else
823 if(cbDest >= cbSrc)
824 memcpy(pDest, pSrc, cbSrc);
825 else
826 ATLASSERT(FALSE);
827 #endif
830 inline void memmove_x(void* pDest, size_t cbDest, const void* pSrc, size_t cbSrc)
832 #if _SECURE_ATL
833 ATL::Checked::memmove_s(pDest, cbDest, pSrc, cbSrc);
834 #else
835 if(cbDest >= cbSrc)
836 memmove(pDest, pSrc, cbSrc);
837 else
838 ATLASSERT(FALSE);
839 #endif
842 inline int vsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, va_list args)
844 #if _SECURE_ATL && !defined(_ATL_MIN_CRT) && !defined(_WIN32_WCE)
845 return _vstprintf_s(lpstrBuff, cchBuff, lpstrFormat, args);
846 #else
847 cchBuff; // Avoid unused argument warning
848 return _vstprintf(lpstrBuff, lpstrFormat, args);
849 #endif
852 inline int wvsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, va_list args)
854 #if _SECURE_ATL && !defined(_ATL_MIN_CRT) && !defined(_WIN32_WCE)
855 return _vstprintf_s(lpstrBuff, cchBuff, lpstrFormat, args);
856 #else
857 cchBuff; // Avoid unused argument warning
858 return ::wvsprintf(lpstrBuff, lpstrFormat, args);
859 #endif
862 inline int sprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, ...)
864 va_list args;
865 va_start(args, lpstrFormat);
866 int nRes = vsprintf_x(lpstrBuff, cchBuff, lpstrFormat, args);
867 va_end(args);
868 return nRes;
871 inline int wsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, ...)
873 va_list args;
874 va_start(args, lpstrFormat);
875 int nRes = wvsprintf_x(lpstrBuff, cchBuff, lpstrFormat, args);
876 va_end(args);
877 return nRes;
879 }; // namespace SecureHelper
882 ///////////////////////////////////////////////////////////////////////////////
883 // CMessageFilter - Interface for message filter support
885 class CMessageFilter
887 public:
888 virtual BOOL PreTranslateMessage(MSG* pMsg) = 0;
892 ///////////////////////////////////////////////////////////////////////////////
893 // CIdleHandler - Interface for idle processing
895 class CIdleHandler
897 public:
898 virtual BOOL OnIdle() = 0;
901 #ifndef _ATL_NO_OLD_NAMES
902 // for compatilibility with old names only
903 typedef CIdleHandler CUpdateUIObject;
904 #define DoUpdate OnIdle
905 #endif // !_ATL_NO_OLD_NAMES
908 ///////////////////////////////////////////////////////////////////////////////
909 // CMessageLoop - message loop implementation
911 class CMessageLoop
913 public:
914 ATL::CSimpleArray<CMessageFilter*> m_aMsgFilter;
915 ATL::CSimpleArray<CIdleHandler*> m_aIdleHandler;
916 MSG m_msg;
918 // Message filter operations
919 BOOL AddMessageFilter(CMessageFilter* pMessageFilter)
921 return m_aMsgFilter.Add(pMessageFilter);
924 BOOL RemoveMessageFilter(CMessageFilter* pMessageFilter)
926 return m_aMsgFilter.Remove(pMessageFilter);
929 // Idle handler operations
930 BOOL AddIdleHandler(CIdleHandler* pIdleHandler)
932 return m_aIdleHandler.Add(pIdleHandler);
935 BOOL RemoveIdleHandler(CIdleHandler* pIdleHandler)
937 return m_aIdleHandler.Remove(pIdleHandler);
940 #ifndef _ATL_NO_OLD_NAMES
941 // for compatilibility with old names only
942 BOOL AddUpdateUI(CIdleHandler* pIdleHandler)
944 ATLTRACE2(atlTraceUI, 0, _T("CUpdateUIObject and AddUpdateUI are deprecated. Please change your code to use CIdleHandler and OnIdle\n"));
945 return AddIdleHandler(pIdleHandler);
948 BOOL RemoveUpdateUI(CIdleHandler* pIdleHandler)
950 ATLTRACE2(atlTraceUI, 0, _T("CUpdateUIObject and RemoveUpdateUI are deprecated. Please change your code to use CIdleHandler and OnIdle\n"));
951 return RemoveIdleHandler(pIdleHandler);
953 #endif // !_ATL_NO_OLD_NAMES
955 // message loop
956 int Run()
958 BOOL bDoIdle = TRUE;
959 int nIdleCount = 0;
960 BOOL bRet;
962 for(;;)
964 while(bDoIdle && !::PeekMessage(&m_msg, NULL, 0, 0, PM_NOREMOVE))
966 if(!OnIdle(nIdleCount++))
967 bDoIdle = FALSE;
970 bRet = ::GetMessage(&m_msg, NULL, 0, 0);
972 if(bRet == -1)
974 ATLTRACE2(atlTraceUI, 0, _T("::GetMessage returned -1 (error)\n"));
975 continue; // error, don't process
977 else if(!bRet)
979 ATLTRACE2(atlTraceUI, 0, _T("CMessageLoop::Run - exiting\n"));
980 break; // WM_QUIT, exit message loop
983 if(!PreTranslateMessage(&m_msg))
985 ::TranslateMessage(&m_msg);
986 ::DispatchMessage(&m_msg);
989 if(IsIdleMessage(&m_msg))
991 bDoIdle = TRUE;
992 nIdleCount = 0;
996 return (int)m_msg.wParam;
999 static BOOL IsIdleMessage(MSG* pMsg)
1001 // These messages should NOT cause idle processing
1002 switch(pMsg->message)
1004 case WM_MOUSEMOVE:
1005 #ifndef _WIN32_WCE
1006 case WM_NCMOUSEMOVE:
1007 #endif // !_WIN32_WCE
1008 case WM_PAINT:
1009 case 0x0118: // WM_SYSTIMER (caret blink)
1010 return FALSE;
1013 return TRUE;
1016 // Overrideables
1017 // Override to change message filtering
1018 virtual BOOL PreTranslateMessage(MSG* pMsg)
1020 // loop backwards
1021 for(int i = m_aMsgFilter.GetSize() - 1; i >= 0; i--)
1023 CMessageFilter* pMessageFilter = m_aMsgFilter[i];
1024 if(pMessageFilter != NULL && pMessageFilter->PreTranslateMessage(pMsg))
1025 return TRUE;
1027 return FALSE; // not translated
1030 // override to change idle processing
1031 virtual BOOL OnIdle(int /*nIdleCount*/)
1033 for(int i = 0; i < m_aIdleHandler.GetSize(); i++)
1035 CIdleHandler* pIdleHandler = m_aIdleHandler[i];
1036 if(pIdleHandler != NULL)
1037 pIdleHandler->OnIdle();
1039 return FALSE; // don't continue
1044 ///////////////////////////////////////////////////////////////////////////////
1045 // CStaticDataInitCriticalSectionLock and CWindowCreateCriticalSectionLock
1046 // internal classes to manage critical sections for both ATL3 and ATL7
1048 class CStaticDataInitCriticalSectionLock
1050 public:
1051 #if (_ATL_VER >= 0x0700)
1052 ATL::CComCritSecLock<ATL::CComCriticalSection> m_cslock;
1054 CStaticDataInitCriticalSectionLock() : m_cslock(ATL::_pAtlModule->m_csStaticDataInitAndTypeInfo, false)
1056 #endif // (_ATL_VER >= 0x0700)
1058 HRESULT Lock()
1060 #if (_ATL_VER >= 0x0700)
1061 return m_cslock.Lock();
1062 #else // !(_ATL_VER >= 0x0700)
1063 ::EnterCriticalSection(&ATL::_pModule->m_csStaticDataInit);
1064 return S_OK;
1065 #endif // !(_ATL_VER >= 0x0700)
1068 void Unlock()
1070 #if (_ATL_VER >= 0x0700)
1071 m_cslock.Unlock();
1072 #else // !(_ATL_VER >= 0x0700)
1073 ::LeaveCriticalSection(&ATL::_pModule->m_csStaticDataInit);
1074 #endif // !(_ATL_VER >= 0x0700)
1079 class CWindowCreateCriticalSectionLock
1081 public:
1082 #if (_ATL_VER >= 0x0700)
1083 ATL::CComCritSecLock<ATL::CComCriticalSection> m_cslock;
1085 CWindowCreateCriticalSectionLock() : m_cslock(ATL::_AtlWinModule.m_csWindowCreate, false)
1087 #endif // (_ATL_VER >= 0x0700)
1089 HRESULT Lock()
1091 #if (_ATL_VER >= 0x0700)
1092 return m_cslock.Lock();
1093 #else // !(_ATL_VER >= 0x0700)
1094 ::EnterCriticalSection(&ATL::_pModule->m_csWindowCreate);
1095 return S_OK;
1096 #endif // !(_ATL_VER >= 0x0700)
1099 void Unlock()
1101 #if (_ATL_VER >= 0x0700)
1102 m_cslock.Unlock();
1103 #else // !(_ATL_VER >= 0x0700)
1104 ::LeaveCriticalSection(&ATL::_pModule->m_csWindowCreate);
1105 #endif // !(_ATL_VER >= 0x0700)
1110 ///////////////////////////////////////////////////////////////////////////////
1111 // CTempBuffer - helper class for stack allocations for ATL3
1113 #ifndef _WTL_STACK_ALLOC_THRESHOLD
1114 #define _WTL_STACK_ALLOC_THRESHOLD 512
1115 #endif
1117 #if (_ATL_VER >= 0x0700)
1119 using ATL::CTempBuffer;
1121 #else // !(_ATL_VER >= 0x0700)
1123 #ifndef SIZE_MAX
1124 #ifdef _WIN64
1125 #define SIZE_MAX _UI64_MAX
1126 #else
1127 #define SIZE_MAX UINT_MAX
1128 #endif
1129 #endif
1131 #pragma warning(disable: 4284) // warning for operator ->
1133 template<typename T, int t_nFixedBytes = 128>
1134 class CTempBuffer
1136 public:
1137 CTempBuffer() : m_p(NULL)
1141 CTempBuffer(size_t nElements) : m_p(NULL)
1143 Allocate(nElements);
1146 ~CTempBuffer()
1148 if(m_p != reinterpret_cast<T*>(m_abFixedBuffer))
1149 free(m_p);
1152 operator T*() const
1154 return m_p;
1157 T* operator ->() const
1159 ATLASSERT(m_p != NULL);
1160 return m_p;
1163 T* Allocate(size_t nElements)
1165 ATLASSERT(nElements <= (SIZE_MAX / sizeof(T)));
1166 return AllocateBytes(nElements * sizeof(T));
1169 T* AllocateBytes(size_t nBytes)
1171 ATLASSERT(m_p == NULL);
1172 if(nBytes > t_nFixedBytes)
1173 m_p = static_cast<T*>(malloc(nBytes));
1174 else
1175 m_p = reinterpret_cast<T*>(m_abFixedBuffer);
1177 return m_p;
1180 private:
1181 T* m_p;
1182 BYTE m_abFixedBuffer[t_nFixedBytes];
1185 #pragma warning(default: 4284)
1187 #endif // !(_ATL_VER >= 0x0700)
1190 ///////////////////////////////////////////////////////////////////////////////
1191 // CAppModule - module class for an application
1193 class CAppModule : public ATL::CComModule
1195 public:
1196 DWORD m_dwMainThreadID;
1197 ATL::CSimpleMap<DWORD, CMessageLoop*>* m_pMsgLoopMap;
1198 ATL::CSimpleArray<HWND>* m_pSettingChangeNotify;
1200 // Overrides of CComModule::Init and Term
1201 HRESULT Init(ATL::_ATL_OBJMAP_ENTRY* pObjMap, HINSTANCE hInstance, const GUID* pLibID = NULL)
1203 HRESULT hRet = CComModule::Init(pObjMap, hInstance, pLibID);
1204 if(FAILED(hRet))
1205 return hRet;
1207 m_dwMainThreadID = ::GetCurrentThreadId();
1208 typedef ATL::CSimpleMap<DWORD, CMessageLoop*> _mapClass;
1209 m_pMsgLoopMap = NULL;
1210 ATLTRY(m_pMsgLoopMap = new _mapClass);
1211 if(m_pMsgLoopMap == NULL)
1212 return E_OUTOFMEMORY;
1213 m_pSettingChangeNotify = NULL;
1215 return hRet;
1218 void Term()
1220 TermSettingChangeNotify();
1221 delete m_pMsgLoopMap;
1222 CComModule::Term();
1225 // Message loop map methods
1226 BOOL AddMessageLoop(CMessageLoop* pMsgLoop)
1228 CStaticDataInitCriticalSectionLock lock;
1229 if(FAILED(lock.Lock()))
1231 ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::AddMessageLoop.\n"));
1232 ATLASSERT(FALSE);
1233 return FALSE;
1236 ATLASSERT(pMsgLoop != NULL);
1237 ATLASSERT(m_pMsgLoopMap->Lookup(::GetCurrentThreadId()) == NULL); // not in map yet
1239 BOOL bRet = m_pMsgLoopMap->Add(::GetCurrentThreadId(), pMsgLoop);
1241 lock.Unlock();
1243 return bRet;
1246 BOOL RemoveMessageLoop()
1248 CStaticDataInitCriticalSectionLock lock;
1249 if(FAILED(lock.Lock()))
1251 ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::RemoveMessageLoop.\n"));
1252 ATLASSERT(FALSE);
1253 return FALSE;
1256 BOOL bRet = m_pMsgLoopMap->Remove(::GetCurrentThreadId());
1258 lock.Unlock();
1260 return bRet;
1263 CMessageLoop* GetMessageLoop(DWORD dwThreadID = ::GetCurrentThreadId()) const
1265 CStaticDataInitCriticalSectionLock lock;
1266 if(FAILED(lock.Lock()))
1268 ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::GetMessageLoop.\n"));
1269 ATLASSERT(FALSE);
1270 return NULL;
1273 CMessageLoop* pLoop = m_pMsgLoopMap->Lookup(dwThreadID);
1275 lock.Unlock();
1277 return pLoop;
1280 // Setting change notify methods
1281 // Note: Call this from the main thread for MSDI apps
1282 BOOL InitSettingChangeNotify(DLGPROC pfnDlgProc = _SettingChangeDlgProc)
1284 CStaticDataInitCriticalSectionLock lock;
1285 if(FAILED(lock.Lock()))
1287 ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::InitSettingChangeNotify.\n"));
1288 ATLASSERT(FALSE);
1289 return FALSE;
1292 if(m_pSettingChangeNotify == NULL)
1294 typedef ATL::CSimpleArray<HWND> _notifyClass;
1295 ATLTRY(m_pSettingChangeNotify = new _notifyClass);
1296 ATLASSERT(m_pSettingChangeNotify != NULL);
1299 BOOL bRet = (m_pSettingChangeNotify != NULL);
1300 if(bRet && m_pSettingChangeNotify->GetSize() == 0)
1302 // init everything
1303 _ATL_EMPTY_DLGTEMPLATE templ;
1304 HWND hNtfWnd = ::CreateDialogIndirect(GetModuleInstance(), &templ, NULL, pfnDlgProc);
1305 ATLASSERT(::IsWindow(hNtfWnd));
1306 if(::IsWindow(hNtfWnd))
1308 // need conditional code because types don't match in winuser.h
1309 #ifdef _WIN64
1310 ::SetWindowLongPtr(hNtfWnd, GWLP_USERDATA, (LONG_PTR)this);
1311 #else
1312 ::SetWindowLongPtr(hNtfWnd, GWLP_USERDATA, PtrToLong(this));
1313 #endif
1314 bRet = m_pSettingChangeNotify->Add(hNtfWnd);
1316 else
1318 bRet = FALSE;
1322 lock.Unlock();
1324 return bRet;
1327 void TermSettingChangeNotify()
1329 CStaticDataInitCriticalSectionLock lock;
1330 if(FAILED(lock.Lock()))
1332 ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::TermSettingChangeNotify.\n"));
1333 ATLASSERT(FALSE);
1334 return;
1337 if(m_pSettingChangeNotify != NULL && m_pSettingChangeNotify->GetSize() > 0)
1338 ::DestroyWindow((*m_pSettingChangeNotify)[0]);
1339 delete m_pSettingChangeNotify;
1340 m_pSettingChangeNotify = NULL;
1342 lock.Unlock();
1345 BOOL AddSettingChangeNotify(HWND hWnd)
1347 CStaticDataInitCriticalSectionLock lock;
1348 if(FAILED(lock.Lock()))
1350 ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::AddSettingChangeNotify.\n"));
1351 ATLASSERT(FALSE);
1352 return FALSE;
1355 ATLASSERT(::IsWindow(hWnd));
1356 BOOL bRet = FALSE;
1357 if(InitSettingChangeNotify() != FALSE)
1358 bRet = m_pSettingChangeNotify->Add(hWnd);
1360 lock.Unlock();
1362 return bRet;
1365 BOOL RemoveSettingChangeNotify(HWND hWnd)
1367 CStaticDataInitCriticalSectionLock lock;
1368 if(FAILED(lock.Lock()))
1370 ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::RemoveSettingChangeNotify.\n"));
1371 ATLASSERT(FALSE);
1372 return FALSE;
1375 BOOL bRet = FALSE;
1376 if(m_pSettingChangeNotify != NULL)
1377 bRet = m_pSettingChangeNotify->Remove(hWnd);
1379 lock.Unlock();
1381 return bRet;
1384 // Implementation - setting change notify dialog template and dialog procedure
1385 struct _ATL_EMPTY_DLGTEMPLATE : DLGTEMPLATE
1387 _ATL_EMPTY_DLGTEMPLATE()
1389 memset(this, 0, sizeof(_ATL_EMPTY_DLGTEMPLATE));
1390 style = WS_POPUP;
1392 WORD wMenu, wClass, wTitle;
1395 #ifdef _WIN64
1396 static INT_PTR CALLBACK _SettingChangeDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1397 #else
1398 static BOOL CALLBACK _SettingChangeDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1399 #endif
1401 if(uMsg == WM_SETTINGCHANGE)
1403 // need conditional code because types don't match in winuser.h
1404 #ifdef _WIN64
1405 CAppModule* pModule = (CAppModule*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
1406 #else
1407 CAppModule* pModule = (CAppModule*)LongToPtr(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
1408 #endif
1409 ATLASSERT(pModule != NULL);
1410 ATLASSERT(pModule->m_pSettingChangeNotify != NULL);
1411 const UINT uTimeout = 1500; // ms
1412 for(int i = 1; i < pModule->m_pSettingChangeNotify->GetSize(); i++)
1414 #if !defined(_WIN32_WCE)
1415 ::SendMessageTimeout((*pModule->m_pSettingChangeNotify)[i], uMsg, wParam, lParam, SMTO_ABORTIFHUNG, uTimeout, NULL);
1416 #elif(_WIN32_WCE >= 400) // CE specific
1417 ::SendMessageTimeout((*pModule->m_pSettingChangeNotify)[i], uMsg, wParam, lParam, SMTO_NORMAL, uTimeout, NULL);
1418 #else // _WIN32_WCE < 400 specific
1419 uTimeout;
1420 ::SendMessage((*pModule->m_pSettingChangeNotify)[i], uMsg, wParam, lParam);
1421 #endif
1423 return TRUE;
1425 return FALSE;
1430 ///////////////////////////////////////////////////////////////////////////////
1431 // CServerAppModule - module class for a COM server application
1433 class CServerAppModule : public CAppModule
1435 public:
1436 HANDLE m_hEventShutdown;
1437 bool m_bActivity;
1438 DWORD m_dwTimeOut;
1439 DWORD m_dwPause;
1441 // Override of CAppModule::Init
1442 HRESULT Init(ATL::_ATL_OBJMAP_ENTRY* pObjMap, HINSTANCE hInstance, const GUID* pLibID = NULL)
1444 m_dwTimeOut = 5000;
1445 m_dwPause = 1000;
1446 return CAppModule::Init(pObjMap, hInstance, pLibID);
1449 void Term()
1451 if(m_hEventShutdown != NULL && ::CloseHandle(m_hEventShutdown))
1452 m_hEventShutdown = NULL;
1453 CAppModule::Term();
1456 // COM Server methods
1457 LONG Unlock()
1459 LONG lRet = CComModule::Unlock();
1460 if(lRet == 0)
1462 m_bActivity = true;
1463 ::SetEvent(m_hEventShutdown); // tell monitor that we transitioned to zero
1465 return lRet;
1468 void MonitorShutdown()
1470 for(;;)
1472 ::WaitForSingleObject(m_hEventShutdown, INFINITE);
1473 DWORD dwWait = 0;
1476 m_bActivity = false;
1477 dwWait = ::WaitForSingleObject(m_hEventShutdown, m_dwTimeOut);
1479 while(dwWait == WAIT_OBJECT_0);
1480 // timed out
1481 if(!m_bActivity && m_nLockCnt == 0) // if no activity let's really bail
1483 #if ((_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM)) && defined(_ATL_FREE_THREADED) && !defined(_WIN32_WCE)
1484 ::CoSuspendClassObjects();
1485 if(!m_bActivity && m_nLockCnt == 0)
1486 #endif
1487 break;
1490 // This handle should be valid now. If it isn't,
1491 // check if _Module.Term was called first (it shouldn't)
1492 if(::CloseHandle(m_hEventShutdown))
1493 m_hEventShutdown = NULL;
1494 ::PostThreadMessage(m_dwMainThreadID, WM_QUIT, 0, 0);
1497 bool StartMonitor()
1499 m_hEventShutdown = ::CreateEvent(NULL, false, false, NULL);
1500 if(m_hEventShutdown == NULL)
1501 return false;
1502 DWORD dwThreadID = 0;
1503 #if !defined(_ATL_MIN_CRT) && defined(_MT) && !defined(_WIN32_WCE)
1504 HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, (UINT (WINAPI*)(void*))MonitorProc, this, 0, (UINT*)&dwThreadID);
1505 #else
1506 HANDLE hThread = ::CreateThread(NULL, 0, MonitorProc, this, 0, &dwThreadID);
1507 #endif
1508 bool bRet = (hThread != NULL);
1509 if(bRet)
1510 ::CloseHandle(hThread);
1511 return bRet;
1514 static DWORD WINAPI MonitorProc(void* pv)
1516 CServerAppModule* p = (CServerAppModule*)pv;
1517 p->MonitorShutdown();
1518 return 0;
1521 #if (_ATL_VER < 0x0700)
1522 // search for an occurence of string p2 in string p1
1523 static LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
1525 while(p1 != NULL && *p1 != NULL)
1527 LPCTSTR p = p2;
1528 while(p != NULL && *p != NULL)
1530 if(*p1 == *p)
1531 return ::CharNext(p1);
1532 p = ::CharNext(p);
1534 p1 = ::CharNext(p1);
1536 return NULL;
1538 #endif // (_ATL_VER < 0x0700)
1542 ///////////////////////////////////////////////////////////////////////////////
1543 // CString forward reference (enables CString use in atluser.h and atlgdi.h)
1545 #if defined(_WTL_FORWARD_DECLARE_CSTRING) && !defined(_WTL_USE_CSTRING)
1546 #define _WTL_USE_CSTRING
1547 #endif // defined(_WTL_FORWARD_DECLARE_CSTRING) && !defined(_WTL_USE_CSTRING)
1549 #ifdef _WTL_USE_CSTRING
1550 class CString; // forward declaration (include atlmisc.h for the whole class)
1551 #endif // _WTL_USE_CSTRING
1553 // CString namespace
1554 #ifndef _CSTRING_NS
1555 #ifdef __ATLSTR_H__
1556 #define _CSTRING_NS ATL
1557 #else
1558 #define _CSTRING_NS WTL
1559 #endif
1560 #endif // _CSTRING_NS
1562 // Type classes namespace
1563 #ifndef _WTYPES_NS
1564 #ifdef __ATLTYPES_H__
1565 #define _WTYPES_NS
1566 #else
1567 #define _WTYPES_NS WTL
1568 #endif
1569 #endif // _WTYPES_NS
1571 }; // namespace WTL
1574 ///////////////////////////////////////////////////////////////////////////////
1575 // General DLL version helpers
1576 // (ATL3: excluded from atlbase.h if _ATL_DLL is defined; ATL11: removed)
1578 #if (((_ATL_VER < 0x0700) && defined(_ATL_DLL)) || (_ATL_VER >= 0x0B00)) && !defined(_WIN32_WCE)
1580 namespace ATL
1583 inline HRESULT AtlGetDllVersion(HINSTANCE hInstDLL, DLLVERSIONINFO* pDllVersionInfo)
1585 ATLASSERT(pDllVersionInfo != NULL);
1586 if(pDllVersionInfo == NULL)
1587 return E_INVALIDARG;
1589 // We must get this function explicitly because some DLLs don't implement it.
1590 DLLGETVERSIONPROC pfnDllGetVersion = (DLLGETVERSIONPROC)::GetProcAddress(hInstDLL, "DllGetVersion");
1591 if(pfnDllGetVersion == NULL)
1592 return E_NOTIMPL;
1594 return (*pfnDllGetVersion)(pDllVersionInfo);
1597 inline HRESULT AtlGetDllVersion(LPCTSTR lpstrDllName, DLLVERSIONINFO* pDllVersionInfo)
1599 HINSTANCE hInstDLL = ::LoadLibrary(lpstrDllName);
1600 if(hInstDLL == NULL)
1601 return E_FAIL;
1602 HRESULT hRet = AtlGetDllVersion(hInstDLL, pDllVersionInfo);
1603 ::FreeLibrary(hInstDLL);
1604 return hRet;
1607 // Common Control Versions:
1608 // Win95/WinNT 4.0 maj=4 min=00
1609 // IE 3.x maj=4 min=70
1610 // IE 4.0 maj=4 min=71
1611 inline HRESULT AtlGetCommCtrlVersion(LPDWORD pdwMajor, LPDWORD pdwMinor)
1613 ATLASSERT(pdwMajor != NULL && pdwMinor != NULL);
1614 if(pdwMajor == NULL || pdwMinor == NULL)
1615 return E_INVALIDARG;
1617 DLLVERSIONINFO dvi;
1618 ::ZeroMemory(&dvi, sizeof(dvi));
1619 dvi.cbSize = sizeof(dvi);
1620 HRESULT hRet = AtlGetDllVersion(_T("comctl32.dll"), &dvi);
1622 if(SUCCEEDED(hRet))
1624 *pdwMajor = dvi.dwMajorVersion;
1625 *pdwMinor = dvi.dwMinorVersion;
1627 else if(hRet == E_NOTIMPL)
1629 // If DllGetVersion is not there, then the DLL is a version
1630 // previous to the one shipped with IE 3.x
1631 *pdwMajor = 4;
1632 *pdwMinor = 0;
1633 hRet = S_OK;
1636 return hRet;
1639 // Shell Versions:
1640 // Win95/WinNT 4.0 maj=4 min=00
1641 // IE 3.x, IE 4.0 without Web Integrated Desktop maj=4 min=00
1642 // IE 4.0 with Web Integrated Desktop maj=4 min=71
1643 // IE 4.01 with Web Integrated Desktop maj=4 min=72
1644 inline HRESULT AtlGetShellVersion(LPDWORD pdwMajor, LPDWORD pdwMinor)
1646 ATLASSERT(pdwMajor != NULL && pdwMinor != NULL);
1647 if(pdwMajor == NULL || pdwMinor == NULL)
1648 return E_INVALIDARG;
1650 DLLVERSIONINFO dvi;
1651 ::ZeroMemory(&dvi, sizeof(dvi));
1652 dvi.cbSize = sizeof(dvi);
1653 HRESULT hRet = AtlGetDllVersion(_T("shell32.dll"), &dvi);
1655 if(SUCCEEDED(hRet))
1657 *pdwMajor = dvi.dwMajorVersion;
1658 *pdwMinor = dvi.dwMinorVersion;
1660 else if(hRet == E_NOTIMPL)
1662 // If DllGetVersion is not there, then the DLL is a version
1663 // previous to the one shipped with IE 4.x
1664 *pdwMajor = 4;
1665 *pdwMinor = 0;
1666 hRet = S_OK;
1669 return hRet;
1672 }; // namespace ATL
1674 #endif // (_ATL_VER < 0x0700) && defined(_ATL_DLL) && !defined(_WIN32_WCE)
1677 // These are always included
1678 #include "atlwinx.h"
1679 #include "atluser.h"
1680 #include "atlgdi.h"
1682 #ifndef _WTL_NO_AUTOMATIC_NAMESPACE
1683 using namespace WTL;
1684 #endif // !_WTL_NO_AUTOMATIC_NAMESPACE
1686 #endif // __ATLAPP_H__