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