1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225 |
- // Windows Template Library - WTL version 10.0
- // Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
- //
- // This file is a part of the Windows Template Library.
- // The use and distribution terms for this software are covered by the
- // Microsoft Public License (http://opensource.org/licenses/MS-PL)
- // which can be found in the file MS-PL.txt at the root folder.
- #ifndef __ATLAPP_H__
- #define __ATLAPP_H__
- #pragma once
- #ifndef __cplusplus
- #error WTL requires C++ compilation (use a .cpp suffix)
- #endif
- #ifndef __ATLBASE_H__
- #error atlapp.h requires atlbase.h to be included first
- #endif
- #ifdef _WIN32_WCE
- #error WTL10 doesn't support Windows CE
- #endif
- #ifdef _ATL_NO_COMMODULE
- #error WTL doesn't support _ATL_NO_COMMODULE
- #endif
- #ifdef _ATL_NO_WIN_SUPPORT
- #error WTL doesn't support _ATL_NO_WIN_SUPPORT
- #endif
- #if (_MSC_VER < 1400)
- #error WTL10 requires C++ compiler version 14 (Visual C++ 2005) or higher
- #endif
- #if (WINVER < 0x0501)
- #error WTL requires WINVER >= 0x0501
- #endif
- #if (_WIN32_WINNT < 0x0501)
- #error WTL requires _WIN32_WINNT >= 0x0501
- #endif
- #if (_WIN32_IE < 0x0600)
- #error WTL requires _WIN32_IE >= 0x0600
- #endif
- #if (_ATL_VER < 0x0800)
- #error WTL10 requires ATL version 8 or higher
- #endif
- #ifdef _ATL_MIN_CRT
- #error WTL10 doesn't support _ATL_MIN_CRT
- #endif
- #ifdef _ATL_NO_MSIMG
- #error WTL10 doesn't support _ATL_NO_MSIMG
- #endif
- #include <limits.h>
- #ifdef _MT
- #include <process.h> // for _beginthreadex
- #endif
- #include <commctrl.h>
- #pragma comment(lib, "comctl32.lib")
- #include <commdlg.h>
- #include <shellapi.h>
- // Check for VS2005 without newer WinSDK
- #if (_MSC_VER == 1400) && !defined(RB_GETEXTENDEDSTYLE)
- #error WTL10 requires WinSDK 6.0 ot higher
- #endif
- #include <uxtheme.h>
- #pragma comment(lib, "uxtheme.lib")
- #if defined(_SYSINFOAPI_H_) && defined(NOT_BUILD_WINDOWS_DEPRECATE)
- #include <VersionHelpers.h>
- #endif
- #include "atlres.h"
- ///////////////////////////////////////////////////////////////////////////////
- // WTL version number
- #define _WTL_VER 0x1000 // version 10.0
- ///////////////////////////////////////////////////////////////////////////////
- // Classes in this file:
- //
- // CMessageFilter
- // CIdleHandler
- // CMessageLoop
- //
- // CAppModule
- // CServerAppModule
- //
- // Global functions:
- // AtlInitCommonControls()
- // AtlGetDefaultGuiFont()
- // AtlCreateControlFont()
- // AtlCreateBoldFont()
- // AtlGetStringPtr()
- ///////////////////////////////////////////////////////////////////////////////
- // Miscellaneous global support
- // define useful macros from winuser.h
- #ifndef IS_INTRESOURCE
- #define IS_INTRESOURCE(_r) (((ULONG_PTR)(_r) >> 16) == 0)
- #endif // IS_INTRESOURCE
- // protect template members from windowsx.h macros
- #ifdef _INC_WINDOWSX
- #undef SubclassWindow
- #endif // _INC_WINDOWSX
- // define useful macros from windowsx.h
- #ifndef GET_X_LPARAM
- #define GET_X_LPARAM(lParam) ((int)(short)LOWORD(lParam))
- #endif
- #ifndef GET_Y_LPARAM
- #define GET_Y_LPARAM(lParam) ((int)(short)HIWORD(lParam))
- #endif
- // Dummy structs for compiling with /CLR
- #ifdef _MANAGED
- __if_not_exists(_IMAGELIST::_IMAGELIST) { struct _IMAGELIST { }; }
- __if_not_exists(_TREEITEM::_TREEITEM) { struct _TREEITEM { }; }
- __if_not_exists(_PSP::_PSP) { struct _PSP { }; }
- #endif
- // Forward declaration for ATL11 fix
- #if (_ATL_VER >= 0x0B00)
- namespace ATL { HRESULT AtlGetCommCtrlVersion(LPDWORD pdwMajor, LPDWORD pdwMinor); }
- #endif
- #ifndef WM_MOUSEHWHEEL
- #define WM_MOUSEHWHEEL 0x020E
- #endif
- // Used for stack allocations with ATL::CTempBuffer
- #ifndef _WTL_STACK_ALLOC_THRESHOLD
- #define _WTL_STACK_ALLOC_THRESHOLD 512
- #endif
- namespace WTL
- {
- DECLARE_TRACE_CATEGORY(atlTraceUI)
- #ifdef _DEBUG
- __declspec(selectany) ATL::CTraceCategory atlTraceUI(_T("atlTraceUI"));
- #endif // _DEBUG
- // Common Controls initialization helper
- inline BOOL AtlInitCommonControls(DWORD dwFlags)
- {
- INITCOMMONCONTROLSEX iccx = { sizeof(INITCOMMONCONTROLSEX), dwFlags };
- BOOL bRet = ::InitCommonControlsEx(&iccx);
- ATLASSERT(bRet);
- return bRet;
- }
- // Default GUI font helper - "MS Shell Dlg" stock font
- inline HFONT AtlGetDefaultGuiFont()
- {
- return (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
- }
- // Control font helper - default font for controls not in a dialog
- // (NOTE: Caller owns the font, and should destroy it when it's no longer needed)
- inline HFONT AtlCreateControlFont()
- {
- LOGFONT lf = {};
- ATLVERIFY(::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &lf, 0) != FALSE);
- HFONT hFont = ::CreateFontIndirect(&lf);
- ATLASSERT(hFont != NULL);
- return hFont;
- }
- // Bold font helper
- // (NOTE: Caller owns the font, and should destroy it when it's no longer needed)
- inline HFONT AtlCreateBoldFont(HFONT hFont = NULL)
- {
- LOGFONT lf = {};
- if(hFont == NULL)
- ATLVERIFY(::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &lf, 0) != FALSE);
- else
- ATLVERIFY(::GetObject(hFont, sizeof(LOGFONT), &lf) == sizeof(LOGFONT));
- lf.lfWeight = FW_BOLD;
- HFONT hFontBold = ::CreateFontIndirect(&lf);
- ATLASSERT(hFontBold != NULL);
- return hFontBold;
- }
- // Resource string pointer
- inline LPCWSTR AtlGetStringPtr(UINT uID, int* pch = NULL)
- {
- LPCWSTR lpstr = NULL;
- int nRet = ::LoadStringW(ATL::_AtlBaseModule.GetResourceInstance(), uID, (LPWSTR)&lpstr, 0);
- if(pch != NULL)
- *pch = nRet;
- return lpstr;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // RunTimeHelper - helper functions for Windows version and structure sizes
- #ifndef _WTL_NO_RUNTIME_STRUCT_SIZE
- #ifndef _SIZEOF_STRUCT
- #define _SIZEOF_STRUCT(structname, member) (((int)((LPBYTE)(&((structname*)0)->member) - ((LPBYTE)((structname*)0)))) + sizeof(((structname*)0)->member))
- #endif
- #if (_WIN32_WINNT >= 0x0600) && !defined(REBARBANDINFO_V6_SIZE)
- #define REBARBANDINFO_V6_SIZE _SIZEOF_STRUCT(REBARBANDINFO, cxHeader)
- #endif // (_WIN32_WINNT >= 0x0600) && !defined(REBARBANDINFO_V6_SIZE)
- #if (_WIN32_WINNT >= 0x0600) && !defined(LVGROUP_V5_SIZE)
- #define LVGROUP_V5_SIZE _SIZEOF_STRUCT(LVGROUP, uAlign)
- #endif // (_WIN32_WINNT >= 0x0600) && !defined(LVGROUP_V5_SIZE)
- #if (_WIN32_WINNT >= 0x0600) && !defined(LVTILEINFO_V5_SIZE)
- #define LVTILEINFO_V5_SIZE _SIZEOF_STRUCT(LVTILEINFO, puColumns)
- #endif // (_WIN32_WINNT >= 0x0600) && !defined(LVTILEINFO_V5_SIZE)
- #if defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN) && !defined(MCHITTESTINFO_V1_SIZE)
- #define MCHITTESTINFO_V1_SIZE _SIZEOF_STRUCT(MCHITTESTINFO, st)
- #endif // defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN) && !defined(MCHITTESTINFO_V1_SIZE)
- #if (WINVER >= 0x0600) && !defined(NONCLIENTMETRICS_V1_SIZE)
- #define NONCLIENTMETRICS_V1_SIZE _SIZEOF_STRUCT(NONCLIENTMETRICS, lfMessageFont)
- #endif // (WINVER >= 0x0600) && !defined(NONCLIENTMETRICS_V1_SIZE)
- #ifndef TTTOOLINFO_V2_SIZE
- #define TTTOOLINFO_V2_SIZE _SIZEOF_STRUCT(TTTOOLINFO, lParam)
- #endif
- #endif // !_WTL_NO_RUNTIME_STRUCT_SIZE
- namespace RunTimeHelper
- {
- inline bool IsCommCtrl6()
- {
- DWORD dwMajor = 0, dwMinor = 0;
- HRESULT hRet = ATL::AtlGetCommCtrlVersion(&dwMajor, &dwMinor);
- return (SUCCEEDED(hRet) && (dwMajor >= 6));
- }
- inline bool IsVista()
- {
- #ifdef _versionhelpers_H_INCLUDED_
- return ::IsWindowsVistaOrGreater();
- #else // !_versionhelpers_H_INCLUDED_
- OSVERSIONINFO ovi = { sizeof(OSVERSIONINFO) };
- BOOL bRet = ::GetVersionEx(&ovi);
- return ((bRet != FALSE) && (ovi.dwMajorVersion >= 6));
- #endif // _versionhelpers_H_INCLUDED_
- }
- inline bool IsThemeAvailable()
- {
- return IsCommCtrl6() && (::IsThemeActive() != FALSE) && (::IsAppThemed() != FALSE);
- }
- inline bool IsWin7()
- {
- #ifdef _versionhelpers_H_INCLUDED_
- return ::IsWindows7OrGreater();
- #else // !_versionhelpers_H_INCLUDED_
- OSVERSIONINFO ovi = { sizeof(OSVERSIONINFO) };
- BOOL bRet = ::GetVersionEx(&ovi);
- return ((bRet != FALSE) && ((ovi.dwMajorVersion > 6) || ((ovi.dwMajorVersion == 6) && (ovi.dwMinorVersion >= 1))));
- #endif // _versionhelpers_H_INCLUDED_
- }
- inline bool IsRibbonUIAvailable()
- {
- static INT iRibbonUI = -1;
- #if defined(NTDDI_WIN7) && (NTDDI_VERSION >= NTDDI_WIN7)
- if (iRibbonUI == -1)
- {
- HMODULE hRibbonDLL = ::LoadLibrary(_T("propsys.dll"));
- if (hRibbonDLL != NULL)
- {
- const GUID CLSID_UIRibbonFramework = { 0x926749fa, 0x2615, 0x4987, { 0x88, 0x45, 0xc3, 0x3e, 0x65, 0xf2, 0xb9, 0x57 } };
- // block - create instance
- {
- ATL::CComPtr<IUnknown> pIUIFramework;
- iRibbonUI = SUCCEEDED(pIUIFramework.CoCreateInstance(CLSID_UIRibbonFramework)) ? 1 : 0;
- }
- ::FreeLibrary(hRibbonDLL);
- }
- else
- {
- iRibbonUI = 0;
- }
- }
- #endif // defined(NTDDI_WIN7) && (NTDDI_VERSION >= NTDDI_WIN7)
- return (iRibbonUI == 1);
- }
- inline UINT SizeOf_REBARBANDINFO()
- {
- UINT uSize = sizeof(REBARBANDINFO);
- #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
- if(!(IsVista() && IsCommCtrl6()))
- uSize = REBARBANDINFO_V6_SIZE;
- #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
- return uSize;
- }
- inline UINT SizeOf_LVGROUP()
- {
- UINT uSize = sizeof(LVGROUP);
- #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
- if(!IsVista())
- uSize = LVGROUP_V5_SIZE;
- #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
- return uSize;
- }
- inline UINT SizeOf_LVTILEINFO()
- {
- UINT uSize = sizeof(LVTILEINFO);
- #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
- if(!IsVista())
- uSize = LVTILEINFO_V5_SIZE;
- #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
- return uSize;
- }
- inline UINT SizeOf_MCHITTESTINFO()
- {
- UINT uSize = sizeof(MCHITTESTINFO);
- #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
- if(!(IsVista() && IsCommCtrl6()))
- uSize = MCHITTESTINFO_V1_SIZE;
- #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
- return uSize;
- }
- inline UINT SizeOf_NONCLIENTMETRICS()
- {
- UINT uSize = sizeof(NONCLIENTMETRICS);
- #if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (WINVER >= 0x0600)
- if(!IsVista())
- uSize = NONCLIENTMETRICS_V1_SIZE;
- #endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (WINVER >= 0x0600)
- return uSize;
- }
- inline UINT SizeOf_TOOLINFO()
- {
- UINT uSize = sizeof(TOOLINFO);
- #ifndef _WTL_NO_RUNTIME_STRUCT_SIZE
- if(!IsVista())
- uSize = TTTOOLINFO_V2_SIZE;
- #endif
- return uSize;
- }
- } // namespace RunTimeHelper
- ///////////////////////////////////////////////////////////////////////////////
- // ModuleHelper - helper functions for ATL (deprecated)
- namespace ModuleHelper
- {
- inline HINSTANCE GetModuleInstance()
- {
- return ATL::_AtlBaseModule.GetModuleInstance();
- }
- inline HINSTANCE GetResourceInstance()
- {
- return ATL::_AtlBaseModule.GetResourceInstance();
- }
- inline void AddCreateWndData(ATL::_AtlCreateWndData* pData, void* pObject)
- {
- ATL::_AtlWinModule.AddCreateWndData(pData, pObject);
- }
- inline void* ExtractCreateWndData()
- {
- return ATL::_AtlWinModule.ExtractCreateWndData();
- }
- } // namespace ModuleHelper
- ///////////////////////////////////////////////////////////////////////////////
- // SecureHelper - WTL10 requires use of secure functions
- // these are here only for compatibility with existing projects
- namespace SecureHelper
- {
- inline void strcpyA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc)
- {
- ATL::Checked::strcpy_s(lpstrDest, cchDest, lpstrSrc);
- }
- inline void strcpyW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc)
- {
- ATL::Checked::wcscpy_s(lpstrDest, cchDest, lpstrSrc);
- }
- inline void strcpy_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc)
- {
- #ifdef _UNICODE
- strcpyW_x(lpstrDest, cchDest, lpstrSrc);
- #else
- strcpyA_x(lpstrDest, cchDest, lpstrSrc);
- #endif
- }
- inline errno_t strncpyA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc, size_t cchCount)
- {
- return ATL::Checked::strncpy_s(lpstrDest, cchDest, lpstrSrc, cchCount);
- }
- inline errno_t strncpyW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc, size_t cchCount)
- {
- return ATL::Checked::wcsncpy_s(lpstrDest, cchDest, lpstrSrc, cchCount);
- }
- inline errno_t strncpy_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc, size_t cchCount)
- {
- #ifdef _UNICODE
- return strncpyW_x(lpstrDest, cchDest, lpstrSrc, cchCount);
- #else
- return strncpyA_x(lpstrDest, cchDest, lpstrSrc, cchCount);
- #endif
- }
- inline void strcatA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc)
- {
- ATL::Checked::strcat_s(lpstrDest, cchDest, lpstrSrc);
- }
- inline void strcatW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc)
- {
- ATL::Checked::wcscat_s(lpstrDest, cchDest, lpstrSrc);
- }
- inline void strcat_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc)
- {
- #ifdef _UNICODE
- strcatW_x(lpstrDest, cchDest, lpstrSrc);
- #else
- strcatA_x(lpstrDest, cchDest, lpstrSrc);
- #endif
- }
- inline void memcpy_x(void* pDest, size_t cbDest, const void* pSrc, size_t cbSrc)
- {
- ATL::Checked::memcpy_s(pDest, cbDest, pSrc, cbSrc);
- }
- inline void memmove_x(void* pDest, size_t cbDest, const void* pSrc, size_t cbSrc)
- {
- ATL::Checked::memmove_s(pDest, cbDest, pSrc, cbSrc);
- }
- inline int vsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, va_list args)
- {
- return _vstprintf_s(lpstrBuff, cchBuff, lpstrFormat, args);
- }
- inline int wvsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, va_list args)
- {
- return _vstprintf_s(lpstrBuff, cchBuff, lpstrFormat, args);
- }
- inline int sprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, ...)
- {
- va_list args;
- va_start(args, lpstrFormat);
- int nRes = vsprintf_x(lpstrBuff, cchBuff, lpstrFormat, args);
- va_end(args);
- return nRes;
- }
- inline int wsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, ...)
- {
- va_list args;
- va_start(args, lpstrFormat);
- int nRes = wvsprintf_x(lpstrBuff, cchBuff, lpstrFormat, args);
- va_end(args);
- return nRes;
- }
- } // namespace SecureHelper
- ///////////////////////////////////////////////////////////////////////////////
- // MinCrtHelper - WTL10 doesn't support _ATL_MIN_CRT,
- // these are here only for compatibility with existing projects
- namespace MinCrtHelper
- {
- inline int _isspace(TCHAR ch)
- {
- return _istspace(ch);
- }
- inline int _isdigit(TCHAR ch)
- {
- return _istdigit(ch);
- }
- inline int _atoi(LPCTSTR str)
- {
- return _ttoi(str);
- }
- inline LPCTSTR _strrchr(LPCTSTR str, TCHAR ch)
- {
- return _tcsrchr(str, ch);
- }
- inline LPTSTR _strrchr(LPTSTR str, TCHAR ch)
- {
- return _tcsrchr(str, ch);
- }
- } // namespace MinCrtHelper
- ///////////////////////////////////////////////////////////////////////////////
- // GenericWndClass - generic window class usable for subclassing
- // Use in dialog templates to specify a placeholder to be subclassed
- // Specify as a custom control with class name WTL_GenericWindow
- // Call Rregister() before creating dialog (for example, in WinMain)
- namespace GenericWndClass
- {
- inline LPCTSTR GetName()
- {
- return _T("WTL_GenericWindow");
- }
- inline ATOM Register()
- {
- WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
- wc.lpfnWndProc = ::DefWindowProc;
- wc.hInstance = ModuleHelper::GetModuleInstance();
- wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wc.lpszClassName = GetName();
- ATOM atom = ::RegisterClassEx(&wc);
- ATLASSERT(atom != 0);
- return atom;
- }
- inline BOOL Unregister() // only needed for DLLs or tmp use
- {
- return ::UnregisterClass(GetName(), ModuleHelper::GetModuleInstance());
- }
- } // namespace GenericWndClass
- ///////////////////////////////////////////////////////////////////////////////
- // CMessageFilter - Interface for message filter support
- class ATL_NO_VTABLE CMessageFilter
- {
- public:
- virtual BOOL PreTranslateMessage(MSG* pMsg) = 0;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // CIdleHandler - Interface for idle processing
- class ATL_NO_VTABLE CIdleHandler
- {
- public:
- virtual BOOL OnIdle() = 0;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // CMessageLoop - message loop implementation
- class CMessageLoop
- {
- public:
- ATL::CSimpleArray<CMessageFilter*> m_aMsgFilter;
- ATL::CSimpleArray<CIdleHandler*> m_aIdleHandler;
- MSG m_msg;
- CMessageLoop()
- {
- memset(&m_msg, 0, sizeof(m_msg));
- }
- virtual ~CMessageLoop()
- { }
- // Message filter operations
- BOOL AddMessageFilter(CMessageFilter* pMessageFilter)
- {
- return m_aMsgFilter.Add(pMessageFilter);
- }
- BOOL RemoveMessageFilter(CMessageFilter* pMessageFilter)
- {
- return m_aMsgFilter.Remove(pMessageFilter);
- }
- // Idle handler operations
- BOOL AddIdleHandler(CIdleHandler* pIdleHandler)
- {
- return m_aIdleHandler.Add(pIdleHandler);
- }
- BOOL RemoveIdleHandler(CIdleHandler* pIdleHandler)
- {
- return m_aIdleHandler.Remove(pIdleHandler);
- }
- // message loop
- int Run()
- {
- BOOL bDoIdle = TRUE;
- int nIdleCount = 0;
- BOOL bRet = FALSE;
- for(;;)
- {
- while(bDoIdle && !::PeekMessage(&m_msg, NULL, 0, 0, PM_NOREMOVE))
- {
- if(!OnIdle(nIdleCount++))
- bDoIdle = FALSE;
- }
- bRet = ::GetMessage(&m_msg, NULL, 0, 0);
- if(bRet == -1)
- {
- ATLTRACE2(atlTraceUI, 0, _T("::GetMessage returned -1 (error)\n"));
- continue; // error, don't process
- }
- else if(!bRet)
- {
- ATLTRACE2(atlTraceUI, 0, _T("CMessageLoop::Run - exiting\n"));
- break; // WM_QUIT, exit message loop
- }
- if(!PreTranslateMessage(&m_msg))
- {
- ::TranslateMessage(&m_msg);
- ::DispatchMessage(&m_msg);
- }
- if(IsIdleMessage(&m_msg))
- {
- bDoIdle = TRUE;
- nIdleCount = 0;
- }
- }
- return (int)m_msg.wParam;
- }
- // Overrideables
- // Override to change message filtering
- virtual BOOL PreTranslateMessage(MSG* pMsg)
- {
- // loop backwards
- for(int i = m_aMsgFilter.GetSize() - 1; i >= 0; i--)
- {
- CMessageFilter* pMessageFilter = m_aMsgFilter[i];
- if((pMessageFilter != NULL) && pMessageFilter->PreTranslateMessage(pMsg))
- return TRUE;
- }
- return FALSE; // not translated
- }
- // override to change idle processing
- virtual BOOL OnIdle(int /*nIdleCount*/)
- {
- for(int i = 0; i < m_aIdleHandler.GetSize(); i++)
- {
- CIdleHandler* pIdleHandler = m_aIdleHandler[i];
- if(pIdleHandler != NULL)
- pIdleHandler->OnIdle();
- }
- return FALSE; // don't continue
- }
- // override to change non-idle messages
- virtual BOOL IsIdleMessage(MSG* pMsg) const
- {
- // These messages should NOT cause idle processing
- switch(pMsg->message)
- {
- case WM_MOUSEMOVE:
- case WM_NCMOUSEMOVE:
- case WM_PAINT:
- case 0x0118: // WM_SYSTIMER (caret blink)
- return FALSE;
- }
- return TRUE;
- }
- };
- ///////////////////////////////////////////////////////////////////////////////
- // CStaticDataInitCriticalSectionLock and CWindowCreateCriticalSectionLock
- // internal classes to manage critical sections for ATL (deprecated)
- class CStaticDataInitCriticalSectionLock
- {
- public:
- ATL::CComCritSecLock<ATL::CComCriticalSection> m_cslock;
- CStaticDataInitCriticalSectionLock() : m_cslock(ATL::_pAtlModule->m_csStaticDataInitAndTypeInfo, false)
- { }
- HRESULT Lock()
- {
- return m_cslock.Lock();
- }
- void Unlock()
- {
- m_cslock.Unlock();
- }
- };
- class CWindowCreateCriticalSectionLock
- {
- public:
- ATL::CComCritSecLock<ATL::CComCriticalSection> m_cslock;
- CWindowCreateCriticalSectionLock() : m_cslock(ATL::_AtlWinModule.m_csWindowCreate, false)
- { }
- HRESULT Lock()
- {
- return m_cslock.Lock();
- }
- void Unlock()
- {
- m_cslock.Unlock();
- }
- };
- ///////////////////////////////////////////////////////////////////////////////
- // CAppModule - module class for an application
- #if (_MSC_VER == 1400) // VS2005
- #pragma warning(push)
- #pragma warning(disable : 4244)
- #pragma warning(disable : 4312)
- #endif
- class CAppModule : public ATL::CComModule
- {
- public:
- DWORD m_dwMainThreadID;
- ATL::CSimpleMap<DWORD, CMessageLoop*>* m_pMsgLoopMap;
- ATL::CSimpleArray<HWND>* m_pSettingChangeNotify;
- CAppModule() : m_dwMainThreadID(0), m_pMsgLoopMap(NULL), m_pSettingChangeNotify(NULL)
- { }
- // Overrides of CComModule::Init and Term
- HRESULT Init(ATL::_ATL_OBJMAP_ENTRY* pObjMap, HINSTANCE hInstance, const GUID* pLibID = NULL)
- {
- HRESULT hRet = CComModule::Init(pObjMap, hInstance, pLibID);
- if(FAILED(hRet))
- return hRet;
- m_dwMainThreadID = ::GetCurrentThreadId();
- typedef ATL::CSimpleMap<DWORD, CMessageLoop*> _mapClass;
- m_pMsgLoopMap = NULL;
- ATLTRY(m_pMsgLoopMap = new _mapClass);
- if(m_pMsgLoopMap == NULL)
- return E_OUTOFMEMORY;
- m_pSettingChangeNotify = NULL;
- return hRet;
- }
- void Term()
- {
- TermSettingChangeNotify();
- delete m_pMsgLoopMap;
- CComModule::Term();
- }
- // Message loop map methods
- BOOL AddMessageLoop(CMessageLoop* pMsgLoop)
- {
- CStaticDataInitCriticalSectionLock lock;
- if(FAILED(lock.Lock()))
- {
- ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::AddMessageLoop.\n"));
- ATLASSERT(FALSE);
- return FALSE;
- }
- ATLASSERT(pMsgLoop != NULL);
- ATLASSERT(m_pMsgLoopMap->Lookup(::GetCurrentThreadId()) == NULL); // not in map yet
- BOOL bRet = m_pMsgLoopMap->Add(::GetCurrentThreadId(), pMsgLoop);
- lock.Unlock();
- return bRet;
- }
- BOOL RemoveMessageLoop()
- {
- CStaticDataInitCriticalSectionLock lock;
- if(FAILED(lock.Lock()))
- {
- ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::RemoveMessageLoop.\n"));
- ATLASSERT(FALSE);
- return FALSE;
- }
- BOOL bRet = m_pMsgLoopMap->Remove(::GetCurrentThreadId());
- lock.Unlock();
- return bRet;
- }
- CMessageLoop* GetMessageLoop(DWORD dwThreadID = ::GetCurrentThreadId()) const
- {
- CStaticDataInitCriticalSectionLock lock;
- if(FAILED(lock.Lock()))
- {
- ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::GetMessageLoop.\n"));
- ATLASSERT(FALSE);
- return NULL;
- }
- CMessageLoop* pLoop = m_pMsgLoopMap->Lookup(dwThreadID);
- lock.Unlock();
- return pLoop;
- }
- // Setting change notify methods
- // Note: Call this from the main thread for MSDI apps
- BOOL InitSettingChangeNotify(DLGPROC pfnDlgProc = _SettingChangeDlgProc)
- {
- CStaticDataInitCriticalSectionLock lock;
- if(FAILED(lock.Lock()))
- {
- ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::InitSettingChangeNotify.\n"));
- ATLASSERT(FALSE);
- return FALSE;
- }
- if(m_pSettingChangeNotify == NULL)
- {
- typedef ATL::CSimpleArray<HWND> _notifyClass;
- ATLTRY(m_pSettingChangeNotify = new _notifyClass);
- ATLASSERT(m_pSettingChangeNotify != NULL);
- }
- BOOL bRet = (m_pSettingChangeNotify != NULL);
- if(bRet && (m_pSettingChangeNotify->GetSize() == 0))
- {
- // init everything
- _ATL_EMPTY_DLGTEMPLATE templ;
- HWND hNtfWnd = ::CreateDialogIndirect(GetModuleInstance(), &templ, NULL, pfnDlgProc);
- ATLASSERT(::IsWindow(hNtfWnd));
- if(::IsWindow(hNtfWnd))
- {
- ::SetWindowLongPtr(hNtfWnd, GWLP_USERDATA, (LONG_PTR)this);
- bRet = m_pSettingChangeNotify->Add(hNtfWnd);
- }
- else
- {
- bRet = FALSE;
- }
- }
- lock.Unlock();
- return bRet;
- }
- void TermSettingChangeNotify()
- {
- CStaticDataInitCriticalSectionLock lock;
- if(FAILED(lock.Lock()))
- {
- ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::TermSettingChangeNotify.\n"));
- ATLASSERT(FALSE);
- return;
- }
- if((m_pSettingChangeNotify != NULL) && (m_pSettingChangeNotify->GetSize() > 0))
- ::DestroyWindow((*m_pSettingChangeNotify)[0]);
- delete m_pSettingChangeNotify;
- m_pSettingChangeNotify = NULL;
- lock.Unlock();
- }
- BOOL AddSettingChangeNotify(HWND hWnd)
- {
- CStaticDataInitCriticalSectionLock lock;
- if(FAILED(lock.Lock()))
- {
- ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::AddSettingChangeNotify.\n"));
- ATLASSERT(FALSE);
- return FALSE;
- }
- ATLASSERT(::IsWindow(hWnd));
- BOOL bRet = FALSE;
- if(InitSettingChangeNotify() != FALSE)
- bRet = m_pSettingChangeNotify->Add(hWnd);
- lock.Unlock();
- return bRet;
- }
- BOOL RemoveSettingChangeNotify(HWND hWnd)
- {
- CStaticDataInitCriticalSectionLock lock;
- if(FAILED(lock.Lock()))
- {
- ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::RemoveSettingChangeNotify.\n"));
- ATLASSERT(FALSE);
- return FALSE;
- }
- BOOL bRet = FALSE;
- if(m_pSettingChangeNotify != NULL)
- bRet = m_pSettingChangeNotify->Remove(hWnd);
- lock.Unlock();
- return bRet;
- }
- // Implementation - setting change notify dialog template and dialog procedure
- struct _ATL_EMPTY_DLGTEMPLATE : DLGTEMPLATE
- {
- _ATL_EMPTY_DLGTEMPLATE()
- {
- memset(this, 0, sizeof(_ATL_EMPTY_DLGTEMPLATE));
- style = WS_POPUP;
- }
- WORD wMenu, wClass, wTitle;
- };
- static INT_PTR CALLBACK _SettingChangeDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- if(uMsg == WM_SETTINGCHANGE)
- {
- CAppModule* pModule = (CAppModule*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
- ATLASSERT(pModule != NULL);
- ATLASSERT(pModule->m_pSettingChangeNotify != NULL);
- const UINT uTimeout = 1500; // ms
- for(int i = 1; i < pModule->m_pSettingChangeNotify->GetSize(); i++)
- ::SendMessageTimeout((*pModule->m_pSettingChangeNotify)[i], uMsg, wParam, lParam, SMTO_ABORTIFHUNG, uTimeout, NULL);
- return TRUE;
- }
- return FALSE;
- }
- };
- #if (_MSC_VER == 1400) // VS2005
- #pragma warning(pop)
- #endif
- ///////////////////////////////////////////////////////////////////////////////
- // CServerAppModule - module class for a COM server application
- class CServerAppModule : public CAppModule
- {
- public:
- HANDLE m_hEventShutdown;
- bool m_bActivity;
- DWORD m_dwTimeOut;
- DWORD m_dwPause;
- CServerAppModule() : m_hEventShutdown(NULL), m_bActivity(false), m_dwTimeOut(5000), m_dwPause(1000)
- { }
- // Override of CAppModule::Init
- HRESULT Init(ATL::_ATL_OBJMAP_ENTRY* pObjMap, HINSTANCE hInstance, const GUID* pLibID = NULL)
- {
- m_dwTimeOut = 5000;
- m_dwPause = 1000;
- return CAppModule::Init(pObjMap, hInstance, pLibID);
- }
- void Term()
- {
- if((m_hEventShutdown != NULL) && ::CloseHandle(m_hEventShutdown))
- m_hEventShutdown = NULL;
- CAppModule::Term();
- }
- // COM Server methods
- LONG Unlock() throw()
- {
- LONG lRet = CComModule::Unlock();
- if(lRet == 0)
- {
- m_bActivity = true;
- ::SetEvent(m_hEventShutdown); // tell monitor that we transitioned to zero
- }
- return lRet;
- }
- void MonitorShutdown()
- {
- for(;;)
- {
- ::WaitForSingleObject(m_hEventShutdown, INFINITE);
- DWORD dwWait = 0;
- do
- {
- m_bActivity = false;
- dwWait = ::WaitForSingleObject(m_hEventShutdown, m_dwTimeOut);
- }
- while(dwWait == WAIT_OBJECT_0);
- // timed out
- if(!m_bActivity && (m_nLockCnt == 0)) // if no activity let's really bail
- {
- #if defined(_WIN32_DCOM) && defined(_ATL_FREE_THREADED)
- ::CoSuspendClassObjects();
- if(!m_bActivity && (m_nLockCnt == 0))
- #endif
- break;
- }
- }
- // This handle should be valid now. If it isn't,
- // check if _Module.Term was called first (it shouldn't)
- if(::CloseHandle(m_hEventShutdown))
- m_hEventShutdown = NULL;
- ::PostThreadMessage(m_dwMainThreadID, WM_QUIT, 0, 0);
- }
- bool StartMonitor()
- {
- m_hEventShutdown = ::CreateEvent(NULL, false, false, NULL);
- if(m_hEventShutdown == NULL)
- return false;
- DWORD dwThreadID = 0;
- #ifdef _MT
- HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, (UINT (WINAPI*)(void*))MonitorProc, this, 0, (UINT*)&dwThreadID);
- #else
- HANDLE hThread = ::CreateThread(NULL, 0, MonitorProc, this, 0, &dwThreadID);
- #endif
- bool bRet = (hThread != NULL);
- if(bRet)
- ::CloseHandle(hThread);
- return bRet;
- }
- static DWORD WINAPI MonitorProc(void* pv)
- {
- CServerAppModule* p = (CServerAppModule*)pv;
- p->MonitorShutdown();
- return 0;
- }
- };
- ///////////////////////////////////////////////////////////////////////////////
- // CRegKeyEx - not used any more, here only for compatibility with old projects
- typedef ATL::CRegKey CRegKeyEx;
- } // namespace WTL
- ///////////////////////////////////////////////////////////////////////////////
- // CString forward reference (enables CString use in atluser.h and atlgdi.h)
- #if (defined(_WTL_USE_CSTRING) || defined(_WTL_FORWARD_DECLARE_CSTRING)) && !defined(__ATLSTR_H__)
- #include <atlstr.h>
- #endif
- // CString namespace
- #define _CSTRING_NS ATL
- // Type classes namespace
- #define _WTYPES_NS
- ///////////////////////////////////////////////////////////////////////////////
- // General DLL version helpers (removed in ATL11)
- #if (_ATL_VER >= 0x0B00)
- namespace ATL
- {
- inline HRESULT AtlGetDllVersion(HINSTANCE hInstDLL, DLLVERSIONINFO* pDllVersionInfo)
- {
- ATLASSERT(pDllVersionInfo != NULL);
- if(pDllVersionInfo == NULL)
- return E_INVALIDARG;
- // We must get this function explicitly because some DLLs don't implement it.
- DLLGETVERSIONPROC pfnDllGetVersion = (DLLGETVERSIONPROC)::GetProcAddress(hInstDLL, "DllGetVersion");
- if(pfnDllGetVersion == NULL)
- return E_NOTIMPL;
- return (*pfnDllGetVersion)(pDllVersionInfo);
- }
- inline HRESULT AtlGetDllVersion(LPCTSTR lpstrDllName, DLLVERSIONINFO* pDllVersionInfo)
- {
- HINSTANCE hInstDLL = ::LoadLibrary(lpstrDllName);
- if(hInstDLL == NULL)
- return E_FAIL;
- HRESULT hRet = AtlGetDllVersion(hInstDLL, pDllVersionInfo);
- ::FreeLibrary(hInstDLL);
- return hRet;
- }
- // Common Control Versions:
- // Win95/WinNT 4.0 maj=4 min=00
- // IE 3.x maj=4 min=70
- // IE 4.0 maj=4 min=71
- inline HRESULT AtlGetCommCtrlVersion(LPDWORD pdwMajor, LPDWORD pdwMinor)
- {
- ATLASSERT((pdwMajor != NULL) && (pdwMinor != NULL));
- if((pdwMajor == NULL) || (pdwMinor == NULL))
- return E_INVALIDARG;
- DLLVERSIONINFO dvi;
- ::ZeroMemory(&dvi, sizeof(dvi));
- dvi.cbSize = sizeof(dvi);
- HRESULT hRet = AtlGetDllVersion(_T("comctl32.dll"), &dvi);
- if(SUCCEEDED(hRet))
- {
- *pdwMajor = dvi.dwMajorVersion;
- *pdwMinor = dvi.dwMinorVersion;
- }
- else if(hRet == E_NOTIMPL)
- {
- // If DllGetVersion is not there, then the DLL is a version
- // previous to the one shipped with IE 3.x
- *pdwMajor = 4;
- *pdwMinor = 0;
- hRet = S_OK;
- }
- return hRet;
- }
- // Shell Versions:
- // Win95/WinNT 4.0 maj=4 min=00
- // IE 3.x, IE 4.0 without Web Integrated Desktop maj=4 min=00
- // IE 4.0 with Web Integrated Desktop maj=4 min=71
- // IE 4.01 with Web Integrated Desktop maj=4 min=72
- inline HRESULT AtlGetShellVersion(LPDWORD pdwMajor, LPDWORD pdwMinor)
- {
- ATLASSERT((pdwMajor != NULL) && (pdwMinor != NULL));
- if((pdwMajor == NULL) || (pdwMinor == NULL))
- return E_INVALIDARG;
- DLLVERSIONINFO dvi;
- ::ZeroMemory(&dvi, sizeof(dvi));
- dvi.cbSize = sizeof(dvi);
- HRESULT hRet = AtlGetDllVersion(_T("shell32.dll"), &dvi);
- if(SUCCEEDED(hRet))
- {
- *pdwMajor = dvi.dwMajorVersion;
- *pdwMinor = dvi.dwMinorVersion;
- }
- else if(hRet == E_NOTIMPL)
- {
- // If DllGetVersion is not there, then the DLL is a version
- // previous to the one shipped with IE 4.x
- *pdwMajor = 4;
- *pdwMinor = 0;
- hRet = S_OK;
- }
- return hRet;
- }
- } // namespace ATL
- #endif // (_ATL_VER >= 0x0B00)
- // These are always included
- #include "atlwinx.h"
- #include "atluser.h"
- #include "atlgdi.h"
- #ifndef _WTL_NO_AUTOMATIC_NAMESPACE
- using namespace WTL;
- #endif // !_WTL_NO_AUTOMATIC_NAMESPACE
- #endif // __ATLAPP_H__
|