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