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