Fixed some problems with scrolling in the edit control.
[wine] / controls / edit.c
1 /*
2  *      Edit control
3  *
4  *      Copyright  David W. Metcalfe, 1994
5  *      Copyright  William Magro, 1995, 1996
6  *      Copyright  Frans van Dorsselaer, 1996, 1997
7  *
8  */
9
10 /*
11  *      please read EDIT.TODO (and update it when you change things)
12  */
13
14 #include "config.h"
15
16 #include <string.h>
17 #include <stdlib.h>
18
19 #include "winbase.h"
20 #include "winnt.h"
21 #include "win.h"
22 #include "wine/winbase16.h"
23 #include "wine/winuser16.h"
24 #include "wine/unicode.h"
25 #include "controls.h"
26 #include "local.h"
27 #include "debugtools.h"
28 #include "tweak.h"
29
30 DEFAULT_DEBUG_CHANNEL(edit);
31 DECLARE_DEBUG_CHANNEL(combo);
32 DECLARE_DEBUG_CHANNEL(relay);
33
34 #define BUFLIMIT_MULTI          65534   /* maximum buffer size (not including '\0')
35                                            FIXME: BTW, new specs say 65535 (do you dare ???) */
36 #define BUFLIMIT_SINGLE         32766   /* maximum buffer size (not including '\0') */
37 #define GROWLENGTH              32      /* buffers granularity in bytes: must be power of 2 */
38 #define HSCROLL_FRACTION        3       /* scroll window by 1/3 width */
39
40 /*
41  *      extra flags for EDITSTATE.flags field
42  */
43 #define EF_MODIFIED             0x0001  /* text has been modified */
44 #define EF_FOCUSED              0x0002  /* we have input focus */
45 #define EF_UPDATE               0x0004  /* notify parent of changed state on next WM_PAINT */
46 #define EF_VSCROLL_TRACK        0x0008  /* don't SetScrollPos() since we are tracking the thumb */
47 #define EF_HSCROLL_TRACK        0x0010  /* don't SetScrollPos() since we are tracking the thumb */
48 #define EF_VSCROLL_HACK         0x0020  /* we already have informed the user of the hacked handler */
49 #define EF_HSCROLL_HACK         0x0040  /* we already have informed the user of the hacked handler */
50 #define EF_AFTER_WRAP           0x0080  /* the caret is displayed after the last character of a
51                                            wrapped line, instead of in front of the next character */
52 #define EF_USE_SOFTBRK          0x0100  /* Enable soft breaks in text. */
53
54 typedef enum
55 {
56         END_0 = 0,      /* line ends with terminating '\0' character */
57         END_WRAP,       /* line is wrapped */
58         END_HARD,       /* line ends with a hard return '\r\n' */
59         END_SOFT        /* line ends with a soft return '\r\r\n' */
60 } LINE_END;
61
62 typedef struct tagLINEDEF {
63         INT length;             /* bruto length of a line in bytes */
64         INT net_length; /* netto length of a line in visible characters */
65         LINE_END ending;
66         INT width;              /* width of the line in pixels */
67         struct tagLINEDEF *next;
68 } LINEDEF;
69
70 typedef struct
71 {
72         BOOL is_unicode;                /* how the control was created */
73         LPWSTR text;                    /* the actual contents of the control */
74         UINT buffer_size;               /* the size of the buffer in characters */
75         UINT buffer_limit;              /* the maximum size to which the buffer may grow in characters */
76         HFONT font;                     /* NULL means standard system font */
77         INT x_offset;                   /* scroll offset        for multi lines this is in pixels
78                                                                 for single lines it's in characters */
79         INT line_height;                /* height of a screen line in pixels */
80         INT char_width;         /* average character width in pixels */
81         DWORD style;                    /* sane version of wnd->dwStyle */
82         WORD flags;                     /* flags that are not in es->style or wnd->flags (EF_XXX) */
83         INT undo_insert_count;  /* number of characters inserted in sequence */
84         UINT undo_position;             /* character index of the insertion and deletion */
85         LPWSTR undo_text;               /* deleted text */
86         UINT undo_buffer_size;          /* size of the deleted text buffer */
87         INT selection_start;            /* == selection_end if no selection */
88         INT selection_end;              /* == current caret position */
89         WCHAR password_char;            /* == 0 if no password char, and for multi line controls */
90         INT left_margin;                /* in pixels */
91         INT right_margin;               /* in pixels */
92         RECT format_rect;
93         INT text_width;                 /* width of the widest line in pixels for multi line controls
94                                            and just line width for single line controls */
95         INT region_posx;                /* Position of cursor relative to region: */
96         INT region_posy;                /* -1: to left, 0: within, 1: to right */
97         EDITWORDBREAKPROC16 word_break_proc16;
98         void *word_break_proc;          /* 32-bit word break proc: ANSI or Unicode */
99         INT line_count;         /* number of lines */
100         INT y_offset;                   /* scroll offset in number of lines */
101         BOOL bCaptureState; /* flag indicating whether mouse was captured */
102         BOOL bEnableState;             /* flag keeping the enable state */
103         HWND hwndListBox;              /* handle of ComboBox's listbox or NULL */
104         /*
105          *      only for multi line controls
106          */
107         INT lock_count;         /* amount of re-entries in the EditWndProc */
108         INT tabs_count;
109         LPINT tabs;
110         LINEDEF *first_line_def;        /* linked list of (soft) linebreaks */
111         HLOCAL hloc32W;         /* our unicode local memory block */
112         HLOCAL16 hloc16;        /* alias for 16-bit control receiving EM_GETHANDLE16
113                                    or EM_SETHANDLE16 */
114         HLOCAL hloc32A;         /* alias for ANSI control receiving EM_GETHANDLE
115                                    or EM_SETHANDLE */
116 } EDITSTATE;
117
118
119 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
120 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
121
122 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
123 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
124
125 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
126         do {TRACE("notification " str " sent to hwnd=%08x\n", \
127                        (UINT)(hwnd));} while(0)
128
129 /* used for disabled or read-only edit control */
130 #define EDIT_SEND_CTLCOLORSTATIC(wnd,hdc) \
131         (SendMessageW((wnd)->parent->hwndSelf, WM_CTLCOLORSTATIC, \
132                         (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
133 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
134         (SendMessageW((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
135                         (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
136 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
137         do {DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str); \
138         SendMessageW((wnd)->parent->hwndSelf, WM_COMMAND, \
139                      MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
140                      (LPARAM)(wnd)->hwndSelf);} while(0)
141 #define DPRINTF_EDIT_MSG16(str) \
142         TRACE(\
143                      "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
144                      (UINT)wnd->hwndSelf, (UINT)wParam, (UINT)lParam)
145 #define DPRINTF_EDIT_MSG32(str) \
146         TRACE(\
147                      "32 bit %c : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
148                      unicode ? 'W' : 'A', \
149                      (UINT)wnd->hwndSelf, (UINT)wParam, (UINT)lParam)
150
151 /*********************************************************************
152  *
153  *      Declarations
154  *
155  */
156
157 /*
158  *      These functions have trivial implementations
159  *      We still like to call them internally
160  *      "static inline" makes them more like macro's
161  */
162 static inline BOOL      EDIT_EM_CanUndo(EDITSTATE *es);
163 static inline void      EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
164 static inline void      EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
165 static inline void      EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
166
167 /*
168  *      Helper functions only valid for one type of control
169  */
170 static void     EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
171 static void     EDIT_CalcLineWidth_SL(WND *wnd, EDITSTATE *es);
172 static LPWSTR   EDIT_GetPasswordPointer_SL(EDITSTATE *es);
173 static void     EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
174 static void     EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
175 static void     EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
176 static void     EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
177 /*
178  *      Helper functions valid for both single line _and_ multi line controls
179  */
180 static INT      EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
181 static INT      EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
182 static void     EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
183 static void     EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
184 static void     EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end);
185 static void     EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
186 static BOOL     EDIT_MakeFit(WND *wnd, EDITSTATE *es, UINT size);
187 static BOOL     EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
188 static void     EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend);
189 static void     EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend);
190 static void     EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend);
191 static void     EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend);
192 static void     EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend);
193 static void     EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend);
194 static void     EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
195 static INT      EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
196 static void     EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos, BOOL after_wrap); 
197 static void     EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT lprc);
198 static void     EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force);
199 static void     EDIT_UpdateScrollInfo(WND *wnd, EDITSTATE *es);
200 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
201 /*
202  *      EM_XXX message handlers
203  */
204 static LRESULT  EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y);
205 static BOOL     EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
206 static HLOCAL   EDIT_EM_GetHandle(EDITSTATE *es);
207 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
208 static INT      EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR lpch);
209 static LRESULT  EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end);
210 static LRESULT  EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
211 static INT      EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
212 static INT      EDIT_EM_LineIndex(EDITSTATE *es, INT line);
213 static INT      EDIT_EM_LineLength(EDITSTATE *es, INT index);
214 static BOOL     EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy);
215 static BOOL     EDIT_EM_LineScroll_internal(WND *wnd, EDITSTATE *es, INT dx, INT dy);
216 static LRESULT  EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap);
217 static void     EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update);
218 static LRESULT  EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action);
219 static void     EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
220 static void     EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc);
221 static void     EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
222 static void     EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
223 static void     EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
224 static void     EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, WCHAR c);
225 static void     EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
226 static BOOL     EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
227 static BOOL     EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
228 static void     EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, LPARAM lParam);
229 static void     EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
230 static BOOL     EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
231 /*
232  *      WM_XXX message handlers
233  */
234 static void     EDIT_WM_Char(WND *wnd, EDITSTATE *es, WCHAR c);
235 static void     EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
236 static void     EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, INT x, INT y);
237 static void     EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
238 static LRESULT  EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCWSTR name);
239 static void     EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
240 static LRESULT  EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc);
241 static INT      EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
242 static LRESULT  EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos);
243 static LRESULT  EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key);
244 static LRESULT  EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es);
245 static LRESULT  EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es);
246 static LRESULT  EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
247 static LRESULT  EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es);
248 static LRESULT  EDIT_WM_MButtonDown(WND *wnd);
249 static LRESULT  EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, INT x, INT y);
250 static LRESULT  EDIT_WM_NCCreate(WND *wnd, DWORD style, HWND hwndParent, BOOL unicode);
251 static void     EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam);
252 static void     EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
253 static void     EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es);
254 static void     EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw);
255 static void     EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPARAM lParam, BOOL unicode);
256 static void     EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height);
257 static LRESULT  EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
258 static void     EDIT_WM_Timer(WND *wnd, EDITSTATE *es);
259 static LRESULT  EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos);
260 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase);
261
262 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
263 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
264
265 /*********************************************************************
266  * edit class descriptor
267  */
268 const struct builtin_class_descr EDIT_builtin_class =
269 {
270     "Edit",               /* name */
271     CS_GLOBALCLASS | CS_DBLCLKS /*| CS_PARENTDC*/,   /* style */
272     EditWndProcA,         /* procA */
273     EditWndProcW,         /* procW */
274     sizeof(EDITSTATE *),  /* extra */
275     IDC_IBEAMA,           /* cursor */
276     0                     /* brush */
277 };
278
279
280 /*********************************************************************
281  *
282  *      EM_CANUNDO
283  *
284  */
285 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
286 {
287         return (es->undo_insert_count || strlenW(es->undo_text));
288 }
289
290
291 /*********************************************************************
292  *
293  *      EM_EMPTYUNDOBUFFER
294  *
295  */
296 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
297 {
298         es->undo_insert_count = 0;
299         *es->undo_text = '\0';
300 }
301
302
303 /*********************************************************************
304  *
305  *      WM_CLEAR
306  *
307  */
308 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
309 {
310         static const WCHAR empty_stringW[] = {0};
311
312         EDIT_EM_ReplaceSel(wnd, es, TRUE, empty_stringW, TRUE);
313
314         if (es->flags & EF_UPDATE) {
315                 es->flags &= ~EF_UPDATE;
316                 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
317         }
318 }
319
320
321 /*********************************************************************
322  *
323  *      WM_CUT
324  *
325  */
326 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
327 {
328         EDIT_WM_Copy(wnd, es);
329         EDIT_WM_Clear(wnd, es);
330 }
331
332
333 /**********************************************************************
334  *         get_app_version
335  *
336  * Returns the window version in case Wine emulates a later version
337  * of windows then the application expects.
338  * 
339  * In a number of cases when windows runs an application that was
340  * designed for an earlier windows version, windows reverts
341  * to "old" behaviour of that earlier version.
342  * 
343  * An example is a disabled  edit control that needs to be painted. 
344  * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was 
345  * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for 
346  * applications with an expected version 0f 4.0 or higher.
347  * 
348  */
349 static DWORD get_app_version(void)
350 {
351     static DWORD version;
352     if (!version)
353     {
354         DWORD dwEmulatedVersion;
355         OSVERSIONINFOW info;
356         DWORD dwProcVersion = GetProcessVersion(0);
357
358         GetVersionExW( &info );
359         dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
360         /* fixme: this may not be 100% correct; see discussion on the
361          * wine developer list in Nov 1999 */
362         version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion; 
363     }
364     return version;
365 }
366
367
368 /*********************************************************************
369  *
370  *      EditWndProc_locked
371  *
372  *      The messages are in the order of the actual integer values
373  *      (which can be found in include/windows.h)
374  *      Whereever possible the 16 bit versions are converted to
375  *      the 32 bit ones, so that we can 'fall through' to the
376  *      helper functions.  These are mostly 32 bit (with a few
377  *      exceptions, clearly indicated by a '16' extension to their
378  *      names).
379  *
380  */
381 static LRESULT WINAPI EditWndProc_locked( WND *wnd, UINT msg,
382                                           WPARAM wParam, LPARAM lParam, BOOL unicode )
383 {
384         EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
385         LRESULT result = 0;
386
387         switch (msg) {
388         case WM_DESTROY:
389                 DPRINTF_EDIT_MSG32("WM_DESTROY");
390                 EDIT_WM_Destroy(wnd, es);
391                 result = 0;
392                 goto END;
393
394         case WM_NCCREATE:
395                 DPRINTF_EDIT_MSG32("WM_NCCREATE");
396                 if(unicode)
397                 {
398                     LPCREATESTRUCTW cs = (LPCREATESTRUCTW)lParam;
399                     result = EDIT_WM_NCCreate(wnd, cs->style, cs->hwndParent, TRUE);
400                 }
401                 else
402                 {
403                     LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
404                     result = EDIT_WM_NCCreate(wnd, cs->style, cs->hwndParent, FALSE);
405                 }
406                 goto END;
407         }
408
409         if (!es)
410         {
411             if(unicode)
412                 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
413             else
414                 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
415             goto END;
416         }
417
418
419         EDIT_LockBuffer(wnd, es);
420         switch (msg) {
421         case EM_GETSEL16:
422                 DPRINTF_EDIT_MSG16("EM_GETSEL");
423                 wParam = 0;
424                 lParam = 0;
425                 /* fall through */
426         case EM_GETSEL:
427                 DPRINTF_EDIT_MSG32("EM_GETSEL");
428                 result = EDIT_EM_GetSel(es, (LPUINT)wParam, (LPUINT)lParam);
429                 break;
430
431         case EM_SETSEL16:
432                 DPRINTF_EDIT_MSG16("EM_SETSEL");
433                 if (SLOWORD(lParam) == -1)
434                         EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
435                 else
436                         EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
437                 if (!wParam)
438                         EDIT_EM_ScrollCaret(wnd, es);
439                 result = 1;
440                 break;
441         case EM_SETSEL:
442                 DPRINTF_EDIT_MSG32("EM_SETSEL");
443                 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
444                 EDIT_EM_ScrollCaret(wnd, es);
445                 result = 1;
446                 break;
447
448         case EM_GETRECT16:
449                 DPRINTF_EDIT_MSG16("EM_GETRECT");
450                 if (lParam)
451                         CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
452                 break;
453         case EM_GETRECT:
454                 DPRINTF_EDIT_MSG32("EM_GETRECT");
455                 if (lParam)
456                         CopyRect((LPRECT)lParam, &es->format_rect);
457                 break;
458
459         case EM_SETRECT16:
460                 DPRINTF_EDIT_MSG16("EM_SETRECT");
461                 if ((es->style & ES_MULTILINE) && lParam) {
462                         RECT rc;
463                         CONV_RECT16TO32(MapSL(lParam), &rc);
464                         EDIT_SetRectNP(wnd, es, &rc);
465                         EDIT_UpdateText(wnd, NULL, TRUE);
466                 }
467                 break;
468         case EM_SETRECT:
469                 DPRINTF_EDIT_MSG32("EM_SETRECT");
470                 if ((es->style & ES_MULTILINE) && lParam) {
471                         EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
472                         EDIT_UpdateText(wnd, NULL, TRUE);
473                 }
474                 break;
475
476         case EM_SETRECTNP16:
477                 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
478                 if ((es->style & ES_MULTILINE) && lParam) {
479                         RECT rc;
480                         CONV_RECT16TO32(MapSL(lParam), &rc);
481                         EDIT_SetRectNP(wnd, es, &rc);
482                 }
483                 break;
484         case EM_SETRECTNP:
485                 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
486                 if ((es->style & ES_MULTILINE) && lParam)
487                         EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
488                 break;
489
490         case EM_SCROLL16:
491                 DPRINTF_EDIT_MSG16("EM_SCROLL");
492                 /* fall through */
493         case EM_SCROLL:
494                 DPRINTF_EDIT_MSG32("EM_SCROLL");
495                 result = EDIT_EM_Scroll(wnd, es, (INT)wParam);
496                 break;
497
498         case EM_LINESCROLL16:
499                 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
500                 wParam = (WPARAM)(INT)SHIWORD(lParam);
501                 lParam = (LPARAM)(INT)SLOWORD(lParam);
502                 /* fall through */
503         case EM_LINESCROLL:
504                 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
505                 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT)wParam, (INT)lParam);
506                 break;
507
508         case EM_SCROLLCARET16:
509                 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
510                 /* fall through */
511         case EM_SCROLLCARET:
512                 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
513                 EDIT_EM_ScrollCaret(wnd, es);
514                 result = 1;
515                 break;
516
517         case EM_GETMODIFY16:
518                 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
519                 /* fall through */
520         case EM_GETMODIFY:
521                 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
522                 result = ((es->flags & EF_MODIFIED) != 0);
523                 break;
524
525         case EM_SETMODIFY16:
526                 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
527                 /* fall through */
528         case EM_SETMODIFY:
529                 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
530                 if (wParam)
531                         es->flags |= EF_MODIFIED;
532                 else
533                         es->flags &= ~(EF_MODIFIED | EF_UPDATE);  /* reset pending updates */
534                 break;
535
536         case EM_GETLINECOUNT16:
537                 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
538                 /* fall through */
539         case EM_GETLINECOUNT:
540                 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
541                 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
542                 break;
543
544         case EM_LINEINDEX16:
545                 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
546                 if ((INT16)wParam == -1)
547                         wParam = (WPARAM)-1;
548                 /* fall through */
549         case EM_LINEINDEX:
550                 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
551                 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
552                 break;
553
554         case EM_SETHANDLE16:
555                 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
556                 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
557                 break;
558         case EM_SETHANDLE:
559                 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
560                 EDIT_EM_SetHandle(wnd, es, (HLOCAL)wParam);
561                 break;
562
563         case EM_GETHANDLE16:
564                 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
565                 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
566                 break;
567         case EM_GETHANDLE:
568                 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
569                 result = (LRESULT)EDIT_EM_GetHandle(es);
570                 break;
571
572         case EM_GETTHUMB16:
573                 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
574                 /* fall through */
575         case EM_GETTHUMB:
576                 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
577                 result = EDIT_EM_GetThumb(wnd, es);
578                 break;
579
580         /* messages 0x00bf and 0x00c0 missing from specs */
581
582         case WM_USER+15:
583                 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
584                 /* fall through */
585         case 0x00bf:
586                 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
587                 if(unicode)
588                     result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
589                 else
590                     result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
591                 break;
592
593         case WM_USER+16:
594                 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
595                 /* fall through */
596         case 0x00c0:
597                 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
598                 if(unicode)
599                     result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
600                 else
601                     result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
602                 break;
603
604         case EM_LINELENGTH16:
605                 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
606                 /* fall through */
607         case EM_LINELENGTH:
608                 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
609                 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
610                 break;
611
612         case EM_REPLACESEL16:
613                 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
614                 lParam = (LPARAM)MapSL(lParam);
615                 /* fall through */
616         case EM_REPLACESEL:
617         {
618                 LPWSTR textW;
619                 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
620
621                 if(unicode)
622                     textW = (LPWSTR)lParam;
623                 else
624                 {
625                     LPSTR textA = (LPSTR)lParam;
626                     INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
627                     if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
628                         MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
629                 }
630
631                 EDIT_EM_ReplaceSel(wnd, es, (BOOL)wParam, textW, TRUE);
632                 if (es->flags & EF_UPDATE) {
633                         es->flags &= ~EF_UPDATE;
634                         EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
635                 }
636                 result = 1;
637
638                 if(!unicode)
639                     HeapFree(GetProcessHeap(), 0, textW);
640                 break;
641         }
642         /* message 0x00c3 missing from specs */
643
644         case WM_USER+19:
645                 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
646                 /* fall through */
647         case 0x00c3:
648                 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
649                 if(unicode)
650                     result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
651                 else
652                     result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
653                 break;
654
655         case EM_GETLINE16:
656                 DPRINTF_EDIT_MSG16("EM_GETLINE");
657                 lParam = (LPARAM)MapSL(lParam);
658                 /* fall through */
659         case EM_GETLINE:
660                 DPRINTF_EDIT_MSG32("EM_GETLINE");
661                 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam);
662                 break;
663
664         case EM_LIMITTEXT16:
665                 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
666                 /* fall through */
667         case EM_SETLIMITTEXT:
668                 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
669                 EDIT_EM_SetLimitText(es, (INT)wParam);
670                 break;
671
672         case EM_CANUNDO16:
673                 DPRINTF_EDIT_MSG16("EM_CANUNDO");
674                 /* fall through */
675         case EM_CANUNDO:
676                 DPRINTF_EDIT_MSG32("EM_CANUNDO");
677                 result = (LRESULT)EDIT_EM_CanUndo(es);
678                 break;
679
680         case EM_UNDO16:
681                 DPRINTF_EDIT_MSG16("EM_UNDO");
682                 /* fall through */
683         case EM_UNDO:
684                 /* fall through */
685         case WM_UNDO:
686                 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
687                 result = (LRESULT)EDIT_EM_Undo(wnd, es);
688                 break;
689
690         case EM_FMTLINES16:
691                 DPRINTF_EDIT_MSG16("EM_FMTLINES");
692                 /* fall through */
693         case EM_FMTLINES:
694                 DPRINTF_EDIT_MSG32("EM_FMTLINES");
695                 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
696                 break;
697
698         case EM_LINEFROMCHAR16:
699                 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
700                 /* fall through */
701         case EM_LINEFROMCHAR:
702                 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
703                 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
704                 break;
705
706         /* message 0x00ca missing from specs */
707
708         case WM_USER+26:
709                 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
710                 /* fall through */
711         case 0x00ca:
712                 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
713                 if(unicode)
714                     result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
715                 else
716                     result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
717                 break;
718
719         case EM_SETTABSTOPS16:
720                 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
721                 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
722                 break;
723         case EM_SETTABSTOPS:
724                 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
725                 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
726                 break;
727
728         case EM_SETPASSWORDCHAR16:
729                 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
730                 /* fall through */
731         case EM_SETPASSWORDCHAR:
732         {
733                 WCHAR charW = 0;
734                 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
735
736                 if(unicode)
737                     charW = (WCHAR)wParam;
738                 else
739                 {
740                     CHAR charA = wParam;
741                     MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
742                 }
743
744                 EDIT_EM_SetPasswordChar(wnd, es, charW);
745                 break;
746         }
747
748         case EM_EMPTYUNDOBUFFER16:
749                 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
750                 /* fall through */
751         case EM_EMPTYUNDOBUFFER:
752                 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
753                 EDIT_EM_EmptyUndoBuffer(es);
754                 break;
755
756         case EM_GETFIRSTVISIBLELINE16:
757                 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
758                 result = es->y_offset;
759                 break;
760         case EM_GETFIRSTVISIBLELINE:
761                 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
762                 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
763                 break;
764
765         case EM_SETREADONLY16:
766                 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
767                 /* fall through */
768         case EM_SETREADONLY:
769                 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
770                 if (wParam) {
771                         wnd->dwStyle |= ES_READONLY;
772                         es->style |= ES_READONLY;
773                 } else {
774                         wnd->dwStyle &= ~ES_READONLY;
775                         es->style &= ~ES_READONLY;
776                 }
777                 result = 1;
778                 break;
779
780         case EM_SETWORDBREAKPROC16:
781                 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
782                 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
783                 break;
784         case EM_SETWORDBREAKPROC:
785                 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
786                 EDIT_EM_SetWordBreakProc(wnd, es, lParam);
787                 break;
788
789         case EM_GETWORDBREAKPROC16:
790                 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
791                 result = (LRESULT)es->word_break_proc16;
792                 break;
793         case EM_GETWORDBREAKPROC:
794                 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
795                 result = (LRESULT)es->word_break_proc;
796                 break;
797
798         case EM_GETPASSWORDCHAR16:
799                 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
800                 /* fall through */
801         case EM_GETPASSWORDCHAR:
802         {
803                 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
804
805                 if(unicode)
806                     result = es->password_char;
807                 else
808                 {
809                     WCHAR charW = es->password_char;
810                     CHAR charA = 0;
811                     WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
812                     result = charA;
813                 }
814                 break;
815         }
816
817         /* The following EM_xxx are new to win95 and don't exist for 16 bit */
818
819         case EM_SETMARGINS:
820                 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
821                 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
822                 break;
823
824         case EM_GETMARGINS:
825                 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
826                 result = MAKELONG(es->left_margin, es->right_margin);
827                 break;
828
829         case EM_GETLIMITTEXT:
830                 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
831                 result = es->buffer_limit;
832                 break;
833
834         case EM_POSFROMCHAR:
835                 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
836                 result = EDIT_EM_PosFromChar(wnd, es, (INT)wParam, FALSE);
837                 break;
838
839         case EM_CHARFROMPOS:
840                 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
841                 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
842                 break;
843
844         case WM_GETDLGCODE:
845                 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
846                 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
847
848                 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
849                 {
850                    int vk = (int)((LPMSG)lParam)->wParam;
851
852                    if ((wnd->dwStyle & ES_WANTRETURN) && vk == VK_RETURN)
853                    {
854                       result |= DLGC_WANTMESSAGE;
855                    }
856                    else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
857                    {
858                       if (SendMessageW(wnd->parent->hwndSelf, CB_GETDROPPEDSTATE, 0, 0))
859                          result |= DLGC_WANTMESSAGE;
860                    }
861                 }
862                 break;
863
864         case WM_CHAR:
865         {
866                 WCHAR charW;
867                 DPRINTF_EDIT_MSG32("WM_CHAR");
868
869                 if(unicode)
870                     charW = wParam;
871                 else
872                 {
873                     CHAR charA = wParam;
874                     MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
875                 }
876
877                 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
878                 {
879                    if (SendMessageW(wnd->parent->hwndSelf, CB_GETDROPPEDSTATE, 0, 0))
880                       SendMessageW(wnd->parent->hwndSelf, WM_KEYDOWN, charW, 0);
881                    break;
882                 }
883                 EDIT_WM_Char(wnd, es, charW);
884                 break;
885         }
886
887         case WM_CLEAR:
888                 DPRINTF_EDIT_MSG32("WM_CLEAR");
889                 EDIT_WM_Clear(wnd, es);
890                 break;
891
892         case WM_COMMAND:
893                 DPRINTF_EDIT_MSG32("WM_COMMAND");
894                 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
895                 break;
896
897         case WM_CONTEXTMENU:
898                 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
899                 EDIT_WM_ContextMenu(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
900                 break;
901
902         case WM_COPY:
903                 DPRINTF_EDIT_MSG32("WM_COPY");
904                 EDIT_WM_Copy(wnd, es);
905                 break;
906
907         case WM_CREATE:
908                 DPRINTF_EDIT_MSG32("WM_CREATE");
909                 if(unicode)
910                     result = EDIT_WM_Create(wnd, es, ((LPCREATESTRUCTW)lParam)->lpszName);
911                 else
912                 {
913                     LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
914                     LPWSTR nameW = NULL;
915                     if(nameA)
916                     {
917                         INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
918                         if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
919                             MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
920                     }
921                     result = EDIT_WM_Create(wnd, es, nameW);
922                     if(nameW)
923                         HeapFree(GetProcessHeap(), 0, nameW);
924                 }
925                 break;
926
927         case WM_CUT:
928                 DPRINTF_EDIT_MSG32("WM_CUT");
929                 EDIT_WM_Cut(wnd, es);
930                 break;
931
932         case WM_ENABLE:
933                 DPRINTF_EDIT_MSG32("WM_ENABLE");
934                 es->bEnableState = (BOOL) wParam;
935                 EDIT_UpdateText(wnd, NULL, TRUE);
936                 break;
937
938         case WM_ERASEBKGND:
939                 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
940                 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC)wParam);
941                 break;
942
943         case WM_GETFONT:
944                 DPRINTF_EDIT_MSG32("WM_GETFONT");
945                 result = (LRESULT)es->font;
946                 break;
947
948         case WM_GETTEXT:
949                 DPRINTF_EDIT_MSG32("WM_GETTEXT");
950                 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
951                 break;
952
953         case WM_GETTEXTLENGTH:
954                 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
955                 result = strlenW(es->text);
956                 break;
957
958         case WM_HSCROLL:
959                 DPRINTF_EDIT_MSG32("WM_HSCROLL");
960                 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam));
961                 break;
962
963         case WM_KEYDOWN:
964                 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
965                 result = EDIT_WM_KeyDown(wnd, es, (INT)wParam);
966                 break;
967
968         case WM_KILLFOCUS:
969                 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
970                 result = EDIT_WM_KillFocus(wnd, es);
971                 break;
972
973         case WM_LBUTTONDBLCLK:
974                 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
975                 result = EDIT_WM_LButtonDblClk(wnd, es);
976                 break;
977
978         case WM_LBUTTONDOWN:
979                 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
980                 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
981                 break;
982
983         case WM_LBUTTONUP:
984                 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
985                 result = EDIT_WM_LButtonUp(wnd->hwndSelf, es);
986                 break;
987
988         case WM_MBUTTONDOWN:                        
989                 DPRINTF_EDIT_MSG32("WM_MBUTTONDOWN");    
990                 result = EDIT_WM_MButtonDown(wnd);
991                 break;
992
993         case WM_MOUSEACTIVATE:
994                 /*
995                  *      FIXME: maybe DefWindowProc() screws up, but it seems that
996                  *              modeless dialog boxes need this.  If we don't do this, the focus
997                  *              will _not_ be set by DefWindowProc() for edit controls in a
998                  *              modeless dialog box ???
999                  */
1000                 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
1001                 SetFocus(wnd->hwndSelf);
1002                 result = MA_ACTIVATE;
1003                 break;
1004
1005         case WM_MOUSEMOVE:
1006                 /*
1007                  *      DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
1008                  */
1009                 result = EDIT_WM_MouseMove(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
1010                 break;
1011
1012         case WM_PAINT:
1013                 DPRINTF_EDIT_MSG32("WM_PAINT");
1014                 EDIT_WM_Paint(wnd, es, wParam);
1015                 break;
1016
1017         case WM_PASTE:
1018                 DPRINTF_EDIT_MSG32("WM_PASTE");
1019                 EDIT_WM_Paste(wnd, es);
1020                 break;
1021
1022         case WM_SETFOCUS:
1023                 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
1024                 EDIT_WM_SetFocus(wnd, es);
1025                 break;
1026
1027         case WM_SETFONT:
1028                 DPRINTF_EDIT_MSG32("WM_SETFONT");
1029                 EDIT_WM_SetFont(wnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
1030                 break;
1031
1032         case WM_SETTEXT:
1033                 DPRINTF_EDIT_MSG32("WM_SETTEXT");
1034                 EDIT_WM_SetText(wnd, es, lParam, unicode);
1035                 result = TRUE;
1036                 break;
1037
1038         case WM_SIZE:
1039                 DPRINTF_EDIT_MSG32("WM_SIZE");
1040                 EDIT_WM_Size(wnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
1041                 break;
1042
1043         case WM_SYSKEYDOWN:
1044                 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
1045                 result = EDIT_WM_SysKeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
1046                 break;
1047
1048         case WM_TIMER:
1049                 DPRINTF_EDIT_MSG32("WM_TIMER");
1050                 EDIT_WM_Timer(wnd, es);
1051                 break;
1052
1053         case WM_VSCROLL:
1054                 DPRINTF_EDIT_MSG32("WM_VSCROLL");
1055                 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam));
1056                 break;
1057
1058         case WM_MOUSEWHEEL:
1059                 {
1060                     int gcWheelDelta = 0;
1061                     UINT pulScrollLines = 3;
1062                     SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1063
1064                     if (wParam & (MK_SHIFT | MK_CONTROL)) {
1065                         if(unicode)
1066                             result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
1067                         else
1068                             result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
1069                         break;
1070                     }
1071                     gcWheelDelta -= SHIWORD(wParam);
1072                     if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1073                     {
1074                         int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1075                         cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1076                         result = EDIT_EM_LineScroll(wnd, es, 0, cLineScroll);
1077                     }
1078                 }
1079                 break;
1080         default:
1081                 if(unicode)
1082                     result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
1083                 else
1084                     result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
1085                 break;
1086         }
1087         EDIT_UnlockBuffer(wnd, es, FALSE);
1088     END:
1089         return result;
1090 }
1091
1092 /*********************************************************************
1093  *
1094  *      EditWndProcW   (USER32.@)
1095  */
1096 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1097 {
1098     LRESULT res;
1099     WND *wndPtr = WIN_FindWndPtr(hWnd);
1100
1101     res = EditWndProc_locked(wndPtr, uMsg, wParam, lParam, TRUE);
1102
1103     WIN_ReleaseWndPtr(wndPtr);
1104     return res;
1105 }
1106
1107 /*********************************************************************
1108  *
1109  *      EditWndProcA   (USER32.@)
1110  */
1111 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1112 {
1113     LRESULT res;
1114     WND *wndPtr = WIN_FindWndPtr(hWnd);
1115
1116     res = EditWndProc_locked(wndPtr, uMsg, wParam, lParam, FALSE);
1117
1118     WIN_ReleaseWndPtr(wndPtr);
1119     return res;
1120 }
1121
1122 /*********************************************************************
1123  *
1124  *      EDIT_BuildLineDefs_ML
1125  *
1126  *      Build linked list of text lines.
1127  *      Lines can end with '\0' (last line), a character (if it is wrapped),
1128  *      a soft return '\r\r\n' or a hard return '\r\n'
1129  *
1130  */
1131 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
1132 {
1133         HDC dc;
1134         HFONT old_font = 0;
1135         LPWSTR start, cp;
1136         INT fw;
1137         LINEDEF *current_def;
1138         LINEDEF **previous_next;
1139
1140         current_def = es->first_line_def;
1141         do {
1142                 LINEDEF *next_def = current_def->next;
1143                 HeapFree(GetProcessHeap(), 0, current_def);
1144                 current_def = next_def;
1145         } while (current_def);
1146         es->line_count = 0;
1147         es->text_width = 0;
1148
1149         dc = GetDC(wnd->hwndSelf);
1150         if (es->font)
1151                 old_font = SelectObject(dc, es->font);
1152
1153         fw = es->format_rect.right - es->format_rect.left;
1154         start = es->text;
1155         previous_next = &es->first_line_def;
1156         do {
1157                 current_def = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1158                 current_def->next = NULL;
1159                 cp = start;
1160                 while (*cp) {
1161                         if ((*cp == '\r') && (*(cp + 1) == '\n'))
1162                                 break;
1163                         cp++;
1164                 }
1165                 if (!(*cp)) {
1166                         current_def->ending = END_0;
1167                         current_def->net_length = strlenW(start);
1168                 } else if ((cp > start) && (*(cp - 1) == '\r')) {
1169                         current_def->ending = END_SOFT;
1170                         current_def->net_length = cp - start - 1;
1171                 } else {
1172                         current_def->ending = END_HARD;
1173                         current_def->net_length = cp - start;
1174                 }
1175                 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1176                                         start, current_def->net_length,
1177                                         es->tabs_count, es->tabs));
1178                 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1179                 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
1180                         INT next = 0;
1181                         INT prev;
1182                         do {
1183                                 prev = next;
1184                                 next = EDIT_CallWordBreakProc(es, start - es->text,
1185                                                 prev + 1, current_def->net_length, WB_RIGHT);
1186                                 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1187                                                         start, next, es->tabs_count, es->tabs));
1188                         } while (current_def->width <= fw);
1189                         if (!prev) {
1190                                 next = 0;
1191                                 do {
1192                                         prev = next;
1193                                         next++;
1194                                         current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1195                                                                 start, next, es->tabs_count, es->tabs));
1196                                 } while (current_def->width <= fw);
1197                                 if (!prev)
1198                                         prev = 1;
1199                         }
1200                         current_def->net_length = prev;
1201                         current_def->ending = END_WRAP;
1202                         current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc, start,
1203                                                 current_def->net_length, es->tabs_count, es->tabs));
1204                 }
1205                 switch (current_def->ending) {
1206                 case END_SOFT:
1207                         current_def->length = current_def->net_length + 3;
1208                         break;
1209                 case END_HARD:
1210                         current_def->length = current_def->net_length + 2;
1211                         break;
1212                 case END_WRAP:
1213                 case END_0:
1214                         current_def->length = current_def->net_length;
1215                         break;
1216                 }
1217                 es->text_width = max(es->text_width, current_def->width);
1218                 start += current_def->length;
1219                 *previous_next = current_def;
1220                 previous_next = &current_def->next;
1221                 es->line_count++;
1222         } while (current_def->ending != END_0);
1223         if (es->font)
1224                 SelectObject(dc, old_font);
1225         ReleaseDC(wnd->hwndSelf, dc);
1226 }
1227
1228 /*********************************************************************
1229  *
1230  *      EDIT_CalcLineWidth_SL
1231  *
1232  */
1233 static void EDIT_CalcLineWidth_SL(WND *wnd, EDITSTATE *es)
1234 {
1235     es->text_width = SLOWORD(EDIT_EM_PosFromChar(wnd, es, strlenW(es->text), FALSE));
1236 }
1237
1238 /*********************************************************************
1239  *
1240  *      EDIT_CallWordBreakProc
1241  *
1242  *      Call appropriate WordBreakProc (internal or external).
1243  *
1244  *      Note: The "start" argument should always be an index refering
1245  *              to es->text.  The actual wordbreak proc might be
1246  *              16 bit, so we can't always pass any 32 bit LPSTR.
1247  *              Hence we assume that es->text is the buffer that holds
1248  *              the string under examination (we can decide this for ourselves).
1249  *
1250  */
1251 /* ### start build ### */
1252 extern WORD CALLBACK EDIT_CallTo16_word_lwww(EDITWORDBREAKPROC16,SEGPTR,WORD,WORD,WORD);
1253 /* ### stop build ### */
1254 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1255 {
1256     INT ret, iWndsLocks;
1257
1258     /* To avoid any deadlocks, all the locks on the windows structures
1259        must be suspended before the control is passed to the application */
1260     iWndsLocks = WIN_SuspendWndsLock();
1261
1262         if (es->word_break_proc16) {
1263             HGLOBAL16 hglob16;
1264             SEGPTR segptr;
1265             INT countA;
1266
1267             countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1268             hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1269             segptr = K32WOWGlobalLock16(hglob16);
1270             WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1271             ret = (INT)EDIT_CallTo16_word_lwww(es->word_break_proc16,
1272                                                 segptr, index, countA, action);
1273             GlobalUnlock16(hglob16);
1274             GlobalFree16(hglob16);
1275         }
1276         else if (es->word_break_proc)
1277         {
1278             if(es->is_unicode)
1279             {
1280                 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1281
1282                 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1283                         es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1284                 ret = wbpW(es->text + start, index, count, action);
1285             }
1286             else
1287             {
1288                 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1289                 INT countA;
1290                 CHAR *textA;
1291
1292                 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1293                 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1294                 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1295                 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1296                         es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1297                 ret = wbpA(textA, index, countA, action);
1298                 HeapFree(GetProcessHeap(), 0, textA);
1299             }
1300         }
1301         else
1302             ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1303
1304     WIN_RestoreWndsLock(iWndsLocks);
1305     return ret;
1306 }
1307
1308
1309 /*********************************************************************
1310  *
1311  *      EDIT_CharFromPos
1312  *
1313  *      Beware: This is not the function called on EM_CHARFROMPOS
1314  *              The position _can_ be outside the formatting / client
1315  *              rectangle
1316  *              The return value is only the character index
1317  *
1318  */
1319 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1320 {
1321         INT index;
1322         HDC dc;
1323         HFONT old_font = 0;
1324
1325         if (es->style & ES_MULTILINE) {
1326                 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1327                 INT line_index = 0;
1328                 LINEDEF *line_def = es->first_line_def;
1329                 INT low, high;
1330                 while ((line > 0) && line_def->next) {
1331                         line_index += line_def->length;
1332                         line_def = line_def->next;
1333                         line--;
1334                 }
1335                 x += es->x_offset - es->format_rect.left;
1336                 if (x >= line_def->width) {
1337                         if (after_wrap)
1338                                 *after_wrap = (line_def->ending == END_WRAP);
1339                         return line_index + line_def->net_length;
1340                 }
1341                 if (x <= 0) {
1342                         if (after_wrap)
1343                                 *after_wrap = FALSE;
1344                         return line_index;
1345                 }
1346                 dc = GetDC(wnd->hwndSelf);
1347                 if (es->font)
1348                         old_font = SelectObject(dc, es->font);
1349                     low = line_index + 1;
1350                     high = line_index + line_def->net_length + 1;
1351                     while (low < high - 1)
1352                     {
1353                         INT mid = (low + high) / 2;
1354                         if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1355                         else low = mid;
1356                     }
1357                     index = low;
1358
1359                 if (after_wrap)
1360                         *after_wrap = ((index == line_index + line_def->net_length) &&
1361                                                         (line_def->ending == END_WRAP));
1362         } else {
1363                 LPWSTR text;
1364                 SIZE size;
1365                 if (after_wrap)
1366                         *after_wrap = FALSE;
1367                 x -= es->format_rect.left;
1368                 if (!x)
1369                         return es->x_offset;
1370                 text = EDIT_GetPasswordPointer_SL(es);
1371                 dc = GetDC(wnd->hwndSelf);
1372                 if (es->font)
1373                         old_font = SelectObject(dc, es->font);
1374                 if (x < 0)
1375                 {
1376                     INT low = 0;
1377                     INT high = es->x_offset;
1378                     while (low < high - 1)
1379                     {
1380                         INT mid = (low + high) / 2;
1381                         GetTextExtentPoint32W( dc, text + mid,
1382                                                es->x_offset - mid, &size );
1383                         if (size.cx > -x) low = mid;
1384                         else high = mid;
1385                     }
1386                     index = low;
1387                 }
1388                 else
1389                 {
1390                     INT low = es->x_offset;
1391                     INT high = strlenW(es->text) + 1;
1392                     while (low < high - 1)
1393                     {
1394                         INT mid = (low + high) / 2;
1395                         GetTextExtentPoint32W( dc, text + es->x_offset,
1396                                                mid - es->x_offset, &size );
1397                         if (size.cx > x) high = mid;
1398                         else low = mid;
1399                     }
1400                     index = low;
1401                 }
1402                 if (es->style & ES_PASSWORD)
1403                         HeapFree(GetProcessHeap(), 0, text);
1404         }
1405         if (es->font)
1406                 SelectObject(dc, old_font);
1407         ReleaseDC(wnd->hwndSelf, dc);
1408         return index;
1409 }
1410
1411
1412 /*********************************************************************
1413  *
1414  *      EDIT_ConfinePoint
1415  *
1416  *      adjusts the point to be within the formatting rectangle
1417  *      (so CharFromPos returns the nearest _visible_ character)
1418  *
1419  */
1420 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1421 {
1422         *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1423         *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1424 }
1425
1426
1427 /*********************************************************************
1428  *
1429  *      EDIT_GetLineRect
1430  *
1431  *      Calculates the bounding rectangle for a line from a starting
1432  *      column to an ending column.
1433  *
1434  */
1435 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1436 {
1437         INT line_index =  EDIT_EM_LineIndex(es, line);
1438
1439         if (es->style & ES_MULTILINE)
1440                 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1441         else
1442                 rc->top = es->format_rect.top;
1443         rc->bottom = rc->top + es->line_height;
1444         rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1445         rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1446 }
1447
1448
1449 /*********************************************************************
1450  *
1451  *      EDIT_GetPasswordPointer_SL
1452  *
1453  *      note: caller should free the (optionally) allocated buffer
1454  *
1455  */
1456 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1457 {
1458         if (es->style & ES_PASSWORD) {
1459                 INT len = strlenW(es->text);
1460                 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1461                 text[len] = '\0';
1462                 while(len) text[--len] = es->password_char;
1463                 return text;
1464         } else
1465                 return es->text;
1466 }
1467
1468
1469 /*********************************************************************
1470  *
1471  *      EDIT_LockBuffer
1472  *
1473  *      This acts as a LOCAL_Lock(), but it locks only once.  This way
1474  *      you can call it whenever you like, without unlocking.
1475  *
1476  */
1477 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1478 {
1479         if (!es) {
1480                 ERR("no EDITSTATE ... please report\n");
1481                 return;
1482         }
1483         if (!es->text) {
1484             CHAR *textA = NULL;
1485             INT countA = 0;
1486             BOOL _16bit = FALSE;
1487
1488             if(es->hloc32W)
1489             {
1490                 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1491                 es->text = LocalLock(es->hloc32W);
1492
1493                 if(es->hloc32A)
1494                 {
1495                     TRACE("Synchronizing with 32-bit ANSI buffer\n");
1496                     countA = LocalSize(es->hloc32A);
1497                     textA = LocalLock(es->hloc32A);
1498                 }
1499                 else if(es->hloc16)
1500                 {
1501                     TRACE("Synchronizing with 16-bit ANSI buffer\n");
1502                     countA = LOCAL_Size(wnd->hInstance, es->hloc16);
1503                     textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
1504                     _16bit = TRUE;
1505                 }
1506             }
1507             else {
1508                 ERR("no buffer ... please report\n");
1509                 return;
1510             }
1511
1512             if(textA)
1513             {
1514                 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size);
1515                 if(_16bit)
1516                     LOCAL_Unlock(wnd->hInstance, es->hloc16);
1517                 else
1518                     LocalUnlock(es->hloc32A);
1519             }
1520         }
1521         es->lock_count++;
1522 }
1523
1524
1525 /*********************************************************************
1526  *
1527  *      EDIT_SL_InvalidateText
1528  *
1529  *      Called from EDIT_InvalidateText().
1530  *      Does the job for single-line controls only.
1531  *
1532  */
1533 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1534 {
1535         RECT line_rect;
1536         RECT rc;
1537
1538         EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1539         if (IntersectRect(&rc, &line_rect, &es->format_rect))
1540                 EDIT_UpdateText(wnd, &rc, FALSE);
1541 }
1542
1543
1544 /*********************************************************************
1545  *
1546  *      EDIT_ML_InvalidateText
1547  *
1548  *      Called from EDIT_InvalidateText().
1549  *      Does the job for multi-line controls only.
1550  *
1551  */
1552 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1553 {
1554         INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1555         INT sl = EDIT_EM_LineFromChar(es, start);
1556         INT el = EDIT_EM_LineFromChar(es, end);
1557         INT sc;
1558         INT ec;
1559         RECT rc1;
1560         RECT rcWnd;
1561         RECT rcLine;
1562         RECT rcUpdate;
1563         INT l;
1564
1565         if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1566                 return;
1567
1568         sc = start - EDIT_EM_LineIndex(es, sl);
1569         ec = end - EDIT_EM_LineIndex(es, el);
1570         if (sl < es->y_offset) {
1571                 sl = es->y_offset;
1572                 sc = 0;
1573         }
1574         if (el > es->y_offset + vlc) {
1575                 el = es->y_offset + vlc;
1576                 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1577         }
1578         GetClientRect(wnd->hwndSelf, &rc1);
1579         IntersectRect(&rcWnd, &rc1, &es->format_rect);
1580         if (sl == el) {
1581                 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1582                 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1583                         EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1584         } else {
1585                 EDIT_GetLineRect(wnd, es, sl, sc,
1586                                 EDIT_EM_LineLength(es,
1587                                         EDIT_EM_LineIndex(es, sl)),
1588                                 &rcLine);
1589                 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1590                         EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1591                 for (l = sl + 1 ; l < el ; l++) {
1592                         EDIT_GetLineRect(wnd, es, l, 0,
1593                                 EDIT_EM_LineLength(es,
1594                                         EDIT_EM_LineIndex(es, l)),
1595                                 &rcLine);
1596                         if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1597                                 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1598                 }
1599                 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1600                 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1601                         EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1602         }
1603 }
1604
1605
1606 /*********************************************************************
1607  *
1608  *      EDIT_InvalidateText
1609  *
1610  *      Invalidate the text from offset start upto, but not including,
1611  *      offset end.  Useful for (re)painting the selection.
1612  *      Regions outside the linewidth are not invalidated.
1613  *      end == -1 means end == TextLength.
1614  *      start and end need not be ordered.
1615  *
1616  */
1617 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1618 {
1619         if (end == start)
1620                 return;
1621
1622         if (end == -1)
1623                 end = strlenW(es->text);
1624
1625         ORDER_INT(start, end);
1626
1627         if (es->style & ES_MULTILINE)
1628                 EDIT_ML_InvalidateText(wnd, es, start, end);
1629         else
1630                 EDIT_SL_InvalidateText(wnd, es, start, end);
1631 }
1632
1633
1634 /*********************************************************************
1635  *
1636  *      EDIT_MakeFit
1637  *
1638  *      Try to fit size + 1 characters in the buffer. Constrain to limits.
1639  *
1640  */
1641 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, UINT size)
1642 {
1643         HLOCAL hNew32W;
1644
1645         if (size <= es->buffer_size)
1646                 return TRUE;
1647         if (size > es->buffer_limit) {
1648                 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1649                 return FALSE;
1650         }
1651         if (size > es->buffer_limit)
1652                 size = es->buffer_limit;
1653
1654         TRACE("trying to ReAlloc to %d+1 characters\n", size);
1655
1656         /* Force edit to unlock it's buffer. es->text now NULL */
1657         EDIT_UnlockBuffer(wnd, es, TRUE);
1658
1659         if (es->hloc32W) {
1660             UINT alloc_size = ((size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
1661             if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1662                 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32W, hNew32W);
1663                 es->hloc32W = hNew32W;
1664                 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1665             }
1666         }
1667
1668         EDIT_LockBuffer(wnd, es);
1669
1670         if (es->buffer_size < size) {
1671                 WARN("FAILED !  We now have %d+1\n", es->buffer_size);
1672                 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1673                 return FALSE;
1674         } else {
1675                 TRACE("We now have %d+1\n", es->buffer_size);
1676                 return TRUE;
1677         }
1678 }
1679
1680
1681 /*********************************************************************
1682  *
1683  *      EDIT_MakeUndoFit
1684  *
1685  *      Try to fit size + 1 bytes in the undo buffer.
1686  *
1687  */
1688 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1689 {
1690         UINT alloc_size;
1691
1692         if (size <= es->undo_buffer_size)
1693                 return TRUE;
1694
1695         TRACE("trying to ReAlloc to %d+1\n", size);
1696
1697         alloc_size = ((size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
1698         if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1699                 es->undo_buffer_size = alloc_size/sizeof(WCHAR);
1700                 return TRUE;
1701         }
1702         else
1703         {
1704                 WARN("FAILED !  We now have %d+1\n", es->undo_buffer_size);
1705                 return FALSE;
1706         }
1707 }
1708
1709
1710 /*********************************************************************
1711  *
1712  *      EDIT_MoveBackward
1713  *
1714  */
1715 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1716 {
1717         INT e = es->selection_end;
1718
1719         if (e) {
1720                 e--;
1721                 if ((es->style & ES_MULTILINE) && e &&
1722                                 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1723                         e--;
1724                         if (e && (es->text[e - 1] == '\r'))
1725                                 e--;
1726                 }
1727         }
1728         EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1729         EDIT_EM_ScrollCaret(wnd, es);
1730 }
1731
1732
1733 /*********************************************************************
1734  *
1735  *      EDIT_MoveDown_ML
1736  *
1737  *      Only for multi line controls
1738  *      Move the caret one line down, on a column with the nearest
1739  *      x coordinate on the screen (might be a different column).
1740  *
1741  */
1742 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1743 {
1744         INT s = es->selection_start;
1745         INT e = es->selection_end;
1746         BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1747         LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1748         INT x = SLOWORD(pos);
1749         INT y = SHIWORD(pos);
1750
1751         e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1752         if (!extend)
1753                 s = e;
1754         EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1755         EDIT_EM_ScrollCaret(wnd, es);
1756 }
1757
1758
1759 /*********************************************************************
1760  *
1761  *      EDIT_MoveEnd
1762  *
1763  */
1764 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1765 {
1766         BOOL after_wrap = FALSE;
1767         INT e;
1768
1769         /* Pass a high value in x to make sure of receiving the end of the line */
1770         if (es->style & ES_MULTILINE)
1771                 e = EDIT_CharFromPos(wnd, es, 0x3fffffff,
1772                         HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1773         else
1774                 e = strlenW(es->text);
1775         EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1776         EDIT_EM_ScrollCaret(wnd, es);
1777 }
1778
1779
1780 /*********************************************************************
1781  *
1782  *      EDIT_MoveForward
1783  *
1784  */
1785 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1786 {
1787         INT e = es->selection_end;
1788
1789         if (es->text[e]) {
1790                 e++;
1791                 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1792                         if (es->text[e] == '\n')
1793                                 e++;
1794                         else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1795                                 e += 2;
1796                 }
1797         }
1798         EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1799         EDIT_EM_ScrollCaret(wnd, es);
1800 }
1801
1802
1803 /*********************************************************************
1804  *
1805  *      EDIT_MoveHome
1806  *
1807  *      Home key: move to beginning of line.
1808  *
1809  */
1810 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
1811 {
1812         INT e;
1813
1814         /* Pass the x_offset in x to make sure of receiving the first position of the line */
1815         if (es->style & ES_MULTILINE)
1816                 e = EDIT_CharFromPos(wnd, es, -es->x_offset,
1817                         HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1818         else
1819                 e = 0;
1820         EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1821         EDIT_EM_ScrollCaret(wnd, es);
1822 }
1823
1824
1825 /*********************************************************************
1826  *
1827  *      EDIT_MovePageDown_ML
1828  *
1829  *      Only for multi line controls
1830  *      Move the caret one page down, on a column with the nearest
1831  *      x coordinate on the screen (might be a different column).
1832  *
1833  */
1834 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1835 {
1836         INT s = es->selection_start;
1837         INT e = es->selection_end;
1838         BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1839         LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1840         INT x = SLOWORD(pos);
1841         INT y = SHIWORD(pos);
1842
1843         e = EDIT_CharFromPos(wnd, es, x,
1844                 y + (es->format_rect.bottom - es->format_rect.top),
1845                 &after_wrap);
1846         if (!extend)
1847                 s = e;
1848         EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1849         EDIT_EM_ScrollCaret(wnd, es);
1850 }
1851
1852
1853 /*********************************************************************
1854  *
1855  *      EDIT_MovePageUp_ML
1856  *
1857  *      Only for multi line controls
1858  *      Move the caret one page up, on a column with the nearest
1859  *      x coordinate on the screen (might be a different column).
1860  *
1861  */
1862 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1863 {
1864         INT s = es->selection_start;
1865         INT e = es->selection_end;
1866         BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1867         LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1868         INT x = SLOWORD(pos);
1869         INT y = SHIWORD(pos);
1870
1871         e = EDIT_CharFromPos(wnd, es, x,
1872                 y - (es->format_rect.bottom - es->format_rect.top),
1873                 &after_wrap);
1874         if (!extend)
1875                 s = e;
1876         EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1877         EDIT_EM_ScrollCaret(wnd, es);
1878 }
1879
1880
1881 /*********************************************************************
1882  *
1883  *      EDIT_MoveUp_ML
1884  *
1885  *      Only for multi line controls
1886  *      Move the caret one line up, on a column with the nearest
1887  *      x coordinate on the screen (might be a different column).
1888  *
1889  */ 
1890 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1891 {
1892         INT s = es->selection_start;
1893         INT e = es->selection_end;
1894         BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1895         LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1896         INT x = SLOWORD(pos);
1897         INT y = SHIWORD(pos);
1898
1899         e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1900         if (!extend)
1901                 s = e;
1902         EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1903         EDIT_EM_ScrollCaret(wnd, es);
1904 }
1905
1906
1907 /*********************************************************************
1908  *
1909  *      EDIT_MoveWordBackward
1910  *
1911  */
1912 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1913 {
1914         INT s = es->selection_start;
1915         INT e = es->selection_end;
1916         INT l;
1917         INT ll;
1918         INT li;
1919
1920         l = EDIT_EM_LineFromChar(es, e);
1921         ll = EDIT_EM_LineLength(es, e);
1922         li = EDIT_EM_LineIndex(es, l);
1923         if (e - li == 0) {
1924                 if (l) {
1925                         li = EDIT_EM_LineIndex(es, l - 1);
1926                         e = li + EDIT_EM_LineLength(es, li);
1927                 }
1928         } else {
1929                 e = li + (INT)EDIT_CallWordBreakProc(es,
1930                                 li, e - li, ll, WB_LEFT);
1931         }
1932         if (!extend)
1933                 s = e;
1934         EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1935         EDIT_EM_ScrollCaret(wnd, es);
1936 }
1937
1938
1939 /*********************************************************************
1940  *
1941  *      EDIT_MoveWordForward
1942  *
1943  */
1944 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
1945 {
1946         INT s = es->selection_start;
1947         INT e = es->selection_end;
1948         INT l;
1949         INT ll;
1950         INT li;
1951
1952         l = EDIT_EM_LineFromChar(es, e);
1953         ll = EDIT_EM_LineLength(es, e);
1954         li = EDIT_EM_LineIndex(es, l);
1955         if (e - li == ll) {
1956                 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1957                         e = EDIT_EM_LineIndex(es, l + 1);
1958         } else {
1959                 e = li + EDIT_CallWordBreakProc(es,
1960                                 li, e - li + 1, ll, WB_RIGHT);
1961         }
1962         if (!extend)
1963                 s = e;
1964         EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1965         EDIT_EM_ScrollCaret(wnd, es);
1966 }
1967
1968
1969 /*********************************************************************
1970  *
1971  *      EDIT_PaintLine
1972  *
1973  */
1974 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
1975 {
1976         INT s = es->selection_start;
1977         INT e = es->selection_end;
1978         INT li;
1979         INT ll;
1980         INT x;
1981         INT y;
1982         LRESULT pos;
1983
1984         if (es->style & ES_MULTILINE) {
1985                 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1986                 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1987                         return;
1988         } else if (line)
1989                 return;
1990
1991         TRACE("line=%d\n", line);
1992
1993         pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(es, line), FALSE);
1994         x = SLOWORD(pos);
1995         y = SHIWORD(pos);
1996         li = EDIT_EM_LineIndex(es, line);
1997         ll = EDIT_EM_LineLength(es, li);
1998         s = es->selection_start;
1999         e = es->selection_end;
2000         ORDER_INT(s, e);
2001         s = min(li + ll, max(li, s));
2002         e = min(li + ll, max(li, e));
2003         if (rev && (s != e) &&
2004                         ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2005                 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2006                 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2007                 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2008         } else
2009                 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2010 }
2011
2012
2013 /*********************************************************************
2014  *
2015  *      EDIT_PaintText
2016  *
2017  */
2018 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2019 {
2020         COLORREF BkColor;
2021         COLORREF TextColor;
2022         INT ret;
2023         INT li;
2024         SIZE size;
2025
2026         if (!count)
2027                 return 0;
2028         BkColor = GetBkColor(dc);
2029         TextColor = GetTextColor(dc);
2030         if (rev) {
2031                 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2032                 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2033         }
2034         li = EDIT_EM_LineIndex(es, line);
2035         if (es->style & ES_MULTILINE) {
2036                 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2037                                         es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2038         } else {
2039                 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2040                 TextOutW(dc, x, y, text + li + col, count);
2041                 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2042                 ret = size.cx;
2043                 if (es->style & ES_PASSWORD)
2044                         HeapFree(GetProcessHeap(), 0, text);
2045         }
2046         if (rev) {
2047                 SetBkColor(dc, BkColor);
2048                 SetTextColor(dc, TextColor);
2049         }
2050         return ret;
2051 }
2052
2053
2054 /*********************************************************************
2055  *
2056  *      EDIT_SetCaretPos
2057  *
2058  */
2059 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
2060                              BOOL after_wrap)
2061 {
2062         LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
2063         SetCaretPos(SLOWORD(res), SHIWORD(res));
2064 }
2065
2066
2067 /*********************************************************************
2068  *
2069  *      EDIT_SetRectNP
2070  *
2071  *      note:   this is not (exactly) the handler called on EM_SETRECTNP
2072  *              it is also used to set the rect of a single line control
2073  *
2074  */
2075 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
2076 {
2077         CopyRect(&es->format_rect, rc);
2078         if (es->style & WS_BORDER) {
2079                 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2080                 if(TWEAK_WineLook == WIN31_LOOK)
2081                         bw += 2;
2082                 es->format_rect.left += bw;
2083                 es->format_rect.top += bw;
2084                 es->format_rect.right -= bw;
2085                 es->format_rect.bottom -= bw;
2086         }
2087         es->format_rect.left += es->left_margin;
2088         es->format_rect.right -= es->right_margin;
2089         es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2090         if (es->style & ES_MULTILINE)
2091         {
2092             INT fw, vlc, max_x_offset, max_y_offset;
2093
2094             vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2095             es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2096
2097             /* correct es->x_offset */
2098             fw = es->format_rect.right - es->format_rect.left;
2099             max_x_offset = es->text_width - fw;
2100             if(max_x_offset < 0) max_x_offset = 0;
2101             if(es->x_offset > max_x_offset)
2102                 es->x_offset = max_x_offset;
2103
2104             /* correct es->y_offset */
2105             max_y_offset = es->line_count - vlc;
2106             if(max_y_offset < 0) max_y_offset = 0;
2107             if(es->y_offset > max_y_offset)
2108                 es->y_offset = max_y_offset;
2109         }
2110         else
2111         /* Windows doesn't care to fix text placement for SL controls */
2112                 es->format_rect.bottom = es->format_rect.top + es->line_height;
2113
2114         if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2115                 EDIT_BuildLineDefs_ML(wnd, es);
2116 }
2117
2118
2119 /*********************************************************************
2120  *
2121  *      EDIT_UnlockBuffer
2122  *
2123  */
2124 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
2125 {
2126         if (!es) {
2127                 ERR("no EDITSTATE ... please report\n");
2128                 return;
2129         }
2130         if (!es->lock_count) {
2131                 ERR("lock_count == 0 ... please report\n");
2132                 return;
2133         }
2134         if (!es->text) {
2135                 ERR("es->text == 0 ... please report\n");
2136                 return;
2137         }
2138
2139         if (force || (es->lock_count == 1)) {
2140             if (es->hloc32W) {
2141                 CHAR *textA = NULL;
2142                 BOOL _16bit = FALSE;
2143                 INT countA = 0;
2144
2145                 if(es->hloc32A)
2146                 {
2147                     TRACE("Synchronizing with 32-bit ANSI buffer\n");
2148                     countA = LocalSize(es->hloc32A);
2149                     textA = LocalLock(es->hloc32A);
2150                 }
2151                 else if(es->hloc16)
2152                 {
2153                     TRACE("Synchronizing with 16-bit ANSI buffer\n");
2154                     countA = LOCAL_Size(wnd->hInstance, es->hloc16);
2155                     textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
2156                     _16bit = TRUE;
2157                 }
2158
2159                 if(textA)
2160                 {
2161                     WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, countA, NULL, NULL);
2162                     if(_16bit)
2163                         LOCAL_Unlock(wnd->hInstance, es->hloc16);
2164                     else
2165                         LocalUnlock(es->hloc32A);
2166                 }
2167
2168                 LocalUnlock(es->hloc32W);
2169                 es->text = NULL;
2170             }
2171             else {
2172                 ERR("no buffer ... please report\n");
2173                 return;
2174             }
2175         }
2176         es->lock_count--;
2177 }
2178
2179
2180 /*********************************************************************
2181  *
2182  *      EDIT_UpdateScrollInfo
2183  *
2184  */
2185 static void EDIT_UpdateScrollInfo(WND *wnd, EDITSTATE *es)
2186 {
2187     if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2188     {
2189         SCROLLINFO si;
2190         si.cbSize       = sizeof(SCROLLINFO);
2191         si.fMask        = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2192         si.nMin         = 0;
2193         si.nMax         = es->line_count - 1;
2194         si.nPage        = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2195         si.nPos         = es->y_offset;
2196         TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2197                 si.nMin, si.nMax, si.nPage, si.nPos);
2198         SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
2199     }
2200
2201     if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2202     {
2203         SCROLLINFO si;
2204         si.cbSize       = sizeof(SCROLLINFO);
2205         si.fMask        = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2206         si.nMin         = 0;
2207         si.nMax         = es->text_width - 1;
2208         si.nPage        = es->format_rect.right - es->format_rect.left;
2209         si.nPos         = es->x_offset;
2210         TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2211                 si.nMin, si.nMax, si.nPage, si.nPos);
2212         SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
2213     }
2214 }
2215
2216 /*********************************************************************
2217  *
2218  *      EDIT_WordBreakProc
2219  *
2220  *      Find the beginning of words.
2221  *      Note:   unlike the specs for a WordBreakProc, this function only
2222  *              allows to be called without linebreaks between s[0] upto
2223  *              s[count - 1].  Remember it is only called
2224  *              internally, so we can decide this for ourselves.
2225  *
2226  */
2227 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2228 {
2229         INT ret = 0;
2230
2231         TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2232
2233         if(!s) return 0;
2234
2235         switch (action) {
2236         case WB_LEFT:
2237                 if (!count)
2238                         break;
2239                 if (index)
2240                         index--;
2241                 if (s[index] == ' ') {
2242                         while (index && (s[index] == ' '))
2243                                 index--;
2244                         if (index) {
2245                                 while (index && (s[index] != ' '))
2246                                         index--;
2247                                 if (s[index] == ' ')
2248                                         index++;
2249                         }
2250                 } else {
2251                         while (index && (s[index] != ' '))
2252                                 index--;
2253                         if (s[index] == ' ')
2254                                 index++;
2255                 }
2256                 ret = index;
2257                 break;
2258         case WB_RIGHT:
2259                 if (!count)
2260                         break;
2261                 if (index)
2262                         index--;
2263                 if (s[index] == ' ')
2264                         while ((index < count) && (s[index] == ' ')) index++;
2265                 else {
2266                         while (s[index] && (s[index] != ' ') && (index < count))
2267                                 index++;
2268                         while ((s[index] == ' ') && (index < count)) index++;
2269                 }
2270                 ret = index;
2271                 break;
2272         case WB_ISDELIMITER:
2273                 ret = (s[index] == ' ');
2274                 break;
2275         default:
2276                 ERR("unknown action code, please report !\n");
2277                 break;
2278         }
2279         return ret;
2280 }
2281
2282
2283 /*********************************************************************
2284  *
2285  *      EM_CHARFROMPOS
2286  *
2287  *      returns line number (not index) in high-order word of result.
2288  *      NB : Q137805 is unclear about this. POINT * pointer in lParam apply 
2289  *      to Richedit, not to the edit control. Original documentation is valid.
2290  *      FIXME: do the specs mean to return -1 if outside client area or
2291  *              if outside formatting rectangle ???
2292  *
2293  */
2294 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y)
2295 {
2296         POINT pt;
2297         RECT rc;
2298         INT index;
2299
2300         pt.x = x;
2301         pt.y = y;
2302         GetClientRect(wnd->hwndSelf, &rc);
2303         if (!PtInRect(&rc, pt))
2304                 return -1;
2305
2306         index = EDIT_CharFromPos(wnd, es, x, y, NULL);
2307         return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2308 }
2309
2310
2311 /*********************************************************************
2312  *
2313  *      EM_FMTLINES
2314  *
2315  * Enable or disable soft breaks.
2316  */
2317 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2318 {
2319         es->flags &= ~EF_USE_SOFTBRK;
2320         if (add_eol) {
2321                 es->flags |= EF_USE_SOFTBRK;
2322                 FIXME("soft break enabled, not implemented\n");
2323         }
2324         return add_eol;
2325 }
2326
2327
2328 /*********************************************************************
2329  *
2330  *      EM_GETHANDLE
2331  *
2332  *      Hopefully this won't fire back at us.
2333  *      We always start with a fixed buffer in the local heap.
2334  *      Despite of the documentation says that the local heap is used
2335  *      only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2336  *      buffer on the local heap.
2337  *
2338  */
2339 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2340 {
2341         HLOCAL hLocal;
2342
2343         if (!(es->style & ES_MULTILINE))
2344                 return 0;
2345
2346         if(es->is_unicode)
2347             hLocal = es->hloc32W;
2348         else
2349         {
2350             if(!es->hloc32A)
2351             {
2352                 CHAR *textA;
2353                 INT countA;
2354                 TRACE("Allocating 32-bit ANSI alias buffer\n");
2355                 countA = WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, NULL, 0, NULL, NULL);
2356                 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countA)))
2357                 {
2358                     ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", countA);
2359                     return 0;
2360                 }
2361                 textA = LocalLock(es->hloc32A);
2362                 WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, countA, NULL, NULL);
2363                 LocalUnlock(es->hloc32A);
2364             }
2365             hLocal = es->hloc32A;
2366         }
2367
2368         TRACE("Returning %04X, LocalSize() = %d\n", hLocal, LocalSize(hLocal));
2369         return hLocal;
2370 }
2371
2372
2373 /*********************************************************************
2374  *
2375  *      EM_GETHANDLE16
2376  *
2377  *      Hopefully this won't fire back at us.
2378  *      We always start with a buffer in 32 bit linear memory.
2379  *      However, with this message a 16 bit application requests
2380  *      a handle of 16 bit local heap memory, where it expects to find
2381  *      the text.
2382  *      It's a pitty that from this moment on we have to use this
2383  *      local heap, because applications may rely on the handle
2384  *      in the future.
2385  *
2386  *      In this function we'll try to switch to local heap.
2387  */
2388 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2389 {
2390         CHAR *textA;
2391         INT countA;
2392
2393         if (!(es->style & ES_MULTILINE))
2394                 return 0;
2395
2396         if (es->hloc16)
2397                 return es->hloc16;
2398
2399         if (!LOCAL_HeapSize(wnd->hInstance)) {
2400                 if (!LocalInit16(wnd->hInstance, 0,
2401                                 GlobalSize16(wnd->hInstance))) {
2402                         ERR("could not initialize local heap\n");
2403                         return 0;
2404                 }
2405                 TRACE("local heap initialized\n");
2406         }
2407
2408         countA = WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, NULL, 0, NULL, NULL);
2409
2410         TRACE("Allocating 16-bit ANSI alias buffer\n");
2411         if (!(es->hloc16 = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, countA))) {
2412                 ERR("could not allocate new 16 bit buffer\n");
2413                 return 0;
2414         }
2415
2416         if (!(textA = (LPSTR)LOCAL_Lock(wnd->hInstance, es->hloc16))) {
2417                 ERR("could not lock new 16 bit buffer\n");
2418                 LOCAL_Free(wnd->hInstance, es->hloc16);
2419                 es->hloc16 = 0;
2420                 return 0;
2421         }
2422
2423         WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, countA, NULL, NULL);
2424         LOCAL_Unlock(wnd->hInstance, es->hloc16);
2425
2426         TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(wnd->hInstance, es->hloc16));
2427         return es->hloc16;
2428 }
2429
2430
2431 /*********************************************************************
2432  *
2433  *      EM_GETLINE
2434  *
2435  */
2436 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR lpch)
2437 {
2438         LPWSTR src;
2439         INT len;
2440         INT i;
2441
2442         if (es->style & ES_MULTILINE) {
2443                 if (line >= es->line_count)
2444                         return 0;
2445         } else
2446                 line = 0;
2447         i = EDIT_EM_LineIndex(es, line);
2448         src = es->text + i;
2449         len = min(*(WORD *)lpch, EDIT_EM_LineLength(es, i));
2450         for (i = 0 ; i < len ; i++) {
2451                 *lpch = *src;
2452                 src++;
2453                 lpch++;
2454         }
2455         return (LRESULT)len;
2456 }
2457
2458
2459 /*********************************************************************
2460  *
2461  *      EM_GETSEL
2462  *
2463  */
2464 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end)
2465 {
2466         UINT s = es->selection_start;
2467         UINT e = es->selection_end;
2468
2469         ORDER_UINT(s, e);
2470         if (start)
2471                 *start = s;
2472         if (end)
2473                 *end = e;
2474         return MAKELONG(s, e);
2475 }
2476
2477
2478 /*********************************************************************
2479  *
2480  *      EM_GETTHUMB
2481  *
2482  *      FIXME: is this right ?  (or should it be only VSCROLL)
2483  *      (and maybe only for edit controls that really have their
2484  *      own scrollbars) (and maybe only for multiline controls ?)
2485  *      All in all: very poorly documented
2486  *
2487  *      FIXME: now it's also broken, because of the new WM_HSCROLL /
2488  *              WM_VSCROLL handlers
2489  *
2490  */
2491 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2492 {
2493         return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0),
2494                 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0));
2495 }
2496
2497
2498 /*********************************************************************
2499  *
2500  *      EM_LINEFROMCHAR
2501  *
2502  */
2503 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2504 {
2505         INT line;
2506         LINEDEF *line_def;
2507
2508         if (!(es->style & ES_MULTILINE))
2509                 return 0;
2510         if (index > (INT)strlenW(es->text))
2511                 return es->line_count - 1;
2512         if (index == -1)
2513                 index = min(es->selection_start, es->selection_end);
2514
2515         line = 0;
2516         line_def = es->first_line_def;
2517         index -= line_def->length;
2518         while ((index >= 0) && line_def->next) {
2519                 line++;
2520                 line_def = line_def->next;
2521                 index -= line_def->length;
2522         }
2523         return line;
2524 }
2525
2526
2527 /*********************************************************************
2528  *
2529  *      EM_LINEINDEX
2530  *
2531  */
2532 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2533 {
2534         INT line_index;
2535         LINEDEF *line_def;
2536
2537         if (!(es->style & ES_MULTILINE))
2538                 return 0;
2539         if (line >= es->line_count)
2540                 return -1;
2541
2542         line_index = 0;
2543         line_def = es->first_line_def;
2544         if (line == -1) {
2545                 INT index = es->selection_end - line_def->length;
2546                 while ((index >= 0) && line_def->next) {
2547                         line_index += line_def->length;
2548                         line_def = line_def->next;
2549                         index -= line_def->length;
2550                 }
2551         } else {
2552                 while (line > 0) {
2553                         line_index += line_def->length;
2554                         line_def = line_def->next;
2555                         line--;
2556                 }
2557         }
2558         return line_index;
2559 }
2560
2561
2562 /*********************************************************************
2563  *
2564  *      EM_LINELENGTH
2565  *
2566  */
2567 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2568 {
2569         LINEDEF *line_def;
2570
2571         if (!(es->style & ES_MULTILINE))
2572                 return strlenW(es->text);
2573
2574         if (index == -1) {
2575                 /* get the number of remaining non-selected chars of selected lines */
2576                 INT32 li;
2577                 INT32 count;
2578                 li = EDIT_EM_LineFromChar(es, es->selection_start);
2579                 /* # chars before start of selection area */
2580                 count = es->selection_start - EDIT_EM_LineIndex(es, li);
2581                 li = EDIT_EM_LineFromChar(es, es->selection_end);
2582                 /* # chars after end of selection */
2583                 count += EDIT_EM_LineIndex(es, li) +
2584                         EDIT_EM_LineLength(es, li) - es->selection_end;
2585                 return count;
2586         }
2587         line_def = es->first_line_def;
2588         index -= line_def->length;
2589         while ((index >= 0) && line_def->next) {
2590                 line_def = line_def->next;
2591                 index -= line_def->length;
2592         }
2593         return line_def->net_length;
2594 }
2595
2596
2597 /*********************************************************************
2598  *
2599  *      EM_LINESCROLL
2600  *
2601  *      NOTE: dx is in average character widths, dy - in lines;
2602  *
2603  */
2604 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2605 {
2606         if (!(es->style & ES_MULTILINE))
2607                 return FALSE;
2608
2609         dx *= es->char_width;
2610         return EDIT_EM_LineScroll_internal(wnd, es, dx, dy);
2611 }
2612
2613 /*********************************************************************
2614  *
2615  *      EDIT_EM_LineScroll_internal
2616  *
2617  *      Version of EDIT_EM_LineScroll for internal use.
2618  *      It doesn't refuse if ES_MULTILINE is set and assumes that
2619  *      dx is in pixels, dy - in lines.
2620  *
2621  */
2622 static BOOL EDIT_EM_LineScroll_internal(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2623 {
2624         INT nyoff;
2625         INT x_offset_in_pixels;
2626
2627         if (es->style & ES_MULTILINE)
2628         {
2629             x_offset_in_pixels = es->x_offset;
2630         }
2631         else
2632         {
2633             dy = 0;
2634             x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->x_offset, FALSE));
2635         }
2636
2637         if (-dx > x_offset_in_pixels)
2638                 dx = -x_offset_in_pixels;
2639         if (dx > es->text_width - x_offset_in_pixels)
2640                 dx = es->text_width - x_offset_in_pixels;
2641         nyoff = max(0, es->y_offset + dy);
2642         if (nyoff >= es->line_count)
2643                 nyoff = es->line_count - 1;
2644         dy = (es->y_offset - nyoff) * es->line_height;
2645         if (dx || dy) {
2646                 RECT rc1;
2647                 RECT rc;
2648
2649                 es->y_offset = nyoff;
2650                 if(es->style & ES_MULTILINE)
2651                     es->x_offset += dx;
2652                 else
2653                     es->x_offset += dx / es->char_width;
2654
2655                 GetClientRect(wnd->hwndSelf, &rc1);
2656                 IntersectRect(&rc, &rc1, &es->format_rect);
2657                 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2658                                 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2659         }
2660         if (dx && !(es->flags & EF_HSCROLL_TRACK))
2661                 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2662         if (dy && !(es->flags & EF_VSCROLL_TRACK))
2663                 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2664         return TRUE;
2665 }
2666
2667
2668 /*********************************************************************
2669  *
2670  *      EM_POSFROMCHAR
2671  *
2672  */
2673 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap)
2674 {
2675         INT len = strlenW(es->text);
2676         INT l;
2677         INT li;
2678         INT x;
2679         INT y = 0;
2680         HDC dc;
2681         HFONT old_font = 0;
2682         SIZE size;
2683
2684         index = min(index, len);
2685         dc = GetDC(wnd->hwndSelf);
2686         if (es->font)
2687                 old_font = SelectObject(dc, es->font);
2688         if (es->style & ES_MULTILINE) {
2689                 l = EDIT_EM_LineFromChar(es, index);
2690                 y = (l - es->y_offset) * es->line_height;
2691                 li = EDIT_EM_LineIndex(es, l);
2692                 if (after_wrap && (li == index) && l) {
2693                         INT l2 = l - 1;
2694                         LINEDEF *line_def = es->first_line_def;
2695                         while (l2) {
2696                                 line_def = line_def->next;
2697                                 l2--;
2698                         }
2699                         if (line_def->ending == END_WRAP) {
2700                                 l--;
2701                                 y -= es->line_height;
2702                                 li = EDIT_EM_LineIndex(es, l);
2703                         }
2704                 }
2705                 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2706                                 es->tabs_count, es->tabs)) - es->x_offset;
2707         } else {
2708                 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2709                 if (index < es->x_offset) {
2710                         GetTextExtentPoint32W(dc, text + index,
2711                                         es->x_offset - index, &size);
2712                         x = -size.cx;
2713                 } else {
2714                         GetTextExtentPoint32W(dc, text + es->x_offset,
2715                                         index - es->x_offset, &size);
2716                          x = size.cx;
2717                 }
2718                 y = 0;
2719                 if (es->style & ES_PASSWORD)
2720                         HeapFree(GetProcessHeap(), 0, text);
2721         }
2722         x += es->format_rect.left;
2723         y += es->format_rect.top;
2724         if (es->font)
2725                 SelectObject(dc, old_font);
2726         ReleaseDC(wnd->hwndSelf, dc);
2727         return MAKELONG((INT16)x, (INT16)y);
2728 }
2729
2730
2731 /*********************************************************************
2732  *
2733  *      EM_REPLACESEL
2734  *
2735  *      FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2736  *
2737  */
2738 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
2739 {
2740         UINT strl = strlenW(lpsz_replace);
2741         UINT tl = strlenW(es->text);
2742         UINT utl;
2743         UINT s;
2744         UINT e;
2745         UINT i;
2746         LPWSTR p;
2747
2748         TRACE("%s, can_undo %d, send_update %d\n",
2749             debugstr_w(lpsz_replace), can_undo, send_update);
2750
2751         s = es->selection_start;
2752         e = es->selection_end;
2753
2754         if ((s == e) && !strl)
2755                 return;
2756
2757         ORDER_UINT(s, e);
2758
2759         if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2760                 return;
2761
2762         if (e != s) {
2763                 /* there is something to be deleted */
2764                 if (can_undo) {
2765                         utl = strlenW(es->undo_text);
2766                         if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2767                                 /* undo-buffer is extended to the right */
2768                                 EDIT_MakeUndoFit(es, utl + e - s);
2769                                 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2770                                 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2771                         } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2772                                 /* undo-buffer is extended to the left */
2773                                 EDIT_MakeUndoFit(es, utl + e - s);
2774                                 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2775                                         p[e - s] = p[0];
2776                                 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2777                                         p[i] = (es->text + s)[i];
2778                                 es->undo_position = s;
2779                         } else {
2780                                 /* new undo-buffer */
2781                                 EDIT_MakeUndoFit(es, e - s);
2782                                 strncpyW(es->undo_text, es->text + s, e - s + 1);
2783                                 es->undo_text[e - s] = 0; /* ensure 0 termination */
2784                                 es->undo_position = s;
2785                         }
2786                         /* any deletion makes the old insertion-undo invalid */
2787                         es->undo_insert_count = 0;
2788                 } else
2789                         EDIT_EM_EmptyUndoBuffer(es);
2790
2791                 /* now delete */
2792                 strcpyW(es->text + s, es->text + e);
2793         }
2794         if (strl) {
2795                 /* there is an insertion */
2796                 if (can_undo) {
2797                         if ((s == es->undo_position) ||
2798                                         ((es->undo_insert_count) &&
2799                                         (s == es->undo_position + es->undo_insert_count)))
2800                                 /*
2801                                  * insertion is new and at delete position or
2802                                  * an extension to either left or right
2803                                  */
2804                                 es->undo_insert_count += strl;
2805                         else {
2806                                 /* new insertion undo */
2807                                 es->undo_position = s;
2808                                 es->undo_insert_count = strl;
2809                                 /* new insertion makes old delete-buffer invalid */
2810                                 *es->undo_text = '\0';
2811                         }
2812                 } else
2813                         EDIT_EM_EmptyUndoBuffer(es);
2814
2815                 /* now insert */
2816                 tl = strlenW(es->text);
2817                 for (p = es->text + tl ; p >= es->text + s ; p--)
2818                         p[strl] = p[0];
2819                 for (i = 0 , p = es->text + s ; i < strl ; i++)
2820                         p[i] = lpsz_replace[i];
2821                 if(es->style & ES_UPPERCASE)
2822                         CharUpperBuffW(p, strl);
2823                 else if(es->style & ES_LOWERCASE)
2824                         CharLowerBuffW(p, strl);
2825                 s += strl;
2826         }
2827         /* FIXME: really inefficient */
2828         if (es->style & ES_MULTILINE)
2829                 EDIT_BuildLineDefs_ML(wnd, es);
2830         else
2831             EDIT_CalcLineWidth_SL(wnd, es);
2832
2833         EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2834         es->flags |= EF_MODIFIED;
2835         if (send_update) es->flags |= EF_UPDATE;
2836         EDIT_EM_ScrollCaret(wnd, es);
2837
2838         /* FIXME: really inefficient */
2839         EDIT_UpdateText(wnd, NULL, TRUE);
2840 }
2841
2842
2843 /*********************************************************************
2844  *
2845  *      EM_SCROLL
2846  *
2847  */
2848 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
2849 {
2850         INT dy;
2851
2852         if (!(es->style & ES_MULTILINE))
2853                 return (LRESULT)FALSE;
2854
2855         dy = 0;
2856
2857         switch (action) {
2858         case SB_LINEUP:
2859                 if (es->y_offset)
2860                         dy = -1;
2861                 break;
2862         case SB_LINEDOWN:
2863                 if (es->y_offset < es->line_count - 1)
2864                         dy = 1;
2865                 break;
2866         case SB_PAGEUP:
2867                 if (es->y_offset)
2868                         dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2869                 break;
2870         case SB_PAGEDOWN:
2871                 if (es->y_offset < es->line_count - 1)
2872                         dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2873                 break;
2874         default:
2875                 return (LRESULT)FALSE;
2876         }
2877         if (dy) {
2878             INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2879             /* check if we are going to move too far */
2880             if(es->y_offset + dy > es->line_count - vlc)
2881                 dy = es->line_count - vlc - es->y_offset;
2882
2883             /* Notification is done in EDIT_EM_LineScroll */
2884             if(dy)
2885                 EDIT_EM_LineScroll(wnd, es, 0, dy);
2886         }
2887         return MAKELONG((INT16)dy, (BOOL16)TRUE);
2888 }
2889
2890
2891 /*********************************************************************
2892  *
2893  *      EM_SCROLLCARET
2894  *
2895  */
2896 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
2897 {
2898         if (es->style & ES_MULTILINE) {
2899                 INT l;
2900                 INT li;
2901                 INT vlc;
2902                 INT ww;
2903                 INT cw = es->char_width;
2904                 INT x;
2905                 INT dy = 0;
2906                 INT dx = 0;
2907
2908                 l = EDIT_EM_LineFromChar(es, es->selection_end);
2909                 li = EDIT_EM_LineIndex(es, l);
2910                 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
2911                 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2912                 if (l >= es->y_offset + vlc)
2913                         dy = l - vlc + 1 - es->y_offset;
2914                 if (l < es->y_offset)
2915                         dy = l - es->y_offset;
2916                 ww = es->format_rect.right - es->format_rect.left;
2917                 if (x < es->format_rect.left)
2918                         dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
2919                 if (x > es->format_rect.right)
2920                         dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
2921                 if (dy || dx)
2922                 {
2923                     /* check if we are going to move too far */
2924                     if(es->x_offset + dx + ww > es->text_width)
2925                         dx = es->text_width - ww - es->x_offset;
2926                     if(dx || dy)
2927                         EDIT_EM_LineScroll_internal(wnd, es, dx, dy);
2928                 }
2929         } else {
2930                 INT x;
2931                 INT goal;
2932                 INT format_width;
2933
2934                 if (!(es->style & ES_AUTOHSCROLL))
2935                         return;
2936
2937                 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2938                 format_width = es->format_rect.right - es->format_rect.left;
2939                 if (x < es->format_rect.left) {
2940                         goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
2941                         do {
2942                                 es->x_offset--;
2943                                 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2944                         } while ((x < goal) && es->x_offset);
2945                         /* FIXME: use ScrollWindow() somehow to improve performance */
2946                         EDIT_UpdateText(wnd, NULL, TRUE);
2947                 } else if (x > es->format_rect.right) {
2948                         INT x_last;
2949                         INT len = strlenW(es->text);
2950                         goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
2951                         do {
2952                                 es->x_offset++;
2953                                 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2954                                 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
2955                         } while ((x > goal) && (x_last > es->format_rect.right));
2956                         /* FIXME: use ScrollWindow() somehow to improve performance */
2957                         EDIT_UpdateText(wnd, NULL, TRUE);
2958                 }
2959         }
2960
2961     if(es->flags & EF_FOCUSED)
2962         EDIT_SetCaretPos(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
2963 }
2964
2965
2966 /*********************************************************************
2967  *
2968  *      EM_SETHANDLE
2969  *
2970  *      FIXME:  ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2971  *
2972  */
2973 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc)
2974 {
2975         if (!(es->style & ES_MULTILINE))
2976                 return;
2977
2978         if (!hloc) {
2979                 WARN("called with NULL handle\n");
2980                 return;
2981         }
2982
2983         EDIT_UnlockBuffer(wnd, es, TRUE);
2984
2985         if(es->hloc16)
2986         {
2987             LOCAL_Free(wnd->hInstance, es->hloc16);
2988             es->hloc16 = (HLOCAL16)NULL;
2989         }
2990
2991         if(es->is_unicode)
2992         {
2993             if(es->hloc32A)
2994             {
2995                 LocalFree(es->hloc32A);
2996                 es->hloc32A = (HLOCAL)NULL;
2997             }
2998             es->hloc32W = hloc;
2999         }
3000         else
3001         {
3002             INT countW, countA;
3003             HLOCAL hloc32W_new;
3004             WCHAR *textW;
3005             CHAR *textA;
3006
3007             countA = LocalSize(hloc);
3008             textA = LocalLock(hloc);
3009             countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3010             if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3011             {
3012                 ERR("Could not allocate new unicode buffer\n");
3013                 return;
3014             }
3015             textW = LocalLock(hloc32W_new);
3016             MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3017             LocalUnlock(hloc32W_new);
3018             LocalUnlock(hloc);
3019
3020             if(es->hloc32W)
3021                 LocalFree(es->hloc32W);
3022
3023             es->hloc32W = hloc32W_new;
3024             es->hloc32A = hloc;
3025         }
3026
3027         es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3028
3029         EDIT_LockBuffer(wnd, es);
3030
3031         es->x_offset = es->y_offset = 0;
3032         es->selection_start = es->selection_end = 0;
3033         EDIT_EM_EmptyUndoBuffer(es);
3034         es->flags &= ~EF_MODIFIED;
3035         es->flags &= ~EF_UPDATE;
3036         EDIT_BuildLineDefs_ML(wnd, es);
3037         EDIT_UpdateText(wnd, NULL, TRUE);
3038         EDIT_EM_ScrollCaret(wnd, es);
3039 }
3040
3041
3042 /*********************************************************************
3043  *
3044  *      EM_SETHANDLE16
3045  *
3046  *      FIXME:  ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3047  *
3048  */
3049 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
3050 {
3051         INT countW, countA;
3052         HLOCAL hloc32W_new;
3053         WCHAR *textW;
3054         CHAR *textA;
3055
3056         if (!(es->style & ES_MULTILINE))
3057                 return;
3058
3059         if (!hloc) {
3060                 WARN("called with NULL handle\n");
3061                 return;
3062         }
3063
3064         EDIT_UnlockBuffer(wnd, es, TRUE);
3065
3066         if(es->hloc32A)
3067         {
3068             LocalFree(es->hloc32A);
3069             es->hloc32A = (HLOCAL)NULL;
3070         }
3071
3072         countA = LOCAL_Size(wnd->hInstance, hloc);
3073         textA = LOCAL_Lock(wnd->hInstance, hloc);
3074         countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3075         if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3076         {
3077             ERR("Could not allocate new unicode buffer\n");
3078             return;
3079         }
3080         textW = LocalLock(hloc32W_new);
3081         MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3082         LocalUnlock(hloc32W_new);
3083         LOCAL_Unlock(wnd->hInstance, hloc);
3084
3085         if(es->hloc32W)
3086             LocalFree(es->hloc32W);
3087
3088         es->hloc32W = hloc32W_new;
3089         es->hloc16 = hloc;
3090
3091         es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3092
3093         EDIT_LockBuffer(wnd, es);
3094
3095         es->x_offset = es->y_offset = 0;
3096         es->selection_start = es->selection_end = 0;
3097         EDIT_EM_EmptyUndoBuffer(es);
3098         es->flags &= ~EF_MODIFIED;
3099         es->flags &= ~EF_UPDATE;
3100         EDIT_BuildLineDefs_ML(wnd, es);
3101         EDIT_UpdateText(wnd, NULL, TRUE);
3102         EDIT_EM_ScrollCaret(wnd, es);
3103 }
3104
3105
3106 /*********************************************************************
3107  *
3108  *      EM_SETLIMITTEXT
3109  *
3110  *      FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3111  *      However, the windows version is not complied to yet in all of edit.c
3112  *
3113  */
3114 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3115 {
3116         if (es->style & ES_MULTILINE) {
3117                 if (limit)
3118                         es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3119                 else
3120                         es->buffer_limit = BUFLIMIT_MULTI;
3121         } else {
3122                 if (limit)
3123                         es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3124                 else
3125                         es->buffer_limit = BUFLIMIT_SINGLE;
3126         }
3127 }
3128
3129
3130 /*********************************************************************
3131  *
3132  *      EM_SETMARGINS
3133  * 
3134  * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3135  * action wParam despite what the docs say. EC_USEFONTINFO means one third
3136  * of the char's width, according to the new docs.
3137  *
3138  */
3139 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3140                                INT left, INT right)
3141 {
3142         if (action & EC_LEFTMARGIN) {
3143                 if (left != EC_USEFONTINFO)
3144                         es->left_margin = left;
3145                 else
3146                         es->left_margin = es->char_width / 3;
3147         }
3148
3149         if (action & EC_RIGHTMARGIN) {
3150                 if (right != EC_USEFONTINFO)
3151                         es->right_margin = right;
3152                 else
3153                         es->right_margin = es->char_width / 3;
3154         }
3155         TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3156 }
3157
3158
3159 /*********************************************************************
3160  *
3161  *      EM_SETPASSWORDCHAR
3162  *
3163  */
3164 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, WCHAR c)
3165 {
3166         if (es->style & ES_MULTILINE)
3167                 return;
3168
3169         if (es->password_char == c)
3170                 return;
3171
3172         es->password_char = c;
3173         if (c) {
3174                 wnd->dwStyle |= ES_PASSWORD;
3175                 es->style |= ES_PASSWORD;
3176         } else {
3177                 wnd->dwStyle &= ~ES_PASSWORD;
3178                 es->style &= ~ES_PASSWORD;
3179         }
3180         EDIT_UpdateText(wnd, NULL, TRUE);
3181 }
3182
3183
3184 /*********************************************************************
3185  *
3186  *      EDIT_EM_SetSel
3187  *
3188  *      note:   unlike the specs say: the order of start and end
3189  *              _is_ preserved in Windows.  (i.e. start can be > end)
3190  *              In other words: this handler is OK
3191  *
3192  */
3193 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3194 {
3195         UINT old_start = es->selection_start;
3196         UINT old_end = es->selection_end;
3197         UINT len = strlenW(es->text);
3198
3199         if (start == (UINT)-1) {
3200                 start = es->selection_end;
3201                 end = es->selection_end;
3202         } else {
3203                 start = min(start, len);
3204                 end = min(end, len);
3205         }
3206         es->selection_start = start;
3207         es->selection_end = end;
3208         if (after_wrap)
3209                 es->flags |= EF_AFTER_WRAP;
3210         else
3211                 es->flags &= ~EF_AFTER_WRAP;
3212 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3213         ORDER_UINT(start, end);
3214         ORDER_UINT(end, old_end);
3215         ORDER_UINT(start, old_start);
3216         ORDER_UINT(old_start, old_end);
3217         if (end != old_start)
3218         {
3219 /*
3220  * One can also do 
3221  *          ORDER_UINT32(end, old_start);
3222  *          EDIT_InvalidateText(wnd, es, start, end);
3223  *          EDIT_InvalidateText(wnd, es, old_start, old_end);
3224  * in place of the following if statement.                          
3225  */
3226             if (old_start > end )
3227             {
3228                 EDIT_InvalidateText(wnd, es, start, end);
3229                 EDIT_InvalidateText(wnd, es, old_start, old_end);
3230             }
3231             else
3232             {
3233                 EDIT_InvalidateText(wnd, es, start, old_start);
3234                 EDIT_InvalidateText(wnd, es, end, old_end);
3235             }
3236         }
3237         else EDIT_InvalidateText(wnd, es, start, old_end);
3238 }
3239
3240
3241 /*********************************************************************
3242  *
3243  *      EM_SETTABSTOPS
3244  *
3245  */
3246 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3247 {
3248         if (!(es->style & ES_MULTILINE))
3249                 return FALSE;
3250         if (es->tabs)
3251                 HeapFree(GetProcessHeap(), 0, es->tabs);
3252         es->tabs_count = count;
3253         if (!count)
3254                 es->tabs = NULL;
3255         else {
3256                 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3257                 memcpy(es->tabs, tabs, count * sizeof(INT));
3258         }
3259         return TRUE;
3260 }
3261
3262
3263 /*********************************************************************
3264  *
3265  *      EM_SETTABSTOPS16
3266  *
3267  */
3268 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3269 {
3270         if (!(es->style & ES_MULTILINE))
3271                 return FALSE;
3272         if (es->tabs)
3273                 HeapFree(GetProcessHeap(), 0, es->tabs);
3274         es->tabs_count = count;
3275         if (!count)
3276                 es->tabs = NULL;
3277         else {
3278                 INT i;
3279                 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3280                 for (i = 0 ; i < count ; i++)
3281                         es->tabs[i] = *tabs++;
3282         }
3283         return TRUE;
3284 }
3285
3286
3287 /*********************************************************************
3288  *
3289  *      EM_SETWORDBREAKPROC
3290  *
3291  */
3292 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, LPARAM lParam)
3293 {
3294         if (es->word_break_proc == (void *)lParam)
3295                 return;
3296
3297         es->word_break_proc = (void *)lParam;
3298         es->word_break_proc16 = NULL;
3299
3300         if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3301                 EDIT_BuildLineDefs_ML(wnd, es);
3302                 EDIT_UpdateText(wnd, NULL, TRUE);
3303         }
3304 }
3305
3306
3307 /*********************************************************************
3308  *
3309  *      EM_SETWORDBREAKPROC16
3310  *
3311  */
3312 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3313 {
3314         if (es->word_break_proc16 == wbp)
3315                 return;
3316
3317         es->word_break_proc = NULL;
3318         es->word_break_proc16 = wbp;
3319         if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3320                 EDIT_BuildLineDefs_ML(wnd, es);
3321                 EDIT_UpdateText(wnd, NULL, TRUE);
3322         }
3323 }
3324
3325
3326 /*********************************************************************
3327  *
3328  *      EM_UNDO / WM_UNDO
3329  *
3330  */
3331 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
3332 {
3333         INT ulength = strlenW(es->undo_text);
3334         LPWSTR utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3335
3336         strcpyW(utext, es->undo_text);
3337
3338         TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3339                      es->undo_insert_count, debugstr_w(utext));
3340
3341         EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3342         EDIT_EM_EmptyUndoBuffer(es);
3343         EDIT_EM_ReplaceSel(wnd, es, TRUE, utext, TRUE);
3344         EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3345         EDIT_EM_ScrollCaret(wnd, es);
3346         HeapFree(GetProcessHeap(), 0, utext);
3347
3348         TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3349                         es->undo_insert_count, debugstr_w(es->undo_text));
3350
3351         if (es->flags & EF_UPDATE) {
3352                 es->flags &= ~EF_UPDATE;
3353                 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3354         }
3355
3356         return TRUE;
3357 }
3358
3359
3360 /*********************************************************************
3361  *
3362  *      WM_CHAR
3363  *
3364  */
3365 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, WCHAR c)
3366 {
3367         BOOL control = GetKeyState(VK_CONTROL) & 0x8000;
3368         switch (c) {
3369         case '\r':
3370             /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3371             if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3372                 break;
3373         case '\n':
3374                 if (es->style & ES_MULTILINE) {
3375                         if (es->style & ES_READONLY) {
3376                                 EDIT_MoveHome(wnd, es, FALSE);
3377                                 EDIT_MoveDown_ML(wnd, es, FALSE);
3378                         } else {
3379                                 static const WCHAR cr_lfW[] = {'\r','\n',0};
3380                                 EDIT_EM_ReplaceSel(wnd, es, TRUE, cr_lfW, TRUE);
3381                                 if (es->flags & EF_UPDATE) {
3382                                         es->flags &= ~EF_UPDATE;
3383                                         EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3384                                 }
3385                         }
3386                 }
3387                 break;
3388         case '\t':
3389                 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3390                 {
3391                         static const WCHAR tabW[] = {'\t',0};
3392                         EDIT_EM_ReplaceSel(wnd, es, TRUE, tabW, TRUE);
3393                         if (es->flags & EF_UPDATE) {
3394                                 es->flags &= ~EF_UPDATE;
3395                                 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3396                         }
3397                 }
3398                 break;
3399         case VK_BACK:
3400                 if (!(es->style & ES_READONLY) && !control) {
3401                         if (es->selection_start != es->selection_end)
3402                                 EDIT_WM_Clear(wnd, es);
3403                         else {
3404                                 /* delete character left of caret */
3405                                 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3406                                 EDIT_MoveBackward(wnd, es, TRUE);
3407                                 EDIT_WM_Clear(wnd, es);
3408                         }
3409                 }
3410                 break;
3411         case 0x03: /* ^C */
3412                 SendMessageW(wnd->hwndSelf, WM_COPY, 0, 0);
3413                 break;
3414         case 0x16: /* ^V */
3415                 SendMessageW(wnd->hwndSelf, WM_PASTE, 0, 0);
3416                 break;
3417         case 0x18: /* ^X */
3418                 SendMessageW(wnd->hwndSelf, WM_CUT, 0, 0);
3419                 break;
3420         
3421         default:
3422                 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3423                         WCHAR str[2];
3424                         str[0] = c;
3425                         str[1] = '\0';
3426                         EDIT_EM_ReplaceSel(wnd, es, TRUE, str, TRUE);
3427                         if (es->flags & EF_UPDATE) {
3428                                 es->flags &= ~EF_UPDATE;
3429                                 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3430                         }
3431                 }
3432                 break;
3433         }
3434 }
3435
3436
3437 /*********************************************************************
3438  *
3439  *      WM_COMMAND
3440  *
3441  */
3442 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND control)
3443 {
3444         if (code || control)
3445                 return;
3446
3447         switch (id) {
3448                 case EM_UNDO:
3449                         EDIT_EM_Undo(wnd, es);
3450                         break;
3451                 case WM_CUT:
3452                         EDIT_WM_Cut(wnd, es);
3453                         break;
3454                 case WM_COPY:
3455                         EDIT_WM_Copy(wnd, es);
3456                         break;
3457                 case WM_PASTE:
3458                         EDIT_WM_Paste(wnd, es);
3459                         break;
3460                 case WM_CLEAR:
3461                         EDIT_WM_Clear(wnd, es);
3462                         break;
3463                 case EM_SETSEL:
3464                         EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
3465                         EDIT_EM_ScrollCaret(wnd, es);
3466                         break;
3467                 default:
3468                         ERR("unknown menu item, please report\n");
3469                         break;
3470         }
3471 }
3472
3473
3474 /*********************************************************************
3475  *
3476  *      WM_CONTEXTMENU
3477  *
3478  *      Note: the resource files resource/sysres_??.rc cannot define a
3479  *              single popup menu.  Hence we use a (dummy) menubar
3480  *              containing the single popup menu as its first item.
3481  *
3482  *      FIXME: the message identifiers have been chosen arbitrarily,
3483  *              hence we use MF_BYPOSITION.
3484  *              We might as well use the "real" values (anybody knows ?)
3485  *              The menu definition is in resources/sysres_??.rc.
3486  *              Once these are OK, we better use MF_BYCOMMAND here
3487  *              (as we do in EDIT_WM_Command()).
3488  *
3489  */
3490 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, INT x, INT y)
3491 {
3492         HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3493         HMENU popup = GetSubMenu(menu, 0);
3494         UINT start = es->selection_start;
3495         UINT end = es->selection_end;
3496
3497         ORDER_UINT(start, end);
3498
3499         /* undo */
3500         EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) ? MF_ENABLED : MF_GRAYED));
3501         /* cut */
3502         EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3503         /* copy */
3504         EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3505         /* paste */
3506         EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) ? MF_ENABLED : MF_GRAYED));
3507         /* delete */
3508         EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
3509         /* select all */
3510         EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3511
3512         TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
3513         DestroyMenu(menu);
3514 }
3515
3516
3517 /*********************************************************************
3518  *
3519  *      WM_COPY
3520  *
3521  */
3522 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
3523 {
3524         INT s = es->selection_start;
3525         INT e = es->selection_end;
3526         HGLOBAL hdst;
3527         LPWSTR dst;
3528
3529         if (e == s)
3530                 return;
3531         ORDER_INT(s, e);
3532         hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3533         dst = GlobalLock(hdst);
3534         strncpyW(dst, es->text + s, e - s);
3535         dst[e - s] = 0; /* ensure 0 termination */
3536         TRACE("%s\n", debugstr_w(dst));
3537         GlobalUnlock(hdst);
3538         OpenClipboard(wnd->hwndSelf);
3539         EmptyClipboard();
3540         SetClipboardData(CF_UNICODETEXT, hdst);
3541         CloseClipboard();
3542 }
3543
3544
3545 /*********************************************************************
3546  *
3547  *      WM_CREATE
3548  *
3549  */
3550 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCWSTR name)
3551 {
3552         TRACE("%s\n", debugstr_w(name));
3553        /*
3554         *       To initialize some final structure members, we call some helper
3555         *       functions.  However, since the EDITSTATE is not consistent (i.e.
3556         *       not fully initialized), we should be very careful which
3557         *       functions can be called, and in what order.
3558         */
3559         EDIT_WM_SetFont(wnd, es, 0, FALSE);
3560         EDIT_EM_EmptyUndoBuffer(es);
3561
3562        if (name && *name) {
3563            EDIT_EM_ReplaceSel(wnd, es, FALSE, name, TRUE);
3564            /* if we insert text to the editline, the text scrolls out
3565             * of the window, as the caret is placed after the insert
3566             * pos normally; thus we reset es->selection... to 0 and
3567             * update caret
3568             */
3569            es->selection_start = es->selection_end = 0;
3570            EDIT_EM_ScrollCaret(wnd, es);
3571            if (es->flags & EF_UPDATE) {
3572                 es->flags &= ~EF_UPDATE;
3573                 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3574            }
3575        }
3576        return 0;
3577 }
3578
3579
3580 /*********************************************************************
3581  *
3582  *      WM_DESTROY
3583  *
3584  */
3585 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3586 {
3587         if (es->hloc32W) {
3588                 while (LocalUnlock(es->hloc32W)) ;
3589                 LocalFree(es->hloc32W);
3590         }
3591         if (es->hloc32A) {
3592                 while (LocalUnlock(es->hloc32A)) ;
3593                 LocalFree(es->hloc32A);
3594         }
3595         if (es->hloc16) {
3596                 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3597                 LOCAL_Free(wnd->hInstance, es->hloc16);
3598         }
3599         HeapFree(GetProcessHeap(), 0, es);
3600         *(EDITSTATE **)wnd->wExtra = NULL;
3601 }
3602
3603
3604 /*********************************************************************
3605  *
3606  *      WM_ERASEBKGND
3607  *
3608  */
3609 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3610 {
3611         HBRUSH brush;
3612         RECT rc;
3613
3614         if ( get_app_version() >= 0x40000 &&(
3615                     !es->bEnableState || (es->style & ES_READONLY)))
3616                 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3617         else
3618                 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc);
3619
3620         if (!brush)
3621                 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3622
3623         GetClientRect(wnd->hwndSelf, &rc);
3624         IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3625         GetClipBox(dc, &rc);
3626         /*
3627          *      FIXME:  specs say that we should UnrealizeObject() the brush,
3628          *              but the specs of UnrealizeObject() say that we shouldn't
3629          *              unrealize a stock object.  The default brush that
3630          *              DefWndProc() returns is ... a stock object.
3631          */
3632         FillRect(dc, &rc, brush);
3633         return -1;
3634 }
3635
3636
3637 /*********************************************************************
3638  *
3639  *      WM_GETTEXT
3640  *
3641  */
3642 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3643 {
3644     if(!count) return 0;
3645
3646     if(unicode)
3647     {
3648         LPWSTR textW = (LPWSTR)lParam;
3649         strncpyW(textW, es->text, count);
3650         textW[count - 1] = 0; /* ensure 0 termination */
3651         return strlenW(textW);
3652     }
3653     else
3654     {
3655         LPSTR textA = (LPSTR)lParam;
3656         INT ret = WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, count, NULL, NULL);
3657         textA[count - 1] = 0; /* ensure 0 termination */
3658         return ret;
3659     }
3660 }
3661
3662
3663 /*********************************************************************
3664  *
3665  *      EDIT_HScroll_Hack
3666  *
3667  *      16 bit notepad needs this.  Actually it is not _our_ hack,
3668  *      it is notepad's.  Notepad is sending us scrollbar messages with
3669  *      undocumented parameters without us even having a scrollbar ... !?!?
3670  *
3671  */
3672 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos)
3673 {
3674         INT dx = 0;
3675         INT fw = es->format_rect.right - es->format_rect.left;
3676         LRESULT ret = 0;
3677
3678         if (!(es->flags & EF_HSCROLL_HACK)) {
3679                 ERR("hacked WM_HSCROLL handler invoked\n");
3680                 ERR("      if you are _not_ running 16 bit notepad, please report\n");
3681                 ERR("      (this message is only displayed once per edit control)\n");
3682                 es->flags |= EF_HSCROLL_HACK;
3683         }
3684
3685         switch (action) {
3686         case SB_LINELEFT:
3687                 if (es->x_offset)
3688                         dx = -es->char_width;
3689                 break;
3690         case SB_LINERIGHT:
3691                 if (es->x_offset < es->text_width)
3692                         dx = es->char_width;
3693                 break;
3694         case SB_PAGELEFT:
3695                 if (es->x_offset)
3696                         dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3697                 break;
3698         case SB_PAGERIGHT:
3699                 if (es->x_offset < es->text_width)
3700                         dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3701                 break;
3702         case SB_LEFT:
3703                 if (es->x_offset)
3704                         dx = -es->x_offset;
3705                 break;
3706         case SB_RIGHT:
3707                 if (es->x_offset < es->text_width)
3708                         dx = es->text_width - es->x_offset;
3709                 break;
3710         case SB_THUMBTRACK:
3711                 es->flags |= EF_HSCROLL_TRACK;
3712                 dx = pos * es->text_width / 100 - es->x_offset;
3713                 break;
3714         case SB_THUMBPOSITION:
3715                 es->flags &= ~EF_HSCROLL_TRACK;
3716                 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3717                         EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3718                 break;
3719         case SB_ENDSCROLL:
3720                 break;
3721
3722         /*
3723          *      FIXME : the next two are undocumented !
3724          *      Are we doing the right thing ?
3725          *      At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3726          *      although it's also a regular control message.
3727          */
3728         case EM_GETTHUMB16:
3729                 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3730                 break;
3731         case EM_LINESCROLL16:
3732                 dx = pos;
3733                 break;
3734
3735         default:
3736                 ERR("undocumented (hacked) WM_HSCROLL parameter, please report\n");
3737                 return 0;
3738         }
3739         if (dx)
3740         {
3741             INT fw = es->format_rect.right - es->format_rect.left;
3742             /* check if we are going to move too far */
3743             if(es->x_offset + dx + fw > es->text_width)
3744                 dx = es->text_width - fw - es->x_offset;
3745             if(dx)
3746                 EDIT_EM_LineScroll_internal(wnd, es, dx, 0);
3747         }
3748         return ret;
3749 }
3750
3751
3752 /*********************************************************************
3753  *
3754  *      WM_HSCROLL
3755  *
3756  */
3757 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
3758 {
3759         INT dx;
3760         INT fw;
3761
3762         if (!(es->style & ES_MULTILINE))
3763                 return 0;
3764
3765         if (!(es->style & ES_AUTOHSCROLL))
3766                 return 0;
3767
3768         if (!(es->style & WS_HSCROLL))
3769                 return EDIT_HScroll_Hack(wnd, es, action, pos);
3770
3771         dx = 0;
3772         fw = es->format_rect.right - es->format_rect.left;
3773         switch (action) {
3774         case SB_LINELEFT:
3775                 if (es->x_offset)
3776                         dx = -es->char_width;
3777                 break;
3778         case SB_LINERIGHT:
3779                 if (es->x_offset < es->text_width)
3780                         dx = es->char_width;
3781                 break;
3782         case SB_PAGELEFT:
3783                 if (es->x_offset)
3784                         dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3785                 break;
3786         case SB_PAGERIGHT:
3787                 if (es->x_offset < es->text_width)
3788                         dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3789                 break;
3790         case SB_LEFT:
3791                 if (es->x_offset)
3792                         dx = -es->x_offset;
3793                 break;
3794         case SB_RIGHT:
3795                 if (es->x_offset < es->text_width)
3796                         dx = es->text_width - es->x_offset;
3797                 break;
3798         case SB_THUMBTRACK:
3799                 es->flags |= EF_HSCROLL_TRACK;
3800                 dx = pos - es->x_offset;
3801                 break;
3802         case SB_THUMBPOSITION:
3803                 es->flags &= ~EF_HSCROLL_TRACK;
3804                 if (!(dx = pos - es->x_offset)) {
3805                         SetScrollPos(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3806                         EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3807                 }
3808                 break;
3809         case SB_ENDSCROLL:
3810                 break;
3811
3812         default:
3813                 ERR("undocumented WM_HSCROLL parameter, please report\n");
3814                 return 0;
3815         }
3816         if (dx)
3817         {
3818             INT fw = es->format_rect.right - es->format_rect.left;
3819             /* check if we are going to move too far */
3820             if(es->x_offset + dx + fw > es->text_width)
3821                 dx = es->text_width - fw - es->x_offset;
3822             if(dx)
3823                 EDIT_EM_LineScroll_internal(wnd, es, dx, 0);
3824         }
3825         return 0;
3826 }
3827
3828
3829 /*********************************************************************
3830  *
3831  *      EDIT_CheckCombo
3832  *
3833  */
3834 static BOOL EDIT_CheckCombo(WND *wnd, EDITSTATE *es, UINT msg, INT key)
3835 {
3836    HWND hLBox = es->hwndListBox;
3837    HWND hCombo;
3838    BOOL bDropped;
3839    int  nEUI;
3840
3841    if (!hLBox)
3842       return FALSE;
3843
3844    hCombo   = wnd->parent->hwndSelf;
3845    bDropped = TRUE;
3846    nEUI     = 0;
3847
3848    TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
3849                      wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3850
3851    if (key == VK_UP || key == VK_DOWN)
3852    {
3853       if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3854          nEUI = 1;
3855
3856       if (msg == WM_KEYDOWN || nEUI)
3857           bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3858    }
3859
3860    switch (msg)
3861    {
3862       case WM_KEYDOWN:
3863          if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3864          {
3865             /* make sure ComboLBox pops up */
3866             SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3867             key = VK_F4;
3868             nEUI = 2;
3869          }
3870
3871          SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3872          break;
3873
3874       case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3875          if (nEUI)
3876             SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3877          else
3878             SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
3879          break;
3880    }
3881
3882    if(nEUI == 2)
3883       SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3884
3885    return TRUE;
3886 }
3887
3888
3889 /*********************************************************************
3890  *
3891  *      WM_KEYDOWN
3892  *
3893  *      Handling of special keys that don't produce a WM_CHAR
3894  *      (i.e. non-printable keys) & Backspace & Delete
3895  *
3896  */
3897 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key)
3898 {
3899         BOOL shift;
3900         BOOL control;
3901
3902         if (GetKeyState(VK_MENU) & 0x8000)
3903                 return 0;
3904
3905         shift = GetKeyState(VK_SHIFT) & 0x8000;
3906         control = GetKeyState(VK_CONTROL) & 0x8000;
3907
3908         switch (key) {
3909         case VK_F4:
3910         case VK_UP:
3911                 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key) || key == VK_F4)
3912                         break;
3913
3914                 /* fall through */
3915         case VK_LEFT:
3916                 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3917                         EDIT_MoveUp_ML(wnd, es, shift);
3918                 else
3919                         if (control)
3920                                 EDIT_MoveWordBackward(wnd, es, shift);
3921                         else
3922                                 EDIT_MoveBackward(wnd, es, shift);
3923                 break;
3924         case VK_DOWN:
3925                 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key))
3926                         break;
3927                 /* fall through */
3928         case VK_RIGHT:
3929                 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3930                         EDIT_MoveDown_ML(wnd, es, shift);
3931                 else if (control)
3932                         EDIT_MoveWordForward(wnd, es, shift);
3933                 else
3934                         EDIT_MoveForward(wnd, es, shift);
3935                 break;
3936         case VK_HOME:
3937                 EDIT_MoveHome(wnd, es, shift);
3938                 break;
3939         case VK_END:
3940                 EDIT_MoveEnd(wnd, es, shift);
3941                 break;
3942         case VK_PRIOR:
3943                 if (es->style & ES_MULTILINE)
3944                         EDIT_MovePageUp_ML(wnd, es, shift);
3945                 else
3946                         EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
3947                 break;
3948         case VK_NEXT:
3949                 if (es->style & ES_MULTILINE)
3950                         EDIT_MovePageDown_ML(wnd, es, shift);
3951                 else
3952                         EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
3953                 break;
3954         case VK_DELETE:
3955                 if (!(es->style & ES_READONLY) && !(shift && control)) {
3956                         if (es->selection_start != es->selection_end) {
3957                                 if (shift)
3958                                         EDIT_WM_Cut(wnd, es);
3959                                 else
3960                                         EDIT_WM_Clear(wnd, es);
3961                         } else {
3962                                 if (shift) {
3963                                         /* delete character left of caret */
3964                                         EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3965                                         EDIT_MoveBackward(wnd, es, TRUE);
3966                                         EDIT_WM_Clear(wnd, es);
3967                                 } else if (control) {
3968                                         /* delete to end of line */
3969                                         EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3970                                         EDIT_MoveEnd(wnd, es, TRUE);
3971                                         EDIT_WM_Clear(wnd, es);
3972                                 } else {
3973                                         /* delete character right of caret */
3974                                         EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3975                                         EDIT_MoveForward(wnd, es, TRUE);
3976                                         EDIT_WM_Clear(wnd, es);
3977                                 }
3978                         }
3979                 }
3980                 break;
3981         case VK_INSERT:
3982                 if (shift) {
3983                         if (!(es->style & ES_READONLY))
3984                                 EDIT_WM_Paste(wnd, es);
3985                 } else if (control)
3986                         EDIT_WM_Copy(wnd, es);
3987                 break;
3988         case VK_RETURN:
3989             /* If the edit doesn't want the return send a message to the default object */
3990             if(!(es->style & ES_WANTRETURN))
3991             {
3992                 HWND hwndParent = GetParent(wnd->hwndSelf);
3993                 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
3994                 if (HIWORD(dw) == DC_HASDEFID)
3995                 {
3996                     SendMessageW( hwndParent, WM_COMMAND, 
3997                                   MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
3998                               (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
3999                 }
4000             }
4001             break;
4002         }
4003         return 0;
4004 }
4005
4006
4007 /*********************************************************************
4008  *
4009  *      WM_KILLFOCUS
4010  *
4011  */
4012 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es)
4013 {
4014         es->flags &= ~EF_FOCUSED;
4015         DestroyCaret();
4016         if(!(es->style & ES_NOHIDESEL))
4017                 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
4018         EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
4019         return 0;
4020 }
4021
4022
4023 /*********************************************************************
4024  *
4025  *      WM_LBUTTONDBLCLK
4026  *
4027  *      The caret position has been set on the WM_LBUTTONDOWN message
4028  *
4029  */
4030 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es)
4031 {
4032         INT s;
4033         INT e = es->selection_end;
4034         INT l;
4035         INT li;
4036         INT ll;
4037
4038         if (!(es->flags & EF_FOCUSED))
4039                 return 0;
4040
4041         l = EDIT_EM_LineFromChar(es, e);
4042         li = EDIT_EM_LineIndex(es, l);
4043         ll = EDIT_EM_LineLength(es, e);
4044         s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4045         e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4046         EDIT_EM_SetSel(wnd, es, s, e, FALSE);
4047         EDIT_EM_ScrollCaret(wnd, es);
4048         return 0;
4049 }
4050
4051
4052 /*********************************************************************
4053  *
4054  *      WM_LBUTTONDOWN
4055  *
4056  */
4057 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
4058 {
4059         INT e;
4060         BOOL after_wrap;
4061
4062         if (!(es->flags & EF_FOCUSED))
4063                 return 0;
4064
4065         es->bCaptureState = TRUE;
4066         SetCapture(wnd->hwndSelf);
4067         EDIT_ConfinePoint(es, &x, &y);
4068         e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
4069         EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4070         EDIT_EM_ScrollCaret(wnd, es);
4071         es->region_posx = es->region_posy = 0;
4072         SetTimer(wnd->hwndSelf, 0, 100, NULL);
4073         return 0;
4074 }
4075
4076
4077 /*********************************************************************
4078  *
4079  *      WM_LBUTTONUP
4080  *
4081  */
4082 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es)
4083 {
4084         if (es->bCaptureState && GetCapture() == hwndSelf) {
4085                 KillTimer(hwndSelf, 0);
4086                 ReleaseCapture();
4087         }
4088         es->bCaptureState = FALSE;
4089         return 0;
4090 }
4091
4092
4093 /*********************************************************************
4094  *
4095  *      WM_MBUTTONDOWN
4096  *
4097  */
4098 static LRESULT EDIT_WM_MButtonDown(WND *wnd)
4099 {  
4100     SendMessageW(wnd->hwndSelf,WM_PASTE,0,0);  
4101     return 0;
4102 }
4103
4104
4105 /*********************************************************************
4106  *
4107  *      WM_MOUSEMOVE
4108  *
4109  */
4110 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, INT x, INT y)
4111 {
4112         INT e;
4113         BOOL after_wrap;
4114         INT prex, prey;
4115
4116         if (GetCapture() != wnd->hwndSelf)
4117                 return 0;
4118
4119         /*
4120          *      FIXME: gotta do some scrolling if outside client
4121          *              area.  Maybe reset the timer ?
4122          */
4123         prex = x; prey = y;
4124         EDIT_ConfinePoint(es, &x, &y);
4125         es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4126         es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4127         e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
4128         EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
4129         return 0;
4130 }
4131
4132
4133 /*********************************************************************
4134  *
4135  *      WM_NCCREATE
4136  *
4137  */
4138 static LRESULT EDIT_WM_NCCreate(WND *wnd, DWORD style, HWND hwndParent, BOOL unicode)
4139 {
4140         EDITSTATE *es;
4141         UINT alloc_size;
4142
4143         TRACE("Creating %s edit control\n", unicode ? "Unicode" : "ANSI");
4144
4145         if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4146                 return FALSE;
4147         *(EDITSTATE **)wnd->wExtra = es;
4148
4149        /*
4150         *      Note: since the EDITSTATE has not been fully initialized yet,
4151         *            we can't use any API calls that may send
4152         *            WM_XXX messages before WM_NCCREATE is completed.
4153         */
4154
4155         es->is_unicode = unicode;
4156         es->style = style;
4157
4158         es->bEnableState = !(style & WS_DISABLED);
4159
4160         /*
4161          * In Win95 look and feel, the WS_BORDER style is replaced by the 
4162          * WS_EX_CLIENTEDGE style for the edit control. This gives the edit 
4163          * control a non client area.
4164          */
4165         if (TWEAK_WineLook != WIN31_LOOK)
4166         {
4167           if (es->style & WS_BORDER)
4168           {
4169             es->style      &= ~WS_BORDER;
4170             wnd->dwStyle   &= ~WS_BORDER;
4171             wnd->dwExStyle |= WS_EX_CLIENTEDGE;
4172           }
4173         }
4174         else
4175         {
4176           if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4177             wnd->dwStyle &= ~WS_BORDER;
4178         }
4179
4180         if (es->style & ES_COMBO)
4181            es->hwndListBox = GetDlgItem(hwndParent, ID_CB_LISTBOX);
4182
4183         if (es->style & ES_MULTILINE) {
4184                 es->buffer_limit = BUFLIMIT_MULTI;
4185                 if (es->style & WS_VSCROLL)
4186                         es->style |= ES_AUTOVSCROLL;
4187                 if (es->style & WS_HSCROLL)
4188                         es->style |= ES_AUTOHSCROLL;
4189                 es->style &= ~ES_PASSWORD;
4190                 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4191                         if (es->style & ES_RIGHT)
4192                                 es->style &= ~ES_CENTER;
4193                         es->style &= ~WS_HSCROLL;
4194                         es->style &= ~ES_AUTOHSCROLL;
4195                 }
4196
4197                 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4198                 es->style |= ES_AUTOVSCROLL;
4199         } else {
4200                 es->buffer_limit = BUFLIMIT_SINGLE;
4201                 es->style &= ~ES_CENTER;
4202                 es->style &= ~ES_RIGHT;
4203                 es->style &= ~WS_HSCROLL;
4204                 es->style &= ~WS_VSCROLL;
4205                 es->style &= ~ES_AUTOVSCROLL;
4206                 es->style &= ~ES_WANTRETURN;
4207                 if (es->style & ES_UPPERCASE) {
4208                         es->style &= ~ES_LOWERCASE;
4209                         es->style &= ~ES_NUMBER;
4210                 } else if (es->style & ES_LOWERCASE)
4211                         es->style &= ~ES_NUMBER;
4212                 if (es->style & ES_PASSWORD)
4213                         es->password_char = '*';
4214
4215                 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4216                 es->style |= ES_AUTOHSCROLL;
4217         }
4218
4219         alloc_size = ((es->buffer_size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
4220         if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4221             return FALSE;
4222         es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4223
4224         if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4225                 return FALSE;
4226         es->undo_buffer_size = es->buffer_size;
4227
4228         if (es->style & ES_MULTILINE)
4229                 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4230                         return FALSE;
4231         es->line_count = 1;
4232
4233         return TRUE;
4234 }
4235
4236 /*********************************************************************
4237  *
4238  *      WM_PAINT
4239  *
4240  */
4241 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam)
4242 {
4243         PAINTSTRUCT ps;
4244         INT i;
4245         HDC dc;
4246         HFONT old_font = 0;
4247         RECT rc;
4248         RECT rcLine;
4249         RECT rcRgn;
4250         BOOL rev = es->bEnableState &&
4251                                 ((es->flags & EF_FOCUSED) ||
4252                                         (es->style & ES_NOHIDESEL));
4253         if (!wParam)
4254             dc = BeginPaint(wnd->hwndSelf, &ps);
4255         else
4256             dc = (HDC) wParam;
4257         if(es->style & WS_BORDER) {
4258                 GetClientRect(wnd->hwndSelf, &rc);
4259                 if(es->style & ES_MULTILINE) {
4260                         if(es->style & WS_HSCROLL) rc.bottom++;
4261                         if(es->style & WS_VSCROLL) rc.right++;
4262                 }
4263                 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4264         }
4265         IntersectClipRect(dc, es->format_rect.left,
4266                                 es->format_rect.top,
4267                                 es->format_rect.right,
4268                                 es->format_rect.bottom);
4269         if (es->style & ES_MULTILINE) {
4270                 GetClientRect(wnd->hwndSelf, &rc);
4271                 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4272         }
4273         if (es->font)
4274                 old_font = SelectObject(dc, es->font);
4275         if ( get_app_version() >= 0x40000 &&(
4276                     !es->bEnableState || (es->style & ES_READONLY)))
4277                 EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
4278         else
4279                 EDIT_SEND_CTLCOLOR(wnd, dc);
4280
4281         if (!es->bEnableState)
4282                 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4283         GetClipBox(dc, &rcRgn);
4284         if (es->style & ES_MULTILINE) {
4285                 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4286                 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4287                         EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
4288                         if (IntersectRect(&rc, &rcRgn, &rcLine))
4289                                 EDIT_PaintLine(wnd, es, dc, i, rev);
4290                 }
4291         } else {
4292                 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
4293                 if (IntersectRect(&rc, &rcRgn, &rcLine))
4294                         EDIT_PaintLine(wnd, es, dc, 0, rev);
4295         }
4296         if (es->font)
4297                 SelectObject(dc, old_font);
4298
4299         if (!wParam)
4300             EndPaint(wnd->hwndSelf, &ps);
4301
4302         EDIT_UpdateScrollInfo(wnd, es);
4303 }
4304
4305
4306 /*********************************************************************
4307  *
4308  *      WM_PASTE
4309  *
4310  */
4311 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
4312 {
4313         HGLOBAL hsrc;
4314         LPWSTR src;
4315
4316         OpenClipboard(wnd->hwndSelf);
4317         if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4318                 src = (LPWSTR)GlobalLock(hsrc);
4319                 EDIT_EM_ReplaceSel(wnd, es, TRUE, src, TRUE);
4320                 GlobalUnlock(hsrc);
4321
4322                 if (es->flags & EF_UPDATE) {
4323                         es->flags &= ~EF_UPDATE;
4324                         EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
4325                 }
4326         }
4327         CloseClipboard();
4328 }
4329
4330
4331 /*********************************************************************
4332  *
4333  *      WM_SETFOCUS
4334  *
4335  */
4336 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es)
4337 {
4338         es->flags |= EF_FOCUSED;
4339         CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4340         EDIT_SetCaretPos(wnd, es, es->selection_end,
4341                          es->flags & EF_AFTER_WRAP);
4342         if(!(es->style & ES_NOHIDESEL))
4343                 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
4344         ShowCaret(wnd->hwndSelf);
4345         EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
4346 }
4347
4348
4349 /*********************************************************************
4350  *
4351  *      WM_SETFONT
4352  *
4353  * With Win95 look the margins are set to default font value unless 
4354  * the system font (font == 0) is being set, in which case they are left
4355  * unchanged.
4356  *
4357  */
4358 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
4359 {
4360         TEXTMETRICW tm;
4361         HDC dc;
4362         HFONT old_font = 0;
4363         RECT r;
4364
4365         es->font = font;
4366         dc = GetDC(wnd->hwndSelf);
4367         if (font)
4368                 old_font = SelectObject(dc, font);
4369         GetTextMetricsW(dc, &tm);
4370         es->line_height = tm.tmHeight;
4371         es->char_width = tm.tmAveCharWidth;
4372         if (font)
4373                 SelectObject(dc, old_font);
4374         ReleaseDC(wnd->hwndSelf, dc);
4375         if (font && (TWEAK_WineLook > WIN31_LOOK))
4376                 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4377                                    EC_USEFONTINFO, EC_USEFONTINFO);
4378
4379         /* Force the recalculation of the format rect for each font change */
4380         GetClientRect(wnd->hwndSelf, &r);
4381         EDIT_SetRectNP(wnd, es, &r);
4382
4383         if (es->style & ES_MULTILINE)
4384                 EDIT_BuildLineDefs_ML(wnd, es);
4385         else
4386             EDIT_CalcLineWidth_SL(wnd, es);
4387
4388         if (redraw)
4389                 EDIT_UpdateText(wnd, NULL, TRUE);
4390         if (es->flags & EF_FOCUSED) {
4391                 DestroyCaret();
4392                 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4393                 EDIT_SetCaretPos(wnd, es, es->selection_end,
4394                                  es->flags & EF_AFTER_WRAP);
4395                 ShowCaret(wnd->hwndSelf);
4396         }
4397 }
4398
4399
4400 /*********************************************************************
4401  *
4402  *      WM_SETTEXT
4403  *
4404  * NOTES
4405  *  For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4406  *  The modified flag is reset. No notifications are sent.
4407  *
4408  *  For single-line controls, reception of WM_SETTEXT triggers:
4409  *  The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4410  *
4411  */
4412 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPARAM lParam, BOOL unicode)
4413 {
4414     LPWSTR text;
4415
4416     if(unicode)
4417         text = (LPWSTR)lParam;
4418     else
4419     {
4420         LPCSTR textA = (LPCSTR)lParam;
4421         INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4422         if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4423             MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4424     }
4425
4426         EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
4427         if (text) {
4428                 TRACE("%s\n", debugstr_w(text));
4429                 EDIT_EM_ReplaceSel(wnd, es, FALSE, text, !(es->style & ES_MULTILINE));
4430                 if(!unicode)
4431                     HeapFree(GetProcessHeap(), 0, text);
4432         } else {
4433                 static const WCHAR empty_stringW[] = {0};
4434                 TRACE("<NULL>\n");
4435                 EDIT_EM_ReplaceSel(wnd, es, FALSE, empty_stringW, !(es->style & ES_MULTILINE));
4436         }
4437         es->x_offset = 0;
4438         es->flags &= ~EF_MODIFIED;
4439         EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
4440         EDIT_EM_ScrollCaret(wnd, es);
4441
4442         if (es->flags & EF_UPDATE) {
4443                 es->flags &= ~EF_UPDATE;
4444                 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
4445         }
4446 }
4447
4448
4449 /*********************************************************************
4450  *
4451  *      WM_SIZE
4452  *
4453  */
4454 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
4455 {
4456         if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4457                 RECT rc;
4458                 TRACE("width = %d, height = %d\n", width, height);
4459                 SetRect(&rc, 0, 0, width, height);
4460                 EDIT_SetRectNP(wnd, es, &rc);
4461                 EDIT_UpdateText(wnd, NULL, TRUE);
4462         }
4463 }
4464
4465
4466 /*********************************************************************
4467  *
4468  *      WM_SYSKEYDOWN
4469  *
4470  */
4471 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
4472 {
4473         if ((key == VK_BACK) && (key_data & 0x2000)) {
4474                 if (EDIT_EM_CanUndo(es))
4475                         EDIT_EM_Undo(wnd, es);
4476                 return 0;
4477         } else if (key == VK_UP || key == VK_DOWN) {
4478                 if (EDIT_CheckCombo(wnd, es, WM_SYSKEYDOWN, key))
4479                         return 0;
4480         }
4481         return DefWindowProcW(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4482 }
4483
4484
4485 /*********************************************************************
4486  *
4487  *      WM_TIMER
4488  *
4489  */
4490 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es)
4491 {
4492         if (es->region_posx < 0) {
4493                 EDIT_MoveBackward(wnd, es, TRUE);
4494         } else if (es->region_posx > 0) {
4495                 EDIT_MoveForward(wnd, es, TRUE);
4496         }
4497 /*
4498  *      FIXME: gotta do some vertical scrolling here, like
4499  *              EDIT_EM_LineScroll(wnd, 0, 1);
4500  */
4501 }
4502
4503
4504 /*********************************************************************
4505  *
4506  *      EDIT_VScroll_Hack
4507  *
4508  *      16 bit notepad needs this.  Actually it is not _our_ hack,
4509  *      it is notepad's.  Notepad is sending us scrollbar messages with
4510  *      undocumented parameters without us even having a scrollbar ... !?!?
4511  *
4512  */
4513 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos)
4514 {
4515         INT dy = 0;
4516         LRESULT ret = 0;
4517
4518         if (!(es->flags & EF_VSCROLL_HACK)) {
4519                 ERR("hacked WM_VSCROLL handler invoked\n");
4520                 ERR("      if you are _not_ running 16 bit notepad, please report\n");
4521                 ERR("      (this message is only displayed once per edit control)\n");
4522                 es->flags |= EF_VSCROLL_HACK;
4523         }
4524
4525         switch (action) {
4526         case SB_LINEUP:
4527         case SB_LINEDOWN:
4528         case SB_PAGEUP:
4529         case SB_PAGEDOWN:
4530                 EDIT_EM_Scroll(wnd, es, action);
4531                 return 0;
4532         case SB_TOP:
4533                 dy = -es->y_offset;
4534                 break;
4535         case SB_BOTTOM:
4536                 dy = es->line_count - 1 - es->y_offset;
4537                 break;
4538         case SB_THUMBTRACK:
4539                 es->flags |= EF_VSCROLL_TRACK;
4540                 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
4541                 break;
4542         case SB_THUMBPOSITION:
4543                 es->flags &= ~EF_VSCROLL_TRACK;
4544                 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
4545                         EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4546                 break;
4547         case SB_ENDSCROLL:
4548                 break;
4549
4550         /*
4551          *      FIXME : the next two are undocumented !
4552          *      Are we doing the right thing ?
4553          *      At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4554          *      although it's also a regular control message.
4555          */
4556         case EM_GETTHUMB16:
4557                 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
4558                 break;
4559         case EM_LINESCROLL16:
4560                 dy = pos;
4561                 break;
4562
4563         default:
4564                 ERR("undocumented (hacked) WM_VSCROLL parameter, please report\n");
4565                 return 0;
4566         }
4567         if (dy)
4568                 EDIT_EM_LineScroll(wnd, es, 0, dy);
4569         return ret;
4570 }
4571
4572
4573 /*********************************************************************
4574  *
4575  *      WM_VSCROLL
4576  *
4577  */
4578 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
4579 {
4580         INT dy;
4581
4582         if (!(es->style & ES_MULTILINE))
4583                 return 0;
4584
4585         if (!(es->style & ES_AUTOVSCROLL))
4586                 return 0;
4587
4588         if (!(es->style & WS_VSCROLL))
4589                 return EDIT_VScroll_Hack(wnd, es, action, pos);
4590
4591         dy = 0;
4592         switch (action) {
4593         case SB_LINEUP:
4594         case SB_LINEDOWN:
4595         case SB_PAGEUP:
4596         case SB_PAGEDOWN:
4597                 EDIT_EM_Scroll(wnd, es, action);
4598                 return 0;
4599
4600         case SB_TOP:
4601                 dy = -es->y_offset;
4602                 break;
4603         case SB_BOTTOM:
4604                 dy = es->line_count - 1 - es->y_offset;
4605                 break;
4606         case SB_THUMBTRACK:
4607                 es->flags |= EF_VSCROLL_TRACK;
4608                 dy = pos - es->y_offset;
4609                 break;
4610         case SB_THUMBPOSITION:
4611                 es->flags &= ~EF_VSCROLL_TRACK;
4612                 if (!(dy = pos - es->y_offset)) {
4613                         SetScrollPos(wnd->hwndSelf, SB_VERT, pos, TRUE);
4614                         EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4615                 }
4616                 break;
4617         case SB_ENDSCROLL:
4618                 break;
4619
4620         default:
4621                 ERR("undocumented WM_VSCROLL action %d, please report\n",
4622                     action);
4623                 return 0;
4624         }
4625         if (dy)
4626                 EDIT_EM_LineScroll(wnd, es, 0, dy);
4627         return 0;
4628 }
4629
4630
4631 /*********************************************************************
4632  *
4633  *      EDIT_UpdateText
4634  *
4635  */
4636 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase)
4637 {
4638     EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
4639
4640     /* EF_UPDATE will be turned off in paint */
4641     if (es->flags & EF_UPDATE)
4642         EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
4643
4644     InvalidateRect(wnd->hwndSelf, rc, bErase);
4645 }