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