1 #include "CUIThemes.h"
2 
3 #if defined(_MSC_VER) && _MSC_VER >= 1400 && defined(_DEBUG)
4 #define new DEBUG_CLIENTBLOCK
5 #endif
6 
7 bool CUIThemes::sm_initTried = false;
8 bool CUIThemes::sm_themeLibLoaded = false;
9 HMODULE CUIThemes::sm_hModThemes = NULL;
10 PFNOPENTHEMEDATA CUIThemes::sm_openThemeData = NULL;
11 PFNCLOSETHEMEDATA CUIThemes::sm_closeThemeData = NULL;
12 PFNDRAWTHEMEBACKGROUND CUIThemes::sm_drawThemeBackground = NULL;
13 PFNDRAWTHEMETEXT CUIThemes::sm_drawThemeText = NULL;
14 PFNGETTHEMEBACKGROUNDCONTENTRECT CUIThemes::sm_getThemeBackgroundContentRect =
15 	NULL;
16 PFNDRAWTHEMEEDGE CUIThemes::sm_drawThemeEdge = NULL;
17 PFNSETWINDOWTHEME CUIThemes::sm_setWindowTheme = NULL;
18 PFNGETWINDOWTHEME CUIThemes::sm_getWindowTheme = NULL;
19 PFNGETTHEMECOLOR CUIThemes::sm_getThemeColor = NULL;
20 PFNSETTHEMEAPPPROPERTIED CUIThemes::sm_setThemeAppProperties = NULL;
21 PFNGETTHEMEAPPPROPERTIED CUIThemes::sm_getThemeAppProperties = NULL;
22 PFNGETTHEMERECT CUIThemes::sm_getThemeRect = NULL;
23 PFNGETTHEMEPOSITION CUIThemes::sm_getThemePosition = NULL;
24 PFNGETTHEMEMARGINS CUIThemes::sm_getThemeMargins = NULL;
25 PFNGETTHEMETEXTEXTENT CUIThemes::sm_getThemeTextExtent = NULL;
26 PFNDDRAWTHEMEPARENTBACKGROUND CUIThemes::sm_drawThemeParentBackground = NULL;
27 PFNGETTHEMEPARTSIZE CUIThemes::sm_getThemePartSize = NULL;
28 PFNENABLETHEMEDIALOGTEXTURE CUIThemes::sm_enableThemeDialogTexture = NULL;
29 PFNGETTHEMESYSCOLOR CUIThemes::sm_getThemeSysColor = NULL;
30 PFNISTHEMEACTIVE CUIThemes::sm_isThemeActive = NULL;
31 
~CUIThemesCleanup(void)32 CUIThemes::CUIThemesCleanup::~CUIThemesCleanup(void)
33 {
34 	CUIThemes::deinit();
35 }
36 
CUIThemes(void)37 CUIThemes::CUIThemes(void)
38 {
39 }
40 
~CUIThemes(void)41 CUIThemes::~CUIThemes(void)
42 {
43 }
44 
init(void)45 void CUIThemes::init(void)
46 {
47 	if (!sm_initTried)
48 	{
49 		sm_initTried = true;
50 		sm_hModThemes = LoadLibrary(_UC("UXTHEME.DLL"));
51 		if (sm_hModThemes)
52 		{
53 			sm_openThemeData =
54 				(PFNOPENTHEMEDATA)GetProcAddress(sm_hModThemes,
55 				"OpenThemeData");
56 			sm_closeThemeData =
57 				(PFNCLOSETHEMEDATA)GetProcAddress(sm_hModThemes,
58 				"CloseThemeData");
59 			sm_drawThemeBackground =
60 				(PFNDRAWTHEMEBACKGROUND)GetProcAddress(sm_hModThemes,
61 				"DrawThemeBackground");
62 			sm_drawThemeText = (PFNDRAWTHEMETEXT)GetProcAddress(sm_hModThemes,
63 				"DrawThemeText");
64 			sm_getThemeBackgroundContentRect =
65 				(PFNGETTHEMEBACKGROUNDCONTENTRECT)GetProcAddress(sm_hModThemes,
66 				"GetThemeBackgroundContentRect");
67 			sm_drawThemeEdge = (PFNDRAWTHEMEEDGE)GetProcAddress(sm_hModThemes,
68 				"DrawThemeEdge");
69 			sm_setWindowTheme = (PFNSETWINDOWTHEME)GetProcAddress(sm_hModThemes,
70 				"SetWindowTheme");
71 			sm_getWindowTheme = (PFNGETWINDOWTHEME)GetProcAddress(sm_hModThemes,
72 				"GetWindowTheme");
73 			sm_getThemeColor = (PFNGETTHEMECOLOR)GetProcAddress(sm_hModThemes,
74 				"GetThemeColor");
75 			sm_setThemeAppProperties =
76 				(PFNSETTHEMEAPPPROPERTIED)GetProcAddress(sm_hModThemes,
77 				"SetThemeAppProperties");
78 			sm_getThemeAppProperties =
79 				(PFNGETTHEMEAPPPROPERTIED)GetProcAddress(sm_hModThemes,
80 				"GetThemeAppProperties");
81 			sm_getThemeRect = (PFNGETTHEMERECT)GetProcAddress(sm_hModThemes,
82 				"GetThemeRect");
83 			sm_getThemePosition =
84 				(PFNGETTHEMEPOSITION)GetProcAddress(sm_hModThemes,
85 				"GetThemePosition");
86 			sm_getThemeMargins =
87 				(PFNGETTHEMEMARGINS)GetProcAddress(sm_hModThemes,
88 				"GetThemeMargins");
89 			sm_getThemeTextExtent =
90 				(PFNGETTHEMETEXTEXTENT)GetProcAddress(sm_hModThemes,
91 				"GetThemeTextExtent");
92 			sm_drawThemeParentBackground =
93 				(PFNDDRAWTHEMEPARENTBACKGROUND)GetProcAddress(sm_hModThemes,
94 				"DrawThemeParentBackground");
95 			sm_getThemePartSize =
96 				(PFNGETTHEMEPARTSIZE)GetProcAddress(sm_hModThemes,
97 				"GetThemePartSize");
98 			sm_enableThemeDialogTexture =
99 				(PFNENABLETHEMEDIALOGTEXTURE)GetProcAddress(sm_hModThemes,
100 				"EnableThemeDialogTexture");
101 			sm_getThemeSysColor =
102 				(PFNGETTHEMESYSCOLOR)GetProcAddress(sm_hModThemes,
103 				"GetThemeSysColor");
104 			sm_isThemeActive =
105 				(PFNISTHEMEACTIVE)GetProcAddress(sm_hModThemes,
106 				"IsThemeActive");
107 
108 
109 			if (sm_openThemeData && sm_closeThemeData && sm_drawThemeBackground
110 				&& sm_drawThemeText && sm_getThemeBackgroundContentRect &&
111 				sm_drawThemeEdge && sm_setWindowTheme && sm_getWindowTheme &&
112 				sm_getThemeColor && sm_setThemeAppProperties &&
113 				sm_getThemeRect && sm_getThemePosition && sm_getThemeMargins &&
114 				sm_getThemeTextExtent && sm_drawThemeParentBackground &&
115 				sm_getThemePartSize && sm_enableThemeDialogTexture &&
116 				sm_getThemeSysColor && sm_isThemeActive &&
117 				sm_getThemeAppProperties)
118 			{
119 				sm_themeLibLoaded = true;
120 			}
121 			else
122 			{
123 				FreeLibrary(sm_hModThemes);
124 				sm_hModThemes = NULL;
125 			}
126 		}
127 	}
128 }
129 
deinit(void)130 void CUIThemes::deinit(void)
131 {
132 	sm_initTried = false;
133 	if (sm_themeLibLoaded)
134 	{
135 		FreeLibrary(sm_hModThemes);
136 		sm_hModThemes = NULL;
137 		sm_themeLibLoaded = false;
138 	}
139 }
140 
openThemeData(HWND hwnd,LPCWSTR pszClassList)141 HTHEME CUIThemes::openThemeData(HWND hwnd, LPCWSTR pszClassList)
142 {
143 	if (sm_themeLibLoaded)
144 	{
145 		return sm_openThemeData(hwnd, pszClassList);
146 	}
147 	else
148 	{
149 		return NULL;
150 	}
151 }
152 
closeThemeData(HTHEME hTheme)153 HRESULT CUIThemes::closeThemeData(HTHEME hTheme)
154 {
155 	if (sm_themeLibLoaded)
156 	{
157 		return sm_closeThemeData(hTheme);
158 	}
159 	else
160 	{
161 		return E_NOTIMPL;
162 	}
163 }
164 
drawThemeBackground(HTHEME hTheme,HDC hdc,int iPartId,int iStateId,const RECT * pRect,const RECT * pClipRect)165 HRESULT CUIThemes::drawThemeBackground(HTHEME hTheme, HDC hdc, int iPartId,
166 	int iStateId, const RECT *pRect,  const RECT *pClipRect)
167 {
168 	if (sm_themeLibLoaded)
169 	{
170 		return sm_drawThemeBackground(hTheme, hdc, iPartId, iStateId, pRect,
171 			pClipRect);
172 	}
173 	else
174 	{
175 		return E_NOTIMPL;
176 	}
177 }
178 
drawThemeText(HTHEME hTheme,HDC hdc,int iPartId,int iStateId,LPCWSTR pszText,int iCharCount,DWORD dwTextFlags,DWORD dwTextFlags2,const RECT * pRect)179 HRESULT CUIThemes::drawThemeText(HTHEME hTheme, HDC hdc, int iPartId,
180 	int iStateId, LPCWSTR pszText, int iCharCount, DWORD dwTextFlags,
181 	DWORD dwTextFlags2, const RECT *pRect)
182 {
183 	if (sm_themeLibLoaded)
184 	{
185 		return sm_drawThemeText(hTheme, hdc, iPartId, iStateId, pszText,
186 			iCharCount, dwTextFlags, dwTextFlags2, pRect);
187 	}
188 	else
189 	{
190 		return E_NOTIMPL;
191 	}
192 }
193 
getThemeBackgroundContentRect(HTHEME hTheme,HDC hdc,int iPartId,int iStateId,const RECT * pBoundingRect,RECT * pContentRect)194 HRESULT CUIThemes::getThemeBackgroundContentRect(HTHEME hTheme, HDC hdc,
195 	int iPartId, int iStateId,  const RECT *pBoundingRect,
196 	RECT *pContentRect)
197 {
198 	if (sm_themeLibLoaded)
199 	{
200 		return sm_getThemeBackgroundContentRect(hTheme, hdc, iPartId, iStateId,
201 			pBoundingRect, pContentRect);
202 	}
203 	else
204 	{
205 		return E_NOTIMPL;
206 	}
207 }
208 
drawThemeEdge(HTHEME hTheme,HDC hdc,int iPartId,int iStateId,const RECT * pDestRect,UINT uEdge,UINT uFlags,RECT * pContentRect)209 HRESULT CUIThemes::drawThemeEdge(HTHEME hTheme, HDC hdc, int iPartId,
210 		int iStateId, const RECT *pDestRect, UINT uEdge, UINT uFlags,
211 		RECT *pContentRect)
212 {
213 	if (sm_themeLibLoaded)
214 	{
215 		return sm_drawThemeEdge(hTheme, hdc, iPartId, iStateId,
216 			pDestRect, uEdge, uFlags, pContentRect);
217 	}
218 	else
219 	{
220 		return E_NOTIMPL;
221 	}
222 }
223 
setWindowTheme(HWND hWnd,LPCWSTR pwszSubAppName,LPCWSTR pwszSubIdList)224 HRESULT CUIThemes::setWindowTheme(HWND hWnd, LPCWSTR pwszSubAppName,
225 								  LPCWSTR pwszSubIdList)
226 {
227 	if (sm_themeLibLoaded)
228 	{
229 		return sm_setWindowTheme(hWnd, pwszSubAppName, pwszSubIdList);
230 	}
231 	else
232 	{
233 		return E_NOTIMPL;
234 	}
235 }
236 
getWindowTheme(HWND hWnd)237 HTHEME CUIThemes::getWindowTheme(HWND hWnd)
238 {
239 	if (sm_themeLibLoaded)
240 	{
241 		return sm_getWindowTheme(hWnd);
242 	}
243 	else
244 	{
245 		return NULL;
246 	}
247 }
248 
getThemeColor(HTHEME hTheme,int iPartId,int iStateId,int iPropId,COLORREF * pColor)249 HRESULT CUIThemes::getThemeColor(HTHEME hTheme, int iPartId, int iStateId,
250 								 int iPropId, COLORREF *pColor)
251 {
252 	if (sm_themeLibLoaded)
253 	{
254 		return sm_getThemeColor(hTheme, iPartId, iStateId, iPropId, pColor);
255 	}
256 	else
257 	{
258 		return E_NOTIMPL;
259 	}
260 }
261 
setThemeAppProperties(DWORD dwFlags)262 void CUIThemes::setThemeAppProperties(DWORD dwFlags)
263 {
264 	if (sm_themeLibLoaded)
265 	{
266 		sm_setThemeAppProperties(dwFlags);
267 	}
268 }
269 
getThemeAppProperties(void)270 DWORD CUIThemes::getThemeAppProperties(void)
271 {
272 	if (sm_themeLibLoaded)
273 	{
274 		return sm_getThemeAppProperties();
275 	}
276 	else
277 	{
278 		return 0;
279 	}
280 }
281 
getThemeRect(HTHEME hTheme,int iPartId,int iStateId,int iPropId,RECT * pRect)282 HRESULT CUIThemes::getThemeRect(HTHEME hTheme, int iPartId, int iStateId,
283 								int iPropId, RECT *pRect)
284 {
285 	if (sm_themeLibLoaded)
286 	{
287 		return sm_getThemeRect(hTheme, iPartId, iStateId, iPropId, pRect);
288 	}
289 	else
290 	{
291 		return E_NOTIMPL;
292 	}
293 }
294 
getThemePosition(HTHEME hTheme,int iPartId,int iStateId,int iPropId,POINT * pPoint)295 HRESULT CUIThemes::getThemePosition(
296 	HTHEME hTheme,
297 	int iPartId,
298 	int iStateId,
299 	int iPropId,
300 	POINT *pPoint)
301 {
302 	if (sm_themeLibLoaded)
303 	{
304 		return sm_getThemePosition(hTheme, iPartId, iStateId, iPropId, pPoint);
305 	}
306 	else
307 	{
308 		return E_NOTIMPL;
309 	}
310 }
311 
getThemeMargins(HTHEME hTheme,HDC hdc,int iPartId,int iStateId,int iPropId,RECT * pRect,MARGINS * pMargins)312 HRESULT CUIThemes::getThemeMargins(
313 	HTHEME hTheme,
314 	HDC hdc,
315 	int iPartId,
316 	int iStateId,
317 	int iPropId,
318 	RECT *pRect,
319 	MARGINS *pMargins)
320 {
321 	if (sm_themeLibLoaded)
322 	{
323 		return sm_getThemeMargins(hTheme, hdc, iPartId, iStateId, iPropId,
324 			pRect, pMargins);
325 	}
326 	else
327 	{
328 		return E_NOTIMPL;
329 	}
330 }
331 
getThemeTextExtent(HTHEME hTheme,HDC hdc,int iPartId,int iStateId,LPCWSTR pszText,int iCharCount,DWORD dwTextFlags,const RECT * pBoundingRect,RECT * pExtentRect)332 HRESULT CUIThemes::getThemeTextExtent(HTHEME hTheme, HDC hdc, int iPartId,
333 									  int iStateId, LPCWSTR pszText,
334 									  int iCharCount, DWORD dwTextFlags,
335 									  const RECT *pBoundingRect,
336 									  RECT *pExtentRect)
337 {
338 	if (sm_themeLibLoaded)
339 	{
340 		return sm_getThemeTextExtent(hTheme, hdc, iPartId, iStateId, pszText,
341 			iCharCount, dwTextFlags, pBoundingRect, pExtentRect);
342 	}
343 	else
344 	{
345 		return E_NOTIMPL;
346 	}
347 }
348 
drawThemeParentBackground(HWND hwnd,HDC hdc,RECT * prc)349 HRESULT CUIThemes::drawThemeParentBackground(HWND hwnd, HDC hdc, RECT* prc)
350 {
351 	if (sm_themeLibLoaded)
352 	{
353 		return sm_drawThemeParentBackground(hwnd, hdc, prc);
354 	}
355 	else
356 	{
357 		return E_NOTIMPL;
358 	}
359 }
360 
getThemePartSize(HTHEME hTheme,HDC hdc,int iPartId,int iStateId,RECT * prc,enum THEMESIZE eSize,SIZE * psz)361 HRESULT CUIThemes::getThemePartSize(HTHEME hTheme, HDC hdc, int iPartId,
362 									int iStateId, RECT *prc,
363 									enum THEMESIZE eSize, SIZE *psz)
364 {
365 	if (sm_themeLibLoaded)
366 	{
367 		return sm_getThemePartSize(hTheme, hdc, iPartId, iStateId, prc, eSize,
368 			psz);
369 	}
370 	else
371 	{
372 		return E_NOTIMPL;
373 	}
374 }
375 
enableThemeDialogTexture(HWND hwnd,DWORD dwFlags)376 HRESULT CUIThemes::enableThemeDialogTexture(HWND hwnd, DWORD dwFlags)
377 {
378 	if (sm_themeLibLoaded)
379 	{
380 		return sm_enableThemeDialogTexture(hwnd, dwFlags);
381 	}
382 	else
383 	{
384 		return E_NOTIMPL;
385 	}
386 }
387 
getThemeSysColor(HTHEME hTheme,int iColorID)388 COLORREF CUIThemes::getThemeSysColor(HTHEME hTheme, int iColorID)
389 {
390 	if (sm_themeLibLoaded)
391 	{
392 		return sm_getThemeSysColor(hTheme, iColorID);
393 	}
394 	else
395 	{
396 		return GetSysColor(iColorID);
397 	}
398 }
399 
isThemeActive(void)400 BOOL CUIThemes::isThemeActive(void)
401 {
402 	if (sm_themeLibLoaded)
403 	{
404 		return sm_isThemeActive();
405 	}
406 	else
407 	{
408 		return FALSE;
409 	}
410 }
411