EnumThemeColors() and EnumThemeSizes() actually do not return a single
[wine] / dlls / uxtheme / system.c
1 /*
2  * Win32 5.1 Theme system
3  *
4  * Copyright (C) 2003 Kevin Koltzau
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "wingdi.h"
30 #include "winreg.h"
31 #include "shlwapi.h"
32 #include "uxtheme.h"
33 #include "tmschema.h"
34
35 #include "uxthemedll.h"
36 #include "msstyles.h"
37
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);
41
42 /***********************************************************************
43  * Defines and global variables
44  */
45
46 static const WCHAR szThemeManager[] = {
47     'S','o','f','t','w','a','r','e','\\',
48     'M','i','c','r','o','s','o','f','t','\\',
49     'W','i','n','d','o','w','s','\\',
50     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
51     'T','h','e','m','e','M','a','n','a','g','e','r','\0'
52 };
53 static const WCHAR szThemeActive[] = {'T','h','e','m','e','A','c','t','i','v','e','\0'};
54 static const WCHAR szSizeName[] = {'S','i','z','e','N','a','m','e','\0'};
55 static const WCHAR szColorName[] = {'C','o','l','o','r','N','a','m','e','\0'};
56 static const WCHAR szDllName[] = {'D','l','l','N','a','m','e','\0'};
57
58 static const WCHAR szIniDocumentation[] = {'d','o','c','u','m','e','n','t','a','t','i','o','n','\0'};
59
60 HINSTANCE hDllInst;
61
62 DWORD dwThemeAppProperties = STAP_ALLOW_NONCLIENT | STAP_ALLOW_CONTROLS;
63 ATOM atWindowTheme;
64 ATOM atSubAppName;
65 ATOM atSubIdList;
66 ATOM atDialogThemeEnabled;
67
68 BOOL bThemeActive = FALSE;
69 WCHAR szCurrentTheme[MAX_PATH];
70 WCHAR szCurrentColor[64];
71 WCHAR szCurrentSize[64];
72
73 /***********************************************************************/
74
75 static BOOL CALLBACK UXTHEME_broadcast_msg_enumchild (HWND hWnd, LPARAM msg)
76 {
77     PostMessageW(hWnd, msg, 0, 0);
78     return TRUE;
79 }
80
81 /* Broadcast a message to *all* windows, including children */
82 static BOOL CALLBACK UXTHEME_broadcast_msg (HWND hWnd, LPARAM msg)
83 {
84     if (hWnd == NULL)
85     {
86         EnumWindows (UXTHEME_broadcast_msg, msg);
87     }
88     else
89     {
90         PostMessageW(hWnd, msg, 0, 0);
91         EnumChildWindows (hWnd, UXTHEME_broadcast_msg_enumchild, msg);
92     }
93     return TRUE;
94 }
95
96 /***********************************************************************
97  *      UXTHEME_LoadTheme
98  *
99  * Set the current active theme from the registry
100  */
101 static void UXTHEME_LoadTheme(void)
102 {
103     HKEY hKey;
104     DWORD buffsize;
105     HRESULT hr;
106     WCHAR tmp[10];
107     PTHEME_FILE pt;
108
109     /* Get current theme configuration */
110     if(!RegOpenKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
111         TRACE("Loading theme config\n");
112         buffsize = sizeof(tmp)/sizeof(tmp[0]);
113         if(!RegQueryValueExW(hKey, szThemeActive, NULL, NULL, (LPBYTE)tmp, &buffsize)) {
114             bThemeActive = (tmp[0] != '0');
115         }
116         else {
117             bThemeActive = FALSE;
118             TRACE("Failed to get ThemeActive: %ld\n", GetLastError());
119         }
120         buffsize = sizeof(szCurrentColor)/sizeof(szCurrentColor[0]);
121         if(RegQueryValueExW(hKey, szColorName, NULL, NULL, (LPBYTE)szCurrentColor, &buffsize))
122             szCurrentColor[0] = '\0';
123         buffsize = sizeof(szCurrentSize)/sizeof(szCurrentSize[0]);
124         if(RegQueryValueExW(hKey, szSizeName, NULL, NULL, (LPBYTE)szCurrentSize, &buffsize))
125             szCurrentSize[0] = '\0';
126         if(SHRegGetPathW(hKey, NULL, szDllName, szCurrentTheme, 0))
127             szCurrentTheme[0] = '\0';
128         RegCloseKey(hKey);
129     }
130     else
131         TRACE("Failed to open theme registry key\n");
132
133     if(bThemeActive) {
134         /* Make sure the theme requested is actually valid */
135         hr = MSSTYLES_OpenThemeFile(szCurrentTheme,
136                                     szCurrentColor[0]?szCurrentColor:NULL,
137                                     szCurrentSize[0]?szCurrentSize:NULL,
138                                     &pt);
139         if(FAILED(hr)) {
140             bThemeActive = FALSE;
141             szCurrentTheme[0] = '\0';
142             szCurrentColor[0] = '\0';
143             szCurrentSize[0] = '\0';
144         }
145         else {
146             /* Make sure the global color & size match the theme */
147             lstrcpynW(szCurrentColor, pt->pszSelectedColor, sizeof(szCurrentColor)/sizeof(szCurrentColor[0]));
148             lstrcpynW(szCurrentSize, pt->pszSelectedSize, sizeof(szCurrentSize)/sizeof(szCurrentSize[0]));
149
150             MSSTYLES_SetActiveTheme(pt, FALSE);
151             TRACE("Theme active: %s %s %s\n", debugstr_w(szCurrentTheme),
152                 debugstr_w(szCurrentColor), debugstr_w(szCurrentSize));
153             MSSTYLES_CloseThemeFile(pt);
154         }
155     }
156     if(!bThemeActive) {
157         MSSTYLES_SetActiveTheme(NULL, FALSE);
158         TRACE("Themeing not active\n");
159     }
160 }
161
162 /***********************************************************************/
163
164 static const char * const SysColorsNames[] =
165 {
166     "Scrollbar",                /* COLOR_SCROLLBAR */
167     "Background",               /* COLOR_BACKGROUND */
168     "ActiveTitle",              /* COLOR_ACTIVECAPTION */
169     "InactiveTitle",            /* COLOR_INACTIVECAPTION */
170     "Menu",                     /* COLOR_MENU */
171     "Window",                   /* COLOR_WINDOW */
172     "WindowFrame",              /* COLOR_WINDOWFRAME */
173     "MenuText",                 /* COLOR_MENUTEXT */
174     "WindowText",               /* COLOR_WINDOWTEXT */
175     "TitleText",                /* COLOR_CAPTIONTEXT */
176     "ActiveBorder",             /* COLOR_ACTIVEBORDER */
177     "InactiveBorder",           /* COLOR_INACTIVEBORDER */
178     "AppWorkSpace",             /* COLOR_APPWORKSPACE */
179     "Hilight",                  /* COLOR_HIGHLIGHT */
180     "HilightText",              /* COLOR_HIGHLIGHTTEXT */
181     "ButtonFace",               /* COLOR_BTNFACE */
182     "ButtonShadow",             /* COLOR_BTNSHADOW */
183     "GrayText",                 /* COLOR_GRAYTEXT */
184     "ButtonText",               /* COLOR_BTNTEXT */
185     "InactiveTitleText",        /* COLOR_INACTIVECAPTIONTEXT */
186     "ButtonHilight",            /* COLOR_BTNHIGHLIGHT */
187     "ButtonDkShadow",           /* COLOR_3DDKSHADOW */
188     "ButtonLight",              /* COLOR_3DLIGHT */
189     "InfoText",                 /* COLOR_INFOTEXT */
190     "InfoWindow",               /* COLOR_INFOBK */
191     "ButtonAlternateFace",      /* COLOR_ALTERNATEBTNFACE */
192     "HotTrackingColor",         /* COLOR_HOTLIGHT */
193     "GradientActiveTitle",      /* COLOR_GRADIENTACTIVECAPTION */
194     "GradientInactiveTitle",    /* COLOR_GRADIENTINACTIVECAPTION */
195     "MenuHilight",              /* COLOR_MENUHILIGHT */
196     "MenuBar",                  /* COLOR_MENUBAR */
197 };
198 static const WCHAR strColorKey[] = 
199     { 'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
200       'C','o','l','o','r','s',0 };
201 static const WCHAR keyFlatMenus[] = { 'F','l','a','t','M','e','n','u', 0};
202
203 static const struct BackupSysParam
204 {
205     int spiGet, spiSet;
206     const WCHAR* keyName;
207 } backupSysParams[] = 
208 {
209     {SPI_GETFLATMENU, SPI_SETFLATMENU, keyFlatMenus},
210     {-1, -1, 0}
211 };
212
213 #define NUM_SYS_COLORS     (COLOR_MENUBAR+1)
214
215 static void save_sys_colors (HKEY baseKey)
216 {
217     char colorStr[13];
218     HKEY hKey;
219     int i;
220
221     if (RegCreateKeyExW( baseKey, strColorKey,
222                          0, 0, 0, KEY_ALL_ACCESS,
223                          0, &hKey, 0 ) == ERROR_SUCCESS)
224     {
225         for (i = 0; i < NUM_SYS_COLORS; i++)
226         {
227             COLORREF col = GetSysColor (i);
228         
229             sprintf (colorStr, "%d %d %d", 
230                 GetRValue (col), GetGValue (col), GetBValue (col));
231
232             RegSetValueExA (hKey, SysColorsNames[i], 0, REG_SZ, 
233                 (BYTE*)colorStr, strlen (colorStr)+1);
234         }
235         RegCloseKey (hKey);
236     }
237 }
238
239 /* Before activating a theme, query current system colors, certain settings 
240  * and backup them in the registry, so they can be restored when the theme 
241  * is deactivated */
242 static void UXTHEME_BackupSystemMetrics(void)
243 {
244     HKEY hKey;
245     const struct BackupSysParam* bsp = backupSysParams;
246
247     if (RegCreateKeyExW( HKEY_CURRENT_USER, szThemeManager,
248                          0, 0, 0, KEY_ALL_ACCESS,
249                          0, &hKey, 0) == ERROR_SUCCESS)
250     {
251         save_sys_colors (hKey);
252     
253         while (bsp->spiGet >= 0)
254         {
255             DWORD value;
256             
257             SystemParametersInfoW (bsp->spiGet, 0, &value, 0);
258             RegSetValueExW (hKey, bsp->keyName, 0, REG_DWORD, 
259                 (LPBYTE)&value, sizeof (value));
260         
261             bsp++;
262         }
263     
264         RegCloseKey (hKey);
265     }
266 }
267
268 /* Read back old settings after a theme was deactivated */
269 static void UXTHEME_RestoreSystemMetrics(void)
270 {
271     HKEY hKey;
272     const struct BackupSysParam* bsp = backupSysParams;
273
274     if (RegOpenKeyExW (HKEY_CURRENT_USER, szThemeManager,
275                        0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) 
276     {
277         HKEY colorKey;
278     
279         /* read backed-up colors */
280         if (RegOpenKeyExW (hKey, strColorKey,
281                            0, KEY_QUERY_VALUE, &colorKey) == ERROR_SUCCESS) 
282         {
283             int i;
284             COLORREF sysCols[NUM_SYS_COLORS];
285             int sysColsIndices[NUM_SYS_COLORS];
286             int sysColCount = 0;
287         
288             for (i = 0; i < NUM_SYS_COLORS; i++)
289             {
290                 DWORD type;
291                 char colorStr[13];
292                 DWORD count = sizeof(colorStr);
293             
294                 if (RegQueryValueExA (colorKey, SysColorsNames[i], 0,
295                     &type, colorStr, &count) == ERROR_SUCCESS)
296                 {
297                     int r, g, b;
298                     if (sscanf (colorStr, "%d %d %d", &r, &g, &b) == 3)
299                     {
300                         sysColsIndices[sysColCount] = i;
301                         sysCols[sysColCount] = RGB(r, g, b);
302                         sysColCount++;
303                     }
304                 }
305             }
306             RegCloseKey (colorKey);
307           
308             SetSysColors (sysColCount, sysColsIndices, sysCols);
309         }
310     
311         /* read backed-up other settings */
312         while (bsp->spiGet >= 0)
313         {
314             DWORD value;
315             DWORD count = sizeof(value);
316             DWORD type;
317             
318             if (RegQueryValueExW (hKey, bsp->keyName, 0,
319                 &type, (LPBYTE)&value, &count) == ERROR_SUCCESS)
320             {
321                 SystemParametersInfoW (bsp->spiSet, 0, (LPVOID)value,
322                     SPIF_UPDATEINIFILE);
323             }
324         
325             bsp++;
326         }
327     
328       
329         RegCloseKey (hKey);
330     }
331 }
332
333 /* Make system settings persistent, so they're in effect even w/o uxtheme 
334  * loaded */
335 static void UXTHEME_SaveSystemMetrics(void)
336 {
337     const struct BackupSysParam* bsp = backupSysParams;
338
339     save_sys_colors (HKEY_CURRENT_USER);
340
341     while (bsp->spiGet >= 0)
342     {
343         DWORD value;
344         
345         SystemParametersInfoW (bsp->spiGet, 0, &value, 0);
346         SystemParametersInfoW (bsp->spiSet, 0, (LPVOID)value,
347             SPIF_UPDATEINIFILE);
348     
349         bsp++;
350     }
351
352 }
353
354 /***********************************************************************
355  *      UXTHEME_SetActiveTheme
356  *
357  * Change the current active theme
358  */
359 HRESULT UXTHEME_SetActiveTheme(PTHEME_FILE tf)
360 {
361     HKEY hKey;
362     WCHAR tmp[2];
363     HRESULT hr;
364
365     if(tf) UXTHEME_BackupSystemMetrics();
366     hr = MSSTYLES_SetActiveTheme(tf, TRUE);
367     if(FAILED(hr))
368         return hr;
369     if(tf) {
370         bThemeActive = TRUE;
371         lstrcpynW(szCurrentTheme, tf->szThemeFile, sizeof(szCurrentTheme)/sizeof(szCurrentTheme[0]));
372         lstrcpynW(szCurrentColor, tf->pszSelectedColor, sizeof(szCurrentColor)/sizeof(szCurrentColor[0]));
373         lstrcpynW(szCurrentSize, tf->pszSelectedSize, sizeof(szCurrentSize)/sizeof(szCurrentSize[0]));
374     }
375     else {
376         UXTHEME_RestoreSystemMetrics();
377         bThemeActive = FALSE;
378         szCurrentTheme[0] = '\0';
379         szCurrentColor[0] = '\0';
380         szCurrentSize[0] = '\0';
381     }
382
383     TRACE("Writing theme config to registry\n");
384     if(!RegCreateKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
385         tmp[0] = bThemeActive?'1':'0';
386         tmp[1] = '\0';
387         RegSetValueExW(hKey, szThemeActive, 0, REG_SZ, (const BYTE*)tmp, sizeof(WCHAR)*2);
388         if(bThemeActive) {
389             RegSetValueExW(hKey, szColorName, 0, REG_SZ, (const BYTE*)szCurrentColor, 
390                 (lstrlenW(szCurrentColor)+1)*sizeof(WCHAR));
391             RegSetValueExW(hKey, szSizeName, 0, REG_SZ, (const BYTE*)szCurrentSize, 
392                 (lstrlenW(szCurrentSize)+1)*sizeof(WCHAR));
393             RegSetValueExW(hKey, szDllName, 0, REG_SZ, (const BYTE*)szCurrentTheme, 
394                 (lstrlenW(szCurrentTheme)+1)*sizeof(WCHAR));
395         }
396         else {
397             RegDeleteValueW(hKey, szColorName);
398             RegDeleteValueW(hKey, szSizeName);
399             RegDeleteValueW(hKey, szDllName);
400
401         }
402         RegCloseKey(hKey);
403     }
404     else
405         TRACE("Failed to open theme registry key\n");
406     
407     UXTHEME_SaveSystemMetrics ();
408     
409     return hr;
410 }
411
412 /***********************************************************************
413  *      UXTHEME_InitSystem
414  */
415 void UXTHEME_InitSystem(HINSTANCE hInst)
416 {
417     static const WCHAR szWindowTheme[] = {
418         'u','x','_','t','h','e','m','e','\0'
419     };
420     static const WCHAR szSubAppName[] = {
421         'u','x','_','s','u','b','a','p','p','\0'
422     };
423     static const WCHAR szSubIdList[] = {
424         'u','x','_','s','u','b','i','d','l','s','t','\0'
425     };
426     static const WCHAR szDialogThemeEnabled[] = {
427         'u','x','_','d','i','a','l','o','g','t','h','e','m','e','\0'
428     };
429
430     hDllInst = hInst;
431
432     atWindowTheme        = GlobalAddAtomW(szWindowTheme);
433     atSubAppName         = GlobalAddAtomW(szSubAppName);
434     atSubIdList          = GlobalAddAtomW(szSubIdList);
435     atDialogThemeEnabled = GlobalAddAtomW(szDialogThemeEnabled);
436
437     UXTHEME_LoadTheme();
438 }
439
440 /***********************************************************************
441  *      IsAppThemed                                         (UXTHEME.@)
442  */
443 BOOL WINAPI IsAppThemed(void)
444 {
445     return IsThemeActive();
446 }
447
448 /***********************************************************************
449  *      IsThemeActive                                       (UXTHEME.@)
450  */
451 BOOL WINAPI IsThemeActive(void)
452 {
453     TRACE("\n");
454     return bThemeActive;
455 }
456
457 /***********************************************************************
458  *      EnableTheming                                       (UXTHEME.@)
459  *
460  * NOTES
461  * This is a global and persistent change
462  */
463 HRESULT WINAPI EnableTheming(BOOL fEnable)
464 {
465     HKEY hKey;
466     WCHAR szEnabled[] = {'0','\0'};
467
468     TRACE("(%d)\n", fEnable);
469
470     if(fEnable != bThemeActive) {
471         if(fEnable) 
472             UXTHEME_BackupSystemMetrics();
473         else
474             UXTHEME_RestoreSystemMetrics();
475         UXTHEME_SaveSystemMetrics ();
476         bThemeActive = fEnable;
477         if(bThemeActive) szEnabled[0] = '1';
478         if(!RegOpenKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
479             RegSetValueExW(hKey, szThemeActive, 0, REG_SZ, (LPBYTE)szEnabled, sizeof(WCHAR));
480             RegCloseKey(hKey);
481         }
482         UXTHEME_broadcast_msg (NULL, WM_THEMECHANGED);
483     }
484     return S_OK;
485 }
486
487 /***********************************************************************
488  *      UXTHEME_SetWindowProperty
489  *
490  * I'm using atoms as there may be large numbers of duplicated strings
491  * and they do the work of keeping memory down as a cause of that quite nicely
492  */
493 HRESULT UXTHEME_SetWindowProperty(HWND hwnd, ATOM aProp, LPCWSTR pszValue)
494 {
495     ATOM oldValue = (ATOM)(size_t)RemovePropW(hwnd, MAKEINTATOMW(aProp));
496     if(oldValue)
497         DeleteAtom(oldValue);
498     if(pszValue) {
499         ATOM atValue = AddAtomW(pszValue);
500         if(!atValue
501            || !SetPropW(hwnd, MAKEINTATOMW(aProp), (LPWSTR)MAKEINTATOMW(atValue))) {
502             HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
503             if(atValue) DeleteAtom(atValue);
504             return hr;
505         }
506     }
507     return S_OK;
508 }
509
510 LPWSTR UXTHEME_GetWindowProperty(HWND hwnd, ATOM aProp, LPWSTR pszBuffer, int dwLen)
511 {
512     ATOM atValue = (ATOM)(size_t)GetPropW(hwnd, MAKEINTATOMW(aProp));
513     if(atValue) {
514         if(GetAtomNameW(atValue, pszBuffer, dwLen))
515             return pszBuffer;
516         TRACE("property defined, but unable to get value\n");
517     }
518     return NULL;
519 }
520
521 /***********************************************************************
522  *      OpenThemeData                                       (UXTHEME.@)
523  */
524 HTHEME WINAPI OpenThemeData(HWND hwnd, LPCWSTR pszClassList)
525 {
526     WCHAR szAppBuff[256];
527     WCHAR szClassBuff[256];
528     LPCWSTR pszAppName;
529     LPCWSTR pszUseClassList;
530     HTHEME hTheme = NULL;
531     TRACE("(%p,%s)", hwnd, debugstr_w(pszClassList));
532
533     if(bThemeActive)
534     {
535         pszAppName = UXTHEME_GetWindowProperty(hwnd, atSubAppName, szAppBuff, sizeof(szAppBuff)/sizeof(szAppBuff[0]));
536         /* If SetWindowTheme was used on the window, that overrides the class list passed to this function */
537         pszUseClassList = UXTHEME_GetWindowProperty(hwnd, atSubIdList, szClassBuff, sizeof(szClassBuff)/sizeof(szClassBuff[0]));
538         if(!pszUseClassList)
539             pszUseClassList = pszClassList;
540
541         if (pszUseClassList)
542             hTheme = MSSTYLES_OpenThemeClass(pszAppName, pszUseClassList);
543     }
544     if(IsWindow(hwnd))
545         SetPropW(hwnd, MAKEINTATOMW(atWindowTheme), hTheme);
546     TRACE(" = %p\n", hTheme);
547     return hTheme;
548 }
549
550 /***********************************************************************
551  *      GetWindowTheme                                      (UXTHEME.@)
552  *
553  * Retrieve the last theme opened for a window
554  */
555 HTHEME WINAPI GetWindowTheme(HWND hwnd)
556 {
557     TRACE("(%p)\n", hwnd);
558     return GetPropW(hwnd, MAKEINTATOMW(atWindowTheme));
559 }
560
561 /***********************************************************************
562  *      SetWindowTheme                                      (UXTHEME.@)
563  *
564  * Persistent through the life of the window, even after themes change
565  */
566 HRESULT WINAPI SetWindowTheme(HWND hwnd, LPCWSTR pszSubAppName,
567                               LPCWSTR pszSubIdList)
568 {
569     HRESULT hr;
570     TRACE("(%p,%s,%s)\n", hwnd, debugstr_w(pszSubAppName),
571           debugstr_w(pszSubIdList));
572     hr = UXTHEME_SetWindowProperty(hwnd, atSubAppName, pszSubAppName);
573     if(SUCCEEDED(hr))
574         hr = UXTHEME_SetWindowProperty(hwnd, atSubIdList, pszSubIdList);
575     if(SUCCEEDED(hr))
576         UXTHEME_broadcast_msg (hwnd, WM_THEMECHANGED);
577     return hr;
578 }
579
580 /***********************************************************************
581  *      GetCurrentThemeName                                 (UXTHEME.@)
582  */
583 HRESULT WINAPI GetCurrentThemeName(LPWSTR pszThemeFileName, int dwMaxNameChars,
584                                    LPWSTR pszColorBuff, int cchMaxColorChars,
585                                    LPWSTR pszSizeBuff, int cchMaxSizeChars)
586 {
587     if(!bThemeActive)
588         return E_PROP_ID_UNSUPPORTED;
589     if(pszThemeFileName) lstrcpynW(pszThemeFileName, szCurrentTheme, dwMaxNameChars);
590     if(pszColorBuff) lstrcpynW(pszColorBuff, szCurrentColor, cchMaxColorChars);
591     if(pszSizeBuff) lstrcpynW(pszSizeBuff, szCurrentSize, cchMaxSizeChars);
592     return S_OK;
593 }
594
595 /***********************************************************************
596  *      GetThemeAppProperties                               (UXTHEME.@)
597  */
598 DWORD WINAPI GetThemeAppProperties(void)
599 {
600     return dwThemeAppProperties;
601 }
602
603 /***********************************************************************
604  *      SetThemeAppProperties                               (UXTHEME.@)
605  */
606 void WINAPI SetThemeAppProperties(DWORD dwFlags)
607 {
608     TRACE("(0x%08lx)\n", dwFlags);
609     dwThemeAppProperties = dwFlags;
610 }
611
612 /***********************************************************************
613  *      CloseThemeData                                      (UXTHEME.@)
614  */
615 HRESULT WINAPI CloseThemeData(HTHEME hTheme)
616 {
617     TRACE("(%p)\n", hTheme);
618     if(!hTheme)
619         return E_HANDLE;
620     return MSSTYLES_CloseThemeClass(hTheme);
621 }
622
623 /***********************************************************************
624  *      HitTestThemeBackground                              (UXTHEME.@)
625  */
626 HRESULT WINAPI HitTestThemeBackground(HTHEME hTheme, HDC hdc, int iPartId,
627                                      int iStateId, DWORD dwOptions,
628                                      const RECT *pRect, HRGN hrgn,
629                                      POINT ptTest, WORD *pwHitTestCode)
630 {
631     FIXME("%d %d 0x%08lx: stub\n", iPartId, iStateId, dwOptions);
632     if(!hTheme)
633         return E_HANDLE;
634     return ERROR_CALL_NOT_IMPLEMENTED;
635 }
636
637 /***********************************************************************
638  *      IsThemePartDefined                                  (UXTHEME.@)
639  */
640 BOOL WINAPI IsThemePartDefined(HTHEME hTheme, int iPartId, int iStateId)
641 {
642     TRACE("(%p,%d,%d)\n", hTheme, iPartId, iStateId);
643     if(!hTheme) {
644         SetLastError(E_HANDLE);
645         return FALSE;
646     }
647     if(MSSTYLES_FindPartState(hTheme, iPartId, iStateId, NULL))
648         return TRUE;
649     return FALSE;
650 }
651
652 /***********************************************************************
653  *      GetThemeDocumentationProperty                       (UXTHEME.@)
654  *
655  * Try and retrieve the documentation property from string resources
656  * if that fails, get it from the [documentation] section of themes.ini
657  */
658 HRESULT WINAPI GetThemeDocumentationProperty(LPCWSTR pszThemeName,
659                                              LPCWSTR pszPropertyName,
660                                              LPWSTR pszValueBuff,
661                                              int cchMaxValChars)
662 {
663     const WORD wDocToRes[] = {
664         TMT_DISPLAYNAME,5000,
665         TMT_TOOLTIP,5001,
666         TMT_COMPANY,5002,
667         TMT_AUTHOR,5003,
668         TMT_COPYRIGHT,5004,
669         TMT_URL,5005,
670         TMT_VERSION,5006,
671         TMT_DESCRIPTION,5007
672     };
673
674     PTHEME_FILE pt;
675     HRESULT hr;
676     unsigned int i;
677     int iDocId;
678     TRACE("(%s,%s,%p,%d)\n", debugstr_w(pszThemeName), debugstr_w(pszPropertyName),
679           pszValueBuff, cchMaxValChars);
680
681     hr = MSSTYLES_OpenThemeFile(pszThemeName, NULL, NULL, &pt);
682     if(FAILED(hr)) return hr;
683
684     /* Try to load from string resources */
685     hr = E_PROP_ID_UNSUPPORTED;
686     if(MSSTYLES_LookupProperty(pszPropertyName, NULL, &iDocId)) {
687         for(i=0; i<sizeof(wDocToRes)/sizeof(wDocToRes[0]); i+=2) {
688             if(wDocToRes[i] == iDocId) {
689                 if(LoadStringW(pt->hTheme, wDocToRes[i+1], pszValueBuff, cchMaxValChars)) {
690                     hr = S_OK;
691                     break;
692                 }
693             }
694         }
695     }
696     /* If loading from string resource failed, try getting it from the theme.ini */
697     if(FAILED(hr)) {
698         PUXINI_FILE uf = MSSTYLES_GetThemeIni(pt);
699         if(UXINI_FindSection(uf, szIniDocumentation)) {
700             LPCWSTR lpValue;
701             DWORD dwLen;
702             if(UXINI_FindValue(uf, pszPropertyName, &lpValue, &dwLen)) {
703                 lstrcpynW(pszValueBuff, lpValue, min(dwLen+1,cchMaxValChars));
704                 hr = S_OK;
705             }
706         }
707         UXINI_CloseINI(uf);
708     }
709
710     MSSTYLES_CloseThemeFile(pt);
711     return hr;
712 }
713
714 /**********************************************************************
715  *      Undocumented functions
716  */
717
718 /**********************************************************************
719  *      QueryThemeServices                                 (UXTHEME.1)
720  *
721  * RETURNS
722  *     some kind of status flag
723  */
724 DWORD WINAPI QueryThemeServices()
725 {
726     FIXME("stub\n");
727     return 3; /* This is what is returned under XP in most cases */
728 }
729
730
731 /**********************************************************************
732  *      OpenThemeFile                                      (UXTHEME.2)
733  *
734  * Opens a theme file, which can be used to change the current theme, etc
735  *
736  * PARAMS
737  *     pszThemeFileName    Path to a msstyles theme file
738  *     pszColorName        Color defined in the theme, eg. NormalColor
739  *     pszSizeName         Size defined in the theme, eg. NormalSize
740  *     hThemeFile          Handle to theme file
741  */
742 HRESULT WINAPI OpenThemeFile(LPCWSTR pszThemeFileName, LPCWSTR pszColorName,
743                              LPCWSTR pszSizeName, HTHEMEFILE *hThemeFile,
744                              DWORD unknown)
745 {
746     TRACE("(%s,%s,%s,%p,%ld)\n", debugstr_w(pszThemeFileName),
747           debugstr_w(pszColorName), debugstr_w(pszSizeName),
748           hThemeFile, unknown);
749     return MSSTYLES_OpenThemeFile(pszThemeFileName, pszColorName, pszSizeName, (PTHEME_FILE*)hThemeFile);
750 }
751
752 /**********************************************************************
753  *      CloseThemeFile                                     (UXTHEME.3)
754  *
755  * Releases theme file handle returned by OpenThemeFile
756  *
757  * PARAMS
758  *     hThemeFile           Handle to theme file
759  */
760 HRESULT WINAPI CloseThemeFile(HTHEMEFILE hThemeFile)
761 {
762     TRACE("(%p)\n", hThemeFile);
763     MSSTYLES_CloseThemeFile(hThemeFile);
764     return S_OK;
765 }
766
767 /**********************************************************************
768  *      ApplyTheme                                         (UXTHEME.4)
769  *
770  * Set a theme file to be the currently active theme
771  *
772  * PARAMS
773  *     hThemeFile           Handle to theme file
774  *     unknown              See notes
775  *     hWnd                 Window requesting the theme change
776  *
777  * NOTES
778  * I'm not sure what the second parameter is (the datatype is likely wrong), other then this:
779  * Under XP if I pass
780  * char b[] = "";
781  *   the theme is applied with the screen redrawing really badly (flickers)
782  * char b[] = "\0"; where \0 can be one or more of any character, makes no difference
783  *   the theme is applied smoothly (screen does not flicker)
784  * char *b = "\0" or NULL; where \0 can be zero or more of any character, makes no difference
785  *   the function fails returning invalid parameter...very strange
786  */
787 HRESULT WINAPI ApplyTheme(HTHEMEFILE hThemeFile, char *unknown, HWND hWnd)
788 {
789     HRESULT hr;
790     TRACE("(%p,%s,%p)\n", hThemeFile, unknown, hWnd);
791     hr = UXTHEME_SetActiveTheme(hThemeFile);
792     UXTHEME_broadcast_msg (NULL, WM_THEMECHANGED);
793     return hr;
794 }
795
796 /**********************************************************************
797  *      GetThemeDefaults                                   (UXTHEME.7)
798  *
799  * Get the default color & size for a theme
800  *
801  * PARAMS
802  *     pszThemeFileName    Path to a msstyles theme file
803  *     pszColorName        Buffer to receive the default color name
804  *     dwColorNameLen      Length, in characters, of color name buffer
805  *     pszSizeName         Buffer to receive the default size name
806  *     dwSizeNameLen       Length, in characters, of size name buffer
807  */
808 HRESULT WINAPI GetThemeDefaults(LPCWSTR pszThemeFileName, LPWSTR pszColorName,
809                                 DWORD dwColorNameLen, LPWSTR pszSizeName,
810                                 DWORD dwSizeNameLen)
811 {
812     PTHEME_FILE pt;
813     HRESULT hr;
814     TRACE("(%s,%p,%ld,%p,%ld)\n", debugstr_w(pszThemeFileName),
815           pszColorName, dwColorNameLen,
816           pszSizeName, dwSizeNameLen);
817
818     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, NULL, &pt);
819     if(FAILED(hr)) return hr;
820
821     lstrcpynW(pszColorName, pt->pszSelectedColor, dwColorNameLen);
822     lstrcpynW(pszSizeName, pt->pszSelectedSize, dwSizeNameLen);
823
824     MSSTYLES_CloseThemeFile(pt);
825     return S_OK;
826 }
827
828 /**********************************************************************
829  *      EnumThemes                                         (UXTHEME.8)
830  *
831  * Enumerate available themes, calls specified EnumThemeProc for each
832  * theme found. Passes lpData through to callback function.
833  *
834  * PARAMS
835  *     pszThemePath        Path containing themes
836  *     callback            Called for each theme found in path
837  *     lpData              Passed through to callback
838  */
839 HRESULT WINAPI EnumThemes(LPCWSTR pszThemePath, EnumThemeProc callback,
840                           LPVOID lpData)
841 {
842     WCHAR szDir[MAX_PATH];
843     WCHAR szPath[MAX_PATH];
844     static const WCHAR szStar[] = {'*','.','*','\0'};
845     static const WCHAR szFormat[] = {'%','s','%','s','\\','%','s','.','m','s','s','t','y','l','e','s','\0'};
846     static const WCHAR szDisplayName[] = {'d','i','s','p','l','a','y','n','a','m','e','\0'};
847     static const WCHAR szTooltip[] = {'t','o','o','l','t','i','p','\0'};
848     WCHAR szName[60];
849     WCHAR szTip[60];
850     HANDLE hFind;
851     WIN32_FIND_DATAW wfd;
852     HRESULT hr;
853
854     TRACE("(%s,%p,%p)\n", debugstr_w(pszThemePath), callback, lpData);
855
856     if(!pszThemePath || !callback)
857         return E_POINTER;
858
859     lstrcpyW(szDir, pszThemePath);
860     PathAddBackslashW(szDir);
861
862     lstrcpyW(szPath, szDir);
863     lstrcatW(szPath, szStar);
864     TRACE("searching %s\n", debugstr_w(szPath));
865
866     hFind = FindFirstFileW(szPath, &wfd);
867     if(hFind != INVALID_HANDLE_VALUE) {
868         do {
869             if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
870                && !(wfd.cFileName[0] == '.' && ((wfd.cFileName[1] == '.' && wfd.cFileName[2] == 0) || wfd.cFileName[1] == 0))) {
871                 wsprintfW(szPath, szFormat, szDir, wfd.cFileName, wfd.cFileName);
872
873                 hr = GetThemeDocumentationProperty(szPath, szDisplayName, szName, sizeof(szName)/sizeof(szName[0]));
874                 if(SUCCEEDED(hr))
875                     hr = GetThemeDocumentationProperty(szPath, szTooltip, szTip, sizeof(szTip)/sizeof(szTip[0]));
876                 if(SUCCEEDED(hr)) {
877                     TRACE("callback(%s,%s,%s,%p)\n", debugstr_w(szPath), debugstr_w(szName), debugstr_w(szTip), lpData);
878                     if(!callback(NULL, szPath, szName, szTip, NULL, lpData)) {
879                         TRACE("callback ended enum\n");
880                         break;
881                     }
882                 }
883             }
884         } while(FindNextFileW(hFind, &wfd));
885         FindClose(hFind);
886     }
887     return S_OK;
888 }
889
890
891 /**********************************************************************
892  *      EnumThemeColors                                    (UXTHEME.9)
893  *
894  * Enumerate theme colors available with a particular size
895  *
896  * PARAMS
897  *     pszThemeFileName    Path to a msstyles theme file
898  *     pszSizeName         Theme size to enumerate available colors
899  *                         If NULL the default theme size is used
900  *     dwColorNum          Color index to retrieve, increment from 0
901  *     pszColorNames       Output color names
902  *
903  * RETURNS
904  *     S_OK on success
905  *     E_PROP_ID_UNSUPPORTED when dwColorName does not refer to a color
906  *          or when pszSizeName does not refer to a valid size
907  *
908  * NOTES
909  * XP fails with E_POINTER when pszColorNames points to a buffer smaller than 
910  * sizeof(THEMENAMES).
911  *
912  * Not very efficient that I'm opening & validating the theme every call, but
913  * this is undocumented and almost never called..
914  * (and this is how windows works too)
915  */
916 HRESULT WINAPI EnumThemeColors(LPWSTR pszThemeFileName, LPWSTR pszSizeName,
917                                DWORD dwColorNum, PTHEMENAMES pszColorNames)
918 {
919     PTHEME_FILE pt;
920     HRESULT hr;
921     LPWSTR tmp;
922     UINT resourceId = dwColorNum + 1000;
923     TRACE("(%s,%s,%ld)\n", debugstr_w(pszThemeFileName),
924           debugstr_w(pszSizeName), dwColorNum);
925
926     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, pszSizeName, &pt);
927     if(FAILED(hr)) return hr;
928
929     tmp = pt->pszAvailColors;
930     while(dwColorNum && *tmp) {
931         dwColorNum--;
932         tmp += lstrlenW(tmp)+1;
933     }
934     if(!dwColorNum && *tmp) {
935         TRACE("%s\n", debugstr_w(tmp));
936         lstrcpyW(pszColorNames->szName, tmp);
937         LoadStringW (pt->hTheme, resourceId,
938             pszColorNames->szDisplayName,
939             sizeof (pszColorNames->szDisplayName) / sizeof (WCHAR));
940         LoadStringW (pt->hTheme, resourceId+1000,
941             pszColorNames->szTooltip,
942             sizeof (pszColorNames->szTooltip) / sizeof (WCHAR));
943     }
944     else
945         hr = E_PROP_ID_UNSUPPORTED;
946
947     MSSTYLES_CloseThemeFile(pt);
948     return hr;
949 }
950
951 /**********************************************************************
952  *      EnumThemeSizes                                     (UXTHEME.10)
953  *
954  * Enumerate theme colors available with a particular size
955  *
956  * PARAMS
957  *     pszThemeFileName    Path to a msstyles theme file
958  *     pszColorName        Theme color to enumerate available sizes
959  *                         If NULL the default theme color is used
960  *     dwSizeNum           Size index to retrieve, increment from 0
961  *     pszSizeNames        Output size names
962  *
963  * RETURNS
964  *     S_OK on success
965  *     E_PROP_ID_UNSUPPORTED when dwSizeName does not refer to a size
966  *          or when pszColorName does not refer to a valid color
967  *
968  * NOTES
969  * XP fails with E_POINTER when pszSizeNames points to a buffer smaller than 
970  * sizeof(THEMENAMES).
971  *
972  * Not very efficient that I'm opening & validating the theme every call, but
973  * this is undocumented and almost never called..
974  * (and this is how windows works too)
975  */
976 HRESULT WINAPI EnumThemeSizes(LPWSTR pszThemeFileName, LPWSTR pszColorName,
977                               DWORD dwSizeNum, PTHEMENAMES pszSizeNames)
978 {
979     PTHEME_FILE pt;
980     HRESULT hr;
981     LPWSTR tmp;
982     UINT resourceId = dwSizeNum + 3000;
983     TRACE("(%s,%s,%ld)\n", debugstr_w(pszThemeFileName),
984           debugstr_w(pszColorName), dwSizeNum);
985
986     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, pszColorName, NULL, &pt);
987     if(FAILED(hr)) return hr;
988
989     tmp = pt->pszAvailSizes;
990     while(dwSizeNum && *tmp) {
991         dwSizeNum--;
992         tmp += lstrlenW(tmp)+1;
993     }
994     if(!dwSizeNum && *tmp) {
995         TRACE("%s\n", debugstr_w(tmp));
996         lstrcpyW(pszSizeNames->szName, tmp);
997         LoadStringW (pt->hTheme, resourceId,
998             pszSizeNames->szDisplayName,
999             sizeof (pszSizeNames->szDisplayName) / sizeof (WCHAR));
1000         LoadStringW (pt->hTheme, resourceId+1000,
1001             pszSizeNames->szTooltip,
1002             sizeof (pszSizeNames->szTooltip) / sizeof (WCHAR));
1003     }
1004     else
1005         hr = E_PROP_ID_UNSUPPORTED;
1006
1007     MSSTYLES_CloseThemeFile(pt);
1008     return hr;
1009 }
1010
1011 /**********************************************************************
1012  *      ParseThemeIniFile                                  (UXTHEME.11)
1013  *
1014  * Enumerate data in a theme INI file.
1015  *
1016  * PARAMS
1017  *     pszIniFileName      Path to a theme ini file
1018  *     pszUnknown          Cannot be NULL, L"" is valid
1019  *     callback            Called for each found entry
1020  *     lpData              Passed through to callback
1021  *
1022  * RETURNS
1023  *     S_OK on success
1024  *     0x800706488 (Unknown property) when enumeration is canceled from callback
1025  *
1026  * NOTES
1027  * When pszUnknown is NULL the callback is never called, the value does not seem to surve
1028  * any other purpose
1029  */
1030 HRESULT WINAPI ParseThemeIniFile(LPCWSTR pszIniFileName, LPWSTR pszUnknown,
1031                                  ParseThemeIniFileProc callback, LPVOID lpData)
1032 {
1033     FIXME("%s %s: stub\n", debugstr_w(pszIniFileName), debugstr_w(pszUnknown));
1034     return ERROR_CALL_NOT_IMPLEMENTED;
1035 }
1036
1037 /**********************************************************************
1038  *      CheckThemeSignature                                (UXTHEME.29)
1039  *
1040  * Validates the signature of a theme file
1041  *
1042  * PARAMS
1043  *     pszIniFileName      Path to a theme file
1044  */
1045 HRESULT WINAPI CheckThemeSignature(LPCWSTR pszThemeFileName)
1046 {
1047     PTHEME_FILE pt;
1048     HRESULT hr;
1049     TRACE("(%s)\n", debugstr_w(pszThemeFileName));
1050     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, NULL, &pt);
1051     if(FAILED(hr))
1052         return hr;
1053     MSSTYLES_CloseThemeFile(pt);
1054     return S_OK;
1055 }