Have the theme INI file parsed upon theme activation and not just when
[wine/multimedia.git] / dlls / uxtheme / msstyles.c
blob9ce1d17155c1b45e5b7a21c87739c4042f1ff834
1 /*
2 * Win32 5.1 msstyles theme format
4 * Copyright (C) 2003 Kevin Koltzau
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.
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.
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
21 #include "config.h"
23 #include <stdarg.h>
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"
35 #include "uxthemedll.h"
36 #include "msstyles.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);
42 /***********************************************************************
43 * Defines and global variables
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);
50 extern HINSTANCE hDllInst;
52 #define MSSTYLES_VERSION 0x0003
54 static const WCHAR szThemesIniResource[] = {
55 't','h','e','m','e','s','_','i','n','i','\0'
58 PTHEME_FILE tfActiveTheme = NULL;
60 /***********************************************************************/
62 /**********************************************************************
63 * MSSTYLES_OpenThemeFile
65 * Load and validate a theme
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
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.
77 HRESULT MSSTYLES_OpenThemeFile(LPCWSTR lpThemeFile, LPCWSTR pszColorName, LPCWSTR pszSizeName, PTHEME_FILE *tf)
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'
85 static const WCHAR szColorNamesResource[] = {
86 'C','O','L','O','R','N','A','M','E','S','\0'
88 static const WCHAR szSizeNamesResource[] = {
89 'S','I','Z','E','N','A','M','E','S','\0'
92 WORD version;
93 DWORD versize;
94 LPWSTR pszColors;
95 LPWSTR pszSelectedColor = NULL;
96 LPWSTR pszSizes;
97 LPWSTR pszSelectedSize = NULL;
98 LPWSTR tmp;
100 TRACE("Opening %s\n", debugstr_w(lpThemeFile));
102 hTheme = LoadLibraryExW(lpThemeFile, NULL, LOAD_LIBRARY_AS_DATAFILE);
104 /* Validate that this is really a theme */
105 if(!hTheme) {
106 hr = HRESULT_FROM_WIN32(GetLastError());
107 goto invalid_theme;
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;
114 if((versize = SizeofResource(hTheme, hrsc)) != 2)
116 TRACE("Version resource found, but wrong size: %ld\n", versize);
117 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
118 goto invalid_theme;
120 version = *(WORD*)LoadResource(hTheme, hrsc);
121 if(version != MSSTYLES_VERSION)
123 TRACE("Version of theme file is unsupported: 0x%04x\n", version);
124 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
125 goto invalid_theme;
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;
133 pszColors = (LPWSTR)LoadResource(hTheme, hrsc);
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;
140 pszSizes = (LPWSTR)LoadResource(hTheme, hrsc);
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;
150 tmp += lstrlenW(tmp)+1;
153 else
154 pszSelectedColor = pszColors; /* Use the default color */
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;
164 tmp += lstrlenW(tmp)+1;
167 else
168 pszSelectedSize = pszSizes; /* Use the default size */
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;
177 *tf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(THEME_FILE));
178 (*tf)->hTheme = hTheme;
180 GetFullPathNameW(lpThemeFile, MAX_PATH, (*tf)->szThemeFile, NULL);
182 (*tf)->pszAvailColors = pszColors;
183 (*tf)->pszAvailSizes = pszSizes;
184 (*tf)->pszSelectedColor = pszSelectedColor;
185 (*tf)->pszSelectedSize = pszSelectedSize;
186 (*tf)->dwRefCount = 1;
187 return S_OK;
189 invalid_theme:
190 if(hTheme) FreeLibrary(hTheme);
191 return hr;
194 /***********************************************************************
195 * MSSTYLES_CloseThemeFile
197 * Close theme file and free resources
199 void MSSTYLES_CloseThemeFile(PTHEME_FILE tf)
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);
214 HeapFree(GetProcessHeap(), 0, pcls);
217 HeapFree(GetProcessHeap(), 0, tf);
222 /***********************************************************************
223 * MSSTYLES_SetActiveTheme
225 * Set the current active theme
227 HRESULT MSSTYLES_SetActiveTheme(PTHEME_FILE tf)
229 if(tfActiveTheme)
230 MSSTYLES_CloseThemeFile(tfActiveTheme);
231 tfActiveTheme = tf;
232 if (tfActiveTheme)
234 tfActiveTheme->dwRefCount++;
235 if(!tfActiveTheme->classes)
236 MSSTYLES_ParseThemeIni(tfActiveTheme);
238 return S_OK;
241 /***********************************************************************
242 * MSSTYLES_GetThemeIni
244 * Retrieves themes.ini from a theme
246 PUXINI_FILE MSSTYLES_GetThemeIni(PTHEME_FILE tf)
248 return UXINI_LoadINI(tf->hTheme, szThemesIniResource);
251 /***********************************************************************
252 * MSSTYLES_GetActiveThemeIni
254 * Retrieve the ini file for the selected color/style
256 PUXINI_FILE MSSTYLES_GetActiveThemeIni(PTHEME_FILE tf)
258 static const WCHAR szFileResNamesResource[] = {
259 'F','I','L','E','R','E','S','N','A','M','E','S','\0'
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;
270 /* Count the number of available colors & styles, and determine the index number
271 of the color/style we are interested in
273 tmp = tf->pszAvailColors;
274 while(*tmp) {
275 if(!lstrcmpiW(tf->pszSelectedColor, tmp))
276 dwColorNum = dwColorCount;
277 tmp += lstrlenW(tmp)+1;
278 dwColorCount++;
280 tmp = tf->pszAvailSizes;
281 while(*tmp) {
282 if(!lstrcmpiW(tf->pszSelectedSize, tmp))
283 dwSizeNum = dwSizeCount;
284 tmp += lstrlenW(tmp)+1;
285 dwSizeCount++;
288 if(!(hrsc = FindResourceW(tf->hTheme, MAKEINTRESOURCEW(1), szFileResNamesResource))) {
289 TRACE("FILERESNAMES map not found\n");
290 return NULL;
292 tmp = (LPWSTR)LoadResource(tf->hTheme, hrsc);
293 dwResourceIndex = (dwSizeCount * dwColorNum) + dwSizeNum;
294 for(i=0; i < dwResourceIndex; i++) {
295 tmp += lstrlenW(tmp)+1;
297 return UXINI_LoadINI(tf->hTheme, tmp);
301 /***********************************************************************
302 * MSSTYLES_ParseIniSectionName
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)]
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
323 BOOL MSSTYLES_ParseIniSectionName(LPCWSTR lpSection, DWORD dwLen, LPWSTR szAppName, LPWSTR szClassName, int *iPartId, int *iStateId)
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])));
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;
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]));
361 else {
362 lstrcpynW(part, comp, sizeof(part)/sizeof(part[0]));
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]));
375 else {
376 lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
379 if(!*szClassName) return FALSE;
380 return MSSTYLES_LookupPartState(szClassName, part[0]?part:NULL, state[0]?state:NULL, iPartId, iStateId);
383 /***********************************************************************
384 * MSSTYLES_FindClass
386 * Find a class
388 * PARAMS
389 * tf Theme file
390 * pszAppName App name to find
391 * pszClassName Class name to find
393 * RETURNS
394 * The class found, or NULL
396 PTHEME_CLASS MSSTYLES_FindClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
398 PTHEME_CLASS cur = tf->classes;
399 while(cur) {
400 if(!pszAppName) {
401 if(!*cur->szAppName && !lstrcmpiW(pszClassName, cur->szClassName))
402 return cur;
404 else {
405 if(!lstrcmpiW(pszAppName, cur->szAppName) && !lstrcmpiW(pszClassName, cur->szClassName))
406 return cur;
408 cur = cur->next;
410 return NULL;
413 /***********************************************************************
414 * MSSTYLES_AddClass
416 * Add a class to a theme file
418 * PARAMS
419 * tf Theme file
420 * pszAppName App name to add
421 * pszClassName Class name to add
423 * RETURNS
424 * The class added, or a class previously added with the same name
426 PTHEME_CLASS MSSTYLES_AddClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
428 PTHEME_CLASS cur = MSSTYLES_FindClass(tf, pszAppName, pszClassName);
429 if(cur) return cur;
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;
442 /***********************************************************************
443 * MSSTYLES_FindPartState
445 * Find a part/state
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
453 * RETURNS
454 * The part/state found, or NULL
456 PTHEME_PARTSTATE MSSTYLES_FindPartState(PTHEME_CLASS tc, int iPartId, int iStateId, PTHEME_CLASS *tcNext)
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;
464 cur = cur->next;
466 if(tc->overrides) return MSSTYLES_FindPartState(tc->overrides, iPartId, iStateId, tcNext);
467 return NULL;
470 /***********************************************************************
471 * MSSTYLES_AddPartState
473 * Add a part/state to a class
475 * PARAMS
476 * tc Theme class
477 * iPartId Part ID to add
478 * iStateId State ID to add
480 * RETURNS
481 * The part/state added, or a part/state previously added with the same IDs
483 PTHEME_PARTSTATE MSSTYLES_AddPartState(PTHEME_CLASS tc, int iPartId, int iStateId)
485 PTHEME_PARTSTATE cur = MSSTYLES_FindPartState(tc, iPartId, iStateId, NULL);
486 if(cur) return cur;
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;
497 /***********************************************************************
498 * MSSTYLES_LFindProperty
500 * Find a property within a property list
502 * PARAMS
503 * tp property list to scan
504 * iPropertyPrimitive Type of value expected
505 * iPropertyId ID of the required value
507 * RETURNS
508 * The property found, or NULL
510 PTHEME_PROPERTY MSSTYLES_LFindProperty(PTHEME_PROPERTY tp, int iPropertyPrimitive, int iPropertyId)
512 PTHEME_PROPERTY cur = tp;
513 while(cur) {
514 if(cur->iPropertyId == iPropertyId) {
515 if(cur->iPrimitiveType == iPropertyPrimitive) {
516 return cur;
518 else {
519 if(!iPropertyPrimitive)
520 return cur;
521 return NULL;
524 cur = cur->next;
526 return NULL;
529 /***********************************************************************
530 * MSSTYLES_PSFindProperty
532 * Find a value within a part/state
534 * PARAMS
535 * ps Part/state to search
536 * iPropertyPrimitive Type of value expected
537 * iPropertyId ID of the required value
539 * RETURNS
540 * The property found, or NULL
542 static inline PTHEME_PROPERTY MSSTYLES_PSFindProperty(PTHEME_PARTSTATE ps, int iPropertyPrimitive, int iPropertyId)
544 return MSSTYLES_LFindProperty(ps->properties, iPropertyPrimitive, iPropertyId);
547 /***********************************************************************
548 * MSSTYLES_FFindMetric
550 * Find a metric property for a theme file
552 * PARAMS
553 * tf Theme file
554 * iPropertyPrimitive Type of value expected
555 * iPropertyId ID of the required value
557 * RETURNS
558 * The property found, or NULL
560 static inline PTHEME_PROPERTY MSSTYLES_FFindMetric(PTHEME_FILE tf, int iPropertyPrimitive, int iPropertyId)
562 return MSSTYLES_LFindProperty(tf->metrics, iPropertyPrimitive, iPropertyId);
565 /***********************************************************************
566 * MSSTYLES_FindMetric
568 * Find a metric property for the current installed theme
570 * PARAMS
571 * tf Theme file
572 * iPropertyPrimitive Type of value expected
573 * iPropertyId ID of the required value
575 * RETURNS
576 * The property found, or NULL
578 PTHEME_PROPERTY MSSTYLES_FindMetric(int iPropertyPrimitive, int iPropertyId)
580 if(!tfActiveTheme) return NULL;
581 return MSSTYLES_FFindMetric(tfActiveTheme, iPropertyPrimitive, iPropertyId);
584 /***********************************************************************
585 * MSSTYLES_AddProperty
587 * Add a property to a part/state
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
596 * RETURNS
597 * The property added, or a property previously added with the same IDs
599 PTHEME_PROPERTY MSSTYLES_AddProperty(PTHEME_PARTSTATE ps, int iPropertyPrimitive, int iPropertyId, LPCWSTR lpValue, DWORD dwValueLen, BOOL isGlobal)
601 PTHEME_PROPERTY cur = MSSTYLES_PSFindProperty(ps, iPropertyPrimitive, iPropertyId);
602 /* Should duplicate properties overwrite the original, or be ignored? */
603 if(cur) return cur;
605 cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PROPERTY));
606 cur->iPrimitiveType = iPropertyPrimitive;
607 cur->iPropertyId = iPropertyId;
608 cur->lpValue = lpValue;
609 cur->dwValueLen = dwValueLen;
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;
620 cur->next = ps->properties;
621 ps->properties = cur;
622 return cur;
625 /***********************************************************************
626 * MSSTYLES_AddMetric
628 * Add a property to a part/state
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
637 * RETURNS
638 * The property added, or a property previously added with the same IDs
640 PTHEME_PROPERTY MSSTYLES_AddMetric(PTHEME_FILE tf, int iPropertyPrimitive, int iPropertyId, LPCWSTR lpValue, DWORD dwValueLen)
642 PTHEME_PROPERTY cur = MSSTYLES_FFindMetric(tf, iPropertyPrimitive, iPropertyId);
643 /* Should duplicate properties overwrite the original, or be ignored? */
644 if(cur) return cur;
646 cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PROPERTY));
647 cur->iPrimitiveType = iPropertyPrimitive;
648 cur->iPropertyId = iPropertyId;
649 cur->lpValue = lpValue;
650 cur->dwValueLen = dwValueLen;
652 cur->origin = PO_GLOBAL;
654 cur->next = tf->metrics;
655 tf->metrics = cur;
656 return cur;
659 /***********************************************************************
660 * MSSTYLES_ParseThemeIni
662 * Parse the theme ini for the selected color/style
664 * PARAMS
665 * tf Theme to parse
667 void MSSTYLES_ParseThemeIni(PTHEME_FILE tf)
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;
687 ini = MSSTYLES_GetActiveThemeIni(tf);
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;
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);
708 else {
709 FIXME("Invalid color value for %s\n", debugstr_w(szPropertyName));
712 /* Catch all metrics, including colors */
713 MSSTYLES_AddMetric(tf, iPropertyPrimitive, iPropertyId, lpValue, dwValueLen);
715 else {
716 TRACE("Unknown system metric %s\n", debugstr_w(szPropertyName));
719 if(colorCount > 0)
720 SetSysColors(colorCount, colorElements, colorRgb);
721 continue;
723 if(MSSTYLES_ParseIniSectionName(lpName, dwLen, szAppName, szClassName, &iPartId, &iStateId)) {
724 BOOL isGlobal = FALSE;
725 if(!lstrcmpiW(szClassName, szGlobals)) {
726 isGlobal = TRUE;
728 cls = MSSTYLES_AddClass(tf, szAppName, szClassName);
729 ps = MSSTYLES_AddPartState(cls, iPartId, iStateId);
731 while((lpName=UXINI_GetNextValue(ini, &dwLen, &lpValue, &dwValueLen))) {
732 lstrcpynW(szPropertyName, lpName, min(dwLen+1, sizeof(szPropertyName)/sizeof(szPropertyName[0])));
733 if(MSSTYLES_LookupProperty(szPropertyName, &iPropertyPrimitive, &iPropertyId)) {
734 MSSTYLES_AddProperty(ps, iPropertyPrimitive, iPropertyId, lpValue, dwValueLen, isGlobal);
736 else {
737 TRACE("Unknown property %s\n", debugstr_w(szPropertyName));
743 /* App/Class combos override values defined by the base class, map these overrides */
744 globals = MSSTYLES_FindClass(tf, NULL, szGlobals);
745 cls = tf->classes;
746 while(cls) {
747 if(*cls->szAppName) {
748 cls->overrides = MSSTYLES_FindClass(tf, NULL, cls->szClassName);
749 if(!cls->overrides) {
750 TRACE("No overrides found for app %s class %s\n", debugstr_w(cls->szAppName), debugstr_w(cls->szClassName));
752 else {
753 cls->overrides = globals;
756 else {
757 /* Everything overrides globals..except globals */
758 if(cls != globals) cls->overrides = globals;
760 cls = cls->next;
762 UXINI_CloseINI(ini);
764 if(!tf->classes) {
765 ERR("Failed to parse theme ini\n");
769 /***********************************************************************
770 * MSSTYLES_OpenThemeClass
772 * Open a theme class, uses the current active theme
774 * PARAMS
775 * pszAppName Application name, for theme styles specific
776 * to a particular application
777 * pszClassList List of requested classes, semicolon delimited
779 PTHEME_CLASS MSSTYLES_OpenThemeClass(LPCWSTR pszAppName, LPCWSTR pszClassList)
781 PTHEME_CLASS cls = NULL;
782 WCHAR szClassName[MAX_THEME_CLASS_NAME];
783 LPCWSTR start;
784 LPCWSTR end;
785 DWORD len;
787 if(!tfActiveTheme) {
788 TRACE("there is no active theme\n");
789 return NULL;
791 if(!tfActiveTheme->classes) {
792 return NULL;
795 start = pszClassList;
796 while((end = StrChrW(start, ';'))) {
797 len = end-start;
798 lstrcpynW(szClassName, start, min(len+1, sizeof(szClassName)/sizeof(szClassName[0])));
799 start = end+1;
800 cls = MSSTYLES_FindClass(tfActiveTheme, pszAppName, szClassName);
801 if(cls) break;
803 if(!cls && *start) {
804 lstrcpynW(szClassName, start, sizeof(szClassName)/sizeof(szClassName[0]));
805 cls = MSSTYLES_FindClass(tfActiveTheme, pszAppName, szClassName);
807 if(cls) {
808 TRACE("Opened app %s, class %s from list %s\n", debugstr_w(cls->szAppName), debugstr_w(cls->szClassName), debugstr_w(pszClassList));
810 return cls;
813 /***********************************************************************
814 * MSSTYLES_CloseThemeClass
816 * Close a theme class
818 * PARAMS
819 * tc Theme class to close
821 * NOTES
822 * There is currently no need clean anything up for theme classes,
823 * so do nothing for now
825 HRESULT MSSTYLES_CloseThemeClass(PTHEME_CLASS tc)
827 return S_OK;
830 /***********************************************************************
831 * MSSTYLES_FindProperty
833 * Locate a property in a class. Part and state IDs will be used as a
834 * preference, but may be ignored in the attempt to locate the property.
835 * Will scan the entire chain of overrides for this class.
837 PTHEME_PROPERTY MSSTYLES_FindProperty(PTHEME_CLASS tc, int iPartId, int iStateId, int iPropertyPrimitive, int iPropertyId)
839 PTHEME_CLASS next = tc;
840 PTHEME_PARTSTATE ps;
841 PTHEME_PROPERTY tp;
843 TRACE("(%p, %d, %d, %d)\n", tc, iPartId, iStateId, iPropertyId);
844 /* Try and find an exact match on part & state */
845 while(next && (ps = MSSTYLES_FindPartState(next, iPartId, iStateId, &next))) {
846 if((tp = MSSTYLES_PSFindProperty(ps, iPropertyPrimitive, iPropertyId))) {
847 return tp;
850 /* If that fails, and we didn't already try it, search for just part */
851 if(iStateId != 0)
852 iStateId = 0;
853 /* As a last ditch attempt..go for just class */
854 else if(iPartId != 0)
855 iPartId = 0;
856 else
857 return NULL;
859 if((tp = MSSTYLES_FindProperty(tc, iPartId, iStateId, iPropertyPrimitive, iPropertyId)))
860 return tp;
861 return NULL;
864 HBITMAP MSSTYLES_LoadBitmap(HDC hdc, PTHEME_CLASS tc, LPCWSTR lpFilename)
866 WCHAR szFile[MAX_PATH];
867 LPWSTR tmp;
868 lstrcpynW(szFile, lpFilename, sizeof(szFile)/sizeof(szFile[0]));
869 tmp = szFile;
870 do {
871 if(*tmp == '\\') *tmp = '_';
872 if(*tmp == '/') *tmp = '_';
873 if(*tmp == '.') *tmp = '_';
874 } while(*tmp++);
875 return LoadImageW(tc->hTheme, szFile, IMAGE_BITMAP, 0, 0, LR_SHARED|LR_CREATEDIBSECTION);
878 BOOL MSSTYLES_GetNextInteger(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, int *value)
880 LPCWSTR cur = lpStringStart;
881 int total = 0;
882 BOOL gotNeg = FALSE;
884 while(cur < lpStringEnd && (*cur < '0' || *cur > '9' || *cur == '-')) cur++;
885 if(cur >= lpStringEnd) {
886 return FALSE;
888 if(*cur == '-') {
889 cur++;
890 gotNeg = TRUE;
892 while(cur < lpStringEnd && (*cur >= '0' && *cur <= '9')) {
893 total = total * 10 + (*cur - '0');
894 cur++;
896 if(gotNeg) total = -total;
897 *value = total;
898 if(lpValEnd) *lpValEnd = cur;
899 return TRUE;
902 BOOL MSSTYLES_GetNextToken(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LPWSTR lpBuff, DWORD buffSize) {
903 LPCWSTR cur = lpStringStart;
904 LPCWSTR start;
905 LPCWSTR end;
907 while(cur < lpStringEnd && (isspace(*cur) || *cur == ',')) cur++;
908 if(cur >= lpStringEnd) {
909 return FALSE;
911 start = cur;
912 while(cur < lpStringEnd && *cur != ',') cur++;
913 end = cur;
914 while(isspace(*end)) end--;
916 lstrcpynW(lpBuff, start, min(buffSize, end-start+1));
918 if(lpValEnd) *lpValEnd = cur;
919 return TRUE;
922 /***********************************************************************
923 * MSSTYLES_GetPropertyBool
925 * Retrieve a color value for a property
927 HRESULT MSSTYLES_GetPropertyBool(PTHEME_PROPERTY tp, BOOL *pfVal)
929 *pfVal = FALSE;
930 if(*tp->lpValue == 't' || *tp->lpValue == 'T')
931 *pfVal = TRUE;
932 return S_OK;
935 /***********************************************************************
936 * MSSTYLES_GetPropertyColor
938 * Retrieve a color value for a property
940 HRESULT MSSTYLES_GetPropertyColor(PTHEME_PROPERTY tp, COLORREF *pColor)
942 LPCWSTR lpEnd;
943 LPCWSTR lpCur;
944 int red, green, blue;
946 lpCur = tp->lpValue;
947 lpEnd = tp->lpValue + tp->dwValueLen;
949 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &red);
950 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &green);
951 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &blue)) {
952 TRACE("Could not parse color property\n");
953 return E_PROP_ID_UNSUPPORTED;
955 *pColor = RGB(red,green,blue);
956 return S_OK;
959 /***********************************************************************
960 * MSSTYLES_GetPropertyColor
962 * Retrieve a color value for a property
964 HRESULT MSSTYLES_GetPropertyFont(PTHEME_PROPERTY tp, HDC hdc, LOGFONTW *pFont)
966 static const WCHAR szBold[] = {'b','o','l','d','\0'};
967 static const WCHAR szItalic[] = {'i','t','a','l','i','c','\0'};
968 static const WCHAR szUnderline[] = {'u','n','d','e','r','l','i','n','e','\0'};
969 static const WCHAR szStrikeOut[] = {'s','t','r','i','k','e','o','u','t','\0'};
970 int pointSize;
971 WCHAR attr[32];
972 LPCWSTR lpCur = tp->lpValue;
973 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
975 ZeroMemory(pFont, sizeof(LOGFONTW));
977 if(!MSSTYLES_GetNextToken(lpCur, lpEnd, &lpCur, pFont->lfFaceName, LF_FACESIZE)) {
978 TRACE("Property is there, but failed to get face name\n");
979 return E_PROP_ID_UNSUPPORTED;
981 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pointSize)) {
982 TRACE("Property is there, but failed to get point size\n");
983 return E_PROP_ID_UNSUPPORTED;
985 pFont->lfHeight = -MulDiv(pointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
986 pFont->lfWeight = FW_REGULAR;
987 pFont->lfCharSet = DEFAULT_CHARSET;
988 while(MSSTYLES_GetNextToken(lpCur, lpEnd, &lpCur, attr, sizeof(attr)/sizeof(attr[0]))) {
989 if(!lstrcmpiW(szBold, attr)) pFont->lfWeight = FW_BOLD;
990 else if(!!lstrcmpiW(szItalic, attr)) pFont->lfItalic = TRUE;
991 else if(!!lstrcmpiW(szUnderline, attr)) pFont->lfUnderline = TRUE;
992 else if(!!lstrcmpiW(szStrikeOut, attr)) pFont->lfStrikeOut = TRUE;
994 return S_OK;
997 /***********************************************************************
998 * MSSTYLES_GetPropertyInt
1000 * Retrieve an int value for a property
1002 HRESULT MSSTYLES_GetPropertyInt(PTHEME_PROPERTY tp, int *piVal)
1004 if(!MSSTYLES_GetNextInteger(tp->lpValue, (tp->lpValue + tp->dwValueLen), NULL, piVal)) {
1005 TRACE("Could not parse int property\n");
1006 return E_PROP_ID_UNSUPPORTED;
1008 return S_OK;
1011 /***********************************************************************
1012 * MSSTYLES_GetPropertyIntList
1014 * Retrieve an int list value for a property
1016 HRESULT MSSTYLES_GetPropertyIntList(PTHEME_PROPERTY tp, INTLIST *pIntList)
1018 int i;
1019 LPCWSTR lpCur = tp->lpValue;
1020 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1022 for(i=0; i < MAX_INTLIST_COUNT; i++) {
1023 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pIntList->iValues[i]))
1024 break;
1026 pIntList->iValueCount = i;
1027 return S_OK;
1030 /***********************************************************************
1031 * MSSTYLES_GetPropertyPosition
1033 * Retrieve a position value for a property
1035 HRESULT MSSTYLES_GetPropertyPosition(PTHEME_PROPERTY tp, POINT *pPoint)
1037 int x,y;
1038 LPCWSTR lpCur = tp->lpValue;
1039 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1041 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &x);
1042 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &y)) {
1043 TRACE("Could not parse position property\n");
1044 return E_PROP_ID_UNSUPPORTED;
1046 pPoint->x = x;
1047 pPoint->y = y;
1048 return S_OK;
1051 /***********************************************************************
1052 * MSSTYLES_GetPropertyString
1054 * Retrieve a string value for a property
1056 HRESULT MSSTYLES_GetPropertyString(PTHEME_PROPERTY tp, LPWSTR pszBuff, int cchMaxBuffChars)
1058 lstrcpynW(pszBuff, tp->lpValue, min(tp->dwValueLen+1, cchMaxBuffChars));
1059 return S_OK;
1062 /***********************************************************************
1063 * MSSTYLES_GetPropertyRect
1065 * Retrieve a rect value for a property
1067 HRESULT MSSTYLES_GetPropertyRect(PTHEME_PROPERTY tp, RECT *pRect)
1069 LPCWSTR lpCur = tp->lpValue;
1070 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1072 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->left);
1073 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->top);
1074 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->right);
1075 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->bottom)) {
1076 TRACE("Could not parse rect property\n");
1077 return E_PROP_ID_UNSUPPORTED;
1079 return S_OK;
1082 /***********************************************************************
1083 * MSSTYLES_GetPropertyMargins
1085 * Retrieve a margins value for a property
1087 HRESULT MSSTYLES_GetPropertyMargins(PTHEME_PROPERTY tp, RECT *prc, MARGINS *pMargins)
1089 LPCWSTR lpCur = tp->lpValue;
1090 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1092 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cxLeftWidth);
1093 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cxRightWidth);
1094 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cyTopHeight);
1095 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cyBottomHeight)) {
1096 TRACE("Could not parse margins property\n");
1097 return E_PROP_ID_UNSUPPORTED;
1099 return S_OK;