[User32]
[reactos.git] / reactos / dll / win32 / user32 / controls / edit.c
index 686818f..b07830f 100644 (file)
@@ -116,9 +116,6 @@ typedef struct
                                           and just line width for single line controls */
        INT region_posx;                /* Position of cursor relative to region: */
        INT region_posy;                /* -1: to left, 0: within, 1: to right */
-#ifndef __REACTOS__
-       EDITWORDBREAKPROC16 word_break_proc16;
-#endif
        void *word_break_proc;          /* 32-bit word break proc: ANSI or Unicode */
        INT line_count;                 /* number of lines */
        INT y_offset;                   /* scroll offset in number of lines */
@@ -137,10 +134,6 @@ typedef struct
        LPINT tabs;
        LINEDEF *first_line_def;        /* linked list of (soft) linebreaks */
        HLOCAL hloc32W;                 /* our unicode local memory block */
-#ifndef __REACTOS__
-       HLOCAL16 hloc16;                /* alias for 16-bit control receiving EM_GETHANDLE16
-                                          or EM_SETHANDLE16 */
-#endif
        HLOCAL hloc32A;                 /* alias for ANSI control receiving EM_GETHANDLE
                                           or EM_SETHANDLE */
        /*
@@ -164,6 +157,8 @@ typedef struct
                     (LPARAM)(es->hwndSelf)); \
        } while(0)
 
+static const WCHAR empty_stringW[] = {0};
+
 /*********************************************************************
  *
  *     EM_CANUNDO
@@ -324,32 +319,7 @@ static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count
 {
        INT ret;
 
-#ifndef __REACTOS__
-       if (es->word_break_proc16) {
-           HGLOBAL16 hglob16;
-           SEGPTR segptr;
-           INT countA;
-            WORD args[5];
-            DWORD result;
-
-           countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
-           hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
-           segptr = WOWGlobalLock16(hglob16);
-           WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
-            args[4] = SELECTOROF(segptr);
-            args[3] = OFFSETOF(segptr);
-            args[2] = index;
-            args[1] = countA;
-            args[0] = action;
-            WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
-            ret = LOWORD(result);
-           GlobalUnlock16(hglob16);
-           GlobalFree16(hglob16);
-       }
-       else if (es->word_break_proc)
-#else
-  if (es->word_break_proc)
-#endif
+        if (es->word_break_proc)
         {
            if(es->is_unicode)
            {
@@ -367,8 +337,7 @@ static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count
 
                countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
                textA = HeapAlloc(GetProcessHeap(), 0, countA);
-        if (textA == NULL)
-            return 0;
+                if (textA == NULL) return 0;
                WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
                TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
                        es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
@@ -457,8 +426,8 @@ static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta
                                /* The buffer has been expanded, create a new line and
                                   insert it into the link list */
                                LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
-                if (new_line == NULL)
-                    break;
+                if (new_line == NULL) // reactos r33509
+                    break; // reactos r33509
                                new_line->next = previous_line->next;
                                previous_line->next = new_line;
                                current_line = new_line;
@@ -1113,33 +1082,15 @@ static inline void text_buffer_changed(EDITSTATE *es)
 }
 
 /*********************************************************************
- *
- *     EDIT_LockBuffer
- *
- *     This acts as a LocalLock16(), but it locks only once.  This way
- *     you can call it whenever you like, without unlocking.
- *
- *     Initially the edit control allocates a HLOCAL32 buffer 
- *     (32 bit linear memory handler).  However, 16 bit application
- *     might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
- *     handler).  From that moment on we have to keep using this 16 bit memory
- *     handler, because it is supposed to be valid at all times after EM_GETHANDLE.
- *     What we do is create a HLOCAL16 buffer, copy the text, and do pointer
- *     conversion.
+ * EDIT_LockBuffer
  *
  */
 static void EDIT_LockBuffer(EDITSTATE *es)
 {
-#ifndef __REACTOS__
-       STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
-       HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
-#endif
        if (!es->text) {
-           CHAR *textA = NULL;
+
+           CHAR *textA = NULL; // ReactOS Hacked!
            UINT countA = 0;
-#ifndef __REACTOS__
-           BOOL _16bit = FALSE;
-#endif
 
            if(es->hloc32W)
            {
@@ -1149,18 +1100,6 @@ static void EDIT_LockBuffer(EDITSTATE *es)
                    textA = LocalLock(es->hloc32A);
                    countA = strlen(textA) + 1;
                }
-#ifndef __REACTOS__
-               else if(es->hloc16)
-               {
-                   HANDLE16 oldDS = stack16->ds;
-                   TRACE("Synchronizing with 16-bit ANSI buffer\n");
-                   stack16->ds = hInstance;
-                   textA = MapSL(LocalLock16(es->hloc16));
-                   stack16->ds = oldDS;
-                   countA = strlen(textA) + 1;
-                   _16bit = TRUE;
-               }
-#endif
            }
            else {
                ERR("no buffer ... please report\n");
@@ -1170,8 +1109,7 @@ static void EDIT_LockBuffer(EDITSTATE *es)
            if(textA)
            {
                HLOCAL hloc32W_new;
-               UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
-               TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
+               UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
                if(countW_new > es->buffer_size + 1)
                {
                    UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
@@ -1186,26 +1124,11 @@ static void EDIT_LockBuffer(EDITSTATE *es)
                    else
                        WARN("FAILED! Will synchronize partially\n");
                }
+                es->text = LocalLock(es->hloc32W);
+               MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
+                LocalUnlock(es->hloc32A);
            }
-
-           /*TRACE("Locking 32-bit UNICODE buffer\n");*/
-           es->text = LocalLock(es->hloc32W);
-
-           if(textA)
-           {
-               MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
-#ifndef __REACTOS__
-               if(_16bit)
-               {
-                   HANDLE16 oldDS = stack16->ds;
-                   stack16->ds = hInstance;
-                   LocalUnlock16(es->hloc16);
-                   stack16->ds = oldDS;
-               }
-               else
-#endif
-                   LocalUnlock(es->hloc32A);
-           }
+           else es->text = LocalLock(es->hloc32W);
        }
         if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
        es->lock_count++;
@@ -1225,7 +1148,7 @@ static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
        {
            WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
            return;
-       }
+       }
 
        if (!es->lock_count) {
                ERR("lock_count == 0 ... please report\n");
@@ -1238,13 +1161,8 @@ static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
 
        if (force || (es->lock_count == 1)) {
            if (es->hloc32W) {
-               CHAR *textA = NULL;
                UINT countA = 0;
                UINT countW = get_text_length(es) + 1;
-#ifndef __REACTOS__
-               STACK16FRAME* stack16 = NULL;
-               HANDLE16 oldDS = 0;
-#endif
 
                if(es->hloc32A)
                {
@@ -1267,54 +1185,11 @@ static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
                        else
                            WARN("FAILED! Will synchronize partially\n");
                    }
-                   textA = LocalLock(es->hloc32A);
-               }
-#ifndef __REACTOS__
-               else if(es->hloc16)
-               {
-                   UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
-
-                   TRACE("Synchronizing with 16-bit ANSI buffer\n");
-                   TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
-
-                   stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
-                   oldDS = stack16->ds;
-                   stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
-
-                   countA = LocalSize16(es->hloc16);
-                   if(countA_new > countA)
-                   {
-                       HLOCAL16 hloc16_new;
-                       UINT alloc_size = ROUND_TO_GROW(countA_new);
-                       TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
-                       hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
-                       if(hloc16_new)
-                       {
-                           es->hloc16 = hloc16_new;
-                           countA = LocalSize16(hloc16_new);
-                           TRACE("Real new size %d bytes\n", countA);
-                       }
-                       else
-                           WARN("FAILED! Will synchronize partially\n");
-                   }
-                   textA = MapSL(LocalLock16(es->hloc16));
-               }
-#endif
-
-               if(textA)
-               {
-                   WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
-#ifndef __REACTOS__
-                   if(stack16)
-                       LocalUnlock16(es->hloc16);
-                   else
-#endif
-                       LocalUnlock(es->hloc32A);
+                   WideCharToMultiByte(CP_ACP, 0, es->text, countW,
+                                        LocalLock(es->hloc32A), countA, NULL, NULL);
+                    LocalUnlock(es->hloc32A);
                }
 
-#ifndef __REACTOS__
-               if (stack16) stack16->ds = oldDS;
-#endif
                LocalUnlock(es->hloc32W);
                es->text = NULL;
            }
@@ -1764,17 +1639,16 @@ static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
            INT vlc = get_vertical_line_count(es);
            /* check if we are going to move too far */
            if(es->y_offset + dy > es->line_count - vlc)
-               dy = es->line_count - vlc - es->y_offset;
+               dy = max(es->line_count - vlc, 0) - es->y_offset;
 
            /* Notification is done in EDIT_EM_LineScroll */
-           if(dy)
+           if(dy) {
                EDIT_EM_LineScroll(es, 0, dy);
+               return MAKELONG(dy, TRUE);
+           }
+
        }
-#ifdef __REACTOS__
-       return MAKELONG((SHORT)dy, (BOOL)TRUE);
-#else
-       return MAKELONG((INT16)dy, (BOOL16)TRUE);
-#endif
+       return (LRESULT)FALSE;
 }
 
 
@@ -2427,77 +2301,6 @@ static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
 }
 
 
-#ifndef __REACTOS__
-/*********************************************************************
- *
- *     EM_GETHANDLE16
- *
- *     Hopefully this won't fire back at us.
- *     We always start with a buffer in 32 bit linear memory.
- *     However, with this message a 16 bit application requests
- *     a handle of 16 bit local heap memory, where it expects to find
- *     the text.
- *     It's a pitty that from this moment on we have to use this
- *     local heap, because applications may rely on the handle
- *     in the future.
- *
- *     In this function we'll try to switch to local heap.
- */
-static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
-{
-       CHAR *textA;
-       UINT countA, alloc_size;
-       STACK16FRAME* stack16;
-       HANDLE16 oldDS;
-
-       if (!(es->style & ES_MULTILINE))
-               return 0;
-
-       if (es->hloc16)
-               return es->hloc16;
-
-       stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
-       oldDS = stack16->ds;
-       stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
-
-       if (!LocalHeapSize16()) {
-
-               if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
-                       ERR("could not initialize local heap\n");
-                       goto done;
-               }
-               TRACE("local heap initialized\n");
-       }
-
-       countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
-       alloc_size = ROUND_TO_GROW(countA);
-
-       TRACE("Allocating 16-bit ANSI alias buffer\n");
-       if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
-               ERR("could not allocate new 16 bit buffer\n");
-               goto done;
-       }
-
-       if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
-               ERR("could not lock new 16 bit buffer\n");
-               LocalFree16(es->hloc16);
-               es->hloc16 = 0;
-               goto done;
-       }
-
-       WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
-       LocalUnlock16(es->hloc16);
-        es->flags |= EF_APP_HAS_HANDLE;
-
-       TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
-
-done:
-       stack16->ds = oldDS;
-       return es->hloc16;
-}
-#endif
-
-
 /*********************************************************************
  *
  *     EM_GETLINE
@@ -2790,19 +2593,6 @@ static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
 
        EDIT_UnlockBuffer(es, TRUE);
 
-#ifndef __REACTOS__
-       if(es->hloc16)
-       {
-           STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
-           HANDLE16 oldDS = stack16->ds;
-       
-           stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
-           LocalFree16(es->hloc16);
-           stack16->ds = oldDS;
-           es->hloc16 = 0;
-       }
-#endif
-
        if(es->is_unicode)
        {
            if(es->hloc32A)
@@ -2857,80 +2647,6 @@ static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
 }
 
 
-#ifndef __REACTOS__
-/*********************************************************************
- *
- *     EM_SETHANDLE16
- *
- *     FIXME:  ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
- *
- */
-static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
-{
-       STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
-       HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
-       HANDLE16 oldDS = stack16->ds;
-       INT countW, countA;
-       HLOCAL hloc32W_new;
-       WCHAR *textW;
-       CHAR *textA;
-
-       if (!(es->style & ES_MULTILINE))
-               return;
-
-       if (!hloc) {
-               WARN("called with NULL handle\n");
-               return;
-       }
-
-       EDIT_UnlockBuffer(es, TRUE);
-
-       if(es->hloc32A)
-       {
-           LocalFree(es->hloc32A);
-           es->hloc32A = NULL;
-       }
-
-       stack16->ds = hInstance;
-       countA = LocalSize16(hloc);
-       textA = MapSL(LocalLock16(hloc));
-       countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
-       if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
-       {
-           ERR("Could not allocate new unicode buffer\n");
-           return;
-       }
-       textW = LocalLock(hloc32W_new);
-       MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
-       LocalUnlock(hloc32W_new);
-       LocalUnlock16(hloc);
-       stack16->ds = oldDS;
-
-       if(es->hloc32W)
-           LocalFree(es->hloc32W);
-
-       es->hloc32W = hloc32W_new;
-       es->hloc16 = hloc;
-
-       es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
-
-        es->flags |= EF_APP_HAS_HANDLE;
-       EDIT_LockBuffer(es);
-
-       es->x_offset = es->y_offset = 0;
-       es->selection_start = es->selection_end = 0;
-       EDIT_EM_EmptyUndoBuffer(es);
-       es->flags &= ~EF_MODIFIED;
-       es->flags &= ~EF_UPDATE;
-       EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
-       EDIT_UpdateText(es, NULL, TRUE);
-       EDIT_EM_ScrollCaret(es);
-       /* force scroll info update */
-       EDIT_UpdateScrollInfo(es);
-}
-#endif
-
-
 /*********************************************************************
  *
  *     EM_SETLIMITTEXT
@@ -3046,13 +2762,13 @@ static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
        if (es->password_char == c)
                return;
 
-        style = GetWindowLongPtrW( es->hwndSelf, GWL_STYLE );
+        style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
        es->password_char = c;
        if (c) {
-            SetWindowLongPtrW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
+            SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
             es->style |= ES_PASSWORD;
        } else {
-            SetWindowLongPtrW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
+            SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
             es->style &= ~ES_PASSWORD;
        }
        EDIT_UpdateText(es, NULL, TRUE);
@@ -3074,42 +2790,17 @@ static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
                es->tabs = NULL;
        else {
                es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
-        if (es->tabs == NULL)
+        if (es->tabs == NULL) // reactos r33503
         {
             es->tabs_count = 0;
             return FALSE;
-        }
+        } // reactos r33503
                memcpy(es->tabs, tabs, count * sizeof(INT));
        }
        return TRUE;
 }
 
 
-#ifndef __REACTOS__
-/*********************************************************************
- *
- *     EM_SETTABSTOPS16
- *
- */
-static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs)
-{
-       if (!(es->style & ES_MULTILINE))
-               return FALSE;
-        HeapFree(GetProcessHeap(), 0, es->tabs);
-       es->tabs_count = count;
-       if (!count)
-               es->tabs = NULL;
-       else {
-               INT i;
-               es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
-               for (i = 0 ; i < count ; i++)
-                       es->tabs[i] = *tabs++;
-       }
-       return TRUE;
-}
-#endif
-
-
 /*********************************************************************
  *
  *     EM_SETWORDBREAKPROC
@@ -3121,36 +2812,12 @@ static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
                return;
 
        es->word_break_proc = wbp;
-#ifndef __REACTOS__
-       es->word_break_proc16 = NULL;
-#endif
-
-       if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
-               EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
-               EDIT_UpdateText(es, NULL, TRUE);
-       }
-}
-
-
-#ifndef __REACTOS__
-/*********************************************************************
- *
- *     EM_SETWORDBREAKPROC16
- *
- */
-static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
-{
-       if (es->word_break_proc16 == wbp)
-               return;
 
-       es->word_break_proc = NULL;
-       es->word_break_proc16 = wbp;
        if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
                EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
                EDIT_UpdateText(es, NULL, TRUE);
        }
 }
-#endif
 
 
 /*********************************************************************
@@ -3171,8 +2838,8 @@ static BOOL EDIT_EM_Undo(EDITSTATE *es)
        ulength = strlenW(es->undo_text);
 
        utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
-    if (utext == NULL)
-        return FALSE;
+    if (utext == NULL) // reactos r33503
+        return FALSE; // reactos r33503
 
        strcpyW(utext, es->undo_text);
 
@@ -3201,7 +2868,7 @@ static BOOL EDIT_EM_Undo(EDITSTATE *es)
  * controls without ES_WANTRETURN would attempt to detect whether it is inside
  * a dialog box or not.
  */
-static BOOL EDIT_IsInsideDialog(EDITSTATE *es)
+static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
 {
     return (es->flags & EF_DIALOGMODE);
 }
@@ -3229,8 +2896,7 @@ static void EDIT_WM_Paste(EDITSTATE *es)
        }
         else if (es->style & ES_PASSWORD) {
             /* clear selected text in password edit box even with empty clipboard */
-            const WCHAR empty_strW[] = { 0 };
-            EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
+            EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
         }
        CloseClipboard();
 }
@@ -3272,8 +2938,6 @@ static void EDIT_WM_Copy(EDITSTATE *es)
  */
 static inline void EDIT_WM_Clear(EDITSTATE *es)
 {
-       static const WCHAR empty_stringW[] = {0};
-
        /* Protect read-only edit control from modification */
        if(es->style & ES_READONLY)
            return;
@@ -3379,7 +3043,7 @@ static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
     return 1;
 }
 
-
+#if 0 // Removed see Revision 43925 comments.
 /*********************************************************************
  *
  *     WM_COMMAND
@@ -3415,6 +3079,7 @@ static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
                        break;
        }
 }
+#endif
 
 
 /*********************************************************************
@@ -3435,15 +3100,12 @@ static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
  */
 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
 {
-#ifdef __REACTOS__
-       HMENU menu = LoadMenuA(User32Instance, "EDITMENU");
-#else
        HMENU menu = LoadMenuA(user32_module, "EDITMENU");
-#endif
        HMENU popup = GetSubMenu(menu, 0);
        UINT start = es->selection_start;
        UINT end = es->selection_end;
 
+    BOOL selectedItem;
        ORDER_UINT(start, end);
 
        /* undo */
@@ -3469,7 +3131,37 @@ static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
             y = rc.top + (rc.bottom - rc.top) / 2;
         }
 
-       TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, x, y, 0, es->hwndSelf, NULL);
+       if (!(es->flags & EF_FOCUSED))
+            SetFocus(es->hwndSelf);
+
+#ifdef __REACTOS__
+       selectedItem = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, x, y, 0, es->hwndSelf, NULL);
+        // Added see Revision 43925 comments.
+       switch (selectedItem) {
+               case EM_UNDO:
+                        SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
+                       break;
+               case WM_CUT:
+                        SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
+                       break;
+               case WM_COPY:
+                        SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
+                       break;
+               case WM_PASTE:
+                        SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
+                       break;
+               case WM_CLEAR:
+                        SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
+                       break;
+               case EM_SETSEL:
+                       EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
+                       EDIT_EM_ScrollCaret(es);
+                       break;
+               default:
+                       ERR("unknown menu item, please report\n");
+                       break;
+       }
+#endif
        DestroyMenu(menu);
 }
 
@@ -3538,14 +3230,14 @@ static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
             nEUI = 2;
          }
 
-         SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
+         SendMessageW(hLBox, WM_KEYDOWN, key, 0);
          break;
 
       case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
          if (nEUI)
             SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
          else
-            SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
+            SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
          break;
    }
 
@@ -3659,32 +3351,32 @@ static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
            /* If the edit doesn't want the return send a message to the default object */
            if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
            {
-               DWORD dw;
+                DWORD dw;
 
                 if (!EDIT_IsInsideDialog(es)) break;
                 if (control) break;
-                dw = SendMessageW( es->hwndParent, DM_GETDEFID, 0, 0 );
+                dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
                 if (HIWORD(dw) == DC_HASDEFID)
                 {
                     HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
                     if (hwDefCtrl)
                     {
-                        SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, (LPARAM)TRUE);
+                        SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
                         PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
                     }
                 }
            }
            break;
         case VK_ESCAPE:
-           if (!(es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
-               PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
+            if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
+                PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
             break;
         case VK_TAB:
             if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
                 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
             break;
        }
-       return TRUE;
+        return TRUE;
 }
 
 
@@ -3981,7 +3673,7 @@ static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
                EDIT_UpdateText(es, NULL, TRUE);
        if (es->flags & EF_FOCUSED) {
                DestroyCaret();
-               CreateCaret(es->hwndSelf, 0, 2, es->line_height);
+               CreateCaret(es->hwndSelf, 0, 1, es->line_height);
                EDIT_SetCaretPos(es, es->selection_end,
                                 es->flags & EF_AFTER_WRAP);
                ShowCaret(es->hwndSelf);
@@ -4029,7 +3721,6 @@ static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
     } 
     else 
     {
-       static const WCHAR empty_stringW[] = {0};
        TRACE("<NULL>\n");
        EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
     }
@@ -4140,7 +3831,7 @@ static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
                if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
                        return 0;
        }
-       return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
+       return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
 }
 
 
@@ -4230,7 +3921,7 @@ static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
        case SB_THUMBPOSITION:
                TRACE("SB_THUMBPOSITION %d\n", pos);
                es->flags &= ~EF_HSCROLL_TRACK;
-               if(GetWindowLongPtrW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
+               if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
                    dx = pos - es->x_offset;
                else
                {
@@ -4258,12 +3949,9 @@ static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
         *      although it's also a regular control message.
         */
        case EM_GETTHUMB: /* this one is used by NT notepad */
-#ifndef __REACTOS__
-       case EM_GETTHUMB16:
-#endif
        {
                LRESULT ret;
-               if(GetWindowLongPtrW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
+               if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
                    ret = GetScrollPos(es->hwndSelf, SB_HORZ);
                else
                {
@@ -4274,12 +3962,10 @@ static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
                TRACE("EM_GETTHUMB: returning %ld\n", ret);
                return ret;
        }
-#ifndef __REACTOS__
-       case EM_LINESCROLL16:
+       case EM_LINESCROLL:
                TRACE("EM_LINESCROLL16\n");
                dx = pos;
                break;
-#endif
 
        default:
                ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
@@ -4386,12 +4072,9 @@ static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
         *      although it's also a regular control message.
         */
        case EM_GETTHUMB: /* this one is used by NT notepad */
-#ifndef __REACTOS__
-       case EM_GETTHUMB16:
-#endif
        {
                LRESULT ret;
-               if(GetWindowLongPtrW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
+               if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
                    ret = GetScrollPos(es->hwndSelf, SB_VERT);
                else
                {
@@ -4402,12 +4085,10 @@ static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
                TRACE("EM_GETTHUMB: returning %ld\n", ret);
                return ret;
        }
-#ifndef __REACTOS__
-       case EM_LINESCROLL16:
-               TRACE("EM_LINESCROLL16 %d\n", pos);
+       case EM_LINESCROLL:
+               TRACE("EM_LINESCROLL %d\n", pos);
                dy = pos;
                break;
-#endif
 
        default:
                ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
@@ -4431,15 +4112,11 @@ static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
  */
 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
 {
-#ifdef __REACTOS__
        return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
-               EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
-#else
-       return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
-               EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
-#endif
+                        EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
 }
 
+
 /********************************************************************
  * 
  * The Following code is to handle inline editing from IMEs
@@ -4558,7 +4235,6 @@ static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
 
     if (es->composition_len == 0 && es->selection_start != es->selection_end)
     {
-        static const WCHAR empty_stringW[] = {0};
         EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
         es->composition_start = es->selection_end;
     }
@@ -4601,8 +4277,8 @@ static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
         *            WM_XXX messages before WM_NCCREATE is completed.
         */
 
-       es->is_unicode = unicode;
-       es->style = lpcs->style;
+       es->is_unicode = unicode;
+       es->style = lpcs->style;
 
         es->bEnableState = !(es->style & WS_DISABLED);
 
@@ -4651,16 +4327,16 @@ static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
 
        alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
        if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
-           return FALSE;
+           goto cleanup;
        es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
 
        if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
-               return FALSE;
+               goto cleanup;
        es->undo_buffer_size = es->buffer_size;
 
        if (es->style & ES_MULTILINE)
                if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
-                       return FALSE;
+                       goto cleanup;
        es->line_count = 1;
 
        /*
@@ -4670,16 +4346,24 @@ static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
          * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
          * a nonclient area and we should handle painting the border ourselves.
          *
-         * When making modifications please ensure that the code still works 
+         * When making modifications please ensure that the code still works
          * for edit controls created directly with style 0x50800000, exStyle 0
          * (which should have a single pixel border)
         */
        if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
                es->style &= ~WS_BORDER;
         else if (es->style & WS_BORDER)
-               SetWindowLongPtrW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
+               SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
 
        return TRUE;
+
+cleanup:
+       SetWindowLongPtrW(es->hwndSelf, 0, 0);
+       HeapFree(GetProcessHeap(), 0, es->first_line_def);
+       HeapFree(GetProcessHeap(), 0, es->undo_text);
+       if (es->hloc32W) LocalFree(es->hloc32W);
+       HeapFree(GetProcessHeap(), 0, es);
+       return FALSE;
 }
 
 
@@ -4691,7 +4375,7 @@ static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
 {
         RECT clientRect;
-        
+
        TRACE("%s\n", debugstr_w(name));
        /*
         *      To initialize some final structure members, we call some helper
@@ -4701,7 +4385,7 @@ static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
         */
         EDIT_WM_SetFont(es, 0, FALSE);
         EDIT_EM_EmptyUndoBuffer(es);
-        
+
         /* We need to calculate the format rect
            (applications may send EM_SETMARGINS before the control gets visible) */
         GetClientRect(es->hwndSelf, &clientRect);
@@ -4735,10 +4419,10 @@ static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
 
 /*********************************************************************
  *
- *     WM_DESTROY
+ *     WM_NCDESTROY
  *
  */
-static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
+static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
 {
        LINEDEF *pc, *pp;
 
@@ -4748,18 +4432,6 @@ static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
        if (es->hloc32A) {
                LocalFree(es->hloc32A);
        }
-#ifndef __REACTOS__
-       if (es->hloc16) {
-               STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
-               HANDLE16 oldDS = stack16->ds;
-
-               stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
-               while (LocalUnlock16(es->hloc16)) ;
-               LocalFree16(es->hloc16);
-               stack16->ds = oldDS;
-       }
-#endif
-
        pc = es->first_line_def;
        while (pc)
        {
@@ -4768,7 +4440,8 @@ static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
                pc = pp;
        }
 
-        SetWindowLongPtrW( es->hwndSelf, 0, 0 );
+       SetWindowLongPtrW( es->hwndSelf, 0, 0 );
+       HeapFree(GetProcessHeap(), 0, es->undo_text);
        HeapFree(GetProcessHeap(), 0, es);
 
        return 0;
@@ -4789,85 +4462,47 @@ static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
  *
  *     The messages are in the order of the actual integer values
  *     (which can be found in include/windows.h)
- *     Wherever possible the 16 bit versions are converted to
- *     the 32 bit ones, so that we can 'fall through' to the
- *     helper functions.  These are mostly 32 bit (with a few
- *     exceptions, clearly indicated by a '16' extension to their
- *     names).
- *
  */
-LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
-                                   WPARAM wParam, LPARAM lParam, BOOL unicode )
+LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
 {
        EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
        LRESULT result = 0;
+#ifdef __REACTOS__
+        PWND pWnd;
+
+        pWnd = ValidateHwnd(hwnd);
+        if (pWnd)
+        {
+           if (!pWnd->fnid)
+           {
+              NtUserSetWindowFNID(hwnd, FNID_EDIT);
+           }
+        }
+#endif
 
         TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
-       
+
        if (!es && msg != WM_NCCREATE)
                return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
 
-       if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
+       if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
 
        switch (msg) {
-#ifndef __REACTOS__
-       case EM_GETSEL16:
-               wParam = 0;
-               lParam = 0;
-               /* fall through */
-#endif
        case EM_GETSEL:
                result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_SETSEL16:
-               if ((short)LOWORD(lParam) == -1)
-                       EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
-               else
-                       EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
-               if (!wParam)
-                       EDIT_EM_ScrollCaret(es);
-               result = 1;
-               break;
-#endif
        case EM_SETSEL:
                EDIT_EM_SetSel(es, wParam, lParam, FALSE);
                EDIT_EM_ScrollCaret(es);
                result = 1;
                break;
 
-#ifndef __REACTOS__
-       case EM_GETRECT16:
-               if (lParam)
-                {
-                    RECT16 *r16 = MapSL(lParam);
-                    r16->left   = es->format_rect.left;
-                    r16->top    = es->format_rect.top;
-                    r16->right  = es->format_rect.right;
-                    r16->bottom = es->format_rect.bottom;
-                }
-               break;
-#endif
        case EM_GETRECT:
                if (lParam)
                        CopyRect((LPRECT)lParam, &es->format_rect);
                break;
 
-#ifndef __REACTOS__
-       case EM_SETRECT16:
-               if ((es->style & ES_MULTILINE) && lParam) {
-                       RECT rc;
-                       RECT16 *r16 = MapSL(lParam);
-                       rc.left   = r16->left;
-                       rc.top    = r16->top;
-                       rc.right  = r16->right;
-                       rc.bottom = r16->bottom;
-                       EDIT_SetRectNP(es, &rc);
-                       EDIT_UpdateText(es, NULL, TRUE);
-               }
-               break;
-#endif
        case EM_SETRECT:
                if ((es->style & ES_MULTILINE) && lParam) {
                        EDIT_SetRectNP(es, (LPRECT)lParam);
@@ -4875,59 +4510,28 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
                }
                break;
 
-#ifndef __REACTOS__
-       case EM_SETRECTNP16:
-               if ((es->style & ES_MULTILINE) && lParam) {
-                       RECT rc;
-                       RECT16 *r16 = MapSL(lParam);
-                       rc.left   = r16->left;
-                       rc.top    = r16->top;
-                       rc.right  = r16->right;
-                       rc.bottom = r16->bottom;
-                       EDIT_SetRectNP(es, &rc);
-               }
-               break;
-#endif
        case EM_SETRECTNP:
                if ((es->style & ES_MULTILINE) && lParam)
                        EDIT_SetRectNP(es, (LPRECT)lParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_SCROLL16:
-#endif
        case EM_SCROLL:
                result = EDIT_EM_Scroll(es, (INT)wParam);
                 break;
 
-#ifndef __REACTOS__
-       case EM_LINESCROLL16:
-               wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
-               lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
-               /* fall through */
-#endif
        case EM_LINESCROLL:
                result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_SCROLLCARET16:
-#endif
        case EM_SCROLLCARET:
                EDIT_EM_ScrollCaret(es);
                result = 1;
                break;
 
-#ifndef __REACTOS__
-       case EM_GETMODIFY16:
-#endif
        case EM_GETMODIFY:
                result = ((es->flags & EF_MODIFIED) != 0);
                break;
 
-#ifndef __REACTOS__
-       case EM_SETMODIFY16:
-#endif
        case EM_SETMODIFY:
                if (wParam)
                        es->flags |= EF_MODIFIED;
@@ -4935,74 +4539,39 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
                         es->flags &= ~(EF_MODIFIED | EF_UPDATE);  /* reset pending updates */
                break;
 
-#ifndef __REACTOS__
-       case EM_GETLINECOUNT16:
-#endif
        case EM_GETLINECOUNT:
                result = (es->style & ES_MULTILINE) ? es->line_count : 1;
                break;
 
-#ifndef __REACTOS__
-       case EM_LINEINDEX16:
-               if ((INT16)wParam == -1)
-                       wParam = (WPARAM)-1;
-               /* fall through */
-#endif
        case EM_LINEINDEX:
                result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_SETHANDLE16:
-               EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
-               break;
-#endif
        case EM_SETHANDLE:
                EDIT_EM_SetHandle(es, (HLOCAL)wParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_GETHANDLE16:
-               result = (LRESULT)EDIT_EM_GetHandle16(es);
-               break;
-#endif
        case EM_GETHANDLE:
                result = (LRESULT)EDIT_EM_GetHandle(es);
                break;
 
-#ifndef __REACTOS__
-       case EM_GETTHUMB16:
-#endif
        case EM_GETTHUMB:
                result = EDIT_EM_GetThumb(es);
                break;
 
        /* these messages missing from specs */
-       case WM_USER+15:
        case 0x00bf:
-       case WM_USER+16:
        case 0x00c0:
-       case WM_USER+19:
        case 0x00c3:
-       case WM_USER+26:
        case 0x00ca:
                FIXME("undocumented message 0x%x, please report\n", msg);
                result = DefWindowProcW(hwnd, msg, wParam, lParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_LINELENGTH16:
-#endif
        case EM_LINELENGTH:
                result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_REPLACESEL16:
-               lParam = (LPARAM)MapSL(lParam);
-               unicode = FALSE;  /* 16-bit message is always ascii */
-               /* fall through */
-#endif
        case EM_REPLACESEL:
        {
                LPWSTR textW;
@@ -5013,8 +4582,8 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
                {
                    LPSTR textA = (LPSTR)lParam;
                    INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
-                   if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
-                       MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
+                   if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
+                    MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
                }
 
                EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
@@ -5025,66 +4594,35 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
                break;
        }
 
-#ifndef __REACTOS__
-       case EM_GETLINE16:
-               lParam = (LPARAM)MapSL(lParam);
-               unicode = FALSE;  /* 16-bit message is always ascii */
-               /* fall through */
-#endif
        case EM_GETLINE:
                result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
                break;
 
-#ifndef __REACTOS__
-       case EM_LIMITTEXT16:
-#endif
        case EM_SETLIMITTEXT:
                EDIT_EM_SetLimitText(es, wParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_CANUNDO16:
-#endif
        case EM_CANUNDO:
                result = (LRESULT)EDIT_EM_CanUndo(es);
                break;
 
-#ifndef __REACTOS__
-       case EM_UNDO16:
-#endif
        case EM_UNDO:
        case WM_UNDO:
                result = (LRESULT)EDIT_EM_Undo(es);
                break;
 
-#ifndef __REACTOS__
-       case EM_FMTLINES16:
-#endif
        case EM_FMTLINES:
                result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_LINEFROMCHAR16:
-#endif
        case EM_LINEFROMCHAR:
                result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_SETTABSTOPS16:
-               result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
-               break;
-#endif
        case EM_SETTABSTOPS:
                result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_SETPASSWORDCHAR16:
-               unicode = FALSE;  /* 16-bit message is always ascii */
-               /* fall through */
-#endif
        case EM_SETPASSWORDCHAR:
        {
                WCHAR charW = 0;
@@ -5101,61 +4639,43 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
                break;
        }
 
-#ifndef __REACTOS__
-       case EM_EMPTYUNDOBUFFER16:
-#endif
        case EM_EMPTYUNDOBUFFER:
                EDIT_EM_EmptyUndoBuffer(es);
                break;
 
-#ifndef __REACTOS__
-       case EM_GETFIRSTVISIBLELINE16:
-               result = es->y_offset;
-               break;
-#endif
        case EM_GETFIRSTVISIBLELINE:
                result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
                break;
 
-#ifndef __REACTOS__
-       case EM_SETREADONLY16:
-#endif
        case EM_SETREADONLY:
+       {
+               DWORD old_style = es->style;
+
                if (wParam) {
-                    SetWindowLongPtrW( hwnd, GWL_STYLE,
-                                       GetWindowLongPtrW( hwnd, GWL_STYLE ) | ES_READONLY );
+                    SetWindowLongW( hwnd, GWL_STYLE,
+                                    GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
                     es->style |= ES_READONLY;
                } else {
-                    SetWindowLongPtrW( hwnd, GWL_STYLE,
-                                       GetWindowLongPtrW( hwnd, GWL_STYLE ) & ~ES_READONLY );
+                    SetWindowLongW( hwnd, GWL_STYLE,
+                                    GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
                     es->style &= ~ES_READONLY;
                }
-                result = 1;
-               break;
 
-#ifndef __REACTOS__
-       case EM_SETWORDBREAKPROC16:
-               EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
+               if (old_style ^ es->style)
+                   InvalidateRect(es->hwndSelf, NULL, TRUE);
+
+               result = 1;
                break;
-#endif
+       }
+
        case EM_SETWORDBREAKPROC:
                EDIT_EM_SetWordBreakProc(es, (void *)lParam);
                break;
 
-#ifndef __REACTOS__
-       case EM_GETWORDBREAKPROC16:
-               result = (LRESULT)es->word_break_proc16;
-               break;
-#endif
        case EM_GETWORDBREAKPROC:
                result = (LRESULT)es->word_break_proc;
                break;
 
-#ifndef __REACTOS__
-       case EM_GETPASSWORDCHAR16:
-               unicode = FALSE;  /* 16-bit message is always ascii */
-               /* fall through */
-#endif
        case EM_GETPASSWORDCHAR:
        {
                if(unicode)
@@ -5170,8 +4690,6 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
                break;
        }
 
-       /* The following EM_xxx are new to win95 and don't exist for 16 bit */
-
        case EM_SETMARGINS:
                EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
                break;
@@ -5201,14 +4719,17 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
                result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
                break;
 
-       case WM_DESTROY:
-               result = EDIT_WM_Destroy(es);
-               es = NULL;
+       case WM_NCDESTROY:
+               result = EDIT_WM_NCDestroy(es);
+//             es = NULL; reactos
+#ifdef __REACTOS__
+                NtUserSetWindowFNID(hwnd, FNID_DESTROY);
+#endif
                break;
 
        case WM_GETDLGCODE:
                result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
-               
+
                if (es->style & ES_MULTILINE)
                   result |= DLGC_WANTALLKEYS;
 
@@ -5290,11 +4811,11 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
        case WM_CLEAR:
                EDIT_WM_Clear(es);
                break;
-
+#if 0 // Removed see Revision 43925 comments.
        case WM_COMMAND:
                EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
                break;
-
+#endif
         case WM_CONTEXTMENU:
                EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
                break;
@@ -5374,7 +4895,7 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
                break;
 
        case WM_MBUTTONDOWN:
-               result = EDIT_WM_MButtonDown(es);
+               result = EDIT_WM_MButtonDown(es);
                break;
 
        case WM_MOUSEMOVE:
@@ -5451,8 +4972,8 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
                 }
                 break;
 
-            
-       /* IME messages to make the edit control IME aware */           
+
+       /* IME messages to make the edit control IME aware */
        case WM_IME_SETCONTEXT:
                break;
 
@@ -5468,7 +4989,6 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
        case WM_IME_ENDCOMPOSITION:
                 if (es->composition_len > 0)
                 {
-                        static const WCHAR empty_stringW[] = {0};
                         EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
                         es->selection_end = es->selection_start;
                         es->composition_len= 0;
@@ -5483,13 +5003,15 @@ LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
 
        case WM_IME_CONTROL:
                break;
-                
+
        default:
                result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
                break;
        }
-       
-       if (es) EDIT_UnlockBuffer(es, FALSE);
+
+       /* reactos: check GetWindowLong in case es has been destroyed during processing */
+       if (IsWindow(hwnd) && es && GetWindowLongPtrW(hwnd, 0))
+               EDIT_UnlockBuffer(es, FALSE);
 
         TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
 
@@ -5524,7 +5046,11 @@ const struct builtin_class_descr EDIT_builtin_class =
     CS_DBLCLKS | CS_PARENTDC,   /* style */
     EditWndProcA,         /* procA */
     EditWndProcW,         /* procW */
+#ifndef _WIN64
+    sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
+#else
     sizeof(EDITSTATE *),  /* extra */
+#endif
     IDC_IBEAM,            /* cursor */
     0                     /* brush */
 };