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