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