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