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