ws2_32: Avoid using wineserver definitions.
[wine.git] / dlls / uxtheme / system.c
blobafae1e23f2b1b7f1420750ec65e720a0ab3d1c39
1 /*
2 * Win32 5.1 Theme system
4 * Copyright (C) 2003 Kevin Koltzau
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "vfwmsgs.h"
30 #include "uxtheme.h"
31 #include "tmschema.h"
33 #include "uxthemedll.h"
34 #include "msstyles.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);
40 /***********************************************************************
41 * Defines and global variables
44 static const WCHAR szThemeManager[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\ThemeManager";
46 DECLSPEC_HIDDEN ATOM atDialogThemeEnabled;
48 static DWORD dwThemeAppProperties = STAP_ALLOW_NONCLIENT | STAP_ALLOW_CONTROLS;
49 static ATOM atWindowTheme;
50 static ATOM atSubAppName;
51 static ATOM atSubIdList;
53 static BOOL bThemeActive = FALSE;
54 static WCHAR szCurrentTheme[MAX_PATH];
55 static WCHAR szCurrentColor[64];
56 static WCHAR szCurrentSize[64];
58 struct user_api_hook user_api = {0};
60 /***********************************************************************/
62 static BOOL CALLBACK UXTHEME_broadcast_msg_enumchild (HWND hWnd, LPARAM msg)
64 PostMessageW(hWnd, msg, 0, 0);
65 return TRUE;
68 /* Broadcast a message to *all* windows, including children */
69 static BOOL CALLBACK UXTHEME_broadcast_msg (HWND hWnd, LPARAM msg)
71 if (hWnd == NULL)
73 EnumWindows (UXTHEME_broadcast_msg, msg);
75 else
77 PostMessageW(hWnd, msg, 0, 0);
78 EnumChildWindows (hWnd, UXTHEME_broadcast_msg_enumchild, msg);
80 return TRUE;
83 /* At the end of the day this is a subset of what SHRegGetPath() does - copied
84 * here to avoid linking against shlwapi. */
85 static DWORD query_reg_path (HKEY hKey, LPCWSTR lpszValue,
86 LPVOID pvData)
88 DWORD dwRet, dwType, dwUnExpDataLen = MAX_PATH, dwExpDataLen;
90 TRACE("(hkey=%p,%s,%p)\n", hKey, debugstr_w(lpszValue),
91 pvData);
93 dwRet = RegQueryValueExW(hKey, lpszValue, 0, &dwType, pvData, &dwUnExpDataLen);
94 if (dwRet!=ERROR_SUCCESS && dwRet!=ERROR_MORE_DATA)
95 return dwRet;
97 if (dwType == REG_EXPAND_SZ)
99 DWORD nBytesToAlloc;
101 /* Expand type REG_EXPAND_SZ into REG_SZ */
102 LPWSTR szData;
104 /* If the caller didn't supply a buffer or the buffer is too small we have
105 * to allocate our own
107 if (dwRet == ERROR_MORE_DATA)
109 WCHAR emptyW[] = L"";
110 nBytesToAlloc = dwUnExpDataLen;
112 szData = LocalAlloc(LMEM_ZEROINIT, nBytesToAlloc);
113 RegQueryValueExW (hKey, lpszValue, 0, NULL, (LPBYTE)szData, &nBytesToAlloc);
114 dwExpDataLen = ExpandEnvironmentStringsW(szData, emptyW, 1);
115 dwUnExpDataLen = max(nBytesToAlloc, dwExpDataLen);
116 LocalFree(szData);
118 else
120 nBytesToAlloc = (lstrlenW(pvData) + 1) * sizeof(WCHAR);
121 szData = LocalAlloc(LMEM_ZEROINIT, nBytesToAlloc );
122 lstrcpyW(szData, pvData);
123 dwExpDataLen = ExpandEnvironmentStringsW(szData, pvData, MAX_PATH );
124 if (dwExpDataLen > MAX_PATH) dwRet = ERROR_MORE_DATA;
125 dwUnExpDataLen = max(nBytesToAlloc, dwExpDataLen);
126 LocalFree(szData);
130 RegCloseKey(hKey);
131 return dwRet;
134 /***********************************************************************
135 * UXTHEME_LoadTheme
137 * Set the current active theme from the registry
139 static void UXTHEME_LoadTheme(void)
141 HKEY hKey;
142 DWORD buffsize;
143 HRESULT hr;
144 WCHAR tmp[10];
145 PTHEME_FILE pt;
147 /* Get current theme configuration */
148 if(!RegOpenKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
149 TRACE("Loading theme config\n");
150 buffsize = ARRAY_SIZE(tmp);
151 if (!RegQueryValueExW(hKey, L"ThemeActive", NULL, NULL, (BYTE*)tmp, &buffsize)) {
152 bThemeActive = (tmp[0] != '0');
154 else {
155 bThemeActive = FALSE;
156 TRACE("Failed to get ThemeActive: %d\n", GetLastError());
158 buffsize = ARRAY_SIZE(szCurrentColor);
159 if (RegQueryValueExW(hKey, L"ColorName", NULL, NULL, (BYTE*)szCurrentColor, &buffsize))
160 szCurrentColor[0] = '\0';
161 buffsize = ARRAY_SIZE(szCurrentSize);
162 if (RegQueryValueExW(hKey, L"SizeName", NULL, NULL, (BYTE*)szCurrentSize, &buffsize))
163 szCurrentSize[0] = '\0';
164 if (query_reg_path (hKey, L"DllName", szCurrentTheme))
165 szCurrentTheme[0] = '\0';
166 RegCloseKey(hKey);
168 else
169 TRACE("Failed to open theme registry key\n");
171 if(bThemeActive) {
172 /* Make sure the theme requested is actually valid */
173 hr = MSSTYLES_OpenThemeFile(szCurrentTheme,
174 szCurrentColor[0]?szCurrentColor:NULL,
175 szCurrentSize[0]?szCurrentSize:NULL,
176 &pt);
177 if(FAILED(hr)) {
178 bThemeActive = FALSE;
179 szCurrentTheme[0] = '\0';
180 szCurrentColor[0] = '\0';
181 szCurrentSize[0] = '\0';
183 else {
184 /* Make sure the global color & size match the theme */
185 lstrcpynW(szCurrentColor, pt->pszSelectedColor, ARRAY_SIZE(szCurrentColor));
186 lstrcpynW(szCurrentSize, pt->pszSelectedSize, ARRAY_SIZE(szCurrentSize));
188 MSSTYLES_SetActiveTheme(pt, FALSE);
189 TRACE("Theme active: %s %s %s\n", debugstr_w(szCurrentTheme),
190 debugstr_w(szCurrentColor), debugstr_w(szCurrentSize));
191 MSSTYLES_CloseThemeFile(pt);
194 if(!bThemeActive) {
195 MSSTYLES_SetActiveTheme(NULL, FALSE);
196 TRACE("Theming not active\n");
200 /***********************************************************************/
202 static const char * const SysColorsNames[] =
204 "Scrollbar", /* COLOR_SCROLLBAR */
205 "Background", /* COLOR_BACKGROUND */
206 "ActiveTitle", /* COLOR_ACTIVECAPTION */
207 "InactiveTitle", /* COLOR_INACTIVECAPTION */
208 "Menu", /* COLOR_MENU */
209 "Window", /* COLOR_WINDOW */
210 "WindowFrame", /* COLOR_WINDOWFRAME */
211 "MenuText", /* COLOR_MENUTEXT */
212 "WindowText", /* COLOR_WINDOWTEXT */
213 "TitleText", /* COLOR_CAPTIONTEXT */
214 "ActiveBorder", /* COLOR_ACTIVEBORDER */
215 "InactiveBorder", /* COLOR_INACTIVEBORDER */
216 "AppWorkSpace", /* COLOR_APPWORKSPACE */
217 "Hilight", /* COLOR_HIGHLIGHT */
218 "HilightText", /* COLOR_HIGHLIGHTTEXT */
219 "ButtonFace", /* COLOR_BTNFACE */
220 "ButtonShadow", /* COLOR_BTNSHADOW */
221 "GrayText", /* COLOR_GRAYTEXT */
222 "ButtonText", /* COLOR_BTNTEXT */
223 "InactiveTitleText", /* COLOR_INACTIVECAPTIONTEXT */
224 "ButtonHilight", /* COLOR_BTNHIGHLIGHT */
225 "ButtonDkShadow", /* COLOR_3DDKSHADOW */
226 "ButtonLight", /* COLOR_3DLIGHT */
227 "InfoText", /* COLOR_INFOTEXT */
228 "InfoWindow", /* COLOR_INFOBK */
229 "ButtonAlternateFace", /* COLOR_ALTERNATEBTNFACE */
230 "HotTrackingColor", /* COLOR_HOTLIGHT */
231 "GradientActiveTitle", /* COLOR_GRADIENTACTIVECAPTION */
232 "GradientInactiveTitle", /* COLOR_GRADIENTINACTIVECAPTION */
233 "MenuHilight", /* COLOR_MENUHILIGHT */
234 "MenuBar", /* COLOR_MENUBAR */
237 static const WCHAR strColorKey[] = L"Control Panel\\Colors";
239 static const struct BackupSysParam
241 int spiGet, spiSet;
242 const WCHAR* keyName;
243 } backupSysParams[] =
245 {SPI_GETFLATMENU, SPI_SETFLATMENU, L"FlatMenu"},
246 {SPI_GETGRADIENTCAPTIONS, SPI_SETGRADIENTCAPTIONS, L"GradientCaption"},
247 {-1, -1, 0}
250 #define NUM_SYS_COLORS (COLOR_MENUBAR+1)
252 static void save_sys_colors (HKEY baseKey)
254 char colorStr[13];
255 HKEY hKey;
256 int i;
258 if (RegCreateKeyExW( baseKey, strColorKey,
259 0, 0, 0, KEY_ALL_ACCESS,
260 0, &hKey, 0 ) == ERROR_SUCCESS)
262 for (i = 0; i < NUM_SYS_COLORS; i++)
264 COLORREF col = GetSysColor (i);
266 sprintf (colorStr, "%d %d %d",
267 GetRValue (col), GetGValue (col), GetBValue (col));
269 RegSetValueExA (hKey, SysColorsNames[i], 0, REG_SZ,
270 (BYTE*)colorStr, strlen (colorStr)+1);
272 RegCloseKey (hKey);
276 /* Before activating a theme, query current system colors, certain settings
277 * and backup them in the registry, so they can be restored when the theme
278 * is deactivated */
279 static void UXTHEME_BackupSystemMetrics(void)
281 HKEY hKey;
282 const struct BackupSysParam* bsp = backupSysParams;
284 if (RegCreateKeyExW( HKEY_CURRENT_USER, szThemeManager,
285 0, 0, 0, KEY_ALL_ACCESS,
286 0, &hKey, 0) == ERROR_SUCCESS)
288 NONCLIENTMETRICSW ncm;
289 LOGFONTW iconTitleFont;
291 /* back up colors */
292 save_sys_colors (hKey);
294 /* back up "other" settings */
295 while (bsp->spiGet >= 0)
297 DWORD value;
299 SystemParametersInfoW (bsp->spiGet, 0, &value, 0);
300 RegSetValueExW (hKey, bsp->keyName, 0, REG_DWORD,
301 (LPBYTE)&value, sizeof (value));
303 bsp++;
306 /* back up non-client metrics */
307 memset (&ncm, 0, sizeof (ncm));
308 ncm.cbSize = sizeof (ncm);
309 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof (ncm), &ncm, 0);
310 RegSetValueExW (hKey, L"NonClientMetrics", 0, REG_BINARY, (BYTE*)&ncm,
311 sizeof (ncm));
312 memset (&iconTitleFont, 0, sizeof (iconTitleFont));
313 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, sizeof (iconTitleFont),
314 &iconTitleFont, 0);
315 RegSetValueExW (hKey, L"IconTitleFont", 0, REG_BINARY,
316 (LPBYTE)&iconTitleFont, sizeof (iconTitleFont));
318 RegCloseKey (hKey);
322 /* Read back old settings after a theme was deactivated */
323 static void UXTHEME_RestoreSystemMetrics(void)
325 HKEY hKey;
326 const struct BackupSysParam* bsp = backupSysParams;
328 if (RegOpenKeyExW (HKEY_CURRENT_USER, szThemeManager,
329 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
331 HKEY colorKey;
333 /* read backed-up colors */
334 if (RegOpenKeyExW (hKey, strColorKey,
335 0, KEY_QUERY_VALUE, &colorKey) == ERROR_SUCCESS)
337 int i;
338 COLORREF sysCols[NUM_SYS_COLORS];
339 int sysColsIndices[NUM_SYS_COLORS];
340 int sysColCount = 0;
342 for (i = 0; i < NUM_SYS_COLORS; i++)
344 DWORD type;
345 char colorStr[13];
346 DWORD count = sizeof(colorStr);
348 if (RegQueryValueExA (colorKey, SysColorsNames[i], 0,
349 &type, (LPBYTE) colorStr, &count) == ERROR_SUCCESS)
351 int r, g, b;
352 if (sscanf (colorStr, "%d %d %d", &r, &g, &b) == 3)
354 sysColsIndices[sysColCount] = i;
355 sysCols[sysColCount] = RGB(r, g, b);
356 sysColCount++;
360 RegCloseKey (colorKey);
362 SetSysColors (sysColCount, sysColsIndices, sysCols);
365 /* read backed-up other settings */
366 while (bsp->spiGet >= 0)
368 DWORD value;
369 DWORD count = sizeof(value);
370 DWORD type;
372 if (RegQueryValueExW (hKey, bsp->keyName, 0,
373 &type, (LPBYTE)&value, &count) == ERROR_SUCCESS)
375 SystemParametersInfoW (bsp->spiSet, 0, UlongToPtr(value), SPIF_UPDATEINIFILE);
378 bsp++;
381 /* read backed-up non-client metrics */
383 NONCLIENTMETRICSW ncm;
384 LOGFONTW iconTitleFont;
385 DWORD count = sizeof(ncm);
386 DWORD type;
388 if (RegQueryValueExW (hKey, L"NonClientMetrics", 0,
389 &type, (LPBYTE)&ncm, &count) == ERROR_SUCCESS)
391 SystemParametersInfoW (SPI_SETNONCLIENTMETRICS,
392 count, &ncm, SPIF_UPDATEINIFILE);
395 count = sizeof(iconTitleFont);
397 if (RegQueryValueExW (hKey, L"IconTitleFont", 0,
398 &type, (LPBYTE)&iconTitleFont, &count) == ERROR_SUCCESS)
400 SystemParametersInfoW (SPI_SETICONTITLELOGFONT,
401 count, &iconTitleFont, SPIF_UPDATEINIFILE);
405 RegCloseKey (hKey);
409 /* Make system settings persistent, so they're in effect even w/o uxtheme
410 * loaded.
411 * For efficiency reasons, only the last SystemParametersInfoW sets
412 * SPIF_SENDWININICHANGE */
413 static void UXTHEME_SaveSystemMetrics(void)
415 const struct BackupSysParam* bsp = backupSysParams;
416 NONCLIENTMETRICSW ncm;
417 LOGFONTW iconTitleFont;
419 save_sys_colors (HKEY_CURRENT_USER);
421 while (bsp->spiGet >= 0)
423 DWORD value;
425 SystemParametersInfoW (bsp->spiGet, 0, &value, 0);
426 SystemParametersInfoW (bsp->spiSet, 0, UlongToPtr(value), SPIF_UPDATEINIFILE);
427 bsp++;
430 memset (&ncm, 0, sizeof (ncm));
431 ncm.cbSize = sizeof (ncm);
432 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof (ncm), &ncm, 0);
433 SystemParametersInfoW (SPI_SETNONCLIENTMETRICS, sizeof (ncm), &ncm,
434 SPIF_UPDATEINIFILE);
436 memset (&iconTitleFont, 0, sizeof (iconTitleFont));
437 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, sizeof (iconTitleFont),
438 &iconTitleFont, 0);
439 SystemParametersInfoW (SPI_SETICONTITLELOGFONT, sizeof (iconTitleFont),
440 &iconTitleFont, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
443 /***********************************************************************
444 * UXTHEME_SetActiveTheme
446 * Change the current active theme
448 static HRESULT UXTHEME_SetActiveTheme(PTHEME_FILE tf)
450 HKEY hKey;
451 WCHAR tmp[2];
452 HRESULT hr;
454 if(tf && !bThemeActive) UXTHEME_BackupSystemMetrics();
455 hr = MSSTYLES_SetActiveTheme(tf, TRUE);
456 if(FAILED(hr))
457 return hr;
458 if(tf) {
459 bThemeActive = TRUE;
460 lstrcpynW(szCurrentTheme, tf->szThemeFile, ARRAY_SIZE(szCurrentTheme));
461 lstrcpynW(szCurrentColor, tf->pszSelectedColor, ARRAY_SIZE(szCurrentColor));
462 lstrcpynW(szCurrentSize, tf->pszSelectedSize, ARRAY_SIZE(szCurrentSize));
464 else {
465 UXTHEME_RestoreSystemMetrics();
466 bThemeActive = FALSE;
467 szCurrentTheme[0] = '\0';
468 szCurrentColor[0] = '\0';
469 szCurrentSize[0] = '\0';
472 TRACE("Writing theme config to registry\n");
473 if(!RegCreateKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
474 tmp[0] = bThemeActive?'1':'0';
475 tmp[1] = '\0';
476 RegSetValueExW(hKey, L"ThemeActive", 0, REG_SZ, (const BYTE*)tmp, sizeof(WCHAR)*2);
477 if(bThemeActive) {
478 RegSetValueExW(hKey, L"ColorName", 0, REG_SZ, (const BYTE*)szCurrentColor,
479 (lstrlenW(szCurrentColor)+1)*sizeof(WCHAR));
480 RegSetValueExW(hKey, L"SizeName", 0, REG_SZ, (const BYTE*)szCurrentSize,
481 (lstrlenW(szCurrentSize)+1)*sizeof(WCHAR));
482 RegSetValueExW(hKey, L"DllName", 0, REG_SZ, (const BYTE*)szCurrentTheme,
483 (lstrlenW(szCurrentTheme)+1)*sizeof(WCHAR));
485 else {
486 RegDeleteValueW(hKey, L"ColorName");
487 RegDeleteValueW(hKey, L"SizeName");
488 RegDeleteValueW(hKey, L"DllName");
491 RegCloseKey(hKey);
493 else
494 TRACE("Failed to open theme registry key\n");
496 UXTHEME_SaveSystemMetrics ();
498 return hr;
501 /***********************************************************************
502 * UXTHEME_InitSystem
504 void UXTHEME_InitSystem(HINSTANCE hInst)
506 atWindowTheme = GlobalAddAtomW(L"ux_theme");
507 atSubAppName = GlobalAddAtomW(L"ux_subapp");
508 atSubIdList = GlobalAddAtomW(L"ux_subidlst");
509 atDialogThemeEnabled = GlobalAddAtomW(L"ux_dialogtheme");
511 UXTHEME_LoadTheme();
512 ThemeHooksInstall();
515 void UXTHEME_UninitSystem(void)
517 ThemeHooksRemove();
518 MSSTYLES_SetActiveTheme(NULL, FALSE);
520 GlobalDeleteAtom(atWindowTheme);
521 GlobalDeleteAtom(atSubAppName);
522 GlobalDeleteAtom(atSubIdList);
523 GlobalDeleteAtom(atDialogThemeEnabled);
526 /***********************************************************************
527 * IsAppThemed (UXTHEME.@)
529 BOOL WINAPI IsAppThemed(void)
531 return IsThemeActive();
534 /***********************************************************************
535 * IsThemeActive (UXTHEME.@)
537 BOOL WINAPI IsThemeActive(void)
539 TRACE("\n");
540 SetLastError(ERROR_SUCCESS);
541 return bThemeActive;
544 /************************************************************
545 * IsCompositionActive (UXTHEME.@)
547 BOOL WINAPI IsCompositionActive(void)
549 FIXME(": stub\n");
551 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
553 return FALSE;
556 /***********************************************************************
557 * EnableTheming (UXTHEME.@)
559 * NOTES
560 * This is a global and persistent change
562 HRESULT WINAPI EnableTheming(BOOL fEnable)
564 HKEY hKey;
565 WCHAR szEnabled[] = L"0";
567 TRACE("(%d)\n", fEnable);
569 if(fEnable != bThemeActive) {
570 if(fEnable)
571 UXTHEME_BackupSystemMetrics();
572 else
573 UXTHEME_RestoreSystemMetrics();
574 UXTHEME_SaveSystemMetrics ();
575 bThemeActive = fEnable;
576 if(bThemeActive) szEnabled[0] = '1';
577 if(!RegOpenKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
578 RegSetValueExW(hKey, L"ThemeActive", 0, REG_SZ, (BYTE*)szEnabled, sizeof(WCHAR));
579 RegCloseKey(hKey);
581 UXTHEME_broadcast_msg (NULL, WM_THEMECHANGED);
583 return S_OK;
586 /***********************************************************************
587 * UXTHEME_SetWindowProperty
589 * I'm using atoms as there may be large numbers of duplicated strings
590 * and they do the work of keeping memory down as a cause of that quite nicely
592 static HRESULT UXTHEME_SetWindowProperty(HWND hwnd, ATOM aProp, LPCWSTR pszValue)
594 ATOM oldValue = (ATOM)(size_t)RemovePropW(hwnd, (LPCWSTR)MAKEINTATOM(aProp));
595 if(oldValue)
596 DeleteAtom(oldValue);
597 if(pszValue) {
598 ATOM atValue = AddAtomW(pszValue);
599 if(!atValue
600 || !SetPropW(hwnd, (LPCWSTR)MAKEINTATOM(aProp), (LPWSTR)MAKEINTATOM(atValue))) {
601 HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
602 if(atValue) DeleteAtom(atValue);
603 return hr;
606 return S_OK;
609 static LPWSTR UXTHEME_GetWindowProperty(HWND hwnd, ATOM aProp, LPWSTR pszBuffer, int dwLen)
611 ATOM atValue = (ATOM)(size_t)GetPropW(hwnd, (LPCWSTR)MAKEINTATOM(aProp));
612 if(atValue) {
613 if(GetAtomNameW(atValue, pszBuffer, dwLen))
614 return pszBuffer;
615 TRACE("property defined, but unable to get value\n");
617 return NULL;
620 /***********************************************************************
621 * OpenThemeDataEx (UXTHEME.61)
623 HTHEME WINAPI OpenThemeDataEx(HWND hwnd, LPCWSTR pszClassList, DWORD flags)
625 WCHAR szAppBuff[256];
626 WCHAR szClassBuff[256];
627 LPCWSTR pszAppName;
628 LPCWSTR pszUseClassList;
629 HTHEME hTheme = NULL;
630 TRACE("(%p,%s, %x)\n", hwnd, debugstr_w(pszClassList), flags);
632 if(!pszClassList)
634 SetLastError(E_POINTER);
635 return NULL;
638 if(flags)
639 FIXME("unhandled flags: %x\n", flags);
641 if(bThemeActive)
643 pszAppName = UXTHEME_GetWindowProperty(hwnd, atSubAppName, szAppBuff, ARRAY_SIZE(szAppBuff));
644 /* If SetWindowTheme was used on the window, that overrides the class list passed to this function */
645 pszUseClassList = UXTHEME_GetWindowProperty(hwnd, atSubIdList, szClassBuff, ARRAY_SIZE(szClassBuff));
646 if(!pszUseClassList)
647 pszUseClassList = pszClassList;
649 if (pszUseClassList)
650 hTheme = MSSTYLES_OpenThemeClass(pszAppName, pszUseClassList);
652 /* Fall back to default class if the specified subclass is not found */
653 if (!hTheme)
654 hTheme = MSSTYLES_OpenThemeClass(NULL, pszUseClassList);
656 if(IsWindow(hwnd))
657 SetPropW(hwnd, (LPCWSTR)MAKEINTATOM(atWindowTheme), hTheme);
658 TRACE(" = %p\n", hTheme);
660 SetLastError(hTheme ? ERROR_SUCCESS : E_PROP_ID_UNSUPPORTED);
661 return hTheme;
664 /***********************************************************************
665 * OpenThemeData (UXTHEME.@)
667 HTHEME WINAPI OpenThemeData(HWND hwnd, LPCWSTR classlist)
669 return OpenThemeDataEx(hwnd, classlist, 0);
672 /***********************************************************************
673 * GetWindowTheme (UXTHEME.@)
675 * Retrieve the last theme opened for a window.
677 * PARAMS
678 * hwnd [I] window to retrieve the theme for
680 * RETURNS
681 * The most recent theme.
683 HTHEME WINAPI GetWindowTheme(HWND hwnd)
685 TRACE("(%p)\n", hwnd);
687 if (!hwnd)
689 SetLastError(E_HANDLE);
690 return NULL;
693 return GetPropW(hwnd, (LPCWSTR)MAKEINTATOM(atWindowTheme));
696 /***********************************************************************
697 * SetWindowTheme (UXTHEME.@)
699 * Persistent through the life of the window, even after themes change
701 HRESULT WINAPI SetWindowTheme(HWND hwnd, LPCWSTR pszSubAppName,
702 LPCWSTR pszSubIdList)
704 HRESULT hr;
705 TRACE("(%p,%s,%s)\n", hwnd, debugstr_w(pszSubAppName),
706 debugstr_w(pszSubIdList));
708 if (!hwnd)
709 return E_HANDLE;
711 hr = UXTHEME_SetWindowProperty(hwnd, atSubAppName, pszSubAppName);
712 if(SUCCEEDED(hr))
713 hr = UXTHEME_SetWindowProperty(hwnd, atSubIdList, pszSubIdList);
714 if(SUCCEEDED(hr))
715 SendMessageW(hwnd, WM_THEMECHANGED, 0, 0);
716 return hr;
719 /***********************************************************************
720 * SetWindowThemeAttribute (UXTHEME.@)
722 HRESULT WINAPI SetWindowThemeAttribute(HWND hwnd, enum WINDOWTHEMEATTRIBUTETYPE type,
723 PVOID attribute, DWORD size)
725 FIXME("(%p,%d,%p,%d): stub\n", hwnd, type, attribute, size);
726 return E_NOTIMPL;
729 /***********************************************************************
730 * GetCurrentThemeName (UXTHEME.@)
732 HRESULT WINAPI GetCurrentThemeName(LPWSTR pszThemeFileName, int dwMaxNameChars,
733 LPWSTR pszColorBuff, int cchMaxColorChars,
734 LPWSTR pszSizeBuff, int cchMaxSizeChars)
736 if(!bThemeActive)
737 return E_PROP_ID_UNSUPPORTED;
738 if(pszThemeFileName) lstrcpynW(pszThemeFileName, szCurrentTheme, dwMaxNameChars);
739 if(pszColorBuff) lstrcpynW(pszColorBuff, szCurrentColor, cchMaxColorChars);
740 if(pszSizeBuff) lstrcpynW(pszSizeBuff, szCurrentSize, cchMaxSizeChars);
741 return S_OK;
744 /***********************************************************************
745 * GetThemeAppProperties (UXTHEME.@)
747 DWORD WINAPI GetThemeAppProperties(void)
749 return dwThemeAppProperties;
752 /***********************************************************************
753 * SetThemeAppProperties (UXTHEME.@)
755 void WINAPI SetThemeAppProperties(DWORD dwFlags)
757 TRACE("(0x%08x)\n", dwFlags);
758 dwThemeAppProperties = dwFlags;
761 /***********************************************************************
762 * CloseThemeData (UXTHEME.@)
764 HRESULT WINAPI CloseThemeData(HTHEME hTheme)
766 TRACE("(%p)\n", hTheme);
767 if(!hTheme || hTheme == INVALID_HANDLE_VALUE)
768 return E_HANDLE;
769 return MSSTYLES_CloseThemeClass(hTheme);
772 /***********************************************************************
773 * HitTestThemeBackground (UXTHEME.@)
775 HRESULT WINAPI HitTestThemeBackground(HTHEME hTheme, HDC hdc, int iPartId,
776 int iStateId, DWORD dwOptions,
777 const RECT *pRect, HRGN hrgn,
778 POINT ptTest, WORD *pwHitTestCode)
780 FIXME("%d %d 0x%08x: stub\n", iPartId, iStateId, dwOptions);
781 if(!hTheme)
782 return E_HANDLE;
783 return E_NOTIMPL;
786 /***********************************************************************
787 * IsThemePartDefined (UXTHEME.@)
789 BOOL WINAPI IsThemePartDefined(HTHEME hTheme, int iPartId, int iStateId)
791 TRACE("(%p,%d,%d)\n", hTheme, iPartId, iStateId);
792 if(!hTheme) {
793 SetLastError(E_HANDLE);
794 return FALSE;
796 if(MSSTYLES_FindPartState(hTheme, iPartId, iStateId, NULL))
797 return TRUE;
798 return FALSE;
801 /***********************************************************************
802 * GetThemeDocumentationProperty (UXTHEME.@)
804 * Try and retrieve the documentation property from string resources
805 * if that fails, get it from the [documentation] section of themes.ini
807 HRESULT WINAPI GetThemeDocumentationProperty(LPCWSTR pszThemeName,
808 LPCWSTR pszPropertyName,
809 LPWSTR pszValueBuff,
810 int cchMaxValChars)
812 static const WORD wDocToRes[] = {
813 TMT_DISPLAYNAME,5000,
814 TMT_TOOLTIP,5001,
815 TMT_COMPANY,5002,
816 TMT_AUTHOR,5003,
817 TMT_COPYRIGHT,5004,
818 TMT_URL,5005,
819 TMT_VERSION,5006,
820 TMT_DESCRIPTION,5007
823 PTHEME_FILE pt;
824 HRESULT hr;
825 unsigned int i;
826 int iDocId;
827 TRACE("(%s,%s,%p,%d)\n", debugstr_w(pszThemeName), debugstr_w(pszPropertyName),
828 pszValueBuff, cchMaxValChars);
830 hr = MSSTYLES_OpenThemeFile(pszThemeName, NULL, NULL, &pt);
831 if(FAILED(hr)) return hr;
833 /* Try to load from string resources */
834 hr = E_PROP_ID_UNSUPPORTED;
835 if(MSSTYLES_LookupProperty(pszPropertyName, NULL, &iDocId)) {
836 for(i=0; i<ARRAY_SIZE(wDocToRes); i+=2) {
837 if(wDocToRes[i] == iDocId) {
838 if(LoadStringW(pt->hTheme, wDocToRes[i+1], pszValueBuff, cchMaxValChars)) {
839 hr = S_OK;
840 break;
845 /* If loading from string resource failed, try getting it from the theme.ini */
846 if(FAILED(hr)) {
847 PUXINI_FILE uf = MSSTYLES_GetThemeIni(pt);
848 if(UXINI_FindSection(uf, L"documentation")) {
849 LPCWSTR lpValue;
850 DWORD dwLen;
851 if(UXINI_FindValue(uf, pszPropertyName, &lpValue, &dwLen)) {
852 lstrcpynW(pszValueBuff, lpValue, min(dwLen+1,cchMaxValChars));
853 hr = S_OK;
856 UXINI_CloseINI(uf);
859 MSSTYLES_CloseThemeFile(pt);
860 return hr;
863 /**********************************************************************
864 * Undocumented functions
867 /**********************************************************************
868 * QueryThemeServices (UXTHEME.1)
870 * RETURNS
871 * some kind of status flag
873 DWORD WINAPI QueryThemeServices(void)
875 FIXME("stub\n");
876 return 3; /* This is what is returned under XP in most cases */
880 /**********************************************************************
881 * OpenThemeFile (UXTHEME.2)
883 * Opens a theme file, which can be used to change the current theme, etc
885 * PARAMS
886 * pszThemeFileName Path to a msstyles theme file
887 * pszColorName Color defined in the theme, eg. NormalColor
888 * pszSizeName Size defined in the theme, eg. NormalSize
889 * hThemeFile Handle to theme file
891 * RETURNS
892 * Success: S_OK
893 * Failure: HRESULT error-code
895 HRESULT WINAPI OpenThemeFile(LPCWSTR pszThemeFileName, LPCWSTR pszColorName,
896 LPCWSTR pszSizeName, HTHEMEFILE *hThemeFile,
897 DWORD unknown)
899 TRACE("(%s,%s,%s,%p,%d)\n", debugstr_w(pszThemeFileName),
900 debugstr_w(pszColorName), debugstr_w(pszSizeName),
901 hThemeFile, unknown);
902 return MSSTYLES_OpenThemeFile(pszThemeFileName, pszColorName, pszSizeName, (PTHEME_FILE*)hThemeFile);
905 /**********************************************************************
906 * CloseThemeFile (UXTHEME.3)
908 * Releases theme file handle returned by OpenThemeFile
910 * PARAMS
911 * hThemeFile Handle to theme file
913 * RETURNS
914 * Success: S_OK
915 * Failure: HRESULT error-code
917 HRESULT WINAPI CloseThemeFile(HTHEMEFILE hThemeFile)
919 TRACE("(%p)\n", hThemeFile);
920 MSSTYLES_CloseThemeFile(hThemeFile);
921 return S_OK;
924 /**********************************************************************
925 * ApplyTheme (UXTHEME.4)
927 * Set a theme file to be the currently active theme
929 * PARAMS
930 * hThemeFile Handle to theme file
931 * unknown See notes
932 * hWnd Window requesting the theme change
934 * RETURNS
935 * Success: S_OK
936 * Failure: HRESULT error-code
938 * NOTES
939 * I'm not sure what the second parameter is (the datatype is likely wrong), other then this:
940 * Under XP if I pass
941 * char b[] = "";
942 * the theme is applied with the screen redrawing really badly (flickers)
943 * char b[] = "\0"; where \0 can be one or more of any character, makes no difference
944 * the theme is applied smoothly (screen does not flicker)
945 * char *b = "\0" or NULL; where \0 can be zero or more of any character, makes no difference
946 * the function fails returning invalid parameter... very strange
948 HRESULT WINAPI ApplyTheme(HTHEMEFILE hThemeFile, char *unknown, HWND hWnd)
950 HRESULT hr;
951 TRACE("(%p,%s,%p)\n", hThemeFile, unknown, hWnd);
952 hr = UXTHEME_SetActiveTheme(hThemeFile);
953 UXTHEME_broadcast_msg (NULL, WM_THEMECHANGED);
954 return hr;
957 /**********************************************************************
958 * GetThemeDefaults (UXTHEME.7)
960 * Get the default color & size for a theme
962 * PARAMS
963 * pszThemeFileName Path to a msstyles theme file
964 * pszColorName Buffer to receive the default color name
965 * dwColorNameLen Length, in characters, of color name buffer
966 * pszSizeName Buffer to receive the default size name
967 * dwSizeNameLen Length, in characters, of size name buffer
969 * RETURNS
970 * Success: S_OK
971 * Failure: HRESULT error-code
973 HRESULT WINAPI GetThemeDefaults(LPCWSTR pszThemeFileName, LPWSTR pszColorName,
974 DWORD dwColorNameLen, LPWSTR pszSizeName,
975 DWORD dwSizeNameLen)
977 PTHEME_FILE pt;
978 HRESULT hr;
979 TRACE("(%s,%p,%d,%p,%d)\n", debugstr_w(pszThemeFileName),
980 pszColorName, dwColorNameLen,
981 pszSizeName, dwSizeNameLen);
983 hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, NULL, &pt);
984 if(FAILED(hr)) return hr;
986 lstrcpynW(pszColorName, pt->pszSelectedColor, dwColorNameLen);
987 lstrcpynW(pszSizeName, pt->pszSelectedSize, dwSizeNameLen);
989 MSSTYLES_CloseThemeFile(pt);
990 return S_OK;
993 /**********************************************************************
994 * EnumThemes (UXTHEME.8)
996 * Enumerate available themes, calls specified EnumThemeProc for each
997 * theme found. Passes lpData through to callback function.
999 * PARAMS
1000 * pszThemePath Path containing themes
1001 * callback Called for each theme found in path
1002 * lpData Passed through to callback
1004 * RETURNS
1005 * Success: S_OK
1006 * Failure: HRESULT error-code
1008 HRESULT WINAPI EnumThemes(LPCWSTR pszThemePath, EnumThemeProc callback,
1009 LPVOID lpData)
1011 WCHAR szDir[MAX_PATH];
1012 WCHAR szPath[MAX_PATH];
1013 WCHAR szName[60];
1014 WCHAR szTip[60];
1015 HANDLE hFind;
1016 WIN32_FIND_DATAW wfd;
1017 HRESULT hr;
1018 size_t pathLen;
1020 TRACE("(%s,%p,%p)\n", debugstr_w(pszThemePath), callback, lpData);
1022 if(!pszThemePath || !callback)
1023 return E_POINTER;
1025 lstrcpyW(szDir, pszThemePath);
1026 pathLen = lstrlenW (szDir);
1027 if ((pathLen > 0) && (pathLen < MAX_PATH-1) && (szDir[pathLen - 1] != '\\'))
1029 szDir[pathLen] = '\\';
1030 szDir[pathLen+1] = 0;
1033 lstrcpyW(szPath, szDir);
1034 lstrcatW(szPath, L"*.*");
1035 TRACE("searching %s\n", debugstr_w(szPath));
1037 hFind = FindFirstFileW(szPath, &wfd);
1038 if(hFind != INVALID_HANDLE_VALUE) {
1039 do {
1040 if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
1041 && !(wfd.cFileName[0] == '.' && ((wfd.cFileName[1] == '.' && wfd.cFileName[2] == 0) || wfd.cFileName[1] == 0))) {
1042 wsprintfW(szPath, L"%s%s\\%s.msstyles", szDir, wfd.cFileName, wfd.cFileName);
1044 hr = GetThemeDocumentationProperty(szPath, L"displayname", szName, ARRAY_SIZE(szName));
1045 if(SUCCEEDED(hr))
1046 hr = GetThemeDocumentationProperty(szPath, L"tooltip", szTip, ARRAY_SIZE(szTip));
1047 if(SUCCEEDED(hr)) {
1048 TRACE("callback(%s,%s,%s,%p)\n", debugstr_w(szPath), debugstr_w(szName), debugstr_w(szTip), lpData);
1049 if(!callback(NULL, szPath, szName, szTip, NULL, lpData)) {
1050 TRACE("callback ended enum\n");
1051 break;
1055 } while(FindNextFileW(hFind, &wfd));
1056 FindClose(hFind);
1058 return S_OK;
1062 /**********************************************************************
1063 * EnumThemeColors (UXTHEME.9)
1065 * Enumerate theme colors available with a particular size
1067 * PARAMS
1068 * pszThemeFileName Path to a msstyles theme file
1069 * pszSizeName Theme size to enumerate available colors
1070 * If NULL the default theme size is used
1071 * dwColorNum Color index to retrieve, increment from 0
1072 * pszColorNames Output color names
1074 * RETURNS
1075 * S_OK on success
1076 * E_PROP_ID_UNSUPPORTED when dwColorName does not refer to a color
1077 * or when pszSizeName does not refer to a valid size
1079 * NOTES
1080 * XP fails with E_POINTER when pszColorNames points to a buffer smaller than
1081 * sizeof(THEMENAMES).
1083 * Not very efficient that I'm opening & validating the theme every call, but
1084 * this is undocumented and almost never called..
1085 * (and this is how windows works too)
1087 HRESULT WINAPI EnumThemeColors(LPWSTR pszThemeFileName, LPWSTR pszSizeName,
1088 DWORD dwColorNum, PTHEMENAMES pszColorNames)
1090 PTHEME_FILE pt;
1091 HRESULT hr;
1092 LPWSTR tmp;
1093 UINT resourceId = dwColorNum + 1000;
1094 TRACE("(%s,%s,%d)\n", debugstr_w(pszThemeFileName),
1095 debugstr_w(pszSizeName), dwColorNum);
1097 hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, pszSizeName, &pt);
1098 if(FAILED(hr)) return hr;
1100 tmp = pt->pszAvailColors;
1101 while(dwColorNum && *tmp) {
1102 dwColorNum--;
1103 tmp += lstrlenW(tmp)+1;
1105 if(!dwColorNum && *tmp) {
1106 TRACE("%s\n", debugstr_w(tmp));
1107 lstrcpyW(pszColorNames->szName, tmp);
1108 LoadStringW(pt->hTheme, resourceId, pszColorNames->szDisplayName,
1109 ARRAY_SIZE(pszColorNames->szDisplayName));
1110 LoadStringW(pt->hTheme, resourceId+1000, pszColorNames->szTooltip,
1111 ARRAY_SIZE(pszColorNames->szTooltip));
1113 else
1114 hr = E_PROP_ID_UNSUPPORTED;
1116 MSSTYLES_CloseThemeFile(pt);
1117 return hr;
1120 /**********************************************************************
1121 * EnumThemeSizes (UXTHEME.10)
1123 * Enumerate theme colors available with a particular size
1125 * PARAMS
1126 * pszThemeFileName Path to a msstyles theme file
1127 * pszColorName Theme color to enumerate available sizes
1128 * If NULL the default theme color is used
1129 * dwSizeNum Size index to retrieve, increment from 0
1130 * pszSizeNames Output size names
1132 * RETURNS
1133 * S_OK on success
1134 * E_PROP_ID_UNSUPPORTED when dwSizeName does not refer to a size
1135 * or when pszColorName does not refer to a valid color
1137 * NOTES
1138 * XP fails with E_POINTER when pszSizeNames points to a buffer smaller than
1139 * sizeof(THEMENAMES).
1141 * Not very efficient that I'm opening & validating the theme every call, but
1142 * this is undocumented and almost never called..
1143 * (and this is how windows works too)
1145 HRESULT WINAPI EnumThemeSizes(LPWSTR pszThemeFileName, LPWSTR pszColorName,
1146 DWORD dwSizeNum, PTHEMENAMES pszSizeNames)
1148 PTHEME_FILE pt;
1149 HRESULT hr;
1150 LPWSTR tmp;
1151 UINT resourceId = dwSizeNum + 3000;
1152 TRACE("(%s,%s,%d)\n", debugstr_w(pszThemeFileName),
1153 debugstr_w(pszColorName), dwSizeNum);
1155 hr = MSSTYLES_OpenThemeFile(pszThemeFileName, pszColorName, NULL, &pt);
1156 if(FAILED(hr)) return hr;
1158 tmp = pt->pszAvailSizes;
1159 while(dwSizeNum && *tmp) {
1160 dwSizeNum--;
1161 tmp += lstrlenW(tmp)+1;
1163 if(!dwSizeNum && *tmp) {
1164 TRACE("%s\n", debugstr_w(tmp));
1165 lstrcpyW(pszSizeNames->szName, tmp);
1166 LoadStringW(pt->hTheme, resourceId, pszSizeNames->szDisplayName,
1167 ARRAY_SIZE(pszSizeNames->szDisplayName));
1168 LoadStringW(pt->hTheme, resourceId+1000, pszSizeNames->szTooltip,
1169 ARRAY_SIZE(pszSizeNames->szTooltip));
1171 else
1172 hr = E_PROP_ID_UNSUPPORTED;
1174 MSSTYLES_CloseThemeFile(pt);
1175 return hr;
1178 /**********************************************************************
1179 * ParseThemeIniFile (UXTHEME.11)
1181 * Enumerate data in a theme INI file.
1183 * PARAMS
1184 * pszIniFileName Path to a theme ini file
1185 * pszUnknown Cannot be NULL, L"" is valid
1186 * callback Called for each found entry
1187 * lpData Passed through to callback
1189 * RETURNS
1190 * S_OK on success
1191 * 0x800706488 (Unknown property) when enumeration is canceled from callback
1193 * NOTES
1194 * When pszUnknown is NULL the callback is never called, the value does not seem to serve
1195 * any other purpose
1197 HRESULT WINAPI ParseThemeIniFile(LPCWSTR pszIniFileName, LPWSTR pszUnknown,
1198 ParseThemeIniFileProc callback, LPVOID lpData)
1200 FIXME("%s %s: stub\n", debugstr_w(pszIniFileName), debugstr_w(pszUnknown));
1201 return E_NOTIMPL;
1204 /**********************************************************************
1205 * CheckThemeSignature (UXTHEME.29)
1207 * Validates the signature of a theme file
1209 * PARAMS
1210 * pszIniFileName Path to a theme file
1212 * RETURNS
1213 * Success: S_OK
1214 * Failure: HRESULT error-code
1216 HRESULT WINAPI CheckThemeSignature(LPCWSTR pszThemeFileName)
1218 PTHEME_FILE pt;
1219 HRESULT hr;
1220 TRACE("(%s)\n", debugstr_w(pszThemeFileName));
1221 hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, NULL, &pt);
1222 if(FAILED(hr))
1223 return hr;
1224 MSSTYLES_CloseThemeFile(pt);
1225 return S_OK;
1228 BOOL WINAPI ThemeHooksInstall(void)
1230 struct user_api_hook hooks;
1232 hooks.pDefDlgProc = UXTHEME_DefDlgProc;
1233 hooks.pScrollBarDraw = UXTHEME_ScrollBarDraw;
1234 hooks.pScrollBarWndProc = UXTHEME_ScrollbarWndProc;
1235 return RegisterUserApiHook(&hooks, &user_api);
1238 BOOL WINAPI ThemeHooksRemove(void)
1240 UnregisterUserApiHook();
1241 return TRUE;