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