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