4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * please read EDIT.TODO (and update it when you change things)
36 #include "wine/winbase16.h"
37 #include "wine/winuser16.h"
38 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(edit);
45 WINE_DECLARE_DEBUG_CHANNEL(combo);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
49 FIXME: BTW, new specs say 65535 (do you dare ???) */
50 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
51 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
52 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
53 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
56 * extra flags for EDITSTATE.flags field
58 #define EF_MODIFIED 0x0001 /* text has been modified */
59 #define EF_FOCUSED 0x0002 /* we have input focus */
60 #define EF_UPDATE 0x0004 /* notify parent of changed state */
61 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
62 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
63 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
64 wrapped line, instead of in front of the next character */
65 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
69 END_0 = 0, /* line ends with terminating '\0' character */
70 END_WRAP, /* line is wrapped */
71 END_HARD, /* line ends with a hard return '\r\n' */
72 END_SOFT /* line ends with a soft return '\r\r\n' */
75 typedef struct tagLINEDEF {
76 INT length; /* bruto length of a line in bytes */
77 INT net_length; /* netto length of a line in visible characters */
79 INT width; /* width of the line in pixels */
80 INT index; /* line index into the buffer */
81 struct tagLINEDEF *next;
86 BOOL is_unicode; /* how the control was created */
87 LPWSTR text; /* the actual contents of the control */
88 UINT buffer_size; /* the size of the buffer in characters */
89 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
90 HFONT font; /* NULL means standard system font */
91 INT x_offset; /* scroll offset for multi lines this is in pixels
92 for single lines it's in characters */
93 INT line_height; /* height of a screen line in pixels */
94 INT char_width; /* average character width in pixels */
95 DWORD style; /* sane version of wnd->dwStyle */
96 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
97 INT undo_insert_count; /* number of characters inserted in sequence */
98 UINT undo_position; /* character index of the insertion and deletion */
99 LPWSTR undo_text; /* deleted text */
100 UINT undo_buffer_size; /* size of the deleted text buffer */
101 INT selection_start; /* == selection_end if no selection */
102 INT selection_end; /* == current caret position */
103 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
104 INT left_margin; /* in pixels */
105 INT right_margin; /* in pixels */
107 INT text_width; /* width of the widest line in pixels for multi line controls
108 and just line width for single line controls */
109 INT region_posx; /* Position of cursor relative to region: */
110 INT region_posy; /* -1: to left, 0: within, 1: to right */
111 EDITWORDBREAKPROC16 word_break_proc16;
112 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
113 INT line_count; /* number of lines */
114 INT y_offset; /* scroll offset in number of lines */
115 BOOL bCaptureState; /* flag indicating whether mouse was captured */
116 BOOL bEnableState; /* flag keeping the enable state */
117 HWND hwndParent; /* Handle of parent for sending EN_* messages.
118 Even if parent will change, EN_* messages
119 should be sent to the first parent. */
120 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
122 * only for multi line controls
124 INT lock_count; /* amount of re-entries in the EditWndProc */
127 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
128 HLOCAL hloc32W; /* our unicode local memory block */
129 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
131 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
136 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
137 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
139 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
140 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
142 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
143 do {TRACE("notification " str " sent to hwnd=%08x\n", \
144 (UINT)(hwnd));} while(0)
146 /* used for disabled or read-only edit control */
147 #define EDIT_SEND_CTLCOLORSTATIC(hwnd,hdc) \
148 (SendMessageW(GetParent(hwnd), WM_CTLCOLORSTATIC, \
149 (WPARAM)(hdc), (LPARAM)(hwnd)))
150 #define EDIT_SEND_CTLCOLOR(hwnd,hdc) \
151 (SendMessageW(GetParent(hwnd), WM_CTLCOLOREDIT, \
152 (WPARAM)(hdc), (LPARAM)(hwnd)))
153 #define EDIT_NOTIFY_PARENT(hwnd, es, wNotifyCode, str) \
155 { /* Notify parent which has created this edit control */ \
156 DPRINTF_EDIT_NOTIFY((es)->hwndParent, str); \
157 SendMessageW((es)->hwndParent, WM_COMMAND, \
158 MAKEWPARAM(GetWindowLongA((hwnd),GWL_ID), wNotifyCode), \
161 #define DPRINTF_EDIT_MSG16(str) \
163 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
164 hwnd, (UINT)wParam, (UINT)lParam)
165 #define DPRINTF_EDIT_MSG32(str) \
167 "32 bit %c : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
168 unicode ? 'W' : 'A', \
169 hwnd, (UINT)wParam, (UINT)lParam)
171 /*********************************************************************
178 * These functions have trivial implementations
179 * We still like to call them internally
180 * "static inline" makes them more like macro's
182 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
183 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
184 static inline void EDIT_WM_Clear(HWND hwnd, EDITSTATE *es);
185 static inline void EDIT_WM_Cut(HWND hwnd, EDITSTATE *es);
188 * Helper functions only valid for one type of control
190 static void EDIT_BuildLineDefs_ML(HWND hwnd, EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
191 static void EDIT_CalcLineWidth_SL(HWND hwnd, EDITSTATE *es);
192 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
193 static void EDIT_MoveDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
194 static void EDIT_MovePageDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
195 static void EDIT_MovePageUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
196 static void EDIT_MoveUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
198 * Helper functions valid for both single line _and_ multi line controls
200 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
201 static INT EDIT_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
202 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
203 static void EDIT_GetLineRect(HWND hwnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
204 static void EDIT_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end);
205 static void EDIT_LockBuffer(HWND hwnd, EDITSTATE *es);
206 static BOOL EDIT_MakeFit(HWND hwnd, EDITSTATE *es, UINT size);
207 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
208 static void EDIT_MoveBackward(HWND hwnd, EDITSTATE *es, BOOL extend);
209 static void EDIT_MoveEnd(HWND hwnd, EDITSTATE *es, BOOL extend);
210 static void EDIT_MoveForward(HWND hwnd, EDITSTATE *es, BOOL extend);
211 static void EDIT_MoveHome(HWND hwnd, EDITSTATE *es, BOOL extend);
212 static void EDIT_MoveWordBackward(HWND hwnd, EDITSTATE *es, BOOL extend);
213 static void EDIT_MoveWordForward(HWND hwnd, EDITSTATE *es, BOOL extend);
214 static void EDIT_PaintLine(HWND hwnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
215 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
216 static void EDIT_SetCaretPos(HWND hwnd, EDITSTATE *es, INT pos, BOOL after_wrap);
217 static void EDIT_SetRectNP(HWND hwnd, EDITSTATE *es, LPRECT lprc);
218 static void EDIT_UnlockBuffer(HWND hwnd, EDITSTATE *es, BOOL force);
219 static void EDIT_UpdateScrollInfo(HWND hwnd, EDITSTATE *es);
220 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
222 * EM_XXX message handlers
224 static LRESULT EDIT_EM_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y);
225 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
226 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
227 static HLOCAL16 EDIT_EM_GetHandle16(HWND hwnd, EDITSTATE *es);
228 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
229 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end);
230 static LRESULT EDIT_EM_GetThumb(HWND hwnd, EDITSTATE *es);
231 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
232 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
233 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
234 static BOOL EDIT_EM_LineScroll(HWND hwnd, EDITSTATE *es, INT dx, INT dy);
235 static BOOL EDIT_EM_LineScroll_internal(HWND hwnd, EDITSTATE *es, INT dx, INT dy);
236 static LRESULT EDIT_EM_PosFromChar(HWND hwnd, EDITSTATE *es, INT index, BOOL after_wrap);
237 static void EDIT_EM_ReplaceSel(HWND hwnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update);
238 static LRESULT EDIT_EM_Scroll(HWND hwnd, EDITSTATE *es, INT action);
239 static void EDIT_EM_ScrollCaret(HWND hwnd, EDITSTATE *es);
240 static void EDIT_EM_SetHandle(HWND hwnd, EDITSTATE *es, HLOCAL hloc);
241 static void EDIT_EM_SetHandle16(HWND hwnd, EDITSTATE *es, HLOCAL16 hloc);
242 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
243 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
244 static void EDIT_EM_SetPasswordChar(HWND hwnd, EDITSTATE *es, WCHAR c);
245 static void EDIT_EM_SetSel(HWND hwnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
246 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
247 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
248 static void EDIT_EM_SetWordBreakProc(HWND hwnd, EDITSTATE *es, LPARAM lParam);
249 static void EDIT_EM_SetWordBreakProc16(HWND hwnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
250 static BOOL EDIT_EM_Undo(HWND hwnd, EDITSTATE *es);
252 * WM_XXX message handlers
254 static void EDIT_WM_Char(HWND hwnd, EDITSTATE *es, WCHAR c);
255 static void EDIT_WM_Command(HWND hwnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
256 static void EDIT_WM_ContextMenu(HWND hwnd, EDITSTATE *es, INT x, INT y);
257 static void EDIT_WM_Copy(HWND hwnd, EDITSTATE *es);
258 static LRESULT EDIT_WM_Create(HWND hwnd, EDITSTATE *es, LPCWSTR name);
259 static void EDIT_WM_Destroy(HWND hwnd, EDITSTATE *es);
260 static LRESULT EDIT_WM_EraseBkGnd(HWND hwnd, EDITSTATE *es, HDC dc);
261 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
262 static LRESULT EDIT_WM_HScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos);
263 static LRESULT EDIT_WM_KeyDown(HWND hwnd, EDITSTATE *es, INT key);
264 static LRESULT EDIT_WM_KillFocus(HWND hwnd, EDITSTATE *es);
265 static LRESULT EDIT_WM_LButtonDblClk(HWND hwnd, EDITSTATE *es);
266 static LRESULT EDIT_WM_LButtonDown(HWND hwnd, EDITSTATE *es, DWORD keys, INT x, INT y);
267 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es);
268 static LRESULT EDIT_WM_MButtonDown(HWND hwnd);
269 static LRESULT EDIT_WM_MouseMove(HWND hwnd, EDITSTATE *es, INT x, INT y);
270 static LRESULT EDIT_WM_NCCreate(HWND hwnd, DWORD style, HWND hwndParent, BOOL unicode);
271 static void EDIT_WM_Paint(HWND hwnd, EDITSTATE *es, WPARAM wParam);
272 static void EDIT_WM_Paste(HWND hwnd, EDITSTATE *es);
273 static void EDIT_WM_SetFocus(HWND hwnd, EDITSTATE *es);
274 static void EDIT_WM_SetFont(HWND hwnd, EDITSTATE *es, HFONT font, BOOL redraw);
275 static void EDIT_WM_SetText(HWND hwnd, EDITSTATE *es, LPARAM lParam, BOOL unicode);
276 static void EDIT_WM_Size(HWND hwnd, EDITSTATE *es, UINT action, INT width, INT height);
277 static LRESULT EDIT_WM_StyleChanged (HWND hwnd, EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
278 static LRESULT EDIT_WM_SysKeyDown(HWND hwnd, EDITSTATE *es, INT key, DWORD key_data);
279 static void EDIT_WM_Timer(HWND hwnd, EDITSTATE *es);
280 static LRESULT EDIT_WM_VScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos);
281 static void EDIT_UpdateText(HWND hwnd, EDITSTATE *es, LPRECT rc, BOOL bErase);
282 static void EDIT_UpdateTextRegion(HWND hwnd, EDITSTATE *es, HRGN hrgn, BOOL bErase);
284 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
285 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
287 /*********************************************************************
288 * edit class descriptor
290 const struct builtin_class_descr EDIT_builtin_class =
293 CS_GLOBALCLASS | CS_DBLCLKS /*| CS_PARENTDC*/, /* style */
294 EditWndProcA, /* procA */
295 EditWndProcW, /* procW */
296 sizeof(EDITSTATE *), /* extra */
297 IDC_IBEAMA, /* cursor */
302 /*********************************************************************
307 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
309 return (es->undo_insert_count || strlenW(es->undo_text));
313 /*********************************************************************
318 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
320 es->undo_insert_count = 0;
321 *es->undo_text = '\0';
325 /*********************************************************************
330 static inline void EDIT_WM_Clear(HWND hwnd, EDITSTATE *es)
332 static const WCHAR empty_stringW[] = {0};
334 /* Protect read-only edit control from modification */
335 if(es->style & ES_READONLY)
338 EDIT_EM_ReplaceSel(hwnd, es, TRUE, empty_stringW, TRUE);
342 /*********************************************************************
347 static inline void EDIT_WM_Cut(HWND hwnd, EDITSTATE *es)
349 EDIT_WM_Copy(hwnd, es);
350 EDIT_WM_Clear(hwnd, es);
354 /**********************************************************************
357 * Returns the window version in case Wine emulates a later version
358 * of windows then the application expects.
360 * In a number of cases when windows runs an application that was
361 * designed for an earlier windows version, windows reverts
362 * to "old" behaviour of that earlier version.
364 * An example is a disabled edit control that needs to be painted.
365 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
366 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
367 * applications with an expected version 0f 4.0 or higher.
370 static DWORD get_app_version(void)
372 static DWORD version;
375 DWORD dwEmulatedVersion;
377 DWORD dwProcVersion = GetProcessVersion(0);
379 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
380 GetVersionExW( &info );
381 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
382 /* FIXME: this may not be 100% correct; see discussion on the
383 * wine developer list in Nov 1999 */
384 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
390 /*********************************************************************
394 * The messages are in the order of the actual integer values
395 * (which can be found in include/windows.h)
396 * Wherever possible the 16 bit versions are converted to
397 * the 32 bit ones, so that we can 'fall through' to the
398 * helper functions. These are mostly 32 bit (with a few
399 * exceptions, clearly indicated by a '16' extension to their
403 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
404 WPARAM wParam, LPARAM lParam, BOOL unicode )
406 EDITSTATE *es = (EDITSTATE *)GetWindowLongA( hwnd, 0 );
411 DPRINTF_EDIT_MSG32("WM_DESTROY");
412 if (es) EDIT_WM_Destroy(hwnd, es);
417 DPRINTF_EDIT_MSG32("WM_NCCREATE");
420 LPCREATESTRUCTW cs = (LPCREATESTRUCTW)lParam;
421 result = EDIT_WM_NCCreate(hwnd, cs->style, cs->hwndParent, TRUE);
425 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
426 result = EDIT_WM_NCCreate(hwnd, cs->style, cs->hwndParent, FALSE);
434 result = DefWindowProcW(hwnd, msg, wParam, lParam);
436 result = DefWindowProcA(hwnd, msg, wParam, lParam);
441 EDIT_LockBuffer(hwnd, es);
444 DPRINTF_EDIT_MSG16("EM_GETSEL");
449 DPRINTF_EDIT_MSG32("EM_GETSEL");
450 result = EDIT_EM_GetSel(es, (LPUINT)wParam, (LPUINT)lParam);
454 DPRINTF_EDIT_MSG16("EM_SETSEL");
455 if (SLOWORD(lParam) == -1)
456 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
458 EDIT_EM_SetSel(hwnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
460 EDIT_EM_ScrollCaret(hwnd, es);
464 DPRINTF_EDIT_MSG32("EM_SETSEL");
465 EDIT_EM_SetSel(hwnd, es, wParam, lParam, FALSE);
466 EDIT_EM_ScrollCaret(hwnd, es);
471 DPRINTF_EDIT_MSG16("EM_GETRECT");
473 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
476 DPRINTF_EDIT_MSG32("EM_GETRECT");
478 CopyRect((LPRECT)lParam, &es->format_rect);
482 DPRINTF_EDIT_MSG16("EM_SETRECT");
483 if ((es->style & ES_MULTILINE) && lParam) {
485 CONV_RECT16TO32(MapSL(lParam), &rc);
486 EDIT_SetRectNP(hwnd, es, &rc);
487 EDIT_UpdateText(hwnd, es, NULL, TRUE);
491 DPRINTF_EDIT_MSG32("EM_SETRECT");
492 if ((es->style & ES_MULTILINE) && lParam) {
493 EDIT_SetRectNP(hwnd, es, (LPRECT)lParam);
494 EDIT_UpdateText(hwnd, es, NULL, TRUE);
499 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
500 if ((es->style & ES_MULTILINE) && lParam) {
502 CONV_RECT16TO32(MapSL(lParam), &rc);
503 EDIT_SetRectNP(hwnd, es, &rc);
507 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
508 if ((es->style & ES_MULTILINE) && lParam)
509 EDIT_SetRectNP(hwnd, es, (LPRECT)lParam);
513 DPRINTF_EDIT_MSG16("EM_SCROLL");
516 DPRINTF_EDIT_MSG32("EM_SCROLL");
517 result = EDIT_EM_Scroll(hwnd, es, (INT)wParam);
520 case EM_LINESCROLL16:
521 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
522 wParam = (WPARAM)(INT)SHIWORD(lParam);
523 lParam = (LPARAM)(INT)SLOWORD(lParam);
526 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
527 result = (LRESULT)EDIT_EM_LineScroll(hwnd, es, (INT)wParam, (INT)lParam);
530 case EM_SCROLLCARET16:
531 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
534 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
535 EDIT_EM_ScrollCaret(hwnd, es);
540 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
543 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
544 result = ((es->flags & EF_MODIFIED) != 0);
548 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
551 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
553 es->flags |= EF_MODIFIED;
555 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
558 case EM_GETLINECOUNT16:
559 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
561 case EM_GETLINECOUNT:
562 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
563 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
567 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
568 if ((INT16)wParam == -1)
572 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
573 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
577 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
578 EDIT_EM_SetHandle16(hwnd, es, (HLOCAL16)wParam);
581 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
582 EDIT_EM_SetHandle(hwnd, es, (HLOCAL)wParam);
586 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
587 result = (LRESULT)EDIT_EM_GetHandle16(hwnd, es);
590 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
591 result = (LRESULT)EDIT_EM_GetHandle(es);
595 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
598 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
599 result = EDIT_EM_GetThumb(hwnd, es);
602 /* messages 0x00bf and 0x00c0 missing from specs */
605 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
608 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
609 result = DefWindowProcW(hwnd, msg, wParam, lParam);
613 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
616 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
617 result = DefWindowProcW(hwnd, msg, wParam, lParam);
620 case EM_LINELENGTH16:
621 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
624 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
625 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
628 case EM_REPLACESEL16:
629 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
630 lParam = (LPARAM)MapSL(lParam);
631 unicode = FALSE; /* 16-bit message is always ascii */
636 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
639 textW = (LPWSTR)lParam;
642 LPSTR textA = (LPSTR)lParam;
643 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
644 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
645 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
648 EDIT_EM_ReplaceSel(hwnd, es, (BOOL)wParam, textW, TRUE);
652 HeapFree(GetProcessHeap(), 0, textW);
655 /* message 0x00c3 missing from specs */
658 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
661 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
662 result = DefWindowProcW(hwnd, msg, wParam, lParam);
666 DPRINTF_EDIT_MSG16("EM_GETLINE");
667 lParam = (LPARAM)MapSL(lParam);
668 unicode = FALSE; /* 16-bit message is always ascii */
671 DPRINTF_EDIT_MSG32("EM_GETLINE");
672 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
676 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
678 case EM_SETLIMITTEXT:
679 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
680 EDIT_EM_SetLimitText(es, (INT)wParam);
684 DPRINTF_EDIT_MSG16("EM_CANUNDO");
687 DPRINTF_EDIT_MSG32("EM_CANUNDO");
688 result = (LRESULT)EDIT_EM_CanUndo(es);
692 DPRINTF_EDIT_MSG16("EM_UNDO");
697 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
698 result = (LRESULT)EDIT_EM_Undo(hwnd, es);
702 DPRINTF_EDIT_MSG16("EM_FMTLINES");
705 DPRINTF_EDIT_MSG32("EM_FMTLINES");
706 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
709 case EM_LINEFROMCHAR16:
710 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
712 case EM_LINEFROMCHAR:
713 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
714 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
717 /* message 0x00ca missing from specs */
720 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
723 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
724 result = DefWindowProcW(hwnd, msg, wParam, lParam);
727 case EM_SETTABSTOPS16:
728 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
729 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
732 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
733 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
736 case EM_SETPASSWORDCHAR16:
737 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
738 unicode = FALSE; /* 16-bit message is always ascii */
740 case EM_SETPASSWORDCHAR:
743 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
746 charW = (WCHAR)wParam;
750 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
753 EDIT_EM_SetPasswordChar(hwnd, es, charW);
757 case EM_EMPTYUNDOBUFFER16:
758 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
760 case EM_EMPTYUNDOBUFFER:
761 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
762 EDIT_EM_EmptyUndoBuffer(es);
765 case EM_GETFIRSTVISIBLELINE16:
766 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
767 result = es->y_offset;
769 case EM_GETFIRSTVISIBLELINE:
770 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
771 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
774 case EM_SETREADONLY16:
775 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
778 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
780 SetWindowLongA( hwnd, GWL_STYLE,
781 GetWindowLongA( hwnd, GWL_STYLE ) | ES_READONLY );
782 es->style |= ES_READONLY;
784 SetWindowLongA( hwnd, GWL_STYLE,
785 GetWindowLongA( hwnd, GWL_STYLE ) & ~ES_READONLY );
786 es->style &= ~ES_READONLY;
791 case EM_SETWORDBREAKPROC16:
792 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
793 EDIT_EM_SetWordBreakProc16(hwnd, es, (EDITWORDBREAKPROC16)lParam);
795 case EM_SETWORDBREAKPROC:
796 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
797 EDIT_EM_SetWordBreakProc(hwnd, es, lParam);
800 case EM_GETWORDBREAKPROC16:
801 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
802 result = (LRESULT)es->word_break_proc16;
804 case EM_GETWORDBREAKPROC:
805 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
806 result = (LRESULT)es->word_break_proc;
809 case EM_GETPASSWORDCHAR16:
810 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
811 unicode = FALSE; /* 16-bit message is always ascii */
813 case EM_GETPASSWORDCHAR:
815 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
818 result = es->password_char;
821 WCHAR charW = es->password_char;
823 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
829 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
832 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
833 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
837 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
838 result = MAKELONG(es->left_margin, es->right_margin);
841 case EM_GETLIMITTEXT:
842 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
843 result = es->buffer_limit;
847 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
848 result = EDIT_EM_PosFromChar(hwnd, es, (INT)wParam, FALSE);
852 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
853 result = EDIT_EM_CharFromPos(hwnd, es, SLOWORD(lParam), SHIWORD(lParam));
856 /* End of the EM_ messages which were in numerical order; what order
857 * are these in? vaguely alphabetical?
861 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
862 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
864 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
866 int vk = (int)((LPMSG)lParam)->wParam;
868 if (vk == VK_RETURN && (GetWindowLongA( hwnd, GWL_STYLE ) & ES_WANTRETURN))
870 result |= DLGC_WANTMESSAGE;
872 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
874 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
875 result |= DLGC_WANTMESSAGE;
883 DPRINTF_EDIT_MSG32("WM_CHAR");
890 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
893 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
895 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
896 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
899 EDIT_WM_Char(hwnd, es, charW);
904 DPRINTF_EDIT_MSG32("WM_CLEAR");
905 EDIT_WM_Clear(hwnd, es);
909 DPRINTF_EDIT_MSG32("WM_COMMAND");
910 EDIT_WM_Command(hwnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
914 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
915 EDIT_WM_ContextMenu(hwnd, es, SLOWORD(lParam), SHIWORD(lParam));
919 DPRINTF_EDIT_MSG32("WM_COPY");
920 EDIT_WM_Copy(hwnd, es);
924 DPRINTF_EDIT_MSG32("WM_CREATE");
926 result = EDIT_WM_Create(hwnd, es, ((LPCREATESTRUCTW)lParam)->lpszName);
929 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
933 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
934 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
935 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
937 result = EDIT_WM_Create(hwnd, es, nameW);
939 HeapFree(GetProcessHeap(), 0, nameW);
944 DPRINTF_EDIT_MSG32("WM_CUT");
945 EDIT_WM_Cut(hwnd, es);
949 DPRINTF_EDIT_MSG32("WM_ENABLE");
950 es->bEnableState = (BOOL) wParam;
951 EDIT_UpdateText(hwnd, es, NULL, TRUE);
955 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
956 result = EDIT_WM_EraseBkGnd(hwnd, es, (HDC)wParam);
960 DPRINTF_EDIT_MSG32("WM_GETFONT");
961 result = (LRESULT)es->font;
965 DPRINTF_EDIT_MSG32("WM_GETTEXT");
966 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
969 case WM_GETTEXTLENGTH:
970 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
971 result = strlenW(es->text);
975 DPRINTF_EDIT_MSG32("WM_HSCROLL");
976 result = EDIT_WM_HScroll(hwnd, es, LOWORD(wParam), SHIWORD(wParam));
980 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
981 result = EDIT_WM_KeyDown(hwnd, es, (INT)wParam);
985 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
986 result = EDIT_WM_KillFocus(hwnd, es);
989 case WM_LBUTTONDBLCLK:
990 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
991 result = EDIT_WM_LButtonDblClk(hwnd, es);
995 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
996 result = EDIT_WM_LButtonDown(hwnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
1000 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
1001 result = EDIT_WM_LButtonUp(hwnd, es);
1004 case WM_MBUTTONDOWN:
1005 DPRINTF_EDIT_MSG32("WM_MBUTTONDOWN");
1006 result = EDIT_WM_MButtonDown(hwnd);
1009 case WM_MOUSEACTIVATE:
1011 * FIXME: maybe DefWindowProc() screws up, but it seems that
1012 * modeless dialog boxes need this. If we don't do this, the focus
1013 * will _not_ be set by DefWindowProc() for edit controls in a
1014 * modeless dialog box ???
1016 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
1018 result = MA_ACTIVATE;
1023 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
1025 result = EDIT_WM_MouseMove(hwnd, es, SLOWORD(lParam), SHIWORD(lParam));
1029 DPRINTF_EDIT_MSG32("WM_PAINT");
1030 EDIT_WM_Paint(hwnd, es, wParam);
1034 DPRINTF_EDIT_MSG32("WM_PASTE");
1035 EDIT_WM_Paste(hwnd, es);
1039 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
1040 EDIT_WM_SetFocus(hwnd, es);
1044 DPRINTF_EDIT_MSG32("WM_SETFONT");
1045 EDIT_WM_SetFont(hwnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
1049 /* FIXME: actually set an internal flag and behave accordingly */
1053 DPRINTF_EDIT_MSG32("WM_SETTEXT");
1054 EDIT_WM_SetText(hwnd, es, lParam, unicode);
1059 DPRINTF_EDIT_MSG32("WM_SIZE");
1060 EDIT_WM_Size(hwnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
1063 case WM_STYLECHANGED:
1064 DPRINTF_EDIT_MSG32("WM_STYLECHANGED");
1065 result = EDIT_WM_StyleChanged (hwnd, es, wParam, (const STYLESTRUCT *)lParam);
1068 case WM_STYLECHANGING:
1069 DPRINTF_EDIT_MSG32("WM_STYLECHANGING");
1070 result = 0; /* See EDIT_WM_StyleChanged */
1074 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
1075 result = EDIT_WM_SysKeyDown(hwnd, es, (INT)wParam, (DWORD)lParam);
1079 DPRINTF_EDIT_MSG32("WM_TIMER");
1080 EDIT_WM_Timer(hwnd, es);
1084 DPRINTF_EDIT_MSG32("WM_VSCROLL");
1085 result = EDIT_WM_VScroll(hwnd, es, LOWORD(wParam), SHIWORD(wParam));
1090 int gcWheelDelta = 0;
1091 UINT pulScrollLines = 3;
1092 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1094 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1095 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1098 gcWheelDelta -= SHIWORD(wParam);
1099 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1101 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1102 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1103 result = EDIT_EM_LineScroll(hwnd, es, 0, cLineScroll);
1109 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1111 result = DefWindowProcA(hwnd, msg, wParam, lParam);
1114 EDIT_UnlockBuffer(hwnd, es, FALSE);
1119 /*********************************************************************
1121 * EditWndProcW (USER32.@)
1123 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1125 if (!IsWindow( hWnd )) return 0;
1126 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1129 /*********************************************************************
1131 * EditWndProc (USER32.@)
1133 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1135 if (!IsWindow( hWnd )) return 0;
1136 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1139 /*********************************************************************
1141 * EDIT_BuildLineDefs_ML
1143 * Build linked list of text lines.
1144 * Lines can end with '\0' (last line), a character (if it is wrapped),
1145 * a soft return '\r\r\n' or a hard return '\r\n'
1148 static void EDIT_BuildLineDefs_ML(HWND hwnd, EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1152 LPWSTR current_position, cp;
1154 LINEDEF *current_line;
1155 LINEDEF *previous_line;
1156 LINEDEF *start_line;
1157 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1158 INT line_count = es->line_count;
1159 INT orig_net_length;
1162 if (istart == iend && delta == 0)
1167 old_font = SelectObject(dc, es->font);
1169 previous_line = NULL;
1170 current_line = es->first_line_def;
1172 /* Find starting line. istart must lie inside an existing line or
1173 * at the end of buffer */
1175 if (istart < current_line->index + current_line->length ||
1176 current_line->ending == END_0)
1179 previous_line = current_line;
1180 current_line = current_line->next;
1182 } while (current_line);
1184 if (!current_line) /* Error occurred start is not inside previous buffer */
1186 FIXME(" modification occurred outside buffer\n");
1190 /* Remember start of modifications in order to calculate update region */
1191 nstart_line = line_index;
1192 nstart_index = current_line->index;
1194 /* We must start to reformat from the previous line since the modifications
1195 * may have caused the line to wrap upwards. */
1196 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1199 current_line = previous_line;
1201 start_line = current_line;
1203 fw = es->format_rect.right - es->format_rect.left;
1204 current_position = es->text + current_line->index;
1206 if (current_line != start_line)
1208 if (!current_line || current_line->index + delta > current_position - es->text)
1210 /* The buffer has been expanded, create a new line and
1211 insert it into the link list */
1212 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1213 new_line->next = previous_line->next;
1214 previous_line->next = new_line;
1215 current_line = new_line;
1218 else if (current_line->index + delta < current_position - es->text)
1220 /* The previous line merged with this line so we delete this extra entry */
1221 previous_line->next = current_line->next;
1222 HeapFree(GetProcessHeap(), 0, current_line);
1223 current_line = previous_line->next;
1227 else /* current_line->index + delta == current_position */
1229 if (current_position - es->text > iend)
1230 break; /* We reached end of line modifications */
1231 /* else recalulate this line */
1235 current_line->index = current_position - es->text;
1236 orig_net_length = current_line->net_length;
1238 /* Find end of line */
1239 cp = current_position;
1241 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1246 /* Mark type of line termination */
1248 current_line->ending = END_0;
1249 current_line->net_length = strlenW(current_position);
1250 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1251 current_line->ending = END_SOFT;
1252 current_line->net_length = cp - current_position - 1;
1254 current_line->ending = END_HARD;
1255 current_line->net_length = cp - current_position;
1258 /* Calculate line width */
1259 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1260 current_position, current_line->net_length,
1261 es->tabs_count, es->tabs));
1263 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1264 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1269 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1270 prev + 1, current_line->net_length, WB_RIGHT);
1271 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1272 current_position, next, es->tabs_count, es->tabs));
1273 } while (current_line->width <= fw);
1274 if (!prev) { /* Didn't find a line break so force a break */
1279 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1280 current_position, next, es->tabs_count, es->tabs));
1281 } while (current_line->width <= fw);
1286 /* If the first line we are calculating, wrapped before istart, we must
1287 * adjust istart in order for this to be reflected in the update region. */
1288 if (current_line->index == nstart_index && istart > current_line->index + prev)
1289 istart = current_line->index + prev;
1290 /* else if we are updating the previous line before the first line we
1291 * are re-calculating and it expanded */
1292 else if (current_line == start_line &&
1293 current_line->index != nstart_index && orig_net_length < prev)
1295 /* Line expanded due to an upwards line wrap so we must partially include
1296 * previous line in update region */
1297 nstart_line = line_index;
1298 nstart_index = current_line->index;
1299 istart = current_line->index + orig_net_length;
1302 current_line->net_length = prev;
1303 current_line->ending = END_WRAP;
1304 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1305 current_line->net_length, es->tabs_count, es->tabs));
1309 /* Adjust length to include line termination */
1310 switch (current_line->ending) {
1312 current_line->length = current_line->net_length + 3;
1315 current_line->length = current_line->net_length + 2;
1319 current_line->length = current_line->net_length;
1322 es->text_width = max(es->text_width, current_line->width);
1323 current_position += current_line->length;
1324 previous_line = current_line;
1325 current_line = current_line->next;
1327 } while (previous_line->ending != END_0);
1329 /* Finish adjusting line indexes by delta or remove hanging lines */
1330 if (previous_line->ending == END_0)
1332 LINEDEF *pnext = NULL;
1334 previous_line->next = NULL;
1335 while (current_line)
1337 pnext = current_line->next;
1338 HeapFree(GetProcessHeap(), 0, current_line);
1339 current_line = pnext;
1345 while (current_line)
1347 current_line->index += delta;
1348 current_line = current_line->next;
1352 /* Calculate rest of modification rectangle */
1357 * We calculate two rectangles. One for the first line which may have
1358 * an indent with respect to the format rect. The other is a format-width
1359 * rectangle that spans the rest of the lines that changed or moved.
1361 rc.top = es->format_rect.top + nstart_line * es->line_height -
1362 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1363 rc.bottom = rc.top + es->line_height;
1364 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1365 es->text + nstart_index, istart - nstart_index,
1366 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1367 rc.right = es->format_rect.right;
1368 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1371 rc.left = es->format_rect.left;
1372 rc.right = es->format_rect.right;
1374 * If lines were added or removed we must re-paint the remainder of the
1375 * lines since the remaining lines were either shifted up or down.
1377 if (line_count < es->line_count) /* We added lines */
1378 rc.bottom = es->line_count * es->line_height;
1379 else if (line_count > es->line_count) /* We removed lines */
1380 rc.bottom = line_count * es->line_height;
1382 rc.bottom = line_index * es->line_height;
1383 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1384 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1385 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1386 DeleteObject(tmphrgn);
1390 SelectObject(dc, old_font);
1392 ReleaseDC(hwnd, dc);
1395 /*********************************************************************
1397 * EDIT_CalcLineWidth_SL
1400 static void EDIT_CalcLineWidth_SL(HWND hwnd, EDITSTATE *es)
1402 es->text_width = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, strlenW(es->text), FALSE));
1405 /*********************************************************************
1407 * EDIT_CallWordBreakProc
1409 * Call appropriate WordBreakProc (internal or external).
1411 * Note: The "start" argument should always be an index referring
1412 * to es->text. The actual wordbreak proc might be
1413 * 16 bit, so we can't always pass any 32 bit LPSTR.
1414 * Hence we assume that es->text is the buffer that holds
1415 * the string under examination (we can decide this for ourselves).
1418 /* ### start build ### */
1419 extern WORD CALLBACK EDIT_CallTo16_word_lwww(EDITWORDBREAKPROC16,SEGPTR,WORD,WORD,WORD);
1420 /* ### stop build ### */
1421 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1423 INT ret, iWndsLocks;
1425 /* To avoid any deadlocks, all the locks on the window structures
1426 must be suspended before the control is passed to the application */
1427 iWndsLocks = WIN_SuspendWndsLock();
1429 if (es->word_break_proc16) {
1434 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1435 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1436 segptr = K32WOWGlobalLock16(hglob16);
1437 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1438 ret = (INT)EDIT_CallTo16_word_lwww(es->word_break_proc16,
1439 segptr, index, countA, action);
1440 GlobalUnlock16(hglob16);
1441 GlobalFree16(hglob16);
1443 else if (es->word_break_proc)
1447 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1449 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1450 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1451 ret = wbpW(es->text + start, index, count, action);
1455 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1459 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1460 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1461 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1462 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1463 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1464 ret = wbpA(textA, index, countA, action);
1465 HeapFree(GetProcessHeap(), 0, textA);
1469 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1471 WIN_RestoreWndsLock(iWndsLocks);
1476 /*********************************************************************
1480 * Beware: This is not the function called on EM_CHARFROMPOS
1481 * The position _can_ be outside the formatting / client
1483 * The return value is only the character index
1486 static INT EDIT_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1492 if (es->style & ES_MULTILINE) {
1493 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1495 LINEDEF *line_def = es->first_line_def;
1497 while ((line > 0) && line_def->next) {
1498 line_index += line_def->length;
1499 line_def = line_def->next;
1502 x += es->x_offset - es->format_rect.left;
1503 if (x >= line_def->width) {
1505 *after_wrap = (line_def->ending == END_WRAP);
1506 return line_index + line_def->net_length;
1510 *after_wrap = FALSE;
1515 old_font = SelectObject(dc, es->font);
1516 low = line_index + 1;
1517 high = line_index + line_def->net_length + 1;
1518 while (low < high - 1)
1520 INT mid = (low + high) / 2;
1521 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1527 *after_wrap = ((index == line_index + line_def->net_length) &&
1528 (line_def->ending == END_WRAP));
1533 *after_wrap = FALSE;
1534 x -= es->format_rect.left;
1536 return es->x_offset;
1537 text = EDIT_GetPasswordPointer_SL(es);
1540 old_font = SelectObject(dc, es->font);
1544 INT high = es->x_offset;
1545 while (low < high - 1)
1547 INT mid = (low + high) / 2;
1548 GetTextExtentPoint32W( dc, text + mid,
1549 es->x_offset - mid, &size );
1550 if (size.cx > -x) low = mid;
1557 INT low = es->x_offset;
1558 INT high = strlenW(es->text) + 1;
1559 while (low < high - 1)
1561 INT mid = (low + high) / 2;
1562 GetTextExtentPoint32W( dc, text + es->x_offset,
1563 mid - es->x_offset, &size );
1564 if (size.cx > x) high = mid;
1569 if (es->style & ES_PASSWORD)
1570 HeapFree(GetProcessHeap(), 0, text);
1573 SelectObject(dc, old_font);
1574 ReleaseDC(hwnd, dc);
1579 /*********************************************************************
1583 * adjusts the point to be within the formatting rectangle
1584 * (so CharFromPos returns the nearest _visible_ character)
1587 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1589 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1590 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1594 /*********************************************************************
1598 * Calculates the bounding rectangle for a line from a starting
1599 * column to an ending column.
1602 static void EDIT_GetLineRect(HWND hwnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1604 INT line_index = EDIT_EM_LineIndex(es, line);
1606 if (es->style & ES_MULTILINE)
1607 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1609 rc->top = es->format_rect.top;
1610 rc->bottom = rc->top + es->line_height;
1611 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(hwnd, es, line_index + scol, TRUE));
1612 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(hwnd, es, line_index + ecol, TRUE));
1616 /*********************************************************************
1618 * EDIT_GetPasswordPointer_SL
1620 * note: caller should free the (optionally) allocated buffer
1623 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1625 if (es->style & ES_PASSWORD) {
1626 INT len = strlenW(es->text);
1627 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1629 while(len) text[--len] = es->password_char;
1636 /*********************************************************************
1640 * This acts as a LOCAL_Lock(), but it locks only once. This way
1641 * you can call it whenever you like, without unlocking.
1644 static void EDIT_LockBuffer(HWND hwnd, EDITSTATE *es)
1646 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
1648 ERR("no EDITSTATE ... please report\n");
1654 BOOL _16bit = FALSE;
1660 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1661 textA = LocalLock(es->hloc32A);
1662 countA = strlen(textA) + 1;
1666 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1667 textA = LOCAL_Lock(hInstance, es->hloc16);
1668 countA = strlen(textA) + 1;
1673 ERR("no buffer ... please report\n");
1680 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1681 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1682 if(countW_new > es->buffer_size + 1)
1684 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1685 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1686 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1689 es->hloc32W = hloc32W_new;
1690 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1691 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1694 WARN("FAILED! Will synchronize partially\n");
1698 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1699 es->text = LocalLock(es->hloc32W);
1703 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1705 LOCAL_Unlock(hInstance, es->hloc16);
1707 LocalUnlock(es->hloc32A);
1714 /*********************************************************************
1716 * EDIT_SL_InvalidateText
1718 * Called from EDIT_InvalidateText().
1719 * Does the job for single-line controls only.
1722 static void EDIT_SL_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end)
1727 EDIT_GetLineRect(hwnd, es, 0, start, end, &line_rect);
1728 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1729 EDIT_UpdateText(hwnd, es, &rc, FALSE);
1733 /*********************************************************************
1735 * EDIT_ML_InvalidateText
1737 * Called from EDIT_InvalidateText().
1738 * Does the job for multi-line controls only.
1741 static void EDIT_ML_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end)
1743 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1744 INT sl = EDIT_EM_LineFromChar(es, start);
1745 INT el = EDIT_EM_LineFromChar(es, end);
1754 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1757 sc = start - EDIT_EM_LineIndex(es, sl);
1758 ec = end - EDIT_EM_LineIndex(es, el);
1759 if (sl < es->y_offset) {
1763 if (el > es->y_offset + vlc) {
1764 el = es->y_offset + vlc;
1765 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1767 GetClientRect(hwnd, &rc1);
1768 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1770 EDIT_GetLineRect(hwnd, es, sl, sc, ec, &rcLine);
1771 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1772 EDIT_UpdateText(hwnd, es, &rcUpdate, FALSE);
1774 EDIT_GetLineRect(hwnd, es, sl, sc,
1775 EDIT_EM_LineLength(es,
1776 EDIT_EM_LineIndex(es, sl)),
1778 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1779 EDIT_UpdateText(hwnd, es, &rcUpdate, FALSE);
1780 for (l = sl + 1 ; l < el ; l++) {
1781 EDIT_GetLineRect(hwnd, es, l, 0,
1782 EDIT_EM_LineLength(es,
1783 EDIT_EM_LineIndex(es, l)),
1785 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1786 EDIT_UpdateText(hwnd, es, &rcUpdate, FALSE);
1788 EDIT_GetLineRect(hwnd, es, el, 0, ec, &rcLine);
1789 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1790 EDIT_UpdateText(hwnd, es, &rcUpdate, FALSE);
1795 /*********************************************************************
1797 * EDIT_InvalidateText
1799 * Invalidate the text from offset start upto, but not including,
1800 * offset end. Useful for (re)painting the selection.
1801 * Regions outside the linewidth are not invalidated.
1802 * end == -1 means end == TextLength.
1803 * start and end need not be ordered.
1806 static void EDIT_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end)
1812 end = strlenW(es->text);
1814 ORDER_INT(start, end);
1816 if (es->style & ES_MULTILINE)
1817 EDIT_ML_InvalidateText(hwnd, es, start, end);
1819 EDIT_SL_InvalidateText(hwnd, es, start, end);
1823 /*********************************************************************
1827 * Try to fit size + 1 characters in the buffer. Constrain to limits.
1830 static BOOL EDIT_MakeFit(HWND hwnd, EDITSTATE *es, UINT size)
1834 if (size <= es->buffer_size)
1836 if (size > es->buffer_limit) {
1837 EDIT_NOTIFY_PARENT(hwnd, es, EN_MAXTEXT, "EN_MAXTEXT");
1840 if (size > es->buffer_limit)
1841 size = es->buffer_limit;
1843 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1845 /* Force edit to unlock it's buffer. es->text now NULL */
1846 EDIT_UnlockBuffer(hwnd, es, TRUE);
1849 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1850 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1851 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32W, hNew32W);
1852 es->hloc32W = hNew32W;
1853 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1857 EDIT_LockBuffer(hwnd, es);
1859 if (es->buffer_size < size) {
1860 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1861 EDIT_NOTIFY_PARENT(hwnd, es, EN_ERRSPACE, "EN_ERRSPACE");
1864 TRACE("We now have %d+1\n", es->buffer_size);
1870 /*********************************************************************
1874 * Try to fit size + 1 bytes in the undo buffer.
1877 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1881 if (size <= es->undo_buffer_size)
1884 TRACE("trying to ReAlloc to %d+1\n", size);
1886 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1887 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1888 es->undo_buffer_size = alloc_size/sizeof(WCHAR);
1893 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1899 /*********************************************************************
1904 static void EDIT_MoveBackward(HWND hwnd, EDITSTATE *es, BOOL extend)
1906 INT e = es->selection_end;
1910 if ((es->style & ES_MULTILINE) && e &&
1911 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1913 if (e && (es->text[e - 1] == '\r'))
1917 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, FALSE);
1918 EDIT_EM_ScrollCaret(hwnd, es);
1922 /*********************************************************************
1926 * Only for multi line controls
1927 * Move the caret one line down, on a column with the nearest
1928 * x coordinate on the screen (might be a different column).
1931 static void EDIT_MoveDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
1933 INT s = es->selection_start;
1934 INT e = es->selection_end;
1935 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1936 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
1937 INT x = SLOWORD(pos);
1938 INT y = SHIWORD(pos);
1940 e = EDIT_CharFromPos(hwnd, es, x, y + es->line_height, &after_wrap);
1943 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
1944 EDIT_EM_ScrollCaret(hwnd, es);
1948 /*********************************************************************
1953 static void EDIT_MoveEnd(HWND hwnd, EDITSTATE *es, BOOL extend)
1955 BOOL after_wrap = FALSE;
1958 /* Pass a high value in x to make sure of receiving the end of the line */
1959 if (es->style & ES_MULTILINE)
1960 e = EDIT_CharFromPos(hwnd, es, 0x3fffffff,
1961 HIWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1963 e = strlenW(es->text);
1964 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, after_wrap);
1965 EDIT_EM_ScrollCaret(hwnd, es);
1969 /*********************************************************************
1974 static void EDIT_MoveForward(HWND hwnd, EDITSTATE *es, BOOL extend)
1976 INT e = es->selection_end;
1980 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1981 if (es->text[e] == '\n')
1983 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1987 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, FALSE);
1988 EDIT_EM_ScrollCaret(hwnd, es);
1992 /*********************************************************************
1996 * Home key: move to beginning of line.
1999 static void EDIT_MoveHome(HWND hwnd, EDITSTATE *es, BOOL extend)
2003 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2004 if (es->style & ES_MULTILINE)
2005 e = EDIT_CharFromPos(hwnd, es, -es->x_offset,
2006 HIWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2009 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, FALSE);
2010 EDIT_EM_ScrollCaret(hwnd, es);
2014 /*********************************************************************
2016 * EDIT_MovePageDown_ML
2018 * Only for multi line controls
2019 * Move the caret one page down, on a column with the nearest
2020 * x coordinate on the screen (might be a different column).
2023 static void EDIT_MovePageDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
2025 INT s = es->selection_start;
2026 INT e = es->selection_end;
2027 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2028 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
2029 INT x = SLOWORD(pos);
2030 INT y = SHIWORD(pos);
2032 e = EDIT_CharFromPos(hwnd, es, x,
2033 y + (es->format_rect.bottom - es->format_rect.top),
2037 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
2038 EDIT_EM_ScrollCaret(hwnd, es);
2042 /*********************************************************************
2044 * EDIT_MovePageUp_ML
2046 * Only for multi line controls
2047 * Move the caret one page up, on a column with the nearest
2048 * x coordinate on the screen (might be a different column).
2051 static void EDIT_MovePageUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
2053 INT s = es->selection_start;
2054 INT e = es->selection_end;
2055 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2056 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
2057 INT x = SLOWORD(pos);
2058 INT y = SHIWORD(pos);
2060 e = EDIT_CharFromPos(hwnd, es, x,
2061 y - (es->format_rect.bottom - es->format_rect.top),
2065 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
2066 EDIT_EM_ScrollCaret(hwnd, es);
2070 /*********************************************************************
2074 * Only for multi line controls
2075 * Move the caret one line up, on a column with the nearest
2076 * x coordinate on the screen (might be a different column).
2079 static void EDIT_MoveUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
2081 INT s = es->selection_start;
2082 INT e = es->selection_end;
2083 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2084 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
2085 INT x = SLOWORD(pos);
2086 INT y = SHIWORD(pos);
2088 e = EDIT_CharFromPos(hwnd, es, x, y - es->line_height, &after_wrap);
2091 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
2092 EDIT_EM_ScrollCaret(hwnd, es);
2096 /*********************************************************************
2098 * EDIT_MoveWordBackward
2101 static void EDIT_MoveWordBackward(HWND hwnd, EDITSTATE *es, BOOL extend)
2103 INT s = es->selection_start;
2104 INT e = es->selection_end;
2109 l = EDIT_EM_LineFromChar(es, e);
2110 ll = EDIT_EM_LineLength(es, e);
2111 li = EDIT_EM_LineIndex(es, l);
2114 li = EDIT_EM_LineIndex(es, l - 1);
2115 e = li + EDIT_EM_LineLength(es, li);
2118 e = li + (INT)EDIT_CallWordBreakProc(es,
2119 li, e - li, ll, WB_LEFT);
2123 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
2124 EDIT_EM_ScrollCaret(hwnd, es);
2128 /*********************************************************************
2130 * EDIT_MoveWordForward
2133 static void EDIT_MoveWordForward(HWND hwnd, EDITSTATE *es, BOOL extend)
2135 INT s = es->selection_start;
2136 INT e = es->selection_end;
2141 l = EDIT_EM_LineFromChar(es, e);
2142 ll = EDIT_EM_LineLength(es, e);
2143 li = EDIT_EM_LineIndex(es, l);
2145 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2146 e = EDIT_EM_LineIndex(es, l + 1);
2148 e = li + EDIT_CallWordBreakProc(es,
2149 li, e - li + 1, ll, WB_RIGHT);
2153 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
2154 EDIT_EM_ScrollCaret(hwnd, es);
2158 /*********************************************************************
2163 static void EDIT_PaintLine(HWND hwnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
2165 INT s = es->selection_start;
2166 INT e = es->selection_end;
2173 if (es->style & ES_MULTILINE) {
2174 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2175 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2180 TRACE("line=%d\n", line);
2182 pos = EDIT_EM_PosFromChar(hwnd, es, EDIT_EM_LineIndex(es, line), FALSE);
2185 li = EDIT_EM_LineIndex(es, line);
2186 ll = EDIT_EM_LineLength(es, li);
2187 s = es->selection_start;
2188 e = es->selection_end;
2190 s = min(li + ll, max(li, s));
2191 e = min(li + ll, max(li, e));
2192 if (rev && (s != e) &&
2193 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2194 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2195 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2196 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2198 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2202 /*********************************************************************
2207 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2218 BkMode = GetBkMode(dc);
2219 BkColor = GetBkColor(dc);
2220 TextColor = GetTextColor(dc);
2222 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2223 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2224 SetBkMode( dc, OPAQUE);
2226 li = EDIT_EM_LineIndex(es, line);
2227 if (es->style & ES_MULTILINE) {
2228 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2229 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2231 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2232 TextOutW(dc, x, y, text + li + col, count);
2233 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2235 if (es->style & ES_PASSWORD)
2236 HeapFree(GetProcessHeap(), 0, text);
2239 SetBkColor(dc, BkColor);
2240 SetTextColor(dc, TextColor);
2241 SetBkMode( dc, BkMode);
2247 /*********************************************************************
2252 static void EDIT_SetCaretPos(HWND hwnd, EDITSTATE *es, INT pos,
2255 LRESULT res = EDIT_EM_PosFromChar(hwnd, es, pos, after_wrap);
2256 SetCaretPos(SLOWORD(res), SHIWORD(res));
2260 /*********************************************************************
2264 * note: this is not (exactly) the handler called on EM_SETRECTNP
2265 * it is also used to set the rect of a single line control
2268 static void EDIT_SetRectNP(HWND hwnd, EDITSTATE *es, LPRECT rc)
2270 CopyRect(&es->format_rect, rc);
2271 if (es->style & WS_BORDER) {
2272 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2273 if(TWEAK_WineLook == WIN31_LOOK)
2275 es->format_rect.left += bw;
2276 es->format_rect.top += bw;
2277 es->format_rect.right -= bw;
2278 es->format_rect.bottom -= bw;
2280 es->format_rect.left += es->left_margin;
2281 es->format_rect.right -= es->right_margin;
2282 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2283 if (es->style & ES_MULTILINE)
2285 INT fw, vlc, max_x_offset, max_y_offset;
2287 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2288 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2290 /* correct es->x_offset */
2291 fw = es->format_rect.right - es->format_rect.left;
2292 max_x_offset = es->text_width - fw;
2293 if(max_x_offset < 0) max_x_offset = 0;
2294 if(es->x_offset > max_x_offset)
2295 es->x_offset = max_x_offset;
2297 /* correct es->y_offset */
2298 max_y_offset = es->line_count - vlc;
2299 if(max_y_offset < 0) max_y_offset = 0;
2300 if(es->y_offset > max_y_offset)
2301 es->y_offset = max_y_offset;
2303 /* force scroll info update */
2304 EDIT_UpdateScrollInfo(hwnd, es);
2307 /* Windows doesn't care to fix text placement for SL controls */
2308 es->format_rect.bottom = es->format_rect.top + es->line_height;
2310 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2311 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
2315 /*********************************************************************
2320 static void EDIT_UnlockBuffer(HWND hwnd, EDITSTATE *es, BOOL force)
2322 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
2324 /* Edit window might be already destroyed */
2327 WARN("edit hwnd %04x already destroyed\n", hwnd);
2332 ERR("no EDITSTATE ... please report\n");
2335 if (!es->lock_count) {
2336 ERR("lock_count == 0 ... please report\n");
2340 ERR("es->text == 0 ... please report\n");
2344 if (force || (es->lock_count == 1)) {
2347 BOOL _16bit = FALSE;
2349 UINT countW = strlenW(es->text) + 1;
2353 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2354 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2355 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2356 countA = LocalSize(es->hloc32A);
2357 if(countA_new > countA)
2360 UINT alloc_size = ROUND_TO_GROW(countA_new);
2361 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2362 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2365 es->hloc32A = hloc32A_new;
2366 countA = LocalSize(hloc32A_new);
2367 TRACE("Real new size %d bytes\n", countA);
2370 WARN("FAILED! Will synchronize partially\n");
2372 textA = LocalLock(es->hloc32A);
2376 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2377 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2378 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2379 countA = LOCAL_Size(hInstance, es->hloc16);
2380 if(countA_new > countA)
2382 HLOCAL16 hloc16_new;
2383 UINT alloc_size = ROUND_TO_GROW(countA_new);
2384 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2385 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2388 es->hloc16 = hloc16_new;
2389 countA = LOCAL_Size(hInstance, hloc16_new);
2390 TRACE("Real new size %d bytes\n", countA);
2393 WARN("FAILED! Will synchronize partially\n");
2395 textA = LOCAL_Lock(hInstance, es->hloc16);
2401 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2403 LOCAL_Unlock(hInstance, es->hloc16);
2405 LocalUnlock(es->hloc32A);
2408 LocalUnlock(es->hloc32W);
2412 ERR("no buffer ... please report\n");
2420 /*********************************************************************
2422 * EDIT_UpdateScrollInfo
2425 static void EDIT_UpdateScrollInfo(HWND hwnd, EDITSTATE *es)
2427 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2430 si.cbSize = sizeof(SCROLLINFO);
2431 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2433 si.nMax = es->line_count - 1;
2434 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2435 si.nPos = es->y_offset;
2436 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2437 si.nMin, si.nMax, si.nPage, si.nPos);
2438 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2441 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2444 si.cbSize = sizeof(SCROLLINFO);
2445 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2447 si.nMax = es->text_width - 1;
2448 si.nPage = es->format_rect.right - es->format_rect.left;
2449 si.nPos = es->x_offset;
2450 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2451 si.nMin, si.nMax, si.nPage, si.nPos);
2452 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2456 /*********************************************************************
2458 * EDIT_WordBreakProc
2460 * Find the beginning of words.
2461 * Note: unlike the specs for a WordBreakProc, this function only
2462 * allows to be called without linebreaks between s[0] upto
2463 * s[count - 1]. Remember it is only called
2464 * internally, so we can decide this for ourselves.
2467 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2471 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2481 if (s[index] == ' ') {
2482 while (index && (s[index] == ' '))
2485 while (index && (s[index] != ' '))
2487 if (s[index] == ' ')
2491 while (index && (s[index] != ' '))
2493 if (s[index] == ' ')
2503 if (s[index] == ' ')
2504 while ((index < count) && (s[index] == ' ')) index++;
2506 while (s[index] && (s[index] != ' ') && (index < count))
2508 while ((s[index] == ' ') && (index < count)) index++;
2512 case WB_ISDELIMITER:
2513 ret = (s[index] == ' ');
2516 ERR("unknown action code, please report !\n");
2523 /*********************************************************************
2527 * returns line number (not index) in high-order word of result.
2528 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2529 * to Richedit, not to the edit control. Original documentation is valid.
2530 * FIXME: do the specs mean to return -1 if outside client area or
2531 * if outside formatting rectangle ???
2534 static LRESULT EDIT_EM_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y)
2542 GetClientRect(hwnd, &rc);
2543 if (!PtInRect(&rc, pt))
2546 index = EDIT_CharFromPos(hwnd, es, x, y, NULL);
2547 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2551 /*********************************************************************
2555 * Enable or disable soft breaks.
2557 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2559 es->flags &= ~EF_USE_SOFTBRK;
2561 es->flags |= EF_USE_SOFTBRK;
2562 FIXME("soft break enabled, not implemented\n");
2568 /*********************************************************************
2572 * Hopefully this won't fire back at us.
2573 * We always start with a fixed buffer in the local heap.
2574 * Despite of the documentation says that the local heap is used
2575 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2576 * buffer on the local heap.
2579 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2583 if (!(es->style & ES_MULTILINE))
2587 hLocal = es->hloc32W;
2593 UINT countA, alloc_size;
2594 TRACE("Allocating 32-bit ANSI alias buffer\n");
2595 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2596 alloc_size = ROUND_TO_GROW(countA);
2597 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2599 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2602 textA = LocalLock(es->hloc32A);
2603 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2604 LocalUnlock(es->hloc32A);
2606 hLocal = es->hloc32A;
2609 TRACE("Returning %04X, LocalSize() = %d\n", hLocal, LocalSize(hLocal));
2614 /*********************************************************************
2618 * Hopefully this won't fire back at us.
2619 * We always start with a buffer in 32 bit linear memory.
2620 * However, with this message a 16 bit application requests
2621 * a handle of 16 bit local heap memory, where it expects to find
2623 * It's a pitty that from this moment on we have to use this
2624 * local heap, because applications may rely on the handle
2627 * In this function we'll try to switch to local heap.
2629 static HLOCAL16 EDIT_EM_GetHandle16(HWND hwnd, EDITSTATE *es)
2631 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
2633 UINT countA, alloc_size;
2635 if (!(es->style & ES_MULTILINE))
2641 if (!LOCAL_HeapSize(hInstance)) {
2642 if (!LocalInit16(hInstance, 0,
2643 GlobalSize16(hInstance))) {
2644 ERR("could not initialize local heap\n");
2647 TRACE("local heap initialized\n");
2650 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2651 alloc_size = ROUND_TO_GROW(countA);
2653 TRACE("Allocating 16-bit ANSI alias buffer\n");
2654 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2655 ERR("could not allocate new 16 bit buffer\n");
2659 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2660 ERR("could not lock new 16 bit buffer\n");
2661 LOCAL_Free(hInstance, es->hloc16);
2666 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2667 LOCAL_Unlock(hInstance, es->hloc16);
2669 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2674 /*********************************************************************
2679 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2682 INT line_len, dst_len;
2685 if (es->style & ES_MULTILINE) {
2686 if (line >= es->line_count)
2690 i = EDIT_EM_LineIndex(es, line);
2692 line_len = EDIT_EM_LineLength(es, i);
2693 dst_len = *(WORD *)lParam;
2696 LPWSTR dst = (LPWSTR)lParam;
2697 if(dst_len <= line_len)
2699 memcpy(dst, src, dst_len * sizeof(WCHAR));
2702 else /* Append 0 if enough space */
2704 memcpy(dst, src, line_len * sizeof(WCHAR));
2711 LPSTR dst = (LPSTR)lParam;
2713 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2714 if(!ret) /* Insufficient buffer size */
2716 if(ret < dst_len) /* Append 0 if enough space */
2723 /*********************************************************************
2728 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end)
2730 UINT s = es->selection_start;
2731 UINT e = es->selection_end;
2738 return MAKELONG(s, e);
2742 /*********************************************************************
2746 * FIXME: is this right ? (or should it be only VSCROLL)
2747 * (and maybe only for edit controls that really have their
2748 * own scrollbars) (and maybe only for multiline controls ?)
2749 * All in all: very poorly documented
2752 static LRESULT EDIT_EM_GetThumb(HWND hwnd, EDITSTATE *es)
2754 return MAKELONG(EDIT_WM_VScroll(hwnd, es, EM_GETTHUMB16, 0),
2755 EDIT_WM_HScroll(hwnd, es, EM_GETTHUMB16, 0));
2759 /*********************************************************************
2764 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2769 if (!(es->style & ES_MULTILINE))
2771 if (index > (INT)strlenW(es->text))
2772 return es->line_count - 1;
2774 index = min(es->selection_start, es->selection_end);
2777 line_def = es->first_line_def;
2778 index -= line_def->length;
2779 while ((index >= 0) && line_def->next) {
2781 line_def = line_def->next;
2782 index -= line_def->length;
2788 /*********************************************************************
2793 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2798 if (!(es->style & ES_MULTILINE))
2800 if (line >= es->line_count)
2804 line_def = es->first_line_def;
2806 INT index = es->selection_end - line_def->length;
2807 while ((index >= 0) && line_def->next) {
2808 line_index += line_def->length;
2809 line_def = line_def->next;
2810 index -= line_def->length;
2814 line_index += line_def->length;
2815 line_def = line_def->next;
2823 /*********************************************************************
2828 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2832 if (!(es->style & ES_MULTILINE))
2833 return strlenW(es->text);
2836 /* get the number of remaining non-selected chars of selected lines */
2837 INT32 l; /* line number */
2838 INT32 li; /* index of first char in line */
2840 l = EDIT_EM_LineFromChar(es, es->selection_start);
2841 /* # chars before start of selection area */
2842 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2843 l = EDIT_EM_LineFromChar(es, es->selection_end);
2844 /* # chars after end of selection */
2845 li = EDIT_EM_LineIndex(es, l);
2846 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2849 line_def = es->first_line_def;
2850 index -= line_def->length;
2851 while ((index >= 0) && line_def->next) {
2852 line_def = line_def->next;
2853 index -= line_def->length;
2855 return line_def->net_length;
2859 /*********************************************************************
2863 * NOTE: dx is in average character widths, dy - in lines;
2866 static BOOL EDIT_EM_LineScroll(HWND hwnd, EDITSTATE *es, INT dx, INT dy)
2868 if (!(es->style & ES_MULTILINE))
2871 dx *= es->char_width;
2872 return EDIT_EM_LineScroll_internal(hwnd, es, dx, dy);
2875 /*********************************************************************
2877 * EDIT_EM_LineScroll_internal
2879 * Version of EDIT_EM_LineScroll for internal use.
2880 * It doesn't refuse if ES_MULTILINE is set and assumes that
2881 * dx is in pixels, dy - in lines.
2884 static BOOL EDIT_EM_LineScroll_internal(HWND hwnd, EDITSTATE *es, INT dx, INT dy)
2887 INT x_offset_in_pixels;
2889 if (es->style & ES_MULTILINE)
2891 x_offset_in_pixels = es->x_offset;
2896 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->x_offset, FALSE));
2899 if (-dx > x_offset_in_pixels)
2900 dx = -x_offset_in_pixels;
2901 if (dx > es->text_width - x_offset_in_pixels)
2902 dx = es->text_width - x_offset_in_pixels;
2903 nyoff = max(0, es->y_offset + dy);
2904 if (nyoff >= es->line_count)
2905 nyoff = es->line_count - 1;
2906 dy = (es->y_offset - nyoff) * es->line_height;
2911 es->y_offset = nyoff;
2912 if(es->style & ES_MULTILINE)
2915 es->x_offset += dx / es->char_width;
2917 GetClientRect(hwnd, &rc1);
2918 IntersectRect(&rc, &rc1, &es->format_rect);
2919 ScrollWindowEx(hwnd, -dx, dy,
2920 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2921 /* force scroll info update */
2922 EDIT_UpdateScrollInfo(hwnd, es);
2924 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2925 EDIT_NOTIFY_PARENT(hwnd, es, EN_HSCROLL, "EN_HSCROLL");
2926 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2927 EDIT_NOTIFY_PARENT(hwnd, es, EN_VSCROLL, "EN_VSCROLL");
2932 /*********************************************************************
2937 static LRESULT EDIT_EM_PosFromChar(HWND hwnd, EDITSTATE *es, INT index, BOOL after_wrap)
2939 INT len = strlenW(es->text);
2948 index = min(index, len);
2951 old_font = SelectObject(dc, es->font);
2952 if (es->style & ES_MULTILINE) {
2953 l = EDIT_EM_LineFromChar(es, index);
2954 y = (l - es->y_offset) * es->line_height;
2955 li = EDIT_EM_LineIndex(es, l);
2956 if (after_wrap && (li == index) && l) {
2958 LINEDEF *line_def = es->first_line_def;
2960 line_def = line_def->next;
2963 if (line_def->ending == END_WRAP) {
2965 y -= es->line_height;
2966 li = EDIT_EM_LineIndex(es, l);
2969 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2970 es->tabs_count, es->tabs)) - es->x_offset;
2972 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2973 if (index < es->x_offset) {
2974 GetTextExtentPoint32W(dc, text + index,
2975 es->x_offset - index, &size);
2978 GetTextExtentPoint32W(dc, text + es->x_offset,
2979 index - es->x_offset, &size);
2983 if (es->style & ES_PASSWORD)
2984 HeapFree(GetProcessHeap(), 0, text);
2986 x += es->format_rect.left;
2987 y += es->format_rect.top;
2989 SelectObject(dc, old_font);
2990 ReleaseDC(hwnd, dc);
2991 return MAKELONG((INT16)x, (INT16)y);
2995 /*********************************************************************
2999 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3002 static void EDIT_EM_ReplaceSel(HWND hwnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
3004 UINT strl = strlenW(lpsz_replace);
3005 UINT tl = strlenW(es->text);
3013 TRACE("%s, can_undo %d, send_update %d\n",
3014 debugstr_w(lpsz_replace), can_undo, send_update);
3016 s = es->selection_start;
3017 e = es->selection_end;
3019 if ((s == e) && !strl)
3024 if (!EDIT_MakeFit(hwnd, es, tl - (e - s) + strl))
3028 /* there is something to be deleted */
3029 TRACE("deleting stuff.\n");
3031 utl = strlenW(es->undo_text);
3032 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3033 /* undo-buffer is extended to the right */
3034 EDIT_MakeUndoFit(es, utl + e - s);
3035 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
3036 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3037 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3038 /* undo-buffer is extended to the left */
3039 EDIT_MakeUndoFit(es, utl + e - s);
3040 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3042 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3043 p[i] = (es->text + s)[i];
3044 es->undo_position = s;
3046 /* new undo-buffer */
3047 EDIT_MakeUndoFit(es, e - s);
3048 strncpyW(es->undo_text, es->text + s, e - s + 1);
3049 es->undo_text[e - s] = 0; /* ensure 0 termination */
3050 es->undo_position = s;
3052 /* any deletion makes the old insertion-undo invalid */
3053 es->undo_insert_count = 0;
3055 EDIT_EM_EmptyUndoBuffer(es);
3058 strcpyW(es->text + s, es->text + e);
3061 /* there is an insertion */
3063 if ((s == es->undo_position) ||
3064 ((es->undo_insert_count) &&
3065 (s == es->undo_position + es->undo_insert_count)))
3067 * insertion is new and at delete position or
3068 * an extension to either left or right
3070 es->undo_insert_count += strl;
3072 /* new insertion undo */
3073 es->undo_position = s;
3074 es->undo_insert_count = strl;
3075 /* new insertion makes old delete-buffer invalid */
3076 *es->undo_text = '\0';
3079 EDIT_EM_EmptyUndoBuffer(es);
3082 tl = strlenW(es->text);
3083 TRACE("inserting stuff (tl %d, strl %d, selstart %d ('%s'), text '%s')\n", tl, strl, s, debugstr_w(es->text + s), debugstr_w(es->text));
3084 for (p = es->text + tl ; p >= es->text + s ; p--)
3086 for (i = 0 , p = es->text + s ; i < strl ; i++)
3087 p[i] = lpsz_replace[i];
3088 if(es->style & ES_UPPERCASE)
3089 CharUpperBuffW(p, strl);
3090 else if(es->style & ES_LOWERCASE)
3091 CharLowerBuffW(p, strl);
3094 if (es->style & ES_MULTILINE)
3096 INT s = min(es->selection_start, es->selection_end);
3098 hrgn = CreateRectRgn(0, 0, 0, 0);
3099 EDIT_BuildLineDefs_ML(hwnd, es, s, s + strl,
3100 strl - abs(es->selection_end - es->selection_start), hrgn);
3103 EDIT_CalcLineWidth_SL(hwnd, es);
3105 EDIT_EM_SetSel(hwnd, es, s, s, FALSE);
3106 es->flags |= EF_MODIFIED;
3107 if (send_update) es->flags |= EF_UPDATE;
3108 EDIT_EM_ScrollCaret(hwnd, es);
3110 /* force scroll info update */
3111 EDIT_UpdateScrollInfo(hwnd, es);
3115 EDIT_UpdateTextRegion(hwnd, es, hrgn, TRUE);
3119 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3121 if(es->flags & EF_UPDATE)
3123 es->flags &= ~EF_UPDATE;
3124 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3129 /*********************************************************************
3134 static LRESULT EDIT_EM_Scroll(HWND hwnd, EDITSTATE *es, INT action)
3138 if (!(es->style & ES_MULTILINE))
3139 return (LRESULT)FALSE;
3149 if (es->y_offset < es->line_count - 1)
3154 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3157 if (es->y_offset < es->line_count - 1)
3158 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3161 return (LRESULT)FALSE;
3164 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3165 /* check if we are going to move too far */
3166 if(es->y_offset + dy > es->line_count - vlc)
3167 dy = es->line_count - vlc - es->y_offset;
3169 /* Notification is done in EDIT_EM_LineScroll */
3171 EDIT_EM_LineScroll(hwnd, es, 0, dy);
3173 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3177 /*********************************************************************
3182 static void EDIT_EM_ScrollCaret(HWND hwnd, EDITSTATE *es)
3184 if (es->style & ES_MULTILINE) {
3189 INT cw = es->char_width;
3194 l = EDIT_EM_LineFromChar(es, es->selection_end);
3195 li = EDIT_EM_LineIndex(es, l);
3196 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
3197 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3198 if (l >= es->y_offset + vlc)
3199 dy = l - vlc + 1 - es->y_offset;
3200 if (l < es->y_offset)
3201 dy = l - es->y_offset;
3202 ww = es->format_rect.right - es->format_rect.left;
3203 if (x < es->format_rect.left)
3204 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3205 if (x > es->format_rect.right)
3206 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3209 /* check if we are going to move too far */
3210 if(es->x_offset + dx + ww > es->text_width)
3211 dx = es->text_width - ww - es->x_offset;
3213 EDIT_EM_LineScroll_internal(hwnd, es, dx, dy);
3220 if (!(es->style & ES_AUTOHSCROLL))
3223 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3224 format_width = es->format_rect.right - es->format_rect.left;
3225 if (x < es->format_rect.left) {
3226 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3229 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3230 } while ((x < goal) && es->x_offset);
3231 /* FIXME: use ScrollWindow() somehow to improve performance */
3232 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3233 } else if (x > es->format_rect.right) {
3235 INT len = strlenW(es->text);
3236 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3239 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3240 x_last = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, len, FALSE));
3241 } while ((x > goal) && (x_last > es->format_rect.right));
3242 /* FIXME: use ScrollWindow() somehow to improve performance */
3243 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3247 if(es->flags & EF_FOCUSED)
3248 EDIT_SetCaretPos(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
3252 /*********************************************************************
3256 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3259 static void EDIT_EM_SetHandle(HWND hwnd, EDITSTATE *es, HLOCAL hloc)
3261 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3263 if (!(es->style & ES_MULTILINE))
3267 WARN("called with NULL handle\n");
3271 EDIT_UnlockBuffer(hwnd, es, TRUE);
3275 LOCAL_Free(hInstance, es->hloc16);
3276 es->hloc16 = (HLOCAL16)NULL;
3283 LocalFree(es->hloc32A);
3284 es->hloc32A = (HLOCAL)NULL;
3295 countA = LocalSize(hloc);
3296 textA = LocalLock(hloc);
3297 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3298 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3300 ERR("Could not allocate new unicode buffer\n");
3303 textW = LocalLock(hloc32W_new);
3304 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3305 LocalUnlock(hloc32W_new);
3309 LocalFree(es->hloc32W);
3311 es->hloc32W = hloc32W_new;
3315 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3317 EDIT_LockBuffer(hwnd, es);
3319 es->x_offset = es->y_offset = 0;
3320 es->selection_start = es->selection_end = 0;
3321 EDIT_EM_EmptyUndoBuffer(es);
3322 es->flags &= ~EF_MODIFIED;
3323 es->flags &= ~EF_UPDATE;
3324 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3325 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3326 EDIT_EM_ScrollCaret(hwnd, es);
3327 /* force scroll info update */
3328 EDIT_UpdateScrollInfo(hwnd, es);
3332 /*********************************************************************
3336 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3339 static void EDIT_EM_SetHandle16(HWND hwnd, EDITSTATE *es, HLOCAL16 hloc)
3341 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3347 if (!(es->style & ES_MULTILINE))
3351 WARN("called with NULL handle\n");
3355 EDIT_UnlockBuffer(hwnd, es, TRUE);
3359 LocalFree(es->hloc32A);
3360 es->hloc32A = (HLOCAL)NULL;
3363 countA = LOCAL_Size(hInstance, hloc);
3364 textA = LOCAL_Lock(hInstance, hloc);
3365 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3366 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3368 ERR("Could not allocate new unicode buffer\n");
3371 textW = LocalLock(hloc32W_new);
3372 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3373 LocalUnlock(hloc32W_new);
3374 LOCAL_Unlock(hInstance, hloc);
3377 LocalFree(es->hloc32W);
3379 es->hloc32W = hloc32W_new;
3382 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3384 EDIT_LockBuffer(hwnd, es);
3386 es->x_offset = es->y_offset = 0;
3387 es->selection_start = es->selection_end = 0;
3388 EDIT_EM_EmptyUndoBuffer(es);
3389 es->flags &= ~EF_MODIFIED;
3390 es->flags &= ~EF_UPDATE;
3391 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3392 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3393 EDIT_EM_ScrollCaret(hwnd, es);
3394 /* force scroll info update */
3395 EDIT_UpdateScrollInfo(hwnd, es);
3399 /*********************************************************************
3403 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3404 * However, the windows version is not complied to yet in all of edit.c
3407 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3409 if (es->style & ES_MULTILINE) {
3411 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3413 es->buffer_limit = BUFLIMIT_MULTI;
3416 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3418 es->buffer_limit = BUFLIMIT_SINGLE;
3423 /*********************************************************************
3427 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3428 * action wParam despite what the docs say. EC_USEFONTINFO means one third
3429 * of the char's width, according to the new docs.
3432 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3433 INT left, INT right)
3435 if (action & EC_LEFTMARGIN) {
3436 if (left != EC_USEFONTINFO)
3437 es->left_margin = left;
3439 es->left_margin = es->char_width / 3;
3442 if (action & EC_RIGHTMARGIN) {
3443 if (right != EC_USEFONTINFO)
3444 es->right_margin = right;
3446 es->right_margin = es->char_width / 3;
3448 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3452 /*********************************************************************
3454 * EM_SETPASSWORDCHAR
3457 static void EDIT_EM_SetPasswordChar(HWND hwnd, EDITSTATE *es, WCHAR c)
3461 if (es->style & ES_MULTILINE)
3464 if (es->password_char == c)
3467 style = GetWindowLongA( hwnd, GWL_STYLE );
3468 es->password_char = c;
3470 SetWindowLongA( hwnd, GWL_STYLE, style | ES_PASSWORD );
3471 es->style |= ES_PASSWORD;
3473 SetWindowLongA( hwnd, GWL_STYLE, style & ~ES_PASSWORD );
3474 es->style &= ~ES_PASSWORD;
3476 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3480 /*********************************************************************
3484 * note: unlike the specs say: the order of start and end
3485 * _is_ preserved in Windows. (i.e. start can be > end)
3486 * In other words: this handler is OK
3489 static void EDIT_EM_SetSel(HWND hwnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3491 UINT old_start = es->selection_start;
3492 UINT old_end = es->selection_end;
3493 UINT len = strlenW(es->text);
3495 if (start == (UINT)-1) {
3496 start = es->selection_end;
3497 end = es->selection_end;
3499 start = min(start, len);
3500 end = min(end, len);
3502 es->selection_start = start;
3503 es->selection_end = end;
3505 es->flags |= EF_AFTER_WRAP;
3507 es->flags &= ~EF_AFTER_WRAP;
3508 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3509 ORDER_UINT(start, end);
3510 ORDER_UINT(end, old_end);
3511 ORDER_UINT(start, old_start);
3512 ORDER_UINT(old_start, old_end);
3513 if (end != old_start)
3517 * ORDER_UINT32(end, old_start);
3518 * EDIT_InvalidateText(hwnd, es, start, end);
3519 * EDIT_InvalidateText(hwnd, es, old_start, old_end);
3520 * in place of the following if statement.
3522 if (old_start > end )
3524 EDIT_InvalidateText(hwnd, es, start, end);
3525 EDIT_InvalidateText(hwnd, es, old_start, old_end);
3529 EDIT_InvalidateText(hwnd, es, start, old_start);
3530 EDIT_InvalidateText(hwnd, es, end, old_end);
3533 else EDIT_InvalidateText(hwnd, es, start, old_end);
3537 /*********************************************************************
3542 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3544 if (!(es->style & ES_MULTILINE))
3547 HeapFree(GetProcessHeap(), 0, es->tabs);
3548 es->tabs_count = count;
3552 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3553 memcpy(es->tabs, tabs, count * sizeof(INT));
3559 /*********************************************************************
3564 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3566 if (!(es->style & ES_MULTILINE))
3569 HeapFree(GetProcessHeap(), 0, es->tabs);
3570 es->tabs_count = count;
3575 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3576 for (i = 0 ; i < count ; i++)
3577 es->tabs[i] = *tabs++;
3583 /*********************************************************************
3585 * EM_SETWORDBREAKPROC
3588 static void EDIT_EM_SetWordBreakProc(HWND hwnd, EDITSTATE *es, LPARAM lParam)
3590 if (es->word_break_proc == (void *)lParam)
3593 es->word_break_proc = (void *)lParam;
3594 es->word_break_proc16 = NULL;
3596 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3597 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3598 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3603 /*********************************************************************
3605 * EM_SETWORDBREAKPROC16
3608 static void EDIT_EM_SetWordBreakProc16(HWND hwnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3610 if (es->word_break_proc16 == wbp)
3613 es->word_break_proc = NULL;
3614 es->word_break_proc16 = wbp;
3615 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3616 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3617 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3622 /*********************************************************************
3627 static BOOL EDIT_EM_Undo(HWND hwnd, EDITSTATE *es)
3632 /* Protect read-only edit control from modification */
3633 if(es->style & ES_READONLY)
3636 ulength = strlenW(es->undo_text);
3637 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3639 strcpyW(utext, es->undo_text);
3641 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3642 es->undo_insert_count, debugstr_w(utext));
3644 EDIT_EM_SetSel(hwnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3645 EDIT_EM_EmptyUndoBuffer(es);
3646 EDIT_EM_ReplaceSel(hwnd, es, TRUE, utext, FALSE);
3647 EDIT_EM_SetSel(hwnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3648 /* send the notification after the selection start and end are set */
3649 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3650 EDIT_EM_ScrollCaret(hwnd, es);
3651 HeapFree(GetProcessHeap(), 0, utext);
3653 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3654 es->undo_insert_count, debugstr_w(es->undo_text));
3659 /*********************************************************************
3664 static void EDIT_WM_Char(HWND hwnd, EDITSTATE *es, WCHAR c)
3668 /* Protect read-only edit control from modification */
3669 if(es->style & ES_READONLY)
3672 control = GetKeyState(VK_CONTROL) & 0x8000;
3676 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3677 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3680 if (es->style & ES_MULTILINE) {
3681 if (es->style & ES_READONLY) {
3682 EDIT_MoveHome(hwnd, es, FALSE);
3683 EDIT_MoveDown_ML(hwnd, es, FALSE);
3685 static const WCHAR cr_lfW[] = {'\r','\n',0};
3686 EDIT_EM_ReplaceSel(hwnd, es, TRUE, cr_lfW, TRUE);
3691 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3693 static const WCHAR tabW[] = {'\t',0};
3694 EDIT_EM_ReplaceSel(hwnd, es, TRUE, tabW, TRUE);
3698 if (!(es->style & ES_READONLY) && !control) {
3699 if (es->selection_start != es->selection_end)
3700 EDIT_WM_Clear(hwnd, es);
3702 /* delete character left of caret */
3703 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
3704 EDIT_MoveBackward(hwnd, es, TRUE);
3705 EDIT_WM_Clear(hwnd, es);
3710 SendMessageW(hwnd, WM_COPY, 0, 0);
3713 SendMessageW(hwnd, WM_PASTE, 0, 0);
3716 SendMessageW(hwnd, WM_CUT, 0, 0);
3720 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3724 EDIT_EM_ReplaceSel(hwnd, es, TRUE, str, TRUE);
3731 /*********************************************************************
3736 static void EDIT_WM_Command(HWND hwnd, EDITSTATE *es, INT code, INT id, HWND control)
3738 if (code || control)
3743 EDIT_EM_Undo(hwnd, es);
3746 EDIT_WM_Cut(hwnd, es);
3749 EDIT_WM_Copy(hwnd, es);
3752 EDIT_WM_Paste(hwnd, es);
3755 EDIT_WM_Clear(hwnd, es);
3758 EDIT_EM_SetSel(hwnd, es, 0, (UINT)-1, FALSE);
3759 EDIT_EM_ScrollCaret(hwnd, es);
3762 ERR("unknown menu item, please report\n");
3768 /*********************************************************************
3772 * Note: the resource files resource/sysres_??.rc cannot define a
3773 * single popup menu. Hence we use a (dummy) menubar
3774 * containing the single popup menu as its first item.
3776 * FIXME: the message identifiers have been chosen arbitrarily,
3777 * hence we use MF_BYPOSITION.
3778 * We might as well use the "real" values (anybody knows ?)
3779 * The menu definition is in resources/sysres_??.rc.
3780 * Once these are OK, we better use MF_BYCOMMAND here
3781 * (as we do in EDIT_WM_Command()).
3784 static void EDIT_WM_ContextMenu(HWND hwnd, EDITSTATE *es, INT x, INT y)
3786 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3787 HMENU popup = GetSubMenu(menu, 0);
3788 UINT start = es->selection_start;
3789 UINT end = es->selection_end;
3791 ORDER_UINT(start, end);
3794 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3796 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3798 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3800 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3802 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3804 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3806 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, hwnd, NULL);
3811 /*********************************************************************
3816 static void EDIT_WM_Copy(HWND hwnd, EDITSTATE *es)
3818 INT s = es->selection_start;
3819 INT e = es->selection_end;
3826 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3827 dst = GlobalLock(hdst);
3828 strncpyW(dst, es->text + s, e - s);
3829 dst[e - s] = 0; /* ensure 0 termination */
3830 TRACE("%s\n", debugstr_w(dst));
3832 OpenClipboard(hwnd);
3834 SetClipboardData(CF_UNICODETEXT, hdst);
3839 /*********************************************************************
3844 static LRESULT EDIT_WM_Create(HWND hwnd, EDITSTATE *es, LPCWSTR name)
3846 TRACE("%s\n", debugstr_w(name));
3848 * To initialize some final structure members, we call some helper
3849 * functions. However, since the EDITSTATE is not consistent (i.e.
3850 * not fully initialized), we should be very careful which
3851 * functions can be called, and in what order.
3853 EDIT_WM_SetFont(hwnd, es, 0, FALSE);
3854 EDIT_EM_EmptyUndoBuffer(es);
3856 if (name && *name) {
3857 EDIT_EM_ReplaceSel(hwnd, es, FALSE, name, FALSE);
3858 /* if we insert text to the editline, the text scrolls out
3859 * of the window, as the caret is placed after the insert
3860 * pos normally; thus we reset es->selection... to 0 and
3863 es->selection_start = es->selection_end = 0;
3864 /* send the notification after the selection start and end are set */
3865 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3866 EDIT_EM_ScrollCaret(hwnd, es);
3868 /* force scroll info update */
3869 EDIT_UpdateScrollInfo(hwnd, es);
3874 /*********************************************************************
3879 static void EDIT_WM_Destroy(HWND hwnd, EDITSTATE *es)
3881 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3885 while (LocalUnlock(es->hloc32W)) ;
3886 LocalFree(es->hloc32W);
3889 while (LocalUnlock(es->hloc32A)) ;
3890 LocalFree(es->hloc32A);
3893 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3894 LOCAL_Free(hInstance, es->hloc16);
3897 pc = es->first_line_def;
3901 HeapFree(GetProcessHeap(), 0, pc);
3905 SetWindowLongA( hwnd, 0, 0 );
3906 HeapFree(GetProcessHeap(), 0, es);
3910 /*********************************************************************
3915 static LRESULT EDIT_WM_EraseBkGnd(HWND hwnd, EDITSTATE *es, HDC dc)
3920 if ( get_app_version() >= 0x40000 &&(
3921 !es->bEnableState || (es->style & ES_READONLY)))
3922 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(hwnd, dc);
3924 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(hwnd, dc);
3927 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3929 GetClientRect(hwnd, &rc);
3930 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3931 GetClipBox(dc, &rc);
3933 * FIXME: specs say that we should UnrealizeObject() the brush,
3934 * but the specs of UnrealizeObject() say that we shouldn't
3935 * unrealize a stock object. The default brush that
3936 * DefWndProc() returns is ... a stock object.
3938 FillRect(dc, &rc, brush);
3943 /*********************************************************************
3948 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3950 if(!count) return 0;
3954 LPWSTR textW = (LPWSTR)lParam;
3955 strncpyW(textW, es->text, count);
3956 textW[count - 1] = 0; /* ensure 0 termination */
3957 return strlenW(textW);
3961 LPSTR textA = (LPSTR)lParam;
3962 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL);
3963 textA[count - 1] = 0; /* ensure 0 termination */
3964 return strlen(textA);
3968 /*********************************************************************
3973 static LRESULT EDIT_WM_HScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos)
3978 if (!(es->style & ES_MULTILINE))
3981 if (!(es->style & ES_AUTOHSCROLL))
3985 fw = es->format_rect.right - es->format_rect.left;
3988 TRACE("SB_LINELEFT\n");
3990 dx = -es->char_width;
3993 TRACE("SB_LINERIGHT\n");
3994 if (es->x_offset < es->text_width)
3995 dx = es->char_width;
3998 TRACE("SB_PAGELEFT\n");
4000 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4003 TRACE("SB_PAGERIGHT\n");
4004 if (es->x_offset < es->text_width)
4005 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4013 TRACE("SB_RIGHT\n");
4014 if (es->x_offset < es->text_width)
4015 dx = es->text_width - es->x_offset;
4018 TRACE("SB_THUMBTRACK %d\n", pos);
4019 es->flags |= EF_HSCROLL_TRACK;
4020 if(es->style & WS_HSCROLL)
4021 dx = pos - es->x_offset;
4026 if(pos < 0 || pos > 100) return 0;
4027 /* Assume default scroll range 0-100 */
4028 fw = es->format_rect.right - es->format_rect.left;
4029 new_x = pos * (es->text_width - fw) / 100;
4030 dx = es->text_width ? (new_x - es->x_offset) : 0;
4033 case SB_THUMBPOSITION:
4034 TRACE("SB_THUMBPOSITION %d\n", pos);
4035 es->flags &= ~EF_HSCROLL_TRACK;
4036 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_HSCROLL)
4037 dx = pos - es->x_offset;
4042 if(pos < 0 || pos > 100) return 0;
4043 /* Assume default scroll range 0-100 */
4044 fw = es->format_rect.right - es->format_rect.left;
4045 new_x = pos * (es->text_width - fw) / 100;
4046 dx = es->text_width ? (new_x - es->x_offset) : 0;
4049 /* force scroll info update */
4050 EDIT_UpdateScrollInfo(hwnd, es);
4051 EDIT_NOTIFY_PARENT(hwnd, es, EN_HSCROLL, "EN_HSCROLL");
4055 TRACE("SB_ENDSCROLL\n");
4058 * FIXME : the next two are undocumented !
4059 * Are we doing the right thing ?
4060 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4061 * although it's also a regular control message.
4063 case EM_GETTHUMB: /* this one is used by NT notepad */
4067 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_HSCROLL)
4068 ret = GetScrollPos(hwnd, SB_HORZ);
4071 /* Assume default scroll range 0-100 */
4072 INT fw = es->format_rect.right - es->format_rect.left;
4073 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4075 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4078 case EM_LINESCROLL16:
4079 TRACE("EM_LINESCROLL16\n");
4084 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4090 INT fw = es->format_rect.right - es->format_rect.left;
4091 /* check if we are going to move too far */
4092 if(es->x_offset + dx + fw > es->text_width)
4093 dx = es->text_width - fw - es->x_offset;
4095 EDIT_EM_LineScroll_internal(hwnd, es, dx, 0);
4101 /*********************************************************************
4106 static BOOL EDIT_CheckCombo(HWND hwnd, EDITSTATE *es, UINT msg, INT key)
4108 HWND hLBox = es->hwndListBox;
4116 hCombo = GetParent(hwnd);
4120 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
4121 hwnd, (UINT16)msg, (UINT16)key);
4123 if (key == VK_UP || key == VK_DOWN)
4125 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4128 if (msg == WM_KEYDOWN || nEUI)
4129 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4135 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4137 /* make sure ComboLBox pops up */
4138 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4143 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4146 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4148 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4150 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4155 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4161 /*********************************************************************
4165 * Handling of special keys that don't produce a WM_CHAR
4166 * (i.e. non-printable keys) & Backspace & Delete
4169 static LRESULT EDIT_WM_KeyDown(HWND hwnd, EDITSTATE *es, INT key)
4174 if (GetKeyState(VK_MENU) & 0x8000)
4177 shift = GetKeyState(VK_SHIFT) & 0x8000;
4178 control = GetKeyState(VK_CONTROL) & 0x8000;
4183 if (EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key) || key == VK_F4)
4188 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4189 EDIT_MoveUp_ML(hwnd, es, shift);
4192 EDIT_MoveWordBackward(hwnd, es, shift);
4194 EDIT_MoveBackward(hwnd, es, shift);
4197 if (EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key))
4201 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4202 EDIT_MoveDown_ML(hwnd, es, shift);
4204 EDIT_MoveWordForward(hwnd, es, shift);
4206 EDIT_MoveForward(hwnd, es, shift);
4209 EDIT_MoveHome(hwnd, es, shift);
4212 EDIT_MoveEnd(hwnd, es, shift);
4215 if (es->style & ES_MULTILINE)
4216 EDIT_MovePageUp_ML(hwnd, es, shift);
4218 EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key);
4221 if (es->style & ES_MULTILINE)
4222 EDIT_MovePageDown_ML(hwnd, es, shift);
4224 EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key);
4227 if (!(es->style & ES_READONLY) && !(shift && control)) {
4228 if (es->selection_start != es->selection_end) {
4230 EDIT_WM_Cut(hwnd, es);
4232 EDIT_WM_Clear(hwnd, es);
4235 /* delete character left of caret */
4236 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4237 EDIT_MoveBackward(hwnd, es, TRUE);
4238 EDIT_WM_Clear(hwnd, es);
4239 } else if (control) {
4240 /* delete to end of line */
4241 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4242 EDIT_MoveEnd(hwnd, es, TRUE);
4243 EDIT_WM_Clear(hwnd, es);
4245 /* delete character right of caret */
4246 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4247 EDIT_MoveForward(hwnd, es, TRUE);
4248 EDIT_WM_Clear(hwnd, es);
4255 if (!(es->style & ES_READONLY))
4256 EDIT_WM_Paste(hwnd, es);
4258 EDIT_WM_Copy(hwnd, es);
4261 /* If the edit doesn't want the return send a message to the default object */
4262 if(!(es->style & ES_WANTRETURN))
4264 HWND hwndParent = GetParent(hwnd);
4265 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4266 if (HIWORD(dw) == DC_HASDEFID)
4268 SendMessageW( hwndParent, WM_COMMAND,
4269 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4270 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4279 /*********************************************************************
4284 static LRESULT EDIT_WM_KillFocus(HWND hwnd, EDITSTATE *es)
4286 es->flags &= ~EF_FOCUSED;
4288 if(!(es->style & ES_NOHIDESEL))
4289 EDIT_InvalidateText(hwnd, es, es->selection_start, es->selection_end);
4290 EDIT_NOTIFY_PARENT(hwnd, es, EN_KILLFOCUS, "EN_KILLFOCUS");
4295 /*********************************************************************
4299 * The caret position has been set on the WM_LBUTTONDOWN message
4302 static LRESULT EDIT_WM_LButtonDblClk(HWND hwnd, EDITSTATE *es)
4305 INT e = es->selection_end;
4310 if (!(es->flags & EF_FOCUSED))
4313 l = EDIT_EM_LineFromChar(es, e);
4314 li = EDIT_EM_LineIndex(es, l);
4315 ll = EDIT_EM_LineLength(es, e);
4316 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4317 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4318 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
4319 EDIT_EM_ScrollCaret(hwnd, es);
4324 /*********************************************************************
4329 static LRESULT EDIT_WM_LButtonDown(HWND hwnd, EDITSTATE *es, DWORD keys, INT x, INT y)
4334 if (!(es->flags & EF_FOCUSED))
4337 es->bCaptureState = TRUE;
4339 EDIT_ConfinePoint(es, &x, &y);
4340 e = EDIT_CharFromPos(hwnd, es, x, y, &after_wrap);
4341 EDIT_EM_SetSel(hwnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4342 EDIT_EM_ScrollCaret(hwnd, es);
4343 es->region_posx = es->region_posy = 0;
4344 SetTimer(hwnd, 0, 100, NULL);
4349 /*********************************************************************
4354 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es)
4356 if (es->bCaptureState && GetCapture() == hwndSelf) {
4357 KillTimer(hwndSelf, 0);
4360 es->bCaptureState = FALSE;
4365 /*********************************************************************
4370 static LRESULT EDIT_WM_MButtonDown(HWND hwnd)
4372 SendMessageW(hwnd,WM_PASTE,0,0);
4377 /*********************************************************************
4382 static LRESULT EDIT_WM_MouseMove(HWND hwnd, EDITSTATE *es, INT x, INT y)
4388 if (GetCapture() != hwnd)
4392 * FIXME: gotta do some scrolling if outside client
4393 * area. Maybe reset the timer ?
4396 EDIT_ConfinePoint(es, &x, &y);
4397 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4398 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4399 e = EDIT_CharFromPos(hwnd, es, x, y, &after_wrap);
4400 EDIT_EM_SetSel(hwnd, es, es->selection_start, e, after_wrap);
4405 /*********************************************************************
4409 * See also EDIT_WM_StyleChanged
4411 static LRESULT EDIT_WM_NCCreate(HWND hwnd, DWORD style, HWND hwndParent, BOOL unicode)
4416 TRACE("Creating %s edit control, style = %08lx\n",
4417 unicode ? "Unicode" : "ANSI", style);
4419 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4421 SetWindowLongA( hwnd, 0, (LONG)es );
4424 * Note: since the EDITSTATE has not been fully initialized yet,
4425 * we can't use any API calls that may send
4426 * WM_XXX messages before WM_NCCREATE is completed.
4429 es->is_unicode = unicode;
4432 es->bEnableState = !(style & WS_DISABLED);
4434 /* Save parent, which will be notified by EN_* messages */
4435 es->hwndParent = hwndParent;
4437 if (es->style & ES_COMBO)
4438 es->hwndListBox = GetDlgItem(hwndParent, ID_CB_LISTBOX);
4440 /* Number overrides lowercase overrides uppercase (at least it
4441 * does in Win95). However I'll bet that ES_NUMBER would be
4442 * invalid under Win 3.1.
4444 if (es->style & ES_NUMBER) {
4445 ; /* do not override the ES_NUMBER */
4446 } else if (es->style & ES_LOWERCASE) {
4447 es->style &= ~ES_UPPERCASE;
4449 if (es->style & ES_MULTILINE) {
4450 es->buffer_limit = BUFLIMIT_MULTI;
4451 if (es->style & WS_VSCROLL)
4452 es->style |= ES_AUTOVSCROLL;
4453 if (es->style & WS_HSCROLL)
4454 es->style |= ES_AUTOHSCROLL;
4455 es->style &= ~ES_PASSWORD;
4456 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4457 /* Confirmed - RIGHT overrides CENTER */
4458 if (es->style & ES_RIGHT)
4459 es->style &= ~ES_CENTER;
4460 es->style &= ~WS_HSCROLL;
4461 es->style &= ~ES_AUTOHSCROLL;
4464 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4465 es->style |= ES_AUTOVSCROLL;
4467 es->buffer_limit = BUFLIMIT_SINGLE;
4468 if (WIN31_LOOK == TWEAK_WineLook ||
4469 WIN95_LOOK == TWEAK_WineLook) {
4470 es->style &= ~ES_CENTER;
4471 es->style &= ~ES_RIGHT;
4473 if (es->style & ES_RIGHT)
4474 es->style &= ~ES_CENTER;
4476 es->style &= ~WS_HSCROLL;
4477 es->style &= ~WS_VSCROLL;
4478 es->style &= ~ES_AUTOVSCROLL;
4479 es->style &= ~ES_WANTRETURN;
4480 if (es->style & ES_PASSWORD)
4481 es->password_char = '*';
4483 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4484 es->style |= ES_AUTOHSCROLL;
4487 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4488 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4490 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4492 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4494 es->undo_buffer_size = es->buffer_size;
4496 if (es->style & ES_MULTILINE)
4497 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4502 * In Win95 look and feel, the WS_BORDER style is replaced by the
4503 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4504 * control a non client area. Not always. This coordinates in some
4505 * way with the window creation code in dialog.c When making
4506 * modifications please ensure that the code still works for edit
4507 * controls created directly with style 0x50800000, exStyle 0 (
4508 * which should have a single pixel border)
4510 if (TWEAK_WineLook != WIN31_LOOK)
4512 es->style &= ~WS_BORDER;
4516 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4517 SetWindowLongA( hwnd, GWL_STYLE,
4518 GetWindowLongA( hwnd, GWL_STYLE ) & ~WS_BORDER );
4524 /*********************************************************************
4529 static void EDIT_WM_Paint(HWND hwnd, EDITSTATE *es, WPARAM wParam)
4538 BOOL rev = es->bEnableState &&
4539 ((es->flags & EF_FOCUSED) ||
4540 (es->style & ES_NOHIDESEL));
4542 dc = BeginPaint(hwnd, &ps);
4545 if(es->style & WS_BORDER) {
4546 GetClientRect(hwnd, &rc);
4547 if(es->style & ES_MULTILINE) {
4548 if(es->style & WS_HSCROLL) rc.bottom++;
4549 if(es->style & WS_VSCROLL) rc.right++;
4551 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4553 IntersectClipRect(dc, es->format_rect.left,
4554 es->format_rect.top,
4555 es->format_rect.right,
4556 es->format_rect.bottom);
4557 if (es->style & ES_MULTILINE) {
4558 GetClientRect(hwnd, &rc);
4559 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4562 old_font = SelectObject(dc, es->font);
4563 if ( get_app_version() >= 0x40000 &&(
4564 !es->bEnableState || (es->style & ES_READONLY)))
4565 EDIT_SEND_CTLCOLORSTATIC(hwnd, dc);
4567 EDIT_SEND_CTLCOLOR(hwnd, dc);
4569 if (!es->bEnableState)
4570 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4571 GetClipBox(dc, &rcRgn);
4572 if (es->style & ES_MULTILINE) {
4573 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4574 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4575 EDIT_GetLineRect(hwnd, es, i, 0, -1, &rcLine);
4576 if (IntersectRect(&rc, &rcRgn, &rcLine))
4577 EDIT_PaintLine(hwnd, es, dc, i, rev);
4580 EDIT_GetLineRect(hwnd, es, 0, 0, -1, &rcLine);
4581 if (IntersectRect(&rc, &rcRgn, &rcLine))
4582 EDIT_PaintLine(hwnd, es, dc, 0, rev);
4585 SelectObject(dc, old_font);
4588 EndPaint(hwnd, &ps);
4592 /*********************************************************************
4597 static void EDIT_WM_Paste(HWND hwnd, EDITSTATE *es)
4602 /* Protect read-only edit control from modification */
4603 if(es->style & ES_READONLY)
4606 OpenClipboard(hwnd);
4607 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4608 src = (LPWSTR)GlobalLock(hsrc);
4609 EDIT_EM_ReplaceSel(hwnd, es, TRUE, src, TRUE);
4616 /*********************************************************************
4621 static void EDIT_WM_SetFocus(HWND hwnd, EDITSTATE *es)
4623 es->flags |= EF_FOCUSED;
4624 CreateCaret(hwnd, 0, 2, es->line_height);
4625 EDIT_SetCaretPos(hwnd, es, es->selection_end,
4626 es->flags & EF_AFTER_WRAP);
4627 if(!(es->style & ES_NOHIDESEL))
4628 EDIT_InvalidateText(hwnd, es, es->selection_start, es->selection_end);
4630 EDIT_NOTIFY_PARENT(hwnd, es, EN_SETFOCUS, "EN_SETFOCUS");
4634 /*********************************************************************
4638 * With Win95 look the margins are set to default font value unless
4639 * the system font (font == 0) is being set, in which case they are left
4643 static void EDIT_WM_SetFont(HWND hwnd, EDITSTATE *es, HFONT font, BOOL redraw)
4653 old_font = SelectObject(dc, font);
4654 GetTextMetricsW(dc, &tm);
4655 es->line_height = tm.tmHeight;
4656 es->char_width = tm.tmAveCharWidth;
4658 SelectObject(dc, old_font);
4659 ReleaseDC(hwnd, dc);
4660 if (font && (TWEAK_WineLook > WIN31_LOOK))
4661 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4662 EC_USEFONTINFO, EC_USEFONTINFO);
4664 /* Force the recalculation of the format rect for each font change */
4665 GetClientRect(hwnd, &r);
4666 EDIT_SetRectNP(hwnd, es, &r);
4668 if (es->style & ES_MULTILINE)
4669 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
4671 EDIT_CalcLineWidth_SL(hwnd, es);
4674 EDIT_UpdateText(hwnd, es, NULL, TRUE);
4675 if (es->flags & EF_FOCUSED) {
4677 CreateCaret(hwnd, 0, 2, es->line_height);
4678 EDIT_SetCaretPos(hwnd, es, es->selection_end,
4679 es->flags & EF_AFTER_WRAP);
4685 /*********************************************************************
4690 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4691 * The modified flag is reset. No notifications are sent.
4693 * For single-line controls, reception of WM_SETTEXT triggers:
4694 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4697 static void EDIT_WM_SetText(HWND hwnd, EDITSTATE *es, LPARAM lParam, BOOL unicode)
4702 text = (LPWSTR)lParam;
4705 LPCSTR textA = (LPCSTR)lParam;
4706 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4707 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4708 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4711 EDIT_EM_SetSel(hwnd, es, 0, (UINT)-1, FALSE);
4713 TRACE("%s\n", debugstr_w(text));
4714 EDIT_EM_ReplaceSel(hwnd, es, FALSE, text, FALSE);
4716 HeapFree(GetProcessHeap(), 0, text);
4718 static const WCHAR empty_stringW[] = {0};
4720 EDIT_EM_ReplaceSel(hwnd, es, FALSE, empty_stringW, FALSE);
4723 es->flags &= ~EF_MODIFIED;
4724 EDIT_EM_SetSel(hwnd, es, 0, 0, FALSE);
4725 /* Send the notification after the selection start and end have been set
4726 * edit control doesn't send notification on WM_SETTEXT
4727 * if it is multiline, or it is part of combobox
4729 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4730 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
4731 EDIT_EM_ScrollCaret(hwnd, es);
4735 /*********************************************************************
4740 static void EDIT_WM_Size(HWND hwnd, EDITSTATE *es, UINT action, INT width, INT height)
4742 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4744 TRACE("width = %d, height = %d\n", width, height);
4745 SetRect(&rc, 0, 0, width, height);
4746 EDIT_SetRectNP(hwnd, es, &rc);
4747 EDIT_UpdateText(hwnd, es, NULL, TRUE);
4752 /*********************************************************************
4756 * This message is sent by SetWindowLong on having changed either the Style
4757 * or the extended style.
4759 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4761 * See also EDIT_WM_NCCreate
4763 * It appears that the Windows version of the edit control allows the style
4764 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4765 * style variable which will generally be different. In this function we
4766 * update the internal style based on what changed in the externally visible
4769 * Much of this content as based upon the MSDN, especially:
4770 * Platform SDK Documentation -> User Interface Services ->
4771 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4772 * Edit Control Styles
4774 static LRESULT EDIT_WM_StyleChanged (HWND hwnd,
4777 const STYLESTRUCT *style)
4779 if (GWL_STYLE == which) {
4780 DWORD style_change_mask;
4782 /* Only a subset of changes can be applied after the control
4785 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4787 if (es->style & ES_MULTILINE)
4788 style_change_mask |= ES_WANTRETURN;
4790 new_style = style->styleNew & style_change_mask;
4792 /* Number overrides lowercase overrides uppercase (at least it
4793 * does in Win95). However I'll bet that ES_NUMBER would be
4794 * invalid under Win 3.1.
4796 if (new_style & ES_NUMBER) {
4797 ; /* do not override the ES_NUMBER */
4798 } else if (new_style & ES_LOWERCASE) {
4799 new_style &= ~ES_UPPERCASE;
4802 es->style = (es->style & ~style_change_mask) | new_style;
4803 } else if (GWL_EXSTYLE == which) {
4804 ; /* FIXME - what is needed here */
4806 WARN ("Invalid style change %d\n",which);
4812 /*********************************************************************
4817 static LRESULT EDIT_WM_SysKeyDown(HWND hwnd, EDITSTATE *es, INT key, DWORD key_data)
4819 if ((key == VK_BACK) && (key_data & 0x2000)) {
4820 if (EDIT_EM_CanUndo(es))
4821 EDIT_EM_Undo(hwnd, es);
4823 } else if (key == VK_UP || key == VK_DOWN) {
4824 if (EDIT_CheckCombo(hwnd, es, WM_SYSKEYDOWN, key))
4827 return DefWindowProcW(hwnd, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4831 /*********************************************************************
4836 static void EDIT_WM_Timer(HWND hwnd, EDITSTATE *es)
4838 if (es->region_posx < 0) {
4839 EDIT_MoveBackward(hwnd, es, TRUE);
4840 } else if (es->region_posx > 0) {
4841 EDIT_MoveForward(hwnd, es, TRUE);
4844 * FIXME: gotta do some vertical scrolling here, like
4845 * EDIT_EM_LineScroll(hwnd, 0, 1);
4849 /*********************************************************************
4854 static LRESULT EDIT_WM_VScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos)
4858 if (!(es->style & ES_MULTILINE))
4861 if (!(es->style & ES_AUTOVSCROLL))
4870 TRACE("action %d\n", action);
4871 EDIT_EM_Scroll(hwnd, es, action);
4878 TRACE("SB_BOTTOM\n");
4879 dy = es->line_count - 1 - es->y_offset;
4882 TRACE("SB_THUMBTRACK %d\n", pos);
4883 es->flags |= EF_VSCROLL_TRACK;
4884 if(es->style & WS_VSCROLL)
4885 dy = pos - es->y_offset;
4888 /* Assume default scroll range 0-100 */
4891 if(pos < 0 || pos > 100) return 0;
4892 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4893 new_y = pos * (es->line_count - vlc) / 100;
4894 dy = es->line_count ? (new_y - es->y_offset) : 0;
4895 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4896 es->line_count, es->y_offset, pos, dy);
4899 case SB_THUMBPOSITION:
4900 TRACE("SB_THUMBPOSITION %d\n", pos);
4901 es->flags &= ~EF_VSCROLL_TRACK;
4902 if(es->style & WS_VSCROLL)
4903 dy = pos - es->y_offset;
4906 /* Assume default scroll range 0-100 */
4909 if(pos < 0 || pos > 100) return 0;
4910 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4911 new_y = pos * (es->line_count - vlc) / 100;
4912 dy = es->line_count ? (new_y - es->y_offset) : 0;
4913 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4914 es->line_count, es->y_offset, pos, dy);
4918 /* force scroll info update */
4919 EDIT_UpdateScrollInfo(hwnd, es);
4920 EDIT_NOTIFY_PARENT(hwnd, es, EN_VSCROLL, "EN_VSCROLL");
4924 TRACE("SB_ENDSCROLL\n");
4927 * FIXME : the next two are undocumented !
4928 * Are we doing the right thing ?
4929 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4930 * although it's also a regular control message.
4932 case EM_GETTHUMB: /* this one is used by NT notepad */
4936 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_VSCROLL)
4937 ret = GetScrollPos(hwnd, SB_VERT);
4940 /* Assume default scroll range 0-100 */
4941 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4942 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4944 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4947 case EM_LINESCROLL16:
4948 TRACE("EM_LINESCROLL16 %d\n", pos);
4953 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4958 EDIT_EM_LineScroll(hwnd, es, 0, dy);
4962 /*********************************************************************
4967 static void EDIT_UpdateTextRegion(HWND hwnd, EDITSTATE *es, HRGN hrgn, BOOL bErase)
4969 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(hwnd, es, EN_UPDATE, "EN_UPDATE");
4970 InvalidateRgn(hwnd, hrgn, bErase);
4974 /*********************************************************************
4979 static void EDIT_UpdateText(HWND hwnd, EDITSTATE *es, LPRECT rc, BOOL bErase)
4981 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(hwnd, es, EN_UPDATE, "EN_UPDATE");
4982 InvalidateRect(hwnd, rc, bErase);