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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/exception.h"
36 #include "wine/debug.h"
37 #include "wine/heap.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(uxtheme
);
41 /***********************************************************************
42 * Defines and global variables
45 static BOOL
MSSTYLES_GetNextInteger(LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, int *value
);
46 static BOOL
MSSTYLES_GetNextLong(LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, LONG
*value
);
47 static BOOL
MSSTYLES_GetNextToken(LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, LPWSTR lpBuff
, DWORD buffSize
);
48 static void MSSTYLES_ParseThemeIni(PTHEME_FILE tf
, BOOL setMetrics
);
49 static HRESULT
MSSTYLES_GetFont (LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, LOGFONTW
* logfont
);
51 #define MSSTYLES_VERSION 0x0003
53 #define THEME_CLASS_SIGNATURE 0x12bc6d83
55 static PTHEME_FILE tfActiveTheme
;
57 /***********************************************************************/
59 /**********************************************************************
60 * MSSTYLES_OpenThemeFile
62 * Load and validate a theme
65 * lpThemeFile Path to theme file to load
66 * pszColorName Color name wanted, can be NULL
67 * pszSizeName Size name wanted, can be NULL
70 * If pszColorName or pszSizeName are NULL, the default color/size will be used.
71 * If one/both are provided, they are validated against valid color/sizes and if
72 * a match is not found, the function fails.
74 HRESULT
MSSTYLES_OpenThemeFile(LPCWSTR lpThemeFile
, LPCWSTR pszColorName
, LPCWSTR pszSizeName
, PTHEME_FILE
*tf
)
83 LPWSTR pszSelectedColor
= NULL
;
85 LPWSTR pszSelectedSize
= NULL
;
88 TRACE("Opening %s\n", debugstr_w(lpThemeFile
));
90 hTheme
= LoadLibraryExW(lpThemeFile
, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
92 /* Validate that this is really a theme */
94 hr
= HRESULT_FROM_WIN32(GetLastError());
97 if(!(hrsc
= FindResourceW(hTheme
, MAKEINTRESOURCEW(1), L
"PACKTHEM_VERSION"))) {
98 TRACE("No version resource found\n");
99 hr
= HRESULT_FROM_WIN32(ERROR_BAD_FORMAT
);
102 if((versize
= SizeofResource(hTheme
, hrsc
)) != 2)
104 TRACE("Version resource found, but wrong size: %ld\n", versize
);
105 hr
= HRESULT_FROM_WIN32(ERROR_BAD_FORMAT
);
108 version
= *(WORD
*)LoadResource(hTheme
, hrsc
);
109 if(version
!= MSSTYLES_VERSION
)
111 TRACE("Version of theme file is unsupported: 0x%04x\n", version
);
112 hr
= HRESULT_FROM_WIN32(ERROR_BAD_FORMAT
);
116 if(!(hrsc
= FindResourceW(hTheme
, MAKEINTRESOURCEW(1), L
"COLORNAMES"))) {
117 TRACE("Color names resource not found\n");
118 hr
= HRESULT_FROM_WIN32(ERROR_BAD_FORMAT
);
121 pszColors
= LoadResource(hTheme
, hrsc
);
123 if(!(hrsc
= FindResourceW(hTheme
, MAKEINTRESOURCEW(1), L
"SIZENAMES"))) {
124 TRACE("Size names resource not found\n");
125 hr
= HRESULT_FROM_WIN32(ERROR_BAD_FORMAT
);
128 pszSizes
= LoadResource(hTheme
, hrsc
);
130 /* Validate requested color against what's available from the theme */
134 if(!lstrcmpiW(pszColorName
, tmp
)) {
135 pszSelectedColor
= tmp
;
138 tmp
+= lstrlenW(tmp
)+1;
142 pszSelectedColor
= pszColors
; /* Use the default color */
144 /* Validate requested size against what's available from the theme */
148 if(!lstrcmpiW(pszSizeName
, tmp
)) {
149 pszSelectedSize
= tmp
;
152 tmp
+= lstrlenW(tmp
)+1;
156 pszSelectedSize
= pszSizes
; /* Use the default size */
158 if(!pszSelectedColor
|| !pszSelectedSize
) {
159 TRACE("Requested color/size (%s/%s) not found in theme\n",
160 debugstr_w(pszColorName
), debugstr_w(pszSizeName
));
161 hr
= E_PROP_ID_UNSUPPORTED
;
165 *tf
= heap_alloc_zero(sizeof(THEME_FILE
));
166 (*tf
)->hTheme
= hTheme
;
168 GetFullPathNameW(lpThemeFile
, MAX_PATH
, (*tf
)->szThemeFile
, NULL
);
170 (*tf
)->pszAvailColors
= pszColors
;
171 (*tf
)->pszAvailSizes
= pszSizes
;
172 (*tf
)->pszSelectedColor
= pszSelectedColor
;
173 (*tf
)->pszSelectedSize
= pszSelectedSize
;
179 if(hTheme
) FreeLibrary(hTheme
);
183 /***********************************************************************
184 * MSSTYLES_CloseThemeFile
186 * Close theme file and free resources
188 void MSSTYLES_CloseThemeFile(PTHEME_FILE tf
)
193 refcount
= InterlockedDecrement(&tf
->refcount
);
196 if(tf
->hTheme
) FreeLibrary(tf
->hTheme
);
199 PTHEME_CLASS pcls
= tf
->classes
;
200 tf
->classes
= pcls
->next
;
201 while(pcls
->partstate
) {
202 PTHEME_PARTSTATE ps
= pcls
->partstate
;
204 while(ps
->properties
) {
205 PTHEME_PROPERTY prop
= ps
->properties
;
206 ps
->properties
= prop
->next
;
210 pcls
->partstate
= ps
->next
;
219 PTHEME_IMAGE img
= tf
->images
;
220 tf
->images
= img
->next
;
221 DeleteObject (img
->image
);
229 /***********************************************************************
230 * MSSTYLES_SetActiveTheme
232 * Set the current active theme
234 HRESULT
MSSTYLES_SetActiveTheme(PTHEME_FILE tf
, BOOL setMetrics
)
237 MSSTYLES_CloseThemeFile(tfActiveTheme
);
241 InterlockedIncrement(&tfActiveTheme
->refcount
);
242 if(!tfActiveTheme
->classes
)
243 MSSTYLES_ParseThemeIni(tfActiveTheme
, setMetrics
);
248 /***********************************************************************
249 * MSSTYLES_GetThemeIni
251 * Retrieves themes.ini from a theme
253 PUXINI_FILE
MSSTYLES_GetThemeIni(PTHEME_FILE tf
)
255 return UXINI_LoadINI(tf
->hTheme
, L
"themes_ini");
258 /***********************************************************************
259 * MSSTYLES_GetThemeDPI
261 * Retrieves the DPI from a theme handle when it was opened
263 UINT
MSSTYLES_GetThemeDPI(PTHEME_CLASS tc
)
268 /***********************************************************************
269 * MSSTYLES_GetActiveThemeIni
271 * Retrieve the ini file for the selected color/style
273 static PUXINI_FILE
MSSTYLES_GetActiveThemeIni(PTHEME_FILE tf
)
275 DWORD dwColorCount
= 0;
276 DWORD dwSizeCount
= 0;
277 DWORD dwColorNum
= 0;
280 DWORD dwResourceIndex
;
284 /* Count the number of available colors & styles, and determine the index number
285 of the color/style we are interested in
287 tmp
= tf
->pszAvailColors
;
289 if(!lstrcmpiW(tf
->pszSelectedColor
, tmp
))
290 dwColorNum
= dwColorCount
;
291 tmp
+= lstrlenW(tmp
)+1;
294 tmp
= tf
->pszAvailSizes
;
296 if(!lstrcmpiW(tf
->pszSelectedSize
, tmp
))
297 dwSizeNum
= dwSizeCount
;
298 tmp
+= lstrlenW(tmp
)+1;
302 if(!(hrsc
= FindResourceW(tf
->hTheme
, MAKEINTRESOURCEW(1), L
"FILERESNAMES"))) {
303 TRACE("FILERESNAMES map not found\n");
306 tmp
= LoadResource(tf
->hTheme
, hrsc
);
307 dwResourceIndex
= (dwSizeCount
* dwColorNum
) + dwSizeNum
;
308 for(i
=0; i
< dwResourceIndex
; i
++) {
309 tmp
+= lstrlenW(tmp
)+1;
311 return UXINI_LoadINI(tf
->hTheme
, tmp
);
315 /***********************************************************************
316 * MSSTYLES_ParseIniSectionName
318 * Parse an ini section name into its component parts
323 * [classname.part(state)]
324 * [application::classname]
325 * [application::classname(state)]
326 * [application::classname.part]
327 * [application::classname.part(state)]
330 * lpSection Section name
331 * dwLen Length of section name
332 * szAppName Location to store application name
333 * szClassName Location to store class name
334 * iPartId Location to store part id
335 * iStateId Location to store state id
337 static BOOL
MSSTYLES_ParseIniSectionName(LPCWSTR lpSection
, DWORD dwLen
, LPWSTR szAppName
, LPWSTR szClassName
, int *iPartId
, int *iStateId
)
340 WCHAR part
[60] = {'\0'};
341 WCHAR state
[60] = {'\0'};
344 lstrcpynW(sec
, lpSection
, min(dwLen
+1, ARRAY_SIZE(sec
)));
351 /* Get the application name */
352 tmp
= wcschr(comp
, ':');
356 lstrcpynW(szAppName
, comp
, MAX_THEME_APP_NAME
);
360 tmp
= wcschr(comp
, '.');
363 lstrcpynW(szClassName
, comp
, MAX_THEME_CLASS_NAME
);
365 /* now get the part & state */
366 tmp
= wcschr(comp
, '(');
369 lstrcpynW(part
, comp
, ARRAY_SIZE(part
));
371 /* now get the state */
372 tmp
= wcschr(comp
, ')');
376 lstrcpynW(state
, comp
, ARRAY_SIZE(state
));
379 lstrcpynW(part
, comp
, ARRAY_SIZE(part
));
383 tmp
= wcschr(comp
, '(');
386 lstrcpynW(szClassName
, comp
, MAX_THEME_CLASS_NAME
);
388 /* now get the state */
389 tmp
= wcschr(comp
, ')');
393 lstrcpynW(state
, comp
, ARRAY_SIZE(state
));
396 lstrcpynW(szClassName
, comp
, MAX_THEME_CLASS_NAME
);
399 if(!*szClassName
) return FALSE
;
400 return MSSTYLES_LookupPartState(szClassName
, part
[0]?part
:NULL
, state
[0]?state
:NULL
, iPartId
, iStateId
);
403 /***********************************************************************
410 * pszAppName App name to find
411 * pszClassName Class name to find
414 * The class found, or NULL
416 static PTHEME_CLASS
MSSTYLES_FindClass(PTHEME_FILE tf
, LPCWSTR pszAppName
, LPCWSTR pszClassName
)
418 PTHEME_CLASS cur
= tf
->classes
;
421 if(!*cur
->szAppName
&& !lstrcmpiW(pszClassName
, cur
->szClassName
))
425 if(!lstrcmpiW(pszAppName
, cur
->szAppName
) && !lstrcmpiW(pszClassName
, cur
->szClassName
))
433 /***********************************************************************
436 * Add a class to a theme file
440 * pszAppName App name to add
441 * pszClassName Class name to add
444 * The class added, or a class previously added with the same name
446 static PTHEME_CLASS
MSSTYLES_AddClass(PTHEME_FILE tf
, LPCWSTR pszAppName
, LPCWSTR pszClassName
)
448 PTHEME_CLASS cur
= MSSTYLES_FindClass(tf
, pszAppName
, pszClassName
);
451 cur
= heap_alloc(sizeof(*cur
));
452 cur
->signature
= THEME_CLASS_SIGNATURE
;
454 cur
->hTheme
= tf
->hTheme
;
455 lstrcpyW(cur
->szAppName
, pszAppName
);
456 lstrcpyW(cur
->szClassName
, pszClassName
);
457 cur
->next
= tf
->classes
;
458 cur
->partstate
= NULL
;
459 cur
->overrides
= NULL
;
464 /***********************************************************************
471 * iPartId Part ID to find
474 * The part found, or NULL
476 PTHEME_PARTSTATE
MSSTYLES_FindPart(PTHEME_CLASS tc
, int iPartId
)
478 PTHEME_PARTSTATE cur
= tc
->partstate
;
482 if (cur
->iPartId
== iPartId
)
489 return MSSTYLES_FindPart(tc
->overrides
, iPartId
);
494 /***********************************************************************
495 * MSSTYLES_FindPartState
501 * iPartId Part ID to find
502 * iStateId State ID to find
503 * tcNext Receives the next class in the override chain
506 * The part/state found, or NULL
508 PTHEME_PARTSTATE
MSSTYLES_FindPartState(PTHEME_CLASS tc
, int iPartId
, int iStateId
, PTHEME_CLASS
*tcNext
)
510 PTHEME_PARTSTATE cur
= tc
->partstate
;
512 if(cur
->iPartId
== iPartId
&& cur
->iStateId
== iStateId
) {
513 if(tcNext
) *tcNext
= tc
->overrides
;
518 if(tc
->overrides
) return MSSTYLES_FindPartState(tc
->overrides
, iPartId
, iStateId
, tcNext
);
522 /***********************************************************************
523 * MSSTYLES_AddPartState
525 * Add a part/state to a class
529 * iPartId Part ID to add
530 * iStateId State ID to add
533 * The part/state added, or a part/state previously added with the same IDs
535 static PTHEME_PARTSTATE
MSSTYLES_AddPartState(PTHEME_CLASS tc
, int iPartId
, int iStateId
)
537 PTHEME_PARTSTATE cur
= MSSTYLES_FindPartState(tc
, iPartId
, iStateId
, NULL
);
540 cur
= heap_alloc(sizeof(*cur
));
541 cur
->iPartId
= iPartId
;
542 cur
->iStateId
= iStateId
;
543 cur
->properties
= NULL
;
544 cur
->next
= tc
->partstate
;
549 /***********************************************************************
550 * MSSTYLES_LFindProperty
552 * Find a property within a property list
555 * tp property list to scan
556 * iPropertyPrimitive Type of value expected
557 * iPropertyId ID of the required value
560 * The property found, or NULL
562 static PTHEME_PROPERTY
MSSTYLES_LFindProperty(PTHEME_PROPERTY tp
, int iPropertyPrimitive
, int iPropertyId
)
564 PTHEME_PROPERTY cur
= tp
;
566 if(cur
->iPropertyId
== iPropertyId
) {
567 if(cur
->iPrimitiveType
== iPropertyPrimitive
) {
571 if(!iPropertyPrimitive
)
581 /***********************************************************************
582 * MSSTYLES_PSFindProperty
584 * Find a value within a part/state
587 * ps Part/state to search
588 * iPropertyPrimitive Type of value expected
589 * iPropertyId ID of the required value
592 * The property found, or NULL
594 static inline PTHEME_PROPERTY
MSSTYLES_PSFindProperty(PTHEME_PARTSTATE ps
, int iPropertyPrimitive
, int iPropertyId
)
596 return MSSTYLES_LFindProperty(ps
->properties
, iPropertyPrimitive
, iPropertyId
);
599 /***********************************************************************
600 * MSSTYLES_FFindMetric
602 * Find a metric property for a theme file
606 * iPropertyPrimitive Type of value expected
607 * iPropertyId ID of the required value
610 * The property found, or NULL
612 static inline PTHEME_PROPERTY
MSSTYLES_FFindMetric(PTHEME_FILE tf
, int iPropertyPrimitive
, int iPropertyId
)
614 return MSSTYLES_LFindProperty(tf
->metrics
, iPropertyPrimitive
, iPropertyId
);
617 /***********************************************************************
618 * MSSTYLES_FindMetric
620 * Find a metric property for the current installed theme
624 * iPropertyPrimitive Type of value expected
625 * iPropertyId ID of the required value
628 * The property found, or NULL
630 PTHEME_PROPERTY
MSSTYLES_FindMetric(int iPropertyPrimitive
, int iPropertyId
)
632 if(!tfActiveTheme
) return NULL
;
633 return MSSTYLES_FFindMetric(tfActiveTheme
, iPropertyPrimitive
, iPropertyId
);
636 /***********************************************************************
637 * MSSTYLES_AddProperty
639 * Add a property to a part/state
643 * iPropertyPrimitive Primitive type of the property
644 * iPropertyId ID of the property
645 * lpValue Raw value (non-NULL terminated)
646 * dwValueLen Length of the value
649 * The property added, or a property previously added with the same IDs
651 static PTHEME_PROPERTY
MSSTYLES_AddProperty(PTHEME_PARTSTATE ps
, int iPropertyPrimitive
, int iPropertyId
, LPCWSTR lpValue
, DWORD dwValueLen
, BOOL isGlobal
)
653 PTHEME_PROPERTY cur
= MSSTYLES_PSFindProperty(ps
, iPropertyPrimitive
, iPropertyId
);
654 /* Should duplicate properties overwrite the original, or be ignored? */
657 cur
= heap_alloc(sizeof(*cur
));
658 cur
->iPrimitiveType
= iPropertyPrimitive
;
659 cur
->iPropertyId
= iPropertyId
;
660 cur
->lpValue
= lpValue
;
661 cur
->dwValueLen
= dwValueLen
;
664 cur
->origin
= PO_STATE
;
666 cur
->origin
= PO_PART
;
668 cur
->origin
= PO_GLOBAL
;
670 cur
->origin
= PO_CLASS
;
672 cur
->next
= ps
->properties
;
673 ps
->properties
= cur
;
677 /***********************************************************************
680 * Add a property to a part/state
684 * iPropertyPrimitive Primitive type of the property
685 * iPropertyId ID of the property
686 * lpValue Raw value (non-NULL terminated)
687 * dwValueLen Length of the value
690 * The property added, or a property previously added with the same IDs
692 static PTHEME_PROPERTY
MSSTYLES_AddMetric(PTHEME_FILE tf
, int iPropertyPrimitive
, int iPropertyId
, LPCWSTR lpValue
, DWORD dwValueLen
)
694 PTHEME_PROPERTY cur
= MSSTYLES_FFindMetric(tf
, iPropertyPrimitive
, iPropertyId
);
695 /* Should duplicate properties overwrite the original, or be ignored? */
698 cur
= heap_alloc(sizeof(*cur
));
699 cur
->iPrimitiveType
= iPropertyPrimitive
;
700 cur
->iPropertyId
= iPropertyId
;
701 cur
->lpValue
= lpValue
;
702 cur
->dwValueLen
= dwValueLen
;
704 cur
->origin
= PO_GLOBAL
;
706 cur
->next
= tf
->metrics
;
711 /* Color-related state for theme ini parsing */
712 struct PARSECOLORSTATE
715 int colorElements
[TMT_LASTCOLOR
-TMT_FIRSTCOLOR
+1];
716 COLORREF colorRgb
[TMT_LASTCOLOR
-TMT_FIRSTCOLOR
+1];
720 static inline void parse_init_color (struct PARSECOLORSTATE
* state
)
722 memset (state
, 0, sizeof (*state
));
725 static BOOL
parse_handle_color_property (struct PARSECOLORSTATE
* state
,
726 int iPropertyId
, LPCWSTR lpValue
,
730 LPCWSTR lpValueEnd
= lpValue
+ dwValueLen
;
731 if(MSSTYLES_GetNextInteger(lpValue
, lpValueEnd
, &lpValue
, &r
) &&
732 MSSTYLES_GetNextInteger(lpValue
, lpValueEnd
, &lpValue
, &g
) &&
733 MSSTYLES_GetNextInteger(lpValue
, lpValueEnd
, &lpValue
, &b
)) {
734 state
->colorElements
[state
->colorCount
] = iPropertyId
- TMT_FIRSTCOLOR
;
735 state
->colorRgb
[state
->colorCount
++] = RGB(r
,g
,b
);
738 case TMT_ACTIVECAPTION
:
739 state
->captionColors
|= 0x1;
741 case TMT_INACTIVECAPTION
:
742 state
->captionColors
|= 0x2;
744 case TMT_GRADIENTACTIVECAPTION
:
745 state
->captionColors
|= 0x4;
747 case TMT_GRADIENTINACTIVECAPTION
:
748 state
->captionColors
|= 0x8;
758 static void parse_apply_color (struct PARSECOLORSTATE
* state
)
760 if (state
->colorCount
> 0)
761 SetSysColors(state
->colorCount
, state
->colorElements
, state
->colorRgb
);
762 if (state
->captionColors
== 0xf)
763 SystemParametersInfoW (SPI_SETGRADIENTCAPTIONS
, 0, (PVOID
)TRUE
, 0);
766 /* Non-client-metrics-related state for theme ini parsing */
767 struct PARSENONCLIENTSTATE
769 NONCLIENTMETRICSW metrics
;
771 LOGFONTW iconTitleFont
;
774 static inline void parse_init_nonclient (struct PARSENONCLIENTSTATE
* state
)
776 DPI_AWARENESS_CONTEXT old_context
;
778 old_context
= SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE
);
780 memset (state
, 0, sizeof (*state
));
781 state
->metrics
.cbSize
= sizeof (NONCLIENTMETRICSW
);
782 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS
, sizeof (NONCLIENTMETRICSW
),
784 SystemParametersInfoW (SPI_GETICONTITLELOGFONT
, sizeof (LOGFONTW
),
785 &state
->iconTitleFont
, 0);
787 SetThreadDpiAwarenessContext(old_context
);
790 static BOOL
parse_handle_nonclient_font (struct PARSENONCLIENTSTATE
* state
,
791 int iPropertyId
, LPCWSTR lpValue
,
796 memset (&font
, 0, sizeof (font
));
797 if (SUCCEEDED (MSSTYLES_GetFont (lpValue
, lpValue
+ dwValueLen
, &lpValue
,
802 case TMT_CAPTIONFONT
:
803 state
->metrics
.lfCaptionFont
= font
;
804 state
->metricsDirty
= TRUE
;
806 case TMT_SMALLCAPTIONFONT
:
807 state
->metrics
.lfSmCaptionFont
= font
;
808 state
->metricsDirty
= TRUE
;
811 state
->metrics
.lfMenuFont
= font
;
812 state
->metricsDirty
= TRUE
;
815 state
->metrics
.lfStatusFont
= font
;
816 state
->metricsDirty
= TRUE
;
819 state
->metrics
.lfMessageFont
= font
;
820 state
->metricsDirty
= TRUE
;
822 case TMT_ICONTITLEFONT
:
823 state
->iconTitleFont
= font
;
824 state
->metricsDirty
= TRUE
;
833 static BOOL
parse_handle_nonclient_size (struct PARSENONCLIENTSTATE
* state
,
834 int iPropertyId
, LPCWSTR lpValue
,
838 LPCWSTR lpValueEnd
= lpValue
+ dwValueLen
;
839 if(MSSTYLES_GetNextInteger(lpValue
, lpValueEnd
, &lpValue
, &size
)) {
842 case TMT_SIZINGBORDERWIDTH
:
843 state
->metrics
.iBorderWidth
= size
;
844 state
->metricsDirty
= TRUE
;
846 case TMT_SCROLLBARWIDTH
:
847 state
->metrics
.iScrollWidth
= size
;
848 state
->metricsDirty
= TRUE
;
850 case TMT_SCROLLBARHEIGHT
:
851 state
->metrics
.iScrollHeight
= size
;
852 state
->metricsDirty
= TRUE
;
854 case TMT_CAPTIONBARWIDTH
:
855 state
->metrics
.iCaptionWidth
= size
;
856 state
->metricsDirty
= TRUE
;
858 case TMT_CAPTIONBARHEIGHT
:
859 state
->metrics
.iCaptionHeight
= size
;
860 state
->metricsDirty
= TRUE
;
862 case TMT_SMCAPTIONBARWIDTH
:
863 state
->metrics
.iSmCaptionWidth
= size
;
864 state
->metricsDirty
= TRUE
;
866 case TMT_SMCAPTIONBARHEIGHT
:
867 state
->metrics
.iSmCaptionHeight
= size
;
868 state
->metricsDirty
= TRUE
;
870 case TMT_MENUBARWIDTH
:
871 state
->metrics
.iMenuWidth
= size
;
872 state
->metricsDirty
= TRUE
;
874 case TMT_MENUBARHEIGHT
:
875 state
->metrics
.iMenuHeight
= size
;
876 state
->metricsDirty
= TRUE
;
885 static void parse_apply_nonclient (struct PARSENONCLIENTSTATE
* state
)
887 DPI_AWARENESS_CONTEXT old_context
;
889 if (state
->metricsDirty
)
891 old_context
= SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE
);
892 SystemParametersInfoW (SPI_SETNONCLIENTMETRICS
, sizeof (state
->metrics
),
894 SystemParametersInfoW (SPI_SETICONTITLELOGFONT
, sizeof (state
->iconTitleFont
),
895 &state
->iconTitleFont
, 0);
896 SetThreadDpiAwarenessContext(old_context
);
900 /***********************************************************************
901 * MSSTYLES_ParseThemeIni
903 * Parse the theme ini for the selected color/style
908 static void MSSTYLES_ParseThemeIni(PTHEME_FILE tf
, BOOL setMetrics
)
911 PTHEME_CLASS globals
;
914 WCHAR szAppName
[MAX_THEME_APP_NAME
];
915 WCHAR szClassName
[MAX_THEME_CLASS_NAME
];
916 WCHAR szPropertyName
[MAX_THEME_VALUE_NAME
];
919 int iPropertyPrimitive
;
926 ini
= MSSTYLES_GetActiveThemeIni(tf
);
928 while((lpName
=UXINI_GetNextSection(ini
, &dwLen
))) {
929 if(CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
, lpName
, dwLen
, L
"SysMetrics", -1) == CSTR_EQUAL
) {
930 struct PARSECOLORSTATE colorState
;
931 struct PARSENONCLIENTSTATE nonClientState
;
933 parse_init_color (&colorState
);
934 parse_init_nonclient (&nonClientState
);
936 while((lpName
=UXINI_GetNextValue(ini
, &dwLen
, &lpValue
, &dwValueLen
))) {
937 lstrcpynW(szPropertyName
, lpName
, min(dwLen
+1, ARRAY_SIZE(szPropertyName
)));
938 if(MSSTYLES_LookupProperty(szPropertyName
, &iPropertyPrimitive
, &iPropertyId
)) {
939 if(iPropertyId
>= TMT_FIRSTCOLOR
&& iPropertyId
<= TMT_LASTCOLOR
) {
940 if (!parse_handle_color_property (&colorState
, iPropertyId
,
941 lpValue
, dwValueLen
))
942 FIXME("Invalid color value for %s\n",
943 debugstr_w(szPropertyName
));
945 else if (setMetrics
&& (iPropertyId
== TMT_FLATMENUS
)) {
946 BOOL flatMenus
= (*lpValue
== 'T') || (*lpValue
== 't');
947 SystemParametersInfoW (SPI_SETFLATMENU
, 0, (PVOID
)(INT_PTR
)flatMenus
, 0);
949 else if ((iPropertyId
>= TMT_FIRSTFONT
)
950 && (iPropertyId
<= TMT_LASTFONT
))
952 if (!parse_handle_nonclient_font (&nonClientState
,
953 iPropertyId
, lpValue
, dwValueLen
))
954 FIXME("Invalid font value for %s\n",
955 debugstr_w(szPropertyName
));
957 else if ((iPropertyId
>= TMT_FIRSTSIZE
)
958 && (iPropertyId
<= TMT_LASTSIZE
))
960 if (!parse_handle_nonclient_size (&nonClientState
,
961 iPropertyId
, lpValue
, dwValueLen
))
962 FIXME("Invalid size value for %s\n",
963 debugstr_w(szPropertyName
));
965 /* Catch all metrics, including colors */
966 MSSTYLES_AddMetric(tf
, iPropertyPrimitive
, iPropertyId
, lpValue
, dwValueLen
);
969 TRACE("Unknown system metric %s\n", debugstr_w(szPropertyName
));
974 parse_apply_color (&colorState
);
975 parse_apply_nonclient (&nonClientState
);
979 if(MSSTYLES_ParseIniSectionName(lpName
, dwLen
, szAppName
, szClassName
, &iPartId
, &iStateId
)) {
980 BOOL isGlobal
= FALSE
;
981 if(!lstrcmpiW(szClassName
, L
"globals")) {
984 cls
= MSSTYLES_AddClass(tf
, szAppName
, szClassName
);
985 ps
= MSSTYLES_AddPartState(cls
, iPartId
, iStateId
);
987 while((lpName
=UXINI_GetNextValue(ini
, &dwLen
, &lpValue
, &dwValueLen
))) {
988 lstrcpynW(szPropertyName
, lpName
, min(dwLen
+1, ARRAY_SIZE(szPropertyName
)));
989 if(MSSTYLES_LookupProperty(szPropertyName
, &iPropertyPrimitive
, &iPropertyId
)) {
990 MSSTYLES_AddProperty(ps
, iPropertyPrimitive
, iPropertyId
, lpValue
, dwValueLen
, isGlobal
);
993 TRACE("Unknown property %s\n", debugstr_w(szPropertyName
));
999 /* App/Class combos override values defined by the base class, map these overrides */
1000 globals
= MSSTYLES_FindClass(tf
, NULL
, L
"globals");
1003 if(*cls
->szAppName
) {
1004 cls
->overrides
= MSSTYLES_FindClass(tf
, NULL
, cls
->szClassName
);
1005 if(!cls
->overrides
) {
1006 TRACE("No overrides found for app %s class %s\n", debugstr_w(cls
->szAppName
), debugstr_w(cls
->szClassName
));
1009 cls
->overrides
= globals
;
1013 /* Everything overrides globals..except globals */
1014 if(cls
!= globals
) cls
->overrides
= globals
;
1018 UXINI_CloseINI(ini
);
1021 ERR("Failed to parse theme ini\n");
1025 static void parse_app_class_name(LPCWSTR name
, LPWSTR app_name
, LPWSTR class_name
)
1029 app_name
[0] = class_name
[0] = 0;
1031 p
= wcsstr(name
, L
"::");
1034 lstrcpynW(app_name
, name
, min(p
- name
+ 1, MAX_THEME_APP_NAME
));
1036 lstrcpynW(class_name
, p
, min(wcslen(p
) + 1, MAX_THEME_CLASS_NAME
));
1039 lstrcpynW(class_name
, name
, MAX_THEME_CLASS_NAME
);
1042 /***********************************************************************
1043 * MSSTYLES_OpenThemeClass
1045 * Open a theme class, uses the current active theme
1048 * pszAppName Application name, for theme styles specific
1049 * to a particular application
1050 * pszClassList List of requested classes, semicolon delimited
1051 * dpi DPI for theme parts
1053 PTHEME_CLASS
MSSTYLES_OpenThemeClass(LPCWSTR pszAppName
, LPCWSTR pszClassList
, UINT dpi
)
1055 PTHEME_CLASS cls
= NULL
;
1056 WCHAR buf
[MAX_THEME_APP_NAME
+ MAX_THEME_CLASS_NAME
];
1057 WCHAR szAppName
[MAX_THEME_APP_NAME
];
1058 WCHAR szClassName
[MAX_THEME_CLASS_NAME
];
1063 if(!tfActiveTheme
) {
1064 TRACE("there is no active theme\n");
1067 if(!tfActiveTheme
->classes
) {
1071 start
= pszClassList
;
1072 while((end
= wcschr(start
, ';'))) {
1074 lstrcpynW(buf
, start
, min(len
+1, ARRAY_SIZE(buf
)));
1077 parse_app_class_name(buf
, szAppName
, szClassName
);
1079 /* If the window application name is set then fail */
1080 if (szAppName
[0] && pszAppName
)
1083 cls
= MSSTYLES_FindClass(tfActiveTheme
, szAppName
[0] ? szAppName
: pszAppName
, szClassName
);
1084 /* Fall back to default class if the specified subclass is not found */
1085 if (!cls
) cls
= MSSTYLES_FindClass(tfActiveTheme
, NULL
, szClassName
);
1089 if(!cls
&& *start
) {
1090 parse_app_class_name(start
, szAppName
, szClassName
);
1092 /* If the window application name is set then fail */
1093 if (szAppName
[0] && pszAppName
)
1096 cls
= MSSTYLES_FindClass(tfActiveTheme
, szAppName
[0] ? szAppName
: pszAppName
, szClassName
);
1097 /* Fall back to default class if the specified subclass is not found */
1098 if (!cls
) cls
= MSSTYLES_FindClass(tfActiveTheme
, NULL
, szClassName
);
1101 TRACE("Opened app %s, class %s from list %s\n", debugstr_w(cls
->szAppName
), debugstr_w(cls
->szClassName
), debugstr_w(pszClassList
));
1102 cls
->tf
= tfActiveTheme
;
1103 InterlockedIncrement(&cls
->tf
->refcount
);
1104 InterlockedIncrement(&cls
->refcount
);
1110 /***********************************************************************
1111 * MSSTYLES_CloseThemeClass
1113 * Close a theme class
1116 * tc Theme class to close
1119 * The MSSTYLES_CloseThemeFile decreases the refcount of the owning
1120 * theme file and cleans it up, if needed.
1122 HRESULT
MSSTYLES_CloseThemeClass(PTHEME_CLASS tc
)
1128 if (tc
->signature
!= THEME_CLASS_SIGNATURE
)
1139 WARN("Invalid theme class handle\n");
1143 refcount
= InterlockedDecrement(&tc
->refcount
);
1144 /* Some buggy apps may double free HTHEME handles */
1146 MSSTYLES_CloseThemeFile(tc
->tf
);
1150 /***********************************************************************
1151 * MSSTYLES_FindProperty
1153 * Locate a property in a class. Part and state IDs will be used as a
1154 * preference, but may be ignored in the attempt to locate the property.
1155 * Will scan the entire chain of overrides for this class.
1157 PTHEME_PROPERTY
MSSTYLES_FindProperty(PTHEME_CLASS tc
, int iPartId
, int iStateId
, int iPropertyPrimitive
, int iPropertyId
)
1159 PTHEME_CLASS next
= tc
;
1160 PTHEME_PARTSTATE ps
;
1163 TRACE("(%p, %d, %d, %d)\n", tc
, iPartId
, iStateId
, iPropertyId
);
1164 /* Try and find an exact match on part & state */
1165 while(next
&& (ps
= MSSTYLES_FindPartState(next
, iPartId
, iStateId
, &next
))) {
1166 if((tp
= MSSTYLES_PSFindProperty(ps
, iPropertyPrimitive
, iPropertyId
))) {
1170 /* If that fails, and we didn't already try it, search for just part */
1173 /* As a last ditch attempt..go for just class */
1174 else if(iPartId
!= 0)
1179 if((tp
= MSSTYLES_FindProperty(tc
, iPartId
, iStateId
, iPropertyPrimitive
, iPropertyId
)))
1184 /* Prepare a bitmap to be used for alpha blending */
1185 static BOOL
prepare_alpha (HBITMAP bmp
, BOOL
* hasAlpha
)
1193 if (!bmp
|| GetObjectW( bmp
, sizeof(dib
), &dib
) != sizeof(dib
))
1196 if (dib
.dsBm
.bmBitsPixel
!= 32 || dib
.dsBmih
.biCompression
!= BI_RGB
)
1200 /* If all alpha values are 0xff, don't use alpha blending */
1201 for (n
= 0, p
= dib
.dsBm
.bmBits
; n
< dib
.dsBmih
.biWidth
* dib
.dsBmih
.biHeight
; n
++, p
+= 4)
1202 if ((*hasAlpha
= (p
[3] != 0xff)))
1208 p
= dib
.dsBm
.bmBits
;
1209 n
= dib
.dsBmih
.biHeight
* dib
.dsBmih
.biWidth
;
1210 /* AlphaBlend() wants premultiplied alpha, so do that now */
1214 p
[0] = (p
[0] * a
) >> 8;
1215 p
[1] = (p
[1] * a
) >> 8;
1216 p
[2] = (p
[2] * a
) >> 8;
1223 HBITMAP
MSSTYLES_LoadBitmap (PTHEME_CLASS tc
, LPCWSTR lpFilename
, BOOL
* hasAlpha
)
1225 WCHAR szFile
[MAX_PATH
];
1228 lstrcpynW(szFile
, lpFilename
, ARRAY_SIZE(szFile
));
1231 if(*tmp
== '\\') *tmp
= '_';
1232 if(*tmp
== '/') *tmp
= '_';
1233 if(*tmp
== '.') *tmp
= '_';
1236 /* Try to locate in list of loaded images */
1237 img
= tc
->tf
->images
;
1240 if (lstrcmpiW (szFile
, img
->name
) == 0)
1242 TRACE ("found %p %s: %p\n", img
, debugstr_w (img
->name
), img
->image
);
1243 *hasAlpha
= img
->hasAlpha
;
1248 /* Not found? Load from resources */
1249 img
= heap_alloc(sizeof(*img
));
1250 img
->image
= LoadImageW(tc
->hTheme
, szFile
, IMAGE_BITMAP
, 0, 0, LR_CREATEDIBSECTION
);
1251 prepare_alpha (img
->image
, hasAlpha
);
1252 img
->hasAlpha
= *hasAlpha
;
1253 /* ...and stow away for later reuse. */
1254 lstrcpyW (img
->name
, szFile
);
1255 img
->next
= tc
->tf
->images
;
1256 tc
->tf
->images
= img
;
1257 TRACE ("new %p %s: %p\n", img
, debugstr_w (img
->name
), img
->image
);
1261 static BOOL
MSSTYLES_GetNextLong(LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, LONG
*value
)
1263 LPCWSTR cur
= lpStringStart
;
1265 BOOL gotNeg
= FALSE
;
1267 while(cur
< lpStringEnd
&& (*cur
< '0' || *cur
> '9' || *cur
== '-')) cur
++;
1268 if(cur
>= lpStringEnd
) {
1275 while(cur
< lpStringEnd
&& (*cur
>= '0' && *cur
<= '9')) {
1276 total
= total
* 10 + (*cur
- '0');
1279 if(gotNeg
) total
= -total
;
1281 if(lpValEnd
) *lpValEnd
= cur
;
1285 static BOOL
MSSTYLES_GetNextInteger(LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, int *value
)
1287 return MSSTYLES_GetNextLong(lpStringStart
, lpStringEnd
, lpValEnd
, (LONG
*)value
);
1290 static inline BOOL
isSpace(WCHAR c
)
1292 return c
== ' ' || c
== '\f' || c
== '\n' || c
== '\r' || c
== '\t' || c
== '\v';
1295 static BOOL
MSSTYLES_GetNextToken(LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, LPWSTR lpBuff
, DWORD buffSize
) {
1296 LPCWSTR cur
= lpStringStart
;
1300 while(cur
< lpStringEnd
&& (isSpace(*cur
) || *cur
== ',')) cur
++;
1301 if(cur
>= lpStringEnd
) {
1305 while(cur
< lpStringEnd
&& *cur
!= ',') cur
++;
1307 while(isSpace(*end
)) end
--;
1309 lstrcpynW(lpBuff
, start
, min(buffSize
, end
-start
+1));
1311 if(lpValEnd
) *lpValEnd
= cur
;
1315 /***********************************************************************
1316 * MSSTYLES_GetPropertyBool
1318 * Retrieve a color value for a property
1320 HRESULT
MSSTYLES_GetPropertyBool(PTHEME_PROPERTY tp
, BOOL
*pfVal
)
1323 if(*tp
->lpValue
== 't' || *tp
->lpValue
== 'T')
1328 /***********************************************************************
1329 * MSSTYLES_GetPropertyColor
1331 * Retrieve a color value for a property
1333 HRESULT
MSSTYLES_GetPropertyColor(PTHEME_PROPERTY tp
, COLORREF
*pColor
)
1337 int red
, green
, blue
;
1339 lpCur
= tp
->lpValue
;
1340 lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1342 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &red
)) {
1343 TRACE("Could not parse color property\n");
1344 return E_PROP_ID_UNSUPPORTED
;
1346 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &green
)) {
1347 TRACE("Could not parse color property\n");
1348 return E_PROP_ID_UNSUPPORTED
;
1350 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &blue
)) {
1351 TRACE("Could not parse color property\n");
1352 return E_PROP_ID_UNSUPPORTED
;
1354 *pColor
= RGB(red
,green
,blue
);
1358 /***********************************************************************
1359 * MSSTYLES_GetPropertyColor
1361 * Retrieve a color value for a property
1363 static HRESULT
MSSTYLES_GetFont (LPCWSTR lpCur
, LPCWSTR lpEnd
,
1364 LPCWSTR
*lpValEnd
, LOGFONTW
* pFont
)
1369 if(!MSSTYLES_GetNextToken(lpCur
, lpEnd
, &lpCur
, pFont
->lfFaceName
, LF_FACESIZE
)) {
1370 TRACE("Property is there, but failed to get face name\n");
1372 return E_PROP_ID_UNSUPPORTED
;
1374 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pointSize
)) {
1375 TRACE("Property is there, but failed to get point size\n");
1377 return E_PROP_ID_UNSUPPORTED
;
1379 pFont
->lfHeight
= pointSize
;
1380 pFont
->lfWeight
= FW_REGULAR
;
1381 pFont
->lfCharSet
= DEFAULT_CHARSET
;
1382 while(MSSTYLES_GetNextToken(lpCur
, lpEnd
, &lpCur
, attr
, ARRAY_SIZE(attr
))) {
1383 if(!lstrcmpiW(L
"bold", attr
)) pFont
->lfWeight
= FW_BOLD
;
1384 else if(!lstrcmpiW(L
"italic", attr
)) pFont
->lfItalic
= TRUE
;
1385 else if(!lstrcmpiW(L
"underline", attr
)) pFont
->lfUnderline
= TRUE
;
1386 else if(!lstrcmpiW(L
"strikeout", attr
)) pFont
->lfStrikeOut
= TRUE
;
1392 HRESULT
MSSTYLES_GetPropertyFont(PTHEME_PROPERTY tp
, HDC hdc
, LOGFONTW
*pFont
)
1394 LPCWSTR lpCur
= tp
->lpValue
;
1395 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1398 ZeroMemory(pFont
, sizeof(LOGFONTW
));
1399 hr
= MSSTYLES_GetFont (lpCur
, lpEnd
, &lpCur
, pFont
);
1401 pFont
->lfHeight
= -MulDiv(pFont
->lfHeight
, GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1406 /***********************************************************************
1407 * MSSTYLES_GetPropertyInt
1409 * Retrieve an int value for a property
1411 HRESULT
MSSTYLES_GetPropertyInt(PTHEME_PROPERTY tp
, int *piVal
)
1413 if(!MSSTYLES_GetNextInteger(tp
->lpValue
, (tp
->lpValue
+ tp
->dwValueLen
), NULL
, piVal
)) {
1414 TRACE("Could not parse int property\n");
1415 return E_PROP_ID_UNSUPPORTED
;
1420 /***********************************************************************
1421 * MSSTYLES_GetPropertyIntList
1423 * Retrieve an int list value for a property
1425 HRESULT
MSSTYLES_GetPropertyIntList(PTHEME_PROPERTY tp
, INTLIST
*pIntList
)
1428 LPCWSTR lpCur
= tp
->lpValue
;
1429 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1431 for(i
=0; i
< MAX_INTLIST_COUNT
; i
++) {
1432 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pIntList
->iValues
[i
]))
1435 pIntList
->iValueCount
= i
;
1439 /***********************************************************************
1440 * MSSTYLES_GetPropertyPosition
1442 * Retrieve a position value for a property
1444 HRESULT
MSSTYLES_GetPropertyPosition(PTHEME_PROPERTY tp
, POINT
*pPoint
)
1447 LPCWSTR lpCur
= tp
->lpValue
;
1448 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1450 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &x
)) {
1451 TRACE("Could not parse position property\n");
1452 return E_PROP_ID_UNSUPPORTED
;
1454 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &y
)) {
1455 TRACE("Could not parse position property\n");
1456 return E_PROP_ID_UNSUPPORTED
;
1463 /***********************************************************************
1464 * MSSTYLES_GetPropertyString
1466 * Retrieve a string value for a property
1468 HRESULT
MSSTYLES_GetPropertyString(PTHEME_PROPERTY tp
, LPWSTR pszBuff
, int cchMaxBuffChars
)
1470 lstrcpynW(pszBuff
, tp
->lpValue
, min(tp
->dwValueLen
+1, cchMaxBuffChars
));
1474 /***********************************************************************
1475 * MSSTYLES_GetPropertyRect
1477 * Retrieve a rect value for a property
1479 HRESULT
MSSTYLES_GetPropertyRect(PTHEME_PROPERTY tp
, RECT
*pRect
)
1481 LPCWSTR lpCur
= tp
->lpValue
;
1482 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1484 MSSTYLES_GetNextLong(lpCur
, lpEnd
, &lpCur
, &pRect
->left
);
1485 MSSTYLES_GetNextLong(lpCur
, lpEnd
, &lpCur
, &pRect
->top
);
1486 MSSTYLES_GetNextLong(lpCur
, lpEnd
, &lpCur
, &pRect
->right
);
1487 if(!MSSTYLES_GetNextLong(lpCur
, lpEnd
, &lpCur
, &pRect
->bottom
)) {
1488 TRACE("Could not parse rect property\n");
1489 return E_PROP_ID_UNSUPPORTED
;
1494 /***********************************************************************
1495 * MSSTYLES_GetPropertyMargins
1497 * Retrieve a margins value for a property
1499 HRESULT
MSSTYLES_GetPropertyMargins(PTHEME_PROPERTY tp
, RECT
*prc
, MARGINS
*pMargins
)
1501 LPCWSTR lpCur
= tp
->lpValue
;
1502 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1504 MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pMargins
->cxLeftWidth
);
1505 MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pMargins
->cxRightWidth
);
1506 MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pMargins
->cyTopHeight
);
1507 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pMargins
->cyBottomHeight
)) {
1508 TRACE("Could not parse margins property\n");
1509 return E_PROP_ID_UNSUPPORTED
;