Implement RegisterExtensionInfo and RegisterMIMEInfo.
[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
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
29 #include "winreg.h"
30 #include "shlwapi.h"
31 #include "uxtheme.h"
32 #include "tmschema.h"
33
34 #include "uxthemedll.h"
35 #include "msstyles.h"
36
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);
40
41 /***********************************************************************
42  * Defines and global variables
43  */
44
45 static const WCHAR szThemeManager[] = {
46     'S','o','f','t','w','a','r','e','\\',
47     'M','i','c','r','o','s','o','f','t','\\',
48     'W','i','n','d','o','w','s','\\',
49     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
50     'T','h','e','m','e','M','a','n','a','g','e','r','\0'
51 };
52 static const WCHAR szThemeActive[] = {'T','h','e','m','e','A','c','t','i','v','e','\0'};
53 static const WCHAR szSizeName[] = {'S','i','z','e','N','a','m','e','\0'};
54 static const WCHAR szColorName[] = {'C','o','l','o','r','N','a','m','e','\0'};
55 static const WCHAR szDllName[] = {'D','l','l','N','a','m','e','\0'};
56
57 static const WCHAR szIniDocumentation[] = {'d','o','c','u','m','e','n','t','a','t','i','o','n','\0'};
58
59 HINSTANCE hDllInst;
60
61 DWORD dwThemeAppProperties = STAP_ALLOW_NONCLIENT | STAP_ALLOW_CONTROLS;
62 ATOM atWindowTheme;
63 ATOM atSubAppName;
64 ATOM atSubIdList;
65
66 BOOL bThemeActive = FALSE;
67 WCHAR szCurrentTheme[MAX_PATH];
68 WCHAR szCurrentColor[64];
69 WCHAR szCurrentSize[64];
70
71 /***********************************************************************/
72
73 /***********************************************************************
74  *      UXTHEME_LoadTheme
75  *
76  * Set the current active theme from the registry
77  */
78 void UXTHEME_LoadTheme()
79 {
80     HKEY hKey;
81     LONG buffsize;
82     HRESULT hr;
83     WCHAR tmp[10];
84     PTHEME_FILE pt;
85
86     /* Get current theme configuration */
87     if(!RegOpenKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
88         TRACE("Loading theme config\n");
89         buffsize = sizeof(tmp)/sizeof(tmp[0]);
90         if(!RegQueryValueExW(hKey, szThemeActive, NULL, NULL, (LPBYTE)tmp, &buffsize)) {
91             bThemeActive = (tmp[0] != '0');
92         }
93         else {
94             bThemeActive = FALSE;
95             TRACE("Failed to get ThemeActive: %ld\n", GetLastError());
96         }
97         buffsize = sizeof(szCurrentColor)/sizeof(szCurrentColor[0]);
98         if(RegQueryValueExW(hKey, szColorName, NULL, NULL, (LPBYTE)szCurrentColor, &buffsize))
99             szCurrentColor[0] = '\0';
100         buffsize = sizeof(szCurrentSize)/sizeof(szCurrentSize[0]);
101         if(RegQueryValueExW(hKey, szSizeName, NULL, NULL, (LPBYTE)szCurrentSize, &buffsize))
102             szCurrentSize[0] = '\0';
103         if(SHRegGetPathW(hKey, NULL, szDllName, szCurrentTheme, 0))
104             szCurrentTheme[0] = '\0';
105         RegCloseKey(hKey);
106     }
107     else
108         TRACE("Failed to open theme registry key\n");
109
110     if(bThemeActive) {
111         /* Make sure the theme requested is actually valid */
112         hr = MSSTYLES_OpenThemeFile(szCurrentTheme,
113                                     szCurrentColor[0]?szCurrentColor:NULL,
114                                     szCurrentSize[0]?szCurrentSize:NULL,
115                                     &pt);
116         if(FAILED(hr)) {
117             bThemeActive = FALSE;
118             szCurrentTheme[0] = '\0';
119             szCurrentColor[0] = '\0';
120             szCurrentSize[0] = '\0';
121         }
122         else {
123             /* Make sure the global color & size match the theme */
124             lstrcpynW(szCurrentColor, pt->pszSelectedColor, sizeof(szCurrentColor)/sizeof(szCurrentColor[0]));
125             lstrcpynW(szCurrentSize, pt->pszSelectedSize, sizeof(szCurrentSize)/sizeof(szCurrentSize[0]));
126
127             MSSTYLES_SetActiveTheme(pt);
128             TRACE("Theme active: %s %s %s\n", debugstr_w(szCurrentTheme),
129                 debugstr_w(szCurrentColor), debugstr_w(szCurrentSize));
130             MSSTYLES_CloseThemeFile(pt);
131         }
132     }
133     if(!bThemeActive) {
134         MSSTYLES_SetActiveTheme(NULL);
135         TRACE("Themeing not active\n");
136     }
137 }
138
139 /***********************************************************************
140  *      UXTHEME_SetActiveTheme
141  *
142  * Change the current active theme
143  */
144 HRESULT UXTHEME_SetActiveTheme(PTHEME_FILE tf)
145 {
146     HKEY hKey;
147     WCHAR tmp[2];
148     HRESULT hr;
149
150     hr = MSSTYLES_SetActiveTheme(tf);
151     if(FAILED(hr))
152         return hr;
153     if(tf) {
154         bThemeActive = TRUE;
155         lstrcpynW(szCurrentTheme, tf->szThemeFile, sizeof(szCurrentTheme)/sizeof(szCurrentTheme[0]));
156         lstrcpynW(szCurrentColor, tf->pszSelectedColor, sizeof(szCurrentColor)/sizeof(szCurrentColor[0]));
157         lstrcpynW(szCurrentSize, tf->pszSelectedSize, sizeof(szCurrentSize)/sizeof(szCurrentSize[0]));
158     }
159     else {
160         bThemeActive = FALSE;
161         szCurrentTheme[0] = '\0';
162         szCurrentColor[0] = '\0';
163         szCurrentSize[0] = '\0';
164     }
165
166     TRACE("Writing theme config to registry\n");
167     if(!RegCreateKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
168         tmp[0] = bThemeActive?'1':'0';
169         tmp[1] = '\0';
170         RegSetValueExW(hKey, szThemeActive, 0, REG_SZ, (const BYTE*)tmp, sizeof(WCHAR)*2);
171         if(bThemeActive) {
172             RegSetValueExW(hKey, szColorName, 0, REG_SZ, (const BYTE*)szCurrentColor, lstrlenW(szCurrentColor)+1);
173             RegSetValueExW(hKey, szSizeName, 0, REG_SZ, (const BYTE*)szCurrentSize, lstrlenW(szCurrentSize)+1);
174             RegSetValueExW(hKey, szDllName, 0, REG_SZ, (const BYTE*)szCurrentTheme, lstrlenW(szCurrentTheme)+1);
175         }
176         else {
177             RegDeleteValueW(hKey, szColorName);
178             RegDeleteValueW(hKey, szSizeName);
179             RegDeleteValueW(hKey, szDllName);
180
181         }
182         RegCloseKey(hKey);
183     }
184     else
185         TRACE("Failed to open theme registry key\n");
186     return hr;
187 }
188
189 /***********************************************************************
190  *      UXTHEME_InitSystem
191  */
192 void UXTHEME_InitSystem(HINSTANCE hInst)
193 {
194     static const WCHAR szWindowTheme[] = {
195         'u','x','_','t','h','e','m','e','\0'
196     };
197     static const WCHAR szSubAppName[] = {
198         'u','x','_','s','u','b','a','p','p','\0'
199     };
200     static const WCHAR szSubIdList[] = {
201         'u','x','_','s','u','b','i','d','l','s','t','\0'
202     };
203
204     hDllInst = hInst;
205
206     atWindowTheme = GlobalAddAtomW(szWindowTheme);
207     atSubAppName  = GlobalAddAtomW(szSubAppName);
208     atSubIdList   = GlobalAddAtomW(szSubIdList);
209
210     UXTHEME_LoadTheme();
211 }
212
213 /***********************************************************************
214  *      IsAppThemed                                         (UXTHEME.@)
215  */
216 BOOL WINAPI IsAppThemed(void)
217 {
218     return IsThemeActive();
219 }
220
221 /***********************************************************************
222  *      IsThemeActive                                       (UXTHEME.@)
223  */
224 BOOL WINAPI IsThemeActive(void)
225 {
226     TRACE("\n");
227     return bThemeActive;
228 }
229
230 /***********************************************************************
231  *      EnableTheming                                       (UXTHEME.@)
232  *
233  * NOTES
234  * This is a global and persistent change
235  */
236 HRESULT WINAPI EnableTheming(BOOL fEnable)
237 {
238     HKEY hKey;
239     WCHAR szEnabled[] = {'0','\0'};
240
241     TRACE("(%d)\n", fEnable);
242
243     if(fEnable != bThemeActive) {
244         bThemeActive = fEnable;
245         if(bThemeActive) szEnabled[0] = '1';
246         if(!RegOpenKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
247             RegSetValueExW(hKey, szThemeActive, 0, REG_SZ, (LPBYTE)szEnabled, sizeof(WCHAR));
248             RegCloseKey(hKey);
249         }
250         PostMessageW(HWND_BROADCAST, WM_THEMECHANGED, 0, 0);
251     }
252     return S_OK;
253 }
254
255 /***********************************************************************
256  *      UXTHEME_SetWindowProperty
257  *
258  * I'm using atoms as there may be large numbers of duplicated strings
259  * and they do the work of keeping memory down as a cause of that quite nicely
260  */
261 HRESULT UXTHEME_SetWindowProperty(HWND hwnd, ATOM aProp, LPCWSTR pszValue)
262 {
263     ATOM oldValue = (ATOM)(size_t)RemovePropW(hwnd, MAKEINTATOMW(aProp));
264     if(oldValue)
265         DeleteAtom(oldValue);
266     if(pszValue) {
267         ATOM atValue = AddAtomW(pszValue);
268         if(!atValue
269            || !SetPropW(hwnd, MAKEINTATOMW(aProp), (LPWSTR)MAKEINTATOMW(atValue))) {
270             HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
271             if(atValue) DeleteAtom(atValue);
272             return hr;
273         }
274     }
275     return S_OK;
276 }
277
278 LPWSTR UXTHEME_GetWindowProperty(HWND hwnd, ATOM aProp, LPWSTR pszBuffer, int dwLen)
279 {
280     ATOM atValue = (ATOM)(size_t)GetPropW(hwnd, MAKEINTATOMW(aProp));
281     if(atValue) {
282         if(GetAtomNameW(atValue, pszBuffer, dwLen))
283             return pszBuffer;
284         TRACE("property defined, but unable to get value\n");
285     }
286     return NULL;
287 }
288
289 /***********************************************************************
290  *      OpenThemeData                                       (UXTHEME.@)
291  */
292 HTHEME WINAPI OpenThemeData(HWND hwnd, LPCWSTR pszClassList)
293 {
294     WCHAR szAppBuff[256];
295     WCHAR szClassBuff[256];
296     LPCWSTR pszAppName;
297     LPCWSTR pszUseClassList;
298     HTHEME hTheme;
299     TRACE("(%p,%s)\n", hwnd, debugstr_w(pszClassList));
300     if(!bThemeActive)
301         return NULL;
302
303     pszAppName = UXTHEME_GetWindowProperty(hwnd, atSubAppName, szAppBuff, sizeof(szAppBuff)/sizeof(szAppBuff[0]));
304     /* If SetWindowTheme was used on the window, that overrides the class list passed to this function */
305     pszUseClassList = UXTHEME_GetWindowProperty(hwnd, atSubIdList, szClassBuff, sizeof(szClassBuff)/sizeof(szClassBuff[0]));
306     if(!pszUseClassList)
307         pszUseClassList = pszClassList;
308
309     hTheme = MSSTYLES_OpenThemeClass(pszAppName, pszUseClassList);
310     if(IsWindow(hwnd))
311         SetPropW(hwnd, MAKEINTATOMW(atWindowTheme), hTheme);
312     return hTheme;
313 }
314
315 /***********************************************************************
316  *      GetWindowTheme                                      (UXTHEME.@)
317  *
318  * Retrieve the last theme opened for a window
319  */
320 HTHEME WINAPI GetWindowTheme(HWND hwnd)
321 {
322     TRACE("(%p)\n", hwnd);
323     return GetPropW(hwnd, MAKEINTATOMW(atWindowTheme));
324 }
325
326 /***********************************************************************
327  *      SetWindowTheme                                      (UXTHEME.@)
328  *
329  * Persistent through the life of the window, even after themes change
330  */
331 HRESULT WINAPI SetWindowTheme(HWND hwnd, LPCWSTR pszSubAppName,
332                               LPCWSTR pszSubIdList)
333 {
334     HRESULT hr;
335     TRACE("(%p,%s,%s)\n", hwnd, debugstr_w(pszSubAppName),
336           debugstr_w(pszSubIdList));
337     hr = UXTHEME_SetWindowProperty(hwnd, atSubAppName, pszSubAppName);
338     if(SUCCEEDED(hr))
339         hr = UXTHEME_SetWindowProperty(hwnd, atSubIdList, pszSubIdList);
340     if(SUCCEEDED(hr))
341         PostMessageW(hwnd, WM_THEMECHANGED, 0, 0);
342     return hr;
343 }
344
345 /***********************************************************************
346  *      GetCurrentThemeName                                 (UXTHEME.@)
347  */
348 HRESULT WINAPI GetCurrentThemeName(LPWSTR pszThemeFileName, int dwMaxNameChars,
349                                    LPWSTR pszColorBuff, int cchMaxColorChars,
350                                    LPWSTR pszSizeBuff, int cchMaxSizeChars)
351 {
352     if(!bThemeActive)
353         return E_PROP_ID_UNSUPPORTED;
354     if(pszThemeFileName) lstrcpynW(pszThemeFileName, szCurrentTheme, dwMaxNameChars);
355     if(pszColorBuff) lstrcpynW(pszColorBuff, szCurrentColor, cchMaxColorChars);
356     if(pszSizeBuff) lstrcpynW(pszSizeBuff, szCurrentSize, cchMaxSizeChars);
357     return S_OK;
358 }
359
360 /***********************************************************************
361  *      GetThemeAppProperties                               (UXTHEME.@)
362  */
363 DWORD WINAPI GetThemeAppProperties(void)
364 {
365     return dwThemeAppProperties;
366 }
367
368 /***********************************************************************
369  *      SetThemeAppProperties                               (UXTHEME.@)
370  */
371 void WINAPI SetThemeAppProperties(DWORD dwFlags)
372 {
373     TRACE("(0x%08lx)\n", dwFlags);
374     dwThemeAppProperties = dwFlags;
375 }
376
377 /***********************************************************************
378  *      CloseThemeData                                      (UXTHEME.@)
379  */
380 HRESULT WINAPI CloseThemeData(HTHEME hTheme)
381 {
382     TRACE("(%p)\n", hTheme);
383     if(!hTheme)
384         return E_HANDLE;
385     return MSSTYLES_CloseThemeClass(hTheme);
386 }
387
388 /***********************************************************************
389  *      HitTestThemeBackground                              (UXTHEME.@)
390  */
391 HRESULT WINAPI HitTestThemeBackground(HTHEME hTheme, HDC hdc, int iPartId,
392                                      int iStateId, DWORD dwOptions,
393                                      const RECT *pRect, HRGN hrgn,
394                                      POINT ptTest, WORD *pwHitTestCode)
395 {
396     FIXME("%d %d 0x%08lx: stub\n", iPartId, iStateId, dwOptions);
397     if(!hTheme)
398         return E_HANDLE;
399     return ERROR_CALL_NOT_IMPLEMENTED;
400 }
401
402 /***********************************************************************
403  *      IsThemePartDefined                                  (UXTHEME.@)
404  */
405 BOOL WINAPI IsThemePartDefined(HTHEME hTheme, int iPartId, int iStateId)
406 {
407     TRACE("(%p,%d,%d)\n", hTheme, iPartId, iStateId);
408     if(!hTheme) {
409         SetLastError(E_HANDLE);
410         return FALSE;
411     }
412     if(MSSTYLES_FindPartState(hTheme, iPartId, iStateId, NULL))
413         return TRUE;
414     return FALSE;
415 }
416
417 /***********************************************************************
418  *      GetThemeDocumentationProperty                       (UXTHEME.@)
419  *
420  * Try and retrieve the documentation property from string resources
421  * if that fails, get it from the [documentation] section of themes.ini
422  */
423 HRESULT WINAPI GetThemeDocumentationProperty(LPCWSTR pszThemeName,
424                                              LPCWSTR pszPropertyName,
425                                              LPWSTR pszValueBuff,
426                                              int cchMaxValChars)
427 {
428     const WORD wDocToRes[] = {
429         TMT_DISPLAYNAME,5000,
430         TMT_TOOLTIP,5001,
431         TMT_COMPANY,5002,
432         TMT_AUTHOR,5003,
433         TMT_COPYRIGHT,5004,
434         TMT_URL,5005,
435         TMT_VERSION,5006,
436         TMT_DESCRIPTION,5007
437     };
438
439     PTHEME_FILE pt;
440     HRESULT hr;
441     unsigned int i;
442     int iDocId;
443     TRACE("(%s,%s,%p,%d)\n", debugstr_w(pszThemeName), debugstr_w(pszPropertyName),
444           pszValueBuff, cchMaxValChars);
445
446     hr = MSSTYLES_OpenThemeFile(pszThemeName, NULL, NULL, &pt);
447     if(FAILED(hr)) return hr;
448
449     /* Try to load from string resources */
450     hr = E_PROP_ID_UNSUPPORTED;
451     if(MSSTYLES_LookupProperty(pszPropertyName, NULL, &iDocId)) {
452         for(i=0; i<sizeof(wDocToRes)/sizeof(wDocToRes[0]); i+=2) {
453             if(wDocToRes[i] == iDocId) {
454                 if(LoadStringW(pt->hTheme, wDocToRes[i+1], pszValueBuff, cchMaxValChars)) {
455                     hr = S_OK;
456                     break;
457                 }
458             }
459         }
460     }
461     /* If loading from string resource failed, try getting it from the theme.ini */
462     if(FAILED(hr)) {
463         PUXINI_FILE uf = MSSTYLES_GetThemeIni(pt);
464         if(UXINI_FindSection(uf, szIniDocumentation)) {
465             LPCWSTR lpValue;
466             DWORD dwLen;
467             if(UXINI_FindValue(uf, pszPropertyName, &lpValue, &dwLen)) {
468                 lstrcpynW(pszValueBuff, lpValue, min(dwLen+1,cchMaxValChars));
469                 hr = S_OK;
470             }
471         }
472         UXINI_CloseINI(uf);
473     }
474
475     MSSTYLES_CloseThemeFile(pt);
476     return hr;
477 }
478
479 /**********************************************************************
480  *      Undocumented functions
481  */
482
483 /**********************************************************************
484  *      QueryThemeServices                                 (UXTHEME.1)
485  *
486  * RETURNS
487  *     some kind of status flag
488  */
489 DWORD WINAPI QueryThemeServices()
490 {
491     FIXME("stub\n");
492     return 3; /* This is what is returned under XP in most cases */
493 }
494
495
496 /**********************************************************************
497  *      OpenThemeFile                                      (UXTHEME.2)
498  *
499  * Opens a theme file, which can be used to change the current theme, etc
500  *
501  * PARAMS
502  *     pszThemeFileName    Path to a msstyles theme file
503  *     pszColorName        Color defined in the theme, eg. NormalColor
504  *     pszSizeName         Size defined in the theme, eg. NormalSize
505  *     hThemeFile          Handle to theme file
506  */
507 HRESULT WINAPI OpenThemeFile(LPCWSTR pszThemeFileName, LPCWSTR pszColorName,
508                              LPCWSTR pszSizeName, HTHEMEFILE *hThemeFile,
509                              DWORD unknown)
510 {
511     TRACE("(%s,%s,%s,%p,%ld)\n", debugstr_w(pszThemeFileName),
512           debugstr_w(pszColorName), debugstr_w(pszSizeName),
513           hThemeFile, unknown);
514     return MSSTYLES_OpenThemeFile(pszThemeFileName, pszColorName, pszSizeName, (PTHEME_FILE*)hThemeFile);
515 }
516
517 /**********************************************************************
518  *      CloseThemeFile                                     (UXTHEME.3)
519  *
520  * Releases theme file handle returned by OpenThemeFile
521  *
522  * PARAMS
523  *     hThemeFile           Handle to theme file
524  */
525 HRESULT WINAPI CloseThemeFile(HTHEMEFILE hThemeFile)
526 {
527     TRACE("(%p)\n", hThemeFile);
528     MSSTYLES_CloseThemeFile(hThemeFile);
529     return S_OK;
530 }
531
532 /**********************************************************************
533  *      ApplyTheme                                         (UXTHEME.4)
534  *
535  * Set a theme file to be the currently active theme
536  *
537  * PARAMS
538  *     hThemeFile           Handle to theme file
539  *     unknown              See notes
540  *     hWnd                 Window requesting the theme change
541  *
542  * NOTES
543  * I'm not sure what the second parameter is (the datatype is likely wrong), other then this:
544  * Under XP if I pass
545  * char b[] = "";
546  *   the theme is applied with the screen redrawing really badly (flickers)
547  * char b[] = "\0"; where \0 can be one or more of any character, makes no difference
548  *   the theme is applied smoothly (screen does not flicker)
549  * char *b = "\0" or NULL; where \0 can be zero or more of any character, makes no difference
550  *   the function fails returning invalid parameter...very strange
551  */
552 HRESULT WINAPI ApplyTheme(HTHEMEFILE hThemeFile, char *unknown, HWND hWnd)
553 {
554     HRESULT hr;
555     TRACE("(%p,%s,%p)\n", hThemeFile, unknown, hWnd);
556     hr = UXTHEME_SetActiveTheme(hThemeFile);
557     PostMessageW(HWND_BROADCAST, WM_THEMECHANGED, 0, 0);
558     return hr;
559 }
560
561 /**********************************************************************
562  *      GetThemeDefaults                                   (UXTHEME.7)
563  *
564  * Get the default color & size for a theme
565  *
566  * PARAMS
567  *     pszThemeFileName    Path to a msstyles theme file
568  *     pszColorName        Buffer to receive the default color name
569  *     dwColorNameLen      Length, in characters, of color name buffer
570  *     pszSizeName         Buffer to receive the default size name
571  *     dwSizeNameLen       Length, in characters, of size name buffer
572  */
573 HRESULT WINAPI GetThemeDefaults(LPCWSTR pszThemeFileName, LPWSTR pszColorName,
574                                 DWORD dwColorNameLen, LPWSTR pszSizeName,
575                                 DWORD dwSizeNameLen)
576 {
577     PTHEME_FILE pt;
578     HRESULT hr;
579     TRACE("(%s,%p,%ld,%p,%ld)\n", debugstr_w(pszThemeFileName),
580           pszColorName, dwColorNameLen,
581           pszSizeName, dwSizeNameLen);
582
583     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, NULL, &pt);
584     if(FAILED(hr)) return hr;
585
586     lstrcpynW(pszColorName, pt->pszSelectedColor, dwColorNameLen);
587     lstrcpynW(pszSizeName, pt->pszSelectedSize, dwSizeNameLen);
588
589     MSSTYLES_CloseThemeFile(pt);
590     return S_OK;
591 }
592
593 /**********************************************************************
594  *      EnumThemes                                         (UXTHEME.8)
595  *
596  * Enumerate available themes, calls specified EnumThemeProc for each
597  * theme found. Passes lpData through to callback function.
598  *
599  * PARAMS
600  *     pszThemePath        Path containing themes
601  *     callback            Called for each theme found in path
602  *     lpData              Passed through to callback
603  */
604 HRESULT WINAPI EnumThemes(LPCWSTR pszThemePath, EnumThemeProc callback,
605                           LPVOID lpData)
606 {
607     WCHAR szDir[MAX_PATH];
608     WCHAR szPath[MAX_PATH];
609     static const WCHAR szStar[] = {'*','.','*','\0'};
610     static const WCHAR szFormat[] = {'%','s','%','s','\\','%','s','.','m','s','s','t','y','l','e','s','\0'};
611     static const WCHAR szDisplayName[] = {'d','i','s','p','l','a','y','n','a','m','e','\0'};
612     static const WCHAR szTooltip[] = {'t','o','o','l','t','i','p','\0'};
613     WCHAR szName[60];
614     WCHAR szTip[60];
615     HANDLE hFind;
616     WIN32_FIND_DATAW wfd;
617     HRESULT hr;
618
619     TRACE("(%s,%p,%p)\n", debugstr_w(pszThemePath), callback, lpData);
620
621     if(!pszThemePath || !callback)
622         return E_POINTER;
623
624     lstrcpyW(szDir, pszThemePath);
625     PathAddBackslashW(szDir);
626
627     lstrcpyW(szPath, szDir);
628     lstrcatW(szPath, szStar);
629     TRACE("searching %s\n", debugstr_w(szPath));
630
631     hFind = FindFirstFileW(szPath, &wfd);
632     if(hFind != INVALID_HANDLE_VALUE) {
633         do {
634             if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
635                && !(wfd.cFileName[0] == '.' && ((wfd.cFileName[1] == '.' && wfd.cFileName[2] == 0) || wfd.cFileName[1] == 0))) {
636                 wsprintfW(szPath, szFormat, szDir, wfd.cFileName, wfd.cFileName);
637
638                 hr = GetThemeDocumentationProperty(szPath, szDisplayName, szName, sizeof(szName)/sizeof(szName[0]));
639                 if(SUCCEEDED(hr))
640                     hr = GetThemeDocumentationProperty(szPath, szTooltip, szTip, sizeof(szTip)/sizeof(szTip[0]));
641                 if(SUCCEEDED(hr)) {
642                     TRACE("callback(%s,%s,%s,%p)\n", debugstr_w(szPath), debugstr_w(szName), debugstr_w(szTip), lpData);
643                     if(!callback(NULL, szPath, szName, szTip, NULL, lpData)) {
644                         TRACE("callback ended enum\n");
645                         break;
646                     }
647                 }
648             }
649         } while(FindNextFileW(hFind, &wfd));
650         FindClose(hFind);
651     }
652     return S_OK;
653 }
654
655
656 /**********************************************************************
657  *      EnumThemeColors                                    (UXTHEME.9)
658  *
659  * Enumerate theme colors available with a particular size
660  *
661  * PARAMS
662  *     pszThemeFileName    Path to a msstyles theme file
663  *     pszSizeName         Theme size to enumerate available colors
664  *                         If NULL the default theme size is used
665  *     dwColorNum          Color index to retrieve, increment from 0
666  *     pszColorName        Output color name
667  *
668  * RETURNS
669  *     S_OK on success
670  *     E_PROP_ID_UNSUPPORTED when dwColorName does not refer to a color
671  *          or when pszSizeName does not refer to a valid size
672  *
673  * NOTES
674  * XP fails with E_POINTER when pszColorName points to a buffer smaller then 605
675  * characters
676  *
677  * Not very efficient that I'm opening & validating the theme every call, but
678  * this is undocumented and almost never called..
679  * (and this is how windows works too)
680  */
681 HRESULT WINAPI EnumThemeColors(LPWSTR pszThemeFileName, LPWSTR pszSizeName,
682                                DWORD dwColorNum, LPWSTR pszColorName)
683 {
684     PTHEME_FILE pt;
685     HRESULT hr;
686     LPWSTR tmp;
687     TRACE("(%s,%s,%ld)\n", debugstr_w(pszThemeFileName),
688           debugstr_w(pszSizeName), dwColorNum);
689
690     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, pszSizeName, &pt);
691     if(FAILED(hr)) return hr;
692
693     tmp = pt->pszAvailColors;
694     while(dwColorNum && *tmp) {
695         dwColorNum--;
696         tmp += lstrlenW(tmp)+1;
697     }
698     if(!dwColorNum && *tmp) {
699         TRACE("%s\n", debugstr_w(tmp));
700         lstrcpyW(pszColorName, tmp);
701     }
702     else
703         hr = E_PROP_ID_UNSUPPORTED;
704
705     MSSTYLES_CloseThemeFile(pt);
706     return hr;
707 }
708
709 /**********************************************************************
710  *      EnumThemeSizes                                     (UXTHEME.10)
711  *
712  * Enumerate theme colors available with a particular size
713  *
714  * PARAMS
715  *     pszThemeFileName    Path to a msstyles theme file
716  *     pszColorName        Theme color to enumerate available sizes
717  *                         If NULL the default theme color is used
718  *     dwSizeNum           Size index to retrieve, increment from 0
719  *     pszSizeName         Output size name
720  *
721  * RETURNS
722  *     S_OK on success
723  *     E_PROP_ID_UNSUPPORTED when dwSizeName does not refer to a size
724  *          or when pszColorName does not refer to a valid color
725  *
726  * NOTES
727  * XP fails with E_POINTER when pszSizeName points to a buffer smaller then 605
728  * characters
729  *
730  * Not very efficient that I'm opening & validating the theme every call, but
731  * this is undocumented and almost never called..
732  * (and this is how windows works too)
733  */
734 HRESULT WINAPI EnumThemeSizes(LPWSTR pszThemeFileName, LPWSTR pszColorName,
735                               DWORD dwSizeNum, LPWSTR pszSizeName)
736 {
737     PTHEME_FILE pt;
738     HRESULT hr;
739     LPWSTR tmp;
740     TRACE("(%s,%s,%ld)\n", debugstr_w(pszThemeFileName),
741           debugstr_w(pszColorName), dwSizeNum);
742
743     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, pszColorName, NULL, &pt);
744     if(FAILED(hr)) return hr;
745
746     tmp = pt->pszAvailSizes;
747     while(dwSizeNum && *tmp) {
748         dwSizeNum--;
749         tmp += lstrlenW(tmp)+1;
750     }
751     if(!dwSizeNum && *tmp) {
752         TRACE("%s\n", debugstr_w(tmp));
753         lstrcpyW(pszSizeName, tmp);
754     }
755     else
756         hr = E_PROP_ID_UNSUPPORTED;
757
758     MSSTYLES_CloseThemeFile(pt);
759     return hr;
760 }
761
762 /**********************************************************************
763  *      ParseThemeIniFile                                  (UXTHEME.11)
764  *
765  * Enumerate data in a theme INI file.
766  *
767  * PARAMS
768  *     pszIniFileName      Path to a theme ini file
769  *     pszUnknown          Cannot be NULL, L"" is valid
770  *     callback            Called for each found entry
771  *     lpData              Passed through to callback
772  *
773  * RETURNS
774  *     S_OK on success
775  *     0x800706488 (Unknown property) when enumeration is canceled from callback
776  *
777  * NOTES
778  * When pszUnknown is NULL the callback is never called, the value does not seem to surve
779  * any other purpose
780  */
781 HRESULT WINAPI ParseThemeIniFile(LPCWSTR pszIniFileName, LPWSTR pszUnknown,
782                                  ParseThemeIniFileProc callback, LPVOID lpData)
783 {
784     FIXME("%s %s: stub\n", debugstr_w(pszIniFileName), debugstr_w(pszUnknown));
785     return ERROR_CALL_NOT_IMPLEMENTED;
786 }
787
788 /**********************************************************************
789  *      CheckThemeSignature                                (UXTHEME.29)
790  *
791  * Validates the signature of a theme file
792  *
793  * PARAMS
794  *     pszIniFileName      Path to a theme file
795  */
796 HRESULT WINAPI CheckThemeSignature(LPCWSTR pszThemeFileName)
797 {
798     PTHEME_FILE pt;
799     HRESULT hr;
800     TRACE("(%s)\n", debugstr_w(pszThemeFileName));
801     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, NULL, &pt);
802     if(FAILED(hr))
803         return hr;
804     MSSTYLES_CloseThemeFile(pt);
805     return S_OK;
806 }