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