Avoid assertion failure when loading an empty data file.
[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 if (delta != 0)
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         EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2255 }
2256
2257
2258 /*********************************************************************
2259  *
2260  *      EDIT_UnlockBuffer
2261  *
2262  */
2263 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2264 {
2265         HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2266
2267         /* Edit window might be already destroyed */
2268         if(!IsWindow(es->hwndSelf))
2269         {
2270             WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2271             return;
2272         }
2273
2274         if (!es->lock_count) {
2275                 ERR("lock_count == 0 ... please report\n");
2276                 return;
2277         }
2278         if (!es->text) {
2279                 ERR("es->text == 0 ... please report\n");
2280                 return;
2281         }
2282
2283         if (force || (es->lock_count == 1)) {
2284             if (es->hloc32W) {
2285                 CHAR *textA = NULL;
2286                 BOOL _16bit = FALSE;
2287                 UINT countA = 0;
2288                 UINT countW = strlenW(es->text) + 1;
2289
2290                 if(es->hloc32A)
2291                 {
2292                     UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2293                     TRACE("Synchronizing with 32-bit ANSI buffer\n");
2294                     TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2295                     countA = LocalSize(es->hloc32A);
2296                     if(countA_new > countA)
2297                     {
2298                         HLOCAL hloc32A_new;
2299                         UINT alloc_size = ROUND_TO_GROW(countA_new);
2300                         TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2301                         hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2302                         if(hloc32A_new)
2303                         {
2304                             es->hloc32A = hloc32A_new;
2305                             countA = LocalSize(hloc32A_new);
2306                             TRACE("Real new size %d bytes\n", countA);
2307                         }
2308                         else
2309                             WARN("FAILED! Will synchronize partially\n");
2310                     }
2311                     textA = LocalLock(es->hloc32A);
2312                 }
2313                 else if(es->hloc16)
2314                 {
2315                     UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2316                     TRACE("Synchronizing with 16-bit ANSI buffer\n");
2317                     TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2318                     countA = LOCAL_Size(hInstance, es->hloc16);
2319                     if(countA_new > countA)
2320                     {
2321                         HLOCAL16 hloc16_new;
2322                         UINT alloc_size = ROUND_TO_GROW(countA_new);
2323                         TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2324                         hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2325                         if(hloc16_new)
2326                         {
2327                             es->hloc16 = hloc16_new;
2328                             countA = LOCAL_Size(hInstance, hloc16_new);
2329                             TRACE("Real new size %d bytes\n", countA);
2330                         }
2331                         else
2332                             WARN("FAILED! Will synchronize partially\n");
2333                     }
2334                     textA = LOCAL_Lock(hInstance, es->hloc16);
2335                     _16bit = TRUE;
2336                 }
2337
2338                 if(textA)
2339                 {
2340                     WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2341                     if(_16bit)
2342                         LOCAL_Unlock(hInstance, es->hloc16);
2343                     else
2344                         LocalUnlock(es->hloc32A);
2345                 }
2346
2347                 LocalUnlock(es->hloc32W);
2348                 es->text = NULL;
2349             }
2350             else {
2351                 ERR("no buffer ... please report\n");
2352                 return;
2353             }
2354         }
2355         es->lock_count--;
2356 }
2357
2358
2359 /*********************************************************************
2360  *
2361  *      EDIT_UpdateScrollInfo
2362  *
2363  */
2364 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2365 {
2366     if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2367     {
2368         SCROLLINFO si;
2369         si.cbSize       = sizeof(SCROLLINFO);
2370         si.fMask        = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2371         si.nMin         = 0;
2372         si.nMax         = es->line_count - 1;
2373         si.nPage        = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2374         si.nPos         = es->y_offset;
2375         TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2376                 si.nMin, si.nMax, si.nPage, si.nPos);
2377         SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2378     }
2379
2380     if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2381     {
2382         SCROLLINFO si;
2383         si.cbSize       = sizeof(SCROLLINFO);
2384         si.fMask        = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2385         si.nMin         = 0;
2386         si.nMax         = es->text_width - 1;
2387         si.nPage        = es->format_rect.right - es->format_rect.left;
2388         si.nPos         = es->x_offset;
2389         TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2390                 si.nMin, si.nMax, si.nPage, si.nPos);
2391         SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2392     }
2393 }
2394
2395 /*********************************************************************
2396  *
2397  *      EDIT_WordBreakProc
2398  *
2399  *      Find the beginning of words.
2400  *      Note:   unlike the specs for a WordBreakProc, this function only
2401  *              allows to be called without linebreaks between s[0] upto
2402  *              s[count - 1].  Remember it is only called
2403  *              internally, so we can decide this for ourselves.
2404  *
2405  */
2406 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2407 {
2408         INT ret = 0;
2409
2410         TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2411
2412         if(!s) return 0;
2413
2414         switch (action) {
2415         case WB_LEFT:
2416                 if (!count)
2417                         break;
2418                 if (index)
2419                         index--;
2420                 if (s[index] == ' ') {
2421                         while (index && (s[index] == ' '))
2422                                 index--;
2423                         if (index) {
2424                                 while (index && (s[index] != ' '))
2425                                         index--;
2426                                 if (s[index] == ' ')
2427                                         index++;
2428                         }
2429                 } else {
2430                         while (index && (s[index] != ' '))
2431                                 index--;
2432                         if (s[index] == ' ')
2433                                 index++;
2434                 }
2435                 ret = index;
2436                 break;
2437         case WB_RIGHT:
2438                 if (!count)
2439                         break;
2440                 if (index)
2441                         index--;
2442                 if (s[index] == ' ')
2443                         while ((index < count) && (s[index] == ' ')) index++;
2444                 else {
2445                         while (s[index] && (s[index] != ' ') && (index < count))
2446                                 index++;
2447                         while ((s[index] == ' ') && (index < count)) index++;
2448                 }
2449                 ret = index;
2450                 break;
2451         case WB_ISDELIMITER:
2452                 ret = (s[index] == ' ');
2453                 break;
2454         default:
2455                 ERR("unknown action code, please report !\n");
2456                 break;
2457         }
2458         return ret;
2459 }
2460
2461
2462 /*********************************************************************
2463  *
2464  *      EM_CHARFROMPOS
2465  *
2466  *      returns line number (not index) in high-order word of result.
2467  *      NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2468  *      to Richedit, not to the edit control. Original documentation is valid.
2469  *      FIXME: do the specs mean to return -1 if outside client area or
2470  *              if outside formatting rectangle ???
2471  *
2472  */
2473 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2474 {
2475         POINT pt;
2476         RECT rc;
2477         INT index;
2478
2479         pt.x = x;
2480         pt.y = y;
2481         GetClientRect(es->hwndSelf, &rc);
2482         if (!PtInRect(&rc, pt))
2483                 return -1;
2484
2485         index = EDIT_CharFromPos(es, x, y, NULL);
2486         return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2487 }
2488
2489
2490 /*********************************************************************
2491  *
2492  *      EM_FMTLINES
2493  *
2494  * Enable or disable soft breaks.
2495  * 
2496  * This means: insert or remove the soft linebreak character (\r\r\n).
2497  * Take care to check if the text still fits the buffer after insertion.
2498  * If not, notify with EN_ERRSPACE.
2499  * 
2500  */
2501 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2502 {
2503         es->flags &= ~EF_USE_SOFTBRK;
2504         if (add_eol) {
2505                 es->flags |= EF_USE_SOFTBRK;
2506                 FIXME("soft break enabled, not implemented\n");
2507         }
2508         return add_eol;
2509 }
2510
2511
2512 /*********************************************************************
2513  *
2514  *      EM_GETHANDLE
2515  *
2516  *      Hopefully this won't fire back at us.
2517  *      We always start with a fixed buffer in the local heap.
2518  *      Despite of the documentation says that the local heap is used
2519  *      only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2520  *      buffer on the local heap.
2521  *
2522  */
2523 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2524 {
2525         HLOCAL hLocal;
2526
2527         if (!(es->style & ES_MULTILINE))
2528                 return 0;
2529
2530         if(es->is_unicode)
2531             hLocal = es->hloc32W;
2532         else
2533         {
2534             if(!es->hloc32A)
2535             {
2536                 CHAR *textA;
2537                 UINT countA, alloc_size;
2538                 TRACE("Allocating 32-bit ANSI alias buffer\n");
2539                 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2540                 alloc_size = ROUND_TO_GROW(countA);
2541                 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2542                 {
2543                     ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2544                     return 0;
2545                 }
2546                 textA = LocalLock(es->hloc32A);
2547                 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2548                 LocalUnlock(es->hloc32A);
2549             }
2550             hLocal = es->hloc32A;
2551         }
2552
2553         TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2554         return hLocal;
2555 }
2556
2557
2558 /*********************************************************************
2559  *
2560  *      EM_GETHANDLE16
2561  *
2562  *      Hopefully this won't fire back at us.
2563  *      We always start with a buffer in 32 bit linear memory.
2564  *      However, with this message a 16 bit application requests
2565  *      a handle of 16 bit local heap memory, where it expects to find
2566  *      the text.
2567  *      It's a pitty that from this moment on we have to use this
2568  *      local heap, because applications may rely on the handle
2569  *      in the future.
2570  *
2571  *      In this function we'll try to switch to local heap.
2572  */
2573 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2574 {
2575         HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2576         CHAR *textA;
2577         UINT countA, alloc_size;
2578
2579         if (!(es->style & ES_MULTILINE))
2580                 return 0;
2581
2582         if (es->hloc16)
2583                 return es->hloc16;
2584
2585         if (!LOCAL_HeapSize(hInstance)) {
2586                 if (!LocalInit16(hInstance, 0,
2587                                 GlobalSize16(hInstance))) {
2588                         ERR("could not initialize local heap\n");
2589                         return 0;
2590                 }
2591                 TRACE("local heap initialized\n");
2592         }
2593
2594         countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2595         alloc_size = ROUND_TO_GROW(countA);
2596
2597         TRACE("Allocating 16-bit ANSI alias buffer\n");
2598         if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2599                 ERR("could not allocate new 16 bit buffer\n");
2600                 return 0;
2601         }
2602
2603         if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2604                 ERR("could not lock new 16 bit buffer\n");
2605                 LOCAL_Free(hInstance, es->hloc16);
2606                 es->hloc16 = 0;
2607                 return 0;
2608         }
2609
2610         WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2611         LOCAL_Unlock(hInstance, es->hloc16);
2612
2613         TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2614         return es->hloc16;
2615 }
2616
2617
2618 /*********************************************************************
2619  *
2620  *      EM_GETLINE
2621  *
2622  */
2623 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2624 {
2625         LPWSTR src;
2626         INT line_len, dst_len;
2627         INT i;
2628
2629         if (es->style & ES_MULTILINE) {
2630                 if (line >= es->line_count)
2631                         return 0;
2632         } else
2633                 line = 0;
2634         i = EDIT_EM_LineIndex(es, line);
2635         src = es->text + i;
2636         line_len = EDIT_EM_LineLength(es, i);
2637         dst_len = *(WORD *)dst;
2638         if(unicode)
2639         {
2640             if(dst_len <= line_len)
2641             {
2642                 memcpy(dst, src, dst_len * sizeof(WCHAR));
2643                 return dst_len;
2644             }
2645             else /* Append 0 if enough space */
2646             {
2647                 memcpy(dst, src, line_len * sizeof(WCHAR));
2648                 dst[line_len] = 0;
2649                 return line_len;
2650             }
2651         }
2652         else
2653         {
2654             INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2655             if(!ret) /* Insufficient buffer size */
2656                 return dst_len;
2657             if(ret < dst_len) /* Append 0 if enough space */
2658                 ((LPSTR)dst)[ret] = 0;
2659             return ret;
2660         }
2661 }
2662
2663
2664 /*********************************************************************
2665  *
2666  *      EM_GETSEL
2667  *
2668  */
2669 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2670 {
2671         UINT s = es->selection_start;
2672         UINT e = es->selection_end;
2673
2674         ORDER_UINT(s, e);
2675         if (start)
2676                 *start = s;
2677         if (end)
2678                 *end = e;
2679         return MAKELONG(s, e);
2680 }
2681
2682
2683 /*********************************************************************
2684  *
2685  *      EM_GETTHUMB
2686  *
2687  *      FIXME: is this right ?  (or should it be only VSCROLL)
2688  *      (and maybe only for edit controls that really have their
2689  *      own scrollbars) (and maybe only for multiline controls ?)
2690  *      All in all: very poorly documented
2691  *
2692  */
2693 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2694 {
2695         return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2696                 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2697 }
2698
2699
2700 /*********************************************************************
2701  *
2702  *      EM_LINEFROMCHAR
2703  *
2704  */
2705 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2706 {
2707         INT line;
2708         LINEDEF *line_def;
2709
2710         if (!(es->style & ES_MULTILINE))
2711                 return 0;
2712         if (index > (INT)strlenW(es->text))
2713                 return es->line_count - 1;
2714         if (index == -1)
2715                 index = min(es->selection_start, es->selection_end);
2716
2717         line = 0;
2718         line_def = es->first_line_def;
2719         index -= line_def->length;
2720         while ((index >= 0) && line_def->next) {
2721                 line++;
2722                 line_def = line_def->next;
2723                 index -= line_def->length;
2724         }
2725         return line;
2726 }
2727
2728
2729 /*********************************************************************
2730  *
2731  *      EM_LINEINDEX
2732  *
2733  */
2734 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2735 {
2736         INT line_index;
2737         LINEDEF *line_def;
2738
2739         if (!(es->style & ES_MULTILINE))
2740                 return 0;
2741         if (line >= es->line_count)
2742                 return -1;
2743
2744         line_index = 0;
2745         line_def = es->first_line_def;
2746         if (line == -1) {
2747                 INT index = es->selection_end - line_def->length;
2748                 while ((index >= 0) && line_def->next) {
2749                         line_index += line_def->length;
2750                         line_def = line_def->next;
2751                         index -= line_def->length;
2752                 }
2753         } else {
2754                 while (line > 0) {
2755                         line_index += line_def->length;
2756                         line_def = line_def->next;
2757                         line--;
2758                 }
2759         }
2760         return line_index;
2761 }
2762
2763
2764 /*********************************************************************
2765  *
2766  *      EM_LINELENGTH
2767  *
2768  */
2769 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2770 {
2771         LINEDEF *line_def;
2772
2773         if (!(es->style & ES_MULTILINE))
2774                 return strlenW(es->text);
2775
2776         if (index == -1) {
2777                 /* get the number of remaining non-selected chars of selected lines */
2778                 INT32 l; /* line number */
2779                 INT32 li; /* index of first char in line */
2780                 INT32 count;
2781                 l = EDIT_EM_LineFromChar(es, es->selection_start);
2782                 /* # chars before start of selection area */
2783                 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2784                 l = EDIT_EM_LineFromChar(es, es->selection_end);
2785                 /* # chars after end of selection */
2786                 li = EDIT_EM_LineIndex(es, l);
2787                 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2788                 return count;
2789         }
2790         line_def = es->first_line_def;
2791         index -= line_def->length;
2792         while ((index >= 0) && line_def->next) {
2793                 line_def = line_def->next;
2794                 index -= line_def->length;
2795         }
2796         return line_def->net_length;
2797 }
2798
2799
2800 /*********************************************************************
2801  *
2802  *      EM_LINESCROLL
2803  *
2804  *      NOTE: dx is in average character widths, dy - in lines;
2805  *
2806  */
2807 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2808 {
2809         if (!(es->style & ES_MULTILINE))
2810                 return FALSE;
2811
2812         dx *= es->char_width;
2813         return EDIT_EM_LineScroll_internal(es, dx, dy);
2814 }
2815
2816 /*********************************************************************
2817  *
2818  *      EDIT_EM_LineScroll_internal
2819  *
2820  *      Version of EDIT_EM_LineScroll for internal use.
2821  *      It doesn't refuse if ES_MULTILINE is set and assumes that
2822  *      dx is in pixels, dy - in lines.
2823  *
2824  */
2825 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2826 {
2827         INT nyoff;
2828         INT x_offset_in_pixels;
2829         INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2830                               es->line_height;
2831
2832         if (es->style & ES_MULTILINE)
2833         {
2834             x_offset_in_pixels = es->x_offset;
2835         }
2836         else
2837         {
2838             dy = 0;
2839             x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2840         }
2841
2842         if (-dx > x_offset_in_pixels)
2843                 dx = -x_offset_in_pixels;
2844         if (dx > es->text_width - x_offset_in_pixels)
2845                 dx = es->text_width - x_offset_in_pixels;
2846         nyoff = max(0, es->y_offset + dy);
2847         if (nyoff >= es->line_count - lines_per_page)
2848                 nyoff = max(0, es->line_count - lines_per_page);
2849         dy = (es->y_offset - nyoff) * es->line_height;
2850         if (dx || dy) {
2851                 RECT rc1;
2852                 RECT rc;
2853
2854                 es->y_offset = nyoff;
2855                 if(es->style & ES_MULTILINE)
2856                     es->x_offset += dx;
2857                 else
2858                     es->x_offset += dx / es->char_width;
2859
2860                 GetClientRect(es->hwndSelf, &rc1);
2861                 IntersectRect(&rc, &rc1, &es->format_rect);
2862                 ScrollWindowEx(es->hwndSelf, -dx, dy,
2863                                 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2864                 /* force scroll info update */
2865                 EDIT_UpdateScrollInfo(es);
2866         }
2867         if (dx && !(es->flags & EF_HSCROLL_TRACK))
2868                 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2869         if (dy && !(es->flags & EF_VSCROLL_TRACK))
2870                 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2871         return TRUE;
2872 }
2873
2874
2875 /*********************************************************************
2876  *
2877  *      EM_POSFROMCHAR
2878  *
2879  */
2880 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2881 {
2882         INT len = strlenW(es->text);
2883         INT l;
2884         INT li;
2885         INT x;
2886         INT y = 0;
2887         HDC dc;
2888         HFONT old_font = 0;
2889         SIZE size;
2890
2891         index = min(index, len);
2892         dc = GetDC(es->hwndSelf);
2893         if (es->font)
2894                 old_font = SelectObject(dc, es->font);
2895         if (es->style & ES_MULTILINE) {
2896                 l = EDIT_EM_LineFromChar(es, index);
2897                 y = (l - es->y_offset) * es->line_height;
2898                 li = EDIT_EM_LineIndex(es, l);
2899                 if (after_wrap && (li == index) && l) {
2900                         INT l2 = l - 1;
2901                         LINEDEF *line_def = es->first_line_def;
2902                         while (l2) {
2903                                 line_def = line_def->next;
2904                                 l2--;
2905                         }
2906                         if (line_def->ending == END_WRAP) {
2907                                 l--;
2908                                 y -= es->line_height;
2909                                 li = EDIT_EM_LineIndex(es, l);
2910                         }
2911                 }
2912                 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2913                                 es->tabs_count, es->tabs)) - es->x_offset;
2914         } else {
2915                 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2916                 if (index < es->x_offset) {
2917                         GetTextExtentPoint32W(dc, text + index,
2918                                         es->x_offset - index, &size);
2919                         x = -size.cx;
2920                 } else {
2921                         GetTextExtentPoint32W(dc, text + es->x_offset,
2922                                         index - es->x_offset, &size);
2923                          x = size.cx;
2924                 }
2925                 y = 0;
2926                 if (es->style & ES_PASSWORD)
2927                         HeapFree(GetProcessHeap(), 0, text);
2928         }
2929         x += es->format_rect.left;
2930         y += es->format_rect.top;
2931         if (es->font)
2932                 SelectObject(dc, old_font);
2933         ReleaseDC(es->hwndSelf, dc);
2934         return MAKELONG((INT16)x, (INT16)y);
2935 }
2936
2937
2938 /*********************************************************************
2939  *
2940  *      EM_REPLACESEL
2941  *
2942  *      FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2943  *
2944  */
2945 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2946 {
2947         UINT strl = strlenW(lpsz_replace);
2948         UINT tl = strlenW(es->text);
2949         UINT utl;
2950         UINT s;
2951         UINT e;
2952         UINT i;
2953         UINT size;
2954         LPWSTR p;
2955         HRGN hrgn = 0;
2956
2957         TRACE("%s, can_undo %d, send_update %d\n",
2958             debugstr_w(lpsz_replace), can_undo, send_update);
2959
2960         s = es->selection_start;
2961         e = es->selection_end;
2962
2963         if ((s == e) && !strl)
2964                 return;
2965
2966         ORDER_UINT(s, e);
2967
2968         /* Issue the EN_MAXTEXT notification and continue with replacing text
2969          * such that buffer limit is honored. */
2970         size = tl - (e - s) + strl;
2971         if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
2972                 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
2973                 strl = es->buffer_limit - (tl - (e-s));
2974         }
2975
2976         if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2977                 return;
2978
2979         if (e != s) {
2980                 /* there is something to be deleted */
2981                 TRACE("deleting stuff.\n");
2982                 if (can_undo) {
2983                         utl = strlenW(es->undo_text);
2984                         if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2985                                 /* undo-buffer is extended to the right */
2986                                 EDIT_MakeUndoFit(es, utl + e - s);
2987                                 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2988                                 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2989                         } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2990                                 /* undo-buffer is extended to the left */
2991                                 EDIT_MakeUndoFit(es, utl + e - s);
2992                                 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2993                                         p[e - s] = p[0];
2994                                 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2995                                         p[i] = (es->text + s)[i];
2996                                 es->undo_position = s;
2997                         } else {
2998                                 /* new undo-buffer */
2999                                 EDIT_MakeUndoFit(es, e - s);
3000                                 strncpyW(es->undo_text, es->text + s, e - s + 1);
3001                                 es->undo_text[e - s] = 0; /* ensure 0 termination */
3002                                 es->undo_position = s;
3003                         }
3004                         /* any deletion makes the old insertion-undo invalid */
3005                         es->undo_insert_count = 0;
3006                 } else
3007                         EDIT_EM_EmptyUndoBuffer(es);
3008
3009                 /* now delete */
3010                 strcpyW(es->text + s, es->text + e);
3011         }
3012         if (strl) {
3013                 /* there is an insertion */
3014                 if (can_undo) {
3015                         if ((s == es->undo_position) ||
3016                                         ((es->undo_insert_count) &&
3017                                         (s == es->undo_position + es->undo_insert_count)))
3018                                 /*
3019                                  * insertion is new and at delete position or
3020                                  * an extension to either left or right
3021                                  */
3022                                 es->undo_insert_count += strl;
3023                         else {
3024                                 /* new insertion undo */
3025                                 es->undo_position = s;
3026                                 es->undo_insert_count = strl;
3027                                 /* new insertion makes old delete-buffer invalid */
3028                                 *es->undo_text = '\0';
3029                         }
3030                 } else
3031                         EDIT_EM_EmptyUndoBuffer(es);
3032
3033                 /* now insert */
3034                 tl = strlenW(es->text);
3035                 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));
3036                 for (p = es->text + tl ; p >= es->text + s ; p--)
3037                         p[strl] = p[0];
3038                 for (i = 0 , p = es->text + s ; i < strl ; i++)
3039                         p[i] = lpsz_replace[i];
3040                 if(es->style & ES_UPPERCASE)
3041                         CharUpperBuffW(p, strl);
3042                 else if(es->style & ES_LOWERCASE)
3043                         CharLowerBuffW(p, strl);
3044                 s += strl;
3045         }
3046         if (es->style & ES_MULTILINE)
3047         {
3048                 INT s = min(es->selection_start, es->selection_end);
3049
3050                 hrgn = CreateRectRgn(0, 0, 0, 0);
3051                 EDIT_BuildLineDefs_ML(es, s, s + strl,
3052                                 strl - abs(es->selection_end - es->selection_start), hrgn);
3053         }
3054         else
3055             EDIT_CalcLineWidth_SL(es);
3056
3057         EDIT_EM_SetSel(es, s, s, FALSE);
3058         es->flags |= EF_MODIFIED;
3059         if (send_update) es->flags |= EF_UPDATE;
3060         if (hrgn)
3061         {
3062                 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3063                 DeleteObject(hrgn);
3064         }
3065         else
3066             EDIT_UpdateText(es, NULL, TRUE);
3067
3068         EDIT_EM_ScrollCaret(es);
3069
3070         /* force scroll info update */
3071         EDIT_UpdateScrollInfo(es);
3072
3073
3074         if(es->flags & EF_UPDATE)
3075         {
3076             es->flags &= ~EF_UPDATE;
3077             EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3078         }
3079 }
3080
3081
3082 /*********************************************************************
3083  *
3084  *      EM_SCROLL
3085  *
3086  */
3087 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3088 {
3089         INT dy;
3090
3091         if (!(es->style & ES_MULTILINE))
3092                 return (LRESULT)FALSE;
3093
3094         dy = 0;
3095
3096         switch (action) {
3097         case SB_LINEUP:
3098                 if (es->y_offset)
3099                         dy = -1;
3100                 break;
3101         case SB_LINEDOWN:
3102                 if (es->y_offset < es->line_count - 1)
3103                         dy = 1;
3104                 break;
3105         case SB_PAGEUP:
3106                 if (es->y_offset)
3107                         dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3108                 break;
3109         case SB_PAGEDOWN:
3110                 if (es->y_offset < es->line_count - 1)
3111                         dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3112                 break;
3113         default:
3114                 return (LRESULT)FALSE;
3115         }
3116         if (dy) {
3117             INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3118             /* check if we are going to move too far */
3119             if(es->y_offset + dy > es->line_count - vlc)
3120                 dy = es->line_count - vlc - es->y_offset;
3121
3122             /* Notification is done in EDIT_EM_LineScroll */
3123             if(dy)
3124                 EDIT_EM_LineScroll(es, 0, dy);
3125         }
3126         return MAKELONG((INT16)dy, (BOOL16)TRUE);
3127 }
3128
3129
3130 /*********************************************************************
3131  *
3132  *      EM_SCROLLCARET
3133  *
3134  */
3135 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3136 {
3137         if (es->style & ES_MULTILINE) {
3138                 INT l;
3139                 INT li;
3140                 INT vlc;
3141                 INT ww;
3142                 INT cw = es->char_width;
3143                 INT x;
3144                 INT dy = 0;
3145                 INT dx = 0;
3146
3147                 l = EDIT_EM_LineFromChar(es, es->selection_end);
3148                 li = EDIT_EM_LineIndex(es, l);
3149                 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3150                 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3151                 if (l >= es->y_offset + vlc)
3152                         dy = l - vlc + 1 - es->y_offset;
3153                 if (l < es->y_offset)
3154                         dy = l - es->y_offset;
3155                 ww = es->format_rect.right - es->format_rect.left;
3156                 if (x < es->format_rect.left)
3157                         dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3158                 if (x > es->format_rect.right)
3159                         dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3160                 if (dy || dx)
3161                 {
3162                     /* check if we are going to move too far */
3163                     if(es->x_offset + dx + ww > es->text_width)
3164                         dx = es->text_width - ww - es->x_offset;
3165                     if(dx || dy)
3166                         EDIT_EM_LineScroll_internal(es, dx, dy);
3167                 }
3168         } else {
3169                 INT x;
3170                 INT goal;
3171                 INT format_width;
3172
3173                 if (!(es->style & ES_AUTOHSCROLL))
3174                         return;
3175
3176                 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3177                 format_width = es->format_rect.right - es->format_rect.left;
3178                 if (x < es->format_rect.left) {
3179                         goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3180                         do {
3181                                 es->x_offset--;
3182                                 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3183                         } while ((x < goal) && es->x_offset);
3184                         /* FIXME: use ScrollWindow() somehow to improve performance */
3185                         EDIT_UpdateText(es, NULL, TRUE);
3186                 } else if (x > es->format_rect.right) {
3187                         INT x_last;
3188                         INT len = strlenW(es->text);
3189                         goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3190                         do {
3191                                 es->x_offset++;
3192                                 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3193                                 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3194                         } while ((x > goal) && (x_last > es->format_rect.right));
3195                         /* FIXME: use ScrollWindow() somehow to improve performance */
3196                         EDIT_UpdateText(es, NULL, TRUE);
3197                 }
3198         }
3199
3200     if(es->flags & EF_FOCUSED)
3201         EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3202 }
3203
3204
3205 /*********************************************************************
3206  *
3207  *      EM_SETHANDLE
3208  *
3209  *      FIXME:  ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3210  *
3211  */
3212 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3213 {
3214         HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3215
3216         if (!(es->style & ES_MULTILINE))
3217                 return;
3218
3219         if (!hloc) {
3220                 WARN("called with NULL handle\n");
3221                 return;
3222         }
3223
3224         EDIT_UnlockBuffer(es, TRUE);
3225
3226         if(es->hloc16)
3227         {
3228             LOCAL_Free(hInstance, es->hloc16);
3229             es->hloc16 = (HLOCAL16)NULL;
3230         }
3231
3232         if(es->is_unicode)
3233         {
3234             if(es->hloc32A)
3235             {
3236                 LocalFree(es->hloc32A);
3237                 es->hloc32A = NULL;
3238             }
3239             es->hloc32W = hloc;
3240         }
3241         else
3242         {
3243             INT countW, countA;
3244             HLOCAL hloc32W_new;
3245             WCHAR *textW;
3246             CHAR *textA;
3247
3248             countA = LocalSize(hloc);
3249             textA = LocalLock(hloc);
3250             countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3251             if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3252             {
3253                 ERR("Could not allocate new unicode buffer\n");
3254                 return;
3255             }
3256             textW = LocalLock(hloc32W_new);
3257             MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3258             LocalUnlock(hloc32W_new);
3259             LocalUnlock(hloc);
3260
3261             if(es->hloc32W)
3262                 LocalFree(es->hloc32W);
3263
3264             es->hloc32W = hloc32W_new;
3265             es->hloc32A = hloc;
3266         }
3267
3268         es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3269
3270         EDIT_LockBuffer(es);
3271
3272         es->x_offset = es->y_offset = 0;
3273         es->selection_start = es->selection_end = 0;
3274         EDIT_EM_EmptyUndoBuffer(es);
3275         es->flags &= ~EF_MODIFIED;
3276         es->flags &= ~EF_UPDATE;
3277         EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3278         EDIT_UpdateText(es, NULL, TRUE);
3279         EDIT_EM_ScrollCaret(es);
3280         /* force scroll info update */
3281         EDIT_UpdateScrollInfo(es);
3282 }
3283
3284
3285 /*********************************************************************
3286  *
3287  *      EM_SETHANDLE16
3288  *
3289  *      FIXME:  ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3290  *
3291  */
3292 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3293 {
3294         HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3295         INT countW, countA;
3296         HLOCAL hloc32W_new;
3297         WCHAR *textW;
3298         CHAR *textA;
3299
3300         if (!(es->style & ES_MULTILINE))
3301                 return;
3302
3303         if (!hloc) {
3304                 WARN("called with NULL handle\n");
3305                 return;
3306         }
3307
3308         EDIT_UnlockBuffer(es, TRUE);
3309
3310         if(es->hloc32A)
3311         {
3312             LocalFree(es->hloc32A);
3313             es->hloc32A = NULL;
3314         }
3315
3316         countA = LOCAL_Size(hInstance, hloc);
3317         textA = LOCAL_Lock(hInstance, hloc);
3318         countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3319         if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3320         {
3321             ERR("Could not allocate new unicode buffer\n");
3322             return;
3323         }
3324         textW = LocalLock(hloc32W_new);
3325         MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3326         LocalUnlock(hloc32W_new);
3327         LOCAL_Unlock(hInstance, hloc);
3328
3329         if(es->hloc32W)
3330             LocalFree(es->hloc32W);
3331
3332         es->hloc32W = hloc32W_new;
3333         es->hloc16 = hloc;
3334
3335         es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3336
3337         EDIT_LockBuffer(es);
3338
3339         es->x_offset = es->y_offset = 0;
3340         es->selection_start = es->selection_end = 0;
3341         EDIT_EM_EmptyUndoBuffer(es);
3342         es->flags &= ~EF_MODIFIED;
3343         es->flags &= ~EF_UPDATE;
3344         EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3345         EDIT_UpdateText(es, NULL, TRUE);
3346         EDIT_EM_ScrollCaret(es);
3347         /* force scroll info update */
3348         EDIT_UpdateScrollInfo(es);
3349 }
3350
3351
3352 /*********************************************************************
3353  *
3354  *      EM_SETLIMITTEXT
3355  *
3356  *      FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3357  *      However, the windows version is not complied to yet in all of edit.c
3358  *
3359  *  Additionally as the wrapper for RichEdit controls we need larger buffers
3360  *  at present -1 will represent nolimit
3361  */
3362 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3363 {
3364     if (limit == 0xFFFFFFFF)
3365         es->buffer_limit = -1;
3366     else if (es->style & ES_MULTILINE) {
3367                 if (limit)
3368                         es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3369                 else
3370                         es->buffer_limit = BUFLIMIT_MULTI;
3371         } else {
3372                 if (limit)
3373                         es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3374                 else
3375                         es->buffer_limit = BUFLIMIT_SINGLE;
3376         }
3377 }
3378
3379
3380 /*********************************************************************
3381  *
3382  *      EM_SETMARGINS
3383  *
3384  * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3385  * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3386  * margin according to the textmetrics of the current font.
3387  *
3388  * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3389  * of the char's width as the margin, but this is not how Windows handles this.
3390  * For all other fonts Windows sets the margins to zero.
3391  *
3392  */
3393 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3394                                INT left, INT right)
3395 {
3396         TEXTMETRICW tm;
3397         INT default_left_margin  = 0; /* in pixels */
3398         INT default_right_margin = 0; /* in pixels */
3399
3400         /* Set the default margins depending on the font */
3401         if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3402             HDC dc = GetDC(es->hwndSelf);
3403             HFONT old_font = SelectObject(dc, es->font);
3404             GetTextMetricsW(dc, &tm);
3405             /* The default margins are only non zero for TrueType or Vector fonts */
3406             if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3407                 /* This must be calculated more exactly! But how? */
3408                 default_left_margin = tm.tmAveCharWidth / 3;
3409                 default_right_margin = tm.tmAveCharWidth / 3;
3410             }
3411             SelectObject(dc, old_font);
3412             ReleaseDC(es->hwndSelf, dc);
3413         }
3414
3415         if (action & EC_LEFTMARGIN) {
3416                 if (left != EC_USEFONTINFO)
3417                         es->left_margin = left;
3418                 else
3419                         es->left_margin = default_left_margin;
3420         }
3421
3422         if (action & EC_RIGHTMARGIN) {
3423                 if (right != EC_USEFONTINFO)
3424                         es->right_margin = right;
3425                 else
3426                         es->right_margin = default_right_margin;
3427         }
3428         TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3429 }
3430
3431
3432 /*********************************************************************
3433  *
3434  *      EM_SETPASSWORDCHAR
3435  *
3436  */
3437 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3438 {
3439         LONG style;
3440
3441         if (es->style & ES_MULTILINE)
3442                 return;
3443
3444         if (es->password_char == c)
3445                 return;
3446
3447         style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3448         es->password_char = c;
3449         if (c) {
3450             SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3451             es->style |= ES_PASSWORD;
3452         } else {
3453             SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3454             es->style &= ~ES_PASSWORD;
3455         }
3456         EDIT_UpdateText(es, NULL, TRUE);
3457 }
3458
3459
3460 /*********************************************************************
3461  *
3462  *      EDIT_EM_SetSel
3463  *
3464  *      note:   unlike the specs say: the order of start and end
3465  *              _is_ preserved in Windows.  (i.e. start can be > end)
3466  *              In other words: this handler is OK
3467  *
3468  */
3469 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3470 {
3471         UINT old_start = es->selection_start;
3472         UINT old_end = es->selection_end;
3473         UINT len = strlenW(es->text);
3474
3475         if (start == (UINT)-1) {
3476                 start = es->selection_end;
3477                 end = es->selection_end;
3478         } else {
3479                 start = min(start, len);
3480                 end = min(end, len);
3481         }
3482         es->selection_start = start;
3483         es->selection_end = end;
3484         if (after_wrap)
3485                 es->flags |= EF_AFTER_WRAP;
3486         else
3487                 es->flags &= ~EF_AFTER_WRAP;
3488 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3489         ORDER_UINT(start, end);
3490         ORDER_UINT(end, old_end);
3491         ORDER_UINT(start, old_start);
3492         ORDER_UINT(old_start, old_end);
3493         if (end != old_start)
3494         {
3495 /*
3496  * One can also do
3497  *          ORDER_UINT32(end, old_start);
3498  *          EDIT_InvalidateText(es, start, end);
3499  *          EDIT_InvalidateText(es, old_start, old_end);
3500  * in place of the following if statement.
3501  */
3502             if (old_start > end )
3503             {
3504                 EDIT_InvalidateText(es, start, end);
3505                 EDIT_InvalidateText(es, old_start, old_end);
3506             }
3507             else
3508             {
3509                 EDIT_InvalidateText(es, start, old_start);
3510                 EDIT_InvalidateText(es, end, old_end);
3511             }
3512         }
3513         else EDIT_InvalidateText(es, start, old_end);
3514 }
3515
3516
3517 /*********************************************************************
3518  *
3519  *      EM_SETTABSTOPS
3520  *
3521  */
3522 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3523 {
3524         if (!(es->style & ES_MULTILINE))
3525                 return FALSE;
3526         HeapFree(GetProcessHeap(), 0, es->tabs);
3527         es->tabs_count = count;
3528         if (!count)
3529                 es->tabs = NULL;
3530         else {
3531                 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3532                 memcpy(es->tabs, tabs, count * sizeof(INT));
3533         }
3534         return TRUE;
3535 }
3536
3537
3538 /*********************************************************************
3539  *
3540  *      EM_SETTABSTOPS16
3541  *
3542  */
3543 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3544 {
3545         if (!(es->style & ES_MULTILINE))
3546                 return FALSE;
3547         HeapFree(GetProcessHeap(), 0, es->tabs);
3548         es->tabs_count = count;
3549         if (!count)
3550                 es->tabs = NULL;
3551         else {
3552                 INT i;
3553                 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3554                 for (i = 0 ; i < count ; i++)
3555                         es->tabs[i] = *tabs++;
3556         }
3557         return TRUE;
3558 }
3559
3560
3561 /*********************************************************************
3562  *
3563  *      EM_SETWORDBREAKPROC
3564  *
3565  */
3566 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3567 {
3568         if (es->word_break_proc == wbp)
3569                 return;
3570
3571         es->word_break_proc = wbp;
3572         es->word_break_proc16 = NULL;
3573
3574         if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3575                 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3576                 EDIT_UpdateText(es, NULL, TRUE);
3577         }
3578 }
3579
3580
3581 /*********************************************************************
3582  *
3583  *      EM_SETWORDBREAKPROC16
3584  *
3585  */
3586 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3587 {
3588         if (es->word_break_proc16 == wbp)
3589                 return;
3590
3591         es->word_break_proc = NULL;
3592         es->word_break_proc16 = wbp;
3593         if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3594                 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3595                 EDIT_UpdateText(es, NULL, TRUE);
3596         }
3597 }
3598
3599
3600 /*********************************************************************
3601  *
3602  *      EM_UNDO / WM_UNDO
3603  *
3604  */
3605 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3606 {
3607         INT ulength;
3608         LPWSTR utext;
3609
3610         /* As per MSDN spec, for a single-line edit control,
3611            the return value is always TRUE */
3612         if( es->style & ES_READONLY )
3613             return !(es->style & ES_MULTILINE);
3614
3615         ulength = strlenW(es->undo_text);
3616
3617         utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3618
3619         strcpyW(utext, es->undo_text);
3620
3621         TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3622                      es->undo_insert_count, debugstr_w(utext));
3623
3624         EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3625         EDIT_EM_EmptyUndoBuffer(es);
3626         EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3627         EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3628         /* send the notification after the selection start and end are set */
3629         EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3630         EDIT_EM_ScrollCaret(es);
3631         HeapFree(GetProcessHeap(), 0, utext);
3632
3633         TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3634                         es->undo_insert_count, debugstr_w(es->undo_text));
3635         return TRUE;
3636 }
3637
3638
3639 /*********************************************************************
3640  *
3641  *      WM_CHAR
3642  *
3643  */
3644 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3645 {
3646         BOOL control;
3647
3648         control = GetKeyState(VK_CONTROL) & 0x8000;
3649
3650         switch (c) {
3651         case '\r':
3652             /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3653             if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3654                 break;
3655         case '\n':
3656                 if (es->style & ES_MULTILINE) {
3657                         if (es->style & ES_READONLY) {
3658                                 EDIT_MoveHome(es, FALSE);
3659                                 EDIT_MoveDown_ML(es, FALSE);
3660                         } else {
3661                                 static const WCHAR cr_lfW[] = {'\r','\n',0};
3662                                 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3663                         }
3664                 }
3665                 break;
3666         case '\t':
3667                 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3668                 {
3669                         static const WCHAR tabW[] = {'\t',0};
3670                         EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3671                 }
3672                 break;
3673         case VK_BACK:
3674                 if (!(es->style & ES_READONLY) && !control) {
3675                         if (es->selection_start != es->selection_end)
3676                                 EDIT_WM_Clear(es);
3677                         else {
3678                                 /* delete character left of caret */
3679                                 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3680                                 EDIT_MoveBackward(es, TRUE);
3681                                 EDIT_WM_Clear(es);
3682                         }
3683                 }
3684                 break;
3685         case 0x03: /* ^C */
3686                 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3687                 break;
3688         case 0x16: /* ^V */
3689                 if (!(es->style & ES_READONLY))
3690                     SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3691                 break;
3692         case 0x18: /* ^X */
3693                 if (!(es->style & ES_READONLY))
3694                     SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3695                 break;
3696
3697         default:
3698                 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3699                 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3700                         break;
3701                         
3702                 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3703                         WCHAR str[2];
3704                         str[0] = c;
3705                         str[1] = '\0';
3706                         EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3707                 }
3708                 break;
3709         }
3710 }
3711
3712
3713 /*********************************************************************
3714  *
3715  *      WM_COMMAND
3716  *
3717  */
3718 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3719 {
3720         if (code || control)
3721                 return;
3722
3723         switch (id) {
3724                 case EM_UNDO:
3725                         EDIT_EM_Undo(es);
3726                         break;
3727                 case WM_CUT:
3728                         EDIT_WM_Cut(es);
3729                         break;
3730                 case WM_COPY:
3731                         EDIT_WM_Copy(es);
3732                         break;
3733                 case WM_PASTE:
3734                         EDIT_WM_Paste(es);
3735                         break;
3736                 case WM_CLEAR:
3737                         EDIT_WM_Clear(es);
3738                         break;
3739                 case EM_SETSEL:
3740                         EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3741                         EDIT_EM_ScrollCaret(es);
3742                         break;
3743                 default:
3744                         ERR("unknown menu item, please report\n");
3745                         break;
3746         }
3747 }
3748
3749
3750 /*********************************************************************
3751  *
3752  *      WM_CONTEXTMENU
3753  *
3754  *      Note: the resource files resource/sysres_??.rc cannot define a
3755  *              single popup menu.  Hence we use a (dummy) menubar
3756  *              containing the single popup menu as its first item.
3757  *
3758  *      FIXME: the message identifiers have been chosen arbitrarily,
3759  *              hence we use MF_BYPOSITION.
3760  *              We might as well use the "real" values (anybody knows ?)
3761  *              The menu definition is in resources/sysres_??.rc.
3762  *              Once these are OK, we better use MF_BYCOMMAND here
3763  *              (as we do in EDIT_WM_Command()).
3764  *
3765  */
3766 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3767 {
3768         HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3769         HMENU popup = GetSubMenu(menu, 0);
3770         UINT start = es->selection_start;
3771         UINT end = es->selection_end;
3772
3773         ORDER_UINT(start, end);
3774
3775         /* undo */
3776         EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3777         /* cut */
3778         EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3779         /* copy */
3780         EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3781         /* paste */
3782         EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3783         /* delete */
3784         EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3785         /* select all */
3786         EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3787
3788         TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3789         DestroyMenu(menu);
3790 }
3791
3792
3793 /*********************************************************************
3794  *
3795  *      WM_COPY
3796  *
3797  */
3798 static void EDIT_WM_Copy(EDITSTATE *es)
3799 {
3800         INT s = min(es->selection_start, es->selection_end);
3801         INT e = max(es->selection_start, es->selection_end);
3802         HGLOBAL hdst;
3803         LPWSTR dst;
3804
3805         if (e == s) return;
3806
3807         hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3808         dst = GlobalLock(hdst);
3809         strncpyW(dst, es->text + s, e - s);
3810         dst[e - s] = 0; /* ensure 0 termination */
3811         TRACE("%s\n", debugstr_w(dst));
3812         GlobalUnlock(hdst);
3813         OpenClipboard(es->hwndSelf);
3814         EmptyClipboard();
3815         SetClipboardData(CF_UNICODETEXT, hdst);
3816         CloseClipboard();
3817 }
3818
3819
3820 /*********************************************************************
3821  *
3822  *      WM_CREATE
3823  *
3824  */
3825 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3826 {
3827         TRACE("%s\n", debugstr_w(name));
3828        /*
3829         *       To initialize some final structure members, we call some helper
3830         *       functions.  However, since the EDITSTATE is not consistent (i.e.
3831         *       not fully initialized), we should be very careful which
3832         *       functions can be called, and in what order.
3833         */
3834         EDIT_WM_SetFont(es, 0, FALSE);
3835         EDIT_EM_EmptyUndoBuffer(es);
3836
3837        if (name && *name) {
3838            EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3839            /* if we insert text to the editline, the text scrolls out
3840             * of the window, as the caret is placed after the insert
3841             * pos normally; thus we reset es->selection... to 0 and
3842             * update caret
3843             */
3844            es->selection_start = es->selection_end = 0;
3845            /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3846             * Messages are only to be sent when the USER does something to
3847             * change the contents. So I am removing this EN_CHANGE
3848             *
3849             * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3850             */
3851            EDIT_EM_ScrollCaret(es);
3852        }
3853        /* force scroll info update */
3854        EDIT_UpdateScrollInfo(es);
3855        /* The rule seems to return 1 here for success */
3856        /* Power Builder masked edit controls will crash  */
3857        /* if not. */
3858        /* FIXME: is that in all cases so ? */
3859        return 1;
3860 }
3861
3862
3863 /*********************************************************************
3864  *
3865  *      WM_DESTROY
3866  *
3867  */
3868 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3869 {
3870         LINEDEF *pc, *pp;
3871
3872         if (es->hloc32W) {
3873                 while (LocalUnlock(es->hloc32W)) ;
3874                 LocalFree(es->hloc32W);
3875         }
3876         if (es->hloc32A) {
3877                 while (LocalUnlock(es->hloc32A)) ;
3878                 LocalFree(es->hloc32A);
3879         }
3880         if (es->hloc16) {
3881                 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3882                 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3883                 LOCAL_Free(hInstance, es->hloc16);
3884         }
3885
3886         pc = es->first_line_def;
3887         while (pc)
3888         {
3889                 pp = pc->next;
3890                 HeapFree(GetProcessHeap(), 0, pc);
3891                 pc = pp;
3892         }
3893
3894         SetWindowLongW( es->hwndSelf, 0, 0 );
3895         HeapFree(GetProcessHeap(), 0, es);
3896
3897         return 0;
3898 }
3899
3900
3901 /*********************************************************************
3902  *
3903  *      WM_ERASEBKGND
3904  *
3905  */
3906 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3907 {
3908     /* we do the proper erase in EDIT_WM_Paint */
3909     return -1;
3910 }
3911
3912
3913 /*********************************************************************
3914  *
3915  *      WM_GETTEXT
3916  *
3917  */
3918 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3919 {
3920     if(!count) return 0;
3921
3922     if(unicode)
3923     {
3924         lstrcpynW(dst, es->text, count);
3925         return strlenW(dst);
3926     }
3927     else
3928     {
3929         LPSTR textA = (LPSTR)dst;
3930         if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3931             textA[count - 1] = 0; /* ensure 0 termination */
3932         return strlen(textA);
3933     }
3934 }
3935
3936 /*********************************************************************
3937  *
3938  *      WM_HSCROLL
3939  *
3940  */
3941 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3942 {
3943         INT dx;
3944         INT fw;
3945
3946         if (!(es->style & ES_MULTILINE))
3947                 return 0;
3948
3949         if (!(es->style & ES_AUTOHSCROLL))
3950                 return 0;
3951
3952         dx = 0;
3953         fw = es->format_rect.right - es->format_rect.left;
3954         switch (action) {
3955         case SB_LINELEFT:
3956                 TRACE("SB_LINELEFT\n");
3957                 if (es->x_offset)
3958                         dx = -es->char_width;
3959                 break;
3960         case SB_LINERIGHT:
3961                 TRACE("SB_LINERIGHT\n");
3962                 if (es->x_offset < es->text_width)
3963                         dx = es->char_width;
3964                 break;
3965         case SB_PAGELEFT:
3966                 TRACE("SB_PAGELEFT\n");
3967                 if (es->x_offset)
3968                         dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3969                 break;
3970         case SB_PAGERIGHT:
3971                 TRACE("SB_PAGERIGHT\n");
3972                 if (es->x_offset < es->text_width)
3973                         dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3974                 break;
3975         case SB_LEFT:
3976                 TRACE("SB_LEFT\n");
3977                 if (es->x_offset)
3978                         dx = -es->x_offset;
3979                 break;
3980         case SB_RIGHT:
3981                 TRACE("SB_RIGHT\n");
3982                 if (es->x_offset < es->text_width)
3983                         dx = es->text_width - es->x_offset;
3984                 break;
3985         case SB_THUMBTRACK:
3986                 TRACE("SB_THUMBTRACK %d\n", pos);
3987                 es->flags |= EF_HSCROLL_TRACK;
3988                 if(es->style & WS_HSCROLL)
3989                     dx = pos - es->x_offset;
3990                 else
3991                 {
3992                     INT fw, new_x;
3993                     /* Sanity check */
3994                     if(pos < 0 || pos > 100) return 0;
3995                     /* Assume default scroll range 0-100 */
3996                     fw = es->format_rect.right - es->format_rect.left;
3997                     new_x = pos * (es->text_width - fw) / 100;
3998                     dx = es->text_width ? (new_x - es->x_offset) : 0;
3999                 }
4000                 break;
4001         case SB_THUMBPOSITION:
4002                 TRACE("SB_THUMBPOSITION %d\n", pos);
4003                 es->flags &= ~EF_HSCROLL_TRACK;
4004                 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4005                     dx = pos - es->x_offset;
4006                 else
4007                 {
4008                     INT fw, new_x;
4009                     /* Sanity check */
4010                     if(pos < 0 || pos > 100) return 0;
4011                     /* Assume default scroll range 0-100 */
4012                     fw = es->format_rect.right - es->format_rect.left;
4013                     new_x = pos * (es->text_width - fw) / 100;
4014                     dx = es->text_width ? (new_x - es->x_offset) : 0;
4015                 }
4016                 if (!dx) {
4017                         /* force scroll info update */
4018                         EDIT_UpdateScrollInfo(es);
4019                         EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
4020                 }
4021                 break;
4022         case SB_ENDSCROLL:
4023                 TRACE("SB_ENDSCROLL\n");
4024                 break;
4025         /*
4026          *      FIXME : the next two are undocumented !
4027          *      Are we doing the right thing ?
4028          *      At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4029          *      although it's also a regular control message.
4030          */
4031         case EM_GETTHUMB: /* this one is used by NT notepad */
4032         case EM_GETTHUMB16:
4033         {
4034                 LRESULT ret;
4035                 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4036                     ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4037                 else
4038                 {
4039                     /* Assume default scroll range 0-100 */
4040                     INT fw = es->format_rect.right - es->format_rect.left;
4041                     ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4042                 }
4043                 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4044                 return ret;
4045         }
4046         case EM_LINESCROLL16:
4047                 TRACE("EM_LINESCROLL16\n");
4048                 dx = pos;
4049                 break;
4050
4051         default:
4052                 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4053                     action, action);
4054                 return 0;
4055         }
4056         if (dx)
4057         {
4058             INT fw = es->format_rect.right - es->format_rect.left;
4059             /* check if we are going to move too far */
4060             if(es->x_offset + dx + fw > es->text_width)
4061                 dx = es->text_width - fw - es->x_offset;
4062             if(dx)
4063                 EDIT_EM_LineScroll_internal(es, dx, 0);
4064         }
4065         return 0;
4066 }
4067
4068
4069 /*********************************************************************
4070  *
4071  *      EDIT_CheckCombo
4072  *
4073  */
4074 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4075 {
4076    HWND hLBox = es->hwndListBox;
4077    HWND hCombo;
4078    BOOL bDropped;
4079    int  nEUI;
4080
4081    if (!hLBox)
4082       return FALSE;
4083
4084    hCombo   = GetParent(es->hwndSelf);
4085    bDropped = TRUE;
4086    nEUI     = 0;
4087
4088    TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4089
4090    if (key == VK_UP || key == VK_DOWN)
4091    {
4092       if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4093          nEUI = 1;
4094
4095       if (msg == WM_KEYDOWN || nEUI)
4096           bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4097    }
4098
4099    switch (msg)
4100    {
4101       case WM_KEYDOWN:
4102          if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4103          {
4104             /* make sure ComboLBox pops up */
4105             SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4106             key = VK_F4;
4107             nEUI = 2;
4108          }
4109
4110          SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4111          break;
4112
4113       case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4114          if (nEUI)
4115             SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4116          else
4117             SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4118          break;
4119    }
4120
4121    if(nEUI == 2)
4122       SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4123
4124    return TRUE;
4125 }
4126
4127
4128 /*********************************************************************
4129  *
4130  *      WM_KEYDOWN
4131  *
4132  *      Handling of special keys that don't produce a WM_CHAR
4133  *      (i.e. non-printable keys) & Backspace & Delete
4134  *
4135  */
4136 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4137 {
4138         BOOL shift;
4139         BOOL control;
4140
4141         if (GetKeyState(VK_MENU) & 0x8000)
4142                 return 0;
4143
4144         shift = GetKeyState(VK_SHIFT) & 0x8000;
4145         control = GetKeyState(VK_CONTROL) & 0x8000;
4146
4147         switch (key) {
4148         case VK_F4:
4149         case VK_UP:
4150                 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4151                         break;
4152
4153                 /* fall through */
4154         case VK_LEFT:
4155                 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4156                         EDIT_MoveUp_ML(es, shift);
4157                 else
4158                         if (control)
4159                                 EDIT_MoveWordBackward(es, shift);
4160                         else
4161                                 EDIT_MoveBackward(es, shift);
4162                 break;
4163         case VK_DOWN:
4164                 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4165                         break;
4166                 /* fall through */
4167         case VK_RIGHT:
4168                 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4169                         EDIT_MoveDown_ML(es, shift);
4170                 else if (control)
4171                         EDIT_MoveWordForward(es, shift);
4172                 else
4173                         EDIT_MoveForward(es, shift);
4174                 break;
4175         case VK_HOME:
4176                 EDIT_MoveHome(es, shift);
4177                 break;
4178         case VK_END:
4179                 EDIT_MoveEnd(es, shift);
4180                 break;
4181         case VK_PRIOR:
4182                 if (es->style & ES_MULTILINE)
4183                         EDIT_MovePageUp_ML(es, shift);
4184                 else
4185                         EDIT_CheckCombo(es, WM_KEYDOWN, key);
4186                 break;
4187         case VK_NEXT:
4188                 if (es->style & ES_MULTILINE)
4189                         EDIT_MovePageDown_ML(es, shift);
4190                 else
4191                         EDIT_CheckCombo(es, WM_KEYDOWN, key);
4192                 break;
4193         case VK_DELETE:
4194                 if (!(es->style & ES_READONLY) && !(shift && control)) {
4195                         if (es->selection_start != es->selection_end) {
4196                                 if (shift)
4197                                         EDIT_WM_Cut(es);
4198                                 else
4199                                         EDIT_WM_Clear(es);
4200                         } else {
4201                                 if (shift) {
4202                                         /* delete character left of caret */
4203                                         EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4204                                         EDIT_MoveBackward(es, TRUE);
4205                                         EDIT_WM_Clear(es);
4206                                 } else if (control) {
4207                                         /* delete to end of line */
4208                                         EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4209                                         EDIT_MoveEnd(es, TRUE);
4210                                         EDIT_WM_Clear(es);
4211                                 } else {
4212                                         /* delete character right of caret */
4213                                         EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4214                                         EDIT_MoveForward(es, TRUE);
4215                                         EDIT_WM_Clear(es);
4216                                 }
4217                         }
4218                 }
4219                 break;
4220         case VK_INSERT:
4221                 if (shift) {
4222                         if (!(es->style & ES_READONLY))
4223                                 EDIT_WM_Paste(es);
4224                 } else if (control)
4225                         EDIT_WM_Copy(es);
4226                 break;
4227         case VK_RETURN:
4228             /* If the edit doesn't want the return send a message to the default object */
4229             if(!(es->style & ES_WANTRETURN))
4230             {
4231                 HWND hwndParent = GetParent(es->hwndSelf);
4232                 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4233                 if (HIWORD(dw) == DC_HASDEFID)
4234                 {
4235                     SendMessageW( hwndParent, WM_COMMAND,
4236                                   MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4237                               (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4238                 }
4239             }
4240             break;
4241         }
4242         return 0;
4243 }
4244
4245
4246 /*********************************************************************
4247  *
4248  *      WM_KILLFOCUS
4249  *
4250  */
4251 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4252 {
4253         es->flags &= ~EF_FOCUSED;
4254         DestroyCaret();
4255         if(!(es->style & ES_NOHIDESEL))
4256                 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4257         EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4258         return 0;
4259 }
4260
4261
4262 /*********************************************************************
4263  *
4264  *      WM_LBUTTONDBLCLK
4265  *
4266  *      The caret position has been set on the WM_LBUTTONDOWN message
4267  *
4268  */
4269 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4270 {
4271         INT s;
4272         INT e = es->selection_end;
4273         INT l;
4274         INT li;
4275         INT ll;
4276
4277         if (!(es->flags & EF_FOCUSED))
4278                 return 0;
4279
4280         l = EDIT_EM_LineFromChar(es, e);
4281         li = EDIT_EM_LineIndex(es, l);
4282         ll = EDIT_EM_LineLength(es, e);
4283         s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4284         e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4285         EDIT_EM_SetSel(es, s, e, FALSE);
4286         EDIT_EM_ScrollCaret(es);
4287         return 0;
4288 }
4289
4290
4291 /*********************************************************************
4292  *
4293  *      WM_LBUTTONDOWN
4294  *
4295  */
4296 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4297 {
4298         INT e;
4299         BOOL after_wrap;
4300
4301         SetFocus(es->hwndSelf);
4302         if (!(es->flags & EF_FOCUSED))
4303                 return 0;
4304
4305         es->bCaptureState = TRUE;
4306         SetCapture(es->hwndSelf);
4307         EDIT_ConfinePoint(es, &x, &y);
4308         e = EDIT_CharFromPos(es, x, y, &after_wrap);
4309         EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4310         EDIT_EM_ScrollCaret(es);
4311         es->region_posx = es->region_posy = 0;
4312         SetTimer(es->hwndSelf, 0, 100, NULL);
4313         return 0;
4314 }
4315
4316
4317 /*********************************************************************
4318  *
4319  *      WM_LBUTTONUP
4320  *
4321  */
4322 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4323 {
4324         if (es->bCaptureState) {
4325                 KillTimer(es->hwndSelf, 0);
4326                 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4327         }
4328         es->bCaptureState = FALSE;
4329         return 0;
4330 }
4331
4332
4333 /*********************************************************************
4334  *
4335  *      WM_MBUTTONDOWN
4336  *
4337  */
4338 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4339 {
4340     SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4341     return 0;
4342 }
4343
4344
4345 /*********************************************************************
4346  *
4347  *      WM_MOUSEMOVE
4348  *
4349  */
4350 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4351 {
4352         INT e;
4353         BOOL after_wrap;
4354         INT prex, prey;
4355
4356         if (GetCapture() != es->hwndSelf)
4357                 return 0;
4358
4359         /*
4360          *      FIXME: gotta do some scrolling if outside client
4361          *              area.  Maybe reset the timer ?
4362          */
4363         prex = x; prey = y;
4364         EDIT_ConfinePoint(es, &x, &y);
4365         es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4366         es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4367         e = EDIT_CharFromPos(es, x, y, &after_wrap);
4368         EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4369         EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4370         return 0;
4371 }
4372
4373
4374 /*********************************************************************
4375  *
4376  *      WM_NCCREATE
4377  *
4378  * See also EDIT_WM_StyleChanged
4379  */
4380 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4381 {
4382         EDITSTATE *es;
4383         UINT alloc_size;
4384
4385         TRACE("Creating %s edit control, style = %08lx\n",
4386                 unicode ? "Unicode" : "ANSI", lpcs->style);
4387
4388         if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4389                 return FALSE;
4390         SetWindowLongW( hwnd, 0, (LONG)es );
4391
4392        /*
4393         *      Note: since the EDITSTATE has not been fully initialized yet,
4394         *            we can't use any API calls that may send
4395         *            WM_XXX messages before WM_NCCREATE is completed.
4396         */
4397
4398         es->is_unicode = unicode;
4399         es->style = lpcs->style;
4400
4401         es->bEnableState = !(es->style & WS_DISABLED);
4402
4403         es->hwndSelf = hwnd;
4404         /* Save parent, which will be notified by EN_* messages */
4405         es->hwndParent = lpcs->hwndParent;
4406
4407         if (es->style & ES_COMBO)
4408            es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4409
4410         /* Number overrides lowercase overrides uppercase (at least it
4411          * does in Win95).  However I'll bet that ES_NUMBER would be
4412          * invalid under Win 3.1.
4413          */
4414         if (es->style & ES_NUMBER) {
4415                 ; /* do not override the ES_NUMBER */
4416         }  else if (es->style & ES_LOWERCASE) {
4417                 es->style &= ~ES_UPPERCASE;
4418         }
4419         if (es->style & ES_MULTILINE) {
4420                 es->buffer_limit = BUFLIMIT_MULTI;
4421                 if (es->style & WS_VSCROLL)
4422                         es->style |= ES_AUTOVSCROLL;
4423                 if (es->style & WS_HSCROLL)
4424                         es->style |= ES_AUTOHSCROLL;
4425                 es->style &= ~ES_PASSWORD;
4426                 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4427                         /* Confirmed - RIGHT overrides CENTER */
4428                         if (es->style & ES_RIGHT)
4429                                 es->style &= ~ES_CENTER;
4430                         es->style &= ~WS_HSCROLL;
4431                 }
4432
4433                 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4434                 es->style |= ES_AUTOVSCROLL;
4435         } else {
4436                 es->buffer_limit = BUFLIMIT_SINGLE;
4437                 es->style &= ~ES_CENTER;
4438                 es->style &= ~ES_RIGHT;
4439                 es->style &= ~WS_HSCROLL;
4440                 es->style &= ~WS_VSCROLL;
4441                 if (es->style & ES_PASSWORD)
4442                         es->password_char = '*';
4443
4444                 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4445                 es->style |= ES_AUTOHSCROLL;
4446         }
4447
4448         alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4449         if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4450             return FALSE;
4451         es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4452
4453         if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4454                 return FALSE;
4455         es->undo_buffer_size = es->buffer_size;
4456
4457         if (es->style & ES_MULTILINE)
4458                 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4459                         return FALSE;
4460         es->line_count = 1;
4461
4462         /*
4463          * In Win95 look and feel, the WS_BORDER style is replaced by the
4464          * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4465          * control a nonclient area so we don't need to draw the border.
4466          * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4467          * a nonclient area and we should handle painting the border ourselves.
4468          *
4469          * When making modifications please ensure that the code still works 
4470          * for edit controls created directly with style 0x50800000, exStyle 0
4471          * (which should have a single pixel border)
4472          */
4473         if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4474                 es->style &= ~WS_BORDER;
4475         else if (es->style & WS_BORDER)
4476                 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4477
4478         return TRUE;
4479 }
4480
4481 /*********************************************************************
4482  *
4483  *      WM_PAINT
4484  *
4485  */
4486 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4487 {
4488         PAINTSTRUCT ps;
4489         INT i;
4490         HDC dc;
4491         HFONT old_font = 0;
4492         RECT rc;
4493         RECT rcClient;
4494         RECT rcLine;
4495         RECT rcRgn;
4496         HBRUSH brush;
4497         BOOL rev = es->bEnableState &&
4498                                 ((es->flags & EF_FOCUSED) ||
4499                                         (es->style & ES_NOHIDESEL));
4500         dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4501
4502         GetClientRect(es->hwndSelf, &rcClient);
4503
4504         /* paint the background */
4505         if (!(brush = EDIT_NotifyCtlColor(es, dc)))
4506                 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
4507         IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4508         GetClipBox(dc, &rc);
4509         FillRect(dc, &rc, brush);
4510
4511         /* draw the border */
4512         if(es->style & WS_BORDER) {
4513                 rc = rcClient;
4514                 if(es->style & ES_MULTILINE) {
4515                         if(es->style & WS_HSCROLL) rc.bottom++;
4516                         if(es->style & WS_VSCROLL) rc.right++;
4517                 }
4518                 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4519         }
4520         IntersectClipRect(dc, es->format_rect.left,
4521                                 es->format_rect.top,
4522                                 es->format_rect.right,
4523                                 es->format_rect.bottom);
4524         if (es->style & ES_MULTILINE) {
4525                 rc = rcClient;
4526                 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4527         }
4528         if (es->font)
4529                 old_font = SelectObject(dc, es->font);
4530         EDIT_NotifyCtlColor(es, dc);
4531
4532         if (!es->bEnableState)
4533                 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4534         GetClipBox(dc, &rcRgn);
4535         if (es->style & ES_MULTILINE) {
4536                 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4537                 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4538                         EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4539                         if (IntersectRect(&rc, &rcRgn, &rcLine))
4540                                 EDIT_PaintLine(es, dc, i, rev);
4541                 }
4542         } else {
4543                 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4544                 if (IntersectRect(&rc, &rcRgn, &rcLine))
4545                         EDIT_PaintLine(es, dc, 0, rev);
4546         }
4547         if (es->font)
4548                 SelectObject(dc, old_font);
4549
4550         if (!hdc)
4551             EndPaint(es->hwndSelf, &ps);
4552 }
4553
4554
4555 /*********************************************************************
4556  *
4557  *      WM_PASTE
4558  *
4559  */
4560 static void EDIT_WM_Paste(EDITSTATE *es)
4561 {
4562         HGLOBAL hsrc;
4563         LPWSTR src;
4564
4565         /* Protect read-only edit control from modification */
4566         if(es->style & ES_READONLY)
4567             return;
4568
4569         OpenClipboard(es->hwndSelf);
4570         if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4571                 src = (LPWSTR)GlobalLock(hsrc);
4572                 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4573                 GlobalUnlock(hsrc);
4574         }
4575         CloseClipboard();
4576 }
4577
4578
4579 /*********************************************************************
4580  *
4581  *      WM_SETFOCUS
4582  *
4583  */
4584 static void EDIT_WM_SetFocus(EDITSTATE *es)
4585 {
4586         es->flags |= EF_FOCUSED;
4587         CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4588         EDIT_SetCaretPos(es, es->selection_end,
4589                          es->flags & EF_AFTER_WRAP);
4590         if(!(es->style & ES_NOHIDESEL))
4591                 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4592         ShowCaret(es->hwndSelf);
4593         EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4594 }
4595
4596
4597 /*********************************************************************
4598  *
4599  *      WM_SETFONT
4600  *
4601  * With Win95 look the margins are set to default font value unless
4602  * the system font (font == 0) is being set, in which case they are left
4603  * unchanged.
4604  *
4605  */
4606 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4607 {
4608         TEXTMETRICW tm;
4609         HDC dc;
4610         HFONT old_font = 0;
4611         RECT r;
4612
4613         es->font = font;
4614         dc = GetDC(es->hwndSelf);
4615         if (font)
4616                 old_font = SelectObject(dc, font);
4617         GetTextMetricsW(dc, &tm);
4618         es->line_height = tm.tmHeight;
4619         es->char_width = tm.tmAveCharWidth;
4620         if (font)
4621                 SelectObject(dc, old_font);
4622         ReleaseDC(es->hwndSelf, dc);
4623         EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4624                            EC_USEFONTINFO, EC_USEFONTINFO);
4625
4626         /* Force the recalculation of the format rect for each font change */
4627         GetClientRect(es->hwndSelf, &r);
4628         EDIT_SetRectNP(es, &r);
4629
4630         if (es->style & ES_MULTILINE)
4631                 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4632         else
4633             EDIT_CalcLineWidth_SL(es);
4634
4635         if (redraw)
4636                 EDIT_UpdateText(es, NULL, TRUE);
4637         if (es->flags & EF_FOCUSED) {
4638                 DestroyCaret();
4639                 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4640                 EDIT_SetCaretPos(es, es->selection_end,
4641                                  es->flags & EF_AFTER_WRAP);
4642                 ShowCaret(es->hwndSelf);
4643         }
4644 }
4645
4646
4647 /*********************************************************************
4648  *
4649  *      WM_SETTEXT
4650  *
4651  * NOTES
4652  *  For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4653  *  The modified flag is reset. No notifications are sent.
4654  *
4655  *  For single-line controls, reception of WM_SETTEXT triggers:
4656  *  The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4657  *
4658  */
4659 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4660 {
4661     if (!unicode && text)
4662     {
4663         LPCSTR textA = (LPCSTR)text;
4664         INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4665         LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
4666         if (textW)
4667             MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4668         text = textW;
4669     }
4670
4671     EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4672     if (text) 
4673     {
4674         TRACE("%s\n", debugstr_w(text));
4675         EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4676         if(!unicode)
4677             HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
4678     } 
4679     else 
4680     {
4681         static const WCHAR empty_stringW[] = {0};
4682         TRACE("<NULL>\n");
4683         EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4684     }
4685     es->x_offset = 0;
4686     es->flags &= ~EF_MODIFIED;
4687     EDIT_EM_SetSel(es, 0, 0, FALSE);
4688
4689     /* Send the notification after the selection start and end have been set
4690      * edit control doesn't send notification on WM_SETTEXT
4691      * if it is multiline, or it is part of combobox
4692      */
4693     if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4694     {
4695         EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4696         EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4697     }
4698     EDIT_EM_ScrollCaret(es);
4699 }
4700
4701
4702 /*********************************************************************
4703  *
4704  *      WM_SIZE
4705  *
4706  */
4707 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4708 {
4709         if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4710                 RECT rc;
4711                 TRACE("width = %d, height = %d\n", width, height);
4712                 SetRect(&rc, 0, 0, width, height);
4713                 EDIT_SetRectNP(es, &rc);
4714                 EDIT_UpdateText(es, NULL, TRUE);
4715         }
4716 }
4717
4718
4719 /*********************************************************************
4720  *
4721  *      WM_STYLECHANGED
4722  *
4723  * This message is sent by SetWindowLong on having changed either the Style
4724  * or the extended style.
4725  *
4726  * We ensure that the window's version of the styles and the EDITSTATE's agree.
4727  *
4728  * See also EDIT_WM_NCCreate
4729  *
4730  * It appears that the Windows version of the edit control allows the style
4731  * (as retrieved by GetWindowLong) to be any value and maintains an internal
4732  * style variable which will generally be different.  In this function we
4733  * update the internal style based on what changed in the externally visible
4734  * style.
4735  *
4736  * Much of this content as based upon the MSDN, especially:
4737  *  Platform SDK Documentation -> User Interface Services ->
4738  *      Windows User Interface -> Edit Controls -> Edit Control Reference ->
4739  *      Edit Control Styles
4740  */
4741 static LRESULT  EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4742 {
4743         if (GWL_STYLE == which) {
4744                 DWORD style_change_mask;
4745                 DWORD new_style;
4746                 /* Only a subset of changes can be applied after the control
4747                  * has been created.
4748                  */
4749                 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4750                                     ES_NUMBER;
4751                 if (es->style & ES_MULTILINE)
4752                         style_change_mask |= ES_WANTRETURN;
4753
4754                 new_style = style->styleNew & style_change_mask;
4755
4756                 /* Number overrides lowercase overrides uppercase (at least it
4757                  * does in Win95).  However I'll bet that ES_NUMBER would be
4758                  * invalid under Win 3.1.
4759                  */
4760                 if (new_style & ES_NUMBER) {
4761                         ; /* do not override the ES_NUMBER */
4762                 }  else if (new_style & ES_LOWERCASE) {
4763                         new_style &= ~ES_UPPERCASE;
4764                 }
4765
4766                 es->style = (es->style & ~style_change_mask) | new_style;
4767         } else if (GWL_EXSTYLE == which) {
4768                 ; /* FIXME - what is needed here */
4769         } else {
4770                 WARN ("Invalid style change %d\n",which);
4771         }
4772
4773         return 0;
4774 }
4775
4776 /*********************************************************************
4777  *
4778  *      WM_SYSKEYDOWN
4779  *
4780  */
4781 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4782 {
4783         if ((key == VK_BACK) && (key_data & 0x2000)) {
4784                 if (EDIT_EM_CanUndo(es))
4785                         EDIT_EM_Undo(es);
4786                 return 0;
4787         } else if (key == VK_UP || key == VK_DOWN) {
4788                 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4789                         return 0;
4790         }
4791         return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4792 }
4793
4794
4795 /*********************************************************************
4796  *
4797  *      WM_TIMER
4798  *
4799  */
4800 static void EDIT_WM_Timer(EDITSTATE *es)
4801 {
4802         if (es->region_posx < 0) {
4803                 EDIT_MoveBackward(es, TRUE);
4804         } else if (es->region_posx > 0) {
4805                 EDIT_MoveForward(es, TRUE);
4806         }
4807 /*
4808  *      FIXME: gotta do some vertical scrolling here, like
4809  *              EDIT_EM_LineScroll(hwnd, 0, 1);
4810  */
4811 }
4812
4813 /*********************************************************************
4814  *
4815  *      WM_VSCROLL
4816  *
4817  */
4818 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4819 {
4820         INT dy;
4821
4822         if (!(es->style & ES_MULTILINE))
4823                 return 0;
4824
4825         if (!(es->style & ES_AUTOVSCROLL))
4826                 return 0;
4827
4828         dy = 0;
4829         switch (action) {
4830         case SB_LINEUP:
4831         case SB_LINEDOWN:
4832         case SB_PAGEUP:
4833         case SB_PAGEDOWN:
4834                 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4835                                                    (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4836                                                     (action == SB_PAGEUP ? "SB_PAGEUP" :
4837                                                      "SB_PAGEDOWN"))));
4838                 EDIT_EM_Scroll(es, action);
4839                 return 0;
4840         case SB_TOP:
4841                 TRACE("SB_TOP\n");
4842                 dy = -es->y_offset;
4843                 break;
4844         case SB_BOTTOM:
4845                 TRACE("SB_BOTTOM\n");
4846                 dy = es->line_count - 1 - es->y_offset;
4847                 break;
4848         case SB_THUMBTRACK:
4849                 TRACE("SB_THUMBTRACK %d\n", pos);
4850                 es->flags |= EF_VSCROLL_TRACK;
4851                 if(es->style & WS_VSCROLL)
4852                     dy = pos - es->y_offset;
4853                 else
4854                 {
4855                     /* Assume default scroll range 0-100 */
4856                     INT vlc, new_y;
4857                     /* Sanity check */
4858                     if(pos < 0 || pos > 100) return 0;
4859                     vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4860                     new_y = pos * (es->line_count - vlc) / 100;
4861                     dy = es->line_count ? (new_y - es->y_offset) : 0;
4862                     TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4863                             es->line_count, es->y_offset, pos, dy);
4864                 }
4865                 break;
4866         case SB_THUMBPOSITION:
4867                 TRACE("SB_THUMBPOSITION %d\n", pos);
4868                 es->flags &= ~EF_VSCROLL_TRACK;
4869                 if(es->style & WS_VSCROLL)
4870                     dy = pos - es->y_offset;
4871                 else
4872                 {
4873                     /* Assume default scroll range 0-100 */
4874                     INT vlc, new_y;
4875                     /* Sanity check */
4876                     if(pos < 0 || pos > 100) return 0;
4877                     vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4878                     new_y = pos * (es->line_count - vlc) / 100;
4879                     dy = es->line_count ? (new_y - es->y_offset) : 0;
4880                     TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4881                             es->line_count, es->y_offset, pos, dy);
4882                 }
4883                 if (!dy)
4884                 {
4885                         /* force scroll info update */
4886                         EDIT_UpdateScrollInfo(es);
4887                         EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4888                 }
4889                 break;
4890         case SB_ENDSCROLL:
4891                 TRACE("SB_ENDSCROLL\n");
4892                 break;
4893         /*
4894          *      FIXME : the next two are undocumented !
4895          *      Are we doing the right thing ?
4896          *      At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4897          *      although it's also a regular control message.
4898          */
4899         case EM_GETTHUMB: /* this one is used by NT notepad */
4900         case EM_GETTHUMB16:
4901         {
4902                 LRESULT ret;
4903                 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4904                     ret = GetScrollPos(es->hwndSelf, SB_VERT);
4905                 else
4906                 {
4907                     /* Assume default scroll range 0-100 */
4908                     INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4909                     ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4910                 }
4911                 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4912                 return ret;
4913         }
4914         case EM_LINESCROLL16:
4915                 TRACE("EM_LINESCROLL16 %d\n", pos);
4916                 dy = pos;
4917                 break;
4918
4919         default:
4920                 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4921                     action, action);
4922                 return 0;
4923         }
4924         if (dy)
4925                 EDIT_EM_LineScroll(es, 0, dy);
4926         return 0;
4927 }
4928
4929 /*********************************************************************
4930  *
4931  *      EDIT_UpdateText
4932  *
4933  */
4934 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4935 {
4936     if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4937     InvalidateRgn(es->hwndSelf, hrgn, bErase);
4938 }
4939
4940
4941 /*********************************************************************
4942  *
4943  *      EDIT_UpdateText
4944  *
4945  */
4946 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4947 {
4948     if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4949     InvalidateRect(es->hwndSelf, rc, bErase);
4950 }