Unicodify wineesd.
[wine] / dlls / uxtheme / msstyles.c
1 /*
2  * Win32 5.1 msstyles theme format
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 #define NO_SHLWAPI_REG
29 #include "shlwapi.h"
30 #include "winnls.h"
31 #include "wingdi.h"
32 #include "uxtheme.h"
33 #include "tmschema.h"
34
35 #include "uxthemedll.h"
36 #include "msstyles.h"
37
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);
41
42 /***********************************************************************
43  * Defines and global variables
44  */
45
46 BOOL MSSTYLES_GetNextInteger(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, int *value);
47 BOOL MSSTYLES_GetNextToken(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LPWSTR lpBuff, DWORD buffSize);
48 void MSSTYLES_ParseThemeIni(PTHEME_FILE tf);
49
50 extern HINSTANCE hDllInst;
51
52 #define MSSTYLES_VERSION 0x0003
53
54 static const WCHAR szThemesIniResource[] = {
55     't','h','e','m','e','s','_','i','n','i','\0'
56 };
57
58 PTHEME_FILE tfActiveTheme = NULL;
59
60 /***********************************************************************/
61
62 /**********************************************************************
63  *      MSSTYLES_OpenThemeFile
64  *
65  * Load and validate a theme
66  *
67  * PARAMS
68  *     lpThemeFile         Path to theme file to load
69  *     pszColorName        Color name wanted, can be NULL
70  *     pszSizeName         Size name wanted, can be NULL
71  *
72  * NOTES
73  * If pszColorName or pszSizeName are NULL, the default color/size will be used.
74  * If one/both are provided, they are validated against valid color/sizes and if
75  * a match is not found, the function fails.
76  */
77 HRESULT MSSTYLES_OpenThemeFile(LPCWSTR lpThemeFile, LPCWSTR pszColorName, LPCWSTR pszSizeName, PTHEME_FILE *tf)
78 {
79     HMODULE hTheme;
80     HRSRC hrsc;
81     HRESULT hr = S_OK;
82     static const WCHAR szPackThemVersionResource[] = {
83         'P','A','C','K','T','H','E','M','_','V','E','R','S','I','O','N', '\0'
84     };
85     static const WCHAR szColorNamesResource[] = {
86         'C','O','L','O','R','N','A','M','E','S','\0'
87     };
88     static const WCHAR szSizeNamesResource[] = {
89         'S','I','Z','E','N','A','M','E','S','\0'
90     };
91
92     WORD version;
93     DWORD versize;
94     LPWSTR pszColors;
95     LPWSTR pszSelectedColor = NULL;
96     LPWSTR pszSizes;
97     LPWSTR pszSelectedSize = NULL;
98     LPWSTR tmp;
99
100     TRACE("Opening %s\n", debugstr_w(lpThemeFile));
101
102     hTheme = LoadLibraryExW(lpThemeFile, NULL, LOAD_LIBRARY_AS_DATAFILE);
103
104     /* Validate that this is really a theme */
105     if(!hTheme) {
106         hr = HRESULT_FROM_WIN32(GetLastError());
107         goto invalid_theme;
108     }
109     if(!(hrsc = FindResourceW(hTheme, MAKEINTRESOURCEW(1), szPackThemVersionResource))) {
110         TRACE("No version resource found\n");
111         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
112         goto invalid_theme;
113     }
114     if((versize = SizeofResource(hTheme, hrsc)) != 2)
115     {
116         TRACE("Version resource found, but wrong size: %ld\n", versize);
117         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
118         goto invalid_theme;
119     }
120     version = *(WORD*)LoadResource(hTheme, hrsc);
121     if(version != MSSTYLES_VERSION)
122     {
123         TRACE("Version of theme file is unsupported: 0x%04x\n", version);
124         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
125         goto invalid_theme;
126     }
127
128     if(!(hrsc = FindResourceW(hTheme, MAKEINTRESOURCEW(1), szColorNamesResource))) {
129         TRACE("Color names resource not found\n");
130         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
131         goto invalid_theme;
132     }
133     pszColors = (LPWSTR)LoadResource(hTheme, hrsc);
134
135     if(!(hrsc = FindResourceW(hTheme, MAKEINTRESOURCEW(1), szSizeNamesResource))) {
136         TRACE("Size names resource not found\n");
137         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
138         goto invalid_theme;
139     }
140     pszSizes = (LPWSTR)LoadResource(hTheme, hrsc);
141
142     /* Validate requested color against whats available from the theme */
143     if(pszColorName) {
144         tmp = pszColors;
145         while(*tmp) {
146             if(!lstrcmpiW(pszColorName, tmp)) {
147                 pszSelectedColor = tmp;
148                 break;
149             }
150             tmp += lstrlenW(tmp)+1;
151         }
152     }
153     else
154         pszSelectedColor = pszColors; /* Use the default color */
155
156     /* Validate requested size against whats available from the theme */
157     if(pszSizeName) {
158         tmp = pszSizes;
159         while(*tmp) {
160             if(!lstrcmpiW(pszSizeName, tmp)) {
161                 pszSelectedSize = tmp;
162                 break;
163             }
164             tmp += lstrlenW(tmp)+1;
165         }
166     }
167     else
168         pszSelectedSize = pszSizes; /* Use the default size */
169
170     if(!pszSelectedColor || !pszSelectedSize) {
171         TRACE("Requested color/size (%s/%s) not found in theme\n",
172               debugstr_w(pszColorName), debugstr_w(pszSizeName));
173         hr = E_PROP_ID_UNSUPPORTED;
174         goto invalid_theme;
175     }
176
177     *tf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(THEME_FILE));
178     (*tf)->hTheme = hTheme;
179     
180     GetFullPathNameW(lpThemeFile, MAX_PATH, (*tf)->szThemeFile, NULL);
181     
182     (*tf)->pszAvailColors = pszColors;
183     (*tf)->pszAvailSizes = pszSizes;
184     (*tf)->pszSelectedColor = pszSelectedColor;
185     (*tf)->pszSelectedSize = pszSelectedSize;
186     (*tf)->dwRefCount = 1;
187     return S_OK;
188
189 invalid_theme:
190     if(hTheme) FreeLibrary(hTheme);
191     return hr;
192 }
193
194 /***********************************************************************
195  *      MSSTYLES_CloseThemeFile
196  *
197  * Close theme file and free resources
198  */
199 void MSSTYLES_CloseThemeFile(PTHEME_FILE tf)
200 {
201     if(tf) {
202         tf->dwRefCount--;
203         if(!tf->dwRefCount) {
204             if(tf->hTheme) FreeLibrary(tf->hTheme);
205             if(tf->classes) {
206                 while(tf->classes) {
207                     PTHEME_CLASS pcls = tf->classes;
208                     tf->classes = pcls->next;
209                     while(pcls->partstate) {
210                         PTHEME_PARTSTATE ps = pcls->partstate;
211                         pcls->partstate = ps->next;
212                         HeapFree(GetProcessHeap(), 0, ps);
213                     }
214                     HeapFree(GetProcessHeap(), 0, pcls);
215                 }
216             }
217             HeapFree(GetProcessHeap(), 0, tf);
218         }
219     }
220 }
221
222 /***********************************************************************
223  *      MSSTYLES_SetActiveTheme
224  *
225  * Set the current active theme
226  */
227 HRESULT MSSTYLES_SetActiveTheme(PTHEME_FILE tf)
228 {
229     if(tfActiveTheme)
230         MSSTYLES_CloseThemeFile(tfActiveTheme);
231     tfActiveTheme = tf;
232     if (tfActiveTheme)
233     {
234         tfActiveTheme->dwRefCount++;
235         if(!tfActiveTheme->classes)
236             MSSTYLES_ParseThemeIni(tfActiveTheme);
237     }
238     return S_OK;
239 }
240
241 /***********************************************************************
242  *      MSSTYLES_GetThemeIni
243  *
244  * Retrieves themes.ini from a theme
245  */
246 PUXINI_FILE MSSTYLES_GetThemeIni(PTHEME_FILE tf)
247 {
248     return UXINI_LoadINI(tf->hTheme, szThemesIniResource);
249 }
250
251 /***********************************************************************
252  *      MSSTYLES_GetActiveThemeIni
253  *
254  * Retrieve the ini file for the selected color/style
255  */
256 static PUXINI_FILE MSSTYLES_GetActiveThemeIni(PTHEME_FILE tf)
257 {
258     static const WCHAR szFileResNamesResource[] = {
259         'F','I','L','E','R','E','S','N','A','M','E','S','\0'
260     };
261     DWORD dwColorCount = 0;
262     DWORD dwSizeCount = 0;
263     DWORD dwColorNum = 0;
264     DWORD dwSizeNum = 0;
265     DWORD i;
266     DWORD dwResourceIndex;
267     LPWSTR tmp;
268     HRSRC hrsc;
269
270     /* Count the number of available colors & styles, and determine the index number
271        of the color/style we are interested in
272     */
273     tmp = tf->pszAvailColors;
274     while(*tmp) {
275         if(!lstrcmpiW(tf->pszSelectedColor, tmp))
276             dwColorNum = dwColorCount;
277         tmp += lstrlenW(tmp)+1;
278         dwColorCount++;
279     }
280     tmp = tf->pszAvailSizes;
281     while(*tmp) {
282         if(!lstrcmpiW(tf->pszSelectedSize, tmp))
283             dwSizeNum = dwSizeCount;
284         tmp += lstrlenW(tmp)+1;
285         dwSizeCount++;
286     }
287
288     if(!(hrsc = FindResourceW(tf->hTheme, MAKEINTRESOURCEW(1), szFileResNamesResource))) {
289         TRACE("FILERESNAMES map not found\n");
290         return NULL;
291     }
292     tmp = (LPWSTR)LoadResource(tf->hTheme, hrsc);
293     dwResourceIndex = (dwSizeCount * dwColorNum) + dwSizeNum;
294     for(i=0; i < dwResourceIndex; i++) {
295         tmp += lstrlenW(tmp)+1;
296     }
297     return UXINI_LoadINI(tf->hTheme, tmp);
298 }
299
300
301 /***********************************************************************
302  *      MSSTYLES_ParseIniSectionName
303  *
304  * Parse an ini section name into its component parts
305  * Valid formats are:
306  * [classname]
307  * [classname(state)]
308  * [classname.part]
309  * [classname.part(state)]
310  * [application::classname]
311  * [application::classname(state)]
312  * [application::classname.part]
313  * [application::classname.part(state)]
314  *
315  * PARAMS
316  *     lpSection           Section name
317  *     dwLen               Length of section name
318  *     szAppName           Location to store application name
319  *     szClassName         Location to store class name
320  *     iPartId             Location to store part id
321  *     iStateId            Location to store state id
322  */
323 static BOOL MSSTYLES_ParseIniSectionName(LPCWSTR lpSection, DWORD dwLen, LPWSTR szAppName, LPWSTR szClassName, int *iPartId, int *iStateId)
324 {
325     WCHAR sec[255];
326     WCHAR part[60] = {'\0'};
327     WCHAR state[60] = {'\0'};
328     LPWSTR tmp;
329     LPWSTR comp;
330     lstrcpynW(sec, lpSection, min(dwLen+1, sizeof(sec)/sizeof(sec[0])));
331
332     *szAppName = 0;
333     *szClassName = 0;
334     *iPartId = 0;
335     *iStateId = 0;
336     comp = sec;
337     /* Get the application name */
338     tmp = StrChrW(comp, ':');
339     if(tmp) {
340         *tmp++ = 0;
341         tmp++;
342         lstrcpynW(szAppName, comp, MAX_THEME_APP_NAME);
343         comp = tmp;
344     }
345
346     tmp = StrChrW(comp, '.');
347     if(tmp) {
348         *tmp++ = 0;
349         lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
350         comp = tmp;
351         /* now get the part & state */
352         tmp = StrChrW(comp, '(');
353         if(tmp) {
354             *tmp++ = 0;
355             lstrcpynW(part, comp, sizeof(part)/sizeof(part[0]));
356             comp = tmp;
357             /* now get the state */
358             *StrChrW(comp, ')') = 0;
359             lstrcpynW(state, comp, sizeof(state)/sizeof(state[0]));
360         }
361         else {
362             lstrcpynW(part, comp, sizeof(part)/sizeof(part[0]));
363         }
364     }
365     else {
366         tmp = StrChrW(comp, '(');
367         if(tmp) {
368             *tmp++ = 0;
369             lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
370             comp = tmp;
371             /* now get the state */
372             *StrChrW(comp, ')') = 0;
373             lstrcpynW(state, comp, sizeof(state)/sizeof(state[0]));
374         }
375         else {
376             lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
377         }
378     }
379     if(!*szClassName) return FALSE;
380     return MSSTYLES_LookupPartState(szClassName, part[0]?part:NULL, state[0]?state:NULL, iPartId, iStateId);
381 }
382
383 /***********************************************************************
384  *      MSSTYLES_FindClass
385  *
386  * Find a class
387  *
388  * PARAMS
389  *     tf                  Theme file
390  *     pszAppName          App name to find
391  *     pszClassName        Class name to find
392  *
393  * RETURNS
394  *  The class found, or NULL
395  */
396 PTHEME_CLASS MSSTYLES_FindClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
397 {
398     PTHEME_CLASS cur = tf->classes;
399     while(cur) {
400         if(!pszAppName) {
401             if(!*cur->szAppName && !lstrcmpiW(pszClassName, cur->szClassName))
402                 return cur;
403         }
404         else {
405             if(!lstrcmpiW(pszAppName, cur->szAppName) && !lstrcmpiW(pszClassName, cur->szClassName))
406                 return cur;
407         }
408         cur = cur->next;
409     }
410     return NULL;
411 }
412
413 /***********************************************************************
414  *      MSSTYLES_AddClass
415  *
416  * Add a class to a theme file
417  *
418  * PARAMS
419  *     tf                  Theme file
420  *     pszAppName          App name to add
421  *     pszClassName        Class name to add
422  *
423  * RETURNS
424  *  The class added, or a class previously added with the same name
425  */
426 static PTHEME_CLASS MSSTYLES_AddClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
427 {
428     PTHEME_CLASS cur = MSSTYLES_FindClass(tf, pszAppName, pszClassName);
429     if(cur) return cur;
430
431     cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_CLASS));
432     cur->hTheme = tf->hTheme;
433     lstrcpyW(cur->szAppName, pszAppName);
434     lstrcpyW(cur->szClassName, pszClassName);
435     cur->next = tf->classes;
436     cur->partstate = NULL;
437     cur->overrides = NULL;
438     tf->classes = cur;
439     return cur;
440 }
441
442 /***********************************************************************
443  *      MSSTYLES_FindPartState
444  *
445  * Find a part/state
446  *
447  * PARAMS
448  *     tc                  Class to search
449  *     iPartId             Part ID to find
450  *     iStateId            State ID to find
451  *     tcNext              Receives the next class in the override chain
452  *
453  * RETURNS
454  *  The part/state found, or NULL
455  */
456 PTHEME_PARTSTATE MSSTYLES_FindPartState(PTHEME_CLASS tc, int iPartId, int iStateId, PTHEME_CLASS *tcNext)
457 {
458     PTHEME_PARTSTATE cur = tc->partstate;
459     while(cur) {
460         if(cur->iPartId == iPartId && cur->iStateId == iStateId) {
461             if(tcNext) *tcNext = tc->overrides;
462             return cur;
463         }
464         cur = cur->next;
465     }
466     if(tc->overrides) return MSSTYLES_FindPartState(tc->overrides, iPartId, iStateId, tcNext);
467     return NULL;
468 }
469
470 /***********************************************************************
471  *      MSSTYLES_AddPartState
472  *
473  * Add a part/state to a class
474  *
475  * PARAMS
476  *     tc                  Theme class
477  *     iPartId             Part ID to add
478  *     iStateId            State ID to add
479  *
480  * RETURNS
481  *  The part/state added, or a part/state previously added with the same IDs
482  */
483 static PTHEME_PARTSTATE MSSTYLES_AddPartState(PTHEME_CLASS tc, int iPartId, int iStateId)
484 {
485     PTHEME_PARTSTATE cur = MSSTYLES_FindPartState(tc, iPartId, iStateId, NULL);
486     if(cur) return cur;
487
488     cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PARTSTATE));
489     cur->iPartId = iPartId;
490     cur->iStateId = iStateId;
491     cur->properties = NULL;
492     cur->next = tc->partstate;
493     tc->partstate = cur;
494     return cur;
495 }
496
497 /***********************************************************************
498  *      MSSTYLES_LFindProperty
499  *
500  * Find a property within a property list
501  *
502  * PARAMS
503  *     tp                  property list to scan
504  *     iPropertyPrimitive  Type of value expected
505  *     iPropertyId         ID of the required value
506  *
507  * RETURNS
508  *  The property found, or NULL
509  */
510 static PTHEME_PROPERTY MSSTYLES_LFindProperty(PTHEME_PROPERTY tp, int iPropertyPrimitive, int iPropertyId)
511 {
512     PTHEME_PROPERTY cur = tp;
513     while(cur) {
514         if(cur->iPropertyId == iPropertyId) {
515             if(cur->iPrimitiveType == iPropertyPrimitive) {
516                 return cur;
517             }
518             else {
519                 if(!iPropertyPrimitive)
520                     return cur;
521                 return NULL;
522             }
523         }
524         cur = cur->next;
525     }
526     return NULL;
527 }
528
529 /***********************************************************************
530  *      MSSTYLES_PSFindProperty
531  *
532  * Find a value within a part/state
533  *
534  * PARAMS
535  *     ps                  Part/state to search
536  *     iPropertyPrimitive  Type of value expected
537  *     iPropertyId         ID of the required value
538  *
539  * RETURNS
540  *  The property found, or NULL
541  */
542 static inline PTHEME_PROPERTY MSSTYLES_PSFindProperty(PTHEME_PARTSTATE ps, int iPropertyPrimitive, int iPropertyId)
543 {
544     return MSSTYLES_LFindProperty(ps->properties, iPropertyPrimitive, iPropertyId);
545 }
546
547 /***********************************************************************
548  *      MSSTYLES_FFindMetric
549  *
550  * Find a metric property for a theme file
551  *
552  * PARAMS
553  *     tf                  Theme file
554  *     iPropertyPrimitive  Type of value expected
555  *     iPropertyId         ID of the required value
556  *
557  * RETURNS
558  *  The property found, or NULL
559  */
560 static inline PTHEME_PROPERTY MSSTYLES_FFindMetric(PTHEME_FILE tf, int iPropertyPrimitive, int iPropertyId)
561 {
562     return MSSTYLES_LFindProperty(tf->metrics, iPropertyPrimitive, iPropertyId);
563 }
564
565 /***********************************************************************
566  *      MSSTYLES_FindMetric
567  *
568  * Find a metric property for the current installed theme
569  *
570  * PARAMS
571  *     tf                  Theme file
572  *     iPropertyPrimitive  Type of value expected
573  *     iPropertyId         ID of the required value
574  *
575  * RETURNS
576  *  The property found, or NULL
577  */
578 PTHEME_PROPERTY MSSTYLES_FindMetric(int iPropertyPrimitive, int iPropertyId)
579 {
580     if(!tfActiveTheme) return NULL;
581     return MSSTYLES_FFindMetric(tfActiveTheme, iPropertyPrimitive, iPropertyId);
582 }
583
584 /***********************************************************************
585  *      MSSTYLES_AddProperty
586  *
587  * Add a property to a part/state
588  *
589  * PARAMS
590  *     ps                  Part/state
591  *     iPropertyPrimitive  Primitive type of the property
592  *     iPropertyId         ID of the property
593  *     lpValue             Raw value (non-NULL terminated)
594  *     dwValueLen          Length of the value
595  *
596  * RETURNS
597  *  The property added, or a property previously added with the same IDs
598  */
599 static PTHEME_PROPERTY MSSTYLES_AddProperty(PTHEME_PARTSTATE ps, int iPropertyPrimitive, int iPropertyId, LPCWSTR lpValue, DWORD dwValueLen, BOOL isGlobal)
600 {
601     PTHEME_PROPERTY cur = MSSTYLES_PSFindProperty(ps, iPropertyPrimitive, iPropertyId);
602     /* Should duplicate properties overwrite the original, or be ignored? */
603     if(cur) return cur;
604
605     cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PROPERTY));
606     cur->iPrimitiveType = iPropertyPrimitive;
607     cur->iPropertyId = iPropertyId;
608     cur->lpValue = lpValue;
609     cur->dwValueLen = dwValueLen;
610
611     if(ps->iStateId)
612         cur->origin = PO_STATE;
613     else if(ps->iPartId)
614         cur->origin = PO_PART;
615     else if(isGlobal)
616         cur->origin = PO_GLOBAL;
617     else
618         cur->origin = PO_CLASS;
619
620     cur->next = ps->properties;
621     ps->properties = cur;
622     return cur;
623 }
624
625 /***********************************************************************
626  *      MSSTYLES_AddMetric
627  *
628  * Add a property to a part/state
629  *
630  * PARAMS
631  *     tf                  Theme file
632  *     iPropertyPrimitive  Primitive type of the property
633  *     iPropertyId         ID of the property
634  *     lpValue             Raw value (non-NULL terminated)
635  *     dwValueLen          Length of the value
636  *
637  * RETURNS
638  *  The property added, or a property previously added with the same IDs
639  */
640 static PTHEME_PROPERTY MSSTYLES_AddMetric(PTHEME_FILE tf, int iPropertyPrimitive, int iPropertyId, LPCWSTR lpValue, DWORD dwValueLen)
641 {
642     PTHEME_PROPERTY cur = MSSTYLES_FFindMetric(tf, iPropertyPrimitive, iPropertyId);
643     /* Should duplicate properties overwrite the original, or be ignored? */
644     if(cur) return cur;
645
646     cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PROPERTY));
647     cur->iPrimitiveType = iPropertyPrimitive;
648     cur->iPropertyId = iPropertyId;
649     cur->lpValue = lpValue;
650     cur->dwValueLen = dwValueLen;
651
652     cur->origin = PO_GLOBAL;
653
654     cur->next = tf->metrics;
655     tf->metrics = cur;
656     return cur;
657 }
658
659 /***********************************************************************
660  *      MSSTYLES_ParseThemeIni
661  *
662  * Parse the theme ini for the selected color/style
663  *
664  * PARAMS
665  *     tf                  Theme to parse
666  */
667 void MSSTYLES_ParseThemeIni(PTHEME_FILE tf)
668 {
669     static const WCHAR szSysMetrics[] = {'S','y','s','M','e','t','r','i','c','s','\0'};
670     static const WCHAR szGlobals[] = {'g','l','o','b','a','l','s','\0'};
671     PTHEME_CLASS cls;
672     PTHEME_CLASS globals;
673     PTHEME_PARTSTATE ps;
674     PUXINI_FILE ini;
675     WCHAR szAppName[MAX_THEME_APP_NAME];
676     WCHAR szClassName[MAX_THEME_CLASS_NAME];
677     WCHAR szPropertyName[MAX_THEME_VALUE_NAME];
678     int iPartId;
679     int iStateId;
680     int iPropertyPrimitive;
681     int iPropertyId;
682     DWORD dwLen;
683     LPCWSTR lpName;
684     DWORD dwValueLen;
685     LPCWSTR lpValue;
686
687     ini = MSSTYLES_GetActiveThemeIni(tf);
688
689     while((lpName=UXINI_GetNextSection(ini, &dwLen))) {
690         if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, lpName, dwLen, szSysMetrics, -1) == CSTR_EQUAL) {
691             int colorCount = 0;
692             int colorElements[TMT_LASTCOLOR-TMT_FIRSTCOLOR];
693             COLORREF colorRgb[TMT_LASTCOLOR-TMT_FIRSTCOLOR];
694             LPCWSTR lpValueEnd;
695
696             while((lpName=UXINI_GetNextValue(ini, &dwLen, &lpValue, &dwValueLen))) {
697                 lstrcpynW(szPropertyName, lpName, min(dwLen+1, sizeof(szPropertyName)/sizeof(szPropertyName[0])));
698                 if(MSSTYLES_LookupProperty(szPropertyName, &iPropertyPrimitive, &iPropertyId)) {
699                     if(iPropertyId >= TMT_FIRSTCOLOR && iPropertyId <= TMT_LASTCOLOR) {
700                         int r,g,b;
701                         lpValueEnd = lpValue + dwValueLen;
702                         MSSTYLES_GetNextInteger(lpValue, lpValueEnd, &lpValue, &r);
703                         MSSTYLES_GetNextInteger(lpValue, lpValueEnd, &lpValue, &g);
704                         if(MSSTYLES_GetNextInteger(lpValue, lpValueEnd, &lpValue, &b)) {
705                             colorElements[colorCount] = iPropertyId - TMT_FIRSTCOLOR;
706                             colorRgb[colorCount++] = RGB(r,g,b);
707                         }
708                         else {
709                             FIXME("Invalid color value for %s\n", debugstr_w(szPropertyName));
710                         }
711                     }
712                     else if (iPropertyId == TMT_FLATMENUS) {
713                         BOOL flatMenus = (*lpValue == 'T') || (*lpValue == 't');
714                         SystemParametersInfoW (SPI_SETFLATMENU, 0, (PVOID)flatMenus, 0);
715                     }
716                     /* Catch all metrics, including colors */
717                     MSSTYLES_AddMetric(tf, iPropertyPrimitive, iPropertyId, lpValue, dwValueLen);
718                 }
719                 else {
720                     TRACE("Unknown system metric %s\n", debugstr_w(szPropertyName));
721                 }
722             }
723             if(colorCount > 0)
724                 SetSysColors(colorCount, colorElements, colorRgb);
725             continue;
726         }
727         if(MSSTYLES_ParseIniSectionName(lpName, dwLen, szAppName, szClassName, &iPartId, &iStateId)) {
728             BOOL isGlobal = FALSE;
729             if(!lstrcmpiW(szClassName, szGlobals)) {
730                 isGlobal = TRUE;
731             }
732             cls = MSSTYLES_AddClass(tf, szAppName, szClassName);
733             ps = MSSTYLES_AddPartState(cls, iPartId, iStateId);
734
735             while((lpName=UXINI_GetNextValue(ini, &dwLen, &lpValue, &dwValueLen))) {
736                 lstrcpynW(szPropertyName, lpName, min(dwLen+1, sizeof(szPropertyName)/sizeof(szPropertyName[0])));
737                 if(MSSTYLES_LookupProperty(szPropertyName, &iPropertyPrimitive, &iPropertyId)) {
738                     MSSTYLES_AddProperty(ps, iPropertyPrimitive, iPropertyId, lpValue, dwValueLen, isGlobal);
739                 }
740                 else {
741                     TRACE("Unknown property %s\n", debugstr_w(szPropertyName));
742                 }
743             }
744         }
745     }
746
747     /* App/Class combos override values defined by the base class, map these overrides */
748     globals = MSSTYLES_FindClass(tf, NULL, szGlobals);
749     cls = tf->classes;
750     while(cls) {
751         if(*cls->szAppName) {
752             cls->overrides = MSSTYLES_FindClass(tf, NULL, cls->szClassName);
753             if(!cls->overrides) {
754                 TRACE("No overrides found for app %s class %s\n", debugstr_w(cls->szAppName), debugstr_w(cls->szClassName));
755             }
756             else {
757                 cls->overrides = globals;
758             }
759         }
760         else {
761             /* Everything overrides globals..except globals */
762             if(cls != globals) cls->overrides = globals;
763         }
764         cls = cls->next;
765     }
766     UXINI_CloseINI(ini);
767
768     if(!tf->classes) {
769         ERR("Failed to parse theme ini\n");
770     }
771 }
772
773 /***********************************************************************
774  *      MSSTYLES_OpenThemeClass
775  *
776  * Open a theme class, uses the current active theme
777  *
778  * PARAMS
779  *     pszAppName          Application name, for theme styles specific
780  *                         to a particular application
781  *     pszClassList        List of requested classes, semicolon delimited
782  */
783 PTHEME_CLASS MSSTYLES_OpenThemeClass(LPCWSTR pszAppName, LPCWSTR pszClassList)
784 {
785     PTHEME_CLASS cls = NULL;
786     WCHAR szClassName[MAX_THEME_CLASS_NAME];
787     LPCWSTR start;
788     LPCWSTR end;
789     DWORD len;
790
791     if(!tfActiveTheme) {
792         TRACE("there is no active theme\n");
793         return NULL;
794     }
795     if(!tfActiveTheme->classes) {
796         return NULL;
797     }
798
799     start = pszClassList;
800     while((end = StrChrW(start, ';'))) {
801         len = end-start;
802         lstrcpynW(szClassName, start, min(len+1, sizeof(szClassName)/sizeof(szClassName[0])));
803         start = end+1;
804         cls = MSSTYLES_FindClass(tfActiveTheme, pszAppName, szClassName);
805         if(cls) break;
806     }
807     if(!cls && *start) {
808         lstrcpynW(szClassName, start, sizeof(szClassName)/sizeof(szClassName[0]));
809         cls = MSSTYLES_FindClass(tfActiveTheme, pszAppName, szClassName);
810     }
811     if(cls) {
812         TRACE("Opened app %s, class %s from list %s\n", debugstr_w(cls->szAppName), debugstr_w(cls->szClassName), debugstr_w(pszClassList));
813         cls->tf = tfActiveTheme;
814         cls->tf->dwRefCount++;
815     }
816     return cls;
817 }
818
819 /***********************************************************************
820  *      MSSTYLES_CloseThemeClass
821  *
822  * Close a theme class
823  *
824  * PARAMS
825  *     tc                  Theme class to close
826  *
827  * NOTES
828  *  The MSSTYLES_CloseThemeFile decreases the refcount of the owning
829  *  theme file and cleans it up, if needed.
830  */
831 HRESULT MSSTYLES_CloseThemeClass(PTHEME_CLASS tc)
832 {
833     MSSTYLES_CloseThemeFile (tc->tf);
834     return S_OK;
835 }
836
837 /***********************************************************************
838  *      MSSTYLES_FindProperty
839  *
840  * Locate a property in a class. Part and state IDs will be used as a
841  * preference, but may be ignored in the attempt to locate the property.
842  * Will scan the entire chain of overrides for this class.
843  */
844 PTHEME_PROPERTY MSSTYLES_FindProperty(PTHEME_CLASS tc, int iPartId, int iStateId, int iPropertyPrimitive, int iPropertyId)
845 {
846     PTHEME_CLASS next = tc;
847     PTHEME_PARTSTATE ps;
848     PTHEME_PROPERTY tp;
849
850     TRACE("(%p, %d, %d, %d)\n", tc, iPartId, iStateId, iPropertyId);
851      /* Try and find an exact match on part & state */
852     while(next && (ps = MSSTYLES_FindPartState(next, iPartId, iStateId, &next))) {
853         if((tp = MSSTYLES_PSFindProperty(ps, iPropertyPrimitive, iPropertyId))) {
854             return tp;
855         }
856     }
857     /* If that fails, and we didn't already try it, search for just part */
858     if(iStateId != 0)
859         iStateId = 0;
860     /* As a last ditch attempt..go for just class */
861     else if(iPartId != 0)
862         iPartId = 0;
863     else
864         return NULL;
865
866     if((tp = MSSTYLES_FindProperty(tc, iPartId, iStateId, iPropertyPrimitive, iPropertyId)))
867         return tp;
868     return NULL;
869 }
870
871 HBITMAP MSSTYLES_LoadBitmap(HDC hdc, PTHEME_CLASS tc, LPCWSTR lpFilename)
872 {
873     WCHAR szFile[MAX_PATH];
874     LPWSTR tmp;
875     lstrcpynW(szFile, lpFilename, sizeof(szFile)/sizeof(szFile[0]));
876     tmp = szFile;
877     do {
878         if(*tmp == '\\') *tmp = '_';
879         if(*tmp == '/') *tmp = '_';
880         if(*tmp == '.') *tmp = '_';
881     } while(*tmp++);
882     return LoadImageW(tc->hTheme, szFile, IMAGE_BITMAP, 0, 0, LR_SHARED|LR_CREATEDIBSECTION);
883 }
884
885 BOOL MSSTYLES_GetNextInteger(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, int *value)
886 {
887     LPCWSTR cur = lpStringStart;
888     int total = 0;
889     BOOL gotNeg = FALSE;
890
891     while(cur < lpStringEnd && (*cur < '0' || *cur > '9' || *cur == '-')) cur++;
892     if(cur >= lpStringEnd) {
893         return FALSE;
894     }
895     if(*cur == '-') {
896         cur++;
897         gotNeg = TRUE;
898     }
899     while(cur < lpStringEnd && (*cur >= '0' && *cur <= '9')) {
900         total = total * 10 + (*cur - '0');
901         cur++;
902     }
903     if(gotNeg) total = -total;
904     *value = total;
905     if(lpValEnd) *lpValEnd = cur;
906     return TRUE;
907 }
908
909 BOOL MSSTYLES_GetNextToken(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LPWSTR lpBuff, DWORD buffSize) {
910     LPCWSTR cur = lpStringStart;
911     LPCWSTR start;
912     LPCWSTR end;
913
914     while(cur < lpStringEnd && (isspace(*cur) || *cur == ',')) cur++;
915     if(cur >= lpStringEnd) {
916         return FALSE;
917     }
918     start = cur;
919     while(cur < lpStringEnd && *cur != ',') cur++;
920     end = cur;
921     while(isspace(*end)) end--;
922
923     lstrcpynW(lpBuff, start, min(buffSize, end-start+1));
924
925     if(lpValEnd) *lpValEnd = cur;
926     return TRUE;
927 }
928
929 /***********************************************************************
930  *      MSSTYLES_GetPropertyBool
931  *
932  * Retrieve a color value for a property 
933  */
934 HRESULT MSSTYLES_GetPropertyBool(PTHEME_PROPERTY tp, BOOL *pfVal)
935 {
936     *pfVal = FALSE;
937     if(*tp->lpValue == 't' || *tp->lpValue == 'T')
938         *pfVal = TRUE;
939     return S_OK;
940 }
941
942 /***********************************************************************
943  *      MSSTYLES_GetPropertyColor
944  *
945  * Retrieve a color value for a property 
946  */
947 HRESULT MSSTYLES_GetPropertyColor(PTHEME_PROPERTY tp, COLORREF *pColor)
948 {
949     LPCWSTR lpEnd;
950     LPCWSTR lpCur;
951     int red, green, blue;
952
953     lpCur = tp->lpValue;
954     lpEnd = tp->lpValue + tp->dwValueLen;
955
956     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &red);
957     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &green);
958     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &blue)) {
959         TRACE("Could not parse color property\n");
960         return E_PROP_ID_UNSUPPORTED;
961     }
962     *pColor = RGB(red,green,blue);
963     return S_OK;
964 }
965
966 /***********************************************************************
967  *      MSSTYLES_GetPropertyColor
968  *
969  * Retrieve a color value for a property 
970  */
971 HRESULT MSSTYLES_GetPropertyFont(PTHEME_PROPERTY tp, HDC hdc, LOGFONTW *pFont)
972 {
973     static const WCHAR szBold[] = {'b','o','l','d','\0'};
974     static const WCHAR szItalic[] = {'i','t','a','l','i','c','\0'};
975     static const WCHAR szUnderline[] = {'u','n','d','e','r','l','i','n','e','\0'};
976     static const WCHAR szStrikeOut[] = {'s','t','r','i','k','e','o','u','t','\0'};
977     int pointSize;
978     WCHAR attr[32];
979     LPCWSTR lpCur = tp->lpValue;
980     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
981
982     ZeroMemory(pFont, sizeof(LOGFONTW));
983
984     if(!MSSTYLES_GetNextToken(lpCur, lpEnd, &lpCur, pFont->lfFaceName, LF_FACESIZE)) {
985         TRACE("Property is there, but failed to get face name\n");
986         return E_PROP_ID_UNSUPPORTED;
987     }
988     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pointSize)) {
989         TRACE("Property is there, but failed to get point size\n");
990         return E_PROP_ID_UNSUPPORTED;
991     }
992     pFont->lfHeight = -MulDiv(pointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
993     pFont->lfWeight = FW_REGULAR;
994     pFont->lfCharSet = DEFAULT_CHARSET;
995     while(MSSTYLES_GetNextToken(lpCur, lpEnd, &lpCur, attr, sizeof(attr)/sizeof(attr[0]))) {
996         if(!lstrcmpiW(szBold, attr)) pFont->lfWeight = FW_BOLD;
997         else if(!!lstrcmpiW(szItalic, attr)) pFont->lfItalic = TRUE;
998         else if(!!lstrcmpiW(szUnderline, attr)) pFont->lfUnderline = TRUE;
999         else if(!!lstrcmpiW(szStrikeOut, attr)) pFont->lfStrikeOut = TRUE;
1000     }
1001     return S_OK;
1002 }
1003
1004 /***********************************************************************
1005  *      MSSTYLES_GetPropertyInt
1006  *
1007  * Retrieve an int value for a property 
1008  */
1009 HRESULT MSSTYLES_GetPropertyInt(PTHEME_PROPERTY tp, int *piVal)
1010 {
1011     if(!MSSTYLES_GetNextInteger(tp->lpValue, (tp->lpValue + tp->dwValueLen), NULL, piVal)) {
1012         TRACE("Could not parse int property\n");
1013         return E_PROP_ID_UNSUPPORTED;
1014     }
1015     return S_OK;
1016 }
1017
1018 /***********************************************************************
1019  *      MSSTYLES_GetPropertyIntList
1020  *
1021  * Retrieve an int list value for a property 
1022  */
1023 HRESULT MSSTYLES_GetPropertyIntList(PTHEME_PROPERTY tp, INTLIST *pIntList)
1024 {
1025     int i;
1026     LPCWSTR lpCur = tp->lpValue;
1027     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1028
1029     for(i=0; i < MAX_INTLIST_COUNT; i++) {
1030         if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pIntList->iValues[i]))
1031             break;
1032     }
1033     pIntList->iValueCount = i;
1034     return S_OK;
1035 }
1036
1037 /***********************************************************************
1038  *      MSSTYLES_GetPropertyPosition
1039  *
1040  * Retrieve a position value for a property 
1041  */
1042 HRESULT MSSTYLES_GetPropertyPosition(PTHEME_PROPERTY tp, POINT *pPoint)
1043 {
1044     int x,y;
1045     LPCWSTR lpCur = tp->lpValue;
1046     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1047
1048     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &x);
1049     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &y)) {
1050         TRACE("Could not parse position property\n");
1051         return E_PROP_ID_UNSUPPORTED;
1052     }
1053     pPoint->x = x;
1054     pPoint->y = y;
1055     return S_OK;
1056 }
1057
1058 /***********************************************************************
1059  *      MSSTYLES_GetPropertyString
1060  *
1061  * Retrieve a string value for a property 
1062  */
1063 HRESULT MSSTYLES_GetPropertyString(PTHEME_PROPERTY tp, LPWSTR pszBuff, int cchMaxBuffChars)
1064 {
1065     lstrcpynW(pszBuff, tp->lpValue, min(tp->dwValueLen+1, cchMaxBuffChars));
1066     return S_OK;
1067 }
1068
1069 /***********************************************************************
1070  *      MSSTYLES_GetPropertyRect
1071  *
1072  * Retrieve a rect value for a property 
1073  */
1074 HRESULT MSSTYLES_GetPropertyRect(PTHEME_PROPERTY tp, RECT *pRect)
1075 {
1076     LPCWSTR lpCur = tp->lpValue;
1077     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1078
1079     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->left);
1080     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->top);
1081     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->right);
1082     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->bottom)) {
1083         TRACE("Could not parse rect property\n");
1084         return E_PROP_ID_UNSUPPORTED;
1085     }
1086     return S_OK;
1087 }
1088
1089 /***********************************************************************
1090  *      MSSTYLES_GetPropertyMargins
1091  *
1092  * Retrieve a margins value for a property 
1093  */
1094 HRESULT MSSTYLES_GetPropertyMargins(PTHEME_PROPERTY tp, RECT *prc, MARGINS *pMargins)
1095 {
1096     LPCWSTR lpCur = tp->lpValue;
1097     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1098
1099     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cxLeftWidth);
1100     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cxRightWidth);
1101     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cyTopHeight);
1102     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cyBottomHeight)) {
1103         TRACE("Could not parse margins property\n");
1104         return E_PROP_ID_UNSUPPORTED;
1105     }
1106     return S_OK;
1107 }