- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[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 "winnt.h"
15 #include "win.h"
16 #include "wine/winbase16.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 enum
47 {
48         END_0 = 0,      /* line ends with terminating '\0' character */
49         END_WRAP,       /* line is wrapped */
50         END_HARD,       /* line ends with a hard return '\r\n' */
51         END_SOFT        /* line ends with a soft return '\r\r\n' */
52 } LINE_END;
53
54 typedef struct tagLINEDEF {
55         INT32 length;           /* bruto length of a line in bytes */
56         INT32 net_length;       /* netto length of a line in visible characters */
57         LINE_END ending;
58         INT32 width;            /* width of the line in pixels */
59         struct tagLINEDEF *next;
60 } LINEDEF;
61
62 typedef struct
63 {
64         HANDLE32 heap;                  /* our own heap */
65         LPSTR text;                     /* the actual contents of the control */
66         INT32 buffer_size;              /* the size of the buffer */
67         INT32 buffer_limit;             /* the maximum size to which the buffer may grow */
68         HFONT32 font;                   /* NULL means standard system font */
69         INT32 x_offset;                 /* scroll offset        for multi lines this is in pixels
70                                                                 for single lines it's in characters */
71         INT32 line_height;              /* height of a screen line in pixels */
72         INT32 char_width;               /* average character width in pixels */
73         DWORD style;                    /* sane version of wnd->dwStyle */
74         WORD flags;                     /* flags that are not in es->style or wnd->flags (EF_XXX) */
75         INT32 undo_insert_count;        /* number of characters inserted in sequence */
76         INT32 undo_position;            /* character index of the insertion and deletion */
77         LPSTR undo_text;                /* deleted text */
78         INT32 undo_buffer_size;         /* size of the deleted text buffer */
79         INT32 selection_start;          /* == selection_end if no selection */
80         INT32 selection_end;            /* == current caret position */
81         CHAR password_char;             /* == 0 if no password char, and for multi line controls */
82         INT32 left_margin;              /* in pixels */
83         INT32 right_margin;             /* in pixels */
84         RECT32 format_rect;
85         INT32 region_posx;              /* Position of cursor relative to region: */
86         INT32 region_posy;              /* -1: to left, 0: within, 1: to right */
87         EDITWORDBREAKPROC16 word_break_proc16;
88         EDITWORDBREAKPROC32A word_break_proc32A;
89         INT32 line_count;               /* number of lines */
90         INT32 y_offset;                 /* scroll offset in number of lines */
91         /*
92          *      only for multi line controls
93          */
94         INT32 lock_count;               /* amount of re-entries in the EditWndProc */
95         INT32 tabs_count;
96         LPINT32 tabs;
97         INT32 text_width;               /* width of the widest line in pixels */
98         LINEDEF *first_line_def;        /* linked list of (soft) linebreaks */
99         HLOCAL16 hloc16;                /* for controls receiving EM_GETHANDLE16 */
100         HLOCAL32 hloc32;                /* for controls receiving EM_GETHANDLE */
101 } EDITSTATE;
102
103
104 #define SWAP_INT32(x,y) do { INT32 temp = (INT32)(x); (x) = (INT32)(y); (y) = temp; } while(0)
105 #define ORDER_INT32(x,y) do { if ((INT32)(y) < (INT32)(x)) SWAP_INT32((x),(y)); } while(0)
106
107 #define SWAP_UINT32(x,y) do { UINT32 temp = (UINT32)(x); (x) = (UINT32)(y); (y) = temp; } while(0)
108 #define ORDER_UINT32(x,y) do { if ((UINT32)(y) < (UINT32)(x)) SWAP_UINT32((x),(y)); } while(0)
109
110 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
111         ({TRACE(edit, "notification " str " sent to hwnd=%08x\n", \
112                        (UINT32)(hwnd));})
113
114 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
115         (SendMessage32A((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
116                         (WPARAM32)(hdc), (LPARAM)(wnd)->hwndSelf))
117 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
118         (DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str), \
119         SendMessage32A((wnd)->parent->hwndSelf, WM_COMMAND, \
120                         MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
121                         (LPARAM)(wnd)->hwndSelf))
122 #define DPRINTF_EDIT_MSG16(str) \
123         TRACE(edit, \
124                      "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
125                      (UINT32)hwnd, (UINT32)wParam, (UINT32)lParam)
126 #define DPRINTF_EDIT_MSG32(str) \
127         TRACE(edit, \
128                      "32 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
129                      (UINT32)hwnd, (UINT32)wParam, (UINT32)lParam)
130
131 /*********************************************************************
132  *
133  *      Declarations
134  *
135  */
136
137 /*
138  *      These functions have trivial implementations
139  *      We still like to call them internally
140  *      "static __inline__" makes them more like macro's
141  */
142 static __inline__ BOOL32        EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es);
143 static __inline__ void          EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es);
144 static __inline__ void          EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
145 static __inline__ void          EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
146 /*
147  *      This is the only exported function
148  */
149 LRESULT WINAPI EditWndProc( HWND32 hwnd, UINT32 msg,
150                             WPARAM32 wParam, LPARAM lParam );
151 /*
152  *      Helper functions only valid for one type of control
153  */
154 static void     EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
155 static LPSTR    EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es);
156 static void     EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
157 static void     EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
158 static void     EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
159 static void     EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
160 /*
161  *      Helper functions valid for both single line _and_ multi line controls
162  */
163 static INT32    EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT32 start, INT32 index, INT32 count, INT32 action);
164 static INT32    EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y, LPBOOL32 after_wrap);
165 static void     EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT32 x, LPINT32 y);
166 static void     EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT32 line, INT32 scol, INT32 ecol, LPRECT32 rc);
167 static void     EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end);
168 static void     EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
169 static BOOL32   EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT32 size);
170 static BOOL32   EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT32 size);
171 static void     EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL32 extend);
172 static void     EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL32 extend);
173 static void     EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL32 extend);
174 static void     EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL32 extend);
175 static void     EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL32 extend);
176 static void     EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL32 extend);
177 static void     EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC32 hdc, INT32 line, BOOL32 rev);
178 static INT32    EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC32 hdc, INT32 x, INT32 y, INT32 line, INT32 col, INT32 count, BOOL32 rev);
179 static void     EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT32 pos, BOOL32 after_wrap); 
180 static void     EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT32 lprc);
181 static void     EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL32 force);
182 static INT32    EDIT_WordBreakProc(LPSTR s, INT32 index, INT32 count, INT32 action);
183 /*
184  *      EM_XXX message handlers
185  */
186 static LRESULT  EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y);
187 static BOOL32   EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL32 add_eol);
188 static HLOCAL32 EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es);
189 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
190 static INT32    EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT32 line, LPSTR lpch);
191 static LRESULT  EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT32 start, LPUINT32 end);
192 static LRESULT  EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
193 static INT32    EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT32 index);
194 static INT32    EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT32 line);
195 static INT32    EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT32 index);
196 static BOOL32   EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT32 dx, INT32 dy);
197 static LRESULT  EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT32 index, BOOL32 after_wrap);
198 static void     EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL32 can_undo, LPCSTR lpsz_replace);
199 static LRESULT  EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT32 action);
200 static void     EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
201 static void     EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL32 hloc);
202 static void     EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
203 static void     EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT32 limit);
204 static void     EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT32 action, INT32 left, INT32 right);
205 static void     EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c);
206 static void     EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT32 start, UINT32 end, BOOL32 after_wrap);
207 static BOOL32   EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT32 count, LPINT32 tabs);
208 static BOOL32   EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT32 count, LPINT16 tabs);
209 static void     EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC32A wbp);
210 static void     EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
211 static BOOL32   EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
212 /*
213  *      WM_XXX message handlers
214  */
215 static void     EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data);
216 static void     EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT32 code, INT32 id, HWND32 conrtol);
217 static void     EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND32 hwnd, INT32 x, INT32 y);
218 static void     EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
219 static LRESULT  EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCT32A cs);
220 static void     EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
221 static LRESULT  EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC32 dc);
222 static INT32    EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT32 count, LPSTR text);
223 static LRESULT  EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar);
224 static LRESULT  EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data);
225 static LRESULT  EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND32 window_getting_focus);
226 static LRESULT  EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
227 static LRESULT  EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
228 static LRESULT  EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
229 static LRESULT  EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
230 static LRESULT  EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCT32A cs);
231 static void     EDIT_WM_Paint(WND *wnd, EDITSTATE *es);
232 static void     EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
233 static void     EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND32 window_losing_focus);
234 static void     EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT32 font, BOOL32 redraw);
235 static void     EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text);
236 static void     EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT32 action, INT32 width, INT32 height);
237 static LRESULT  EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data);
238 static void     EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT32 id, TIMERPROC32 timer_proc);
239 static LRESULT  EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar);
240
241
242 /*********************************************************************
243  *
244  *      EM_CANUNDO
245  *
246  */
247 static __inline__ BOOL32 EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es)
248 {
249         return (es->undo_insert_count || lstrlen32A(es->undo_text));
250 }
251
252
253 /*********************************************************************
254  *
255  *      EM_EMPTYUNDOBUFFER
256  *
257  */
258 static __inline__ void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es)
259 {
260         es->undo_insert_count = 0;
261         *es->undo_text = '\0';
262 }
263
264
265 /*********************************************************************
266  *
267  *      WM_CLEAR
268  *
269  */
270 static __inline__ void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
271 {
272         EDIT_EM_ReplaceSel(wnd, es, TRUE, "");
273 }
274
275
276 /*********************************************************************
277  *
278  *      WM_CUT
279  *
280  */
281 static __inline__ void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
282 {
283         EDIT_WM_Copy(wnd, es);
284         EDIT_WM_Clear(wnd, es);
285 }
286
287
288 /*********************************************************************
289  *
290  *      EditWndProc()
291  *
292  *      The messages are in the order of the actual integer values
293  *      (which can be found in include/windows.h)
294  *      Whereever possible the 16 bit versions are converted to
295  *      the 32 bit ones, so that we can 'fall through' to the
296  *      helper functions.  These are mostly 32 bit (with a few
297  *      exceptions, clearly indicated by a '16' extension to their
298  *      names).
299  *
300  */
301 LRESULT WINAPI EditWndProc( HWND32 hwnd, UINT32 msg,
302                             WPARAM32 wParam, LPARAM lParam )
303 {
304         WND *wnd = WIN_FindWndPtr(hwnd);
305         EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
306         LRESULT result = 0;
307
308         switch (msg) {
309         case WM_DESTROY:
310                 DPRINTF_EDIT_MSG32("WM_DESTROY");
311                 EDIT_WM_Destroy(wnd, es);
312                 return 0;
313
314         case WM_NCCREATE:
315                 DPRINTF_EDIT_MSG32("WM_NCCREATE");
316                 return EDIT_WM_NCCreate(wnd, (LPCREATESTRUCT32A)lParam);
317         }
318
319         if (!es)
320                 return DefWindowProc32A(hwnd, msg, wParam, lParam);
321
322         EDIT_LockBuffer(wnd, es);
323         switch (msg) {
324         case EM_GETSEL16:
325                 DPRINTF_EDIT_MSG16("EM_GETSEL");
326                 wParam = 0;
327                 lParam = 0;
328                 /* fall through */
329         case EM_GETSEL32:
330                 DPRINTF_EDIT_MSG32("EM_GETSEL");
331                 result = EDIT_EM_GetSel(wnd, es, (LPUINT32)wParam, (LPUINT32)lParam);
332                 break;
333
334         case EM_SETSEL16:
335                 DPRINTF_EDIT_MSG16("EM_SETSEL");
336                 if (SLOWORD(lParam) == -1)
337                         EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
338                 else
339                         EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
340                 if (!wParam)
341                         EDIT_EM_ScrollCaret(wnd, es);
342                 result = 1;
343                 break;
344         case EM_SETSEL32:
345                 DPRINTF_EDIT_MSG32("EM_SETSEL");
346                 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
347                 result = 1;
348                 break;
349
350         case EM_GETRECT16:
351                 DPRINTF_EDIT_MSG16("EM_GETRECT");
352                 if (lParam)
353                         CONV_RECT32TO16(&es->format_rect, (LPRECT16)PTR_SEG_TO_LIN(lParam));
354                 break;
355         case EM_GETRECT32:
356                 DPRINTF_EDIT_MSG32("EM_GETRECT");
357                 if (lParam)
358                         CopyRect32((LPRECT32)lParam, &es->format_rect);
359                 break;
360
361         case EM_SETRECT16:
362                 DPRINTF_EDIT_MSG16("EM_SETRECT");
363                 if ((es->style & ES_MULTILINE) && lParam) {
364                         RECT32 rc;
365                         CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
366                         EDIT_SetRectNP(wnd, es, &rc);
367                         InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
368                 }
369                 break;
370         case EM_SETRECT32:
371                 DPRINTF_EDIT_MSG32("EM_SETRECT");
372                 if ((es->style & ES_MULTILINE) && lParam) {
373                         EDIT_SetRectNP(wnd, es, (LPRECT32)lParam);
374                         InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
375                 }
376                 break;
377
378         case EM_SETRECTNP16:
379                 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
380                 if ((es->style & ES_MULTILINE) && lParam) {
381                         RECT32 rc;
382                         CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
383                         EDIT_SetRectNP(wnd, es, &rc);
384                 }
385                 break;
386         case EM_SETRECTNP32:
387                 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
388                 if ((es->style & ES_MULTILINE) && lParam)
389                         EDIT_SetRectNP(wnd, es, (LPRECT32)lParam);
390                 break;
391
392         case EM_SCROLL16:
393                 DPRINTF_EDIT_MSG16("EM_SCROLL");
394                 /* fall through */
395         case EM_SCROLL32:
396                 DPRINTF_EDIT_MSG32("EM_SCROLL");
397                 result = EDIT_EM_Scroll(wnd, es, (INT32)wParam);
398                 break;
399
400         case EM_LINESCROLL16:
401                 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
402                 wParam = (WPARAM32)(INT32)SHIWORD(lParam);
403                 lParam = (LPARAM)(INT32)SLOWORD(lParam);
404                 /* fall through */
405         case EM_LINESCROLL32:
406                 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
407                 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT32)wParam, (INT32)lParam);
408                 break;
409
410         case EM_SCROLLCARET16:
411                 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
412                 /* fall through */
413         case EM_SCROLLCARET32:
414                 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
415                 EDIT_EM_ScrollCaret(wnd, es);
416                 result = 1;
417                 break;
418
419         case EM_GETMODIFY16:
420                 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
421                 /* fall through */
422         case EM_GETMODIFY32:
423                 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
424                 return ((es->flags & EF_MODIFIED) != 0);
425                 break;
426
427         case EM_SETMODIFY16:
428                 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
429                 /* fall through */
430         case EM_SETMODIFY32:
431                 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
432                 if (wParam)
433                         es->flags |= EF_MODIFIED;
434                 else
435                         es->flags &= ~EF_MODIFIED;
436                 break;
437
438         case EM_GETLINECOUNT16:
439                 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
440                 /* fall through */
441         case EM_GETLINECOUNT32:
442                 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
443                 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
444                 break;
445
446         case EM_LINEINDEX16:
447                 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
448                 if ((INT16)wParam == -1)
449                         wParam = (WPARAM32)-1;
450                 /* fall through */
451         case EM_LINEINDEX32:
452                 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
453                 result = (LRESULT)EDIT_EM_LineIndex(wnd, es, (INT32)wParam);
454                 break;
455
456         case EM_SETHANDLE16:
457                 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
458                 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
459                 break;
460         case EM_SETHANDLE32:
461                 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
462                 EDIT_EM_SetHandle(wnd, es, (HLOCAL32)wParam);
463                 break;
464
465         case EM_GETHANDLE16:
466                 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
467                 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
468                 break;
469         case EM_GETHANDLE32:
470                 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
471                 result = (LRESULT)EDIT_EM_GetHandle(wnd, es);
472                 break;
473
474         case EM_GETTHUMB16:
475                 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
476                 /* fall through */
477         case EM_GETTHUMB32:
478                 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
479                 result = EDIT_EM_GetThumb(wnd, es);
480                 break;
481
482         /* messages 0x00bf and 0x00c0 missing from specs */
483
484         case WM_USER+15:
485                 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
486                 /* fall through */
487         case 0x00bf:
488                 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
489                 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
490                 break;
491
492         case WM_USER+16:
493                 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
494                 /* fall through */
495         case 0x00c0:
496                 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
497                 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
498                 break;
499
500         case EM_LINELENGTH16:
501                 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
502                 /* fall through */
503         case EM_LINELENGTH32:
504                 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
505                 result = (LRESULT)EDIT_EM_LineLength(wnd, es, (INT32)wParam);
506                 break;
507
508         case EM_REPLACESEL16:
509                 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
510                 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
511                 /* fall through */
512         case EM_REPLACESEL32:
513                 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
514                 EDIT_EM_ReplaceSel(wnd, es, (BOOL32)wParam, (LPCSTR)lParam);
515                 result = 1;
516                 break;
517
518         /* message 0x00c3 missing from specs */
519
520         case WM_USER+19:
521                 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
522                 /* fall through */
523         case 0x00c3:
524                 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
525                 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
526                 break;
527
528         case EM_GETLINE16:
529                 DPRINTF_EDIT_MSG16("EM_GETLINE");
530                 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
531                 /* fall through */
532         case EM_GETLINE32:
533                 DPRINTF_EDIT_MSG32("EM_GETLINE");
534                 result = (LRESULT)EDIT_EM_GetLine(wnd, es, (INT32)wParam, (LPSTR)lParam);
535                 break;
536
537         case EM_LIMITTEXT16:
538                 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
539                 /* fall through */
540         case EM_SETLIMITTEXT32:
541                 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
542                 EDIT_EM_SetLimitText(wnd, es, (INT32)wParam);
543                 break;
544
545         case EM_CANUNDO16:
546                 DPRINTF_EDIT_MSG16("EM_CANUNDO");
547                 /* fall through */
548         case EM_CANUNDO32:
549                 DPRINTF_EDIT_MSG32("EM_CANUNDO");
550                 result = (LRESULT)EDIT_EM_CanUndo(wnd, es);
551                 break;
552
553         case EM_UNDO16:
554                 DPRINTF_EDIT_MSG16("EM_UNDO");
555                 /* fall through */
556         case EM_UNDO32:
557                 /* fall through */
558         case WM_UNDO:
559                 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
560                 result = (LRESULT)EDIT_EM_Undo(wnd, es);
561                 break;
562
563         case EM_FMTLINES16:
564                 DPRINTF_EDIT_MSG16("EM_FMTLINES");
565                 /* fall through */
566         case EM_FMTLINES32:
567                 DPRINTF_EDIT_MSG32("EM_FMTLINES");
568                 result = (LRESULT)EDIT_EM_FmtLines(wnd, es, (BOOL32)wParam);
569                 break;
570
571         case EM_LINEFROMCHAR16:
572                 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
573                 /* fall through */
574         case EM_LINEFROMCHAR32:
575                 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
576                 result = (LRESULT)EDIT_EM_LineFromChar(wnd, es, (INT32)wParam);
577                 break;
578
579         /* message 0x00ca missing from specs */
580
581         case WM_USER+26:
582                 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
583                 /* fall through */
584         case 0x00ca:
585                 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
586                 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
587                 break;
588
589         case EM_SETTABSTOPS16:
590                 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
591                 result = (LRESULT)EDIT_EM_SetTabStops16(wnd, es, (INT32)wParam, (LPINT16)PTR_SEG_TO_LIN((SEGPTR)lParam));
592                 break;
593         case EM_SETTABSTOPS32:
594                 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
595                 result = (LRESULT)EDIT_EM_SetTabStops(wnd, es, (INT32)wParam, (LPINT32)lParam);
596                 break;
597
598         case EM_SETPASSWORDCHAR16:
599                 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
600                 /* fall through */
601         case EM_SETPASSWORDCHAR32:
602                 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
603                 EDIT_EM_SetPasswordChar(wnd, es, (CHAR)wParam);
604                 break;
605
606         case EM_EMPTYUNDOBUFFER16:
607                 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
608                 /* fall through */
609         case EM_EMPTYUNDOBUFFER32:
610                 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
611                 EDIT_EM_EmptyUndoBuffer(wnd, es);
612                 break;
613
614         case EM_GETFIRSTVISIBLELINE16:
615                 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
616                 result = es->y_offset;
617                 break;
618         case EM_GETFIRSTVISIBLELINE32:
619                 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
620                 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
621                 break;
622
623         case EM_SETREADONLY16:
624                 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
625                 /* fall through */
626         case EM_SETREADONLY32:
627                 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
628                 if (wParam) {
629                         wnd->dwStyle |= ES_READONLY;
630                         es->style |= ES_READONLY;
631                 } else {
632                         wnd->dwStyle &= ~ES_READONLY;
633                         es->style &= ~ES_READONLY;
634                 }
635                 return 1;
636                 break;
637
638         case EM_SETWORDBREAKPROC16:
639                 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
640                 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
641                 break;
642         case EM_SETWORDBREAKPROC32:
643                 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
644                 EDIT_EM_SetWordBreakProc(wnd, es, (EDITWORDBREAKPROC32A)lParam);
645                 break;
646
647         case EM_GETWORDBREAKPROC16:
648                 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
649                 result = (LRESULT)es->word_break_proc16;
650                 break;
651         case EM_GETWORDBREAKPROC32:
652                 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
653                 result = (LRESULT)es->word_break_proc32A;
654                 break;
655
656         case EM_GETPASSWORDCHAR16:
657                 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
658                 /* fall through */
659         case EM_GETPASSWORDCHAR32:
660                 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
661                 result = es->password_char;
662                 break;
663
664         /* The following EM_xxx are new to win95 and don't exist for 16 bit */
665
666         case EM_SETMARGINS32:
667                 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
668                 EDIT_EM_SetMargins(wnd, es, (INT32)wParam, SLOWORD(lParam), SHIWORD(lParam));
669                 break;
670
671         case EM_GETMARGINS32:
672                 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
673                 result = MAKELONG(es->left_margin, es->right_margin);
674                 break;
675
676         case EM_GETLIMITTEXT32:
677                 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
678                 result = es->buffer_limit;
679                 break;
680
681         case EM_POSFROMCHAR32:
682                 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
683                 result = EDIT_EM_PosFromChar(wnd, es, (INT32)wParam, FALSE);
684                 break;
685
686         case EM_CHARFROMPOS32:
687                 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
688                 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
689                 break;
690
691         case WM_GETDLGCODE:
692                 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
693                 result = (es->style & ES_MULTILINE) ?
694                                 DLGC_WANTALLKEYS | DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS :
695                                 DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
696                 break;
697
698         case WM_CHAR:
699                 DPRINTF_EDIT_MSG32("WM_CHAR");
700                 EDIT_WM_Char(wnd, es, (CHAR)wParam, (DWORD)lParam);
701                 break;
702
703         case WM_CLEAR:
704                 DPRINTF_EDIT_MSG32("WM_CLEAR");
705                 EDIT_WM_Clear(wnd, es);
706                 break;
707
708         case WM_COMMAND:
709                 DPRINTF_EDIT_MSG32("WM_COMMAND");
710                 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND32)lParam);
711                 break;
712
713         case WM_CONTEXTMENU:
714                 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
715                 EDIT_WM_ContextMenu(wnd, es, (HWND32)wParam, SLOWORD(lParam), SHIWORD(lParam));
716                 break;
717
718         case WM_COPY:
719                 DPRINTF_EDIT_MSG32("WM_COPY");
720                 EDIT_WM_Copy(wnd, es);
721                 break;
722
723         case WM_CREATE:
724                 DPRINTF_EDIT_MSG32("WM_CREATE");
725                 result = EDIT_WM_Create(wnd, es, (LPCREATESTRUCT32A)lParam);
726                 break;
727
728         case WM_CUT:
729                 DPRINTF_EDIT_MSG32("WM_CUT");
730                 EDIT_WM_Cut(wnd, es);
731                 break;
732
733         case WM_ENABLE:
734                 DPRINTF_EDIT_MSG32("WM_ENABLE");
735                 InvalidateRect32(hwnd, NULL, TRUE);
736                 break;
737
738         case WM_ERASEBKGND:
739                 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
740                 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC32)wParam);
741                 break;
742
743         case WM_GETFONT:
744                 DPRINTF_EDIT_MSG32("WM_GETFONT");
745                 result = (LRESULT)es->font;
746                 break;
747
748         case WM_GETTEXT:
749                 DPRINTF_EDIT_MSG32("WM_GETTEXT");
750                 result = (LRESULT)EDIT_WM_GetText(wnd, es, (INT32)wParam, (LPSTR)lParam);
751                 break;
752
753         case WM_GETTEXTLENGTH:
754                 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
755                 result = lstrlen32A(es->text);
756                 break;
757
758         case WM_HSCROLL:
759                 DPRINTF_EDIT_MSG32("WM_HSCROLL");
760                 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND32)lParam);
761                 break;
762
763         case WM_KEYDOWN:
764                 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
765                 result = EDIT_WM_KeyDown(wnd, es, (INT32)wParam, (DWORD)lParam);
766                 break;
767
768         case WM_KILLFOCUS:
769                 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
770                 result = EDIT_WM_KillFocus(wnd, es, (HWND32)wParam);
771                 break;
772
773         case WM_LBUTTONDBLCLK:
774                 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
775                 result = EDIT_WM_LButtonDblClk(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
776                 break;
777
778         case WM_LBUTTONDOWN:
779                 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
780                 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
781                 break;
782
783         case WM_LBUTTONUP:
784                 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
785                 result = EDIT_WM_LButtonUp(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
786                 break;
787
788         case WM_MOUSEACTIVATE:
789                 /*
790                  *      FIXME: maybe DefWindowProc() screws up, but it seems that
791                  *              modalless dialog boxes need this.  If we don't do this, the focus
792                  *              will _not_ be set by DefWindowProc() for edit controls in a
793                  *              modalless dialog box ???
794                  */
795                 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
796                 SetFocus32(wnd->hwndSelf);
797                 result = MA_ACTIVATE;
798                 break;
799
800         case WM_MOUSEMOVE:
801                 /*
802                  *      DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
803                  */
804                 result = EDIT_WM_MouseMove(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
805                 break;
806
807         case WM_PAINT:
808                 DPRINTF_EDIT_MSG32("WM_PAINT");
809                 EDIT_WM_Paint(wnd, es);
810                 break;
811
812         case WM_PASTE:
813                 DPRINTF_EDIT_MSG32("WM_PASTE");
814                 EDIT_WM_Paste(wnd, es);
815                 break;
816
817         case WM_SETFOCUS:
818                 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
819                 EDIT_WM_SetFocus(wnd, es, (HWND32)wParam);
820                 break;
821
822         case WM_SETFONT:
823                 DPRINTF_EDIT_MSG32("WM_SETFONT");
824                 EDIT_WM_SetFont(wnd, es, (HFONT32)wParam, LOWORD(lParam) != 0);
825                 break;
826
827         case WM_SETTEXT:
828                 DPRINTF_EDIT_MSG32("WM_SETTEXT");
829                 EDIT_WM_SetText(wnd, es, (LPCSTR)lParam);
830                 result = TRUE;
831                 break;
832
833         case WM_SIZE:
834                 DPRINTF_EDIT_MSG32("WM_SIZE");
835                 EDIT_WM_Size(wnd, es, (UINT32)wParam, LOWORD(lParam), HIWORD(lParam));
836                 break;
837
838         case WM_SYSKEYDOWN:
839                 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
840                 result = EDIT_WM_SysKeyDown(wnd, es, (INT32)wParam, (DWORD)lParam);
841                 break;
842
843         case WM_TIMER:
844                 DPRINTF_EDIT_MSG32("WM_TIMER");
845                 EDIT_WM_Timer(wnd, es, (INT32)wParam, (TIMERPROC32)lParam);
846                 break;
847
848         case WM_VSCROLL:
849                 DPRINTF_EDIT_MSG32("WM_VSCROLL");
850                 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND32)(lParam));
851                 break;
852
853         default:
854                 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
855                 break;
856         }
857         EDIT_UnlockBuffer(wnd, es, FALSE);
858         return result;
859 }
860
861
862 /*********************************************************************
863  *
864  *      EDIT_BuildLineDefs_ML
865  *
866  *      Build linked list of text lines.
867  *      Lines can end with '\0' (last line), a character (if it is wrapped),
868  *      a soft return '\r\r\n' or a hard return '\r\n'
869  *
870  */
871 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
872 {
873         HDC32 dc;
874         HFONT32 old_font = 0;
875         LPSTR start, cp;
876         INT32 fw;
877         LINEDEF *current_def;
878         LINEDEF **previous_next;
879
880         current_def = es->first_line_def;
881         do {
882                 LINEDEF *next_def = current_def->next;
883                 HeapFree(es->heap, 0, current_def);
884                 current_def = next_def;
885         } while (current_def);
886         es->line_count = 0;
887         es->text_width = 0;
888
889         dc = GetDC32(wnd->hwndSelf);
890         if (es->font)
891                 old_font = SelectObject32(dc, es->font);
892
893         fw = es->format_rect.right - es->format_rect.left;
894         start = es->text;
895         previous_next = &es->first_line_def;
896         do {
897                 current_def = HeapAlloc(es->heap, 0, sizeof(LINEDEF));
898                 current_def->next = NULL;
899                 cp = start;
900                 while (*cp) {
901                         if ((*cp == '\r') && (*(cp + 1) == '\n'))
902                                 break;
903                         cp++;
904                 }
905                 if (!(*cp)) {
906                         current_def->ending = END_0;
907                         current_def->net_length = lstrlen32A(start);
908                 } else if ((cp > start) && (*(cp - 1) == '\r')) {
909                         current_def->ending = END_SOFT;
910                         current_def->net_length = cp - start - 1;
911                 } else {
912                         current_def->ending = END_HARD;
913                         current_def->net_length = cp - start;
914                 }
915                 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc,
916                                         start, current_def->net_length,
917                                         es->tabs_count, es->tabs));
918                 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
919                 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
920                         INT32 next = 0;
921                         INT32 prev;
922                         do {
923                                 prev = next;
924                                 next = EDIT_CallWordBreakProc(wnd, es, start - es->text,
925                                                 prev + 1, current_def->net_length, WB_RIGHT);
926                                 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc,
927                                                         start, next, es->tabs_count, es->tabs));
928                         } while (current_def->width <= fw);
929                         if (!prev) {
930                                 next = 0;
931                                 do {
932                                         prev = next;
933                                         next++;
934                                         current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc,
935                                                                 start, next, es->tabs_count, es->tabs));
936                                 } while (current_def->width <= fw);
937                                 if (!prev)
938                                         prev = 1;
939                         }
940                         current_def->net_length = prev;
941                         current_def->ending = END_WRAP;
942                         current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc, start,
943                                                 current_def->net_length, es->tabs_count, es->tabs));
944                 }
945                 switch (current_def->ending) {
946                 case END_SOFT:
947                         current_def->length = current_def->net_length + 3;
948                         break;
949                 case END_HARD:
950                         current_def->length = current_def->net_length + 2;
951                         break;
952                 case END_WRAP:
953                 case END_0:
954                         current_def->length = current_def->net_length;
955                         break;
956                 }
957                 es->text_width = MAX(es->text_width, current_def->width);
958                 start += current_def->length;
959                 *previous_next = current_def;
960                 previous_next = &current_def->next;
961                 es->line_count++;
962         } while (current_def->ending != END_0);
963         if (es->font)
964                 SelectObject32(dc, old_font);
965         ReleaseDC32(wnd->hwndSelf, dc);
966 }
967
968
969 /*********************************************************************
970  *
971  *      EDIT_CallWordBreakProc
972  *
973  *      Call appropriate WordBreakProc (internal or external).
974  *
975  *      Note: The "start" argument should always be an index refering
976  *              to es->text.  The actual wordbreak proc might be
977  *              16 bit, so we can't always pass any 32 bit LPSTR.
978  *              Hence we assume that es->text is the buffer that holds
979  *              the string under examination (we can decide this for ourselves).
980  *
981  */
982 static INT32 EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT32 start, INT32 index, INT32 count, INT32 action)
983 {
984         if (es->word_break_proc16) {
985                 HLOCAL16 hloc16 = EDIT_EM_GetHandle16(wnd, es);
986                 SEGPTR segptr = LocalLock16(hloc16);
987                 INT32 ret = (INT32)Callbacks->CallWordBreakProc(es->word_break_proc16,
988                                                 segptr + start, index, count, action);
989                 LocalUnlock16(hloc16);
990                 return ret;
991         }
992         else if (es->word_break_proc32A)
993         {
994             TRACE(relay, "(wordbrk=%p,str='%s',idx=%d,cnt=%d,act=%d)\n",
995                            es->word_break_proc32A, es->text + start, index,
996                            count, action );
997             return (INT32)es->word_break_proc32A( es->text + start, index,
998                                                   count, action );
999         }
1000         else
1001             return EDIT_WordBreakProc(es->text + start, index, count, action);
1002 }
1003
1004
1005 /*********************************************************************
1006  *
1007  *      EDIT_CharFromPos
1008  *
1009  *      Beware: This is not the function called on EM_CHARFROMPOS
1010  *              The position _can_ be outside the formatting / client
1011  *              rectangle
1012  *              The return value is only the character index
1013  *
1014  */
1015 static INT32 EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y, LPBOOL32 after_wrap)
1016 {
1017         INT32 index;
1018         HDC32 dc;
1019         HFONT32 old_font = 0;
1020
1021         if (es->style & ES_MULTILINE) {
1022                 INT32 line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1023                 INT32 line_index = 0;
1024                 LINEDEF *line_def = es->first_line_def;
1025                 INT32 low, high;
1026                 while ((line > 0) && line_def->next) {
1027                         line_index += line_def->length;
1028                         line_def = line_def->next;
1029                         line--;
1030                 }
1031                 x += es->x_offset - es->format_rect.left;
1032                 if (x >= line_def->width) {
1033                         if (after_wrap)
1034                                 *after_wrap = (line_def->ending == END_WRAP);
1035                         return line_index + line_def->net_length;
1036                 }
1037                 if (x <= 0) {
1038                         if (after_wrap)
1039                                 *after_wrap = FALSE;
1040                         return line_index;
1041                 }
1042                 dc = GetDC32(wnd->hwndSelf);
1043                 if (es->font)
1044                         old_font = SelectObject32(dc, es->font);
1045                     low = line_index + 1;
1046                     high = line_index + line_def->net_length + 1;
1047                     while (low < high - 1)
1048                     {
1049                         INT32 mid = (low + high) / 2;
1050                         if (LOWORD(GetTabbedTextExtent32A(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1051                         else low = mid;
1052                     }
1053                     index = low;
1054
1055                 if (after_wrap)
1056                         *after_wrap = ((index == line_index + line_def->net_length) &&
1057                                                         (line_def->ending == END_WRAP));
1058         } else {
1059                 LPSTR text;
1060                 SIZE32 size;
1061                 if (after_wrap)
1062                         *after_wrap = FALSE;
1063                 x -= es->format_rect.left;
1064                 if (!x)
1065                         return es->x_offset;
1066                 text = EDIT_GetPasswordPointer_SL(wnd, es);
1067                 dc = GetDC32(wnd->hwndSelf);
1068                 if (es->font)
1069                         old_font = SelectObject32(dc, es->font);
1070                 if (x < 0)
1071                 {
1072                     INT32 low = 0;
1073                     INT32 high = es->x_offset;
1074                     while (low < high - 1)
1075                     {
1076                         INT32 mid = (low + high) / 2;
1077                         GetTextExtentPoint32A( dc, text + mid,
1078                                                es->x_offset - mid, &size );
1079                         if (size.cx > -x) low = mid;
1080                         else high = mid;
1081                     }
1082                     index = low;
1083                 }
1084                 else
1085                 {
1086                     INT32 low = es->x_offset;
1087                     INT32 high = lstrlen32A(es->text) + 1;
1088                     while (low < high - 1)
1089                     {
1090                         INT32 mid = (low + high) / 2;
1091                         GetTextExtentPoint32A( dc, text + es->x_offset,
1092                                                mid - es->x_offset, &size );
1093                         if (size.cx > x) high = mid;
1094                         else low = mid;
1095                     }
1096                     index = low;
1097                 }
1098                 if (es->style & ES_PASSWORD)
1099                         HeapFree(es->heap, 0 ,text);
1100         }
1101         if (es->font)
1102                 SelectObject32(dc, old_font);
1103         ReleaseDC32(wnd->hwndSelf, dc);
1104         return index;
1105 }
1106
1107
1108 /*********************************************************************
1109  *
1110  *      EDIT_ConfinePoint
1111  *
1112  *      adjusts the point to be within the formatting rectangle
1113  *      (so CharFromPos returns the nearest _visible_ character)
1114  *
1115  */
1116 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT32 x, LPINT32 y)
1117 {
1118         *x = MIN(MAX(*x, es->format_rect.left), es->format_rect.right - 1);
1119         *y = MIN(MAX(*y, es->format_rect.top), es->format_rect.bottom - 1);
1120 }
1121
1122
1123 /*********************************************************************
1124  *
1125  *      EDIT_GetLineRect
1126  *
1127  *      Calculates the bounding rectangle for a line from a starting
1128  *      column to an ending column.
1129  *
1130  */
1131 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT32 line, INT32 scol, INT32 ecol, LPRECT32 rc)
1132 {
1133         INT32 line_index =  EDIT_EM_LineIndex(wnd, es, line);
1134
1135         if (es->style & ES_MULTILINE)
1136                 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1137         else
1138                 rc->top = es->format_rect.top;
1139         rc->bottom = rc->top + es->line_height;
1140         rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1141         rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1142 }
1143
1144
1145 /*********************************************************************
1146  *
1147  *      EDIT_GetPasswordPointer_SL
1148  *
1149  *      note: caller should free the (optionally) allocated buffer
1150  *
1151  */
1152 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es)
1153 {
1154         if (es->style & ES_PASSWORD) {
1155                 INT32 len = lstrlen32A(es->text);
1156                 LPSTR text = HeapAlloc(es->heap, 0, len + 1);
1157                 RtlFillMemory(text, len, es->password_char);
1158                 text[len] = '\0';
1159                 return text;
1160         } else
1161                 return es->text;
1162 }
1163
1164
1165 /*********************************************************************
1166  *
1167  *      EDIT_LockBuffer
1168  *
1169  *      This acts as a LOCAL_Lock(), but it locks only once.  This way
1170  *      you can call it whenever you like, without unlocking.
1171  *
1172  */
1173 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1174 {
1175         if (!es) {
1176                 ERR(edit, "no EDITSTATE ... please report\n");
1177                 return;
1178         }
1179         if (!(es->style & ES_MULTILINE))
1180                 return;
1181         if (!es->text) {
1182                 if (es->hloc32)
1183                         es->text = LocalLock32(es->hloc32);
1184                 else if (es->hloc16)
1185                         es->text = LOCAL_Lock(wnd->hInstance, es->hloc16);
1186                 else {
1187                         ERR(edit, "no buffer ... please report\n");
1188                         return;
1189                 }
1190         }
1191         es->lock_count++;
1192 }
1193
1194
1195 /*********************************************************************
1196  *
1197  *      EDIT_SL_InvalidateText
1198  *
1199  *      Called from EDIT_InvalidateText().
1200  *      Does the job for single-line controls only.
1201  *
1202  */
1203 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end)
1204 {
1205         RECT32 line_rect;
1206         RECT32 rc;
1207
1208         EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1209         if (IntersectRect32(&rc, &line_rect, &es->format_rect))
1210                 InvalidateRect32(wnd->hwndSelf, &rc, FALSE);
1211 }
1212
1213
1214 /*********************************************************************
1215  *
1216  *      EDIT_ML_InvalidateText
1217  *
1218  *      Called from EDIT_InvalidateText().
1219  *      Does the job for multi-line controls only.
1220  *
1221  */
1222 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end)
1223 {
1224         INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1225         INT32 sl = EDIT_EM_LineFromChar(wnd, es, start);
1226         INT32 el = EDIT_EM_LineFromChar(wnd, es, end);
1227         INT32 sc;
1228         INT32 ec;
1229         RECT32 rc1;
1230         RECT32 rcWnd;
1231         RECT32 rcLine;
1232         RECT32 rcUpdate;
1233         INT32 l;
1234
1235         if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1236                 return;
1237
1238         sc = start - EDIT_EM_LineIndex(wnd, es, sl);
1239         ec = end - EDIT_EM_LineIndex(wnd, es, el);
1240         if (sl < es->y_offset) {
1241                 sl = es->y_offset;
1242                 sc = 0;
1243         }
1244         if (el > es->y_offset + vlc) {
1245                 el = es->y_offset + vlc;
1246                 ec = EDIT_EM_LineLength(wnd, es, EDIT_EM_LineIndex(wnd, es, el));
1247         }
1248         GetClientRect32(wnd->hwndSelf, &rc1);
1249         IntersectRect32(&rcWnd, &rc1, &es->format_rect);
1250         if (sl == el) {
1251                 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1252                 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1253                         InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1254         } else {
1255                 EDIT_GetLineRect(wnd, es, sl, sc,
1256                                 EDIT_EM_LineLength(wnd, es,
1257                                         EDIT_EM_LineIndex(wnd, es, sl)),
1258                                 &rcLine);
1259                 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1260                         InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1261                 for (l = sl + 1 ; l < el ; l++) {
1262                         EDIT_GetLineRect(wnd, es, l, 0,
1263                                 EDIT_EM_LineLength(wnd, es,
1264                                         EDIT_EM_LineIndex(wnd, es, l)),
1265                                 &rcLine);
1266                         if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1267                                 InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1268                 }
1269                 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1270                 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1271                         InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1272         }
1273 }
1274
1275
1276 /*********************************************************************
1277  *
1278  *      EDIT_InvalidateText
1279  *
1280  *      Invalidate the text from offset start upto, but not including,
1281  *      offset end.  Useful for (re)painting the selection.
1282  *      Regions outside the linewidth are not invalidated.
1283  *      end == -1 means end == TextLength.
1284  *      start and end need not be ordered.
1285  *
1286  */
1287 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end)
1288 {
1289         if (end == start)
1290                 return;
1291
1292         if (end == -1)
1293                 end = lstrlen32A(es->text);
1294
1295         ORDER_INT32(start, end);
1296
1297         if (es->style & ES_MULTILINE)
1298                 EDIT_ML_InvalidateText(wnd, es, start, end);
1299         else
1300                 EDIT_SL_InvalidateText(wnd, es, start, end);
1301 }
1302
1303
1304 /*********************************************************************
1305  *
1306  *      EDIT_MakeFit
1307  *
1308  *      Try to fit size + 1 bytes in the buffer.  Constrain to limits.
1309  *
1310  */
1311 static BOOL32 EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT32 size)
1312 {
1313         HLOCAL32 hNew32;
1314         HLOCAL16 hNew16;
1315
1316         if (size <= es->buffer_size)
1317                 return TRUE;
1318         if (size > es->buffer_limit) {
1319                 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1320                 return FALSE;
1321         }
1322         size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1323         if (size > es->buffer_limit)
1324                 size = es->buffer_limit;
1325
1326         TRACE(edit, "trying to ReAlloc to %d+1\n", size);
1327
1328         EDIT_UnlockBuffer(wnd, es, TRUE);
1329         if (es->text) {
1330                 if ((es->text = HeapReAlloc(es->heap, 0, es->text, size + 1)))
1331                         es->buffer_size = MIN(HeapSize(es->heap, 0, es->text) - 1, es->buffer_limit);
1332                 else
1333                         es->buffer_size = 0;
1334         } else if (es->hloc32) {
1335                 if ((hNew32 = LocalReAlloc32(es->hloc32, size + 1, 0))) {
1336                         TRACE(edit, "Old 32 bit handle %08x, new handle %08x\n", es->hloc32, hNew32);
1337                         es->hloc32 = hNew32;
1338                         es->buffer_size = MIN(LocalSize32(es->hloc32) - 1, es->buffer_limit);
1339                 }
1340         } else if (es->hloc16) {
1341                 if ((hNew16 = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, size + 1, LMEM_MOVEABLE))) {
1342                         TRACE(edit, "Old 16 bit handle %08x, new handle %08x\n", es->hloc16, hNew16);
1343                         es->hloc16 = hNew16;
1344                         es->buffer_size = MIN(LOCAL_Size(wnd->hInstance, es->hloc16) - 1, es->buffer_limit);
1345                 }
1346         }
1347         if (es->buffer_size < size) {
1348                 EDIT_LockBuffer(wnd, es);
1349                 WARN(edit, "FAILED !  We now have %d+1\n", es->buffer_size);
1350                 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1351                 return FALSE;
1352         } else {
1353                 EDIT_LockBuffer(wnd, es);
1354                 TRACE(edit, "We now have %d+1\n", es->buffer_size);
1355                 return TRUE;
1356         }
1357 }
1358
1359
1360 /*********************************************************************
1361  *
1362  *      EDIT_MakeUndoFit
1363  *
1364  *      Try to fit size + 1 bytes in the undo buffer.
1365  *
1366  */
1367 static BOOL32 EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT32 size)
1368 {
1369         if (size <= es->undo_buffer_size)
1370                 return TRUE;
1371         size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1372
1373         TRACE(edit, "trying to ReAlloc to %d+1\n", size);
1374
1375         if ((es->undo_text = HeapReAlloc(es->heap, 0, es->undo_text, size + 1))) {
1376                 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
1377                 if (es->undo_buffer_size < size) {
1378                         WARN(edit, "FAILED !  We now have %d+1\n", es->undo_buffer_size);
1379                         return FALSE;
1380                 }
1381                 return TRUE;
1382         }
1383         return FALSE;
1384 }
1385
1386
1387 /*********************************************************************
1388  *
1389  *      EDIT_MoveBackward
1390  *
1391  */
1392 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1393 {
1394         INT32 e = es->selection_end;
1395
1396         if (e) {
1397                 e--;
1398                 if ((es->style & ES_MULTILINE) && e &&
1399                                 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1400                         e--;
1401                         if (e && (es->text[e - 1] == '\r'))
1402                                 e--;
1403                 }
1404         }
1405         EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1406         EDIT_EM_ScrollCaret(wnd, es);
1407 }
1408
1409
1410 /*********************************************************************
1411  *
1412  *      EDIT_MoveDown_ML
1413  *
1414  *      Only for multi line controls
1415  *      Move the caret one line down, on a column with the nearest
1416  *      x coordinate on the screen (might be a different column).
1417  *
1418  */
1419 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1420 {
1421         INT32 s = es->selection_start;
1422         INT32 e = es->selection_end;
1423         BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1424         LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1425         INT32 x = SLOWORD(pos);
1426         INT32 y = SHIWORD(pos);
1427
1428         e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1429         if (!extend)
1430                 s = e;
1431         EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1432         EDIT_EM_ScrollCaret(wnd, es);
1433 }
1434
1435
1436 /*********************************************************************
1437  *
1438  *      EDIT_MoveEnd
1439  *
1440  */
1441 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL32 extend)
1442 {
1443         BOOL32 after_wrap = FALSE;
1444         INT32 e;
1445
1446         if (es->style & ES_MULTILINE)
1447                 e = EDIT_CharFromPos(wnd, es, 0x7fffffff,
1448                         HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1449         else
1450                 e = lstrlen32A(es->text);
1451         EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1452         EDIT_EM_ScrollCaret(wnd, es);
1453 }
1454
1455
1456 /*********************************************************************
1457  *
1458  *      EDIT_MoveForward
1459  *
1460  */
1461 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1462 {
1463         INT32 e = es->selection_end;
1464
1465         if (es->text[e]) {
1466                 e++;
1467                 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1468                         if (es->text[e] == '\n')
1469                                 e++;
1470                         else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1471                                 e += 2;
1472                 }
1473         }
1474         EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1475         EDIT_EM_ScrollCaret(wnd, es);
1476 }
1477
1478
1479 /*********************************************************************
1480  *
1481  *      EDIT_MoveHome
1482  *
1483  *      Home key: move to beginning of line.
1484  *
1485  */
1486 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL32 extend)
1487 {
1488         INT32 e;
1489
1490         if (es->style & ES_MULTILINE)
1491                 e = EDIT_CharFromPos(wnd, es, 0x80000000,
1492                         HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1493         else
1494                 e = 0;
1495         EDIT_EM_SetSel(wnd, es, e, extend ? es->selection_start : e, FALSE);
1496         EDIT_EM_ScrollCaret(wnd, es);
1497 }
1498
1499
1500 /*********************************************************************
1501  *
1502  *      EDIT_MovePageDown_ML
1503  *
1504  *      Only for multi line controls
1505  *      Move the caret one page down, on a column with the nearest
1506  *      x coordinate on the screen (might be a different column).
1507  *
1508  */
1509 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1510 {
1511         INT32 s = es->selection_start;
1512         INT32 e = es->selection_end;
1513         BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1514         LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1515         INT32 x = SLOWORD(pos);
1516         INT32 y = SHIWORD(pos);
1517
1518         e = EDIT_CharFromPos(wnd, es, x,
1519                 y + (es->format_rect.bottom - es->format_rect.top),
1520                 &after_wrap);
1521         if (!extend)
1522                 s = e;
1523         EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1524         EDIT_EM_ScrollCaret(wnd, es);
1525 }
1526
1527
1528 /*********************************************************************
1529  *
1530  *      EDIT_MovePageUp_ML
1531  *
1532  *      Only for multi line controls
1533  *      Move the caret one page up, on a column with the nearest
1534  *      x coordinate on the screen (might be a different column).
1535  *
1536  */
1537 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1538 {
1539         INT32 s = es->selection_start;
1540         INT32 e = es->selection_end;
1541         BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1542         LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1543         INT32 x = SLOWORD(pos);
1544         INT32 y = SHIWORD(pos);
1545
1546         e = EDIT_CharFromPos(wnd, es, x,
1547                 y - (es->format_rect.bottom - es->format_rect.top),
1548                 &after_wrap);
1549         if (!extend)
1550                 s = e;
1551         EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1552         EDIT_EM_ScrollCaret(wnd, es);
1553 }
1554
1555
1556 /*********************************************************************
1557  *
1558  *      EDIT_MoveUp_ML
1559  *
1560  *      Only for multi line controls
1561  *      Move the caret one line up, on a column with the nearest
1562  *      x coordinate on the screen (might be a different column).
1563  *
1564  */ 
1565 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1566 {
1567         INT32 s = es->selection_start;
1568         INT32 e = es->selection_end;
1569         BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1570         LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1571         INT32 x = SLOWORD(pos);
1572         INT32 y = SHIWORD(pos);
1573
1574         e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1575         if (!extend)
1576                 s = e;
1577         EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1578         EDIT_EM_ScrollCaret(wnd, es);
1579 }
1580
1581
1582 /*********************************************************************
1583  *
1584  *      EDIT_MoveWordBackward
1585  *
1586  */
1587 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1588 {
1589         INT32 s = es->selection_start;
1590         INT32 e = es->selection_end;
1591         INT32 l;
1592         INT32 ll;
1593         INT32 li;
1594
1595         l = EDIT_EM_LineFromChar(wnd, es, e);
1596         ll = EDIT_EM_LineLength(wnd, es, e);
1597         li = EDIT_EM_LineIndex(wnd, es, l);
1598         if (e - li == 0) {
1599                 if (l) {
1600                         li = EDIT_EM_LineIndex(wnd, es, l - 1);
1601                         e = li + EDIT_EM_LineLength(wnd, es, li);
1602                 }
1603         } else {
1604                 e = li + (INT32)EDIT_CallWordBreakProc(wnd, es,
1605                                 li, e - li, ll, WB_LEFT);
1606         }
1607         if (!extend)
1608                 s = e;
1609         EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1610         EDIT_EM_ScrollCaret(wnd, es);
1611 }
1612
1613
1614 /*********************************************************************
1615  *
1616  *      EDIT_MoveWordForward
1617  *
1618  */
1619 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1620 {
1621         INT32 s = es->selection_start;
1622         INT32 e = es->selection_end;
1623         INT32 l;
1624         INT32 ll;
1625         INT32 li;
1626
1627         l = EDIT_EM_LineFromChar(wnd, es, e);
1628         ll = EDIT_EM_LineLength(wnd, es, e);
1629         li = EDIT_EM_LineIndex(wnd, es, l);
1630         if (e - li == ll) {
1631                 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1632                         e = EDIT_EM_LineIndex(wnd, es, l + 1);
1633         } else {
1634                 e = li + EDIT_CallWordBreakProc(wnd, es,
1635                                 li, e - li + 1, ll, WB_RIGHT);
1636         }
1637         if (!extend)
1638                 s = e;
1639         EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1640         EDIT_EM_ScrollCaret(wnd, es);
1641 }
1642
1643
1644 /*********************************************************************
1645  *
1646  *      EDIT_PaintLine
1647  *
1648  */
1649 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC32 dc, INT32 line, BOOL32 rev)
1650 {
1651         INT32 s = es->selection_start;
1652         INT32 e = es->selection_end;
1653         INT32 li;
1654         INT32 ll;
1655         INT32 x;
1656         INT32 y;
1657         LRESULT pos;
1658
1659         if (es->style & ES_MULTILINE) {
1660                 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1661                 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1662                         return;
1663         } else if (line)
1664                 return;
1665
1666         TRACE(edit, "line=%d\n", line);
1667
1668         pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(wnd, es, line), FALSE);
1669         x = SLOWORD(pos);
1670         y = SHIWORD(pos);
1671         li = EDIT_EM_LineIndex(wnd, es, line);
1672         ll = EDIT_EM_LineLength(wnd, es, li);
1673         s = es->selection_start;
1674         e = es->selection_end;
1675         ORDER_INT32(s, e);
1676         s = MIN(li + ll, MAX(li, s));
1677         e = MIN(li + ll, MAX(li, e));
1678         if (rev && (s != e) &&
1679                         ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
1680                 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, s - li, FALSE);
1681                 x += EDIT_PaintText(wnd, es, dc, x, y, line, s - li, e - s, TRUE);
1682                 x += EDIT_PaintText(wnd, es, dc, x, y, line, e - li, li + ll - e, FALSE);
1683         } else
1684                 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, ll, FALSE);
1685 }
1686
1687
1688 /*********************************************************************
1689  *
1690  *      EDIT_PaintText
1691  *
1692  */
1693 static INT32 EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC32 dc, INT32 x, INT32 y, INT32 line, INT32 col, INT32 count, BOOL32 rev)
1694 {
1695         COLORREF BkColor;
1696         COLORREF TextColor;
1697         INT32 ret;
1698         INT32 li;
1699         SIZE32 size;
1700
1701         if (!count)
1702                 return 0;
1703         BkColor = GetBkColor32(dc);
1704         TextColor = GetTextColor32(dc);
1705         if (rev) {
1706                 SetBkColor32(dc, GetSysColor32(COLOR_HIGHLIGHT));
1707                 SetTextColor32(dc, GetSysColor32(COLOR_HIGHLIGHTTEXT));
1708         }
1709         li = EDIT_EM_LineIndex(wnd, es, line);
1710         if (es->style & ES_MULTILINE) {
1711                 ret = (INT32)LOWORD(TabbedTextOut32A(dc, x, y, es->text + li + col, count,
1712                                         es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
1713         } else {
1714                 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
1715                 TextOut32A(dc, x, y, text + li + col, count);
1716                 GetTextExtentPoint32A(dc, text + li + col, count, &size);
1717                 ret = size.cx;
1718                 if (es->style & ES_PASSWORD)
1719                         HeapFree(es->heap, 0, text);
1720         }
1721         if (rev) {
1722                 SetBkColor32(dc, BkColor);
1723                 SetTextColor32(dc, TextColor);
1724         }
1725         return ret;
1726 }
1727
1728
1729 /*********************************************************************
1730  *
1731  *      EDIT_SetCaretPos
1732  *
1733  */
1734 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT32 pos,
1735                              BOOL32 after_wrap)
1736 {
1737         LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
1738         INT32 x = SLOWORD(res);
1739         INT32 y = SHIWORD(res);
1740
1741         if(x < es->format_rect.left)
1742                 x = es->format_rect.left;
1743         if(x > es->format_rect.right - 2)
1744                 x = es->format_rect.right - 2;
1745         if(y > es->format_rect.bottom)
1746                 y = es->format_rect.bottom;
1747         if(y < es->format_rect.top)
1748                 y = es->format_rect.top;
1749         SetCaretPos32(x, y);
1750         return;
1751 }
1752
1753
1754 /*********************************************************************
1755  *
1756  *      EDIT_SetRectNP
1757  *
1758  *      note:   this is not (exactly) the handler called on EM_SETRECTNP
1759  *              it is also used to set the rect of a single line control
1760  *
1761  */
1762 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT32 rc)
1763 {
1764         CopyRect32(&es->format_rect, rc);
1765         if (es->style & WS_BORDER) {
1766                 INT32 bw = GetSystemMetrics32(SM_CXBORDER) + 1;
1767                 if(TWEAK_WineLook == WIN31_LOOK)
1768                         bw += 2;
1769                 es->format_rect.left += bw;
1770                 es->format_rect.top += bw;
1771                 es->format_rect.right -= bw;
1772                 es->format_rect.bottom -= bw;
1773         }
1774         es->format_rect.left += es->left_margin;
1775         es->format_rect.right -= es->right_margin;
1776         es->format_rect.right = MAX(es->format_rect.right, es->format_rect.left + es->char_width);
1777         if (es->style & ES_MULTILINE)
1778                 es->format_rect.bottom = es->format_rect.top +
1779                         MAX(1, (es->format_rect.bottom - es->format_rect.top) / es->line_height) * es->line_height;
1780         else
1781                 es->format_rect.bottom = es->format_rect.top + es->line_height;
1782         if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
1783                 EDIT_BuildLineDefs_ML(wnd, es);
1784 }
1785
1786
1787 /*********************************************************************
1788  *
1789  *      EDIT_UnlockBuffer
1790  *
1791  */
1792 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL32 force)
1793 {
1794         if (!es) {
1795                 ERR(edit, "no EDITSTATE ... please report\n");
1796                 return;
1797         }
1798         if (!(es->style & ES_MULTILINE))
1799                 return;
1800         if (!es->lock_count) {
1801                 ERR(edit, "lock_count == 0 ... please report\n");
1802                 return;
1803         }
1804         if (!es->text) {
1805                 ERR(edit, "es->text == 0 ... please report\n");
1806                 return;
1807         }
1808         if (force || (es->lock_count == 1)) {
1809                 if (es->hloc32) {
1810                         LocalUnlock32(es->hloc32);
1811                         es->text = NULL;
1812                 } else if (es->hloc16) {
1813                         LOCAL_Unlock(wnd->hInstance, es->hloc16);
1814                         es->text = NULL;
1815                 }
1816         }
1817         es->lock_count--;
1818 }
1819
1820
1821 /*********************************************************************
1822  *
1823  *      EDIT_WordBreakProc
1824  *
1825  *      Find the beginning of words.
1826  *      Note:   unlike the specs for a WordBreakProc, this function only
1827  *              allows to be called without linebreaks between s[0] upto
1828  *              s[count - 1].  Remember it is only called
1829  *              internally, so we can decide this for ourselves.
1830  *
1831  */
1832 static INT32 EDIT_WordBreakProc(LPSTR s, INT32 index, INT32 count, INT32 action)
1833 {
1834         INT32 ret = 0;
1835
1836         TRACE(edit, "s=%p, index=%u, count=%u, action=%d\n", 
1837                      s, index, count, action);
1838
1839         switch (action) {
1840         case WB_LEFT:
1841                 if (!count)
1842                         break;
1843                 if (index)
1844                         index--;
1845                 if (s[index] == ' ') {
1846                         while (index && (s[index] == ' '))
1847                                 index--;
1848                         if (index) {
1849                                 while (index && (s[index] != ' '))
1850                                         index--;
1851                                 if (s[index] == ' ')
1852                                         index++;
1853                         }
1854                 } else {
1855                         while (index && (s[index] != ' '))
1856                                 index--;
1857                         if (s[index] == ' ')
1858                                 index++;
1859                 }
1860                 ret = index;
1861                 break;
1862         case WB_RIGHT:
1863                 if (!count)
1864                         break;
1865                 if (index)
1866                         index--;
1867                 if (s[index] == ' ')
1868                         while ((index < count) && (s[index] == ' ')) index++;
1869                 else {
1870                         while (s[index] && (s[index] != ' ') && (index < count))
1871                                 index++;
1872                         while ((s[index] == ' ') && (index < count)) index++;
1873                 }
1874                 ret = index;
1875                 break;
1876         case WB_ISDELIMITER:
1877                 ret = (s[index] == ' ');
1878                 break;
1879         default:
1880                 ERR(edit, "unknown action code, please report !\n");
1881                 break;
1882         }
1883         return ret;
1884 }
1885
1886
1887 /*********************************************************************
1888  *
1889  *      EM_CHARFROMPOS
1890  *
1891  *      returns line number (not index) in high-order word of result.
1892  *      NB : Q137805 is unclear about this. POINT * pointer in lParam apply 
1893  *      to Richedit, not to the edit control. Original documentation is valid.
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_LineFromChar(wnd, es, index));
1912 }
1913
1914
1915 /*********************************************************************
1916  *
1917  *      EM_FMTLINES
1918  *
1919  * Enable or disable soft breaks.
1920  */
1921 static BOOL32 EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL32 add_eol)
1922 {
1923         es->flags &= ~EF_USE_SOFTBRK;
1924         if (add_eol) {
1925                 es->flags |= EF_USE_SOFTBRK;
1926                 FIXME(edit, "soft break enabled, not implemented\n");
1927         }
1928         return add_eol;
1929 }
1930
1931
1932 /*********************************************************************
1933  *
1934  *      EM_GETHANDLE
1935  *
1936  *      Hopefully this won't fire back at us.
1937  *      We always start with a fixed buffer in our own heap.
1938  *      However, with this message a 32 bit application requests
1939  *      a handle to 32 bit moveable local heap memory, where it expects
1940  *      to find the text.
1941  *      It's a pity that from this moment on we have to use this
1942  *      local heap, because applications may rely on the handle
1943  *      in the future.
1944  *
1945  *      In this function we'll try to switch to local heap.
1946  *
1947  */
1948 static HLOCAL32 EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es)
1949 {
1950         HLOCAL32 newBuf;
1951         LPSTR newText;
1952         INT32 newSize;
1953
1954         if (!(es->style & ES_MULTILINE))
1955                 return 0;
1956
1957         if (es->hloc32)
1958                 return es->hloc32;
1959         else if (es->hloc16)
1960                 return (HLOCAL32)es->hloc16;
1961
1962         if (!(newBuf = LocalAlloc32(LMEM_MOVEABLE, lstrlen32A(es->text) + 1))) {
1963                 ERR(edit, "could not allocate new 32 bit buffer\n");
1964                 return 0;
1965         }
1966         newSize = MIN(LocalSize32(newBuf) - 1, es->buffer_limit);
1967         if (!(newText = LocalLock32(newBuf))) {
1968                 ERR(edit, "could not lock new 32 bit buffer\n");
1969                 LocalFree32(newBuf);
1970                 return 0;
1971         }
1972         lstrcpy32A(newText, es->text);
1973         EDIT_UnlockBuffer(wnd, es, TRUE);
1974         if (es->text)
1975                 HeapFree(es->heap, 0, es->text);
1976         es->hloc32 = newBuf;
1977         es->hloc16 = (HLOCAL16)NULL;
1978         es->buffer_size = newSize;
1979         es->text = newText;
1980         EDIT_LockBuffer(wnd, es);
1981         TRACE(edit, "switched to 32 bit local heap\n");
1982
1983         return es->hloc32;
1984 }
1985
1986
1987 /*********************************************************************
1988  *
1989  *      EM_GETHANDLE16
1990  *
1991  *      Hopefully this won't fire back at us.
1992  *      We always start with a buffer in 32 bit linear memory.
1993  *      However, with this message a 16 bit application requests
1994  *      a handle of 16 bit local heap memory, where it expects to find
1995  *      the text.
1996  *      It's a pitty that from this moment on we have to use this
1997  *      local heap, because applications may rely on the handle
1998  *      in the future.
1999  *
2000  *      In this function we'll try to switch to local heap.
2001  */
2002 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2003 {
2004         HLOCAL16 newBuf;
2005         LPSTR newText;
2006         INT32 newSize;
2007
2008         if (!(es->style & ES_MULTILINE))
2009                 return 0;
2010
2011         if (es->hloc16)
2012                 return es->hloc16;
2013
2014         if (!LOCAL_HeapSize(wnd->hInstance)) {
2015                 if (!LocalInit(wnd->hInstance, 0,
2016                                 GlobalSize16(wnd->hInstance))) {
2017                         ERR(edit, "could not initialize local heap\n");
2018                         return 0;
2019                 }
2020                 TRACE(edit, "local heap initialized\n");
2021         }
2022         if (!(newBuf = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE, lstrlen32A(es->text) + 1))) {
2023                 ERR(edit, "could not allocate new 16 bit buffer\n");
2024                 return 0;
2025         }
2026         newSize = MIN(LOCAL_Size(wnd->hInstance, newBuf) - 1, es->buffer_limit);
2027         if (!(newText = LOCAL_Lock(wnd->hInstance, newBuf))) {
2028                 ERR(edit, "could not lock new 16 bit buffer\n");
2029                 LOCAL_Free(wnd->hInstance, newBuf);
2030                 return 0;
2031         }
2032         lstrcpy32A(newText, es->text);
2033         EDIT_UnlockBuffer(wnd, es, TRUE);
2034         if (es->text)
2035                 HeapFree(es->heap, 0, es->text);
2036         else if (es->hloc32) {
2037                 while (LocalFree32(es->hloc32)) ;
2038                 LocalFree32(es->hloc32);
2039         }
2040         es->hloc32 = (HLOCAL32)NULL;
2041         es->hloc16 = newBuf;
2042         es->buffer_size = newSize;
2043         es->text = newText;
2044         EDIT_LockBuffer(wnd, es);
2045         TRACE(edit, "switched to 16 bit buffer\n");
2046
2047         return es->hloc16;
2048 }
2049
2050
2051 /*********************************************************************
2052  *
2053  *      EM_GETLINE
2054  *
2055  */
2056 static INT32 EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT32 line, LPSTR lpch)
2057 {
2058         LPSTR src;
2059         INT32 len;
2060         INT32 i;
2061
2062         if (es->style & ES_MULTILINE) {
2063                 if (line >= es->line_count)
2064                         return 0;
2065         } else
2066                 line = 0;
2067         i = EDIT_EM_LineIndex(wnd, es, line);
2068         src = es->text + i;
2069         len = MIN(*(WORD *)lpch, EDIT_EM_LineLength(wnd, es, i));
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  */
2992 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
2993 {
2994         INT32 s = es->selection_start;
2995         INT32 e = es->selection_end;
2996         HGLOBAL32 hdst;
2997         LPSTR dst;
2998
2999         if (e == s)
3000                 return;
3001         ORDER_INT32(s, e);
3002         hdst = GlobalAlloc32(GMEM_MOVEABLE, (DWORD)(e - s + 1));
3003         dst = GlobalLock32(hdst);
3004         lstrcpyn32A(dst, es->text + s, e - s + 1);
3005         GlobalUnlock32(hdst);
3006         OpenClipboard32(wnd->hwndSelf);
3007         EmptyClipboard32();
3008         SetClipboardData32(CF_TEXT, hdst);
3009         CloseClipboard32();
3010 }
3011
3012
3013 /*********************************************************************
3014  *
3015  *      WM_CREATE
3016  *
3017  */
3018 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCT32A cs)
3019 {
3020         /*
3021          *      To initialize some final structure members, we call some helper
3022          *      functions.  However, since the EDITSTATE is not consistent (i.e.
3023          *      not fully initialized), we should be very careful which
3024          *      functions can be called, and in what order.
3025          */
3026         EDIT_WM_SetFont(wnd, es, 0, FALSE);
3027     EDIT_EM_EmptyUndoBuffer(wnd, es);
3028
3029         if (cs->lpszName && *(cs->lpszName) != '\0') {
3030                 EDIT_EM_ReplaceSel(wnd, es, FALSE, cs->lpszName);
3031                 /* 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 */
3032                 es->selection_start = es->selection_end = 0;
3033                 EDIT_EM_ScrollCaret(wnd, es);
3034         }
3035         return 0;
3036 }
3037
3038
3039 /*********************************************************************
3040  *
3041  *      WM_DESTROY
3042  *
3043  */
3044 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3045 {
3046         if (es->hloc32) {
3047                 while (LocalUnlock32(es->hloc32)) ;
3048                 LocalFree32(es->hloc32);
3049         }
3050         if (es->hloc16) {
3051                 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3052                 LOCAL_Free(wnd->hInstance, es->hloc16);
3053         }
3054         HeapDestroy(es->heap);
3055         HeapFree(GetProcessHeap(), 0, es);
3056         *(EDITSTATE **)wnd->wExtra = NULL;
3057 }
3058
3059
3060 /*********************************************************************
3061  *
3062  *      WM_ERASEBKGND
3063  *
3064  */
3065 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC32 dc)
3066 {
3067         HBRUSH32 brush;
3068         RECT32 rc;
3069
3070         if (!(brush = (HBRUSH32)EDIT_SEND_CTLCOLOR(wnd, dc)))
3071                 brush = (HBRUSH32)GetStockObject32(WHITE_BRUSH);
3072
3073         GetClientRect32(wnd->hwndSelf, &rc);
3074         IntersectClipRect32(dc, rc.left, rc.top, rc.right, rc.bottom);
3075         GetClipBox32(dc, &rc);
3076         /*
3077          *      FIXME:  specs say that we should UnrealizeObject() the brush,
3078          *              but the specs of UnrealizeObject() say that we shouldn't
3079          *              unrealize a stock object.  The default brush that
3080          *              DefWndProc() returns is ... a stock object.
3081          */
3082         FillRect32(dc, &rc, brush);
3083         return -1;
3084 }
3085
3086
3087 /*********************************************************************
3088  *
3089  *      WM_GETTEXT
3090  *
3091  */
3092 static INT32 EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT32 count, LPSTR text)
3093 {
3094         INT32 len = lstrlen32A(es->text);
3095
3096         if (count > len) {
3097                 lstrcpy32A(text, es->text);
3098                 return len;
3099         } else
3100                 return -1;
3101 }
3102
3103
3104 /*********************************************************************
3105  *
3106  *      EDIT_HScroll_Hack
3107  *
3108  *      16 bit notepad needs this.  Actually it is not _our_ hack,
3109  *      it is notepad's.  Notepad is sending us scrollbar messages with
3110  *      undocumented parameters without us even having a scrollbar ... !?!?
3111  *
3112  */
3113 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3114 {
3115         INT32 dx = 0;
3116         INT32 fw = es->format_rect.right - es->format_rect.left;
3117         LRESULT ret = 0;
3118
3119         if (!(es->flags & EF_HSCROLL_HACK)) {
3120                 ERR(edit, "hacked WM_HSCROLL handler invoked\n");
3121                 ERR(edit, "      if you are _not_ running 16 bit notepad, please report\n");
3122                 ERR(edit, "      (this message is only displayed once per edit control)\n");
3123                 es->flags |= EF_HSCROLL_HACK;
3124         }
3125
3126         switch (action) {
3127         case SB_LINELEFT:
3128                 if (es->x_offset)
3129                         dx = -es->char_width;
3130                 break;
3131         case SB_LINERIGHT:
3132                 if (es->x_offset < es->text_width)
3133                         dx = es->char_width;
3134                 break;
3135         case SB_PAGELEFT:
3136                 if (es->x_offset)
3137                         dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3138                 break;
3139         case SB_PAGERIGHT:
3140                 if (es->x_offset < es->text_width)
3141                         dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3142                 break;
3143         case SB_LEFT:
3144                 if (es->x_offset)
3145                         dx = -es->x_offset;
3146                 break;
3147         case SB_RIGHT:
3148                 if (es->x_offset < es->text_width)
3149                         dx = es->text_width - es->x_offset;
3150                 break;
3151         case SB_THUMBTRACK:
3152                 es->flags |= EF_HSCROLL_TRACK;
3153                 dx = pos * es->text_width / 100 - es->x_offset;
3154                 break;
3155         case SB_THUMBPOSITION:
3156                 es->flags &= ~EF_HSCROLL_TRACK;
3157                 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3158                         EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3159                 break;
3160         case SB_ENDSCROLL:
3161                 break;
3162
3163         /*
3164          *      FIXME : the next two are undocumented !
3165          *      Are we doing the right thing ?
3166          *      At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3167          *      although it's also a regular control message.
3168          */
3169         case EM_GETTHUMB16:
3170                 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3171                 break;
3172         case EM_LINESCROLL16:
3173                 dx = pos;
3174                 break;
3175
3176         default:
3177                 ERR(edit, "undocumented (hacked) WM_HSCROLL parameter, please report\n");
3178                 return 0;
3179         }
3180         if (dx)
3181                 EDIT_EM_LineScroll(wnd, es, dx, 0);
3182         return ret;
3183 }
3184
3185
3186 /*********************************************************************
3187  *
3188  *      WM_HSCROLL
3189  *
3190  */
3191 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3192 {
3193         INT32 dx;
3194         INT32 fw;
3195
3196         if (!(es->style & ES_MULTILINE))
3197                 return 0;
3198
3199         if (!(es->style & ES_AUTOHSCROLL))
3200                 return 0;
3201
3202         if (!(es->style & WS_HSCROLL))
3203                 return EDIT_HScroll_Hack(wnd, es, action, pos, scroll_bar);
3204
3205         dx = 0;
3206         fw = es->format_rect.right - es->format_rect.left;
3207         switch (action) {
3208         case SB_LINELEFT:
3209                 if (es->x_offset)
3210                         dx = -es->char_width;
3211                 break;
3212         case SB_LINERIGHT:
3213                 if (es->x_offset < es->text_width)
3214                         dx = es->char_width;
3215                 break;
3216         case SB_PAGELEFT:
3217                 if (es->x_offset)
3218                         dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3219                 break;
3220         case SB_PAGERIGHT:
3221                 if (es->x_offset < es->text_width)
3222                         dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3223                 break;
3224         case SB_LEFT:
3225                 if (es->x_offset)
3226                         dx = -es->x_offset;
3227                 break;
3228         case SB_RIGHT:
3229                 if (es->x_offset < es->text_width)
3230                         dx = es->text_width - es->x_offset;
3231                 break;
3232         case SB_THUMBTRACK:
3233                 es->flags |= EF_HSCROLL_TRACK;
3234                 dx = pos - es->x_offset;
3235                 break;
3236         case SB_THUMBPOSITION:
3237                 es->flags &= ~EF_HSCROLL_TRACK;
3238                 if (!(dx = pos - es->x_offset)) {
3239                         SetScrollPos32(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3240                         EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3241                 }
3242                 break;
3243         case SB_ENDSCROLL:
3244                 break;
3245
3246         default:
3247                 ERR(edit, "undocumented WM_HSCROLL parameter, please report\n");
3248                 return 0;
3249         }
3250         if (dx)
3251                 EDIT_EM_LineScroll(wnd, es, dx, 0);
3252         return 0;
3253 }
3254
3255
3256 /*********************************************************************
3257  *
3258  *      EDIT_CheckCombo
3259  *
3260  */
3261 static BOOL32 EDIT_CheckCombo(WND *wnd, UINT32 msg, INT32 key, DWORD key_data)
3262 {
3263         HWND32 hLBox;
3264
3265         if (WIDGETS_IsControl32(wnd->parent, BIC32_COMBO) &&
3266                         (hLBox = COMBO_GetLBWindow(wnd->parent))) {
3267                 HWND32 hCombo = wnd->parent->hwndSelf;
3268                 BOOL32 bUIFlip = TRUE;
3269
3270                 TRACE(combo, "[%04x]: handling msg %04x (%04x)\n",
3271                              wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3272
3273                 switch (msg) {
3274                 case WM_KEYDOWN: /* Handle F4 and arrow keys */
3275                         if (key != VK_F4) {
3276                                 bUIFlip = (BOOL32)SendMessage32A(hCombo, CB_GETEXTENDEDUI32, 0, 0);
3277                                 if (SendMessage32A(hCombo, CB_GETDROPPEDSTATE32, 0, 0))
3278                                         bUIFlip = FALSE;
3279                         }
3280                         if (!bUIFlip)
3281                                 SendMessage32A(hLBox, WM_KEYDOWN, (WPARAM32)key, 0);
3282                         else {
3283                                 /* make sure ComboLBox pops up */
3284                                 SendMessage32A(hCombo, CB_SETEXTENDEDUI32, 0, 0);
3285                                 SendMessage32A(hLBox, WM_KEYDOWN, VK_F4, 0);
3286                                 SendMessage32A(hCombo, CB_SETEXTENDEDUI32, 1, 0);
3287                         }
3288                         break;
3289                 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3290                         bUIFlip = (BOOL32)SendMessage32A(hCombo, CB_GETEXTENDEDUI32, 0, 0);
3291                         if (bUIFlip) {
3292                                 bUIFlip = (BOOL32)SendMessage32A(hCombo, CB_GETDROPPEDSTATE32, 0, 0);
3293                                 SendMessage32A(hCombo, CB_SHOWDROPDOWN32, (bUIFlip) ? FALSE : TRUE, 0);
3294                         } else
3295                                 SendMessage32A(hLBox, WM_KEYDOWN, VK_F4, 0);
3296                         break;
3297                 }
3298                 return TRUE;
3299         }
3300         return FALSE;
3301 }
3302
3303
3304 /*********************************************************************
3305  *
3306  *      WM_KEYDOWN
3307  *
3308  *      Handling of special keys that don't produce a WM_CHAR
3309  *      (i.e. non-printable keys) & Backspace & Delete
3310  *
3311  */
3312 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data)
3313 {
3314         BOOL32 shift;
3315         BOOL32 control;
3316
3317         if (GetKeyState32(VK_MENU) & 0x8000)
3318                 return 0;
3319
3320         shift = GetKeyState32(VK_SHIFT) & 0x8000;
3321         control = GetKeyState32(VK_CONTROL) & 0x8000;
3322
3323         switch (key) {
3324         case VK_F4:
3325         case VK_UP:
3326                 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3327                         break;
3328                 if (key == VK_F4)
3329                         break;
3330                 /* fall through */
3331         case VK_LEFT:
3332                 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3333                         EDIT_MoveUp_ML(wnd, es, shift);
3334                 else
3335                         if (control)
3336                                 EDIT_MoveWordBackward(wnd, es, shift);
3337                         else
3338                                 EDIT_MoveBackward(wnd, es, shift);
3339                 break;
3340         case VK_DOWN:
3341                 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3342                         break;
3343                 /* fall through */
3344         case VK_RIGHT:
3345                 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3346                         EDIT_MoveDown_ML(wnd, es, shift);
3347                 else if (control)
3348                         EDIT_MoveWordForward(wnd, es, shift);
3349                 else
3350                         EDIT_MoveForward(wnd, es, shift);
3351                 break;
3352         case VK_HOME:
3353                 EDIT_MoveHome(wnd, es, shift);
3354                 break;
3355         case VK_END:
3356                 EDIT_MoveEnd(wnd, es, shift);
3357                 break;
3358         case VK_PRIOR:
3359                 if (es->style & ES_MULTILINE)
3360                         EDIT_MovePageUp_ML(wnd, es, shift);
3361                 break;
3362         case VK_NEXT:
3363                 if (es->style & ES_MULTILINE)
3364                         EDIT_MovePageDown_ML(wnd, es, shift);
3365                 break;
3366         case VK_BACK:
3367                 if (!(es->style & ES_READONLY) && !control) {
3368                         if (es->selection_start != es->selection_end)
3369                                 EDIT_WM_Clear(wnd, es);
3370                         else {
3371                                 /* delete character left of caret */
3372                                 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3373                                 EDIT_MoveBackward(wnd, es, TRUE);
3374                                 EDIT_WM_Clear(wnd, es);
3375                         }
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                 }
3404                 break;
3405         case VK_INSERT:
3406                 if (shift) {
3407                         if (!(es->style & ES_READONLY))
3408                                 EDIT_WM_Paste(wnd, es);
3409                 } else if (control)
3410                         EDIT_WM_Copy(wnd, es);
3411                 break;
3412         }
3413         return 0;
3414 }
3415
3416
3417 /*********************************************************************
3418  *
3419  *      WM_KILLFOCUS
3420  *
3421  */
3422 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND32 window_getting_focus)
3423 {
3424         es->flags &= ~EF_FOCUSED;
3425         DestroyCaret32();
3426         if(!(es->style & ES_NOHIDESEL))
3427                 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3428         EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
3429         return 0;
3430 }
3431
3432
3433 /*********************************************************************
3434  *
3435  *      WM_LBUTTONDBLCLK
3436  *
3437  *      The caret position has been set on the WM_LBUTTONDOWN message
3438  *
3439  */
3440 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3441 {
3442         INT32 s;
3443         INT32 e = es->selection_end;
3444         INT32 l;
3445         INT32 li;
3446         INT32 ll;
3447
3448         if (!(es->flags & EF_FOCUSED))
3449                 return 0;
3450
3451         l = EDIT_EM_LineFromChar(wnd, es, e);
3452         li = EDIT_EM_LineIndex(wnd, es, l);
3453         ll = EDIT_EM_LineLength(wnd, es, e);
3454         s = li + EDIT_CallWordBreakProc (wnd, es, li, e - li, ll, WB_LEFT);
3455         e = li + EDIT_CallWordBreakProc(wnd, es, li, e - li, ll, WB_RIGHT);
3456         EDIT_EM_SetSel(wnd, es, s, e, FALSE);
3457         EDIT_EM_ScrollCaret(wnd, es);
3458         return 0;
3459 }
3460
3461
3462 /*********************************************************************
3463  *
3464  *      WM_LBUTTONDOWN
3465  *
3466  */
3467 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3468 {
3469         INT32 e;
3470         BOOL32 after_wrap;
3471
3472         if (!(es->flags & EF_FOCUSED))
3473                 return 0;
3474
3475         SetCapture32(wnd->hwndSelf);
3476         EDIT_ConfinePoint(wnd, es, &x, &y);
3477         e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3478         EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3479         EDIT_EM_ScrollCaret(wnd, es);
3480         es->region_posx = es->region_posy = 0;
3481         SetTimer32(wnd->hwndSelf, 0, 100, NULL);
3482         return 0;
3483 }
3484
3485
3486 /*********************************************************************
3487  *
3488  *      WM_LBUTTONUP
3489  *
3490  */
3491 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3492 {
3493         if (GetCapture32() == wnd->hwndSelf) {
3494                 KillTimer32(wnd->hwndSelf, 0);
3495                 ReleaseCapture();
3496         }
3497         return 0;
3498 }
3499
3500
3501 /*********************************************************************
3502  *
3503  *      WM_MOUSEMOVE
3504  *
3505  */
3506 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3507 {
3508         INT32 e;
3509         BOOL32 after_wrap;
3510         INT32 prex, prey;
3511
3512         if (GetCapture32() != wnd->hwndSelf)
3513                 return 0;
3514
3515         /*
3516          *      FIXME: gotta do some scrolling if outside client
3517          *              area.  Maybe reset the timer ?
3518          */
3519         prex = x; prey = y;
3520         EDIT_ConfinePoint(wnd, es, &x, &y);
3521         es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3522         es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3523         e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3524         EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
3525         return 0;
3526 }
3527
3528
3529 /*********************************************************************
3530  *
3531  *      WM_NCCREATE
3532  *
3533  */
3534 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCT32A cs)
3535 {
3536         EDITSTATE *es;
3537
3538         if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
3539                 return FALSE;
3540         *(EDITSTATE **)wnd->wExtra = es;
3541
3542        /*
3543         *      Note: since the EDITSTATE has not been fully initialized yet,
3544         *            we can't use any API calls that may send
3545         *            WM_XXX messages before WM_NCCREATE is completed.
3546         */
3547
3548         if (!(es->heap = HeapCreate(0, 0x10000, 0)))
3549                 return FALSE;
3550         es->style = cs->style;
3551  
3552         if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
3553                 wnd->dwStyle &= ~WS_BORDER;
3554
3555         if (es->style & ES_MULTILINE) {
3556                 es->buffer_size = BUFSTART_MULTI;
3557                 es->buffer_limit = BUFLIMIT_MULTI;
3558                 if (es->style & WS_VSCROLL)
3559                         es->style |= ES_AUTOVSCROLL;
3560                 if (es->style & WS_HSCROLL)
3561                         es->style |= ES_AUTOHSCROLL;
3562                 es->style &= ~ES_PASSWORD;
3563                 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
3564                         if (es->style & ES_RIGHT)
3565                                 es->style &= ~ES_CENTER;
3566                         es->style &= ~WS_HSCROLL;
3567                         es->style &= ~ES_AUTOHSCROLL;
3568                 }
3569
3570                 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
3571                 es->style |= ES_AUTOVSCROLL;
3572         } else {
3573                 es->buffer_size = BUFSTART_SINGLE;
3574                 es->buffer_limit = BUFLIMIT_SINGLE;
3575                 es->style &= ~ES_CENTER;
3576                 es->style &= ~ES_RIGHT;
3577                 es->style &= ~WS_HSCROLL;
3578                 es->style &= ~WS_VSCROLL;
3579                 es->style &= ~ES_AUTOVSCROLL;
3580                 es->style &= ~ES_WANTRETURN;
3581                 if (es->style & ES_UPPERCASE) {
3582                         es->style &= ~ES_LOWERCASE;
3583                         es->style &= ~ES_NUMBER;
3584                 } else if (es->style & ES_LOWERCASE)
3585                         es->style &= ~ES_NUMBER;
3586                 if (es->style & ES_PASSWORD)
3587                         es->password_char = '*';
3588
3589                 /* FIXME: for now, all single line controls are AUTOHSCROLL */
3590                 es->style |= ES_AUTOHSCROLL;
3591         }
3592         if (!(es->text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3593                 return FALSE;
3594         es->buffer_size = HeapSize(es->heap, 0, es->text) - 1;
3595         if (!(es->undo_text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3596                 return FALSE;
3597         es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
3598         *es->text = '\0';
3599         if (es->style & ES_MULTILINE)
3600                 if (!(es->first_line_def = HeapAlloc(es->heap, HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
3601                         return FALSE;
3602         es->line_count = 1;
3603
3604         return TRUE;
3605 }
3606
3607 /*********************************************************************
3608  *
3609  *      WM_PAINT
3610  *
3611  */
3612 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es)
3613 {
3614         PAINTSTRUCT32 ps;
3615         INT32 i;
3616         HDC32 dc;
3617         HFONT32 old_font = 0;
3618         RECT32 rc;
3619         RECT32 rcLine;
3620         RECT32 rcRgn;
3621         BOOL32 rev = IsWindowEnabled32(wnd->hwndSelf) &&
3622                                 ((es->flags & EF_FOCUSED) ||
3623                                         (es->style & ES_NOHIDESEL));
3624
3625         if (es->flags & EF_UPDATE)
3626                 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
3627
3628         dc = BeginPaint32(wnd->hwndSelf, &ps);
3629         if(es->style & WS_BORDER) {
3630                 GetClientRect32(wnd->hwndSelf, &rc);
3631                 if(es->style & ES_MULTILINE) {
3632                         if(es->style & WS_HSCROLL) rc.bottom++;
3633                         if(es->style & WS_VSCROLL) rc.right++;
3634                 }
3635                 Rectangle32(dc, rc.left, rc.top, rc.right, rc.bottom);
3636         }
3637         IntersectClipRect32(dc, es->format_rect.left,
3638                                 es->format_rect.top,
3639                                 es->format_rect.right,
3640                                 es->format_rect.bottom);
3641         if (es->style & ES_MULTILINE) {
3642                 GetClientRect32(wnd->hwndSelf, &rc);
3643                 IntersectClipRect32(dc, rc.left, rc.top, rc.right, rc.bottom);
3644         }
3645         if (es->font)
3646                 old_font = SelectObject32(dc, es->font);
3647         EDIT_SEND_CTLCOLOR(wnd, dc);
3648         if (!IsWindowEnabled32(wnd->hwndSelf))
3649                 SetTextColor32(dc, GetSysColor32(COLOR_GRAYTEXT));
3650         GetClipBox32(dc, &rcRgn);
3651         if (es->style & ES_MULTILINE) {
3652                 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3653                 for (i = es->y_offset ; i <= MIN(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3654                         EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
3655                         if (IntersectRect32(&rc, &rcRgn, &rcLine))
3656                                 EDIT_PaintLine(wnd, es, dc, i, rev);
3657                 }
3658         } else {
3659                 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
3660                 if (IntersectRect32(&rc, &rcRgn, &rcLine))
3661                         EDIT_PaintLine(wnd, es, dc, 0, rev);
3662         }
3663         if (es->font)
3664                 SelectObject32(dc, old_font);
3665         if (es->flags & EF_FOCUSED)
3666                 EDIT_SetCaretPos(wnd, es, es->selection_end,
3667                                  es->flags & EF_AFTER_WRAP);
3668         EndPaint32(wnd->hwndSelf, &ps);
3669         if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK)) {
3670                 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3671                 SCROLLINFO si;
3672                 si.cbSize       = sizeof(SCROLLINFO);
3673                 si.fMask        = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3674                 si.nMin         = 0;
3675                 si.nMax         = es->line_count + vlc - 2;
3676                 si.nPage        = vlc;
3677                 si.nPos         = es->y_offset;
3678                 SetScrollInfo32(wnd->hwndSelf, SB_VERT, &si, TRUE);
3679         }
3680         if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK)) {
3681                 SCROLLINFO si;
3682                 INT32 fw = es->format_rect.right - es->format_rect.left;
3683                 si.cbSize       = sizeof(SCROLLINFO);
3684                 si.fMask        = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3685                 si.nMin         = 0;
3686                 si.nMax         = es->text_width + fw - 1;
3687                 si.nPage        = fw;
3688                 si.nPos         = es->x_offset;
3689                 SetScrollInfo32(wnd->hwndSelf, SB_HORZ, &si, TRUE);
3690         }
3691
3692         if (es->flags & EF_UPDATE) {
3693                 es->flags &= ~EF_UPDATE;
3694                 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3695         }
3696 }
3697
3698
3699 /*********************************************************************
3700  *
3701  *      WM_PASTE
3702  *
3703  */
3704 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
3705 {
3706         HGLOBAL32 hsrc;
3707         LPSTR src;
3708
3709         OpenClipboard32(wnd->hwndSelf);
3710         if ((hsrc = GetClipboardData32(CF_TEXT))) {
3711                 src = (LPSTR)GlobalLock32(hsrc);
3712                 EDIT_EM_ReplaceSel(wnd, es, TRUE, src);
3713                 GlobalUnlock32(hsrc);
3714         }
3715         CloseClipboard32();
3716 }
3717
3718
3719 /*********************************************************************
3720  *
3721  *      WM_SETFOCUS
3722  *
3723  */
3724 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND32 window_losing_focus)
3725 {
3726         es->flags |= EF_FOCUSED;
3727         CreateCaret32(wnd->hwndSelf, 0, 2, es->line_height);
3728         EDIT_SetCaretPos(wnd, es, es->selection_end,
3729                          es->flags & EF_AFTER_WRAP);
3730         if(!(es->style & ES_NOHIDESEL))
3731                 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3732         ShowCaret32(wnd->hwndSelf);
3733         EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
3734 }
3735
3736
3737 /*********************************************************************
3738  *
3739  *      WM_SETFONT
3740  *
3741  * With Win95 look the margins are set to default font value unless 
3742  * the system font (font == 0) is being set, in which case they are left
3743  * unchanged.
3744  *
3745  */
3746 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT32 font, BOOL32 redraw)
3747 {
3748         TEXTMETRIC32A tm;
3749         HDC32 dc;
3750         HFONT32 old_font = 0;
3751
3752         es->font = font;
3753         dc = GetDC32(wnd->hwndSelf);
3754         if (font)
3755                 old_font = SelectObject32(dc, font);
3756         GetTextMetrics32A(dc, &tm);
3757         es->line_height = tm.tmHeight;
3758         es->char_width = tm.tmAveCharWidth;
3759         if (font)
3760                 SelectObject32(dc, old_font);
3761         ReleaseDC32(wnd->hwndSelf, dc);
3762         if (font && (TWEAK_WineLook > WIN31_LOOK))
3763                 EDIT_EM_SetMargins(wnd, es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3764                                    EC_USEFONTINFO, EC_USEFONTINFO);
3765         if (es->style & ES_MULTILINE)
3766                 EDIT_BuildLineDefs_ML(wnd, es);
3767         else {
3768                 RECT32 r;
3769                 GetClientRect32(wnd->hwndSelf, &r);
3770                 EDIT_SetRectNP(wnd, es, &r);
3771         }
3772         if (redraw)
3773                 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
3774         if (es->flags & EF_FOCUSED) {
3775                 DestroyCaret32();
3776                 CreateCaret32(wnd->hwndSelf, 0, 2, es->line_height);
3777                 EDIT_SetCaretPos(wnd, es, es->selection_end,
3778                                  es->flags & EF_AFTER_WRAP);
3779                 ShowCaret32(wnd->hwndSelf);
3780         }
3781 }
3782
3783
3784 /*********************************************************************
3785  *
3786  *      WM_SETTEXT
3787  *
3788  * NOTES
3789  *  For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3790  *  The modified flag is reset. No notifications are sent.
3791  *
3792  *  For single-line controls, reception of WM_SETTEXT triggers:
3793  *  The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3794  *
3795  */
3796 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text)
3797 {
3798         EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3799         if (text) {
3800                 TRACE(edit, "\t'%s'\n", text);
3801                 EDIT_EM_ReplaceSel(wnd, es, FALSE, text);
3802         } else {
3803                 TRACE(edit, "\t<NULL>\n");
3804                 EDIT_EM_ReplaceSel(wnd, es, FALSE, "");
3805         }
3806         es->x_offset = 0;
3807         if (es->style & ES_MULTILINE) {
3808                 es->flags &= ~EF_UPDATE;
3809         } else {
3810                 es->flags |= EF_UPDATE;
3811         }
3812         es->flags &= ~EF_MODIFIED;
3813         EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
3814         EDIT_EM_ScrollCaret(wnd, es);
3815 }
3816
3817
3818 /*********************************************************************
3819  *
3820  *      WM_SIZE
3821  *
3822  */
3823 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT32 action, INT32 width, INT32 height)
3824 {
3825         if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3826                 RECT32 rc;
3827                 SetRect32(&rc, 0, 0, width, height);
3828                 EDIT_SetRectNP(wnd, es, &rc);
3829                 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
3830         }
3831 }
3832
3833
3834 /*********************************************************************
3835  *
3836  *      WM_SYSKEYDOWN
3837  *
3838  */
3839 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data)
3840 {
3841         if ((key == VK_BACK) && (key_data & 0x2000)) {
3842                 if (EDIT_EM_CanUndo(wnd, es))
3843                         EDIT_EM_Undo(wnd, es);
3844                 return 0;
3845         } else if (key == VK_UP || key == VK_DOWN)
3846                 if (EDIT_CheckCombo(wnd, WM_SYSKEYDOWN, key, key_data))
3847                         return 0;
3848         return DefWindowProc32A(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM32)key, (LPARAM)key_data);
3849 }
3850
3851
3852 /*********************************************************************
3853  *
3854  *      WM_TIMER
3855  *
3856  */
3857 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT32 id, TIMERPROC32 timer_proc)
3858 {
3859         if (es->region_posx < 0) {
3860                 EDIT_MoveBackward(wnd, es, TRUE);
3861         } else if (es->region_posx > 0) {
3862                 EDIT_MoveForward(wnd, es, TRUE);
3863         }
3864 /*
3865  *      FIXME: gotta do some vertical scrolling here, like
3866  *              EDIT_EM_LineScroll(wnd, 0, 1);
3867  */
3868 }
3869
3870
3871 /*********************************************************************
3872  *
3873  *      EDIT_VScroll_Hack
3874  *
3875  *      16 bit notepad needs this.  Actually it is not _our_ hack,
3876  *      it is notepad's.  Notepad is sending us scrollbar messages with
3877  *      undocumented parameters without us even having a scrollbar ... !?!?
3878  *
3879  */
3880 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3881 {
3882         INT32 dy = 0;
3883         LRESULT ret = 0;
3884
3885         if (!(es->flags & EF_VSCROLL_HACK)) {
3886                 ERR(edit, "hacked WM_VSCROLL handler invoked\n");
3887                 ERR(edit, "      if you are _not_ running 16 bit notepad, please report\n");
3888                 ERR(edit, "      (this message is only displayed once per edit control)\n");
3889                 es->flags |= EF_VSCROLL_HACK;
3890         }
3891
3892         switch (action) {
3893         case SB_LINEUP:
3894         case SB_LINEDOWN:
3895         case SB_PAGEUP:
3896         case SB_PAGEDOWN:
3897                 EDIT_EM_Scroll(wnd, es, action);
3898                 return 0;
3899         case SB_TOP:
3900                 dy = -es->y_offset;
3901                 break;
3902         case SB_BOTTOM:
3903                 dy = es->line_count - 1 - es->y_offset;
3904                 break;
3905         case SB_THUMBTRACK:
3906                 es->flags |= EF_VSCROLL_TRACK;
3907                 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
3908                 break;
3909         case SB_THUMBPOSITION:
3910                 es->flags &= ~EF_VSCROLL_TRACK;
3911                 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
3912                         EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3913                 break;
3914         case SB_ENDSCROLL:
3915                 break;
3916
3917         /*
3918          *      FIXME : the next two are undocumented !
3919          *      Are we doing the right thing ?
3920          *      At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3921          *      although it's also a regular control message.
3922          */
3923         case EM_GETTHUMB16:
3924                 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
3925                 break;
3926         case EM_LINESCROLL16:
3927                 dy = pos;
3928                 break;
3929
3930         default:
3931                 ERR(edit, "undocumented (hacked) WM_VSCROLL parameter, please report\n");
3932                 return 0;
3933         }
3934         if (dy)
3935                 EDIT_EM_LineScroll(wnd, es, 0, dy);
3936         return ret;
3937 }
3938
3939
3940 /*********************************************************************
3941  *
3942  *      WM_VSCROLL
3943  *
3944  */
3945 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3946 {
3947         INT32 dy;
3948
3949         if (!(es->style & ES_MULTILINE))
3950                 return 0;
3951
3952         if (!(es->style & ES_AUTOVSCROLL))
3953                 return 0;
3954
3955         if (!(es->style & WS_VSCROLL))
3956                 return EDIT_VScroll_Hack(wnd, es, action, pos, scroll_bar);
3957
3958         dy = 0;
3959         switch (action) {
3960         case SB_LINEUP:
3961         case SB_LINEDOWN:
3962         case SB_PAGEUP:
3963         case SB_PAGEDOWN:
3964                 EDIT_EM_Scroll(wnd, es, action);
3965                 return 0;
3966
3967         case SB_TOP:
3968                 dy = -es->y_offset;
3969                 break;
3970         case SB_BOTTOM:
3971                 dy = es->line_count - 1 - es->y_offset;
3972                 break;
3973         case SB_THUMBTRACK:
3974                 es->flags |= EF_VSCROLL_TRACK;
3975                 dy = pos - es->y_offset;
3976                 break;
3977         case SB_THUMBPOSITION:
3978                 es->flags &= ~EF_VSCROLL_TRACK;
3979                 if (!(dy = pos - es->y_offset)) {
3980                         SetScrollPos32(wnd->hwndSelf, SB_VERT, pos, TRUE);
3981                         EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3982                 }
3983                 break;
3984         case SB_ENDSCROLL:
3985                 break;
3986
3987         default:
3988                 ERR(edit, "undocumented WM_VSCROLL action %d, please report\n",
3989                         action);
3990                 return 0;
3991         }
3992         if (dy)
3993                 EDIT_EM_LineScroll(wnd, es, 0, dy);
3994         return 0;
3995 }