[User32]
[reactos.git] / reactos / win32ss / user / user32 / windows / message.c
index 2d1c7ee..f3a7291 100644 (file)
@@ -236,6 +236,119 @@ DdeGetPair(HGLOBAL ServerMem)
   return Ret;
 }
 
+DWORD FASTCALL get_input_codepage( void )
+{
+    DWORD cp;
+    int ret;
+    HKL hkl = GetKeyboardLayout( 0 );
+    ret = GetLocaleInfoW( LOWORD(hkl), LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER, (WCHAR *)&cp, sizeof(cp) / sizeof(WCHAR) );
+    if (!ret) cp = CP_ACP;
+    return cp;
+}
+                                                  
+static WPARAM FASTCALL map_wparam_char_WtoA( WPARAM wParam, DWORD len )
+{
+    WCHAR wch = wParam;
+    BYTE ch[2];
+    DWORD cp = get_input_codepage();
+
+    len = WideCharToMultiByte( cp, 0, &wch, 1, (LPSTR)ch, len, NULL, NULL );
+    if (len == 2)      
+       return MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wParam) );
+    else
+    return MAKEWPARAM( ch[0], HIWORD(wParam) );
+}
+
+/***********************************************************************
+ *             map_wparam_AtoW
+ *
+ * Convert the wparam of an ASCII message to Unicode.
+ */
+static WPARAM FASTCALL
+map_wparam_AtoW( UINT message, WPARAM wparam )
+{
+    char ch[2];
+    WCHAR wch[2];
+
+    wch[0] = wch[1] = 0;
+    switch(message)
+    {
+    case WM_CHAR:
+        /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
+         * messages, in which case the first char is stored, and the conversion
+         * to Unicode only takes place once the second char is sent/posted.
+         */
+#if 0
+        if (mapping != WMCHAR_MAP_NOMAPPING) // NlsMbCodePageTag
+        {
+            PCLIENTINFO pci = GetWin32ClientInfo();
+
+            struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
+
+            BYTE low = LOBYTE(wparam);
+
+            if (HIBYTE(wparam))
+            {
+                ch[0] = low;
+                ch[1] = HIBYTE(wparam);
+                RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
+                TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
+                if (data) data->lead_byte[mapping] = 0;
+            }
+            else if (data && data->lead_byte[mapping])
+            {
+                ch[0] = data->lead_byte[mapping];
+                ch[1] = low;
+                RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
+                TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
+                data->lead_byte[mapping] = 0;
+            }
+            else if (!IsDBCSLeadByte( low ))
+            {
+                ch[0] = low;
+                RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 1 );
+                TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
+                if (data) data->lead_byte[mapping] = 0;
+            }
+            else  /* store it and wait for trail byte */
+            {
+                if (!data)
+                {
+                    if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
+                        return FALSE;
+                    get_user_thread_info()->wmchar_data = data;
+                }
+                TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
+                data->lead_byte[mapping] = low;
+                return FALSE;
+            }
+            wparam = MAKEWPARAM(wch[0], wch[1]);
+            break;
+        }
+#endif
+        /* else fall through */
+    case WM_CHARTOITEM:
+    case EM_SETPASSWORDCHAR:
+    case WM_DEADCHAR:
+    case WM_SYSCHAR:
+    case WM_SYSDEADCHAR:
+    case WM_MENUCHAR:
+        ch[0] = LOBYTE(wparam);
+        ch[1] = HIBYTE(wparam);
+        RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
+        wparam = MAKEWPARAM(wch[0], wch[1]);
+        break;
+    case WM_IME_CHAR:
+        ch[0] = HIBYTE(wparam);
+        ch[1] = LOBYTE(wparam);
+        if (ch[0]) RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch, 2 );
+        else RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch + 1, 1 );
+        wparam = MAKEWPARAM(wch[0], HIWORD(wparam));
+        break;
+    }
+    return wparam;
+}
+
 static
 BOOL FASTCALL
 MsgiUMToKMMessage(PMSG UMMsg, PMSG KMMsg, BOOL Posted)
@@ -452,6 +565,9 @@ MsgiKMToUMReply(PMSG KMMsg, PMSG UMMsg, LRESULT *Result)
   return TRUE;
 }
 
+//
+//  Ansi to Unicode -> callout
+//
 static BOOL FASTCALL
 MsgiAnsiToUnicodeMessage(HWND hwnd, LPMSG UnicodeMsg, LPMSG AnsiMsg)
 {
@@ -464,16 +580,47 @@ MsgiAnsiToUnicodeMessage(HWND hwnd, LPMSG UnicodeMsg, LPMSG AnsiMsg)
     case WM_GETTEXT:
     case WM_ASKCBFORMATNAME:
       {
-        LPWSTR Buffer = HeapAlloc(GetProcessHeap(), 0,
-           AnsiMsg->wParam * sizeof(WCHAR));
-        if (!Buffer)
-          {
-            return FALSE;
-          }
+        LPWSTR Buffer;
+        if (!AnsiMsg->lParam) break;
+        Buffer = RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, AnsiMsg->wParam * sizeof(WCHAR));
+        //ERR("WM_GETTEXT A2U Size %d\n",AnsiMsg->wParam);
+        if (!Buffer) return FALSE;
         UnicodeMsg->lParam = (LPARAM)Buffer;
         break;
       }
 
+    case LB_GETTEXT:
+      {
+        DWORD Size = 1024 * sizeof(WCHAR);
+        if (!AnsiMsg->lParam || !listbox_has_strings( AnsiMsg->hwnd )) break;
+        /*Size = SendMessageW( AnsiMsg->hwnd, LB_GETTEXTLEN, AnsiMsg->wParam, 0 );
+        if (Size == LB_ERR)
+        {
+           ERR("LB_GETTEXT LB_ERR\n");
+           Size = sizeof(ULONG_PTR);
+        }
+        Size = (Size + 1) * sizeof(WCHAR);*/
+        UnicodeMsg->lParam = (LPARAM) RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, Size);
+        if (!UnicodeMsg->lParam) return FALSE;
+        break;
+      }
+
+    case CB_GETLBTEXT:
+      {
+        DWORD Size = 1024 * sizeof(WCHAR);
+        if (!AnsiMsg->lParam || !combobox_has_strings( AnsiMsg->hwnd )) break;
+        /*Size = SendMessageW( AnsiMsg->hwnd, CB_GETLBTEXTLEN, AnsiMsg->wParam, 0 );
+        if (Size == LB_ERR)
+        {
+           ERR("CB_GETTEXT LB_ERR\n");
+           Size = sizeof(ULONG_PTR);
+        }
+        Size = (Size + 1) * sizeof(WCHAR);*/
+        UnicodeMsg->lParam = (LPARAM) RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, Size);
+        if (!UnicodeMsg->lParam) return FALSE;
+        break;
+      }
+
     /* AnsiMsg->lParam is string (0-terminated) */
     case WM_SETTEXT:
     case WM_WININICHANGE:
@@ -483,6 +630,7 @@ MsgiAnsiToUnicodeMessage(HWND hwnd, LPMSG UnicodeMsg, LPMSG AnsiMsg)
     case LB_ADDFILE:
     case EM_REPLACESEL:
       {
+        if (!AnsiMsg->lParam) break;
         RtlCreateUnicodeStringFromAsciiz(&UnicodeString, (LPSTR)AnsiMsg->lParam);
         UnicodeMsg->lParam = (LPARAM)UnicodeString.Buffer;
         break;
@@ -498,7 +646,7 @@ MsgiAnsiToUnicodeMessage(HWND hwnd, LPMSG UnicodeMsg, LPMSG AnsiMsg)
     case LB_FINDSTRINGEXACT:
     case LB_SELECTSTRING:
       {
-        if (listbox_has_strings(AnsiMsg->hwnd))
+        if (AnsiMsg->lParam && listbox_has_strings(AnsiMsg->hwnd))
           {
             RtlCreateUnicodeStringFromAsciiz(&UnicodeString, (LPSTR)AnsiMsg->lParam);
             UnicodeMsg->lParam = (LPARAM)UnicodeString.Buffer;
@@ -512,7 +660,7 @@ MsgiAnsiToUnicodeMessage(HWND hwnd, LPMSG UnicodeMsg, LPMSG AnsiMsg)
     case CB_FINDSTRINGEXACT:
     case CB_SELECTSTRING:
       {
-        if (combobox_has_strings(AnsiMsg->hwnd))
+        if (AnsiMsg->lParam && combobox_has_strings(AnsiMsg->hwnd))
           {
             RtlCreateUnicodeStringFromAsciiz(&UnicodeString, (LPSTR)AnsiMsg->lParam);
             UnicodeMsg->lParam = (LPARAM)UnicodeString.Buffer;
@@ -584,6 +732,29 @@ MsgiAnsiToUnicodeMessage(HWND hwnd, LPMSG UnicodeMsg, LPMSG AnsiMsg)
         UnicodeMsg->lParam = (LPARAM)cs;
         break;
       }
+
+    case WM_GETDLGCODE:
+      if (UnicodeMsg->lParam)
+      {
+         MSG newmsg = *(MSG *)UnicodeMsg->lParam;
+         newmsg.wParam = map_wparam_AtoW( newmsg.message, newmsg.wParam);
+      }
+      break;
+
+    case WM_CHARTOITEM:
+    case WM_MENUCHAR:
+    case WM_CHAR:   
+    case WM_DEADCHAR:
+    case WM_SYSCHAR: 
+    case WM_SYSDEADCHAR:
+    case EM_SETPASSWORDCHAR:
+    case WM_IME_CHAR:
+      UnicodeMsg->wParam = map_wparam_AtoW( AnsiMsg->message, AnsiMsg->wParam );
+      break;
+    case EM_GETLINE:
+      ERR("FIXME EM_GETLINE A2U\n");
+      break;
+
     }
 
   return TRUE;
@@ -596,10 +767,15 @@ MsgiAnsiToUnicodeCleanup(LPMSG UnicodeMsg, LPMSG AnsiMsg)
 
   switch (AnsiMsg->message)
     {
+    case LB_GETTEXT:
+        if (!listbox_has_strings( UnicodeMsg->hwnd )) break;
+    case CB_GETLBTEXT:
+        if (UnicodeMsg->message == CB_GETLBTEXT && !combobox_has_strings( UnicodeMsg->hwnd )) break;
     case WM_GETTEXT:
     case WM_ASKCBFORMATNAME:
       {
-        HeapFree(GetProcessHeap(), 0, (PVOID) UnicodeMsg->lParam);
+        if (!UnicodeMsg->lParam) break;
+        RtlFreeHeap(GetProcessHeap(), 0, (PVOID) UnicodeMsg->lParam);
         break;
       }
 
@@ -611,6 +787,7 @@ MsgiAnsiToUnicodeCleanup(LPMSG UnicodeMsg, LPMSG AnsiMsg)
     case LB_ADDFILE:
     case EM_REPLACESEL:
       {
+        if (!UnicodeMsg->lParam) break;
         RtlInitUnicodeString(&UnicodeString, (PCWSTR)UnicodeMsg->lParam);
         RtlFreeUnicodeString(&UnicodeString);
         break;
@@ -626,7 +803,7 @@ MsgiAnsiToUnicodeCleanup(LPMSG UnicodeMsg, LPMSG AnsiMsg)
     case LB_FINDSTRINGEXACT:
     case LB_SELECTSTRING:
       {
-        if (listbox_has_strings(AnsiMsg->hwnd))
+        if (UnicodeMsg->lParam && listbox_has_strings(AnsiMsg->hwnd))
           {
             RtlInitUnicodeString(&UnicodeString, (PCWSTR)UnicodeMsg->lParam);
             RtlFreeUnicodeString(&UnicodeString);
@@ -640,7 +817,7 @@ MsgiAnsiToUnicodeCleanup(LPMSG UnicodeMsg, LPMSG AnsiMsg)
     case CB_FINDSTRINGEXACT:
     case CB_SELECTSTRING:
       {
-        if (combobox_has_strings(AnsiMsg->hwnd))
+        if (UnicodeMsg->lParam && combobox_has_strings(AnsiMsg->hwnd))
           {
             RtlInitUnicodeString(&UnicodeString, (PCWSTR)UnicodeMsg->lParam);
             RtlFreeUnicodeString(&UnicodeString);
@@ -691,52 +868,50 @@ MsgiAnsiToUnicodeCleanup(LPMSG UnicodeMsg, LPMSG AnsiMsg)
   return(TRUE);
 }
 
+/*
+ *    callout return -> Unicode Result to Ansi Result
+ */
 static BOOL FASTCALL
 MsgiAnsiToUnicodeReply(LPMSG UnicodeMsg, LPMSG AnsiMsg, LRESULT *Result)
 {
-  LRESULT Size;
+  LPWSTR Buffer = (LPWSTR)UnicodeMsg->lParam;
+  LPSTR AnsiBuffer = (LPSTR)AnsiMsg->lParam;
+
   switch (AnsiMsg->message)
     {
     case WM_GETTEXT:
     case WM_ASKCBFORMATNAME:
       {
-        LPWSTR Buffer = (LPWSTR)UnicodeMsg->lParam;
-        LPSTR AnsiBuffer = (LPSTR)AnsiMsg->lParam;
-        if (UnicodeMsg->wParam > 0 &&
-            !WideCharToMultiByte(CP_ACP, 0, Buffer, -1, AnsiBuffer, UnicodeMsg->wParam, NULL, NULL))
+        if (UnicodeMsg->wParam)
         {
-            AnsiBuffer[UnicodeMsg->wParam - 1] = 0;
+           DWORD len = 0;
+           if (*Result) RtlUnicodeToMultiByteN( AnsiBuffer, UnicodeMsg->wParam - 1, &len, Buffer, strlenW(Buffer) * sizeof(WCHAR));
+           AnsiBuffer[len] = 0;
+           *Result = len;
+           //ERR("WM_GETTEXT U2A Result %d Size %d\n",*Result,AnsiMsg->wParam);
         }
         break;
       }
     case LB_GETTEXT:
       {
-        LPWSTR Buffer = (LPWSTR) UnicodeMsg->lParam;
-        LPSTR AnsiBuffer = (LPSTR) AnsiMsg->lParam;
-        if (!listbox_has_strings( UnicodeMsg->hwnd )) break;
-        Size = SendMessageW( UnicodeMsg->hwnd, LB_GETTEXTLEN, UnicodeMsg->wParam, 0 );
-        if (Size == LB_ERR) break;
-        Size = Size + 1;
-        if (Size > 1 &&
-            !WideCharToMultiByte(CP_ACP, 0, Buffer, -1, AnsiBuffer, Size, NULL, NULL))
+        if (!AnsiBuffer || !listbox_has_strings( UnicodeMsg->hwnd )) break;
+        if (*Result >= 0)
         {
-            AnsiBuffer[Size - 1] = 0;
-        }        
+           DWORD len;
+           RtlUnicodeToMultiByteN( AnsiBuffer, ~0u, &len, Buffer, (strlenW(Buffer) + 1) * sizeof(WCHAR) );
+           *Result = len - 1;
+        }
         break;
       }
     case CB_GETLBTEXT:
       {
-        LPWSTR Buffer = (LPWSTR) UnicodeMsg->lParam;
-        LPSTR AnsiBuffer = (LPSTR) AnsiMsg->lParam;
-        if (!combobox_has_strings( UnicodeMsg->hwnd )) break;
-        Size = SendMessageW( UnicodeMsg->hwnd, CB_GETLBTEXTLEN, UnicodeMsg->wParam, 0 );
-        if (Size == CB_ERR) break;
-        Size = Size + 1;
-        if (Size > 1 &&
-            !WideCharToMultiByte(CP_ACP, 0, Buffer, -1, AnsiBuffer, Size, NULL, NULL))
+        if (!AnsiBuffer || !combobox_has_strings( UnicodeMsg->hwnd )) break;
+        if (*Result >= 0)
         {
-            AnsiBuffer[Size - 1] = 0;
-        }        
+           DWORD len;
+           RtlUnicodeToMultiByteN( AnsiBuffer, ~0u, &len, Buffer, (strlenW(Buffer) + 1) * sizeof(WCHAR) );
+           *Result = len - 1;
+        }
         break;
       }
     }
@@ -746,6 +921,9 @@ MsgiAnsiToUnicodeReply(LPMSG UnicodeMsg, LPMSG AnsiMsg, LRESULT *Result)
   return TRUE;
 }
 
+//
+//  Unicode to Ansi callout ->
+//
 static BOOL FASTCALL
 MsgiUnicodeToAnsiMessage(HWND hwnd, LPMSG AnsiMsg, LPMSG UnicodeMsg)
 {
@@ -759,19 +937,22 @@ MsgiUnicodeToAnsiMessage(HWND hwnd, LPMSG AnsiMsg, LPMSG UnicodeMsg)
       case WM_CREATE:
       case WM_NCCREATE:
         {
-          MDICREATESTRUCTA mdi_cs;
+          MDICREATESTRUCTA *pmdi_cs;
           CREATESTRUCTA* CsA;
           CREATESTRUCTW* CsW;
           NTSTATUS Status;
 
           CsW = (CREATESTRUCTW*)(UnicodeMsg->lParam);
-          CsA = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(CREATESTRUCTA));
+          CsA = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(CREATESTRUCTA) + sizeof(MDICREATESTRUCTA));
           if (NULL == CsA)
             {
               return FALSE;
             }
           memcpy(CsA, CsW, sizeof(CREATESTRUCTW));
 
+          /* pmdi_cs starts right after CsA */
+          pmdi_cs = (MDICREATESTRUCTA*)(CsA + 1);
+
           RtlInitUnicodeString(&UnicodeString, CsW->lpszName);
           Status = RtlUnicodeStringToAnsiString(&AnsiString, &UnicodeString, TRUE);
           if (! NT_SUCCESS(Status))
@@ -796,31 +977,67 @@ MsgiUnicodeToAnsiMessage(HWND hwnd, LPMSG AnsiMsg, LPMSG UnicodeMsg)
 
           if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
           {
-             mdi_cs = *(MDICREATESTRUCTA *)CsW->lpCreateParams;
-             mdi_cs.szTitle = CsA->lpszName; 
-             mdi_cs.szClass = CsA->lpszClass;
-             CsA->lpCreateParams = &mdi_cs;
+             *pmdi_cs = *(MDICREATESTRUCTA *)CsW->lpCreateParams;
+             pmdi_cs->szTitle = CsA->lpszName;
+             pmdi_cs->szClass = CsA->lpszClass;
+             CsA->lpCreateParams = pmdi_cs;
           }
 
           AnsiMsg->lParam = (LPARAM)CsA;
           break;
         }
       case WM_GETTEXT:
+      case WM_ASKCBFORMATNAME:
         {
+          if (!UnicodeMsg->lParam) break;
           /* Ansi string might contain MBCS chars so we need 2 * the number of chars */
-          AnsiMsg->wParam = UnicodeMsg->wParam * 2;
-          AnsiMsg->lParam = (LPARAM) RtlAllocateHeap(GetProcessHeap(), 0, AnsiMsg->wParam);
-          if (NULL == (PVOID) AnsiMsg->lParam)
-            {
-              return FALSE;
-            }
+          AnsiMsg->lParam = (LPARAM) RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, UnicodeMsg->wParam * 2);
+          //ERR("WM_GETTEXT U2A Size %d\n",AnsiMsg->wParam);
+          if (!AnsiMsg->lParam) return FALSE;
           break;
         }
+
+    case LB_GETTEXT:
+      {
+        DWORD Size = 1024;
+        if (!UnicodeMsg->lParam || !listbox_has_strings( UnicodeMsg->hwnd )) break;
+        /*Size = SendMessageA( UnicodeMsg->hwnd, LB_GETTEXTLEN, UnicodeMsg->wParam, 0 );
+        if (Size == LB_ERR)
+        {
+           ERR("LB_GETTEXT LB_ERR\n");
+           Size = sizeof(ULONG_PTR);
+        }
+        Size = (Size + 1) * sizeof(WCHAR);*/
+        AnsiMsg->lParam = (LPARAM) RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, Size);
+        if (!AnsiMsg->lParam) return FALSE;
+        break;
+      }
+
+    case CB_GETLBTEXT:
+      {
+        DWORD Size = 1024;
+        if (!UnicodeMsg->lParam || !combobox_has_strings( UnicodeMsg->hwnd )) break;
+        /*Size = SendMessageA( UnicodeMsg->hwnd, CB_GETLBTEXTLEN, UnicodeMsg->wParam, 0 );
+        if (Size == LB_ERR)
+        {
+           ERR("CB_GETTEXT LB_ERR\n");
+           Size = sizeof(ULONG_PTR);
+        }
+        Size = (Size + 1) * sizeof(WCHAR);*/
+        AnsiMsg->lParam = (LPARAM) RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, Size);
+        if (!AnsiMsg->lParam) return FALSE;
+        break;
+      }
+
       case WM_SETTEXT:
+      case WM_WININICHANGE:
+      case WM_DEVMODECHANGE:
       case CB_DIR:
       case LB_DIR:
       case LB_ADDFILE:
+      case EM_REPLACESEL:
         {
+          if (!UnicodeMsg->lParam) break;
           RtlInitUnicodeString(&UnicodeString, (PWSTR) UnicodeMsg->lParam);
           if (! NT_SUCCESS(RtlUnicodeStringToAnsiString(&AnsiString,
                                                         &UnicodeString,
@@ -842,7 +1059,7 @@ MsgiUnicodeToAnsiMessage(HWND hwnd, LPMSG AnsiMsg, LPMSG UnicodeMsg)
       case LB_FINDSTRINGEXACT:
       case LB_SELECTSTRING:
         {
-          if (listbox_has_strings(AnsiMsg->hwnd))
+          if (UnicodeMsg->lParam && listbox_has_strings(AnsiMsg->hwnd))
             {
               RtlInitUnicodeString(&UnicodeString, (PWSTR) UnicodeMsg->lParam);
               if (! NT_SUCCESS(RtlUnicodeStringToAnsiString(&AnsiString,
@@ -862,7 +1079,7 @@ MsgiUnicodeToAnsiMessage(HWND hwnd, LPMSG AnsiMsg, LPMSG UnicodeMsg)
       case CB_FINDSTRINGEXACT:
       case CB_SELECTSTRING:
         {
-          if (combobox_has_strings(AnsiMsg->hwnd))
+          if (UnicodeMsg->lParam && combobox_has_strings(AnsiMsg->hwnd))
             {
               RtlInitUnicodeString(&UnicodeString, (PWSTR) UnicodeMsg->lParam);
               if (! NT_SUCCESS(RtlUnicodeStringToAnsiString(&AnsiString,
@@ -920,8 +1137,53 @@ MsgiUnicodeToAnsiMessage(HWND hwnd, LPMSG AnsiMsg, LPMSG UnicodeMsg)
           AnsiMsg->lParam = (LPARAM)cs;
           break;
         }
-    }
 
+      case WM_GETDLGCODE:
+        if (UnicodeMsg->lParam)   
+        {
+           MSG newmsg = *(MSG *)UnicodeMsg->lParam;
+           switch(newmsg.message)
+           {
+              case WM_CHAR:
+              case WM_DEADCHAR:
+              case WM_SYSCHAR: 
+              case WM_SYSDEADCHAR:
+                newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 1 );
+                break;
+              case WM_IME_CHAR:
+                newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 2 );
+                break;
+           }
+        }
+        break;
+
+      case WM_CHAR:
+        {
+           WCHAR wch = UnicodeMsg->wParam;
+           char ch[2];
+           DWORD cp = get_input_codepage();
+           DWORD len = WideCharToMultiByte( cp, 0, &wch, 1, ch, 2, NULL, NULL );
+           AnsiMsg->wParam = (BYTE)ch[0];
+           if (len == 2) AnsiMsg->wParam = (BYTE)ch[1];
+        }
+        break;
+
+      case WM_CHARTOITEM:
+      case WM_MENUCHAR:  
+      case WM_DEADCHAR:  
+      case WM_SYSCHAR:   
+      case WM_SYSDEADCHAR:
+      case EM_SETPASSWORDCHAR:
+          AnsiMsg->wParam = map_wparam_char_WtoA(UnicodeMsg->wParam,1);
+          break;
+
+      case WM_IME_CHAR:
+          AnsiMsg->wParam = map_wparam_char_WtoA(UnicodeMsg->wParam,2);
+          break;
+      case EM_GETLINE:
+          ERR("FIXME EM_GETLINE U2A\n");
+          break;
+    }
   return TRUE;
 }
 
@@ -932,17 +1194,17 @@ MsgiUnicodeToAnsiCleanup(LPMSG AnsiMsg, LPMSG UnicodeMsg)
 
   switch(UnicodeMsg->message)
     {
+      case LB_GETTEXT:
+        if (!listbox_has_strings( AnsiMsg->hwnd )) break;
+      case CB_GETLBTEXT:
+        if (AnsiMsg->message == CB_GETLBTEXT && !combobox_has_strings( AnsiMsg->hwnd )) break;
       case WM_GETTEXT:
+      case WM_ASKCBFORMATNAME:
         {
+          if (!AnsiMsg->lParam) break;
           RtlFreeHeap(GetProcessHeap(), 0, (PVOID) AnsiMsg->lParam);
           break;
         }
-      case WM_SETTEXT:
-        {
-          RtlInitAnsiString(&AnsiString, (PSTR) AnsiMsg->lParam);
-          RtlFreeAnsiString(&AnsiString);
-          break;
-        }
       case WM_CREATE:
       case WM_NCCREATE:
         {
@@ -960,6 +1222,20 @@ MsgiUnicodeToAnsiCleanup(LPMSG AnsiMsg, LPMSG UnicodeMsg)
           break;
         }
 
+      case WM_SETTEXT:
+      case WM_WININICHANGE:
+      case WM_DEVMODECHANGE:
+      case CB_DIR:
+      case LB_DIR:
+      case LB_ADDFILE:
+      case EM_REPLACESEL:
+        {
+          if (!AnsiMsg->lParam) break;
+          RtlInitAnsiString(&AnsiString, (PSTR) AnsiMsg->lParam);
+          RtlFreeAnsiString(&AnsiString);
+          break;
+        }
+
       case LB_ADDSTRING:
       case LB_ADDSTRING_LOWER:
       case LB_ADDSTRING_UPPER:
@@ -970,7 +1246,7 @@ MsgiUnicodeToAnsiCleanup(LPMSG AnsiMsg, LPMSG UnicodeMsg)
       case LB_FINDSTRINGEXACT:
       case LB_SELECTSTRING:
         {
-          if (listbox_has_strings(AnsiMsg->hwnd))
+          if (AnsiMsg->lParam && listbox_has_strings(AnsiMsg->hwnd))
             {
               RtlInitAnsiString(&AnsiString, (PSTR) AnsiMsg->lParam);
               RtlFreeAnsiString(&AnsiString);
@@ -984,9 +1260,7 @@ MsgiUnicodeToAnsiCleanup(LPMSG AnsiMsg, LPMSG UnicodeMsg)
       case CB_FINDSTRINGEXACT:
       case CB_SELECTSTRING:
         {
-          DWORD dwStyle = GetWindowLongPtrW(AnsiMsg->hwnd, GWL_STYLE);
-          if (!(dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) &&
-               (dwStyle & CBS_HASSTRINGS))
+          if (AnsiMsg->lParam && combobox_has_strings(AnsiMsg->hwnd))
             {
               RtlInitAnsiString(&AnsiString, (PSTR) AnsiMsg->lParam);
               RtlFreeAnsiString(&AnsiString);
@@ -1013,52 +1287,53 @@ MsgiUnicodeToAnsiCleanup(LPMSG AnsiMsg, LPMSG UnicodeMsg)
   return TRUE;
 }
 
+/*
+ *    callout return -> Ansi Result to Unicode Result
+ */
 static BOOL FASTCALL
 MsgiUnicodeToAnsiReply(LPMSG AnsiMsg, LPMSG UnicodeMsg, LRESULT *Result)
 {
-  LRESULT Size;
+  LPSTR Buffer = (LPSTR) AnsiMsg->lParam;
+  LPWSTR UBuffer = (LPWSTR) UnicodeMsg->lParam;
+
   switch (UnicodeMsg->message)
     {
     case WM_GETTEXT:
     case WM_ASKCBFORMATNAME:
       {
-        LPSTR Buffer = (LPSTR) AnsiMsg->lParam;
-        LPWSTR UBuffer = (LPWSTR) UnicodeMsg->lParam;
-        if (0 < AnsiMsg->wParam &&
-            ! MultiByteToWideChar(CP_ACP, 0, Buffer, -1, UBuffer, UnicodeMsg->wParam))
-        {
-            UBuffer[UnicodeMsg->wParam - 1] = L'\0';
+        DWORD len = AnsiMsg->wParam;// * 2;
+        if (len)
+        { 
+           if (*Result)
+           {
+              RtlMultiByteToUnicodeN( UBuffer, AnsiMsg->wParam*sizeof(WCHAR), &len, Buffer, strlen(Buffer)+1 );
+              *Result = len/sizeof(WCHAR) - 1;  /* do not count terminating null */
+              //ERR("WM_GETTEXT U2A Result %d Size %d\n",*Result,AnsiMsg->wParam);
+           }
+           UBuffer[*Result] = 0;
         }
         break;
       }
     case LB_GETTEXT:
       {
-        LPSTR Buffer = (LPSTR) AnsiMsg->lParam;
-        LPWSTR UBuffer = (LPWSTR) UnicodeMsg->lParam;
-        if (!listbox_has_strings( UnicodeMsg->hwnd )) break;
-        Size = SendMessageW( UnicodeMsg->hwnd, LB_GETTEXTLEN, UnicodeMsg->wParam, 0 );
-        if (Size == LB_ERR) break;
-        Size = Size + 1;
-        if (1 < Size &&
-            ! MultiByteToWideChar(CP_ACP, 0, Buffer, -1, UBuffer, Size))
+        if (!UBuffer || !listbox_has_strings( UnicodeMsg->hwnd )) break;
+        if (*Result >= 0)
         {
-            UBuffer[Size - 1] = L'\0';
-        }        
+           DWORD len;
+           RtlMultiByteToUnicodeN( UBuffer, ~0u, &len, Buffer, strlen(Buffer) + 1 );
+           *Result = len / sizeof(WCHAR) - 1;
+        }
         break;
       }
     case CB_GETLBTEXT:
       {
-        LPSTR Buffer = (LPSTR) AnsiMsg->lParam;
-        LPWSTR UBuffer = (LPWSTR) UnicodeMsg->lParam;
-        if (!combobox_has_strings( UnicodeMsg->hwnd )) break;
-        Size = SendMessageW( UnicodeMsg->hwnd, CB_GETLBTEXTLEN, UnicodeMsg->wParam, 0 );
-        if (Size == CB_ERR) break;
-        Size = Size + 1;
-        if (1 < Size &&
-            ! MultiByteToWideChar(CP_ACP, 0, Buffer, -1, UBuffer, Size))
+        if (!UBuffer || !combobox_has_strings( UnicodeMsg->hwnd )) break;
+        if (*Result >= 0)
         {
-            UBuffer[Size - 1] = L'\0';
-        }        
+           DWORD len;
+           RtlMultiByteToUnicodeN( UBuffer, ~0u, &len, Buffer, strlen(Buffer) + 1 );
+           *Result = len / sizeof(WCHAR) - 1;
+        }
         break;
       }
     }
@@ -1068,95 +1343,6 @@ MsgiUnicodeToAnsiReply(LPMSG AnsiMsg, LPMSG UnicodeMsg, LRESULT *Result)
   return TRUE;
 }
 
-/***********************************************************************
- *             map_wparam_AtoW
- *
- * Convert the wparam of an ASCII message to Unicode.
- */
-static WPARAM
-map_wparam_AtoW( UINT message, WPARAM wparam )
-{
-    char ch[2];
-    WCHAR wch[2];
-
-    wch[0] = wch[1] = 0;
-    switch(message)
-    {
-    case WM_CHAR:
-        /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
-         * messages, in which case the first char is stored, and the conversion
-         * to Unicode only takes place once the second char is sent/posted.
-         */
-#if 0
-        if (mapping != WMCHAR_MAP_NOMAPPING) // NlsMbCodePageTag
-        {
-            PCLIENTINFO pci = GetWin32ClientInfo();
-
-            struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
-
-            BYTE low = LOBYTE(wparam);
-
-            if (HIBYTE(wparam))
-            {
-                ch[0] = low;
-                ch[1] = HIBYTE(wparam);
-                RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
-                TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
-                if (data) data->lead_byte[mapping] = 0;
-            }
-            else if (data && data->lead_byte[mapping])
-            {
-                ch[0] = data->lead_byte[mapping];
-                ch[1] = low;
-                RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
-                TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
-                data->lead_byte[mapping] = 0;
-            }
-            else if (!IsDBCSLeadByte( low ))
-            {
-                ch[0] = low;
-                RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 1 );
-                TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
-                if (data) data->lead_byte[mapping] = 0;
-            }
-            else  /* store it and wait for trail byte */
-            {
-                if (!data)
-                {
-                    if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
-                        return FALSE;
-                    get_user_thread_info()->wmchar_data = data;
-                }
-                TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
-                data->lead_byte[mapping] = low;
-                return FALSE;
-            }
-            wparam = MAKEWPARAM(wch[0], wch[1]);
-            break;
-        }
-#endif
-        /* else fall through */
-    case WM_CHARTOITEM:
-    case EM_SETPASSWORDCHAR:
-    case WM_DEADCHAR:
-    case WM_SYSCHAR:
-    case WM_SYSDEADCHAR:
-    case WM_MENUCHAR:
-        ch[0] = LOBYTE(wparam);
-        ch[1] = HIBYTE(wparam);
-        RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
-        wparam = MAKEWPARAM(wch[0], wch[1]);
-        break;
-    case WM_IME_CHAR:
-        ch[0] = HIBYTE(wparam);
-        ch[1] = LOBYTE(wparam);
-        if (ch[0]) RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch, 2 );
-        else RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch + 1, 1 );
-        wparam = MAKEWPARAM(wch[0], HIWORD(wparam));
-        break;
-    }
-    return wparam;
-}
 
 LRESULT
 WINAPI
@@ -1179,12 +1365,12 @@ DesktopWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
   }
 
   Result = DesktopWndProcW(hwnd, message, UcMsg.wParam, UcMsg.lParam);
+
   MsgiAnsiToUnicodeCleanup(&UcMsg, &AnsiMsg);
 
   return Result;
  }
+
 /*
  * @implemented
  */
@@ -1285,7 +1471,6 @@ IntCallWindowProcW(BOOL IsAnsiProc,
 {
   MSG AnsiMsg;
   MSG UnicodeMsg;
-  ULONG_PTR LowLimit;
   BOOL Hook = FALSE, MsgOverride = FALSE, Dialog;
   LRESULT Result = 0, PreResult = 0;
   DWORD Data = 0;
@@ -1296,14 +1481,6 @@ IntCallWindowProcW(BOOL IsAnsiProc,
       return FALSE;
   }
 
-  // Safeguard against excessive recursions.
-  LowLimit = (ULONG_PTR)NtCurrentTeb()->NtTib.StackLimit;
-  if (((ULONG_PTR)&lParam - LowLimit) < PAGE_SIZE )
-  {
-     ERR("IntCallWindowsProcW() Exceeded Stack!\n");
-     return FALSE;
-  }
-
   if (pWnd)
      Dialog = (pWnd->fnid == FNID_DIALOG);
   else
@@ -1317,7 +1494,7 @@ IntCallWindowProcW(BOOL IsAnsiProc,
      else
         MsgOverride = IsMsgOverride( Msg, &guah.DlgProcArray);
   }
-  
+
   if (IsAnsiProc)
   {
       UnicodeMsg.hwnd = hWnd;
@@ -1352,7 +1529,7 @@ IntCallWindowProcW(BOOL IsAnsiProc,
       }
       _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
       {
-         ERR("Got exception when calling Ansi WndProc %p Msg %d \n",WndProc,Msg);
+         ERR("Exception when calling Ansi WndProc %p Msg %d pti %p Wndpti %p\n",WndProc,Msg,GetW32ThreadInfo(),pWnd->head.pti);
       }
       _SEH2_END;
 
@@ -1401,7 +1578,7 @@ IntCallWindowProcW(BOOL IsAnsiProc,
       }
       _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
       {
-         ERR("Got exception when calling unicode WndProc %p Msg %d \n",WndProc, Msg);
+         ERR("Exception when calling unicode WndProc %p Msg %d pti %p Wndpti %p\n",WndProc, Msg,GetW32ThreadInfo(),pWnd->head.pti);
       }
       _SEH2_END;
 
@@ -1437,7 +1614,7 @@ IntCallWindowProcA(BOOL IsAnsiProc,
 {
   MSG AnsiMsg;
   MSG UnicodeMsg;
-  ULONG_PTR LowLimit;
+  //ULONG_PTR LowLimit;
   BOOL Hook = FALSE, MsgOverride = FALSE, Dialog;
   LRESULT Result = 0, PreResult = 0;
   DWORD Data = 0;
@@ -1447,14 +1624,14 @@ IntCallWindowProcA(BOOL IsAnsiProc,
       WARN("IntCallWindowsProcA() called with WndProc = NULL!\n");
       return FALSE;
   }
-
+#if 0
   LowLimit = (ULONG_PTR)NtCurrentTeb()->NtTib.StackLimit;
   if (((ULONG_PTR)&lParam - LowLimit) < PAGE_SIZE )
   {
      ERR("IntCallWindowsProcA() Exceeded Stack!\n");
      return FALSE;
   }
-
+#endif
   if (pWnd)
      Dialog = (pWnd->fnid == FNID_DIALOG);
   else
@@ -1494,7 +1671,7 @@ IntCallWindowProcA(BOOL IsAnsiProc,
       }
       _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
       {
-         ERR("Got exception when calling Ansi WndProc %p Msg %d \n",WndProc,Msg);
+         ERR("Exception when calling Ansi WndProc %p Msg %d pti %p Wndpti %p\n",WndProc,Msg,GetW32ThreadInfo(),pWnd->head.pti);
       }
       _SEH2_END;
 
@@ -1548,7 +1725,7 @@ IntCallWindowProcA(BOOL IsAnsiProc,
       }
       _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
       {
-         ERR("Got exception when calling unicode WndProc %p Msg %d \n",WndProc, Msg);
+         ERR("Exception when calling unicode WndProc %p Msg %d pti %p Wndpti %p\n",WndProc, Msg,GetW32ThreadInfo(),pWnd->head.pti);
       }
       _SEH2_END;
 
@@ -1585,8 +1762,8 @@ IntCallMessageProc(IN PWND Wnd, IN HWND hWnd, IN UINT Msg, IN WPARAM wParam, IN
     WNDPROC WndProc;
     BOOL IsAnsi;
     PCLS Class;
-    
-    Class = DesktopPtrToUser(Wnd->pcls); 
+
+    Class = DesktopPtrToUser(Wnd->pcls);
     WndProc = NULL;
 
     if ( Wnd->head.pti != GetW32ThreadInfo())
@@ -1731,7 +1908,9 @@ CallWindowProcW(WNDPROC lpPrevWndFunc,
 /*
  * @implemented
  */
-LRESULT WINAPI
+LRESULT
+WINAPI
+DECLSPEC_HOTPATCH
 DispatchMessageA(CONST MSG *lpmsg)
 {
     LRESULT Ret = 0;
@@ -1823,7 +2002,9 @@ DispatchMessageA(CONST MSG *lpmsg)
 /*
  * @implemented
  */
-LRESULT WINAPI
+LRESULT
+WINAPI
+DECLSPEC_HOTPATCH
 DispatchMessageW(CONST MSG *lpmsg)
 {
     LRESULT Ret = 0;
@@ -1923,7 +2104,9 @@ IntConvertMsgToAnsi(LPMSG lpMsg)
 /*
  * @implemented
  */
-BOOL WINAPI
+BOOL
+WINAPI
+DECLSPEC_HOTPATCH
 GetMessageA(LPMSG lpMsg,
             HWND hWnd,
             UINT wMsgFilterMin,
@@ -1951,7 +2134,9 @@ GetMessageA(LPMSG lpMsg,
 /*
  * @implemented
  */
-BOOL WINAPI
+BOOL
+WINAPI
+DECLSPEC_HOTPATCH
 GetMessageW(LPMSG lpMsg,
             HWND hWnd,
             UINT wMsgFilterMin,
@@ -2014,7 +2199,9 @@ PeekMessageWorker( PMSG pMsg,
 /*
  * @implemented
  */
-BOOL WINAPI
+BOOL
+WINAPI
+DECLSPEC_HOTPATCH
 PeekMessageA(LPMSG lpMsg,
             HWND hWnd,
             UINT wMsgFilterMin,
@@ -2040,6 +2227,7 @@ PeekMessageA(LPMSG lpMsg,
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 PeekMessageW(
   LPMSG lpMsg,
   HWND hWnd,
@@ -2084,7 +2272,7 @@ PostMessageA(
 
   /* No drop files or current Process, just post message. */
   if ( (Msg != WM_DROPFILES) ||
-       ( NtUserQueryWindow( hWnd, QUERY_WINDOW_UNIQUE_PROCESS_ID) == 
+       ( NtUserQueryWindow( hWnd, QUERY_WINDOW_UNIQUE_PROCESS_ID) ==
                   PtrToUint(NtCurrentTeb()->ClientId.UniqueProcess) ) )
   {
     return NtUserPostMessage(hWnd, Msg, wParam, lParam);
@@ -2129,7 +2317,7 @@ PostMessageW(
 
   /* No drop files or current Process, just post message. */
   if ( (Msg != WM_DROPFILES) ||
-       ( NtUserQueryWindow( hWnd, QUERY_WINDOW_UNIQUE_PROCESS_ID) == 
+       ( NtUserQueryWindow( hWnd, QUERY_WINDOW_UNIQUE_PROCESS_ID) ==
                   PtrToUint(NtCurrentTeb()->ClientId.UniqueProcess) ) )
   {
     return NtUserPostMessage(hWnd, Msg, wParam, lParam);
@@ -2216,8 +2404,7 @@ SendMessageW(HWND Wnd,
 
       if ( Window != NULL &&
            Window->head.pti == ti &&
-//          !IsThreadHooked(GetWin32ClientInfo()) && // Enable to test message system bug.
-          !ISITHOOKED(WH_CALLWNDPROC) &&
+          !ISITHOOKED(WH_CALLWNDPROC) &&        
           !ISITHOOKED(WH_CALLWNDPROCRET) &&
           !(Window->state & WNDS_SERVERSIDEWINDOWPROC) )
       {
@@ -2245,13 +2432,13 @@ SendMessageW(HWND Wnd,
   }
 
   Result = NtUserMessageCall( Wnd,
-                              KMMsg.message, 
+                              KMMsg.message,
                               KMMsg.wParam,
                               KMMsg.lParam,
                              (ULONG_PTR)&Result,
                               FNID_SENDMESSAGE,
                               FALSE);
-  
+
   MsgiUMToKMCleanup(&UMMsg, &KMMsg);
 
   return Result;
@@ -2281,8 +2468,7 @@ SendMessageA(HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam)
 
       if ( Window != NULL &&
            Window->head.pti == ti &&
-//          !IsThreadHooked(GetWin32ClientInfo()) && // Enable to test message system bug.
-          !ISITHOOKED(WH_CALLWNDPROC) &&
+          !ISITHOOKED(WH_CALLWNDPROC) &&        
           !ISITHOOKED(WH_CALLWNDPROCRET) &&
           !(Window->state & WNDS_SERVERSIDEWINDOWPROC) )
       {
@@ -2291,7 +2477,7 @@ SendMessageA(HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam)
 
                    * Window belongs to calling thread
                    * The calling thread is not being hooked for CallWndProc
-                   * Not calling a server side proc: 
+                   * Not calling a server side proc:
                      Desktop, Switch, ScrollBar, Menu, IconTitle, or hWndMessage
            */
 
@@ -2316,7 +2502,7 @@ SendMessageA(HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam)
   }
 
   Result = NtUserMessageCall( Wnd,
-                              KMMsg.message, 
+                              KMMsg.message,
                               KMMsg.wParam,
                               KMMsg.lParam,
                              (ULONG_PTR)&Result,
@@ -2403,7 +2589,7 @@ SendMessageCallbackW(
   CallBackInfo.Context = dwData;
 
   return NtUserMessageCall(hWnd,
-                            Msg, 
+                            Msg,
                          wParam,
                          lParam,
        (ULONG_PTR)&CallBackInfo,
@@ -2428,34 +2614,15 @@ SendMessageTimeoutA(
   MSG AnsiMsg, UcMsg;
   LRESULT Result;
   DOSENDMESSAGE dsm;
-  PWND Window;
-  PTHREADINFO ti = GetW32ThreadInfo();
 
   if ( Msg & ~WM_MAXIMUM || fuFlags & ~(SMTO_NOTIMEOUTIFNOTHUNG|SMTO_ABORTIFHUNG|SMTO_BLOCK))
   {
      SetLastError( ERROR_INVALID_PARAMETER );
      return 0;
   }
-  
-  if (lpdwResult) *lpdwResult = 0;
 
-  //// This is due to message system bug.
-  if (hWnd != HWND_TOPMOST && hWnd != HWND_BROADCAST && (Msg < WM_DDE_FIRST || Msg > WM_DDE_LAST))
-  {
-      Window = ValidateHwnd(hWnd);
+  if (lpdwResult) *lpdwResult = 0;
 
-      if ( Window != NULL &&
-           Window->head.pti == ti &&
-          !ISITHOOKED(WH_CALLWNDPROC) &&
-          !ISITHOOKED(WH_CALLWNDPROCRET) &&
-          !(Window->state & WNDS_SERVERSIDEWINDOWPROC) )
-      {
-          Result = IntCallMessageProc(Window, hWnd, Msg, wParam, lParam, TRUE);
-          if (lpdwResult) *lpdwResult = Result;
-          return TRUE;
-      }
-  }
-  ////
   SPY_EnterMessage(SPY_SENDMESSAGE, hWnd, Msg, wParam, lParam);
 
   dsm.uFlags = fuFlags;
@@ -2505,41 +2672,22 @@ SendMessageTimeoutW(
 {
   LRESULT Result;
   DOSENDMESSAGE dsm;
-  PWND Window;
-  PTHREADINFO ti = GetW32ThreadInfo();
 
   if ( Msg & ~WM_MAXIMUM || fuFlags & ~(SMTO_NOTIMEOUTIFNOTHUNG|SMTO_ABORTIFHUNG|SMTO_BLOCK))
   {
      SetLastError( ERROR_INVALID_PARAMETER );
      return 0;
   }
-  
-  if (lpdwResult) *lpdwResult = 0;
 
-  //// This is due to message system bug.
-  if (hWnd != HWND_TOPMOST && hWnd != HWND_BROADCAST && (Msg < WM_DDE_FIRST || Msg > WM_DDE_LAST))
-  {
-      Window = ValidateHwnd(hWnd);
+  if (lpdwResult) *lpdwResult = 0;
 
-      if ( Window != NULL &&
-           Window->head.pti == ti &&
-          !ISITHOOKED(WH_CALLWNDPROC) &&
-          !ISITHOOKED(WH_CALLWNDPROCRET) &&
-          !(Window->state & WNDS_SERVERSIDEWINDOWPROC) )
-      {
-          Result = IntCallMessageProc(Window, hWnd, Msg, wParam, lParam, FALSE);
-          if (lpdwResult) *lpdwResult = Result;
-          return TRUE;
-      }
-  }
-  ////
   SPY_EnterMessage(SPY_SENDMESSAGE, hWnd, Msg, wParam, lParam);
 
   dsm.uFlags = fuFlags;
   dsm.uTimeout = uTimeout;
 
   Result = NtUserMessageCall( hWnd,
-                              Msg, 
+                              Msg,
                               wParam,
                               lParam,
                              (ULONG_PTR)&dsm,
@@ -2658,7 +2806,7 @@ BOOL WINAPI
 TranslateMessage(CONST MSG *lpMsg)
 {
   BOOL Ret = FALSE;
-  
+
 // Ref: msdn ImmGetVirtualKey:
 // http://msdn.microsoft.com/en-us/library/aa912145.aspx
 /*
@@ -2681,11 +2829,9 @@ UINT WINAPI
 RegisterWindowMessageA(LPCSTR lpString)
 {
   UNICODE_STRING String;
-  BOOLEAN Result;
   UINT Atom;
 
-  Result = RtlCreateUnicodeStringFromAsciiz(&String, (PCSZ)lpString);
-  if (!Result)
+  if (!RtlCreateUnicodeStringFromAsciiz(&String, (PCSZ)lpString))
     {
       return(0);
     }
@@ -2752,7 +2898,7 @@ BOOL WINAPI GetInputState(VOID)
 
    if ((!pcti) || (pcti->fsChangeBits & (QS_KEY|QS_MOUSEBUTTON)))
       return (BOOL)NtUserGetThreadState(THREADSTATE_GETINPUTSTATE);
-            
+
    return FALSE;
 }
 
@@ -2763,7 +2909,6 @@ User32CallWindowProcFromKernel(PVOID Arguments, ULONG ArgumentLength)
   PWINDOWPROC_CALLBACK_ARGUMENTS CallbackArgs;
   MSG KMMsg, UMMsg;
   PWND pWnd = NULL;
-  ULONG_PTR LowLimit;
   PCLIENTINFO pci = GetWin32ClientInfo();
 
   /* Make sure we don't try to access mem beyond what we were given */
@@ -2772,13 +2917,6 @@ User32CallWindowProcFromKernel(PVOID Arguments, ULONG ArgumentLength)
       return STATUS_INFO_LENGTH_MISMATCH;
     }
 
-  LowLimit = (ULONG_PTR)NtCurrentTeb()->NtTib.StackLimit;
-  if (((ULONG_PTR)&ArgumentLength - LowLimit) < PAGE_SIZE )
-  {
-     ERR("Callback from Win32k Exceeded Stack!\n");
-     return STATUS_BAD_STACK;
-  }
-
   CallbackArgs = (PWINDOWPROC_CALLBACK_ARGUMENTS) Arguments;
   KMMsg.hwnd = CallbackArgs->Wnd;
   KMMsg.message = CallbackArgs->Msg;
@@ -2792,6 +2930,17 @@ User32CallWindowProcFromKernel(PVOID Arguments, ULONG ArgumentLength)
           return STATUS_INFO_LENGTH_MISMATCH;
         }
       KMMsg.lParam = (LPARAM) ((char *) CallbackArgs + sizeof(WINDOWPROC_CALLBACK_ARGUMENTS));
+     switch(KMMsg.message)
+     {
+        case WM_SIZING:
+        {
+           PRECT prect = (PRECT) KMMsg.lParam;
+           ERR("WM_SIZING 1 t %d l %d r %d b %d\n",prect->top,prect->left,prect->right,prect->bottom);
+           break;
+        }
+        default:
+           break;
+     }
     }
   else
     {
@@ -2813,7 +2962,7 @@ User32CallWindowProcFromKernel(PVOID Arguments, ULONG ArgumentLength)
     }
 
   if (pci->CallbackWnd.hWnd == UMMsg.hwnd)
-     pWnd  = pci->CallbackWnd.pWnd;
+     pWnd = pci->CallbackWnd.pWnd;
 
   CallbackArgs->Result = IntCallWindowProcW( CallbackArgs->IsAnsiProc,
                                              CallbackArgs->Proc,
@@ -2827,6 +2976,20 @@ User32CallWindowProcFromKernel(PVOID Arguments, ULONG ArgumentLength)
     {
     }
 
+  if (0 <= CallbackArgs->lParamBufferSize)
+  {
+     switch(KMMsg.message)
+     {
+        case WM_SIZING:
+        {
+           PRECT prect = (PRECT) KMMsg.lParam;
+           ERR("WM_SIZING 2 t %d l %d r %d b %d\n",prect->top,prect->left,prect->right,prect->bottom);
+           break;
+        }
+        default:
+           break;
+     }
+  }
   return ZwCallbackReturn(CallbackArgs, ArgumentLength, STATUS_SUCCESS);
 }
 
@@ -3103,7 +3266,7 @@ IntBroadcastSystemMessage(
                                    | BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOTIMEOUTIFNOTHUNG
                                    | BSF_ALLOWSFW | BSF_SENDNOTIFYMESSAGE | BSF_RETURNHDESK | BSF_LUID );
 
-    if ((dwflags & ~all_flags) || 
+    if ((dwflags & ~all_flags) ||
         (!pBSMInfo && (dwflags & (BSF_RETURNHDESK|BSF_LUID))) )
     {
         SetLastError(ERROR_INVALID_PARAMETER);
@@ -3117,7 +3280,7 @@ IntBroadcastSystemMessage(
     }
 
     if (dwflags & BSF_FORCEIFHUNG) dwflags |= BSF_NOHANG;
-    
+
     if (dwflags & BSF_QUERY) dwflags &= ~BSF_SENDNOTIFYMESSAGE|BSF_POSTMESSAGE;
 
     if (!lpdwRecipients)