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