uxtheme.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/uxtheme.h
  3. // Purpose: wxUxThemeEngine class: support for XP themes
  4. // Author: John Platts, Vadim Zeitlin
  5. // Modified by:
  6. // Created: 2003
  7. // Copyright: (c) 2003 John Platts, Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_UXTHEME_H_
  11. #define _WX_UXTHEME_H_
  12. #include "wx/defs.h"
  13. #include "wx/msw/private.h" // we use GetHwndOf()
  14. #include "wx/msw/uxthemep.h"
  15. // Amazingly, GetThemeFont() and GetThemeSysFont() functions use LOGFONTA under
  16. // XP but LOGFONTW (even in non-Unicode build) under later versions of Windows.
  17. // If we declare them as taking LOGFONT below, the code would be able to
  18. // silently pass LOGFONTA to them in ANSI build and would crash at run-time
  19. // under Windows Vista/7 because of a buffer overrun (LOGFONTA being smaller
  20. // than LOGFONTW expected by these functions). If we we declare them as taking
  21. // LOGFONTW, the code wouldn't work correctly under XP. So we use a special
  22. // wxUxThemeFont class to encapsulate this and intentionally change the LOGFONT
  23. // output parameters of the theme functions to take it instead.
  24. class wxUxThemeFont
  25. {
  26. public:
  27. // Trivial default ctor.
  28. wxUxThemeFont() { }
  29. // Just some unique type.
  30. struct Ptr { };
  31. #if wxUSE_UNICODE
  32. // In Unicode build we always use LOGFONT anyhow so this class is
  33. // completely trivial.
  34. Ptr *GetPtr() { return reinterpret_cast<Ptr *>(&m_lfW); }
  35. const LOGFONTW& GetLOGFONT() { return m_lfW; }
  36. #else // !wxUSE_UNICODE
  37. // Return either LOGFONTA or LOGFONTW pointer as required by the current
  38. // Windows version.
  39. Ptr *GetPtr()
  40. {
  41. return UseLOGFONTW() ? reinterpret_cast<Ptr *>(&m_lfW)
  42. : reinterpret_cast<Ptr *>(&m_lfA);
  43. }
  44. // This method returns LOGFONT (i.e. LOGFONTA in ANSI build and LOGFONTW in
  45. // Unicode one) which can be used with other, normal, Windows or wx
  46. // functions. Internally it may need to transform LOGFONTW to LOGFONTA.
  47. const LOGFONTA& GetLOGFONT()
  48. {
  49. if ( UseLOGFONTW() )
  50. {
  51. // Most of the fields are the same in LOGFONTA and LOGFONTW so just
  52. // copy everything by default.
  53. memcpy(&m_lfA, &m_lfW, sizeof(m_lfA));
  54. // But the face name must be converted from Unicode.
  55. WideCharToMultiByte(CP_ACP, 0, m_lfW.lfFaceName, -1,
  56. m_lfA.lfFaceName, sizeof(m_lfA.lfFaceName),
  57. NULL, NULL);
  58. }
  59. return m_lfA;
  60. }
  61. private:
  62. static bool UseLOGFONTW()
  63. {
  64. return wxGetWinVersion() >= wxWinVersion_Vista;
  65. }
  66. LOGFONTA m_lfA;
  67. #endif // wxUSE_UNICODE/!wxUSE_UNICODE
  68. private:
  69. LOGFONTW m_lfW;
  70. wxDECLARE_NO_COPY_CLASS(wxUxThemeFont);
  71. };
  72. typedef HTHEME (__stdcall *PFNWXUOPENTHEMEDATA)(HWND, const wchar_t *);
  73. typedef HRESULT (__stdcall *PFNWXUCLOSETHEMEDATA)(HTHEME);
  74. typedef HRESULT (__stdcall *PFNWXUDRAWTHEMEBACKGROUND)(HTHEME, HDC, int, int, const RECT *, const RECT *);
  75. typedef HRESULT (__stdcall *PFNWXUDRAWTHEMETEXT)(HTHEME, HDC, int, int, const wchar_t *, int, DWORD, DWORD, const RECT *);
  76. typedef HRESULT (__stdcall *PFNWXUGETTHEMEBACKGROUNDCONTENTRECT)(HTHEME, HDC, int, int, const RECT *, RECT *);
  77. typedef HRESULT (__stdcall *PFNWXUGETTHEMEBACKGROUNDEXTENT)(HTHEME, HDC, int, int, const RECT *, RECT *);
  78. typedef HRESULT (__stdcall *PFNWXUGETTHEMEPARTSIZE)(HTHEME, HDC, int, int, const RECT *, /* enum */ THEMESIZE, SIZE *);
  79. typedef HRESULT (__stdcall *PFNWXUGETTHEMETEXTEXTENT)(HTHEME, HDC, int, int, const wchar_t *, int, DWORD, const RECT *, RECT *);
  80. typedef HRESULT (__stdcall *PFNWXUGETTHEMETEXTMETRICS)(HTHEME, HDC, int, int, TEXTMETRIC*);
  81. typedef HRESULT (__stdcall *PFNWXUGETTHEMEBACKGROUNDREGION)(HTHEME, HDC, int, int, const RECT *, HRGN *);
  82. typedef HRESULT (__stdcall *PFNWXUHITTESTTHEMEBACKGROUND)(HTHEME, HDC, int, int, DWORD, const RECT *, HRGN, POINT, unsigned short *);
  83. typedef HRESULT (__stdcall *PFNWXUDRAWTHEMEEDGE)(HTHEME, HDC, int, int, const RECT *, unsigned int, unsigned int, RECT *);
  84. typedef HRESULT (__stdcall *PFNWXUDRAWTHEMEICON)(HTHEME, HDC, int, int, const RECT *, HIMAGELIST, int);
  85. typedef BOOL (__stdcall *PFNWXUISTHEMEPARTDEFINED)(HTHEME, int, int);
  86. typedef BOOL (__stdcall *PFNWXUISTHEMEBACKGROUNDPARTIALLYTRANSPARENT)(HTHEME, int, int);
  87. typedef HRESULT (__stdcall *PFNWXUGETTHEMECOLOR)(HTHEME, int, int, int, COLORREF*);
  88. typedef HRESULT (__stdcall *PFNWXUGETTHEMEMETRIC)(HTHEME, HDC, int, int, int, int *);
  89. typedef HRESULT (__stdcall *PFNWXUGETTHEMESTRING)(HTHEME, int, int, int, wchar_t *, int);
  90. typedef HRESULT (__stdcall *PFNWXUGETTHEMEBOOL)(HTHEME, int, int, int, BOOL *);
  91. typedef HRESULT (__stdcall *PFNWXUGETTHEMEINT)(HTHEME, int, int, int, int *);
  92. typedef HRESULT (__stdcall *PFNWXUGETTHEMEENUMVALUE)(HTHEME, int, int, int, int *);
  93. typedef HRESULT (__stdcall *PFNWXUGETTHEMEPOSITION)(HTHEME, int, int, int, POINT *);
  94. typedef HRESULT (__stdcall *PFNWXUGETTHEMEFONT)(HTHEME, HDC, int, int, int, wxUxThemeFont::Ptr *);
  95. typedef HRESULT (__stdcall *PFNWXUGETTHEMERECT)(HTHEME, int, int, int, RECT *);
  96. typedef HRESULT (__stdcall *PFNWXUGETTHEMEMARGINS)(HTHEME, HDC, int, int, int, RECT *, MARGINS *);
  97. typedef HRESULT (__stdcall *PFNWXUGETTHEMEINTLIST)(HTHEME, int, int, int, INTLIST*);
  98. typedef HRESULT (__stdcall *PFNWXUGETTHEMEPROPERTYORIGIN)(HTHEME, int, int, int, /* enum */ PROPERTYORIGIN *);
  99. typedef HRESULT (__stdcall *PFNWXUSETWINDOWTHEME)(HWND, const wchar_t*, const wchar_t *);
  100. typedef HRESULT (__stdcall *PFNWXUGETTHEMEFILENAME)(HTHEME, int, int, int, wchar_t *, int);
  101. typedef COLORREF(__stdcall *PFNWXUGETTHEMESYSCOLOR)(HTHEME, int);
  102. typedef HBRUSH (__stdcall *PFNWXUGETTHEMESYSCOLORBRUSH)(HTHEME, int);
  103. typedef BOOL (__stdcall *PFNWXUGETTHEMESYSBOOL)(HTHEME, int);
  104. typedef int (__stdcall *PFNWXUGETTHEMESYSSIZE)(HTHEME, int);
  105. typedef HRESULT (__stdcall *PFNWXUGETTHEMESYSFONT)(HTHEME, int, wxUxThemeFont::Ptr *);
  106. typedef HRESULT (__stdcall *PFNWXUGETTHEMESYSSTRING)(HTHEME, int, wchar_t *, int);
  107. typedef HRESULT (__stdcall *PFNWXUGETTHEMESYSINT)(HTHEME, int, int *);
  108. typedef BOOL (__stdcall *PFNWXUISTHEMEACTIVE)();
  109. typedef BOOL (__stdcall *PFNWXUISAPPTHEMED)();
  110. typedef HTHEME (__stdcall *PFNWXUGETWINDOWTHEME)(HWND);
  111. typedef HRESULT (__stdcall *PFNWXUENABLETHEMEDIALOGTEXTURE)(HWND, DWORD);
  112. typedef BOOL (__stdcall *PFNWXUISTHEMEDIALOGTEXTUREENABLED)(HWND);
  113. typedef DWORD (__stdcall *PFNWXUGETTHEMEAPPPROPERTIES)();
  114. typedef void (__stdcall *PFNWXUSETTHEMEAPPPROPERTIES)(DWORD);
  115. typedef HRESULT (__stdcall *PFNWXUGETCURRENTTHEMENAME)(wchar_t *, int, wchar_t *, int, wchar_t *, int);
  116. typedef HRESULT (__stdcall *PFNWXUGETTHEMEDOCUMENTATIONPROPERTY)(const wchar_t *, const wchar_t *, wchar_t *, int);
  117. typedef HRESULT (__stdcall *PFNWXUDRAWTHEMEPARENTBACKGROUND)(HWND, HDC, RECT *);
  118. typedef HRESULT (__stdcall *PFNWXUENABLETHEMING)(BOOL);
  119. // ----------------------------------------------------------------------------
  120. // wxUxThemeEngine: provides all theme functions from uxtheme.dll
  121. // ----------------------------------------------------------------------------
  122. // we always define this class, even if wxUSE_UXTHEME == 0, but we just make it
  123. // empty in this case -- this allows to use it elsewhere without any #ifdefs
  124. #if wxUSE_UXTHEME
  125. #include "wx/dynlib.h"
  126. #define wxUX_THEME_DECLARE(type, func) type func;
  127. #else
  128. #define wxUX_THEME_DECLARE(type, func) type func(...) { return 0; }
  129. #endif
  130. class WXDLLIMPEXP_CORE wxUxThemeEngine
  131. {
  132. public:
  133. // get the theme engine or NULL if themes are not available
  134. static wxUxThemeEngine *Get();
  135. // get the theme enging or NULL if themes are not available or not used for
  136. // this application
  137. static wxUxThemeEngine *GetIfActive();
  138. // all uxtheme.dll functions
  139. wxUX_THEME_DECLARE(PFNWXUOPENTHEMEDATA, OpenThemeData)
  140. wxUX_THEME_DECLARE(PFNWXUCLOSETHEMEDATA, CloseThemeData)
  141. wxUX_THEME_DECLARE(PFNWXUDRAWTHEMEBACKGROUND, DrawThemeBackground)
  142. wxUX_THEME_DECLARE(PFNWXUDRAWTHEMETEXT, DrawThemeText)
  143. wxUX_THEME_DECLARE(PFNWXUGETTHEMEBACKGROUNDCONTENTRECT, GetThemeBackgroundContentRect)
  144. wxUX_THEME_DECLARE(PFNWXUGETTHEMEBACKGROUNDEXTENT, GetThemeBackgroundExtent)
  145. wxUX_THEME_DECLARE(PFNWXUGETTHEMEPARTSIZE, GetThemePartSize)
  146. wxUX_THEME_DECLARE(PFNWXUGETTHEMETEXTEXTENT, GetThemeTextExtent)
  147. wxUX_THEME_DECLARE(PFNWXUGETTHEMETEXTMETRICS, GetThemeTextMetrics)
  148. wxUX_THEME_DECLARE(PFNWXUGETTHEMEBACKGROUNDREGION, GetThemeBackgroundRegion)
  149. wxUX_THEME_DECLARE(PFNWXUHITTESTTHEMEBACKGROUND, HitTestThemeBackground)
  150. wxUX_THEME_DECLARE(PFNWXUDRAWTHEMEEDGE, DrawThemeEdge)
  151. wxUX_THEME_DECLARE(PFNWXUDRAWTHEMEICON, DrawThemeIcon)
  152. wxUX_THEME_DECLARE(PFNWXUISTHEMEPARTDEFINED, IsThemePartDefined)
  153. wxUX_THEME_DECLARE(PFNWXUISTHEMEBACKGROUNDPARTIALLYTRANSPARENT, IsThemeBackgroundPartiallyTransparent)
  154. wxUX_THEME_DECLARE(PFNWXUGETTHEMECOLOR, GetThemeColor)
  155. wxUX_THEME_DECLARE(PFNWXUGETTHEMEMETRIC, GetThemeMetric)
  156. wxUX_THEME_DECLARE(PFNWXUGETTHEMESTRING, GetThemeString)
  157. wxUX_THEME_DECLARE(PFNWXUGETTHEMEBOOL, GetThemeBool)
  158. wxUX_THEME_DECLARE(PFNWXUGETTHEMEINT, GetThemeInt)
  159. wxUX_THEME_DECLARE(PFNWXUGETTHEMEENUMVALUE, GetThemeEnumValue)
  160. wxUX_THEME_DECLARE(PFNWXUGETTHEMEPOSITION, GetThemePosition)
  161. wxUX_THEME_DECLARE(PFNWXUGETTHEMEFONT, GetThemeFont)
  162. wxUX_THEME_DECLARE(PFNWXUGETTHEMERECT, GetThemeRect)
  163. wxUX_THEME_DECLARE(PFNWXUGETTHEMEMARGINS, GetThemeMargins)
  164. wxUX_THEME_DECLARE(PFNWXUGETTHEMEINTLIST, GetThemeIntList)
  165. wxUX_THEME_DECLARE(PFNWXUGETTHEMEPROPERTYORIGIN, GetThemePropertyOrigin)
  166. wxUX_THEME_DECLARE(PFNWXUSETWINDOWTHEME, SetWindowTheme)
  167. wxUX_THEME_DECLARE(PFNWXUGETTHEMEFILENAME, GetThemeFilename)
  168. wxUX_THEME_DECLARE(PFNWXUGETTHEMESYSCOLOR, GetThemeSysColor)
  169. wxUX_THEME_DECLARE(PFNWXUGETTHEMESYSCOLORBRUSH, GetThemeSysColorBrush)
  170. wxUX_THEME_DECLARE(PFNWXUGETTHEMESYSBOOL, GetThemeSysBool)
  171. wxUX_THEME_DECLARE(PFNWXUGETTHEMESYSSIZE, GetThemeSysSize)
  172. wxUX_THEME_DECLARE(PFNWXUGETTHEMESYSFONT, GetThemeSysFont)
  173. wxUX_THEME_DECLARE(PFNWXUGETTHEMESYSSTRING, GetThemeSysString)
  174. wxUX_THEME_DECLARE(PFNWXUGETTHEMESYSINT, GetThemeSysInt)
  175. wxUX_THEME_DECLARE(PFNWXUISTHEMEACTIVE, IsThemeActive)
  176. wxUX_THEME_DECLARE(PFNWXUISAPPTHEMED, IsAppThemed)
  177. wxUX_THEME_DECLARE(PFNWXUGETWINDOWTHEME, GetWindowTheme)
  178. wxUX_THEME_DECLARE(PFNWXUENABLETHEMEDIALOGTEXTURE, EnableThemeDialogTexture)
  179. wxUX_THEME_DECLARE(PFNWXUISTHEMEDIALOGTEXTUREENABLED, IsThemeDialogTextureEnabled)
  180. wxUX_THEME_DECLARE(PFNWXUGETTHEMEAPPPROPERTIES, GetThemeAppProperties)
  181. wxUX_THEME_DECLARE(PFNWXUSETTHEMEAPPPROPERTIES, SetThemeAppProperties)
  182. wxUX_THEME_DECLARE(PFNWXUGETCURRENTTHEMENAME, GetCurrentThemeName)
  183. wxUX_THEME_DECLARE(PFNWXUGETTHEMEDOCUMENTATIONPROPERTY, GetThemeDocumentationProperty)
  184. wxUX_THEME_DECLARE(PFNWXUDRAWTHEMEPARENTBACKGROUND, DrawThemeParentBackground)
  185. wxUX_THEME_DECLARE(PFNWXUENABLETHEMING, EnableTheming)
  186. private:
  187. // construcor is private as only Get() can create us and is also trivial as
  188. // everything really happens in Initialize()
  189. wxUxThemeEngine() { }
  190. // destructor is private as only Get() and wxUxThemeModule delete us, it is
  191. // not virtual as we're not supposed to be derived from
  192. ~wxUxThemeEngine() { }
  193. #if wxUSE_UXTHEME
  194. // initialize the theme engine: load the DLL, resolve the functions
  195. //
  196. // return true if we can be used, false if themes are not available
  197. bool Initialize();
  198. // uxtheme.dll
  199. wxDynamicLibrary m_dllUxTheme;
  200. // the one and only theme engine, initially NULL
  201. static wxUxThemeEngine *ms_themeEngine;
  202. // this is a bool which initially has the value -1 meaning "unknown"
  203. static int ms_isThemeEngineAvailable;
  204. // it must be able to delete us
  205. friend class wxUxThemeModule;
  206. #endif // wxUSE_UXTHEME
  207. wxDECLARE_NO_COPY_CLASS(wxUxThemeEngine);
  208. };
  209. #if wxUSE_UXTHEME
  210. /* static */ inline wxUxThemeEngine *wxUxThemeEngine::GetIfActive()
  211. {
  212. wxUxThemeEngine *engine = Get();
  213. return engine && engine->IsAppThemed() && engine->IsThemeActive()
  214. ? engine
  215. : NULL;
  216. }
  217. #else // !wxUSE_UXTHEME
  218. /* static */ inline wxUxThemeEngine *wxUxThemeEngine::Get()
  219. {
  220. return NULL;
  221. }
  222. /* static */ inline wxUxThemeEngine *wxUxThemeEngine::GetIfActive()
  223. {
  224. return NULL;
  225. }
  226. #endif // wxUSE_UXTHEME/!wxUSE_UXTHEME
  227. // ----------------------------------------------------------------------------
  228. // wxUxThemeHandle: encapsulates ::Open/CloseThemeData()
  229. // ----------------------------------------------------------------------------
  230. class wxUxThemeHandle
  231. {
  232. public:
  233. wxUxThemeHandle(const wxWindow *win, const wchar_t *classes)
  234. {
  235. wxUxThemeEngine *engine = wxUxThemeEngine::Get();
  236. m_hTheme = engine ? (HTHEME)engine->OpenThemeData(GetHwndOf(win), classes)
  237. : NULL;
  238. }
  239. operator HTHEME() const { return m_hTheme; }
  240. ~wxUxThemeHandle()
  241. {
  242. if ( m_hTheme )
  243. {
  244. wxUxThemeEngine::Get()->CloseThemeData(m_hTheme);
  245. }
  246. }
  247. private:
  248. HTHEME m_hTheme;
  249. wxDECLARE_NO_COPY_CLASS(wxUxThemeHandle);
  250. };
  251. #endif // _WX_UXTHEME_H_