Fixed a couple of crashes.
[wine.git] / dlls / uxtheme / msstyles.c
blob9f63afee31351b260c1d56f408409cef1732870a
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);
49 extern HINSTANCE hDllInst;
51 #define MSSTYLES_VERSION 0x0003
53 static const WCHAR szThemesIniResource[] = {
54 't','h','e','m','e','s','_','i','n','i','\0'
57 PTHEME_FILE tfActiveTheme = NULL;
59 /***********************************************************************/
61 /**********************************************************************
62 * MSSTYLES_OpenThemeFile
64 * Load and validate a theme
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
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.
76 HRESULT MSSTYLES_OpenThemeFile(LPCWSTR lpThemeFile, LPCWSTR pszColorName, LPCWSTR pszSizeName, PTHEME_FILE *tf)
78 HMODULE hTheme;
79 HRSRC hrsc;
80 HRESULT hr = S_OK;
81 WCHAR szPackThemVersionResource[] = {
82 'P','A','C','K','T','H','E','M','_','V','E','R','S','I','O','N', '\0'
84 WCHAR szColorNamesResource[] = {
85 'C','O','L','O','R','N','A','M','E','S','\0'
87 WCHAR szSizeNamesResource[] = {
88 'S','I','Z','E','N','A','M','E','S','\0'
91 WORD version;
92 DWORD versize;
93 LPWSTR pszColors;
94 LPWSTR pszSelectedColor = NULL;
95 LPWSTR pszSizes;
96 LPWSTR pszSelectedSize = NULL;
97 LPWSTR tmp;
99 TRACE("Opening %s\n", debugstr_w(lpThemeFile));
101 hTheme = LoadLibraryExW(lpThemeFile, NULL, LOAD_LIBRARY_AS_DATAFILE);
103 /* Validate that this is really a theme */
104 if(!hTheme) {
105 hr = HRESULT_FROM_WIN32(GetLastError());
106 goto invalid_theme;
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;
113 if((versize = SizeofResource(hTheme, hrsc)) != 2)
115 TRACE("Version resource found, but wrong size: %ld\n", versize);
116 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
117 goto invalid_theme;
119 version = *(WORD*)LoadResource(hTheme, hrsc);
120 if(version != MSSTYLES_VERSION)
122 TRACE("Version of theme file is unsupported: 0x%04x\n", version);
123 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
124 goto invalid_theme;
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;
132 pszColors = (LPWSTR)LoadResource(hTheme, hrsc);
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;
139 pszSizes = (LPWSTR)LoadResource(hTheme, hrsc);
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;
149 tmp += lstrlenW(tmp)+1;
152 else
153 pszSelectedColor = pszColors; /* Use the default color */
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;
163 tmp += lstrlenW(tmp)+1;
166 else
167 pszSelectedSize = pszSizes; /* Use the default size */
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;
176 *tf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(THEME_FILE));
177 (*tf)->hTheme = hTheme;
179 GetFullPathNameW(lpThemeFile, MAX_PATH, (*tf)->szThemeFile, NULL);
181 (*tf)->pszAvailColors = pszColors;
182 (*tf)->pszAvailSizes = pszSizes;
183 (*tf)->pszSelectedColor = pszSelectedColor;
184 (*tf)->pszSelectedSize = pszSelectedSize;
185 (*tf)->dwRefCount = 1;
186 return S_OK;
188 invalid_theme:
189 if(hTheme) FreeLibrary(hTheme);
190 return hr;
193 /***********************************************************************
194 * MSSTYLES_CloseThemeFile
196 * Close theme file and free resources
198 void MSSTYLES_CloseThemeFile(PTHEME_FILE tf)
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);
213 HeapFree(GetProcessHeap(), 0, pcls);
216 HeapFree(GetProcessHeap(), 0, tf);
221 /***********************************************************************
222 * MSSTYLES_SetActiveTheme
224 * Set the current active theme
226 HRESULT MSSTYLES_SetActiveTheme(PTHEME_FILE tf)
228 if(tfActiveTheme)
229 MSSTYLES_CloseThemeFile(tfActiveTheme);
230 tfActiveTheme = tf;
231 if (tfActiveTheme)
232 tfActiveTheme->dwRefCount++;
233 return S_OK;
236 /***********************************************************************
237 * MSSTYLES_GetThemeIni
239 * Retrieves themes.ini from a theme
241 PUXINI_FILE MSSTYLES_GetThemeIni(PTHEME_FILE tf)
243 return UXINI_LoadINI(tf->hTheme, szThemesIniResource);
246 /***********************************************************************
247 * MSSTYLES_GetActiveThemeIni
249 * Retrieve the ini file for the selected color/style
251 PUXINI_FILE MSSTYLES_GetActiveThemeIni(PTHEME_FILE tf)
253 WCHAR szFileResNamesResource[] = {
254 'F','I','L','E','R','E','S','N','A','M','E','S','\0'
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;
265 /* Count the number of available colors & styles, and determine the index number
266 of the color/style we are interested in
268 tmp = tf->pszAvailColors;
269 while(*tmp) {
270 if(!lstrcmpiW(tf->pszSelectedColor, tmp))
271 dwColorNum = dwColorCount;
272 tmp += lstrlenW(tmp)+1;
273 dwColorCount++;
275 tmp = tf->pszAvailSizes;
276 while(*tmp) {
277 if(!lstrcmpiW(tf->pszSelectedSize, tmp))
278 dwSizeNum = dwSizeCount;
279 tmp += lstrlenW(tmp)+1;
280 dwSizeCount++;
283 if(!(hrsc = FindResourceW(tf->hTheme, MAKEINTRESOURCEW(1), szFileResNamesResource))) {
284 TRACE("FILERESNAMES map not found\n");
285 return NULL;
287 tmp = (LPWSTR)LoadResource(tf->hTheme, hrsc);
288 dwResourceIndex = (dwSizeCount * dwColorNum) + dwSizeNum;
289 for(i=0; i < dwResourceIndex; i++) {
290 tmp += lstrlenW(tmp)+1;
292 return UXINI_LoadINI(tf->hTheme, tmp);
296 /***********************************************************************
297 * MSSTYLES_ParseIniSectionName
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)]
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
318 BOOL MSSTYLES_ParseIniSectionName(LPCWSTR lpSection, DWORD dwLen, LPWSTR szAppName, LPWSTR szClassName, int *iPartId, int *iStateId)
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])));
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;
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]));
356 else {
357 lstrcpynW(part, comp, sizeof(part)/sizeof(part[0]));
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]));
370 else {
371 lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
374 if(!*szClassName) return FALSE;
375 return MSSTYLES_LookupPartState(szClassName, part[0]?part:NULL, state[0]?state:NULL, iPartId, iStateId);
378 /***********************************************************************
379 * MSSTYLES_FindClass
381 * Find a class
383 * PARAMS
384 * tf Theme file
385 * pszAppName App name to find
386 * pszClassName Class name to find
388 * RETURNS
389 * The class found, or NULL
391 PTHEME_CLASS MSSTYLES_FindClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
393 PTHEME_CLASS cur = tf->classes;
394 while(cur) {
395 if(!pszAppName) {
396 if(!*cur->szAppName && !lstrcmpiW(pszClassName, cur->szClassName))
397 return cur;
399 else {
400 if(!lstrcmpiW(pszAppName, cur->szAppName) && !lstrcmpiW(pszClassName, cur->szClassName))
401 return cur;
403 cur = cur->next;
405 return NULL;
408 /***********************************************************************
409 * MSSTYLES_AddClass
411 * Add a class to a theme file
413 * PARAMS
414 * tf Theme file
415 * pszAppName App name to add
416 * pszClassName Class name to add
418 * RETURNS
419 * The class added, or a class previously added with the same name
421 PTHEME_CLASS MSSTYLES_AddClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
423 PTHEME_CLASS cur = MSSTYLES_FindClass(tf, pszAppName, pszClassName);
424 if(cur) return cur;
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;
437 /***********************************************************************
438 * MSSTYLES_FindPartState
440 * Find a part/state
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
448 * RETURNS
449 * The part/state found, or NULL
451 PTHEME_PARTSTATE MSSTYLES_FindPartState(PTHEME_CLASS tc, int iPartId, int iStateId, PTHEME_CLASS *tcNext)
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;
459 cur = cur->next;
461 if(tc->overrides) return MSSTYLES_FindPartState(tc->overrides, iPartId, iStateId, tcNext);
462 return NULL;
465 /***********************************************************************
466 * MSSTYLES_AddPartState
468 * Add a part/state to a class
470 * PARAMS
471 * tc Theme class
472 * iPartId Part ID to add
473 * iStateId State ID to add
475 * RETURNS
476 * The part/state added, or a part/state previously added with the same IDs
478 PTHEME_PARTSTATE MSSTYLES_AddPartState(PTHEME_CLASS tc, int iPartId, int iStateId)
480 PTHEME_PARTSTATE cur = MSSTYLES_FindPartState(tc, iPartId, iStateId, NULL);
481 if(cur) return cur;
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;
492 /***********************************************************************
493 * MSSTYLES_LFindProperty
495 * Find a property within a property list
497 * PARAMS
498 * tp property list to scan
499 * iPropertyPrimitive Type of value expected
500 * iPropertyId ID of the required value
502 * RETURNS
503 * The property found, or NULL
505 PTHEME_PROPERTY MSSTYLES_LFindProperty(PTHEME_PROPERTY tp, int iPropertyPrimitive, int iPropertyId)
507 PTHEME_PROPERTY cur = tp;
508 while(cur) {
509 if(cur->iPropertyId == iPropertyId) {
510 if(cur->iPrimitiveType == iPropertyPrimitive) {
511 return cur;
513 else {
514 if(!iPropertyPrimitive)
515 return cur;
516 return NULL;
519 cur = cur->next;
521 return NULL;
524 /***********************************************************************
525 * MSSTYLES_PSFindProperty
527 * Find a value within a part/state
529 * PARAMS
530 * ps Part/state to search
531 * iPropertyPrimitive Type of value expected
532 * iPropertyId ID of the required value
534 * RETURNS
535 * The property found, or NULL
537 static inline PTHEME_PROPERTY MSSTYLES_PSFindProperty(PTHEME_PARTSTATE ps, int iPropertyPrimitive, int iPropertyId)
539 return MSSTYLES_LFindProperty(ps->properties, iPropertyPrimitive, iPropertyId);
542 /***********************************************************************
543 * MSSTYLES_FFindMetric
545 * Find a metric property for a theme file
547 * PARAMS
548 * tf Theme file
549 * iPropertyPrimitive Type of value expected
550 * iPropertyId ID of the required value
552 * RETURNS
553 * The property found, or NULL
555 static inline PTHEME_PROPERTY MSSTYLES_FFindMetric(PTHEME_FILE tf, int iPropertyPrimitive, int iPropertyId)
557 return MSSTYLES_LFindProperty(tf->metrics, iPropertyPrimitive, iPropertyId);
560 /***********************************************************************
561 * MSSTYLES_FindMetric
563 * Find a metric property for the current installed theme
565 * PARAMS
566 * tf Theme file
567 * iPropertyPrimitive Type of value expected
568 * iPropertyId ID of the required value
570 * RETURNS
571 * The property found, or NULL
573 PTHEME_PROPERTY MSSTYLES_FindMetric(int iPropertyPrimitive, int iPropertyId)
575 if(!tfActiveTheme) return NULL;
576 return MSSTYLES_FFindMetric(tfActiveTheme, iPropertyPrimitive, iPropertyId);
579 /***********************************************************************
580 * MSSTYLES_AddProperty
582 * Add a property to a part/state
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
591 * RETURNS
592 * The property added, or a property previously added with the same IDs
594 PTHEME_PROPERTY MSSTYLES_AddProperty(PTHEME_PARTSTATE ps, int iPropertyPrimitive, int iPropertyId, LPCWSTR lpValue, DWORD dwValueLen, BOOL isGlobal)
596 PTHEME_PROPERTY cur = MSSTYLES_PSFindProperty(ps, iPropertyPrimitive, iPropertyId);
597 /* Should duplicate properties overwrite the original, or be ignored? */
598 if(cur) return cur;
600 cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PROPERTY));
601 cur->iPrimitiveType = iPropertyPrimitive;
602 cur->iPropertyId = iPropertyId;
603 cur->lpValue = lpValue;
604 cur->dwValueLen = dwValueLen;
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;
615 cur->next = ps->properties;
616 ps->properties = cur;
617 return cur;
620 /***********************************************************************
621 * MSSTYLES_AddMetric
623 * Add a property to a part/state
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
632 * RETURNS
633 * The property added, or a property previously added with the same IDs
635 PTHEME_PROPERTY MSSTYLES_AddMetric(PTHEME_FILE tf, int iPropertyPrimitive, int iPropertyId, LPCWSTR lpValue, DWORD dwValueLen)
637 PTHEME_PROPERTY cur = MSSTYLES_FFindMetric(tf, iPropertyPrimitive, iPropertyId);
638 /* Should duplicate properties overwrite the original, or be ignored? */
639 if(cur) return cur;
641 cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_PROPERTY));
642 cur->iPrimitiveType = iPropertyPrimitive;
643 cur->iPropertyId = iPropertyId;
644 cur->lpValue = lpValue;
645 cur->dwValueLen = dwValueLen;
647 cur->origin = PO_GLOBAL;
649 cur->next = tf->metrics;
650 tf->metrics = cur;
651 return cur;
654 /***********************************************************************
655 * MSSTYLES_ParseThemeIni
657 * Parse the theme ini for the selected color/style
659 * PARAMS
660 * tf Theme to parse
662 void MSSTYLES_ParseThemeIni(PTHEME_FILE tf)
664 WCHAR szSysMetrics[] = {'S','y','s','M','e','t','r','i','c','s','\0'};
665 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;
682 ini = MSSTYLES_GetActiveThemeIni(tf);
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;
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);
703 else {
704 FIXME("Invalid color value for %s\n", debugstr_w(szPropertyName));
707 /* Catch all metrics, including colors */
708 MSSTYLES_AddMetric(tf, iPropertyPrimitive, iPropertyId, lpValue, dwValueLen);
710 else {
711 TRACE("Unknown system metric %s\n", debugstr_w(szPropertyName));
714 if(colorCount > 0)
715 SetSysColors(colorCount, colorElements, colorRgb);
716 continue;
718 if(MSSTYLES_ParseIniSectionName(lpName, dwLen, szAppName, szClassName, &iPartId, &iStateId)) {
719 BOOL isGlobal = FALSE;
720 if(!lstrcmpiW(szClassName, szGlobals)) {
721 isGlobal = TRUE;
723 cls = MSSTYLES_AddClass(tf, szAppName, szClassName);
724 ps = MSSTYLES_AddPartState(cls, iPartId, iStateId);
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);
731 else {
732 TRACE("Unknown property %s\n", debugstr_w(szPropertyName));
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));
747 else {
748 cls->overrides = globals;
751 else {
752 /* Everything overrides globals..except globals */
753 if(cls != globals) cls->overrides = globals;
755 cls = cls->next;
757 UXINI_CloseINI(ini);
759 if(!tf->classes) {
760 ERR("Failed to parse theme ini\n");
764 /***********************************************************************
765 * MSSTYLES_OpenThemeClass
767 * Open a theme class, uses the current active theme
769 * PARAMS
770 * pszAppName Application name, for theme styles specific
771 * to a particular application
772 * pszClassList List of requested classes, semicolon delimited
774 PTHEME_CLASS MSSTYLES_OpenThemeClass(LPCWSTR pszAppName, LPCWSTR pszClassList)
776 PTHEME_CLASS cls = NULL;
777 WCHAR szClassName[MAX_THEME_CLASS_NAME];
778 LPCWSTR start;
779 LPCWSTR end;
780 DWORD len;
782 if(!tfActiveTheme) {
783 TRACE("there is no active theme\n");
784 return NULL;
786 if(!tfActiveTheme->classes) {
787 MSSTYLES_ParseThemeIni(tfActiveTheme);
788 if(!tfActiveTheme->classes)
789 return NULL;
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;
800 if(!cls && *start) {
801 lstrcpynW(szClassName, start, sizeof(szClassName)/sizeof(szClassName[0]));
802 cls = MSSTYLES_FindClass(tfActiveTheme, pszAppName, szClassName);
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));
807 return cls;
810 /***********************************************************************
811 * MSSTYLES_CloseThemeClass
813 * Close a theme class
815 * PARAMS
816 * tc Theme class to close
818 * NOTES
819 * There is currently no need clean anything up for theme classes,
820 * so do nothing for now
822 HRESULT MSSTYLES_CloseThemeClass(PTHEME_CLASS tc)
824 return S_OK;
827 /***********************************************************************
828 * MSSTYLES_FindProperty
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.
834 PTHEME_PROPERTY MSSTYLES_FindProperty(PTHEME_CLASS tc, int iPartId, int iStateId, int iPropertyPrimitive, int iPropertyId)
836 PTHEME_CLASS next = tc;
837 PTHEME_PARTSTATE ps;
838 PTHEME_PROPERTY tp;
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;
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;
856 if((tp = MSSTYLES_FindProperty(tc, iPartId, iStateId, iPropertyPrimitive, iPropertyId)))
857 return tp;
858 return NULL;
861 HBITMAP MSSTYLES_LoadBitmap(HDC hdc, PTHEME_CLASS tc, LPCWSTR lpFilename)
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);
875 BOOL MSSTYLES_GetNextInteger(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, int *value)
877 LPCWSTR cur = lpStringStart;
878 int total = 0;
879 BOOL gotNeg = FALSE;
881 while(cur < lpStringEnd && (*cur < '0' || *cur > '9' || *cur == '-')) cur++;
882 if(cur >= lpStringEnd) {
883 return FALSE;
885 if(*cur == '-') {
886 cur++;
887 gotNeg = TRUE;
889 while(cur < lpStringEnd && (*cur >= '0' && *cur <= '9')) {
890 total = total * 10 + (*cur - '0');
891 cur++;
893 if(gotNeg) total = -total;
894 *value = total;
895 if(lpValEnd) *lpValEnd = cur;
896 return TRUE;
899 BOOL MSSTYLES_GetNextToken(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LPWSTR lpBuff, DWORD buffSize) {
900 LPCWSTR cur = lpStringStart;
901 LPCWSTR start;
902 LPCWSTR end;
904 while(cur < lpStringEnd && (isspace(*cur) || *cur == ',')) cur++;
905 if(cur >= lpStringEnd) {
906 return FALSE;
908 start = cur;
909 while(cur < lpStringEnd && *cur != ',') cur++;
910 end = cur;
911 while(isspace(*end)) end--;
913 lstrcpynW(lpBuff, start, min(buffSize, end-start+1));
915 if(lpValEnd) *lpValEnd = cur;
916 return TRUE;
919 /***********************************************************************
920 * MSSTYLES_GetPropertyBool
922 * Retrieve a color value for a property
924 HRESULT MSSTYLES_GetPropertyBool(PTHEME_PROPERTY tp, BOOL *pfVal)
926 *pfVal = FALSE;
927 if(*tp->lpValue == 't' || *tp->lpValue == 'T')
928 *pfVal = TRUE;
929 return S_OK;
932 /***********************************************************************
933 * MSSTYLES_GetPropertyColor
935 * Retrieve a color value for a property
937 HRESULT MSSTYLES_GetPropertyColor(PTHEME_PROPERTY tp, COLORREF *pColor)
939 LPCWSTR lpEnd;
940 LPCWSTR lpCur;
941 int red, green, blue;
943 lpCur = tp->lpValue;
944 lpEnd = tp->lpValue + tp->dwValueLen;
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;
952 *pColor = RGB(red,green,blue);
953 return S_OK;
956 /***********************************************************************
957 * MSSTYLES_GetPropertyColor
959 * Retrieve a color value for a property
961 HRESULT MSSTYLES_GetPropertyFont(PTHEME_PROPERTY tp, HDC hdc, LOGFONTW *pFont)
963 const WCHAR szBold[] = {'b','o','l','d','\0'};
964 const WCHAR szItalic[] = {'i','t','a','l','i','c','\0'};
965 const WCHAR szUnderline[] = {'u','n','d','e','r','l','i','n','e','\0'};
966 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;
972 ZeroMemory(pFont, sizeof(LOGFONTW));
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;
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;
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;
991 return S_OK;
994 /***********************************************************************
995 * MSSTYLES_GetPropertyInt
997 * Retrieve an int value for a property
999 HRESULT MSSTYLES_GetPropertyInt(PTHEME_PROPERTY tp, int *piVal)
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;
1005 return S_OK;
1008 /***********************************************************************
1009 * MSSTYLES_GetPropertyIntList
1011 * Retrieve an int list value for a property
1013 HRESULT MSSTYLES_GetPropertyIntList(PTHEME_PROPERTY tp, INTLIST *pIntList)
1015 int i;
1016 LPCWSTR lpCur = tp->lpValue;
1017 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1019 for(i=0; i < MAX_INTLIST_COUNT; i++) {
1020 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pIntList->iValues[i]))
1021 break;
1023 pIntList->iValueCount = i;
1024 return S_OK;
1027 /***********************************************************************
1028 * MSSTYLES_GetPropertyPosition
1030 * Retrieve a position value for a property
1032 HRESULT MSSTYLES_GetPropertyPosition(PTHEME_PROPERTY tp, POINT *pPoint)
1034 int x,y;
1035 LPCWSTR lpCur = tp->lpValue;
1036 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
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;
1043 pPoint->x = x;
1044 pPoint->y = y;
1045 return S_OK;
1048 /***********************************************************************
1049 * MSSTYLES_GetPropertyString
1051 * Retrieve a string value for a property
1053 HRESULT MSSTYLES_GetPropertyString(PTHEME_PROPERTY tp, LPWSTR pszBuff, int cchMaxBuffChars)
1055 lstrcpynW(pszBuff, tp->lpValue, min(tp->dwValueLen+1, cchMaxBuffChars));
1056 return S_OK;
1059 /***********************************************************************
1060 * MSSTYLES_GetPropertyRect
1062 * Retrieve a rect value for a property
1064 HRESULT MSSTYLES_GetPropertyRect(PTHEME_PROPERTY tp, RECT *pRect)
1066 LPCWSTR lpCur = tp->lpValue;
1067 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
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;
1076 return S_OK;
1079 /***********************************************************************
1080 * MSSTYLES_GetPropertyMargins
1082 * Retrieve a margins value for a property
1084 HRESULT MSSTYLES_GetPropertyMargins(PTHEME_PROPERTY tp, RECT *prc, MARGINS *pMargins)
1086 LPCWSTR lpCur = tp->lpValue;
1087 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
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;
1096 return S_OK;