📄 msstyles.c
字号:
/*
* Win32 5.1 msstyles theme format
*
* Copyright (C) 2003 Kevin Koltzau
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winnls.h"
#include "vfwmsgs.h"
#include "uxtheme.h"
#include "tmschema.h"
#include "msstyles.h"
#include "wine/unicode.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);
/***********************************************************************
* Defines and global variables
*/
BOOL MSSTYLES_GetNextInteger(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, int *value);
BOOL MSSTYLES_GetNextToken(LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LPWSTR lpBuff, DWORD buffSize);
void MSSTYLES_ParseThemeIni(PTHEME_FILE tf, BOOL setMetrics);
static HRESULT MSSTYLES_GetFont (LPCWSTR lpStringStart, LPCWSTR lpStringEnd, LPCWSTR *lpValEnd, LOGFONTW* logfont);
extern HINSTANCE hDllInst;
extern int alphaBlendMode;
#define MSSTYLES_VERSION 0x0003
static const WCHAR szThemesIniResource[] = {
't','h','e','m','e','s','_','i','n','i','\0'
};
static PTHEME_FILE tfActiveTheme;
/***********************************************************************/
/**********************************************************************
* MSSTYLES_OpenThemeFile
*
* Load and validate a theme
*
* PARAMS
* lpThemeFile Path to theme file to load
* pszColorName Color name wanted, can be NULL
* pszSizeName Size name wanted, can be NULL
*
* NOTES
* If pszColorName or pszSizeName are NULL, the default color/size will be used.
* If one/both are provided, they are validated against valid color/sizes and if
* a match is not found, the function fails.
*/
HRESULT MSSTYLES_OpenThemeFile(LPCWSTR lpThemeFile, LPCWSTR pszColorName, LPCWSTR pszSizeName, PTHEME_FILE *tf)
{
HMODULE hTheme;
HRSRC hrsc;
HRESULT hr = S_OK;
static const WCHAR szPackThemVersionResource[] = {
'P','A','C','K','T','H','E','M','_','V','E','R','S','I','O','N', '\0'
};
static const WCHAR szColorNamesResource[] = {
'C','O','L','O','R','N','A','M','E','S','\0'
};
static const WCHAR szSizeNamesResource[] = {
'S','I','Z','E','N','A','M','E','S','\0'
};
WORD version;
DWORD versize;
LPWSTR pszColors;
LPWSTR pszSelectedColor = NULL;
LPWSTR pszSizes;
LPWSTR pszSelectedSize = NULL;
LPWSTR tmp;
TRACE("Opening %s\n", debugstr_w(lpThemeFile));
hTheme = LoadLibraryExW(lpThemeFile, NULL, LOAD_LIBRARY_AS_DATAFILE);
/* Validate that this is really a theme */
if(!hTheme) {
hr = HRESULT_FROM_WIN32(GetLastError());
goto invalid_theme;
}
if(!(hrsc = FindResourceW(hTheme, MAKEINTRESOURCEW(1), szPackThemVersionResource))) {
TRACE("No version resource found\n");
hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
goto invalid_theme;
}
if((versize = SizeofResource(hTheme, hrsc)) != 2)
{
TRACE("Version resource found, but wrong size: %d\n", versize);
hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
goto invalid_theme;
}
version = *(WORD*)LoadResource(hTheme, hrsc);
if(version != MSSTYLES_VERSION)
{
TRACE("Version of theme file is unsupported: 0x%04x\n", version);
hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
goto invalid_theme;
}
if(!(hrsc = FindResourceW(hTheme, MAKEINTRESOURCEW(1), szColorNamesResource))) {
TRACE("Color names resource not found\n");
hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
goto invalid_theme;
}
pszColors = (LPWSTR)LoadResource(hTheme, hrsc);
if(!(hrsc = FindResourceW(hTheme, MAKEINTRESOURCEW(1), szSizeNamesResource))) {
TRACE("Size names resource not found\n");
hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
goto invalid_theme;
}
pszSizes = (LPWSTR)LoadResource(hTheme, hrsc);
/* Validate requested color against whats available from the theme */
if(pszColorName) {
tmp = pszColors;
while(*tmp) {
if(!lstrcmpiW(pszColorName, tmp)) {
pszSelectedColor = tmp;
break;
}
tmp += lstrlenW(tmp)+1;
}
}
else
pszSelectedColor = pszColors; /* Use the default color */
/* Validate requested size against whats available from the theme */
if(pszSizeName) {
tmp = pszSizes;
while(*tmp) {
if(!lstrcmpiW(pszSizeName, tmp)) {
pszSelectedSize = tmp;
break;
}
tmp += lstrlenW(tmp)+1;
}
}
else
pszSelectedSize = pszSizes; /* Use the default size */
if(!pszSelectedColor || !pszSelectedSize) {
TRACE("Requested color/size (%s/%s) not found in theme\n",
debugstr_w(pszColorName), debugstr_w(pszSizeName));
hr = E_PROP_ID_UNSUPPORTED;
goto invalid_theme;
}
*tf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(THEME_FILE));
(*tf)->hTheme = hTheme;
GetFullPathNameW(lpThemeFile, MAX_PATH, (*tf)->szThemeFile, NULL);
(*tf)->pszAvailColors = pszColors;
(*tf)->pszAvailSizes = pszSizes;
(*tf)->pszSelectedColor = pszSelectedColor;
(*tf)->pszSelectedSize = pszSelectedSize;
(*tf)->dwRefCount = 1;
return S_OK;
invalid_theme:
if(hTheme) FreeLibrary(hTheme);
return hr;
}
/***********************************************************************
* MSSTYLES_CloseThemeFile
*
* Close theme file and free resources
*/
void MSSTYLES_CloseThemeFile(PTHEME_FILE tf)
{
if(tf) {
tf->dwRefCount--;
if(!tf->dwRefCount) {
if(tf->hTheme) FreeLibrary(tf->hTheme);
if(tf->classes) {
while(tf->classes) {
PTHEME_CLASS pcls = tf->classes;
tf->classes = pcls->next;
while(pcls->partstate) {
PTHEME_PARTSTATE ps = pcls->partstate;
pcls->partstate = ps->next;
HeapFree(GetProcessHeap(), 0, ps);
}
HeapFree(GetProcessHeap(), 0, pcls);
}
}
while (tf->images)
{
PTHEME_IMAGE img = tf->images;
tf->images = img->next;
DeleteObject (img->image);
HeapFree (GetProcessHeap(), 0, img);
}
HeapFree(GetProcessHeap(), 0, tf);
}
}
}
/***********************************************************************
* MSSTYLES_SetActiveTheme
*
* Set the current active theme
*/
HRESULT MSSTYLES_SetActiveTheme(PTHEME_FILE tf, BOOL setMetrics)
{
if(tfActiveTheme)
MSSTYLES_CloseThemeFile(tfActiveTheme);
tfActiveTheme = tf;
if (tfActiveTheme)
{
tfActiveTheme->dwRefCount++;
if(!tfActiveTheme->classes)
MSSTYLES_ParseThemeIni(tfActiveTheme, setMetrics);
}
return S_OK;
}
/***********************************************************************
* MSSTYLES_GetThemeIni
*
* Retrieves themes.ini from a theme
*/
PUXINI_FILE MSSTYLES_GetThemeIni(PTHEME_FILE tf)
{
return UXINI_LoadINI(tf->hTheme, szThemesIniResource);
}
/***********************************************************************
* MSSTYLES_GetActiveThemeIni
*
* Retrieve the ini file for the selected color/style
*/
static PUXINI_FILE MSSTYLES_GetActiveThemeIni(PTHEME_FILE tf)
{
static const WCHAR szFileResNamesResource[] = {
'F','I','L','E','R','E','S','N','A','M','E','S','\0'
};
DWORD dwColorCount = 0;
DWORD dwSizeCount = 0;
DWORD dwColorNum = 0;
DWORD dwSizeNum = 0;
DWORD i;
DWORD dwResourceIndex;
LPWSTR tmp;
HRSRC hrsc;
/* Count the number of available colors & styles, and determine the index number
of the color/style we are interested in
*/
tmp = tf->pszAvailColors;
while(*tmp) {
if(!lstrcmpiW(tf->pszSelectedColor, tmp))
dwColorNum = dwColorCount;
tmp += lstrlenW(tmp)+1;
dwColorCount++;
}
tmp = tf->pszAvailSizes;
while(*tmp) {
if(!lstrcmpiW(tf->pszSelectedSize, tmp))
dwSizeNum = dwSizeCount;
tmp += lstrlenW(tmp)+1;
dwSizeCount++;
}
if(!(hrsc = FindResourceW(tf->hTheme, MAKEINTRESOURCEW(1), szFileResNamesResource))) {
TRACE("FILERESNAMES map not found\n");
return NULL;
}
tmp = (LPWSTR)LoadResource(tf->hTheme, hrsc);
dwResourceIndex = (dwSizeCount * dwColorNum) + dwSizeNum;
for(i=0; i < dwResourceIndex; i++) {
tmp += lstrlenW(tmp)+1;
}
return UXINI_LoadINI(tf->hTheme, tmp);
}
/***********************************************************************
* MSSTYLES_ParseIniSectionName
*
* Parse an ini section name into its component parts
* Valid formats are:
* [classname]
* [classname(state)]
* [classname.part]
* [classname.part(state)]
* [application::classname]
* [application::classname(state)]
* [application::classname.part]
* [application::classname.part(state)]
*
* PARAMS
* lpSection Section name
* dwLen Length of section name
* szAppName Location to store application name
* szClassName Location to store class name
* iPartId Location to store part id
* iStateId Location to store state id
*/
static BOOL MSSTYLES_ParseIniSectionName(LPCWSTR lpSection, DWORD dwLen, LPWSTR szAppName, LPWSTR szClassName, int *iPartId, int *iStateId)
{
WCHAR sec[255];
WCHAR part[60] = {'\0'};
WCHAR state[60] = {'\0'};
LPWSTR tmp;
LPWSTR comp;
lstrcpynW(sec, lpSection, min(dwLen+1, sizeof(sec)/sizeof(sec[0])));
*szAppName = 0;
*szClassName = 0;
*iPartId = 0;
*iStateId = 0;
comp = sec;
/* Get the application name */
tmp = strchrW(comp, ':');
if(tmp) {
*tmp++ = 0;
tmp++;
lstrcpynW(szAppName, comp, MAX_THEME_APP_NAME);
comp = tmp;
}
tmp = strchrW(comp, '.');
if(tmp) {
*tmp++ = 0;
lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
comp = tmp;
/* now get the part & state */
tmp = strchrW(comp, '(');
if(tmp) {
*tmp++ = 0;
lstrcpynW(part, comp, sizeof(part)/sizeof(part[0]));
comp = tmp;
/* now get the state */
*strchrW(comp, ')') = 0;
lstrcpynW(state, comp, sizeof(state)/sizeof(state[0]));
}
else {
lstrcpynW(part, comp, sizeof(part)/sizeof(part[0]));
}
}
else {
tmp = strchrW(comp, '(');
if(tmp) {
*tmp++ = 0;
lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
comp = tmp;
/* now get the state */
*strchrW(comp, ')') = 0;
lstrcpynW(state, comp, sizeof(state)/sizeof(state[0]));
}
else {
lstrcpynW(szClassName, comp, MAX_THEME_CLASS_NAME);
}
}
if(!*szClassName) return FALSE;
return MSSTYLES_LookupPartState(szClassName, part[0]?part:NULL, state[0]?state:NULL, iPartId, iStateId);
}
/***********************************************************************
* MSSTYLES_FindClass
*
* Find a class
*
* PARAMS
* tf Theme file
* pszAppName App name to find
* pszClassName Class name to find
*
* RETURNS
* The class found, or NULL
*/
static PTHEME_CLASS MSSTYLES_FindClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
{
PTHEME_CLASS cur = tf->classes;
while(cur) {
if(!pszAppName) {
if(!*cur->szAppName && !lstrcmpiW(pszClassName, cur->szClassName))
return cur;
}
else {
if(!lstrcmpiW(pszAppName, cur->szAppName) && !lstrcmpiW(pszClassName, cur->szClassName))
return cur;
}
cur = cur->next;
}
return NULL;
}
/***********************************************************************
* MSSTYLES_AddClass
*
* Add a class to a theme file
*
* PARAMS
* tf Theme file
* pszAppName App name to add
* pszClassName Class name to add
*
* RETURNS
* The class added, or a class previously added with the same name
*/
static PTHEME_CLASS MSSTYLES_AddClass(PTHEME_FILE tf, LPCWSTR pszAppName, LPCWSTR pszClassName)
{
PTHEME_CLASS cur = MSSTYLES_FindClass(tf, pszAppName, pszClassName);
if(cur) return cur;
cur = HeapAlloc(GetProcessHeap(), 0, sizeof(THEME_CLASS));
cur->hTheme = tf->hTheme;
lstrcpyW(cur->szAppName, pszAppName);
lstrcpyW(cur->szClassName, pszClassName);
cur->next = tf->classes;
cur->partstate = NULL;
cur->overrides = NULL;
tf->classes = cur;
return cur;
}
/***********************************************************************
* MSSTYLES_FindPartState
*
* Find a part/state
*
* PARAMS
* tc Class to search
* iPartId Part ID to find
* iStateId State ID to find
* tcNext Receives the next class in the override chain
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -