fix bug 838 (Sol.exe is missing it's menubar)
[reactos.git] / reactos / lib / user32 / windows / class.c
index dbfd1f7..935286c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: class.c,v 1.54 2004/12/21 21:38:26 weiden Exp $
+/* $Id$
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS user32.dll
@@ -9,14 +9,7 @@
  *      09-05-2001  CSH  Created
  */
 
-#include "user32.h"
-#include <string.h>
-#include <stdlib.h>
-#include <debug.h>
-#include <window.h>
-#include <strpool.h>
-#include <user32/regcontrol.h>
-
+#include <user32.h>
 
 static BOOL GetClassInfoExCommon(
     HINSTANCE hInst,
@@ -40,6 +33,8 @@ static BOOL GetClassInfoExCommon(
     str = (LPWSTR)lpszClass;
   else
   {
+    extern BOOL ControlsInitialized;
+
     if (unicode)
     {
         str = HEAP_strdupW ( lpszClass, wcslen(lpszClass) );
@@ -54,13 +49,19 @@ static BOOL GetClassInfoExCommon(
     else
     {
         Status = HEAP_strdupAtoW(&str, (LPCSTR)lpszClass, NULL);
-        
+
         if (! NT_SUCCESS(Status))
         {
             SetLastError(RtlNtStatusToDosError(Status));
             return FALSE;
         }
     }
+
+    /* Register built-in controls if not already done */
+    if ( !ControlsInitialized )
+    {
+      ControlsInitialized = ControlsInit(str);
+    }
   }
 
   str2.Length = str3.Length = 0;
@@ -78,6 +79,7 @@ static BOOL GetClassInfoExCommon(
   if ( !str3.Buffer )
   {
     SetLastError (RtlNtStatusToDosError(STATUS_NO_MEMORY));
+    HEAP_free ( str2.Buffer );
     if ( !IS_ATOM(str) )
       HEAP_free ( str );
     return FALSE;
@@ -85,7 +87,14 @@ static BOOL GetClassInfoExCommon(
 
   w.lpszMenuName = (LPCWSTR)&str2;
   w.lpszClassName = (LPCWSTR)&str3;
+  
+  /* get info about system classes? */
+  if (!hInst) hInst = User32Instance;
+  
   retval = (BOOL)NtUserGetClassInfo(hInst, str, &w, TRUE, 0);
+  
+  w.hInstance = (hInst == User32Instance) ? 0 : hInst;
+  
   if ( !IS_ATOM(str) )
     HEAP_free(str);
 
@@ -269,7 +278,7 @@ GetClassNameA(
   int result;
   LPWSTR ClassNameW;
   NTSTATUS Status;
-  
+
   if(!lpClassName)
     return 0;
 
@@ -391,6 +400,170 @@ RealGetWindowClassA(
        return GetClassNameA(hwnd,pszType,cchType);
 }
 
+/*
+ * Create a small icon based on a standard icon
+ */
+static HICON
+CreateSmallIcon(HICON StdIcon)
+{
+   HICON SmallIcon = NULL;
+   ICONINFO StdInfo;
+   int SmallIconWidth;
+   int SmallIconHeight;
+   BITMAP StdBitmapInfo;
+   HDC hInfoDc = NULL;
+   HDC hSourceDc = NULL;
+   HDC hDestDc = NULL;
+   ICONINFO SmallInfo;
+   HBITMAP OldSourceBitmap = NULL;
+   HBITMAP OldDestBitmap = NULL;
+
+   SmallInfo.hbmColor = NULL;
+   SmallInfo.hbmMask = NULL;
+
+   /* We need something to work with... */
+   if (NULL == StdIcon)
+   {
+      goto cleanup;
+   }
+
+   SmallIconWidth = GetSystemMetrics(SM_CXSMICON);
+   SmallIconHeight = GetSystemMetrics(SM_CYSMICON);
+   if (! GetIconInfo(StdIcon, &StdInfo))
+   {
+      DPRINT1("Failed to get icon info for icon 0x%x\n", StdIcon);
+      goto cleanup;
+   }
+   if (! GetObjectW(StdInfo.hbmMask, sizeof(BITMAP), &StdBitmapInfo))
+   {
+      DPRINT1("Failed to get bitmap info for icon 0x%x bitmap 0x%x\n",
+              StdIcon, StdInfo.hbmColor);
+      goto cleanup;
+   }
+   if (StdBitmapInfo.bmWidth == SmallIconWidth &&
+       StdBitmapInfo.bmHeight == SmallIconHeight)
+   {
+      /* Icon already has the correct dimensions */
+      return StdIcon;
+   }
+
+   /* Get a handle to a info DC and handles to DCs which can be used to
+      select a bitmap into. This is done to avoid triggering a switch to
+      graphics mode (if we're currently in text/blue screen mode) */
+   hInfoDc = CreateICW(NULL, NULL, NULL, NULL);
+   if (NULL == hInfoDc)
+   {
+      DPRINT1("Failed to create info DC\n");
+      goto cleanup;
+   }
+   hSourceDc = CreateCompatibleDC(NULL);
+   if (NULL == hSourceDc)
+   {
+      DPRINT1("Failed to create source DC\n");
+      goto cleanup;
+   }
+   hDestDc = CreateCompatibleDC(NULL);
+   if (NULL == hDestDc)
+   {
+      DPRINT1("Failed to create dest DC\n");
+      goto cleanup;
+   }
+
+   OldSourceBitmap = SelectObject(hSourceDc, StdInfo.hbmColor);
+   if (NULL == OldSourceBitmap)
+   {
+      DPRINT1("Failed to select source color bitmap\n");
+      goto cleanup;
+   }
+   SmallInfo.hbmColor = CreateCompatibleBitmap(hInfoDc, SmallIconWidth,
+                                              SmallIconHeight);
+   if (NULL == SmallInfo.hbmColor)
+   {
+      DPRINT1("Failed to create color bitmap\n");
+      goto cleanup;
+   }
+   OldDestBitmap = SelectObject(hDestDc, SmallInfo.hbmColor);
+   if (NULL == OldDestBitmap)
+   {
+      DPRINT1("Failed to select dest color bitmap\n");
+      goto cleanup;
+   }
+   if (! StretchBlt(hDestDc, 0, 0, SmallIconWidth, SmallIconHeight,
+                    hSourceDc, 0, 0, StdBitmapInfo.bmWidth,
+                    StdBitmapInfo.bmHeight, SRCCOPY))
+   {
+     DPRINT1("Failed to stretch color bitmap\n");
+     goto cleanup;
+   }
+
+   if (NULL == SelectObject(hSourceDc, StdInfo.hbmMask))
+   {
+      DPRINT1("Failed to select source mask bitmap\n");
+      goto cleanup;
+   }
+   SmallInfo.hbmMask = CreateBitmap(SmallIconWidth, SmallIconHeight, 1, 1,
+                                    NULL);
+   if (NULL == SmallInfo.hbmMask)
+   {
+      DPRINT1("Failed to create mask bitmap\n");
+      goto cleanup;
+   }
+   if (NULL == SelectObject(hDestDc, SmallInfo.hbmMask))
+   {
+      DPRINT1("Failed to select dest mask bitmap\n");
+      goto cleanup;
+   }
+   if (! StretchBlt(hDestDc, 0, 0, SmallIconWidth, SmallIconHeight,
+                    hSourceDc, 0, 0, StdBitmapInfo.bmWidth,
+                    StdBitmapInfo.bmHeight, SRCCOPY))
+   {
+      DPRINT1("Failed to stretch mask bitmap\n");
+      goto cleanup;
+   }
+
+   SmallInfo.fIcon = TRUE;
+   SmallInfo.xHotspot = SmallIconWidth / 2;
+   SmallInfo.yHotspot = SmallIconHeight / 2;
+   SmallIcon = CreateIconIndirect(&SmallInfo);
+   if (NULL == SmallIcon)
+   {
+      DPRINT1("Failed to create icon\n");
+      goto cleanup;
+   }
+
+cleanup:
+   if (NULL != SmallInfo.hbmMask)
+   {
+      DeleteObject(SmallInfo.hbmMask);
+   }
+   if (NULL != OldDestBitmap)
+   {
+      SelectObject(hDestDc, OldDestBitmap);
+   }
+   if (NULL != SmallInfo.hbmColor)
+   {
+      DeleteObject(SmallInfo.hbmColor);
+   }
+   if (NULL != hDestDc)
+   {
+      DeleteDC(hDestDc);
+   }
+   if (NULL != OldSourceBitmap)
+   {
+      SelectObject(hSourceDc, OldSourceBitmap);
+   }
+   if (NULL != hSourceDc)
+   {
+      DeleteDC(hSourceDc);
+   }
+   if (NULL != hInfoDc)
+   {
+      DeleteDC(hInfoDc);
+   }
+
+   return SmallIcon;
+}
+
 /*
  * @implemented
  */
@@ -401,8 +574,9 @@ RegisterClassExA(CONST WNDCLASSEXA *lpwcx)
    WNDCLASSEXA WndClass;
    UNICODE_STRING ClassName;
    UNICODE_STRING MenuName;
+   HMENU hMenu;
 
-   if (lpwcx == NULL || lpwcx->cbSize != sizeof(WNDCLASSEXW) ||
+   if (lpwcx == NULL || lpwcx->cbSize != sizeof(WNDCLASSEXA) ||
        lpwcx->cbClsExtra < 0 || lpwcx->cbWndExtra < 0 ||
        lpwcx->lpszClassName == NULL)
    {
@@ -427,40 +601,50 @@ RegisterClassExA(CONST WNDCLASSEXA *lpwcx)
    /* Yes, this is correct. We should modify the passed structure. */
    if (lpwcx->hInstance == NULL)
       ((WNDCLASSEXA*)lpwcx)->hInstance = GetModuleHandleW(NULL);
-  
-   RtlCopyMemory(&WndClass, lpwcx, sizeof(WNDCLASSEXW));
 
-   if (IS_ATOM(lpwcx->lpszMenuName))
+   RtlCopyMemory(&WndClass, lpwcx, sizeof(WNDCLASSEXA));
+
+   if (NULL == WndClass.hIconSm)
+   {
+      WndClass.hIconSm = CreateSmallIcon(WndClass.hIcon);
+   }
+
+   if HIWORD(lpwcx->lpszMenuName)
+   {
+      hMenu = 0;
+      RtlCreateUnicodeStringFromAsciiz(&MenuName, WndClass.lpszMenuName);
+    }
+   else
    {
       MenuName.Length =
       MenuName.MaximumLength = 0;
-      MenuName.Buffer = (LPWSTR)lpwcx->lpszMenuName;
-   } else
-   {
-      RtlCreateUnicodeStringFromAsciiz(&MenuName, lpwcx->lpszMenuName);
+      MenuName.Buffer = (LPWSTR)WndClass.lpszMenuName;
+      hMenu = LoadMenuA(WndClass.hInstance, lpwcx->lpszMenuName);
    }
-
-   if (IS_ATOM(lpwcx->lpszClassName))
+   if (IS_ATOM(WndClass.lpszClassName))
    {
       ClassName.Length =
       ClassName.MaximumLength = 0;
-      ClassName.Buffer = (LPWSTR)lpwcx->lpszClassName;
+      ClassName.Buffer = (LPWSTR)WndClass.lpszClassName;
    } else
    {
-      RtlCreateUnicodeStringFromAsciiz(&ClassName, lpwcx->lpszClassName);
+      RtlCreateUnicodeStringFromAsciiz(&ClassName, WndClass.lpszClassName);
    }
 
-   Atom = NtUserRegisterClassEx(
+   Atom = NtUserRegisterClassExWOW(
       (WNDCLASSEXW*)&WndClass,
       &ClassName,
+      &ClassName,
       &MenuName,
       NULL,
       REGISTERCLASS_ANSI,
-      NULL);
+      0,
+      hMenu);
 
-   if (!IS_ATOM(lpwcx->lpszMenuName))
+   if (!IS_ATOM(WndClass.lpszMenuName))
       RtlFreeUnicodeString(&MenuName);
-   if (!IS_ATOM(lpwcx->lpszClassName))
+   if (!IS_ATOM(WndClass.lpszClassName))
       RtlFreeUnicodeString(&ClassName);
 
    return (ATOM)Atom;
@@ -475,6 +659,7 @@ RegisterClassExW(CONST WNDCLASSEXW *lpwcx)
    WNDCLASSEXW WndClass;
    UNICODE_STRING ClassName;
    UNICODE_STRING MenuName;
+   HMENU hMenu;
 
    if (lpwcx == NULL || lpwcx->cbSize != sizeof(WNDCLASSEXW) ||
        lpwcx->cbClsExtra < 0 || lpwcx->cbWndExtra < 0 ||
@@ -504,33 +689,43 @@ RegisterClassExW(CONST WNDCLASSEXW *lpwcx)
 
    RtlCopyMemory(&WndClass, lpwcx, sizeof(WNDCLASSEXW));
 
-   if (IS_ATOM(lpwcx->lpszMenuName))
+   if (NULL == WndClass.hIconSm)
+   {
+      WndClass.hIconSm = CreateSmallIcon(WndClass.hIcon);
+   }
+
+   if HIWORD(lpwcx->lpszMenuName)
+   {
+      hMenu = 0;
+      RtlInitUnicodeString(&MenuName, WndClass.lpszMenuName);
+    }
+   else
    {
       MenuName.Length =
       MenuName.MaximumLength = 0;
-      MenuName.Buffer = (LPWSTR)lpwcx->lpszMenuName;
-   } else
-   {
-      RtlInitUnicodeString(&MenuName, lpwcx->lpszMenuName);
+      MenuName.Buffer = (LPWSTR)WndClass.lpszMenuName;
+      hMenu = LoadMenuW(WndClass.hInstance, lpwcx->lpszMenuName);
    }
 
-   if (IS_ATOM(lpwcx->lpszClassName))
+   if (IS_ATOM(WndClass.lpszClassName))
    {
       ClassName.Length =
       ClassName.MaximumLength = 0;
-      ClassName.Buffer = (LPWSTR)lpwcx->lpszClassName;
+      ClassName.Buffer = (LPWSTR)WndClass.lpszClassName;
    } else
    {
-      RtlInitUnicodeString(&ClassName, lpwcx->lpszClassName);
+      RtlInitUnicodeString(&ClassName, WndClass.lpszClassName);
    }
 
-   return (ATOM)NtUserRegisterClassEx(
+   return (ATOM)NtUserRegisterClassExWOW(
       &WndClass,
       &ClassName,
+      &ClassName,
       &MenuName,
       NULL,
       0,
-      NULL);
+      0,
+      hMenu);
 }
 
 /*
@@ -717,7 +912,7 @@ UnregisterClassA(
   LPWSTR ClassName;
   NTSTATUS Status;
   BOOL Result;
-  
+
   if(!IS_ATOM(lpClassName))
   {
     Status = HEAP_strdupAtoW(&ClassName, lpClassName, NULL);
@@ -729,12 +924,12 @@ UnregisterClassA(
   }
   else
     ClassName = (LPWSTR)lpClassName;
-  
+
   Result = (BOOL)NtUserUnregisterClass((LPCWSTR)ClassName, hInstance, 0);
-  
-  if(ClassName && !IS_ATOM(lpClassName)) 
+
+  if(ClassName && !IS_ATOM(lpClassName))
     HEAP_free(ClassName);
-  
+
   return Result;
 }