Added WC_STATIC.
[wine/hacks.git] / dlls / uxtheme / msstyles.c
blobe1e20d6664c4ffd1f5ae82f4f46b49c294a0a964
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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 else if (iPropertyId == TMT_FLATMENUS) {
713 BOOL flatMenus = (*lpValue == 'T') || (*lpValue == 't');
714 SystemParametersInfoW (SPI_SETFLATMENU, 0, (PVOID)flatMenus, 0);
716 /* Catch all metrics, including colors */
717 MSSTYLES_AddMetric(tf, iPropertyPrimitive, iPropertyId, lpValue, dwValueLen);
719 else {
720 TRACE("Unknown system metric %s\n", debugstr_w(szPropertyName));
723 if(colorCount > 0)
724 SetSysColors(colorCount, colorElements, colorRgb);
725 continue;
727 if(MSSTYLES_ParseIniSectionName(lpName, dwLen, szAppName, szClassName, &iPartId, &iStateId)) {
728 BOOL isGlobal = FALSE;
729 if(!lstrcmpiW(szClassName, szGlobals)) {
730 isGlobal = TRUE;
732 cls = MSSTYLES_AddClass(tf, szAppName, szClassName);
733 ps = MSSTYLES_AddPartState(cls, iPartId, iStateId);
735 while((lpName=UXINI_GetNextValue(ini, &dwLen, &lpValue, &dwValueLen))) {
736 lstrcpynW(szPropertyName, lpName, min(dwLen+1, sizeof(szPropertyName)/sizeof(szPropertyName[0])));
737 if(MSSTYLES_LookupProperty(szPropertyName, &iPropertyPrimitive, &iPropertyId)) {
738 MSSTYLES_AddProperty(ps, iPropertyPrimitive, iPropertyId, lpValue, dwValueLen, isGlobal);
740 else {
741 TRACE("Unknown property %s\n", debugstr_w(szPropertyName));
747 /* App/Class combos override values defined by the base class, map these overrides */
748 globals = MSSTYLES_FindClass(tf, NULL, szGlobals);
749 cls = tf->classes;
750 while(cls) {
751 if(*cls->szAppName) {
752 cls->overrides = MSSTYLES_FindClass(tf, NULL, cls->szClassName);
753 if(!cls->overrides) {
754 TRACE("No overrides found for app %s class %s\n", debugstr_w(cls->szAppName), debugstr_w(cls->szClassName));
756 else {
757 cls->overrides = globals;
760 else {
761 /* Everything overrides globals..except globals */
762 if(cls != globals) cls->overrides = globals;
764 cls = cls->next;
766 UXINI_CloseINI(ini);
768 if(!tf->classes) {
769 ERR("Failed to parse theme ini\n");
773 /***********************************************************************
774 * MSSTYLES_OpenThemeClass
776 * Open a theme class, uses the current active theme
778 * PARAMS
779 * pszAppName Application name, for theme styles specific
780 * to a particular application
781 * pszClassList List of requested classes, semicolon delimited
783 PTHEME_CLASS MSSTYLES_OpenThemeClass(LPCWSTR pszAppName, LPCWSTR pszClassList)
785 PTHEME_CLASS cls = NULL;
786 WCHAR szClassName[MAX_THEME_CLASS_NAME];
787 LPCWSTR start;
788 LPCWSTR end;
789 DWORD len;
791 if(!tfActiveTheme) {
792 TRACE("there is no active theme\n");
793 return NULL;
795 if(!tfActiveTheme->classes) {
796 return NULL;
799 start = pszClassList;
800 while((end = StrChrW(start, ';'))) {
801 len = end-start;
802 lstrcpynW(szClassName, start, min(len+1, sizeof(szClassName)/sizeof(szClassName[0])));
803 start = end+1;
804 cls = MSSTYLES_FindClass(tfActiveTheme, pszAppName, szClassName);
805 if(cls) break;
807 if(!cls && *start) {
808 lstrcpynW(szClassName, start, sizeof(szClassName)/sizeof(szClassName[0]));
809 cls = MSSTYLES_FindClass(tfActiveTheme, pszAppName, szClassName);
811 if(cls) {
812 TRACE("Opened app %s, class %s from list %s\n", debugstr_w(cls->szAppName), debugstr_w(cls->szClassName), debugstr_w(pszClassList));
813 cls->tf = tfActiveTheme;
814 cls->tf->dwRefCount++;
816 return cls;
819 /***********************************************************************
820 * MSSTYLES_CloseThemeClass
822 * Close a theme class
824 * PARAMS
825 * tc Theme class to close
827 * NOTES
828 * The MSSTYLES_CloseThemeFile decreases the refcount of the owning
829 * theme file and cleans it up, if needed.
831 HRESULT MSSTYLES_CloseThemeClass(PTHEME_CLASS tc)
833 MSSTYLES_CloseThemeFile (tc->tf);
834 return S_OK;
837 /***********************************************************************
838 * MSSTYLES_FindProperty
840 * Locate a property in a class. Part and state IDs will be used as a
841 * preference, but may be ignored in the attempt to locate the property.
842 * Will scan the entire chain of overrides for this class.
844 PTHEME_PROPERTY MSSTYLES_FindProperty(PTHEME_CLASS tc, int iPartId, int iStateId, int iPropertyPrimitive, int iPropertyId)
846 PTHEME_CLASS next = tc;
847 PTHEME_PARTSTATE ps;
848 PTHEME_PROPERTY tp;
850 TRACE("(%p, %d, %d, %d)\n", tc, iPartId, iStateId, iPropertyId);
851 /* Try and find an exact match on part & state */
852 while(next && (ps = MSSTYLES_FindPartState(next, iPartId, iStateId, &next))) {
853 if((tp = MSSTYLES_PSFindProperty(ps, iPropertyPrimitive, iPropertyId))) {
854 return tp;
857 /* If that fails, and we didn't already try it, search for just part */
858 if(iStateId != 0)
859 iStateId = 0;
860 /* As a last ditch attempt..go for just class */
861 else if(iPartId != 0)
862 iPartId = 0;
863 else
864 return NULL;
866 if((tp = MSSTYLES_FindProperty(tc, iPartId, iStateId, iPropertyPrimitive, iPropertyId)))
867 return tp;
868 return NULL;
871 HBITMAP MSSTYLES_LoadBitmap(HDC hdc, PTHEME_CLASS tc, LPCWSTR lpFilename)
873 WCHAR szFile[MAX_PATH];
874 LPWSTR tmp;
875 lstrcpynW(szFile, lpFilename, sizeof(szFile)/sizeof(szFile[0]));
876 tmp = szFile;
877 do {
878 if(*tmp == '\\') *tmp = '_';
879 if(*tmp == '/') *tmp = '_';
880 if(*tmp == '.') *tmp = '_';
881 } while(*tmp++);
882 return LoadImageW(tc->hTheme, szFile, IMAGE_BITMAP, 0, 0, LR_SHARED|LR_CREATEDIBSECTION);
885 BOOL MSSTYLES_GetNextInteger(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, int *value)
887 LPCWSTR cur = lpStringStart;
888 int total = 0;
889 BOOL gotNeg = FALSE;
891 while(cur < lpStringEnd && (*cur < '0' || *cur > '9' || *cur == '-')) cur++;
892 if(cur >= lpStringEnd) {
893 return FALSE;
895 if(*cur == '-') {
896 cur++;
897 gotNeg = TRUE;
899 while(cur < lpStringEnd && (*cur >= '0' && *cur <= '9')) {
900 total = total * 10 + (*cur - '0');
901 cur++;
903 if(gotNeg) total = -total;
904 *value = total;
905 if(lpValEnd) *lpValEnd = cur;
906 return TRUE;
909 BOOL MSSTYLES_GetNextToken(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LPWSTR lpBuff, DWORD buffSize) {
910 LPCWSTR cur = lpStringStart;
911 LPCWSTR start;
912 LPCWSTR end;
914 while(cur < lpStringEnd && (isspace(*cur) || *cur == ',')) cur++;
915 if(cur >= lpStringEnd) {
916 return FALSE;
918 start = cur;
919 while(cur < lpStringEnd && *cur != ',') cur++;
920 end = cur;
921 while(isspace(*end)) end--;
923 lstrcpynW(lpBuff, start, min(buffSize, end-start+1));
925 if(lpValEnd) *lpValEnd = cur;
926 return TRUE;
929 /***********************************************************************
930 * MSSTYLES_GetPropertyBool
932 * Retrieve a color value for a property
934 HRESULT MSSTYLES_GetPropertyBool(PTHEME_PROPERTY tp, BOOL *pfVal)
936 *pfVal = FALSE;
937 if(*tp->lpValue == 't' || *tp->lpValue == 'T')
938 *pfVal = TRUE;
939 return S_OK;
942 /***********************************************************************
943 * MSSTYLES_GetPropertyColor
945 * Retrieve a color value for a property
947 HRESULT MSSTYLES_GetPropertyColor(PTHEME_PROPERTY tp, COLORREF *pColor)
949 LPCWSTR lpEnd;
950 LPCWSTR lpCur;
951 int red, green, blue;
953 lpCur = tp->lpValue;
954 lpEnd = tp->lpValue + tp->dwValueLen;
956 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &red);
957 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &green);
958 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &blue)) {
959 TRACE("Could not parse color property\n");
960 return E_PROP_ID_UNSUPPORTED;
962 *pColor = RGB(red,green,blue);
963 return S_OK;
966 /***********************************************************************
967 * MSSTYLES_GetPropertyColor
969 * Retrieve a color value for a property
971 HRESULT MSSTYLES_GetPropertyFont(PTHEME_PROPERTY tp, HDC hdc, LOGFONTW *pFont)
973 static const WCHAR szBold[] = {'b','o','l','d','\0'};
974 static const WCHAR szItalic[] = {'i','t','a','l','i','c','\0'};
975 static const WCHAR szUnderline[] = {'u','n','d','e','r','l','i','n','e','\0'};
976 static const WCHAR szStrikeOut[] = {'s','t','r','i','k','e','o','u','t','\0'};
977 int pointSize;
978 WCHAR attr[32];
979 LPCWSTR lpCur = tp->lpValue;
980 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
982 ZeroMemory(pFont, sizeof(LOGFONTW));
984 if(!MSSTYLES_GetNextToken(lpCur, lpEnd, &lpCur, pFont->lfFaceName, LF_FACESIZE)) {
985 TRACE("Property is there, but failed to get face name\n");
986 return E_PROP_ID_UNSUPPORTED;
988 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pointSize)) {
989 TRACE("Property is there, but failed to get point size\n");
990 return E_PROP_ID_UNSUPPORTED;
992 pFont->lfHeight = -MulDiv(pointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
993 pFont->lfWeight = FW_REGULAR;
994 pFont->lfCharSet = DEFAULT_CHARSET;
995 while(MSSTYLES_GetNextToken(lpCur, lpEnd, &lpCur, attr, sizeof(attr)/sizeof(attr[0]))) {
996 if(!lstrcmpiW(szBold, attr)) pFont->lfWeight = FW_BOLD;
997 else if(!!lstrcmpiW(szItalic, attr)) pFont->lfItalic = TRUE;
998 else if(!!lstrcmpiW(szUnderline, attr)) pFont->lfUnderline = TRUE;
999 else if(!!lstrcmpiW(szStrikeOut, attr)) pFont->lfStrikeOut = TRUE;
1001 return S_OK;
1004 /***********************************************************************
1005 * MSSTYLES_GetPropertyInt
1007 * Retrieve an int value for a property
1009 HRESULT MSSTYLES_GetPropertyInt(PTHEME_PROPERTY tp, int *piVal)
1011 if(!MSSTYLES_GetNextInteger(tp->lpValue, (tp->lpValue + tp->dwValueLen), NULL, piVal)) {
1012 TRACE("Could not parse int property\n");
1013 return E_PROP_ID_UNSUPPORTED;
1015 return S_OK;
1018 /***********************************************************************
1019 * MSSTYLES_GetPropertyIntList
1021 * Retrieve an int list value for a property
1023 HRESULT MSSTYLES_GetPropertyIntList(PTHEME_PROPERTY tp, INTLIST *pIntList)
1025 int i;
1026 LPCWSTR lpCur = tp->lpValue;
1027 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1029 for(i=0; i < MAX_INTLIST_COUNT; i++) {
1030 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pIntList->iValues[i]))
1031 break;
1033 pIntList->iValueCount = i;
1034 return S_OK;
1037 /***********************************************************************
1038 * MSSTYLES_GetPropertyPosition
1040 * Retrieve a position value for a property
1042 HRESULT MSSTYLES_GetPropertyPosition(PTHEME_PROPERTY tp, POINT *pPoint)
1044 int x,y;
1045 LPCWSTR lpCur = tp->lpValue;
1046 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1048 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &x);
1049 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &y)) {
1050 TRACE("Could not parse position property\n");
1051 return E_PROP_ID_UNSUPPORTED;
1053 pPoint->x = x;
1054 pPoint->y = y;
1055 return S_OK;
1058 /***********************************************************************
1059 * MSSTYLES_GetPropertyString
1061 * Retrieve a string value for a property
1063 HRESULT MSSTYLES_GetPropertyString(PTHEME_PROPERTY tp, LPWSTR pszBuff, int cchMaxBuffChars)
1065 lstrcpynW(pszBuff, tp->lpValue, min(tp->dwValueLen+1, cchMaxBuffChars));
1066 return S_OK;
1069 /***********************************************************************
1070 * MSSTYLES_GetPropertyRect
1072 * Retrieve a rect value for a property
1074 HRESULT MSSTYLES_GetPropertyRect(PTHEME_PROPERTY tp, RECT *pRect)
1076 LPCWSTR lpCur = tp->lpValue;
1077 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1079 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->left);
1080 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->top);
1081 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->right);
1082 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, (int*)&pRect->bottom)) {
1083 TRACE("Could not parse rect property\n");
1084 return E_PROP_ID_UNSUPPORTED;
1086 return S_OK;
1089 /***********************************************************************
1090 * MSSTYLES_GetPropertyMargins
1092 * Retrieve a margins value for a property
1094 HRESULT MSSTYLES_GetPropertyMargins(PTHEME_PROPERTY tp, RECT *prc, MARGINS *pMargins)
1096 LPCWSTR lpCur = tp->lpValue;
1097 LPCWSTR lpEnd = tp->lpValue + tp->dwValueLen;
1099 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cxLeftWidth);
1100 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cxRightWidth);
1101 MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cyTopHeight);
1102 if(!MSSTYLES_GetNextInteger(lpCur, lpEnd, &lpCur, &pMargins->cyBottomHeight)) {
1103 TRACE("Could not parse margins property\n");
1104 return E_PROP_ID_UNSUPPORTED;
1106 return S_OK;