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 /***********************************************************************
1026 * MSSTYLES_OpenThemeClass
1028 * Open a theme class, uses the current active theme
1031 * pszAppName Application name, for theme styles specific
1032 * to a particular application
1033 * pszClassList List of requested classes, semicolon delimited
1034 * dpi DPI for theme parts
1036 PTHEME_CLASS
MSSTYLES_OpenThemeClass(LPCWSTR pszAppName
, LPCWSTR pszClassList
, UINT dpi
)
1038 PTHEME_CLASS cls
= NULL
;
1039 WCHAR szClassName
[MAX_THEME_CLASS_NAME
];
1044 if(!tfActiveTheme
) {
1045 TRACE("there is no active theme\n");
1048 if(!tfActiveTheme
->classes
) {
1052 start
= pszClassList
;
1053 while((end
= wcschr(start
, ';'))) {
1055 lstrcpynW(szClassName
, start
, min(len
+1, ARRAY_SIZE(szClassName
)));
1057 cls
= MSSTYLES_FindClass(tfActiveTheme
, pszAppName
, szClassName
);
1060 if(!cls
&& *start
) {
1061 lstrcpynW(szClassName
, start
, ARRAY_SIZE(szClassName
));
1062 cls
= MSSTYLES_FindClass(tfActiveTheme
, pszAppName
, szClassName
);
1065 TRACE("Opened app %s, class %s from list %s\n", debugstr_w(cls
->szAppName
), debugstr_w(cls
->szClassName
), debugstr_w(pszClassList
));
1066 cls
->tf
= tfActiveTheme
;
1067 InterlockedIncrement(&cls
->tf
->refcount
);
1068 InterlockedIncrement(&cls
->refcount
);
1074 /***********************************************************************
1075 * MSSTYLES_CloseThemeClass
1077 * Close a theme class
1080 * tc Theme class to close
1083 * The MSSTYLES_CloseThemeFile decreases the refcount of the owning
1084 * theme file and cleans it up, if needed.
1086 HRESULT
MSSTYLES_CloseThemeClass(PTHEME_CLASS tc
)
1092 if (tc
->signature
!= THEME_CLASS_SIGNATURE
)
1103 WARN("Invalid theme class handle\n");
1107 refcount
= InterlockedDecrement(&tc
->refcount
);
1108 /* Some buggy apps may double free HTHEME handles */
1110 MSSTYLES_CloseThemeFile(tc
->tf
);
1114 /***********************************************************************
1115 * MSSTYLES_FindProperty
1117 * Locate a property in a class. Part and state IDs will be used as a
1118 * preference, but may be ignored in the attempt to locate the property.
1119 * Will scan the entire chain of overrides for this class.
1121 PTHEME_PROPERTY
MSSTYLES_FindProperty(PTHEME_CLASS tc
, int iPartId
, int iStateId
, int iPropertyPrimitive
, int iPropertyId
)
1123 PTHEME_CLASS next
= tc
;
1124 PTHEME_PARTSTATE ps
;
1127 TRACE("(%p, %d, %d, %d)\n", tc
, iPartId
, iStateId
, iPropertyId
);
1128 /* Try and find an exact match on part & state */
1129 while(next
&& (ps
= MSSTYLES_FindPartState(next
, iPartId
, iStateId
, &next
))) {
1130 if((tp
= MSSTYLES_PSFindProperty(ps
, iPropertyPrimitive
, iPropertyId
))) {
1134 /* If that fails, and we didn't already try it, search for just part */
1137 /* As a last ditch attempt..go for just class */
1138 else if(iPartId
!= 0)
1143 if((tp
= MSSTYLES_FindProperty(tc
, iPartId
, iStateId
, iPropertyPrimitive
, iPropertyId
)))
1148 /* Prepare a bitmap to be used for alpha blending */
1149 static BOOL
prepare_alpha (HBITMAP bmp
, BOOL
* hasAlpha
)
1157 if (!bmp
|| GetObjectW( bmp
, sizeof(dib
), &dib
) != sizeof(dib
))
1160 if (dib
.dsBm
.bmBitsPixel
!= 32 || dib
.dsBmih
.biCompression
!= BI_RGB
)
1164 /* If all alpha values are 0xff, don't use alpha blending */
1165 for (n
= 0, p
= dib
.dsBm
.bmBits
; n
< dib
.dsBmih
.biWidth
* dib
.dsBmih
.biHeight
; n
++, p
+= 4)
1166 if ((*hasAlpha
= (p
[3] != 0xff)))
1172 p
= dib
.dsBm
.bmBits
;
1173 n
= dib
.dsBmih
.biHeight
* dib
.dsBmih
.biWidth
;
1174 /* AlphaBlend() wants premultiplied alpha, so do that now */
1178 p
[0] = (p
[0] * a
) >> 8;
1179 p
[1] = (p
[1] * a
) >> 8;
1180 p
[2] = (p
[2] * a
) >> 8;
1187 HBITMAP
MSSTYLES_LoadBitmap (PTHEME_CLASS tc
, LPCWSTR lpFilename
, BOOL
* hasAlpha
)
1189 WCHAR szFile
[MAX_PATH
];
1192 lstrcpynW(szFile
, lpFilename
, ARRAY_SIZE(szFile
));
1195 if(*tmp
== '\\') *tmp
= '_';
1196 if(*tmp
== '/') *tmp
= '_';
1197 if(*tmp
== '.') *tmp
= '_';
1200 /* Try to locate in list of loaded images */
1201 img
= tc
->tf
->images
;
1204 if (lstrcmpiW (szFile
, img
->name
) == 0)
1206 TRACE ("found %p %s: %p\n", img
, debugstr_w (img
->name
), img
->image
);
1207 *hasAlpha
= img
->hasAlpha
;
1212 /* Not found? Load from resources */
1213 img
= heap_alloc(sizeof(*img
));
1214 img
->image
= LoadImageW(tc
->hTheme
, szFile
, IMAGE_BITMAP
, 0, 0, LR_CREATEDIBSECTION
);
1215 prepare_alpha (img
->image
, hasAlpha
);
1216 img
->hasAlpha
= *hasAlpha
;
1217 /* ...and stow away for later reuse. */
1218 lstrcpyW (img
->name
, szFile
);
1219 img
->next
= tc
->tf
->images
;
1220 tc
->tf
->images
= img
;
1221 TRACE ("new %p %s: %p\n", img
, debugstr_w (img
->name
), img
->image
);
1225 static BOOL
MSSTYLES_GetNextLong(LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, LONG
*value
)
1227 LPCWSTR cur
= lpStringStart
;
1229 BOOL gotNeg
= FALSE
;
1231 while(cur
< lpStringEnd
&& (*cur
< '0' || *cur
> '9' || *cur
== '-')) cur
++;
1232 if(cur
>= lpStringEnd
) {
1239 while(cur
< lpStringEnd
&& (*cur
>= '0' && *cur
<= '9')) {
1240 total
= total
* 10 + (*cur
- '0');
1243 if(gotNeg
) total
= -total
;
1245 if(lpValEnd
) *lpValEnd
= cur
;
1249 static BOOL
MSSTYLES_GetNextInteger(LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, int *value
)
1251 return MSSTYLES_GetNextLong(lpStringStart
, lpStringEnd
, lpValEnd
, (LONG
*)value
);
1254 static inline BOOL
isSpace(WCHAR c
)
1256 return c
== ' ' || c
== '\f' || c
== '\n' || c
== '\r' || c
== '\t' || c
== '\v';
1259 static BOOL
MSSTYLES_GetNextToken(LPCWSTR lpStringStart
, LPCWSTR lpStringEnd
, LPCWSTR
*lpValEnd
, LPWSTR lpBuff
, DWORD buffSize
) {
1260 LPCWSTR cur
= lpStringStart
;
1264 while(cur
< lpStringEnd
&& (isSpace(*cur
) || *cur
== ',')) cur
++;
1265 if(cur
>= lpStringEnd
) {
1269 while(cur
< lpStringEnd
&& *cur
!= ',') cur
++;
1271 while(isSpace(*end
)) end
--;
1273 lstrcpynW(lpBuff
, start
, min(buffSize
, end
-start
+1));
1275 if(lpValEnd
) *lpValEnd
= cur
;
1279 /***********************************************************************
1280 * MSSTYLES_GetPropertyBool
1282 * Retrieve a color value for a property
1284 HRESULT
MSSTYLES_GetPropertyBool(PTHEME_PROPERTY tp
, BOOL
*pfVal
)
1287 if(*tp
->lpValue
== 't' || *tp
->lpValue
== 'T')
1292 /***********************************************************************
1293 * MSSTYLES_GetPropertyColor
1295 * Retrieve a color value for a property
1297 HRESULT
MSSTYLES_GetPropertyColor(PTHEME_PROPERTY tp
, COLORREF
*pColor
)
1301 int red
, green
, blue
;
1303 lpCur
= tp
->lpValue
;
1304 lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1306 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &red
)) {
1307 TRACE("Could not parse color property\n");
1308 return E_PROP_ID_UNSUPPORTED
;
1310 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &green
)) {
1311 TRACE("Could not parse color property\n");
1312 return E_PROP_ID_UNSUPPORTED
;
1314 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &blue
)) {
1315 TRACE("Could not parse color property\n");
1316 return E_PROP_ID_UNSUPPORTED
;
1318 *pColor
= RGB(red
,green
,blue
);
1322 /***********************************************************************
1323 * MSSTYLES_GetPropertyColor
1325 * Retrieve a color value for a property
1327 static HRESULT
MSSTYLES_GetFont (LPCWSTR lpCur
, LPCWSTR lpEnd
,
1328 LPCWSTR
*lpValEnd
, LOGFONTW
* pFont
)
1333 if(!MSSTYLES_GetNextToken(lpCur
, lpEnd
, &lpCur
, pFont
->lfFaceName
, LF_FACESIZE
)) {
1334 TRACE("Property is there, but failed to get face name\n");
1336 return E_PROP_ID_UNSUPPORTED
;
1338 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pointSize
)) {
1339 TRACE("Property is there, but failed to get point size\n");
1341 return E_PROP_ID_UNSUPPORTED
;
1343 pFont
->lfHeight
= pointSize
;
1344 pFont
->lfWeight
= FW_REGULAR
;
1345 pFont
->lfCharSet
= DEFAULT_CHARSET
;
1346 while(MSSTYLES_GetNextToken(lpCur
, lpEnd
, &lpCur
, attr
, ARRAY_SIZE(attr
))) {
1347 if(!lstrcmpiW(L
"bold", attr
)) pFont
->lfWeight
= FW_BOLD
;
1348 else if(!lstrcmpiW(L
"italic", attr
)) pFont
->lfItalic
= TRUE
;
1349 else if(!lstrcmpiW(L
"underline", attr
)) pFont
->lfUnderline
= TRUE
;
1350 else if(!lstrcmpiW(L
"strikeout", attr
)) pFont
->lfStrikeOut
= TRUE
;
1356 HRESULT
MSSTYLES_GetPropertyFont(PTHEME_PROPERTY tp
, HDC hdc
, LOGFONTW
*pFont
)
1358 LPCWSTR lpCur
= tp
->lpValue
;
1359 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1362 ZeroMemory(pFont
, sizeof(LOGFONTW
));
1363 hr
= MSSTYLES_GetFont (lpCur
, lpEnd
, &lpCur
, pFont
);
1365 pFont
->lfHeight
= -MulDiv(pFont
->lfHeight
, GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1370 /***********************************************************************
1371 * MSSTYLES_GetPropertyInt
1373 * Retrieve an int value for a property
1375 HRESULT
MSSTYLES_GetPropertyInt(PTHEME_PROPERTY tp
, int *piVal
)
1377 if(!MSSTYLES_GetNextInteger(tp
->lpValue
, (tp
->lpValue
+ tp
->dwValueLen
), NULL
, piVal
)) {
1378 TRACE("Could not parse int property\n");
1379 return E_PROP_ID_UNSUPPORTED
;
1384 /***********************************************************************
1385 * MSSTYLES_GetPropertyIntList
1387 * Retrieve an int list value for a property
1389 HRESULT
MSSTYLES_GetPropertyIntList(PTHEME_PROPERTY tp
, INTLIST
*pIntList
)
1392 LPCWSTR lpCur
= tp
->lpValue
;
1393 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1395 for(i
=0; i
< MAX_INTLIST_COUNT
; i
++) {
1396 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pIntList
->iValues
[i
]))
1399 pIntList
->iValueCount
= i
;
1403 /***********************************************************************
1404 * MSSTYLES_GetPropertyPosition
1406 * Retrieve a position value for a property
1408 HRESULT
MSSTYLES_GetPropertyPosition(PTHEME_PROPERTY tp
, POINT
*pPoint
)
1411 LPCWSTR lpCur
= tp
->lpValue
;
1412 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1414 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &x
)) {
1415 TRACE("Could not parse position property\n");
1416 return E_PROP_ID_UNSUPPORTED
;
1418 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &y
)) {
1419 TRACE("Could not parse position property\n");
1420 return E_PROP_ID_UNSUPPORTED
;
1427 /***********************************************************************
1428 * MSSTYLES_GetPropertyString
1430 * Retrieve a string value for a property
1432 HRESULT
MSSTYLES_GetPropertyString(PTHEME_PROPERTY tp
, LPWSTR pszBuff
, int cchMaxBuffChars
)
1434 lstrcpynW(pszBuff
, tp
->lpValue
, min(tp
->dwValueLen
+1, cchMaxBuffChars
));
1438 /***********************************************************************
1439 * MSSTYLES_GetPropertyRect
1441 * Retrieve a rect value for a property
1443 HRESULT
MSSTYLES_GetPropertyRect(PTHEME_PROPERTY tp
, RECT
*pRect
)
1445 LPCWSTR lpCur
= tp
->lpValue
;
1446 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1448 MSSTYLES_GetNextLong(lpCur
, lpEnd
, &lpCur
, &pRect
->left
);
1449 MSSTYLES_GetNextLong(lpCur
, lpEnd
, &lpCur
, &pRect
->top
);
1450 MSSTYLES_GetNextLong(lpCur
, lpEnd
, &lpCur
, &pRect
->right
);
1451 if(!MSSTYLES_GetNextLong(lpCur
, lpEnd
, &lpCur
, &pRect
->bottom
)) {
1452 TRACE("Could not parse rect property\n");
1453 return E_PROP_ID_UNSUPPORTED
;
1458 /***********************************************************************
1459 * MSSTYLES_GetPropertyMargins
1461 * Retrieve a margins value for a property
1463 HRESULT
MSSTYLES_GetPropertyMargins(PTHEME_PROPERTY tp
, RECT
*prc
, MARGINS
*pMargins
)
1465 LPCWSTR lpCur
= tp
->lpValue
;
1466 LPCWSTR lpEnd
= tp
->lpValue
+ tp
->dwValueLen
;
1468 MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pMargins
->cxLeftWidth
);
1469 MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pMargins
->cxRightWidth
);
1470 MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pMargins
->cyTopHeight
);
1471 if(!MSSTYLES_GetNextInteger(lpCur
, lpEnd
, &lpCur
, &pMargins
->cyBottomHeight
)) {
1472 TRACE("Could not parse margins property\n");
1473 return E_PROP_ID_UNSUPPORTED
;