include: Add some API prototypes to appropriate header files, fix some prototypes.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdlib.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "vfwmsgs.h"
32 #include "uxtheme.h"
33 #include "tmschema.h"
34
35 #include "uxthemedll.h"
36 #include "msstyles.h"
37
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);
42
43 /***********************************************************************
44  * Defines and global variables
45  */
46
47 BOOL MSSTYLES_GetNextInteger(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, int *value);
48 BOOL MSSTYLES_GetNextToken(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LPWSTR lpBuff, DWORD buffSize);
49 void MSSTYLES_ParseThemeIni(PTHEME_FILE tf, BOOL setMetrics);
50 static HRESULT MSSTYLES_GetFont (LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LOGFONTW* logfont);
51
52 extern HINSTANCE hDllInst;
53 extern int alphaBlendMode;
54
55 #define MSSTYLES_VERSION 0x0003
56
57 static const WCHAR szThemesIniResource[] = {
58     't','h','e','m','e','s','_','i','n','i','\0'
59 };
60
61 static PTHEME_FILE tfActiveTheme;
62
63 /***********************************************************************/
64
65 /**********************************************************************
66  *      MSSTYLES_OpenThemeFile
67  *
68  * Load and validate a theme
69  *
70  * PARAMS
71  *     lpThemeFile         Path to theme file to load
72  *     pszColorName        Color name wanted, can be NULL
73  *     pszSizeName         Size name wanted, can be NULL
74  *
75  * NOTES
76  * If pszColorName or pszSizeName are NULL, the default color/size will be used.
77  * If one/both are provided, they are validated against valid color/sizes and if
78  * a match is not found, the function fails.
79  */
80 HRESULT MSSTYLES_OpenThemeFile(LPCWSTR lpThemeFile, LPCWSTR pszColorName, LPCWSTR pszSizeName, PTHEME_FILE *tf)
81 {
82     HMODULE hTheme;
83     HRSRC hrsc;
84     HRESULT hr = S_OK;
85     static const WCHAR szPackThemVersionResource[] = {
86         'P','A','C','K','T','H','E','M','_','V','E','R','S','I','O','N', '\0'
87     };
88     static const WCHAR szColorNamesResource[] = {
89         'C','O','L','O','R','N','A','M','E','S','\0'
90     };
91     static const WCHAR szSizeNamesResource[] = {
92         'S','I','Z','E','N','A','M','E','S','\0'
93     };
94
95     WORD version;
96     DWORD versize;
97     LPWSTR pszColors;
98     LPWSTR pszSelectedColor = NULL;
99     LPWSTR pszSizes;
100     LPWSTR pszSelectedSize = NULL;
101     LPWSTR tmp;
102
103     TRACE("Opening %s\n", debugstr_w(lpThemeFile));
104
105     hTheme = LoadLibraryExW(lpThemeFile, NULL, LOAD_LIBRARY_AS_DATAFILE);
106
107     /* Validate that this is really a theme */
108     if(!hTheme) {
109         hr = HRESULT_FROM_WIN32(GetLastError());
110         goto invalid_theme;
111     }
112     if(!(hrsc = FindResourceW(hTheme, MAKEINTRESOURCEW(1), szPackThemVersionResource))) {
113         TRACE("No version resource found\n");
114         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
115         goto invalid_theme;
116     }
117     if((versize = SizeofResource(hTheme, hrsc)) != 2)
118     {
119         TRACE("Version resource found, but wrong size: %d\n", versize);
120         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
121         goto invalid_theme;
122     }
123     version = *(WORD*)LoadResource(hTheme, hrsc);
124     if(version != MSSTYLES_VERSION)
125     {
126         TRACE("Version of theme file is unsupported: 0x%04x\n", version);
127         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
128         goto invalid_theme;
129     }
130
131     if(!(hrsc = FindResourceW(hTheme, MAKEINTRESOURCEW(1), szColorNamesResource))) {
132         TRACE("Color names resource not found\n");
133         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
134         goto invalid_theme;
135     }
136     pszColors = (LPWSTR)LoadResource(hTheme, hrsc);
137
138     if(!(hrsc = FindResourceW(hTheme, MAKEINTRESOURCEW(1), szSizeNamesResource))) {
139         TRACE("Size names resource not found\n");
140         hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
141         goto invalid_theme;
142     }
143     pszSizes = (LPWSTR)LoadResource(hTheme, hrsc);
144
145     /* Validate requested color against whats available from the theme */
146     if(pszColorName) {
147         tmp = pszColors;
148         while(*tmp) {
149             if(!lstrcmpiW(pszColorName, tmp)) {
150                 pszSelectedColor = tmp;
151                 break;
152             }
153             tmp += lstrlenW(tmp)+1;
154         }
155     }
156     else
157         pszSelectedColor = pszColors; /* Use the default color */
158
159     /* Validate requested size against whats available from the theme */
160     if(pszSizeName) {
161         tmp = pszSizes;
162         while(*tmp) {
163             if(!lstrcmpiW(pszSizeName, tmp)) {
164                 pszSelectedSize = tmp;
165                 break;
166             }
167             tmp += lstrlenW(tmp)+1;
168         }
169     }
170     else
171         pszSelectedSize = pszSizes; /* Use the default size */
172
173     if(!pszSelectedColor || !pszSelectedSize) {
174         TRACE("Requested color/size (%s/%s) not found in theme\n",
175               debugstr_w(pszColorName), debugstr_w(pszSizeName));
176         hr = E_PROP_ID_UNSUPPORTED;
177         goto invalid_theme;
178     }
179
180     *tf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(THEME_FILE));
181     (*tf)->hTheme = hTheme;
182     
183     GetFullPathNameW(lpThemeFile, MAX_PATH, (*tf)->szThemeFile, NULL);
184     
185     (*tf)->pszAvailColors = pszColors;
186     (*tf)->pszAvailSizes = pszSizes;
187     (*tf)->pszSelectedColor = pszSelectedColor;
188     (*tf)->pszSelectedSize = pszSelectedSize;
189     (*tf)->dwRefCount = 1;
190     return S_OK;
191
192 invalid_theme:
193     if(hTheme) FreeLibrary(hTheme);
194     return hr;
195 }
196
197 /***********************************************************************
198  *      MSSTYLES_CloseThemeFile
199  *
200  * Close theme file and free resources
201  */
202 void MSSTYLES_CloseThemeFile(PTHEME_FILE tf)
203 {
204     if(tf) {
205         tf->dwRefCount--;
206         if(!tf->dwRefCount) {
207             if(tf->hTheme) FreeLibrary(tf->hTheme);
208             if(tf->classes) {
209                 while(tf->classes) {
210                     PTHEME_CLASS pcls = tf->classes;
211                     tf->classes = pcls->next;
212                     while(pcls->partstate) {
213                         PTHEME_PARTSTATE ps = pcls->partstate;
214                         pcls->partstate = ps->next;
215                         HeapFree(GetProcessHeap(), 0, ps);
216                     }
217                     HeapFree(GetProcessHeap(), 0, pcls);
218                 }
219             }
220             while (tf->images)
221             {
222                 PTHEME_IMAGE img = tf->images;
223                 tf->images = img->next;
224                 DeleteObject (img->image);
225                 HeapFree (GetProcessHeap(), 0, img);
226             }
227             HeapFree(GetProcessHeap(), 0, tf);
228         }
229     }
230 }
231
232 /***********************************************************************
233  *      MSSTYLES_SetActiveTheme
234  *
235  * Set the current active theme
236  */
237 HRESULT MSSTYLES_SetActiveTheme(PTHEME_FILE tf, BOOL setMetrics)
238 {
239     if(tfActiveTheme)
240         MSSTYLES_CloseThemeFile(tfActiveTheme);
241     tfActiveTheme = tf;
242     if (tfActiveTheme)
243     {
244         tfActiveTheme->dwRefCount++;
245         if(!tfActiveTheme->classes)
246             MSSTYLES_ParseThemeIni(tfActiveTheme, setMetrics);
247     }
248     return S_OK;
249 }
250
251 /***********************************************************************
252  *      MSSTYLES_GetThemeIni
253  *
254  * Retrieves themes.ini from a theme
255  */
256 PUXINI_FILE MSSTYLES_GetThemeIni(PTHEME_FILE tf)
257 {
258     return UXINI_LoadINI(tf->hTheme, szThemesIniResource);
259 }
260
261 /***********************************************************************
262  *      MSSTYLES_GetActiveThemeIni
263  *
264  * Retrieve the ini file for the selected color/style
265  */
266 static PUXINI_FILE MSSTYLES_GetActiveThemeIni(PTHEME_FILE tf)
267 {
268     static const WCHAR szFileResNamesResource[] = {
269         'F','I','L','E','R','E','S','N','A','M','E','S','\0'
270     };
271     DWORD dwColorCount = 0;
272     DWORD dwSizeCount = 0;
273     DWORD dwColorNum = 0;
274     DWORD dwSizeNum = 0;
275     DWORD i;
276     DWORD dwResourceIndex;
277     LPWSTR tmp;
278     HRSRC hrsc;
279
280     /* Count the number of available colors & styles, and determine the index number
281        of the color/style we are interested in
282     */
283     tmp = tf->pszAvailColors;
284     while(*tmp) {
285         if(!lstrcmpiW(tf->pszSelectedColor, tmp))
286             dwColorNum = dwColorCount;
287         tmp += lstrlenW(tmp)+1;
288         dwColorCount++;
289     }
290     tmp = tf->pszAvailSizes;
291     while(*tmp) {
292         if(!lstrcmpiW(tf->pszSelectedSize, tmp))
293             dwSizeNum = dwSizeCount;
294         tmp += lstrlenW(tmp)+1;
295         dwSizeCount++;
296     }
297
298     if(!(hrsc = FindResourceW(tf->hTheme, MAKEINTRESOURCEW(1), szFileResNamesResource))) {
299         TRACE("FILERESNAMES map not found\n");
300         return NULL;
301     }
302     tmp = (LPWSTR)LoadResource(tf->hTheme, hrsc);
303     dwResourceIndex = (dwSizeCount * dwColorNum) + dwSizeNum;
304     for(i=0; i < dwResourceIndex; i++) {
305         tmp += lstrlenW(tmp)+1;
306     }
307     return UXINI_LoadINI(tf->hTheme, tmp);
308 }
309
310
311 /***********************************************************************
312  *      MSSTYLES_ParseIniSectionName
313  *
314  * Parse an ini section name into its component parts
315  * Valid formats are:
316  * [classname]
317  * [classname(state)]
318  * [classname.part]
319  * [classname.part(state)]
320  * [application::classname]
321  * [application::classname(state)]
322  * [application::classname.part]
323  * [application::classname.part(state)]
324  *
325  * PARAMS
326  *     lpSection           Section name
327  *     dwLen               Length of section name
328  *     szAppName           Location to store application name
329  *     szClassName         Location to store class name
330  *     iPartId             Location to store part id
331  *     iStateId            Location to store state id
332  */
333 static BOOL MSSTYLES_ParseIniSectionName(LPCWSTR lpSection, DWORD dwLen, LPWSTR szAppName, LPWSTR szClassName, int *iPartId, int *iStateId)
334 {
335     WCHAR sec[255];
336     WCHAR part[60] = {'\0'};
337     WCHAR state[60] = {'\0'};
338     LPWSTR tmp;
339     LPWSTR comp;
340     lstrcpynW(sec, lpSection, min(dwLen+1, sizeof(sec)/sizeof(sec[0])));
341
342     *szAppName = 0;
343     *szClassName = 0;
344     *iPartId = 0;
345     *iStateId = 0;
346     comp = sec;
347     /* Get the application name */
348     tmp = strchrW(comp, ':');
349     if(tmp) {
350         *tmp++ = 0;
351         tmp++;
352         lstrcpynW(szAppName, comp, MAX_THEME_APP_NAME);
353         comp = tmp;
354     }
355
356     tmp = strchrW(comp, '.');
357     if(tmp) {
358         *tmp++ = 0;
359         lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
360         comp = tmp;
361         /* now get the part & state */
362         tmp = strchrW(comp, '(');
363         if(tmp) {
364             *tmp++ = 0;
365             lstrcpynW(part, comp, sizeof(part)/sizeof(part[0]));
366             comp = tmp;
367             /* now get the state */
368             *strchrW(comp, ')') = 0;
369             lstrcpynW(state, comp, sizeof(state)/sizeof(state[0]));
370         }
371         else {
372             lstrcpynW(part, comp, sizeof(part)/sizeof(part[0]));
373         }
374     }
375     else {
376         tmp = strchrW(comp, '(');
377         if(tmp) {
378             *tmp++ = 0;
379             lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
380             comp = tmp;
381             /* now get the state */
382             *strchrW(comp, ')') = 0;
383             lstrcpynW(state, comp, sizeof(state)/sizeof(state[0]));
384         }
385         else {
386             lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
387         }
388     }
389     if(!*szClassName) return FALSE;
390     return MSSTYLES_LookupPartState(szClassName, part[0]?part:NULL, state[0]?state:NULL, iPartId, iStateId);
391 }
392
393 /***********************************************************************
394  *      MSSTYLES_FindClass
395  *
396  * Find a class
397  *
398  * PARAMS
399  *     tf                  Theme file
400  *     pszAppName          App name to find
401  *     pszClassName        Class name to find
402  *
403  * RETURNS
404  *  The class found, or NULL
405  */
406 static PTHEME_CLASS MSSTYLES_FindClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
407 {
408     PTHEME_CLASS cur = tf->classes;
409     while(cur) {
410         if(!pszAppName) {
411             if(!*cur->szAppName && !lstrcmpiW(pszClassName, cur->szClassName))
412                 return cur;
413         }
414         else {
415             if(!lstrcmpiW(pszAppName, cur->szAppName) && !lstrcmpiW(pszClassName, cur->szClassName))
416                 return cur;
417         }
418         cur = cur->next;
419     }
420     return NULL;
421 }
422
423 /***********************************************************************
424  *      MSSTYLES_AddClass
425  *
426  * Add a class to a theme file
427  *
428  * PARAMS
429  *     tf                  Theme file
430  *     pszAppName          App name to add
431  *     pszClassName        Class name to add
432  *
433  * RETURNS
434  *  The class added, or a class previously added with the same name
435  */
436 static PTHEME_CLASS MSSTYLES_AddClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
437 {
438     PTHEME_CLASS cur = MSSTYLES_FindClass(tf, pszAppName, pszClassName);
439     if(cur) return cur;
440
441     cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_CLASS));
442     cur->hTheme = tf->hTheme;
443     lstrcpyW(cur->szAppName, pszAppName);
444     lstrcpyW(cur->szClassName, pszClassName);
445     cur->next = tf->classes;
446     cur->partstate = NULL;
447     cur->overrides = NULL;
448     tf->classes = cur;
449     return cur;
450 }
451
452 /***********************************************************************
453  *      MSSTYLES_FindPartState
454  *
455  * Find a part/state
456  *
457  * PARAMS
458  *     tc                  Class to search
459  *     iPartId             Part ID to find
460  *     iStateId            State ID to find
461  *     tcNext              Receives the next class in the override chain
462  *
463  * RETURNS
464  *  The part/state found, or NULL
465  */
466 PTHEME_PARTSTATE MSSTYLES_FindPartState(PTHEME_CLASS tc, int iPartId, int iStateId, PTHEME_CLASS *tcNext)
467 {
468     PTHEME_PARTSTATE cur = tc->partstate;
469     while(cur) {
470         if(cur->iPartId == iPartId && cur->iStateId == iStateId) {
471             if(tcNext) *tcNext = tc->overrides;
472             return cur;
473         }
474         cur = cur->next;
475     }
476     if(tc->overrides) return MSSTYLES_FindPartState(tc->overrides, iPartId, iStateId, tcNext);
477     return NULL;
478 }
479
480 /***********************************************************************
481  *      MSSTYLES_AddPartState
482  *
483  * Add a part/state to a class
484  *
485  * PARAMS
486  *     tc                  Theme class
487  *     iPartId             Part ID to add
488  *     iStateId            State ID to add
489  *
490  * RETURNS
491  *  The part/state added, or a part/state previously added with the same IDs
492  */
493 static PTHEME_PARTSTATE MSSTYLES_AddPartState(PTHEME_CLASS tc, int iPartId, int iStateId)
494 {
495     PTHEME_PARTSTATE cur = MSSTYLES_FindPartState(tc, iPartId, iStateId, NULL);
496     if(cur) return cur;
497
498     cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PARTSTATE));
499     cur->iPartId = iPartId;
500     cur->iStateId = iStateId;
501     cur->properties = NULL;
502     cur->next = tc->partstate;
503     tc->partstate = cur;
504     return cur;
505 }
506
507 /***********************************************************************
508  *      MSSTYLES_LFindProperty
509  *
510  * Find a property within a property list
511  *
512  * PARAMS
513  *     tp                  property list to scan
514  *     iPropertyPrimitive  Type of value expected
515  *     iPropertyId         ID of the required value
516  *
517  * RETURNS
518  *  The property found, or NULL
519  */
520 static PTHEME_PROPERTY MSSTYLES_LFindProperty(PTHEME_PROPERTY tp, int iPropertyPrimitive, int iPropertyId)
521 {
522     PTHEME_PROPERTY cur = tp;
523     while(cur) {
524         if(cur->iPropertyId == iPropertyId) {
525             if(cur->iPrimitiveType == iPropertyPrimitive) {
526                 return cur;
527             }
528             else {
529                 if(!iPropertyPrimitive)
530                     return cur;
531                 return NULL;
532             }
533         }
534         cur = cur->next;
535     }
536     return NULL;
537 }
538
539 /***********************************************************************
540  *      MSSTYLES_PSFindProperty
541  *
542  * Find a value within a part/state
543  *
544  * PARAMS
545  *     ps                  Part/state to search
546  *     iPropertyPrimitive  Type of value expected
547  *     iPropertyId         ID of the required value
548  *
549  * RETURNS
550  *  The property found, or NULL
551  */
552 static inline PTHEME_PROPERTY MSSTYLES_PSFindProperty(PTHEME_PARTSTATE ps, int iPropertyPrimitive, int iPropertyId)
553 {
554     return MSSTYLES_LFindProperty(ps->properties, iPropertyPrimitive, iPropertyId);
555 }
556
557 /***********************************************************************
558  *      MSSTYLES_FFindMetric
559  *
560  * Find a metric property for a theme file
561  *
562  * PARAMS
563  *     tf                  Theme file
564  *     iPropertyPrimitive  Type of value expected
565  *     iPropertyId         ID of the required value
566  *
567  * RETURNS
568  *  The property found, or NULL
569  */
570 static inline PTHEME_PROPERTY MSSTYLES_FFindMetric(PTHEME_FILE tf, int iPropertyPrimitive, int iPropertyId)
571 {
572     return MSSTYLES_LFindProperty(tf->metrics, iPropertyPrimitive, iPropertyId);
573 }
574
575 /***********************************************************************
576  *      MSSTYLES_FindMetric
577  *
578  * Find a metric property for the current installed theme
579  *
580  * PARAMS
581  *     tf                  Theme file
582  *     iPropertyPrimitive  Type of value expected
583  *     iPropertyId         ID of the required value
584  *
585  * RETURNS
586  *  The property found, or NULL
587  */
588 PTHEME_PROPERTY MSSTYLES_FindMetric(int iPropertyPrimitive, int iPropertyId)
589 {
590     if(!tfActiveTheme) return NULL;
591     return MSSTYLES_FFindMetric(tfActiveTheme, iPropertyPrimitive, iPropertyId);
592 }
593
594 /***********************************************************************
595  *      MSSTYLES_AddProperty
596  *
597  * Add a property to a part/state
598  *
599  * PARAMS
600  *     ps                  Part/state
601  *     iPropertyPrimitive  Primitive type of the property
602  *     iPropertyId         ID of the property
603  *     lpValue             Raw value (non-NULL terminated)
604  *     dwValueLen          Length of the value
605  *
606  * RETURNS
607  *  The property added, or a property previously added with the same IDs
608  */
609 static PTHEME_PROPERTY MSSTYLES_AddProperty(PTHEME_PARTSTATE ps, int iPropertyPrimitive, int iPropertyId, LPCWSTR lpValue, DWORD dwValueLen, BOOL isGlobal)
610 {
611     PTHEME_PROPERTY cur = MSSTYLES_PSFindProperty(ps, iPropertyPrimitive, iPropertyId);
612     /* Should duplicate properties overwrite the original, or be ignored? */
613     if(cur) return cur;
614
615     cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PROPERTY));
616     cur->iPrimitiveType = iPropertyPrimitive;
617     cur->iPropertyId = iPropertyId;
618     cur->lpValue = lpValue;
619     cur->dwValueLen = dwValueLen;
620
621     if(ps->iStateId)
622         cur->origin = PO_STATE;
623     else if(ps->iPartId)
624         cur->origin = PO_PART;
625     else if(isGlobal)
626         cur->origin = PO_GLOBAL;
627     else
628         cur->origin = PO_CLASS;
629
630     cur->next = ps->properties;
631     ps->properties = cur;
632     return cur;
633 }
634
635 /***********************************************************************
636  *      MSSTYLES_AddMetric
637  *
638  * Add a property to a part/state
639  *
640  * PARAMS
641  *     tf                  Theme file
642  *     iPropertyPrimitive  Primitive type of the property
643  *     iPropertyId         ID of the property
644  *     lpValue             Raw value (non-NULL terminated)
645  *     dwValueLen          Length of the value
646  *
647  * RETURNS
648  *  The property added, or a property previously added with the same IDs
649  */
650 static PTHEME_PROPERTY MSSTYLES_AddMetric(PTHEME_FILE tf, int iPropertyPrimitive, int iPropertyId, LPCWSTR lpValue, DWORD dwValueLen)
651 {
652     PTHEME_PROPERTY cur = MSSTYLES_FFindMetric(tf, iPropertyPrimitive, iPropertyId);
653     /* Should duplicate properties overwrite the original, or be ignored? */
654     if(cur) return cur;
655
656     cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PROPERTY));
657     cur->iPrimitiveType = iPropertyPrimitive;
658     cur->iPropertyId = iPropertyId;
659     cur->lpValue = lpValue;
660     cur->dwValueLen = dwValueLen;
661
662     cur->origin = PO_GLOBAL;
663
664     cur->next = tf->metrics;
665     tf->metrics = cur;
666     return cur;
667 }
668
669 /* Color-related state for theme ini parsing */
670 struct PARSECOLORSTATE
671 {
672     int colorCount;
673     int colorElements[TMT_LASTCOLOR-TMT_FIRSTCOLOR];
674     COLORREF colorRgb[TMT_LASTCOLOR-TMT_FIRSTCOLOR];
675     int captionColors;
676 };
677
678 static inline void parse_init_color (struct PARSECOLORSTATE* state)
679 {
680     memset (state, 0, sizeof (*state));
681 }
682
683 static BOOL parse_handle_color_property (struct PARSECOLORSTATE* state, 
684                                          int iPropertyId, LPCWSTR lpValue,
685                                          DWORD dwValueLen)
686 {
687     int r,g,b;
688     LPCWSTR lpValueEnd = lpValue + dwValueLen;
689     MSSTYLES_GetNextInteger(lpValue, lpValueEnd, &lpValue, &r);
690     MSSTYLES_GetNextInteger(lpValue, lpValueEnd, &lpValue, &g);
691     if(MSSTYLES_GetNextInteger(lpValue, lpValueEnd, &lpValue, &b)) {
692         state->colorElements[state->colorCount] = iPropertyId - TMT_FIRSTCOLOR;
693         state->colorRgb[state->colorCount++] = RGB(r,g,b);
694         switch (iPropertyId)
695         {
696           case TMT_ACTIVECAPTION: 
697             state->captionColors |= 0x1; 
698             break;
699           case TMT_INACTIVECAPTION: 
700             state->captionColors |= 0x2; 
701             break;
702           case TMT_GRADIENTACTIVECAPTION: 
703             state->captionColors |= 0x4; 
704             break;
705           case TMT_GRADIENTINACTIVECAPTION: 
706             state->captionColors |= 0x8; 
707             break;
708         }
709         return TRUE;
710     }
711     else {
712         return FALSE;
713     }
714 }
715
716 static void parse_apply_color (struct PARSECOLORSTATE* state)
717 {
718     if (state->colorCount > 0)
719         SetSysColors(state->colorCount, state->colorElements, state->colorRgb);
720     if (state->captionColors == 0xf)
721         SystemParametersInfoW (SPI_SETGRADIENTCAPTIONS, 0, (PVOID)TRUE, 0);
722 }
723
724 /* Non-client-metrics-related state for theme ini parsing */
725 struct PARSENONCLIENTSTATE
726 {
727     NONCLIENTMETRICSW metrics;
728     BOOL metricsDirty;
729     LOGFONTW iconTitleFont;
730 };
731
732 static inline void parse_init_nonclient (struct PARSENONCLIENTSTATE* state)
733 {
734     memset (state, 0, sizeof (*state));
735     state->metrics.cbSize = sizeof (NONCLIENTMETRICSW);
736     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof (NONCLIENTMETRICSW),
737         (PVOID)&state->metrics, 0);
738     SystemParametersInfoW (SPI_GETICONTITLELOGFONT, sizeof (LOGFONTW),
739         (PVOID)&state->iconTitleFont, 0);
740 }
741
742 static BOOL parse_handle_nonclient_font (struct PARSENONCLIENTSTATE* state, 
743                                          int iPropertyId, LPCWSTR lpValue,
744                                          DWORD dwValueLen)
745 {
746     LOGFONTW font;
747     
748     memset (&font, 0, sizeof (font));
749     if (SUCCEEDED (MSSTYLES_GetFont (lpValue, lpValue + dwValueLen, &lpValue,
750         &font)))
751     {
752         switch (iPropertyId)
753         {
754           case TMT_CAPTIONFONT:
755               memcpy (&state->metrics.lfCaptionFont, &font, sizeof (LOGFONTW));
756               state->metricsDirty = TRUE;
757               break;
758           case TMT_SMALLCAPTIONFONT:
759               memcpy (&state->metrics.lfSmCaptionFont, &font, sizeof (LOGFONTW));
760               state->metricsDirty = TRUE;
761               break;
762           case TMT_MENUFONT:
763               memcpy (&state->metrics.lfMenuFont, &font, sizeof (LOGFONTW));
764               state->metricsDirty = TRUE;
765               break;
766           case TMT_STATUSFONT:
767               memcpy (&state->metrics.lfStatusFont, &font, sizeof (LOGFONTW));
768               state->metricsDirty = TRUE;
769               break;
770           case TMT_MSGBOXFONT:
771               memcpy (&state->metrics.lfMessageFont, &font, sizeof (LOGFONTW));
772               state->metricsDirty = TRUE;
773               break;
774           case TMT_ICONTITLEFONT:
775               memcpy (&state->iconTitleFont, &font, sizeof (LOGFONTW));
776               state->metricsDirty = TRUE;
777               break;
778         }
779         return TRUE;
780     }
781     else
782         return FALSE;
783 }
784
785 static BOOL parse_handle_nonclient_size (struct PARSENONCLIENTSTATE* state, 
786                                          int iPropertyId, LPCWSTR lpValue,
787                                          DWORD dwValueLen)
788 {
789     int size;
790     LPCWSTR lpValueEnd = lpValue + dwValueLen;
791     if(MSSTYLES_GetNextInteger(lpValue, lpValueEnd, &lpValue, &size)) {
792         switch (iPropertyId)
793         {
794             case TMT_SIZINGBORDERWIDTH:
795                 state->metrics.iBorderWidth = size;
796                 state->metricsDirty = TRUE;
797                 break;
798             case TMT_SCROLLBARWIDTH:
799                 state->metrics.iScrollWidth = size;
800                 state->metricsDirty = TRUE;
801                 break;
802             case TMT_SCROLLBARHEIGHT:
803                 state->metrics.iScrollHeight = size;
804                 state->metricsDirty = TRUE;
805                 break;
806             case TMT_CAPTIONBARWIDTH:
807                 state->metrics.iCaptionWidth = size;
808                 state->metricsDirty = TRUE;
809                 break;
810             case TMT_CAPTIONBARHEIGHT:
811                 state->metrics.iCaptionHeight = size;
812                 state->metricsDirty = TRUE;
813                 break;
814             case TMT_SMCAPTIONBARWIDTH:
815                 state->metrics.iSmCaptionWidth = size;
816                 state->metricsDirty = TRUE;
817                 break;
818             case TMT_SMCAPTIONBARHEIGHT:
819                 state->metrics.iSmCaptionHeight = size;
820                 state->metricsDirty = TRUE;
821                 break;
822             case TMT_MENUBARWIDTH:
823                 state->metrics.iMenuWidth = size;
824                 state->metricsDirty = TRUE;
825                 break;
826             case TMT_MENUBARHEIGHT:
827                 state->metrics.iMenuHeight = size;
828                 state->metricsDirty = TRUE;
829                 break;
830         }
831         return TRUE;
832     }
833     else
834         return FALSE;
835 }
836
837 static void parse_apply_nonclient (struct PARSENONCLIENTSTATE* state)
838 {
839     if (state->metricsDirty)
840     {
841         SystemParametersInfoW (SPI_SETNONCLIENTMETRICS, sizeof (state->metrics),
842             (PVOID)&state->metrics, 0);
843         SystemParametersInfoW (SPI_SETICONTITLELOGFONT, sizeof (state->iconTitleFont),
844             (PVOID)&state->iconTitleFont, 0);
845     }
846 }
847
848 /***********************************************************************
849  *      MSSTYLES_ParseThemeIni
850  *
851  * Parse the theme ini for the selected color/style
852  *
853  * PARAMS
854  *     tf                  Theme to parse
855  */
856 void MSSTYLES_ParseThemeIni(PTHEME_FILE tf, BOOL setMetrics)
857 {
858     static const WCHAR szSysMetrics[] = {'S','y','s','M','e','t','r','i','c','s','\0'};
859     static const WCHAR szGlobals[] = {'g','l','o','b','a','l','s','\0'};
860     PTHEME_CLASS cls;
861     PTHEME_CLASS globals;
862     PTHEME_PARTSTATE ps;
863     PUXINI_FILE ini;
864     WCHAR szAppName[MAX_THEME_APP_NAME];
865     WCHAR szClassName[MAX_THEME_CLASS_NAME];
866     WCHAR szPropertyName[MAX_THEME_VALUE_NAME];
867     int iPartId;
868     int iStateId;
869     int iPropertyPrimitive;
870     int iPropertyId;
871     DWORD dwLen;
872     LPCWSTR lpName;
873     DWORD dwValueLen;
874     LPCWSTR lpValue;
875
876     ini = MSSTYLES_GetActiveThemeIni(tf);
877
878     while((lpName=UXINI_GetNextSection(ini, &dwLen))) {
879         if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, lpName, dwLen, szSysMetrics, -1) == CSTR_EQUAL) {
880             struct PARSECOLORSTATE colorState;
881             struct PARSENONCLIENTSTATE nonClientState;
882             
883             parse_init_color (&colorState);
884             parse_init_nonclient (&nonClientState);
885
886             while((lpName=UXINI_GetNextValue(ini, &dwLen, &lpValue, &dwValueLen))) {
887                 lstrcpynW(szPropertyName, lpName, min(dwLen+1, sizeof(szPropertyName)/sizeof(szPropertyName[0])));
888                 if(MSSTYLES_LookupProperty(szPropertyName, &iPropertyPrimitive, &iPropertyId)) {
889                     if(iPropertyId >= TMT_FIRSTCOLOR && iPropertyId <= TMT_LASTCOLOR) {
890                         if (!parse_handle_color_property (&colorState, iPropertyId, 
891                             lpValue, dwValueLen))
892                             FIXME("Invalid color value for %s\n", 
893                                 debugstr_w(szPropertyName)); 
894                     }
895                     else if (setMetrics && (iPropertyId == TMT_FLATMENUS)) {
896                         BOOL flatMenus = (*lpValue == 'T') || (*lpValue == 't');
897                         SystemParametersInfoW (SPI_SETFLATMENU, 0, (PVOID)(INT_PTR)flatMenus, 0);
898                     }
899                     else if ((iPropertyId >= TMT_FIRSTFONT) 
900                         && (iPropertyId <= TMT_LASTFONT))
901                     {
902                         if (!parse_handle_nonclient_font (&nonClientState,
903                             iPropertyId, lpValue, dwValueLen))
904                             FIXME("Invalid font value for %s\n", 
905                                 debugstr_w(szPropertyName)); 
906                     }
907                     else if ((iPropertyId >= TMT_FIRSTSIZE)
908                         && (iPropertyId <= TMT_LASTSIZE))
909                     {
910                         if (!parse_handle_nonclient_size (&nonClientState,
911                             iPropertyId, lpValue, dwValueLen))
912                             FIXME("Invalid size value for %s\n", 
913                                 debugstr_w(szPropertyName)); 
914                     }
915                     /* Catch all metrics, including colors */
916                     MSSTYLES_AddMetric(tf, iPropertyPrimitive, iPropertyId, lpValue, dwValueLen);
917                 }
918                 else {
919                     TRACE("Unknown system metric %s\n", debugstr_w(szPropertyName));
920                 }
921             }
922             if (setMetrics) 
923             {
924                 parse_apply_color (&colorState);
925                 parse_apply_nonclient (&nonClientState);
926             }
927             continue;
928         }
929         if(MSSTYLES_ParseIniSectionName(lpName, dwLen, szAppName, szClassName, &iPartId, &iStateId)) {
930             BOOL isGlobal = FALSE;
931             if(!lstrcmpiW(szClassName, szGlobals)) {
932                 isGlobal = TRUE;
933             }
934             cls = MSSTYLES_AddClass(tf, szAppName, szClassName);
935             ps = MSSTYLES_AddPartState(cls, iPartId, iStateId);
936
937             while((lpName=UXINI_GetNextValue(ini, &dwLen, &lpValue, &dwValueLen))) {
938                 lstrcpynW(szPropertyName, lpName, min(dwLen+1, sizeof(szPropertyName)/sizeof(szPropertyName[0])));
939                 if(MSSTYLES_LookupProperty(szPropertyName, &iPropertyPrimitive, &iPropertyId)) {
940                     MSSTYLES_AddProperty(ps, iPropertyPrimitive, iPropertyId, lpValue, dwValueLen, isGlobal);
941                 }
942                 else {
943                     TRACE("Unknown property %s\n", debugstr_w(szPropertyName));
944                 }
945             }
946         }
947     }
948
949     /* App/Class combos override values defined by the base class, map these overrides */
950     globals = MSSTYLES_FindClass(tf, NULL, szGlobals);
951     cls = tf->classes;
952     while(cls) {
953         if(*cls->szAppName) {
954             cls->overrides = MSSTYLES_FindClass(tf, NULL, cls->szClassName);
955             if(!cls->overrides) {
956                 TRACE("No overrides found for app %s class %s\n", debugstr_w(cls->szAppName), debugstr_w(cls->szClassName));
957             }
958             else {
959                 cls->overrides = globals;
960             }
961         }
962         else {
963             /* Everything overrides globals..except globals */
964             if(cls != globals) cls->overrides = globals;
965         }
966         cls = cls->next;
967     }
968     UXINI_CloseINI(ini);
969
970     if(!tf->classes) {
971         ERR("Failed to parse theme ini\n");
972     }
973 }
974
975 /***********************************************************************
976  *      MSSTYLES_OpenThemeClass
977  *
978  * Open a theme class, uses the current active theme
979  *
980  * PARAMS
981  *     pszAppName          Application name, for theme styles specific
982  *                         to a particular application
983  *     pszClassList        List of requested classes, semicolon delimited
984  */
985 PTHEME_CLASS MSSTYLES_OpenThemeClass(LPCWSTR pszAppName, LPCWSTR pszClassList)
986 {
987     PTHEME_CLASS cls = NULL;
988     WCHAR szClassName[MAX_THEME_CLASS_NAME];
989     LPCWSTR start;
990     LPCWSTR end;
991     DWORD len;
992
993     if(!tfActiveTheme) {
994         TRACE("there is no active theme\n");
995         return NULL;
996     }
997     if(!tfActiveTheme->classes) {
998         return NULL;
999     }
1000
1001     start = pszClassList;
1002     while((end = strchrW(start, ';'))) {
1003         len = end-start;
1004         lstrcpynW(szClassName, start, min(len+1, sizeof(szClassName)/sizeof(szClassName[0])));
1005         start = end+1;
1006         cls = MSSTYLES_FindClass(tfActiveTheme, pszAppName, szClassName);
1007         if(cls) break;
1008     }
1009     if(!cls && *start) {
1010         lstrcpynW(szClassName, start, sizeof(szClassName)/sizeof(szClassName[0]));
1011         cls = MSSTYLES_FindClass(tfActiveTheme, pszAppName, szClassName);
1012     }
1013     if(cls) {
1014         TRACE("Opened app %s, class %s from list %s\n", debugstr_w(cls->szAppName), debugstr_w(cls->szClassName), debugstr_w(pszClassList));
1015         cls->tf = tfActiveTheme;
1016         cls->tf->dwRefCount++;
1017     }
1018     return cls;
1019 }
1020
1021 /***********************************************************************
1022  *      MSSTYLES_CloseThemeClass
1023  *
1024  * Close a theme class
1025  *
1026  * PARAMS
1027  *     tc                  Theme class to close
1028  *
1029  * NOTES
1030  *  The MSSTYLES_CloseThemeFile decreases the refcount of the owning
1031  *  theme file and cleans it up, if needed.
1032  */
1033 HRESULT MSSTYLES_CloseThemeClass(PTHEME_CLASS tc)
1034 {
1035     MSSTYLES_CloseThemeFile (tc->tf);
1036     return S_OK;
1037 }
1038
1039 /***********************************************************************
1040  *      MSSTYLES_FindProperty
1041  *
1042  * Locate a property in a class. Part and state IDs will be used as a
1043  * preference, but may be ignored in the attempt to locate the property.
1044  * Will scan the entire chain of overrides for this class.
1045  */
1046 PTHEME_PROPERTY MSSTYLES_FindProperty(PTHEME_CLASS tc, int iPartId, int iStateId, int iPropertyPrimitive, int iPropertyId)
1047 {
1048     PTHEME_CLASS next = tc;
1049     PTHEME_PARTSTATE ps;
1050     PTHEME_PROPERTY tp;
1051
1052     TRACE("(%p, %d, %d, %d)\n", tc, iPartId, iStateId, iPropertyId);
1053      /* Try and find an exact match on part & state */
1054     while(next && (ps = MSSTYLES_FindPartState(next, iPartId, iStateId, &next))) {
1055         if((tp = MSSTYLES_PSFindProperty(ps, iPropertyPrimitive, iPropertyId))) {
1056             return tp;
1057         }
1058     }
1059     /* If that fails, and we didn't already try it, search for just part */
1060     if(iStateId != 0)
1061         iStateId = 0;
1062     /* As a last ditch attempt..go for just class */
1063     else if(iPartId != 0)
1064         iPartId = 0;
1065     else
1066         return NULL;
1067
1068     if((tp = MSSTYLES_FindProperty(tc, iPartId, iStateId, iPropertyPrimitive, iPropertyId)))
1069         return tp;
1070     return NULL;
1071 }
1072
1073 /* Prepare a bitmap to be used for alpha blending */
1074 static BOOL prepare_alpha (HBITMAP bmp, BOOL* hasAlpha)
1075 {
1076     DIBSECTION dib;
1077     int n;
1078     BYTE* p;
1079
1080     *hasAlpha = FALSE;
1081
1082     if (!bmp || GetObjectW( bmp, sizeof(dib), &dib ) != sizeof(dib))
1083         return FALSE;
1084
1085     if(dib.dsBm.bmBitsPixel != 32)
1086         /* nothing to do */
1087         return TRUE;
1088
1089     *hasAlpha = TRUE;
1090     p = (BYTE*)dib.dsBm.bmBits;
1091     n = abs(dib.dsBmih.biHeight) * dib.dsBmih.biWidth;
1092     /* AlphaBlend() wants premultiplied alpha, so do that now */
1093     while (n-- > 0)
1094     {
1095         int a = p[3]+1;
1096         p[0] = (p[0] * a) >> 8;
1097         p[1] = (p[1] * a) >> 8;
1098         p[2] = (p[2] * a) >> 8;
1099         p += 4;
1100     }
1101
1102     return TRUE;
1103 }
1104
1105 HBITMAP MSSTYLES_LoadBitmap (PTHEME_CLASS tc, LPCWSTR lpFilename, BOOL* hasAlpha)
1106 {
1107     WCHAR szFile[MAX_PATH];
1108     LPWSTR tmp;
1109     PTHEME_IMAGE img;
1110     lstrcpynW(szFile, lpFilename, sizeof(szFile)/sizeof(szFile[0]));
1111     tmp = szFile;
1112     do {
1113         if(*tmp == '\\') *tmp = '_';
1114         if(*tmp == '/') *tmp = '_';
1115         if(*tmp == '.') *tmp = '_';
1116     } while(*tmp++);
1117
1118     /* Try to locate in list of loaded images */
1119     img = tc->tf->images;
1120     while (img)
1121     {
1122         if (lstrcmpiW (szFile, img->name) == 0)
1123         {
1124             TRACE ("found %p %s: %p\n", img, debugstr_w (img->name), img->image);
1125             *hasAlpha = img->hasAlpha;
1126             return img->image;
1127         }
1128         img = img->next;
1129     }
1130     /* Not found? Load from resources */
1131     img = HeapAlloc (GetProcessHeap(), 0, sizeof (THEME_IMAGE));
1132     img->image = LoadImageW(tc->hTheme, szFile, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
1133     prepare_alpha (img->image, hasAlpha);
1134     img->hasAlpha = *hasAlpha;
1135     /* ...and stow away for later reuse. */
1136     lstrcpyW (img->name, szFile);
1137     img->next = tc->tf->images;
1138     tc->tf->images = img;
1139     TRACE ("new %p %s: %p\n", img, debugstr_w (img->name), img->image);
1140     return img->image;
1141 }
1142
1143 BOOL MSSTYLES_GetNextInteger(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, int *value)
1144 {
1145     LPCWSTR cur = lpStringStart;
1146     int total = 0;
1147     BOOL gotNeg = FALSE;
1148
1149     while(cur < lpStringEnd && (*cur < '0' || *cur > '9' || *cur == '-')) cur++;
1150     if(cur >= lpStringEnd) {
1151         return FALSE;
1152     }
1153     if(*cur == '-') {
1154         cur++;
1155         gotNeg = TRUE;
1156     }
1157     while(cur < lpStringEnd && (*cur >= '0' && *cur <= '9')) {
1158         total = total * 10 + (*cur - '0');
1159         cur++;
1160     }
1161     if(gotNeg) total = -total;
1162     *value = total;
1163     if(lpValEnd) *lpValEnd = cur;
1164     return TRUE;
1165 }
1166
1167 BOOL MSSTYLES_GetNextToken(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LPWSTR lpBuff, DWORD buffSize) {
1168     LPCWSTR cur = lpStringStart;
1169     LPCWSTR start;
1170     LPCWSTR end;
1171
1172     while(cur < lpStringEnd && (isspace(*cur) || *cur == ',')) cur++;
1173     if(cur >= lpStringEnd) {
1174         return FALSE;
1175     }
1176     start = cur;
1177     while(cur < lpStringEnd && *cur != ',') cur++;
1178     end = cur;
1179     while(isspace(*end)) end--;
1180
1181     lstrcpynW(lpBuff, start, min(buffSize, end-start+1));
1182
1183     if(lpValEnd) *lpValEnd = cur;
1184     return TRUE;
1185 }
1186
1187 /***********************************************************************
1188  *      MSSTYLES_GetPropertyBool
1189  *
1190  * Retrieve a color value for a property 
1191  */
1192 HRESULT MSSTYLES_GetPropertyBool(PTHEME_PROPERTY tp, BOOL *pfVal)
1193 {
1194     *pfVal = FALSE;
1195     if(*tp->lpValue == 't' || *tp->lpValue == 'T')
1196         *pfVal = TRUE;
1197     return S_OK;
1198 }
1199
1200 /***********************************************************************
1201  *      MSSTYLES_GetPropertyColor
1202  *
1203  * Retrieve a color value for a property 
1204  */
1205 HRESULT MSSTYLES_GetPropertyColor(PTHEME_PROPERTY tp, COLORREF *pColor)
1206 {
1207     LPCWSTR lpEnd;
1208     LPCWSTR lpCur;
1209     int red, green, blue;
1210
1211     lpCur = tp->lpValue;
1212     lpEnd = tp->lpValue + tp->dwValueLen;
1213
1214     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &red);
1215     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &green);
1216     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &blue)) {
1217         TRACE("Could not parse color property\n");
1218         return E_PROP_ID_UNSUPPORTED;
1219     }
1220     *pColor = RGB(red,green,blue);
1221     return S_OK;
1222 }
1223
1224 /***********************************************************************
1225  *      MSSTYLES_GetPropertyColor
1226  *
1227  * Retrieve a color value for a property 
1228  */
1229 static HRESULT MSSTYLES_GetFont (LPCWSTR lpCur, LPCWSTR lpEnd,
1230                                  LPCWSTR *lpValEnd, LOGFONTW* pFont)
1231 {
1232     static const WCHAR szBold[] = {'b','o','l','d','\0'};
1233     static const WCHAR szItalic[] = {'i','t','a','l','i','c','\0'};
1234     static const WCHAR szUnderline[] = {'u','n','d','e','r','l','i','n','e','\0'};
1235     static const WCHAR szStrikeOut[] = {'s','t','r','i','k','e','o','u','t','\0'};
1236     int pointSize;
1237     WCHAR attr[32];
1238
1239     if(!MSSTYLES_GetNextToken(lpCur, lpEnd, &lpCur, pFont->lfFaceName, LF_FACESIZE)) {
1240         TRACE("Property is there, but failed to get face name\n");
1241         *lpValEnd = lpCur;
1242         return E_PROP_ID_UNSUPPORTED;
1243     }
1244     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pointSize)) {
1245         TRACE("Property is there, but failed to get point size\n");
1246         *lpValEnd = lpCur;
1247         return E_PROP_ID_UNSUPPORTED;
1248     }
1249     pFont->lfHeight = pointSize;
1250     pFont->lfWeight = FW_REGULAR;
1251     pFont->lfCharSet = DEFAULT_CHARSET;
1252     while(MSSTYLES_GetNextToken(lpCur, lpEnd, &lpCur, attr, sizeof(attr)/sizeof(attr[0]))) {
1253         if(!lstrcmpiW(szBold, attr)) pFont->lfWeight = FW_BOLD;
1254         else if(!!lstrcmpiW(szItalic, attr)) pFont->lfItalic = TRUE;
1255         else if(!!lstrcmpiW(szUnderline, attr)) pFont->lfUnderline = TRUE;
1256         else if(!!lstrcmpiW(szStrikeOut, attr)) pFont->lfStrikeOut = TRUE;
1257     }
1258     *lpValEnd = lpCur;
1259     return S_OK;
1260 }
1261
1262 HRESULT MSSTYLES_GetPropertyFont(PTHEME_PROPERTY tp, HDC hdc, LOGFONTW *pFont)
1263 {
1264     LPCWSTR lpCur = tp->lpValue;
1265     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1266     HRESULT hr; 
1267
1268     ZeroMemory(pFont, sizeof(LOGFONTW));
1269     hr = MSSTYLES_GetFont (lpCur, lpEnd, &lpCur, pFont);
1270     if (SUCCEEDED (hr))
1271         pFont->lfHeight = -MulDiv(pFont->lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1272
1273     return hr;
1274 }
1275
1276 /***********************************************************************
1277  *      MSSTYLES_GetPropertyInt
1278  *
1279  * Retrieve an int value for a property 
1280  */
1281 HRESULT MSSTYLES_GetPropertyInt(PTHEME_PROPERTY tp, int *piVal)
1282 {
1283     if(!MSSTYLES_GetNextInteger(tp->lpValue, (tp->lpValue + tp->dwValueLen), NULL, piVal)) {
1284         TRACE("Could not parse int property\n");
1285         return E_PROP_ID_UNSUPPORTED;
1286     }
1287     return S_OK;
1288 }
1289
1290 /***********************************************************************
1291  *      MSSTYLES_GetPropertyIntList
1292  *
1293  * Retrieve an int list value for a property 
1294  */
1295 HRESULT MSSTYLES_GetPropertyIntList(PTHEME_PROPERTY tp, INTLIST *pIntList)
1296 {
1297     int i;
1298     LPCWSTR lpCur = tp->lpValue;
1299     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1300
1301     for(i=0; i < MAX_INTLIST_COUNT; i++) {
1302         if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pIntList->iValues[i]))
1303             break;
1304     }
1305     pIntList->iValueCount = i;
1306     return S_OK;
1307 }
1308
1309 /***********************************************************************
1310  *      MSSTYLES_GetPropertyPosition
1311  *
1312  * Retrieve a position value for a property 
1313  */
1314 HRESULT MSSTYLES_GetPropertyPosition(PTHEME_PROPERTY tp, POINT *pPoint)
1315 {
1316     int x,y;
1317     LPCWSTR lpCur = tp->lpValue;
1318     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1319
1320     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &x);
1321     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &y)) {
1322         TRACE("Could not parse position property\n");
1323         return E_PROP_ID_UNSUPPORTED;
1324     }
1325     pPoint->x = x;
1326     pPoint->y = y;
1327     return S_OK;
1328 }
1329
1330 /***********************************************************************
1331  *      MSSTYLES_GetPropertyString
1332  *
1333  * Retrieve a string value for a property 
1334  */
1335 HRESULT MSSTYLES_GetPropertyString(PTHEME_PROPERTY tp, LPWSTR pszBuff, int cchMaxBuffChars)
1336 {
1337     lstrcpynW(pszBuff, tp->lpValue, min(tp->dwValueLen+1, cchMaxBuffChars));
1338     return S_OK;
1339 }
1340
1341 /***********************************************************************
1342  *      MSSTYLES_GetPropertyRect
1343  *
1344  * Retrieve a rect value for a property 
1345  */
1346 HRESULT MSSTYLES_GetPropertyRect(PTHEME_PROPERTY tp, RECT *pRect)
1347 {
1348     LPCWSTR lpCur = tp->lpValue;
1349     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1350
1351     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->left);
1352     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->top);
1353     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->right);
1354     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->bottom)) {
1355         TRACE("Could not parse rect property\n");
1356         return E_PROP_ID_UNSUPPORTED;
1357     }
1358     return S_OK;
1359 }
1360
1361 /***********************************************************************
1362  *      MSSTYLES_GetPropertyMargins
1363  *
1364  * Retrieve a margins value for a property 
1365  */
1366 HRESULT MSSTYLES_GetPropertyMargins(PTHEME_PROPERTY tp, RECT *prc, MARGINS *pMargins)
1367 {
1368     LPCWSTR lpCur = tp->lpValue;
1369     LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1370
1371     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cxLeftWidth);
1372     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cxRightWidth);
1373     MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cyTopHeight);
1374     if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cyBottomHeight)) {
1375         TRACE("Could not parse margins property\n");
1376         return E_PROP_ID_UNSUPPORTED;
1377     }
1378     return S_OK;
1379 }