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