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