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