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