Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / wtl / include / atltheme.h
blob73d46e8288cb9bfb725ddd3cf099d4536478a63b
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 __ATLTHEME_H__
10 #define __ATLTHEME_H__
12 #pragma once
14 #ifndef __cplusplus
15 #error ATL requires C++ compilation (use a .cpp suffix)
16 #endif
18 #ifdef _WIN32_WCE
19 #error atltheme.h is not supported on Windows CE
20 #endif
22 #ifndef __ATLAPP_H__
23 #error atltheme.h requires atlapp.h to be included first
24 #endif
26 #ifndef __ATLWIN_H__
27 #error atltheme.h requires atlwin.h to be included first
28 #endif
30 #if (_WIN32_WINNT < 0x0501)
31 #error atltheme.h requires _WIN32_WINNT >= 0x0501
32 #endif // (_WIN32_WINNT < 0x0501)
34 #if defined(_WTL_USE_VSSYM32) || (defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN))
35 #include <vssym32.h>
36 #else
37 #include <tmschema.h>
38 #endif
40 #include <uxtheme.h>
41 #pragma comment(lib, "uxtheme.lib")
43 // Note: To create an application that also runs on older versions of Windows,
44 // use delay load of uxtheme.dll and ensure that no calls to the Theme API are
45 // made if theming is not supported. It is enough to check if m_hTheme is NULL.
46 // Example:
47 // if(m_hTheme != NULL)
48 // {
49 // DrawThemeBackground(dc, BP_PUSHBUTTON, PBS_NORMAL, &rect, NULL);
50 // DrawThemeText(dc, BP_PUSHBUTTON, PBS_NORMAL, L"Button", -1, DT_SINGLELINE | DT_CENTER | DT_VCENTER, 0, &rect);
51 // }
52 // else
53 // {
54 // dc.DrawFrameControl(&rect, DFC_BUTTON, DFCS_BUTTONPUSH);
55 // dc.DrawText(_T("Button"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
56 // }
58 // Delay load is NOT AUTOMATIC for VC++ 7, you have to link to delayimp.lib,
59 // and add uxtheme.dll in the Linker.Input.Delay Loaded DLLs section of the
60 // project properties.
61 #if (_MSC_VER < 1300) && !defined(_WTL_NO_THEME_DELAYLOAD)
62 #pragma comment(lib, "delayimp.lib")
63 #pragma comment(linker, "/delayload:uxtheme.dll")
64 #endif // (_MSC_VER < 1300) && !defined(_WTL_NO_THEME_DELAYLOAD)
66 // Hack: Signatures in uxtheme.h changed - the only way to check which variant of uxtheme.h
67 // is included is to check for presence of new defines MAX_THEMECOLOR and MAX_THEMESIZE
68 #ifndef _WTL_NEW_UXTHEME
69 #if defined(MAX_THEMECOLOR) && defined(MAX_THEMESIZE)
70 #define _WTL_NEW_UXTHEME
71 #endif // defined(MAX_THEMECOLOR) && defined(MAX_THEMESIZE)
72 #endif // _WTL_NEW_UXTHEME
75 ///////////////////////////////////////////////////////////////////////////////
76 // Classes in this file:
78 // CTheme
79 // CThemeImpl<T, TBase>
81 // CBufferedPaint
82 // CBufferedPaintImpl<T>
83 // CBufferedPaintWindowImpl<T, TBase, TWinTraits>
84 // CBufferedAnimation
85 // CBufferedAnimationImpl<T, TState>
86 // CBufferedAnimationWindowImpl<T, TState, TBase, TWinTraits>
88 // Global functions:
89 // AtlDrawThemeClientEdge()
92 namespace WTL
95 ///////////////////////////////////////////////////////////////////////////////
96 // CTheme - wrapper for theme handle
98 class CTheme
100 public:
101 // Data members
102 HTHEME m_hTheme;
103 static int m_nIsThemingSupported;
105 // Constructor
106 CTheme(HTHEME hTheme = NULL) : m_hTheme(hTheme)
108 IsThemingSupported();
111 // Operators and helpers
112 bool IsThemeNull() const
114 return (m_hTheme == NULL);
117 CTheme& operator =(HTHEME hTheme)
119 m_hTheme = hTheme;
120 return *this;
123 operator HTHEME() const
125 return m_hTheme;
128 void Attach(HTHEME hTheme)
130 m_hTheme = hTheme;
133 HTHEME Detach()
135 HTHEME hTheme = m_hTheme;
136 m_hTheme = NULL;
137 return hTheme;
140 // Theme support helper
141 static bool IsThemingSupported()
143 if(m_nIsThemingSupported == -1)
145 CStaticDataInitCriticalSectionLock lock;
146 if(FAILED(lock.Lock()))
148 ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CTheme::IsThemingSupported.\n"));
149 ATLASSERT(FALSE);
150 return false;
153 if(m_nIsThemingSupported == -1)
155 HMODULE hThemeDLL = ::LoadLibrary(_T("uxtheme.dll"));
156 m_nIsThemingSupported = (hThemeDLL != NULL) ? 1 : 0;
157 if(hThemeDLL != NULL)
158 ::FreeLibrary(hThemeDLL);
161 lock.Unlock();
164 ATLASSERT(m_nIsThemingSupported != -1);
165 return (m_nIsThemingSupported == 1);
168 // Operations and theme properties
169 HTHEME OpenThemeData(HWND hWnd, LPCWSTR pszClassList)
171 if(!IsThemingSupported())
172 return NULL;
174 ATLASSERT(m_hTheme == NULL);
175 m_hTheme = ::OpenThemeData(hWnd, pszClassList);
176 return m_hTheme;
179 HRESULT CloseThemeData()
181 HRESULT hRet = S_FALSE;
182 if(m_hTheme != NULL)
184 hRet = ::CloseThemeData(m_hTheme);
185 if(SUCCEEDED(hRet))
186 m_hTheme = NULL;
188 return hRet;
191 HRESULT DrawThemeBackground(HDC hDC, int nPartID, int nStateID, LPCRECT pRect, LPCRECT pClipRect = NULL)
193 ATLASSERT(m_hTheme != NULL);
194 return ::DrawThemeBackground(m_hTheme, hDC, nPartID, nStateID, pRect, pClipRect);
197 HRESULT DrawThemeBackgroundEx(HDC hDC, int nPartID, int nStateID, LPCRECT pRect, const DTBGOPTS* pOptions = NULL)
199 ATLASSERT(m_hTheme != NULL);
200 return ::DrawThemeBackgroundEx(m_hTheme, hDC, nPartID, nStateID, pRect, pOptions);
203 HRESULT DrawThemeText(HDC hDC, int nPartID, int nStateID, LPCWSTR pszText, int nCharCount, DWORD dwTextFlags, DWORD dwTextFlags2, LPCRECT pRect)
205 ATLASSERT(m_hTheme != NULL);
206 return ::DrawThemeText(m_hTheme, hDC, nPartID, nStateID, pszText, nCharCount, dwTextFlags, dwTextFlags2, pRect);
209 HRESULT GetThemeBackgroundContentRect(HDC hDC, int nPartID, int nStateID, LPCRECT pBoundingRect, LPRECT pContentRect) const
211 ATLASSERT(m_hTheme != NULL);
212 return ::GetThemeBackgroundContentRect(m_hTheme, hDC, nPartID, nStateID, pBoundingRect, pContentRect);
215 HRESULT GetThemeBackgroundExtent(HDC hDC, int nPartID, int nStateID, LPCRECT pContentRect, LPRECT pExtentRect) const
217 ATLASSERT(m_hTheme != NULL);
218 return ::GetThemeBackgroundExtent(m_hTheme, hDC, nPartID, nStateID, pContentRect, pExtentRect);
221 HRESULT GetThemePartSize(HDC hDC, int nPartID, int nStateID, LPRECT pRect, enum THEMESIZE eSize, LPSIZE pSize) const
223 ATLASSERT(m_hTheme != NULL);
224 return ::GetThemePartSize(m_hTheme, hDC, nPartID, nStateID, pRect, eSize, pSize);
227 HRESULT GetThemeTextExtent(HDC hDC, int nPartID, int nStateID, LPCWSTR pszText, int nCharCount, DWORD dwTextFlags, LPCRECT pBoundingRect, LPRECT pExtentRect) const
229 ATLASSERT(m_hTheme != NULL);
230 return ::GetThemeTextExtent(m_hTheme, hDC, nPartID, nStateID, pszText, nCharCount, dwTextFlags, pBoundingRect, pExtentRect);
233 HRESULT GetThemeTextMetrics(HDC hDC, int nPartID, int nStateID, PTEXTMETRICW pTextMetric) const
235 ATLASSERT(m_hTheme != NULL);
236 #ifdef _WTL_NEW_UXTHEME
237 return ::GetThemeTextMetrics(m_hTheme, hDC, nPartID, nStateID, pTextMetric);
238 #else // !_WTL_NEW_UXTHEME
239 // Note: The cast to PTEXTMETRIC is because uxtheme.h incorrectly uses it instead of PTEXTMETRICW
240 return ::GetThemeTextMetrics(m_hTheme, hDC, nPartID, nStateID, (PTEXTMETRIC)pTextMetric);
241 #endif // !_WTL_NEW_UXTHEME
244 HRESULT GetThemeBackgroundRegion(HDC hDC, int nPartID, int nStateID, LPCRECT pRect, HRGN* pRegion) const
246 ATLASSERT(m_hTheme != NULL);
247 return ::GetThemeBackgroundRegion(m_hTheme, hDC, nPartID, nStateID, pRect, pRegion);
250 HRESULT HitTestThemeBackground(HDC hDC, int nPartID, int nStateID, DWORD dwOptions, LPCRECT pRect, HRGN hrgn, POINT ptTest, WORD* pwHitTestCode) const
252 ATLASSERT(m_hTheme != NULL);
253 return ::HitTestThemeBackground(m_hTheme, hDC, nPartID, nStateID, dwOptions, pRect, hrgn, ptTest, pwHitTestCode);
256 HRESULT DrawThemeEdge(HDC hDC, int nPartID, int nStateID, LPCRECT pDestRect, UINT uEdge, UINT uFlags, LPRECT pContentRect = NULL)
258 ATLASSERT(m_hTheme != NULL);
259 return ::DrawThemeEdge(m_hTheme, hDC, nPartID, nStateID, pDestRect, uEdge, uFlags, pContentRect);
262 HRESULT DrawThemeIcon(HDC hDC, int nPartID, int nStateID, LPCRECT pRect, HIMAGELIST himl, int nImageIndex)
264 ATLASSERT(m_hTheme != NULL);
265 return ::DrawThemeIcon(m_hTheme, hDC, nPartID, nStateID, pRect, himl, nImageIndex);
268 BOOL IsThemePartDefined(int nPartID, int nStateID) const
270 ATLASSERT(m_hTheme != NULL);
271 return ::IsThemePartDefined(m_hTheme, nPartID, nStateID);
274 BOOL IsThemeBackgroundPartiallyTransparent(int nPartID, int nStateID) const
276 ATLASSERT(m_hTheme != NULL);
277 return ::IsThemeBackgroundPartiallyTransparent(m_hTheme, nPartID, nStateID);
280 HRESULT GetThemeColor(int nPartID, int nStateID, int nPropID, COLORREF* pColor) const
282 ATLASSERT(m_hTheme != NULL);
283 return ::GetThemeColor(m_hTheme, nPartID, nStateID, nPropID, pColor);
286 HRESULT GetThemeMetric(HDC hDC, int nPartID, int nStateID, int nPropID, int* pnVal) const
288 ATLASSERT(m_hTheme != NULL);
289 return ::GetThemeMetric(m_hTheme, hDC, nPartID, nStateID, nPropID, pnVal);
292 HRESULT GetThemeString(int nPartID, int nStateID, int nPropID, LPWSTR pszBuff, int cchMaxBuffChars) const
294 ATLASSERT(m_hTheme != NULL);
295 return ::GetThemeString(m_hTheme, nPartID, nStateID, nPropID, pszBuff, cchMaxBuffChars);
298 HRESULT GetThemeBool(int nPartID, int nStateID, int nPropID, BOOL* pfVal) const
300 ATLASSERT(m_hTheme != NULL);
301 return ::GetThemeBool(m_hTheme, nPartID, nStateID, nPropID, pfVal);
304 HRESULT GetThemeInt(int nPartID, int nStateID, int nPropID, int* pnVal) const
306 ATLASSERT(m_hTheme != NULL);
307 return ::GetThemeInt(m_hTheme, nPartID, nStateID, nPropID, pnVal);
310 HRESULT GetThemeEnumValue(int nPartID, int nStateID, int nPropID, int* pnVal) const
312 ATLASSERT(m_hTheme != NULL);
313 return ::GetThemeEnumValue(m_hTheme, nPartID, nStateID, nPropID, pnVal);
316 HRESULT GetThemePosition(int nPartID, int nStateID, int nPropID, LPPOINT pPoint) const
318 ATLASSERT(m_hTheme != NULL);
319 return ::GetThemePosition(m_hTheme, nPartID, nStateID, nPropID, pPoint);
322 // deprecated
323 HRESULT GetThemeFont(int nPartID, HDC hDC, int nStateID, int nPropID, LOGFONTW* pFont) const
325 ATLASSERT(m_hTheme != NULL);
326 #ifdef _WTL_NEW_UXTHEME
327 return ::GetThemeFont(m_hTheme, hDC, nPartID, nStateID, nPropID, pFont);
328 #else // !_WTL_NEW_UXTHEME
329 // Note: The cast to LOGFONT* is because uxtheme.h incorrectly uses it instead of LOGFONTW*
330 return ::GetThemeFont(m_hTheme, hDC, nPartID, nStateID, nPropID, (LOGFONT*)pFont);
331 #endif // !_WTL_NEW_UXTHEME
334 HRESULT GetThemeFont(HDC hDC, int nPartID, int nStateID, int nPropID, LOGFONTW* pFont) const
336 ATLASSERT(m_hTheme != NULL);
337 #ifdef _WTL_NEW_UXTHEME
338 return ::GetThemeFont(m_hTheme, hDC, nPartID, nStateID, nPropID, pFont);
339 #else // !_WTL_NEW_UXTHEME
340 // Note: The cast to LOGFONT* is because uxtheme.h incorrectly uses it instead of LOGFONTW*
341 return ::GetThemeFont(m_hTheme, hDC, nPartID, nStateID, nPropID, (LOGFONT*)pFont);
342 #endif // !_WTL_NEW_UXTHEME
345 HRESULT GetThemeRect(int nPartID, int nStateID, int nPropID, LPRECT pRect) const
347 ATLASSERT(m_hTheme != NULL);
348 return ::GetThemeRect(m_hTheme, nPartID, nStateID, nPropID, pRect);
351 HRESULT GetThemeMargins(HDC hDC, int nPartID, int nStateID, int nPropID, LPRECT pRect, PMARGINS pMargins) const
353 ATLASSERT(m_hTheme != NULL);
354 return ::GetThemeMargins(m_hTheme, hDC, nPartID, nStateID, nPropID, pRect, pMargins);
357 HRESULT GetThemeIntList(int nPartID, int nStateID, int nPropID, INTLIST* pIntList) const
359 ATLASSERT(m_hTheme != NULL);
360 return ::GetThemeIntList(m_hTheme, nPartID, nStateID, nPropID, pIntList);
363 HRESULT GetThemePropertyOrigin(int nPartID, int nStateID, int nPropID, enum PROPERTYORIGIN* pOrigin) const
365 ATLASSERT(m_hTheme != NULL);
366 return ::GetThemePropertyOrigin(m_hTheme, nPartID, nStateID, nPropID, pOrigin);
369 HRESULT GetThemeFilename(int nPartID, int nStateID, int nPropID, LPWSTR pszThemeFileName, int cchMaxBuffChars) const
371 ATLASSERT(m_hTheme != NULL);
372 return ::GetThemeFilename(m_hTheme, nPartID, nStateID, nPropID, pszThemeFileName, cchMaxBuffChars);
375 COLORREF GetThemeSysColor(int nColorID) const
377 ATLASSERT(m_hTheme != NULL);
378 return ::GetThemeSysColor(m_hTheme, nColorID);
381 HBRUSH GetThemeSysColorBrush(int nColorID) const
383 ATLASSERT(m_hTheme != NULL);
384 return ::GetThemeSysColorBrush(m_hTheme, nColorID);
387 int GetThemeSysSize(int nSizeID) const
389 ATLASSERT(m_hTheme != NULL);
390 return ::GetThemeSysSize(m_hTheme, nSizeID);
393 BOOL GetThemeSysBool(int nBoolID) const
395 ATLASSERT(m_hTheme != NULL);
396 return ::GetThemeSysBool(m_hTheme, nBoolID);
399 HRESULT GetThemeSysFont(int nFontID, LOGFONTW* plf) const
401 ATLASSERT(m_hTheme != NULL);
402 #ifdef _WTL_NEW_UXTHEME
403 return ::GetThemeSysFont(m_hTheme, nFontID, plf);
404 #else // !_WTL_NEW_UXTHEME
405 // Note: The cast to LOGFONT* is because uxtheme.h incorrectly uses it instead of LOGFONTW*
406 return ::GetThemeSysFont(m_hTheme, nFontID, (LOGFONT*)plf);
407 #endif // !_WTL_NEW_UXTHEME
410 HRESULT GetThemeSysString(int nStringID, LPWSTR pszStringBuff, int cchMaxStringChars) const
412 ATLASSERT(m_hTheme != NULL);
413 return ::GetThemeSysString(m_hTheme, nStringID, pszStringBuff, cchMaxStringChars);
416 HRESULT GetThemeSysInt(int nIntID, int* pnValue) const
418 ATLASSERT(m_hTheme != NULL);
419 return ::GetThemeSysInt(m_hTheme, nIntID, pnValue);
422 #ifdef _WTL_NEW_UXTHEME
423 HTHEME OpenThemeDataEx(HWND hWnd, LPCWSTR pszClassList, DWORD dwFlags)
425 if(!IsThemingSupported())
426 return NULL;
428 ATLASSERT(m_hTheme == NULL);
429 m_hTheme = ::OpenThemeDataEx(hWnd, pszClassList, dwFlags);
430 return m_hTheme;
433 HRESULT DrawThemeTextEx(HDC hDC, int nPartID, int nStateID, LPCWSTR pszText, int cchText, DWORD dwTextFlags, LPRECT lpRect, const DTTOPTS* pOptions)
435 ATLASSERT(m_hTheme != NULL);
436 return ::DrawThemeTextEx(m_hTheme, hDC, nPartID, nStateID, pszText, cchText, dwTextFlags, lpRect, pOptions);
439 HRESULT GetThemeTransitionDuration(int nPartID, int nFromStateID, int nToStateID, int nPropID, DWORD& dwDuration)
441 ATLASSERT(m_hTheme != NULL);
442 return ::GetThemeTransitionDuration(m_hTheme, nPartID, nFromStateID, nToStateID, nPropID, &dwDuration);
444 #endif // _WTL_NEW_UXTHEME
446 #if (_WIN32_WINNT >= 0x0600)
447 HRESULT GetThemeBitmap(int nPartID, int nStateID, int nPropID, ULONG uFlags, HBITMAP& hBitmap)
449 ATLASSERT(m_hTheme != NULL);
450 return ::GetThemeBitmap(m_hTheme, nPartID, nStateID, nPropID, uFlags, &hBitmap);
453 HRESULT GetThemeStream(int nPartID, int nStateID, int nPropID, VOID** ppvStream, DWORD* pcbStream, HINSTANCE hInstance)
455 ATLASSERT(m_hTheme != NULL);
456 return ::GetThemeStream(m_hTheme, nPartID, nStateID, nPropID, ppvStream, pcbStream, hInstance);
458 #endif // (_WIN32_WINNT >= 0x0600)
461 __declspec(selectany) int CTheme::m_nIsThemingSupported = -1;
464 ///////////////////////////////////////////////////////////////////////////////
465 // CThemeImpl - theme support implementation
467 // Derive from this class to implement window with theme support.
468 // Example:
469 // class CMyThemeWindow : public CWindowImpl<CMyThemeWindow>, public CThemeImpl<CMyThemeWindow>
470 // {
471 // ...
472 // BEGIN_MSG_MAP(CMyThemeWindow)
473 // CHAIN_MSG_MAP(CThemeImpl<CMyThemeWindow>)
474 // ...
475 // END_MSG_MAP()
476 // ...
477 // };
479 // If you set theme class list, the class will automaticaly open/close/reopen theme data.
482 // Helper for drawing theme client edge
483 inline bool AtlDrawThemeClientEdge(HTHEME hTheme, HWND hWnd, HRGN hRgnUpdate = NULL, HBRUSH hBrush = NULL, int nPartID = 0, int nStateID = 0)
485 ATLASSERT(hTheme != NULL);
486 ATLASSERT(::IsWindow(hWnd));
488 CWindowDC dc(hWnd);
489 if(dc.IsNull())
490 return false;
492 // Get border size
493 int cxBorder = GetSystemMetrics(SM_CXBORDER);
494 int cyBorder = GetSystemMetrics(SM_CYBORDER);
495 if(SUCCEEDED(::GetThemeInt(hTheme, nPartID, nStateID, TMT_SIZINGBORDERWIDTH, &cxBorder)))
496 cyBorder = cxBorder;
498 RECT rect;
499 ::GetWindowRect(hWnd, &rect);
501 // Remove the client edge from the update region
502 int cxEdge = GetSystemMetrics(SM_CXEDGE);
503 int cyEdge = GetSystemMetrics(SM_CYEDGE);
504 ::InflateRect(&rect, -cxEdge, -cyEdge);
505 CRgn rgn;
506 rgn.CreateRectRgnIndirect(&rect);
507 if(rgn.IsNull())
508 return false;
510 if(hRgnUpdate != NULL)
511 rgn.CombineRgn(hRgnUpdate, rgn, RGN_AND);
513 ::OffsetRect(&rect, -rect.left, -rect.top);
515 ::OffsetRect(&rect, cxEdge, cyEdge);
516 dc.ExcludeClipRect(&rect);
517 ::InflateRect(&rect, cxEdge, cyEdge);
519 ::DrawThemeBackground(hTheme, dc, nPartID, nStateID, &rect, NULL);
521 // Use background brush too, since theme border might not cover everything
522 if(cxBorder < cxEdge && cyBorder < cyEdge)
524 if(hBrush == NULL)
525 // need conditional code because types don't match in winuser.h
526 #ifdef _WIN64
527 hBrush = (HBRUSH)::GetClassLongPtr(hWnd, GCLP_HBRBACKGROUND);
528 #else
529 hBrush = (HBRUSH)UlongToPtr(::GetClassLongPtr(hWnd, GCLP_HBRBACKGROUND));
530 #endif
532 ::InflateRect(&rect, cxBorder - cxEdge, cyBorder - cyEdge);
533 dc.FillRect(&rect, hBrush);
536 ::DefWindowProc(hWnd, WM_NCPAINT, (WPARAM)rgn.m_hRgn, 0L);
538 return true;
542 // Theme extended styles
543 #define THEME_EX_3DCLIENTEDGE 0x00000001
544 #define THEME_EX_THEMECLIENTEDGE 0x00000002
546 template <class T, class TBase = CTheme>
547 class CThemeImpl : public TBase
549 public:
550 // Data members
551 LPWSTR m_lpstrThemeClassList;
552 DWORD m_dwExtendedStyle; // theme specific extended styles
554 // Constructor & destructor
555 CThemeImpl() : m_lpstrThemeClassList(NULL), m_dwExtendedStyle(0)
558 ~CThemeImpl()
560 delete [] m_lpstrThemeClassList;
563 // Attributes
564 bool SetThemeClassList(LPCWSTR lpstrThemeClassList)
566 if(m_lpstrThemeClassList != NULL)
568 delete [] m_lpstrThemeClassList;
569 m_lpstrThemeClassList = NULL;
572 if(lpstrThemeClassList == NULL)
573 return true;
575 int cchLen = lstrlenW(lpstrThemeClassList) + 1;
576 ATLTRY(m_lpstrThemeClassList = new WCHAR[cchLen]);
577 if(m_lpstrThemeClassList == NULL)
578 return false;
580 SecureHelper::strcpyW_x(m_lpstrThemeClassList, cchLen, lpstrThemeClassList);
582 return true;
585 bool GetThemeClassList(LPWSTR lpstrThemeClassList, int cchListBuffer) const
587 int cchLen = lstrlenW(m_lpstrThemeClassList) + 1;
588 if(cchListBuffer < cchLen)
589 return false;
591 SecureHelper::strcpyW_x(lpstrThemeClassList, cchListBuffer, m_lpstrThemeClassList);
593 return true;
596 LPCWSTR GetThemeClassList() const
598 return m_lpstrThemeClassList;
601 DWORD SetThemeExtendedStyle(DWORD dwExtendedStyle, DWORD dwMask = 0)
603 DWORD dwPrevStyle = m_dwExtendedStyle;
604 if(dwMask == 0)
605 m_dwExtendedStyle = dwExtendedStyle;
606 else
607 m_dwExtendedStyle = (m_dwExtendedStyle & ~dwMask) | (dwExtendedStyle & dwMask);
608 return dwPrevStyle;
611 DWORD GetThemeExtendedStyle() const
613 return m_dwExtendedStyle;
616 // Operations
617 HTHEME OpenThemeData()
619 T* pT = static_cast<T*>(this);
620 ATLASSERT(::IsWindow(pT->m_hWnd));
621 ATLASSERT(m_lpstrThemeClassList != NULL);
622 if(m_lpstrThemeClassList == NULL)
623 return NULL;
624 CloseThemeData();
625 return TBase::OpenThemeData(pT->m_hWnd, m_lpstrThemeClassList);
628 HTHEME OpenThemeData(LPCWSTR pszClassList)
630 if(!SetThemeClassList(pszClassList))
631 return NULL;
632 return OpenThemeData();
635 HRESULT SetWindowTheme(LPCWSTR pszSubAppName, LPCWSTR pszSubIDList)
637 if(!IsThemingSupported())
638 return S_FALSE;
640 T* pT = static_cast<T*>(this);
641 ATLASSERT(::IsWindow(pT->m_hWnd));
642 return ::SetWindowTheme(pT->m_hWnd, pszSubAppName, pszSubIDList);
645 HTHEME GetWindowTheme() const
647 if(!IsThemingSupported())
648 return NULL;
650 const T* pT = static_cast<const T*>(this);
651 ATLASSERT(::IsWindow(pT->m_hWnd));
652 return ::GetWindowTheme(pT->m_hWnd);
655 HRESULT EnableThemeDialogTexture(DWORD dwFlags)
657 if(!IsThemingSupported())
658 return S_FALSE;
660 T* pT = static_cast<T*>(this);
661 ATLASSERT(::IsWindow(pT->m_hWnd));
662 return ::EnableThemeDialogTexture(pT->m_hWnd, dwFlags);
665 BOOL IsThemeDialogTextureEnabled() const
667 if(!IsThemingSupported())
668 return FALSE;
670 const T* pT = static_cast<const T*>(this);
671 ATLASSERT(::IsWindow(pT->m_hWnd));
672 return ::IsThemeDialogTextureEnabled(pT->m_hWnd);
675 HRESULT DrawThemeParentBackground(HDC hDC, const RECT* pRect = NULL)
677 if(!IsThemingSupported())
678 return S_FALSE;
680 T* pT = static_cast<T*>(this);
681 ATLASSERT(::IsWindow(pT->m_hWnd));
682 #ifdef _WTL_NEW_UXTHEME
683 return ::DrawThemeParentBackground(pT->m_hWnd, hDC, pRect);
684 #else
685 return ::DrawThemeParentBackground(pT->m_hWnd, hDC, (RECT*)pRect);
686 #endif
689 #ifdef _WTL_NEW_UXTHEME
690 HRESULT SetWindowThemeAttribute(WINDOWTHEMEATTRIBUTETYPE type, PVOID pvAttribute, DWORD cbAttribute)
692 if(!IsThemingSupported())
693 return S_FALSE;
695 T* pT = static_cast<T*>(this);
696 ATLASSERT(::IsWindow(pT->m_hWnd));
697 return ::SetWindowThemeAttribute(pT->m_hWnd, type, pvAttribute, cbAttribute);
700 HRESULT SetWindowThemeNonClientAttributes(DWORD dwAttributes, DWORD dwMask)
702 if(!IsThemingSupported())
703 return S_FALSE;
705 T* pT = static_cast<T*>(this);
706 ATLASSERT(::IsWindow(pT->m_hWnd));
707 WTA_OPTIONS opt = { dwAttributes, dwMask };
708 return ::SetWindowThemeAttribute(pT->m_hWnd, WTA_NONCLIENT, (PVOID)&opt, sizeof(opt));
711 HRESULT DrawThemeParentBackgroundEx(HDC hDC, DWORD dwFlags, const RECT* lpRect = NULL)
713 if(!IsThemingSupported())
714 return S_FALSE;
716 T* pT = static_cast<T*>(this);
717 ATLASSERT(::IsWindow(pT->m_hWnd));
718 return ::DrawThemeParentBackgroundEx(pT->m_hWnd, hDC, dwFlags, lpRect);
720 #endif // _WTL_NEW_UXTHEME
722 // Message map and handlers
723 // Note: If you handle any of these messages in your derived class,
724 // it is better to put CHAIN_MSG_MAP at the start of your message map.
725 BEGIN_MSG_MAP(CThemeImpl)
726 MESSAGE_HANDLER(WM_CREATE, OnCreate)
727 MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
728 MESSAGE_HANDLER(WM_THEMECHANGED, OnThemeChanged)
729 MESSAGE_HANDLER(WM_NCPAINT, OnNcPaint)
730 END_MSG_MAP()
732 LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
734 if(m_lpstrThemeClassList != NULL)
735 OpenThemeData();
736 bHandled = FALSE;
737 return 1;
740 LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
742 CloseThemeData();
743 bHandled = FALSE;
744 return 1;
747 LRESULT OnThemeChanged(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
749 CloseThemeData();
750 if(m_lpstrThemeClassList != NULL)
751 OpenThemeData();
752 bHandled = FALSE;
753 return 1;
756 LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
758 T* pT = static_cast<T*>(this);
759 ATLASSERT(::IsWindow(pT->m_hWnd));
760 LRESULT lRet = 0;
761 bHandled = FALSE;
762 if(IsThemingSupported() && ((pT->GetExStyle() & WS_EX_CLIENTEDGE) != 0))
764 if((m_dwExtendedStyle & THEME_EX_3DCLIENTEDGE) != 0)
766 lRet = ::DefWindowProc(pT->m_hWnd, uMsg, wParam, lParam);
767 bHandled = TRUE;
769 else if((m_hTheme != NULL) && ((m_dwExtendedStyle & THEME_EX_THEMECLIENTEDGE) != 0))
771 HRGN hRgn = (wParam != 1) ? (HRGN)wParam : NULL;
772 if(pT->DrawThemeClientEdge(hRgn))
773 bHandled = TRUE;
776 return lRet;
779 // Drawing helper
780 bool DrawThemeClientEdge(HRGN hRgnUpdate)
782 T* pT = static_cast<T*>(this);
783 return AtlDrawThemeClientEdge(m_hTheme, pT->m_hWnd, hRgnUpdate, NULL, 0, 0);
787 ///////////////////////////////////////////////////////////////////////////////
788 // Buffered Paint and Animation
790 #ifdef _WTL_NEW_UXTHEME
792 ///////////////////////////////////////////////////////////////////////////////
793 // CBufferedPaintBase - Buffered Paint support for othe classes
795 class CBufferedPaintBase
797 public:
798 static int m_nIsBufferedPaintSupported;
800 CBufferedPaintBase()
802 if(IsBufferedPaintSupported())
803 ATLVERIFY(SUCCEEDED(::BufferedPaintInit()));
806 ~CBufferedPaintBase()
808 if(IsBufferedPaintSupported())
809 ATLVERIFY(SUCCEEDED(::BufferedPaintUnInit()));
812 static bool IsBufferedPaintSupported()
814 if(m_nIsBufferedPaintSupported == -1)
816 CStaticDataInitCriticalSectionLock lock;
817 if(FAILED(lock.Lock()))
819 ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CBufferedPaintBase::IsBufferedPaintSupported.\n"));
820 ATLASSERT(FALSE);
821 return false;
824 if(m_nIsBufferedPaintSupported == -1)
825 m_nIsBufferedPaintSupported = RunTimeHelper::IsVista() ? 1 : 0;
827 lock.Unlock();
830 ATLASSERT(m_nIsBufferedPaintSupported != -1);
831 return (m_nIsBufferedPaintSupported == 1);
835 __declspec(selectany) int CBufferedPaintBase::m_nIsBufferedPaintSupported = -1;
838 ///////////////////////////////////////////////////////////////////////////////
839 // CBufferedPaint - support for buffered paint functions
841 class CBufferedPaint
843 public:
844 HPAINTBUFFER m_hPaintBuffer;
846 CBufferedPaint() : m_hPaintBuffer(NULL)
849 ~CBufferedPaint()
851 ATLVERIFY(SUCCEEDED(End()));
854 bool IsNull() const
856 return (m_hPaintBuffer == NULL);
859 HPAINTBUFFER Begin(HDC hdcTarget, const RECT* prcTarget, BP_BUFFERFORMAT dwFormat, BP_PAINTPARAMS* pPaintParams, HDC* phdcPaint)
861 ATLASSERT(m_hPaintBuffer == NULL);
862 m_hPaintBuffer = ::BeginBufferedPaint(hdcTarget, prcTarget, dwFormat, pPaintParams, phdcPaint);
863 return m_hPaintBuffer;
866 HRESULT End(BOOL bUpdate = TRUE)
868 HRESULT hRet = S_FALSE;
869 if(m_hPaintBuffer != NULL)
871 hRet = ::EndBufferedPaint(m_hPaintBuffer, bUpdate);
872 m_hPaintBuffer = NULL;
874 return hRet;
877 HRESULT GetTargetRect(LPRECT pRect) const
879 ATLASSERT(m_hPaintBuffer != NULL);
880 return ::GetBufferedPaintTargetRect(m_hPaintBuffer, pRect);
883 HDC GetTargetDC() const
885 ATLASSERT(m_hPaintBuffer != NULL);
886 return ::GetBufferedPaintTargetDC(m_hPaintBuffer);
889 HDC GetPaintDC() const
891 ATLASSERT(m_hPaintBuffer != NULL);
892 return ::GetBufferedPaintDC(m_hPaintBuffer);
895 HRESULT GetBits(RGBQUAD** ppbBuffer, int* pcxRow) const
897 ATLASSERT(m_hPaintBuffer != NULL);
898 return ::GetBufferedPaintBits(m_hPaintBuffer, ppbBuffer, pcxRow);
901 HRESULT Clear(const RECT* pRect = NULL)
903 ATLASSERT(m_hPaintBuffer != NULL);
904 return ::BufferedPaintClear(m_hPaintBuffer, pRect);
907 HRESULT SetAlpha(BYTE alpha, const RECT* pRect = NULL)
909 ATLASSERT(m_hPaintBuffer != NULL);
910 return ::BufferedPaintSetAlpha(m_hPaintBuffer, pRect, alpha);
913 HRESULT MakeOpaque(const RECT* pRect = NULL)
915 ATLASSERT(m_hPaintBuffer != NULL);
916 return ::BufferedPaintSetAlpha(m_hPaintBuffer, pRect, 255);
921 ///////////////////////////////////////////////////////////////////////////////
922 // CBufferedPaintImpl - provides buffered paint for any window
924 template <class T>
925 class ATL_NO_VTABLE CBufferedPaintImpl : public CBufferedPaintBase
927 public:
928 CBufferedPaint m_BufferedPaint;
929 BP_BUFFERFORMAT m_dwFormat;
930 BP_PAINTPARAMS m_PaintParams;
932 CBufferedPaintImpl() : m_dwFormat(BPBF_TOPDOWNDIB)
934 memset(&m_PaintParams, 0, sizeof(BP_PAINTPARAMS));
935 m_PaintParams.cbSize = sizeof(BP_PAINTPARAMS);
938 // Message map and handlers
939 BEGIN_MSG_MAP(CBufferedPaintImpl)
940 MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
941 MESSAGE_HANDLER(WM_PAINT, OnPaint)
942 MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
943 END_MSG_MAP()
945 LRESULT OnEraseBackground(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
947 return 1; // no background needed
950 LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
952 T* pT = static_cast<T*>(this);
953 if(wParam != NULL)
955 RECT rect = { 0 };
956 pT->GetClientRect(&rect);
957 pT->DoPaint((HDC)wParam, rect);
959 else
961 CPaintDC dc(pT->m_hWnd);
962 pT->DoBufferedPaint(dc.m_hDC, dc.m_ps.rcPaint);
965 return 0;
968 // Overrideables
969 void DoBufferedPaint(CDCHandle dc, RECT& rect)
971 HDC hDCPaint = NULL;
972 if(IsBufferedPaintSupported())
973 m_BufferedPaint.Begin(dc, &rect, m_dwFormat, &m_PaintParams, &hDCPaint);
975 T* pT = static_cast<T*>(this);
976 if(hDCPaint != NULL)
977 pT->DoPaint(hDCPaint, rect);
978 else
979 pT->DoPaint(dc.m_hDC, rect);
981 if(IsBufferedPaintSupported())
982 m_BufferedPaint.End();
985 void DoPaint(CDCHandle /*dc*/, RECT& /*rect*/)
987 // must be implemented in a derived class
988 ATLASSERT(FALSE);
993 ///////////////////////////////////////////////////////////////////////////////
994 // CBufferedPaintWindowImpl - implements a window that uses buffered paint
996 template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
997 class ATL_NO_VTABLE CBufferedPaintWindowImpl :
998 public ATL::CWindowImpl<T, TBase, TWinTraits>,
999 public CBufferedPaintImpl< T >
1001 public:
1002 BEGIN_MSG_MAP(CBufferedPaintWindowImpl)
1003 CHAIN_MSG_MAP(CBufferedPaintImpl< T >)
1004 END_MSG_MAP()
1008 ///////////////////////////////////////////////////////////////////////////////
1009 // CBufferedAnimation - support for buffered animation
1011 class CBufferedAnimation
1013 public:
1014 HANIMATIONBUFFER m_hAnimationBuffer;
1016 CBufferedAnimation() : m_hAnimationBuffer(NULL)
1019 ~CBufferedAnimation()
1021 ATLVERIFY(SUCCEEDED(End()));
1024 bool IsNull() const
1026 return (m_hAnimationBuffer == NULL);
1029 HANIMATIONBUFFER Begin(HWND hWnd, HDC hDCTarget, const RECT* pRectTarget, BP_BUFFERFORMAT dwFormat, BP_PAINTPARAMS* pPaintParams, BP_ANIMATIONPARAMS* pAnimationParams, HDC* phdcFrom, HDC* phdcTo)
1031 ATLASSERT(m_hAnimationBuffer == NULL);
1032 m_hAnimationBuffer = ::BeginBufferedAnimation(hWnd, hDCTarget, pRectTarget, dwFormat, pPaintParams, pAnimationParams, phdcFrom, phdcTo);
1033 return m_hAnimationBuffer;
1036 HRESULT End(BOOL bUpdate = TRUE)
1038 HRESULT hRet = S_FALSE;
1039 if(m_hAnimationBuffer != NULL)
1041 hRet = ::EndBufferedAnimation(m_hAnimationBuffer, bUpdate);
1042 m_hAnimationBuffer = NULL;
1044 return hRet;
1047 static bool IsRendering(HWND hWnd, HDC hDC)
1049 return (::BufferedPaintRenderAnimation(hWnd, hDC) != FALSE);
1054 ///////////////////////////////////////////////////////////////////////////////
1055 // CBufferedAnimationImpl - provides buffered animation support for any window
1057 // Note: You can either use m_State and m_NewState to store the state information
1058 // for the animation change, or map your state to those data members. DoPaint()
1059 // should only rely on the state information that is passed to it.
1061 template <class T, class TState = DWORD_PTR>
1062 class ATL_NO_VTABLE CBufferedAnimationImpl : public CBufferedPaintBase
1064 public:
1065 BP_BUFFERFORMAT m_dwFormat;
1066 BP_PAINTPARAMS m_PaintParams;
1067 BP_ANIMATIONPARAMS m_AnimationParams;
1069 TState m_State;
1070 TState m_NewState;
1072 CBufferedAnimationImpl(TState InitialState) : m_dwFormat(BPBF_TOPDOWNDIB)
1074 memset(&m_PaintParams, 0, sizeof(BP_PAINTPARAMS));
1075 m_PaintParams.cbSize = sizeof(BP_PAINTPARAMS);
1077 memset(&m_AnimationParams, 0, sizeof(BP_ANIMATIONPARAMS));
1078 m_AnimationParams.cbSize = sizeof(BP_ANIMATIONPARAMS);
1079 m_AnimationParams.style = BPAS_LINEAR;
1080 m_AnimationParams.dwDuration = 500;
1082 T* pT = static_cast<T*>(this);
1083 pT->SetState(InitialState);
1084 pT->SetNewState(InitialState);
1087 DWORD GetDuration() const
1089 return m_AnimationParams.dwDuration;
1092 void SetDuration(DWORD dwDuration)
1094 m_AnimationParams.dwDuration = dwDuration;
1097 void DoAnimation(TState NewState, const RECT* pRect = NULL)
1099 T* pT = static_cast<T*>(this);
1100 pT->SetNewState(NewState);
1102 pT->InvalidateRect(pRect, FALSE);
1103 pT->UpdateWindow();
1105 pT->SetState(NewState);
1108 // Message map and handlers
1109 BEGIN_MSG_MAP(CBufferedAnimationImpl)
1110 MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
1111 MESSAGE_HANDLER(WM_PAINT, OnPaint)
1112 MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
1113 END_MSG_MAP()
1115 LRESULT OnEraseBackground(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
1117 return 1; // no background needed
1120 LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
1122 T* pT = static_cast<T*>(this);
1123 if(wParam != NULL)
1125 RECT rect = { 0 };
1126 pT->GetClientRect(&rect);
1127 pT->DoPaint((HDC)wParam, rect, m_NewState);
1129 else
1131 CPaintDC dc(pT->m_hWnd);
1132 pT->DoAnimationPaint(dc.m_hDC, dc.m_ps.rcPaint);
1135 return 0;
1138 // Overrideables
1139 void SetState(TState State)
1141 m_State = State;
1144 void SetNewState(TState State)
1146 m_NewState = State;
1149 bool AreStatesEqual() const
1151 return (m_State == m_NewState);
1154 void DoAnimationPaint(CDCHandle dc, RECT& rect)
1156 T* pT = static_cast<T*>(this);
1157 if(IsBufferedPaintSupported() && CBufferedAnimation::IsRendering(pT->m_hWnd, dc))
1158 return;
1160 DWORD dwDurationSave = m_AnimationParams.dwDuration;
1161 if(pT->AreStatesEqual())
1162 m_AnimationParams.dwDuration = 0;
1164 HDC hdcFrom = NULL, hdcTo = NULL;
1165 CBufferedAnimation ba;
1166 if(IsBufferedPaintSupported())
1167 ba.Begin(pT->m_hWnd, dc, &rect, m_dwFormat, &m_PaintParams, &m_AnimationParams, &hdcFrom, &hdcTo);
1169 if(!ba.IsNull())
1171 if(hdcFrom != NULL)
1172 pT->DoPaint(hdcFrom, rect, m_State);
1174 if (hdcTo != NULL)
1175 pT->DoPaint(hdcTo, rect, m_NewState);
1177 else
1179 pT->DoPaint(dc.m_hDC, rect, m_NewState);
1182 m_AnimationParams.dwDuration = dwDurationSave;
1185 void DoPaint(CDCHandle /*dc*/, RECT& /*rect*/, TState /*State*/)
1187 // must be implemented in a derived class
1188 ATLASSERT(FALSE);
1193 ///////////////////////////////////////////////////////////////////////////////
1194 // CBufferedAnimationWindowImpl - implements a window that uses buffered animation
1196 template <class T, class TState = DWORD_PTR, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
1197 class ATL_NO_VTABLE CBufferedAnimationWindowImpl :
1198 public ATL::CWindowImpl<T, TBase, TWinTraits>,
1199 public CBufferedAnimationImpl< T, TState >
1201 public:
1202 CBufferedAnimationWindowImpl(TState InitialState) : CBufferedAnimationImpl< T, TState >(InitialState)
1205 typedef CBufferedAnimationImpl< T, TState > _baseBufferedAnimation;
1206 BEGIN_MSG_MAP(CBufferedAnimationWindowImpl)
1207 CHAIN_MSG_MAP(_baseBufferedAnimation)
1208 END_MSG_MAP()
1211 #endif // _WTL_NEW_UXTHEME
1213 }; // namespace WTL
1215 #endif // __ATLTHEME_H__