[UXTHEME] -Fix most new tests for DrawThemeParentBackground. Hackfix CloseThemeData.
[reactos.git] / reactos / dll / win32 / uxtheme / system.c
index 40747f6..393778f 100644 (file)
  */
 
 #include "uxthemep.h"
-#include <wine/debug.h>
 
-WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);
+#include <stdio.h>
+#include <winreg.h>
+#include <uxundoc.h>
 
 /***********************************************************************
  * Defines and global variables
@@ -48,17 +49,15 @@ static DWORD dwThemeAppProperties = STAP_ALLOW_NONCLIENT | STAP_ALLOW_CONTROLS;
 ATOM atWindowTheme;
 static ATOM atSubAppName;
 static ATOM atSubIdList;
-ATOM atWndContrext;
+ATOM atWndContext;
 
-static BOOL bThemeActive = FALSE;
-
-static PTHEME_FILE ActiveThemeFile;
+PTHEME_FILE ActiveThemeFile;
 
 /***********************************************************************/
 
 static BOOL CALLBACK UXTHEME_broadcast_msg_enumchild (HWND hWnd, LPARAM msg)
 {
-    PostMessageW(hWnd, msg, 0, 0);
+    SendMessageW(hWnd, msg, 0, 0);
     return TRUE;
 }
 
@@ -71,7 +70,7 @@ BOOL CALLBACK UXTHEME_broadcast_msg (HWND hWnd, LPARAM msg)
     }
     else
     {
-       PostMessageW(hWnd, msg, 0, 0);
+       SendMessageW(hWnd, msg, 0, 0);
        EnumChildWindows (hWnd, UXTHEME_broadcast_msg_enumchild, msg);
     }
     return TRUE;
@@ -82,7 +81,7 @@ BOOL CALLBACK UXTHEME_broadcast_msg (HWND hWnd, LPARAM msg)
 static DWORD query_reg_path (HKEY hKey, LPCWSTR lpszValue,
                              LPVOID pvData)
 {
-  DWORD dwRet, dwType, dwUnExpDataLen = MAX_PATH, dwExpDataLen;
+  DWORD dwRet, dwType, dwUnExpDataLen = MAX_PATH * sizeof(WCHAR), dwExpDataLen;
 
   TRACE("(hkey=%p,%s,%p)\n", hKey, debugstr_w(lpszValue),
         pvData);
@@ -140,6 +139,39 @@ static HRESULT UXTHEME_SetActiveTheme(PTHEME_FILE tf)
     return S_OK;
 }
 
+static BOOL bIsThemeActive(LPCWSTR pszTheme, LPCWSTR pszColor, LPCWSTR pszSize)
+{
+    if (ActiveThemeFile == NULL)
+        return FALSE;
+
+    if (wcscmp(pszTheme, ActiveThemeFile->szThemeFile) != 0)
+        return FALSE;
+
+    if (!pszColor[0])
+    {
+        if (ActiveThemeFile->pszAvailColors != ActiveThemeFile->pszSelectedColor)
+            return FALSE;
+    }
+    else
+    {
+        if (wcscmp(pszColor, ActiveThemeFile->pszSelectedColor) != 0)
+            return FALSE;
+    }
+
+    if (!pszSize[0])
+    {
+        if (ActiveThemeFile->pszAvailSizes != ActiveThemeFile->pszSelectedSize)
+            return FALSE;
+    }
+    else
+    {
+        if (wcscmp(pszSize, ActiveThemeFile->pszSelectedSize) != 0)
+            return FALSE;
+    }
+
+    return TRUE;
+}
+
 /***********************************************************************
  *      UXTHEME_LoadTheme
  *
@@ -155,13 +187,14 @@ void UXTHEME_LoadTheme(BOOL bLoad)
     WCHAR szCurrentTheme[MAX_PATH];
     WCHAR szCurrentColor[64];
     WCHAR szCurrentSize[64];
+    BOOL bThemeActive = FALSE;
 
     if(bLoad == TRUE) 
     {
         /* Get current theme configuration */
         if(!RegOpenKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
             TRACE("Loading theme config\n");
-            buffsize = sizeof(tmp)/sizeof(tmp[0]);
+            buffsize = sizeof(tmp);
             if(!RegQueryValueExW(hKey, szThemeActive, NULL, NULL, (LPBYTE)tmp, &buffsize)) {
                 bThemeActive = (tmp[0] != '0');
             }
@@ -169,10 +202,10 @@ void UXTHEME_LoadTheme(BOOL bLoad)
                 bThemeActive = FALSE;
                 TRACE("Failed to get ThemeActive: %d\n", GetLastError());
             }
-            buffsize = sizeof(szCurrentColor)/sizeof(szCurrentColor[0]);
+            buffsize = sizeof(szCurrentColor);
             if(RegQueryValueExW(hKey, szColorName, NULL, NULL, (LPBYTE)szCurrentColor, &buffsize))
                 szCurrentColor[0] = '\0';
-            buffsize = sizeof(szCurrentSize)/sizeof(szCurrentSize[0]);
+            buffsize = sizeof(szCurrentSize);
             if(RegQueryValueExW(hKey, szSizeName, NULL, NULL, (LPBYTE)szCurrentSize, &buffsize))
                 szCurrentSize[0] = '\0';
             if (query_reg_path (hKey, szDllName, szCurrentTheme))
@@ -187,7 +220,14 @@ void UXTHEME_LoadTheme(BOOL bLoad)
         bThemeActive = FALSE;
     }
 
-    if(bThemeActive) {
+    if(bThemeActive)
+    {
+        if( bIsThemeActive(szCurrentTheme, szCurrentColor, szCurrentSize) )
+        {
+            TRACE("Tried to load active theme again\n");
+            return;
+        }
+
         /* Make sure the theme requested is actually valid */
         hr = MSSTYLES_OpenThemeFile(szCurrentTheme,
                                     szCurrentColor[0]?szCurrentColor:NULL,
@@ -472,24 +512,28 @@ static HRESULT UXTHEME_ApplyTheme(PTHEME_FILE tf)
     WCHAR tmp[2];
     HRESULT hr;
 
-    if(tf && !bThemeActive) UXTHEME_BackupSystemMetrics();
+    TRACE("UXTHEME_ApplyTheme\n");
+
+    if (tf && !ActiveThemeFile)
+    {
+        UXTHEME_BackupSystemMetrics();
+    }
+
     hr = UXTHEME_SetActiveTheme(tf);
-    if(FAILED(hr))
+    if (FAILED(hr))
         return hr;
-    if(tf) {
-        bThemeActive = TRUE;
-    }
-    else {
+
+    if (!tf) 
+    {
         UXTHEME_RestoreSystemMetrics();
-        bThemeActive = FALSE;
     }
 
     TRACE("Writing theme config to registry\n");
     if(!RegCreateKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
-        tmp[0] = bThemeActive?'1':'0';
+        tmp[0] = ActiveThemeFile?'1':'0';
         tmp[1] = '\0';
         RegSetValueExW(hKey, szThemeActive, 0, REG_SZ, (const BYTE*)tmp, sizeof(WCHAR)*2);
-        if(bThemeActive) {
+        if (ActiveThemeFile) {
             RegSetValueExW(hKey, szColorName, 0, REG_SZ, (const BYTE*)tf->pszSelectedColor, 
                (lstrlenW(tf->pszSelectedColor)+1)*sizeof(WCHAR));
             RegSetValueExW(hKey, szSizeName, 0, REG_SZ, (const BYTE*)tf->pszSelectedSize, 
@@ -537,7 +581,7 @@ void UXTHEME_InitSystem(HINSTANCE hInst)
     atSubAppName         = GlobalAddAtomW(szSubAppName);
     atSubIdList          = GlobalAddAtomW(szSubIdList);
     atDialogThemeEnabled = GlobalAddAtomW(szDialogThemeEnabled);
-    atWndContrext        = GlobalAddAtomW(L"ux_WndContext");
+    atWndContext        = GlobalAddAtomW(L"ux_WndContext");
 }
 
 /***********************************************************************
@@ -545,7 +589,9 @@ void UXTHEME_InitSystem(HINSTANCE hInst)
  */
 BOOL WINAPI IsAppThemed(void)
 {
-    return IsThemeActive();
+    TRACE("\n");
+    SetLastError(ERROR_SUCCESS);
+    return (ActiveThemeFile != NULL);
 }
 
 /***********************************************************************
@@ -553,9 +599,32 @@ BOOL WINAPI IsAppThemed(void)
  */
 BOOL WINAPI IsThemeActive(void)
 {
-    TRACE("\n");
+    BOOL bActive;
+    LRESULT Result;
+    HKEY hKey;
+    WCHAR tmp[10];
+    DWORD buffsize;
+
+    TRACE("IsThemeActive\n");
     SetLastError(ERROR_SUCCESS);
-    return bThemeActive;
+
+    if (ActiveThemeFile) 
+        return TRUE;
+
+    if (gbThemeHooksActive)
+        return FALSE;
+
+    bActive = FALSE;
+    Result = RegOpenKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey);
+    if (Result == ERROR_SUCCESS)
+    {
+        buffsize = sizeof(tmp);
+        if (!RegQueryValueExW(hKey, szThemeActive, NULL, NULL, (LPBYTE)tmp, &buffsize)) 
+            bActive = (tmp[0] != '0');
+        RegCloseKey(hKey);
+    }
+
+    return bActive;
 }
 
 /***********************************************************************
@@ -571,14 +640,14 @@ HRESULT WINAPI EnableTheming(BOOL fEnable)
 
     TRACE("(%d)\n", fEnable);
 
-    if(fEnable != bThemeActive) {
+    if (fEnable != (ActiveThemeFile != NULL)) {
         if(fEnable) 
             UXTHEME_BackupSystemMetrics();
         else
             UXTHEME_RestoreSystemMetrics();
         UXTHEME_SaveSystemMetrics ();
-        bThemeActive = fEnable;
-        if(bThemeActive) szEnabled[0] = '1';
+
+        if (fEnable) szEnabled[0] = '1';
         if(!RegOpenKeyW(HKEY_CURRENT_USER, szThemeManager, &hKey)) {
             RegSetValueExW(hKey, szThemeActive, 0, REG_SZ, (LPBYTE)szEnabled, sizeof(WCHAR));
             RegCloseKey(hKey);
@@ -598,14 +667,31 @@ static HRESULT UXTHEME_SetWindowProperty(HWND hwnd, ATOM aProp, LPCWSTR pszValue
 {
     ATOM oldValue = (ATOM)(size_t)RemovePropW(hwnd, (LPCWSTR)MAKEINTATOM(aProp));
     if(oldValue)
+    {
         DeleteAtom(oldValue);
-    if(pszValue) {
-        ATOM atValue = AddAtomW(pszValue);
-        if(!atValue
-           || !SetPropW(hwnd, (LPCWSTR)MAKEINTATOM(aProp), (LPWSTR)MAKEINTATOM(atValue))) {
-            HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
+    }
+
+    if(pszValue) 
+    {
+        ATOM atValue;
+
+        /* A string with zero lenght is not acceptatble in AddAtomW but we want to support
+           users passing an empty string meaning they want no theme for the specified window */
+        if(!pszValue[0])
+            pszValue = L"0";
+
+        atValue = AddAtomW(pszValue);
+        if (!atValue)
+        {
+            ERR("AddAtomW for %S failed with last error %d!\n", pszValue, GetLastError());
+            return HRESULT_FROM_WIN32(GetLastError());
+        }
+
+        if (!SetPropW(hwnd, (LPCWSTR)MAKEINTATOM(aProp), (LPWSTR)MAKEINTATOM(atValue)))
+        {
+            ERR("SetPropW for atom %d failed with last error %d\n", aProp, GetLastError());
             if(atValue) DeleteAtom(atValue);
-            return hr;
+            return HRESULT_FROM_WIN32(GetLastError());
         }
     }
     return S_OK;
@@ -630,8 +716,8 @@ OpenThemeDataInternal(PTHEME_FILE ThemeFile, HWND hwnd, LPCWSTR pszClassList, DW
     LPCWSTR pszAppName;
     LPCWSTR pszUseClassList;
     HTHEME hTheme = NULL;
-    TRACE("(%p,%s)\n", hwnd, debugstr_w(pszClassList));
-    
+    TRACE("(%p,%s, %x)\n", hwnd, debugstr_w(pszClassList), flags);
+
     if(!pszClassList)
     {
         SetLastError(E_POINTER);
@@ -641,7 +727,7 @@ OpenThemeDataInternal(PTHEME_FILE ThemeFile, HWND hwnd, LPCWSTR pszClassList, DW
     if(flags)
         FIXME("unhandled flags: %x\n", flags);
 
-    if(bThemeActive)
+    if (ThemeFile)
     {
         pszAppName = UXTHEME_GetWindowProperty(hwnd, atSubAppName, szAppBuff, sizeof(szAppBuff)/sizeof(szAppBuff[0]));
         /* If SetWindowTheme was used on the window, that overrides the class list passed to this function */
@@ -650,10 +736,24 @@ OpenThemeDataInternal(PTHEME_FILE ThemeFile, HWND hwnd, LPCWSTR pszClassList, DW
             pszUseClassList = pszClassList;
 
         if (pszUseClassList)
+        {
+            if (!ThemeFile->classes)
+                MSSTYLES_ParseThemeIni(ThemeFile);
             hTheme = MSSTYLES_OpenThemeClass(ThemeFile, pszAppName, pszUseClassList);
+        }
     }
+
     if(IsWindow(hwnd))
+    {
         SetPropW(hwnd, (LPCWSTR)MAKEINTATOM(atWindowTheme), hTheme);
+    }
+    else
+    {
+        SetLastError(E_PROP_ID_UNSUPPORTED);
+    }
+
+    SetLastError(hTheme ? ERROR_SUCCESS : E_PROP_ID_UNSUPPORTED);
+
     TRACE(" = %p\n", hTheme);
     return hTheme;
 }
@@ -667,15 +767,11 @@ HTHEME WINAPI OpenThemeDataEx(HWND hwnd, LPCWSTR pszClassList, DWORD flags)
 }
 
 /***********************************************************************
- *      OpenThemeDataEx                                     (UXTHEME.16)
+ *      OpenThemeDataFromFile                               (UXTHEME.16)
  */
 HTHEME WINAPI OpenThemeDataFromFile(HTHEMEFILE hThemeFile, HWND hwnd, LPCWSTR pszClassList, DWORD flags)
 {
-    PTHEME_FILE ThemeFile = (PTHEME_FILE)hThemeFile;
-
-    MSSTYLES_ParseThemeIni(ThemeFile);
-
-    return OpenThemeDataInternal(ThemeFile, hwnd, pszClassList, flags);
+    return OpenThemeDataInternal((PTHEME_FILE)hThemeFile, hwnd, pszClassList, flags);
 }
 
 /***********************************************************************
@@ -700,8 +796,12 @@ HTHEME WINAPI OpenThemeData(HWND hwnd, LPCWSTR classlist)
 HTHEME WINAPI GetWindowTheme(HWND hwnd)
 {
     TRACE("(%p)\n", hwnd);
+
        if(!IsWindow(hwnd))
+    {
                SetLastError(E_HANDLE);
+        return NULL;
+    }
 
     return GetPropW(hwnd, (LPCWSTR)MAKEINTATOM(atWindowTheme));
 }
@@ -714,18 +814,22 @@ HTHEME WINAPI GetWindowTheme(HWND hwnd)
 HRESULT WINAPI SetWindowTheme(HWND hwnd, LPCWSTR pszSubAppName,
                               LPCWSTR pszSubIdList)
 {
-       HRESULT hr;
+    HRESULT hr;
     TRACE("(%p,%s,%s)\n", hwnd, debugstr_w(pszSubAppName),
         debugstr_w(pszSubIdList));
     
     if(!IsWindow(hwnd))
-               return E_HANDLE;
+        return E_HANDLE;
 
     hr = UXTHEME_SetWindowProperty(hwnd, atSubAppName, pszSubAppName);
-    if(SUCCEEDED(hr))
-        hr = UXTHEME_SetWindowProperty(hwnd, atSubIdList, pszSubIdList);
-    if(SUCCEEDED(hr))
-       UXTHEME_broadcast_msg (hwnd, WM_THEMECHANGED);
+    if (!SUCCEEDED(hr))
+        return hr;
+
+    hr = UXTHEME_SetWindowProperty(hwnd, atSubIdList, pszSubIdList);
+    if (!SUCCEEDED(hr))
+        return hr;
+
+    UXTHEME_broadcast_msg (hwnd, WM_THEMECHANGED);
     return hr;
 }
 
@@ -741,7 +845,7 @@ HRESULT WINAPI GetCurrentThemeName(LPWSTR pszThemeFileName, int dwMaxNameChars,
     if(ActiveThemeFile == NULL)
          return E_PROP_ID_UNSUPPORTED;
 
-    if(pszThemeFileName
+    if (pszThemeFileName && dwMaxNameChars
     {
         cchar = lstrlenW(ActiveThemeFile->szThemeFile) + 1;
         if(cchar > dwMaxNameChars)
@@ -749,7 +853,7 @@ HRESULT WINAPI GetCurrentThemeName(LPWSTR pszThemeFileName, int dwMaxNameChars,
         lstrcpynW(pszThemeFileName, ActiveThemeFile->szThemeFile, cchar);
     }
 
-    if(pszColorBuff
+    if (pszColorBuff && cchMaxColorChars
     {
         cchar = lstrlenW(ActiveThemeFile->pszSelectedColor) + 1;
         if(cchar > cchMaxColorChars)
@@ -757,7 +861,7 @@ HRESULT WINAPI GetCurrentThemeName(LPWSTR pszThemeFileName, int dwMaxNameChars,
         lstrcpynW(pszColorBuff, ActiveThemeFile->pszSelectedColor, cchar);
     }
 
-   if(pszSizeBuff
+   if (pszSizeBuff && cchMaxSizeChars
     {
         cchar = lstrlenW(ActiveThemeFile->pszSelectedSize) + 1;
         if(cchar > cchMaxSizeChars)
@@ -791,7 +895,9 @@ void WINAPI SetThemeAppProperties(DWORD dwFlags)
 HRESULT WINAPI CloseThemeData(HTHEME hTheme)
 {
     TRACE("(%p)\n", hTheme);
-    if(!hTheme)
+    if(!hTheme || hTheme == INVALID_HANDLE_VALUE)
+        return E_HANDLE;
+    if(IsBadReadPtr (hTheme, sizeof(THEME_CLASS))) /* This check is a hack! */
         return E_HANDLE;
     return MSSTYLES_CloseThemeClass(hTheme);
 }
@@ -807,7 +913,7 @@ HRESULT WINAPI HitTestThemeBackground(HTHEME hTheme, HDC hdc, int iPartId,
     FIXME("%d %d 0x%08x: stub\n", iPartId, iStateId, dwOptions);
     if(!hTheme)
         return E_HANDLE;
-    return ERROR_CALL_NOT_IMPLEMENTED;
+    return E_NOTIMPL;
 }
 
 /***********************************************************************
@@ -1233,7 +1339,7 @@ HRESULT WINAPI ParseThemeIniFile(LPCWSTR pszIniFileName, LPWSTR pszUnknown,
                                  PARSETHEMEINIFILEPROC callback, LPVOID lpData)
 {
     FIXME("%s %s: stub\n", debugstr_w(pszIniFileName), debugstr_w(pszUnknown));
-    return ERROR_CALL_NOT_IMPLEMENTED;
+    return E_NOTIMPL;
 }
 
 /**********************************************************************
@@ -1259,16 +1365,3 @@ HRESULT WINAPI CheckThemeSignature(LPCWSTR pszThemeFileName)
     MSSTYLES_CloseThemeFile(pt);
     return S_OK;
 }
-
-HRESULT WINAPI DrawNCPreview(HDC hDC, 
-                             DWORD DNCP_Flag,
-                             LPRECT prcPreview, 
-                             LPCWSTR pszThemeFileName, 
-                             LPCWSTR pszColorName,
-                             LPCWSTR pszSizeName,
-                             PNONCLIENTMETRICSW pncMetrics,
-                             COLORREF* lpaRgbValues)
-{
-    return E_NOTIMPL;
-}
-