Fixed white space handling on the last line (reported by Jeremy
[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 = NULL;
328     TRACE("(%p,%s)", hwnd, debugstr_w(pszClassList));
329
330     if(bThemeActive)
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 (pszUseClassList)
339             hTheme = MSSTYLES_OpenThemeClass(pszAppName, pszUseClassList);
340     }
341     if(IsWindow(hwnd))
342         SetPropW(hwnd, MAKEINTATOMW(atWindowTheme), hTheme);
343     TRACE(" = %p\n", hTheme);
344     return hTheme;
345 }
346
347 /***********************************************************************
348  *      GetWindowTheme                                      (UXTHEME.@)
349  *
350  * Retrieve the last theme opened for a window
351  */
352 HTHEME WINAPI GetWindowTheme(HWND hwnd)
353 {
354     TRACE("(%p)\n", hwnd);
355     return GetPropW(hwnd, MAKEINTATOMW(atWindowTheme));
356 }
357
358 /***********************************************************************
359  *      SetWindowTheme                                      (UXTHEME.@)
360  *
361  * Persistent through the life of the window, even after themes change
362  */
363 HRESULT WINAPI SetWindowTheme(HWND hwnd, LPCWSTR pszSubAppName,
364                               LPCWSTR pszSubIdList)
365 {
366     HRESULT hr;
367     TRACE("(%p,%s,%s)\n", hwnd, debugstr_w(pszSubAppName),
368           debugstr_w(pszSubIdList));
369     hr = UXTHEME_SetWindowProperty(hwnd, atSubAppName, pszSubAppName);
370     if(SUCCEEDED(hr))
371         hr = UXTHEME_SetWindowProperty(hwnd, atSubIdList, pszSubIdList);
372     if(SUCCEEDED(hr))
373         UXTHEME_broadcast_msg (hwnd, WM_THEMECHANGED);
374     return hr;
375 }
376
377 /***********************************************************************
378  *      GetCurrentThemeName                                 (UXTHEME.@)
379  */
380 HRESULT WINAPI GetCurrentThemeName(LPWSTR pszThemeFileName, int dwMaxNameChars,
381                                    LPWSTR pszColorBuff, int cchMaxColorChars,
382                                    LPWSTR pszSizeBuff, int cchMaxSizeChars)
383 {
384     if(!bThemeActive)
385         return E_PROP_ID_UNSUPPORTED;
386     if(pszThemeFileName) lstrcpynW(pszThemeFileName, szCurrentTheme, dwMaxNameChars);
387     if(pszColorBuff) lstrcpynW(pszColorBuff, szCurrentColor, cchMaxColorChars);
388     if(pszSizeBuff) lstrcpynW(pszSizeBuff, szCurrentSize, cchMaxSizeChars);
389     return S_OK;
390 }
391
392 /***********************************************************************
393  *      GetThemeAppProperties                               (UXTHEME.@)
394  */
395 DWORD WINAPI GetThemeAppProperties(void)
396 {
397     return dwThemeAppProperties;
398 }
399
400 /***********************************************************************
401  *      SetThemeAppProperties                               (UXTHEME.@)
402  */
403 void WINAPI SetThemeAppProperties(DWORD dwFlags)
404 {
405     TRACE("(0x%08lx)\n", dwFlags);
406     dwThemeAppProperties = dwFlags;
407 }
408
409 /***********************************************************************
410  *      CloseThemeData                                      (UXTHEME.@)
411  */
412 HRESULT WINAPI CloseThemeData(HTHEME hTheme)
413 {
414     TRACE("(%p)\n", hTheme);
415     if(!hTheme)
416         return E_HANDLE;
417     return MSSTYLES_CloseThemeClass(hTheme);
418 }
419
420 /***********************************************************************
421  *      HitTestThemeBackground                              (UXTHEME.@)
422  */
423 HRESULT WINAPI HitTestThemeBackground(HTHEME hTheme, HDC hdc, int iPartId,
424                                      int iStateId, DWORD dwOptions,
425                                      const RECT *pRect, HRGN hrgn,
426                                      POINT ptTest, WORD *pwHitTestCode)
427 {
428     FIXME("%d %d 0x%08lx: stub\n", iPartId, iStateId, dwOptions);
429     if(!hTheme)
430         return E_HANDLE;
431     return ERROR_CALL_NOT_IMPLEMENTED;
432 }
433
434 /***********************************************************************
435  *      IsThemePartDefined                                  (UXTHEME.@)
436  */
437 BOOL WINAPI IsThemePartDefined(HTHEME hTheme, int iPartId, int iStateId)
438 {
439     TRACE("(%p,%d,%d)\n", hTheme, iPartId, iStateId);
440     if(!hTheme) {
441         SetLastError(E_HANDLE);
442         return FALSE;
443     }
444     if(MSSTYLES_FindPartState(hTheme, iPartId, iStateId, NULL))
445         return TRUE;
446     return FALSE;
447 }
448
449 /***********************************************************************
450  *      GetThemeDocumentationProperty                       (UXTHEME.@)
451  *
452  * Try and retrieve the documentation property from string resources
453  * if that fails, get it from the [documentation] section of themes.ini
454  */
455 HRESULT WINAPI GetThemeDocumentationProperty(LPCWSTR pszThemeName,
456                                              LPCWSTR pszPropertyName,
457                                              LPWSTR pszValueBuff,
458                                              int cchMaxValChars)
459 {
460     const WORD wDocToRes[] = {
461         TMT_DISPLAYNAME,5000,
462         TMT_TOOLTIP,5001,
463         TMT_COMPANY,5002,
464         TMT_AUTHOR,5003,
465         TMT_COPYRIGHT,5004,
466         TMT_URL,5005,
467         TMT_VERSION,5006,
468         TMT_DESCRIPTION,5007
469     };
470
471     PTHEME_FILE pt;
472     HRESULT hr;
473     unsigned int i;
474     int iDocId;
475     TRACE("(%s,%s,%p,%d)\n", debugstr_w(pszThemeName), debugstr_w(pszPropertyName),
476           pszValueBuff, cchMaxValChars);
477
478     hr = MSSTYLES_OpenThemeFile(pszThemeName, NULL, NULL, &pt);
479     if(FAILED(hr)) return hr;
480
481     /* Try to load from string resources */
482     hr = E_PROP_ID_UNSUPPORTED;
483     if(MSSTYLES_LookupProperty(pszPropertyName, NULL, &iDocId)) {
484         for(i=0; i<sizeof(wDocToRes)/sizeof(wDocToRes[0]); i+=2) {
485             if(wDocToRes[i] == iDocId) {
486                 if(LoadStringW(pt->hTheme, wDocToRes[i+1], pszValueBuff, cchMaxValChars)) {
487                     hr = S_OK;
488                     break;
489                 }
490             }
491         }
492     }
493     /* If loading from string resource failed, try getting it from the theme.ini */
494     if(FAILED(hr)) {
495         PUXINI_FILE uf = MSSTYLES_GetThemeIni(pt);
496         if(UXINI_FindSection(uf, szIniDocumentation)) {
497             LPCWSTR lpValue;
498             DWORD dwLen;
499             if(UXINI_FindValue(uf, pszPropertyName, &lpValue, &dwLen)) {
500                 lstrcpynW(pszValueBuff, lpValue, min(dwLen+1,cchMaxValChars));
501                 hr = S_OK;
502             }
503         }
504         UXINI_CloseINI(uf);
505     }
506
507     MSSTYLES_CloseThemeFile(pt);
508     return hr;
509 }
510
511 /**********************************************************************
512  *      Undocumented functions
513  */
514
515 /**********************************************************************
516  *      QueryThemeServices                                 (UXTHEME.1)
517  *
518  * RETURNS
519  *     some kind of status flag
520  */
521 DWORD WINAPI QueryThemeServices()
522 {
523     FIXME("stub\n");
524     return 3; /* This is what is returned under XP in most cases */
525 }
526
527
528 /**********************************************************************
529  *      OpenThemeFile                                      (UXTHEME.2)
530  *
531  * Opens a theme file, which can be used to change the current theme, etc
532  *
533  * PARAMS
534  *     pszThemeFileName    Path to a msstyles theme file
535  *     pszColorName        Color defined in the theme, eg. NormalColor
536  *     pszSizeName         Size defined in the theme, eg. NormalSize
537  *     hThemeFile          Handle to theme file
538  */
539 HRESULT WINAPI OpenThemeFile(LPCWSTR pszThemeFileName, LPCWSTR pszColorName,
540                              LPCWSTR pszSizeName, HTHEMEFILE *hThemeFile,
541                              DWORD unknown)
542 {
543     TRACE("(%s,%s,%s,%p,%ld)\n", debugstr_w(pszThemeFileName),
544           debugstr_w(pszColorName), debugstr_w(pszSizeName),
545           hThemeFile, unknown);
546     return MSSTYLES_OpenThemeFile(pszThemeFileName, pszColorName, pszSizeName, (PTHEME_FILE*)hThemeFile);
547 }
548
549 /**********************************************************************
550  *      CloseThemeFile                                     (UXTHEME.3)
551  *
552  * Releases theme file handle returned by OpenThemeFile
553  *
554  * PARAMS
555  *     hThemeFile           Handle to theme file
556  */
557 HRESULT WINAPI CloseThemeFile(HTHEMEFILE hThemeFile)
558 {
559     TRACE("(%p)\n", hThemeFile);
560     MSSTYLES_CloseThemeFile(hThemeFile);
561     return S_OK;
562 }
563
564 /**********************************************************************
565  *      ApplyTheme                                         (UXTHEME.4)
566  *
567  * Set a theme file to be the currently active theme
568  *
569  * PARAMS
570  *     hThemeFile           Handle to theme file
571  *     unknown              See notes
572  *     hWnd                 Window requesting the theme change
573  *
574  * NOTES
575  * I'm not sure what the second parameter is (the datatype is likely wrong), other then this:
576  * Under XP if I pass
577  * char b[] = "";
578  *   the theme is applied with the screen redrawing really badly (flickers)
579  * char b[] = "\0"; where \0 can be one or more of any character, makes no difference
580  *   the theme is applied smoothly (screen does not flicker)
581  * char *b = "\0" or NULL; where \0 can be zero or more of any character, makes no difference
582  *   the function fails returning invalid parameter...very strange
583  */
584 HRESULT WINAPI ApplyTheme(HTHEMEFILE hThemeFile, char *unknown, HWND hWnd)
585 {
586     HRESULT hr;
587     TRACE("(%p,%s,%p)\n", hThemeFile, unknown, hWnd);
588     hr = UXTHEME_SetActiveTheme(hThemeFile);
589     UXTHEME_broadcast_msg (NULL, WM_THEMECHANGED);
590     return hr;
591 }
592
593 /**********************************************************************
594  *      GetThemeDefaults                                   (UXTHEME.7)
595  *
596  * Get the default color & size for a theme
597  *
598  * PARAMS
599  *     pszThemeFileName    Path to a msstyles theme file
600  *     pszColorName        Buffer to receive the default color name
601  *     dwColorNameLen      Length, in characters, of color name buffer
602  *     pszSizeName         Buffer to receive the default size name
603  *     dwSizeNameLen       Length, in characters, of size name buffer
604  */
605 HRESULT WINAPI GetThemeDefaults(LPCWSTR pszThemeFileName, LPWSTR pszColorName,
606                                 DWORD dwColorNameLen, LPWSTR pszSizeName,
607                                 DWORD dwSizeNameLen)
608 {
609     PTHEME_FILE pt;
610     HRESULT hr;
611     TRACE("(%s,%p,%ld,%p,%ld)\n", debugstr_w(pszThemeFileName),
612           pszColorName, dwColorNameLen,
613           pszSizeName, dwSizeNameLen);
614
615     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, NULL, &pt);
616     if(FAILED(hr)) return hr;
617
618     lstrcpynW(pszColorName, pt->pszSelectedColor, dwColorNameLen);
619     lstrcpynW(pszSizeName, pt->pszSelectedSize, dwSizeNameLen);
620
621     MSSTYLES_CloseThemeFile(pt);
622     return S_OK;
623 }
624
625 /**********************************************************************
626  *      EnumThemes                                         (UXTHEME.8)
627  *
628  * Enumerate available themes, calls specified EnumThemeProc for each
629  * theme found. Passes lpData through to callback function.
630  *
631  * PARAMS
632  *     pszThemePath        Path containing themes
633  *     callback            Called for each theme found in path
634  *     lpData              Passed through to callback
635  */
636 HRESULT WINAPI EnumThemes(LPCWSTR pszThemePath, EnumThemeProc callback,
637                           LPVOID lpData)
638 {
639     WCHAR szDir[MAX_PATH];
640     WCHAR szPath[MAX_PATH];
641     static const WCHAR szStar[] = {'*','.','*','\0'};
642     static const WCHAR szFormat[] = {'%','s','%','s','\\','%','s','.','m','s','s','t','y','l','e','s','\0'};
643     static const WCHAR szDisplayName[] = {'d','i','s','p','l','a','y','n','a','m','e','\0'};
644     static const WCHAR szTooltip[] = {'t','o','o','l','t','i','p','\0'};
645     WCHAR szName[60];
646     WCHAR szTip[60];
647     HANDLE hFind;
648     WIN32_FIND_DATAW wfd;
649     HRESULT hr;
650
651     TRACE("(%s,%p,%p)\n", debugstr_w(pszThemePath), callback, lpData);
652
653     if(!pszThemePath || !callback)
654         return E_POINTER;
655
656     lstrcpyW(szDir, pszThemePath);
657     PathAddBackslashW(szDir);
658
659     lstrcpyW(szPath, szDir);
660     lstrcatW(szPath, szStar);
661     TRACE("searching %s\n", debugstr_w(szPath));
662
663     hFind = FindFirstFileW(szPath, &wfd);
664     if(hFind != INVALID_HANDLE_VALUE) {
665         do {
666             if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
667                && !(wfd.cFileName[0] == '.' && ((wfd.cFileName[1] == '.' && wfd.cFileName[2] == 0) || wfd.cFileName[1] == 0))) {
668                 wsprintfW(szPath, szFormat, szDir, wfd.cFileName, wfd.cFileName);
669
670                 hr = GetThemeDocumentationProperty(szPath, szDisplayName, szName, sizeof(szName)/sizeof(szName[0]));
671                 if(SUCCEEDED(hr))
672                     hr = GetThemeDocumentationProperty(szPath, szTooltip, szTip, sizeof(szTip)/sizeof(szTip[0]));
673                 if(SUCCEEDED(hr)) {
674                     TRACE("callback(%s,%s,%s,%p)\n", debugstr_w(szPath), debugstr_w(szName), debugstr_w(szTip), lpData);
675                     if(!callback(NULL, szPath, szName, szTip, NULL, lpData)) {
676                         TRACE("callback ended enum\n");
677                         break;
678                     }
679                 }
680             }
681         } while(FindNextFileW(hFind, &wfd));
682         FindClose(hFind);
683     }
684     return S_OK;
685 }
686
687
688 /**********************************************************************
689  *      EnumThemeColors                                    (UXTHEME.9)
690  *
691  * Enumerate theme colors available with a particular size
692  *
693  * PARAMS
694  *     pszThemeFileName    Path to a msstyles theme file
695  *     pszSizeName         Theme size to enumerate available colors
696  *                         If NULL the default theme size is used
697  *     dwColorNum          Color index to retrieve, increment from 0
698  *     pszColorName        Output color name
699  *
700  * RETURNS
701  *     S_OK on success
702  *     E_PROP_ID_UNSUPPORTED when dwColorName does not refer to a color
703  *          or when pszSizeName does not refer to a valid size
704  *
705  * NOTES
706  * XP fails with E_POINTER when pszColorName points to a buffer smaller then 605
707  * characters
708  *
709  * Not very efficient that I'm opening & validating the theme every call, but
710  * this is undocumented and almost never called..
711  * (and this is how windows works too)
712  */
713 HRESULT WINAPI EnumThemeColors(LPWSTR pszThemeFileName, LPWSTR pszSizeName,
714                                DWORD dwColorNum, LPWSTR pszColorName)
715 {
716     PTHEME_FILE pt;
717     HRESULT hr;
718     LPWSTR tmp;
719     TRACE("(%s,%s,%ld)\n", debugstr_w(pszThemeFileName),
720           debugstr_w(pszSizeName), dwColorNum);
721
722     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, pszSizeName, &pt);
723     if(FAILED(hr)) return hr;
724
725     tmp = pt->pszAvailColors;
726     while(dwColorNum && *tmp) {
727         dwColorNum--;
728         tmp += lstrlenW(tmp)+1;
729     }
730     if(!dwColorNum && *tmp) {
731         TRACE("%s\n", debugstr_w(tmp));
732         lstrcpyW(pszColorName, tmp);
733     }
734     else
735         hr = E_PROP_ID_UNSUPPORTED;
736
737     MSSTYLES_CloseThemeFile(pt);
738     return hr;
739 }
740
741 /**********************************************************************
742  *      EnumThemeSizes                                     (UXTHEME.10)
743  *
744  * Enumerate theme colors available with a particular size
745  *
746  * PARAMS
747  *     pszThemeFileName    Path to a msstyles theme file
748  *     pszColorName        Theme color to enumerate available sizes
749  *                         If NULL the default theme color is used
750  *     dwSizeNum           Size index to retrieve, increment from 0
751  *     pszSizeName         Output size name
752  *
753  * RETURNS
754  *     S_OK on success
755  *     E_PROP_ID_UNSUPPORTED when dwSizeName does not refer to a size
756  *          or when pszColorName does not refer to a valid color
757  *
758  * NOTES
759  * XP fails with E_POINTER when pszSizeName points to a buffer smaller then 605
760  * characters
761  *
762  * Not very efficient that I'm opening & validating the theme every call, but
763  * this is undocumented and almost never called..
764  * (and this is how windows works too)
765  */
766 HRESULT WINAPI EnumThemeSizes(LPWSTR pszThemeFileName, LPWSTR pszColorName,
767                               DWORD dwSizeNum, LPWSTR pszSizeName)
768 {
769     PTHEME_FILE pt;
770     HRESULT hr;
771     LPWSTR tmp;
772     TRACE("(%s,%s,%ld)\n", debugstr_w(pszThemeFileName),
773           debugstr_w(pszColorName), dwSizeNum);
774
775     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, pszColorName, NULL, &pt);
776     if(FAILED(hr)) return hr;
777
778     tmp = pt->pszAvailSizes;
779     while(dwSizeNum && *tmp) {
780         dwSizeNum--;
781         tmp += lstrlenW(tmp)+1;
782     }
783     if(!dwSizeNum && *tmp) {
784         TRACE("%s\n", debugstr_w(tmp));
785         lstrcpyW(pszSizeName, tmp);
786     }
787     else
788         hr = E_PROP_ID_UNSUPPORTED;
789
790     MSSTYLES_CloseThemeFile(pt);
791     return hr;
792 }
793
794 /**********************************************************************
795  *      ParseThemeIniFile                                  (UXTHEME.11)
796  *
797  * Enumerate data in a theme INI file.
798  *
799  * PARAMS
800  *     pszIniFileName      Path to a theme ini file
801  *     pszUnknown          Cannot be NULL, L"" is valid
802  *     callback            Called for each found entry
803  *     lpData              Passed through to callback
804  *
805  * RETURNS
806  *     S_OK on success
807  *     0x800706488 (Unknown property) when enumeration is canceled from callback
808  *
809  * NOTES
810  * When pszUnknown is NULL the callback is never called, the value does not seem to surve
811  * any other purpose
812  */
813 HRESULT WINAPI ParseThemeIniFile(LPCWSTR pszIniFileName, LPWSTR pszUnknown,
814                                  ParseThemeIniFileProc callback, LPVOID lpData)
815 {
816     FIXME("%s %s: stub\n", debugstr_w(pszIniFileName), debugstr_w(pszUnknown));
817     return ERROR_CALL_NOT_IMPLEMENTED;
818 }
819
820 /**********************************************************************
821  *      CheckThemeSignature                                (UXTHEME.29)
822  *
823  * Validates the signature of a theme file
824  *
825  * PARAMS
826  *     pszIniFileName      Path to a theme file
827  */
828 HRESULT WINAPI CheckThemeSignature(LPCWSTR pszThemeFileName)
829 {
830     PTHEME_FILE pt;
831     HRESULT hr;
832     TRACE("(%s)\n", debugstr_w(pszThemeFileName));
833     hr = MSSTYLES_OpenThemeFile(pszThemeFileName, NULL, NULL, &pt);
834     if(FAILED(hr))
835         return hr;
836     MSSTYLES_CloseThemeFile(pt);
837     return S_OK;
838 }