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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
57 #include "wine/winbase16.h"
58 #include "wine/winuser16.h"
59 #include "wine/unicode.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
69 FIXME: BTW, new specs say 65535 (do you dare ???) */
70 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
71 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
72 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
73 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
76 * extra flags for EDITSTATE.flags field
78 #define EF_MODIFIED 0x0001 /* text has been modified */
79 #define EF_FOCUSED 0x0002 /* we have input focus */
80 #define EF_UPDATE 0x0004 /* notify parent of changed state */
81 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
82 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
83 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
84 wrapped line, instead of in front of the next character */
85 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
86 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
87 the text buffer if this is clear. */
90 END_0 = 0, /* line ends with terminating '\0' character */
91 END_WRAP, /* line is wrapped */
92 END_HARD, /* line ends with a hard return '\r\n' */
93 END_SOFT, /* line ends with a soft return '\r\r\n' */
94 END_RICH /* line ends with a single '\n' */
97 typedef struct tagLINEDEF {
98 INT length; /* bruto length of a line in bytes */
99 INT net_length; /* netto length of a line in visible characters */
101 INT width; /* width of the line in pixels */
102 INT index; /* line index into the buffer */
103 struct tagLINEDEF *next;
108 BOOL is_unicode; /* how the control was created */
109 LPWSTR text; /* the actual contents of the control */
110 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
111 UINT buffer_size; /* the size of the buffer in characters */
112 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
113 HFONT font; /* NULL means standard system font */
114 INT x_offset; /* scroll offset for multi lines this is in pixels
115 for single lines it's in characters */
116 INT line_height; /* height of a screen line in pixels */
117 INT char_width; /* average character width in pixels */
118 DWORD style; /* sane version of wnd->dwStyle */
119 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
120 INT undo_insert_count; /* number of characters inserted in sequence */
121 UINT undo_position; /* character index of the insertion and deletion */
122 LPWSTR undo_text; /* deleted text */
123 UINT undo_buffer_size; /* size of the deleted text buffer */
124 INT selection_start; /* == selection_end if no selection */
125 INT selection_end; /* == current caret position */
126 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
127 INT left_margin; /* in pixels */
128 INT right_margin; /* in pixels */
130 INT text_width; /* width of the widest line in pixels for multi line controls
131 and just line width for single line controls */
132 INT region_posx; /* Position of cursor relative to region: */
133 INT region_posy; /* -1: to left, 0: within, 1: to right */
134 EDITWORDBREAKPROC16 word_break_proc16;
135 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
136 INT line_count; /* number of lines */
137 INT y_offset; /* scroll offset in number of lines */
138 BOOL bCaptureState; /* flag indicating whether mouse was captured */
139 BOOL bEnableState; /* flag keeping the enable state */
140 HWND hwndSelf; /* the our window handle */
141 HWND hwndParent; /* Handle of parent for sending EN_* messages.
142 Even if parent will change, EN_* messages
143 should be sent to the first parent. */
144 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
146 * only for multi line controls
148 INT lock_count; /* amount of re-entries in the EditWndProc */
151 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
152 HLOCAL hloc32W; /* our unicode local memory block */
153 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
155 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
160 UINT composition_len; /* length of composition, 0 == no composition */
161 int composition_start; /* the character position for the composition */
165 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
166 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
168 /* used for disabled or read-only edit control */
169 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
171 { /* Notify parent which has created this edit control */ \
172 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
173 SendMessageW(es->hwndParent, WM_COMMAND, \
174 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
175 (LPARAM)(es->hwndSelf)); \
178 /*********************************************************************
185 * These functions have trivial implementations
186 * We still like to call them internally
187 * "static inline" makes them more like macro's
189 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
190 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
191 static inline void EDIT_WM_Clear(EDITSTATE *es);
192 static inline void EDIT_WM_Cut(EDITSTATE *es);
195 * Helper functions only valid for one type of control
197 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
198 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
199 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
200 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
201 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
202 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
203 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
205 * Helper functions valid for both single line _and_ multi line controls
207 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
208 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
209 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
210 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
211 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
212 static void EDIT_LockBuffer(EDITSTATE *es);
213 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
214 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
215 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
216 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
217 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
218 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
219 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
220 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
221 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
222 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
223 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
224 static void EDIT_AdjustFormatRect(EDITSTATE *es);
225 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
226 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
227 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
228 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
230 * EM_XXX message handlers
232 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
233 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
234 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
235 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
236 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
237 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
238 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
239 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
240 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
241 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
242 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
243 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
244 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
245 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
246 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
247 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
248 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
249 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
250 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
251 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, WORD left, WORD right, BOOL repaint);
252 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
253 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
254 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
255 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
256 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
257 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
258 static BOOL EDIT_EM_Undo(EDITSTATE *es);
260 * WM_XXX message handlers
262 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
263 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
264 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
265 static void EDIT_WM_Copy(EDITSTATE *es);
266 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
267 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
268 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
269 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
270 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
271 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
272 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
273 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
274 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
275 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
276 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
277 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
278 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
279 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
280 static void EDIT_WM_Paste(EDITSTATE *es);
281 static void EDIT_WM_SetFocus(EDITSTATE *es);
282 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
283 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
284 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
285 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
286 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
287 static void EDIT_WM_Timer(EDITSTATE *es);
288 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
289 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
290 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
291 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
293 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
294 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
296 /*********************************************************************
297 * edit class descriptor
299 const struct builtin_class_descr EDIT_builtin_class =
302 CS_DBLCLKS | CS_PARENTDC, /* style */
303 EditWndProcA, /* procA */
304 EditWndProcW, /* procW */
305 sizeof(EDITSTATE *), /* extra */
306 IDC_IBEAM, /* cursor */
311 /*********************************************************************
316 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
318 return (es->undo_insert_count || strlenW(es->undo_text));
322 /*********************************************************************
327 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
329 es->undo_insert_count = 0;
330 *es->undo_text = '\0';
334 /*********************************************************************
339 static inline void EDIT_WM_Clear(EDITSTATE *es)
341 static const WCHAR empty_stringW[] = {0};
343 /* Protect read-only edit control from modification */
344 if(es->style & ES_READONLY)
347 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
351 /*********************************************************************
356 static inline void EDIT_WM_Cut(EDITSTATE *es)
363 /**********************************************************************
366 * Returns the window version in case Wine emulates a later version
367 * of windows than the application expects.
369 * In a number of cases when windows runs an application that was
370 * designed for an earlier windows version, windows reverts
371 * to "old" behaviour of that earlier version.
373 * An example is a disabled edit control that needs to be painted.
374 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
375 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
376 * applications with an expected version 0f 4.0 or higher.
379 static DWORD get_app_version(void)
381 static DWORD version;
384 DWORD dwEmulatedVersion;
386 DWORD dwProcVersion = GetProcessVersion(0);
388 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
389 GetVersionExW( &info );
390 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
391 /* FIXME: this may not be 100% correct; see discussion on the
392 * wine developer list in Nov 1999 */
393 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
398 static inline UINT get_text_length(EDITSTATE *es)
400 if(es->text_length == (UINT)-1)
401 es->text_length = strlenW(es->text);
402 return es->text_length;
405 static inline void text_buffer_changed(EDITSTATE *es)
407 es->text_length = (UINT)-1;
410 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
415 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
416 msg = WM_CTLCOLORSTATIC;
418 msg = WM_CTLCOLOREDIT;
420 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
421 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
423 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
427 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
430 return DefWindowProcW(hwnd, msg, wParam, lParam);
432 return DefWindowProcA(hwnd, msg, wParam, lParam);
435 /*********************************************************************
439 * The messages are in the order of the actual integer values
440 * (which can be found in include/windows.h)
441 * Wherever possible the 16 bit versions are converted to
442 * the 32 bit ones, so that we can 'fall through' to the
443 * helper functions. These are mostly 32 bit (with a few
444 * exceptions, clearly indicated by a '16' extension to their
448 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
449 WPARAM wParam, LPARAM lParam, BOOL unicode )
451 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
454 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
456 if (!es && msg != WM_NCCREATE)
457 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
459 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
467 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
471 if ((short)LOWORD(lParam) == -1)
472 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
474 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
476 EDIT_EM_ScrollCaret(es);
480 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
481 EDIT_EM_ScrollCaret(es);
488 RECT16 *r16 = MapSL(lParam);
489 r16->left = es->format_rect.left;
490 r16->top = es->format_rect.top;
491 r16->right = es->format_rect.right;
492 r16->bottom = es->format_rect.bottom;
497 CopyRect((LPRECT)lParam, &es->format_rect);
501 if ((es->style & ES_MULTILINE) && lParam) {
503 RECT16 *r16 = MapSL(lParam);
506 rc.right = r16->right;
507 rc.bottom = r16->bottom;
508 EDIT_SetRectNP(es, &rc);
509 EDIT_UpdateText(es, NULL, TRUE);
513 if ((es->style & ES_MULTILINE) && lParam) {
514 EDIT_SetRectNP(es, (LPRECT)lParam);
515 EDIT_UpdateText(es, NULL, TRUE);
520 if ((es->style & ES_MULTILINE) && lParam) {
522 RECT16 *r16 = MapSL(lParam);
525 rc.right = r16->right;
526 rc.bottom = r16->bottom;
527 EDIT_SetRectNP(es, &rc);
531 if ((es->style & ES_MULTILINE) && lParam)
532 EDIT_SetRectNP(es, (LPRECT)lParam);
537 result = EDIT_EM_Scroll(es, (INT)wParam);
540 case EM_LINESCROLL16:
541 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
542 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
545 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
548 case EM_SCROLLCARET16:
550 EDIT_EM_ScrollCaret(es);
556 result = ((es->flags & EF_MODIFIED) != 0);
562 es->flags |= EF_MODIFIED;
564 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
567 case EM_GETLINECOUNT16:
568 case EM_GETLINECOUNT:
569 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
573 if ((INT16)wParam == -1)
577 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
581 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
584 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
588 result = (LRESULT)EDIT_EM_GetHandle16(es);
591 result = (LRESULT)EDIT_EM_GetHandle(es);
596 result = EDIT_EM_GetThumb(es);
599 /* these messages missing from specs */
608 FIXME("undocumented message 0x%x, please report\n", msg);
609 result = DefWindowProcW(hwnd, msg, wParam, lParam);
612 case EM_LINELENGTH16:
614 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
617 case EM_REPLACESEL16:
618 lParam = (LPARAM)MapSL(lParam);
619 unicode = FALSE; /* 16-bit message is always ascii */
626 textW = (LPWSTR)lParam;
629 LPSTR textA = (LPSTR)lParam;
630 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
631 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
632 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
635 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
639 HeapFree(GetProcessHeap(), 0, textW);
644 lParam = (LPARAM)MapSL(lParam);
645 unicode = FALSE; /* 16-bit message is always ascii */
648 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
652 case EM_SETLIMITTEXT:
653 EDIT_EM_SetLimitText(es, (INT)wParam);
658 result = (LRESULT)EDIT_EM_CanUndo(es);
664 result = (LRESULT)EDIT_EM_Undo(es);
669 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
672 case EM_LINEFROMCHAR16:
673 case EM_LINEFROMCHAR:
674 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
677 case EM_SETTABSTOPS16:
678 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
681 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
684 case EM_SETPASSWORDCHAR16:
685 unicode = FALSE; /* 16-bit message is always ascii */
687 case EM_SETPASSWORDCHAR:
692 charW = (WCHAR)wParam;
696 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
699 EDIT_EM_SetPasswordChar(es, charW);
703 case EM_EMPTYUNDOBUFFER16:
704 case EM_EMPTYUNDOBUFFER:
705 EDIT_EM_EmptyUndoBuffer(es);
708 case EM_GETFIRSTVISIBLELINE16:
709 result = es->y_offset;
711 case EM_GETFIRSTVISIBLELINE:
712 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
715 case EM_SETREADONLY16:
718 SetWindowLongW( hwnd, GWL_STYLE,
719 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
720 es->style |= ES_READONLY;
722 SetWindowLongW( hwnd, GWL_STYLE,
723 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
724 es->style &= ~ES_READONLY;
729 case EM_SETWORDBREAKPROC16:
730 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
732 case EM_SETWORDBREAKPROC:
733 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
736 case EM_GETWORDBREAKPROC16:
737 result = (LRESULT)es->word_break_proc16;
739 case EM_GETWORDBREAKPROC:
740 result = (LRESULT)es->word_break_proc;
743 case EM_GETPASSWORDCHAR16:
744 unicode = FALSE; /* 16-bit message is always ascii */
746 case EM_GETPASSWORDCHAR:
749 result = es->password_char;
752 WCHAR charW = es->password_char;
754 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
760 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
763 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
767 result = MAKELONG(es->left_margin, es->right_margin);
770 case EM_GETLIMITTEXT:
771 result = es->buffer_limit;
775 if ((INT)wParam >= get_text_length(es)) result = -1;
776 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
780 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
783 /* End of the EM_ messages which were in numerical order; what order
784 * are these in? vaguely alphabetical?
788 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
792 result = EDIT_WM_Destroy(es);
797 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
799 if (es->style & ES_MULTILINE)
801 result |= DLGC_WANTALLKEYS;
805 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
807 int vk = (int)((LPMSG)lParam)->wParam;
809 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
811 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
812 result |= DLGC_WANTMESSAGE;
823 strng[0] = wParam >> 8;
824 strng[1] = wParam & 0xff;
825 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
826 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
827 EDIT_WM_Char(es, charW);
840 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
843 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
845 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
846 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
849 EDIT_WM_Char(es, charW);
858 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
862 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
871 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
874 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
878 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
879 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
880 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
882 result = EDIT_WM_Create(es, nameW);
883 HeapFree(GetProcessHeap(), 0, nameW);
892 es->bEnableState = (BOOL) wParam;
893 EDIT_UpdateText(es, NULL, TRUE);
897 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
901 result = (LRESULT)es->font;
905 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
908 case WM_GETTEXTLENGTH:
909 if (unicode) result = get_text_length(es);
910 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
911 NULL, 0, NULL, NULL );
915 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
919 result = EDIT_WM_KeyDown(es, (INT)wParam);
923 result = EDIT_WM_KillFocus(es);
926 case WM_LBUTTONDBLCLK:
927 result = EDIT_WM_LButtonDblClk(es);
931 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
935 result = EDIT_WM_LButtonUp(es);
939 result = EDIT_WM_MButtonDown(es);
943 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
948 EDIT_WM_Paint(es, (HDC)wParam);
956 EDIT_WM_SetFocus(es);
960 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
964 /* FIXME: actually set an internal flag and behave accordingly */
968 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
973 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
976 case WM_STYLECHANGED:
977 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
980 case WM_STYLECHANGING:
981 result = 0; /* See EDIT_WM_StyleChanged */
985 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
993 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
998 int gcWheelDelta = 0;
999 UINT pulScrollLines = 3;
1000 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1002 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1003 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1006 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1007 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1009 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1010 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1011 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1017 /* IME messages to make the edit control IME aware */
1018 case WM_IME_SETCONTEXT:
1021 case WM_IME_STARTCOMPOSITION:
1023 * FIXME in IME: This message is not always sent like it should be
1025 if (es->selection_start != es->selection_end)
1027 static const WCHAR empty_stringW[] = {0};
1028 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1030 es->composition_start = es->selection_end;
1031 es->composition_len = 0;
1034 case WM_IME_COMPOSITION:
1036 int caret_pos = es->selection_end;
1037 if (es->composition_len == 0)
1039 if (es->selection_start != es->selection_end)
1041 static const WCHAR empty_stringW[] = {0};
1042 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1045 es->composition_start = es->selection_end;
1047 EDIT_ImeComposition(hwnd,lParam,es);
1048 EDIT_SetCaretPos(es, caret_pos, es->flags & EF_AFTER_WRAP);
1052 case WM_IME_ENDCOMPOSITION:
1053 es->composition_len= 0;
1056 case WM_IME_COMPOSITIONFULL:
1062 case WM_IME_CONTROL:
1066 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1070 if (es) EDIT_UnlockBuffer(es, FALSE);
1072 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1077 /*********************************************************************
1079 * EditWndProcW (USER32.@)
1081 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1083 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1086 /*********************************************************************
1088 * EditWndProc (USER32.@)
1090 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1092 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1095 /*********************************************************************
1097 * EDIT_BuildLineDefs_ML
1099 * Build linked list of text lines.
1100 * Lines can end with '\0' (last line), a character (if it is wrapped),
1101 * a soft return '\r\r\n' or a hard return '\r\n'
1104 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1108 LPWSTR current_position, cp;
1110 LINEDEF *current_line;
1111 LINEDEF *previous_line;
1112 LINEDEF *start_line;
1113 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1114 INT line_count = es->line_count;
1115 INT orig_net_length;
1118 if (istart == iend && delta == 0)
1121 dc = GetDC(es->hwndSelf);
1123 old_font = SelectObject(dc, es->font);
1125 previous_line = NULL;
1126 current_line = es->first_line_def;
1128 /* Find starting line. istart must lie inside an existing line or
1129 * at the end of buffer */
1131 if (istart < current_line->index + current_line->length ||
1132 current_line->ending == END_0)
1135 previous_line = current_line;
1136 current_line = current_line->next;
1138 } while (current_line);
1140 if (!current_line) /* Error occurred start is not inside previous buffer */
1142 FIXME(" modification occurred outside buffer\n");
1143 ReleaseDC(es->hwndSelf, dc);
1147 /* Remember start of modifications in order to calculate update region */
1148 nstart_line = line_index;
1149 nstart_index = current_line->index;
1151 /* We must start to reformat from the previous line since the modifications
1152 * may have caused the line to wrap upwards. */
1153 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1156 current_line = previous_line;
1158 start_line = current_line;
1160 fw = es->format_rect.right - es->format_rect.left;
1161 current_position = es->text + current_line->index;
1163 if (current_line != start_line)
1165 if (!current_line || current_line->index + delta > current_position - es->text)
1167 /* The buffer has been expanded, create a new line and
1168 insert it into the link list */
1169 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1170 new_line->next = previous_line->next;
1171 previous_line->next = new_line;
1172 current_line = new_line;
1175 else if (current_line->index + delta < current_position - es->text)
1177 /* The previous line merged with this line so we delete this extra entry */
1178 previous_line->next = current_line->next;
1179 HeapFree(GetProcessHeap(), 0, current_line);
1180 current_line = previous_line->next;
1184 else /* current_line->index + delta == current_position */
1186 if (current_position - es->text > iend)
1187 break; /* We reached end of line modifications */
1188 /* else recalulate this line */
1192 current_line->index = current_position - es->text;
1193 orig_net_length = current_line->net_length;
1195 /* Find end of line */
1196 cp = current_position;
1198 if (*cp == '\n') break;
1199 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1204 /* Mark type of line termination */
1206 current_line->ending = END_0;
1207 current_line->net_length = strlenW(current_position);
1208 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1209 current_line->ending = END_SOFT;
1210 current_line->net_length = cp - current_position - 1;
1211 } else if (*cp == '\n') {
1212 current_line->ending = END_RICH;
1213 current_line->net_length = cp - current_position;
1215 current_line->ending = END_HARD;
1216 current_line->net_length = cp - current_position;
1219 /* Calculate line width */
1220 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1221 current_position, current_line->net_length,
1222 es->tabs_count, es->tabs));
1224 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1225 if (!(es->style & ES_AUTOHSCROLL)) {
1226 if (current_line->width > fw) {
1231 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1232 prev + 1, current_line->net_length, WB_RIGHT);
1233 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1234 current_position, next, es->tabs_count, es->tabs));
1235 } while (current_line->width <= fw);
1236 if (!prev) { /* Didn't find a line break so force a break */
1241 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1242 current_position, next, es->tabs_count, es->tabs));
1243 } while (current_line->width <= fw);
1248 /* If the first line we are calculating, wrapped before istart, we must
1249 * adjust istart in order for this to be reflected in the update region. */
1250 if (current_line->index == nstart_index && istart > current_line->index + prev)
1251 istart = current_line->index + prev;
1252 /* else if we are updating the previous line before the first line we
1253 * are re-calculating and it expanded */
1254 else if (current_line == start_line &&
1255 current_line->index != nstart_index && orig_net_length < prev)
1257 /* Line expanded due to an upwards line wrap so we must partially include
1258 * previous line in update region */
1259 nstart_line = line_index;
1260 nstart_index = current_line->index;
1261 istart = current_line->index + orig_net_length;
1264 current_line->net_length = prev;
1265 current_line->ending = END_WRAP;
1266 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1267 current_line->net_length, es->tabs_count, es->tabs));
1269 else if (orig_net_length < current_line->net_length &&
1270 current_line == start_line &&
1271 current_line->index != nstart_index) {
1272 /* The previous line expanded but it's still not as wide as the client rect */
1273 /* The expansion is due to an upwards line wrap so we must partially include
1274 it in the update region */
1275 nstart_line = line_index;
1276 nstart_index = current_line->index;
1277 istart = current_line->index + orig_net_length;
1282 /* Adjust length to include line termination */
1283 switch (current_line->ending) {
1285 current_line->length = current_line->net_length + 3;
1288 current_line->length = current_line->net_length + 1;
1291 current_line->length = current_line->net_length + 2;
1295 current_line->length = current_line->net_length;
1298 es->text_width = max(es->text_width, current_line->width);
1299 current_position += current_line->length;
1300 previous_line = current_line;
1301 current_line = current_line->next;
1303 } while (previous_line->ending != END_0);
1305 /* Finish adjusting line indexes by delta or remove hanging lines */
1306 if (previous_line->ending == END_0)
1308 LINEDEF *pnext = NULL;
1310 previous_line->next = NULL;
1311 while (current_line)
1313 pnext = current_line->next;
1314 HeapFree(GetProcessHeap(), 0, current_line);
1315 current_line = pnext;
1319 else if (delta != 0)
1321 while (current_line)
1323 current_line->index += delta;
1324 current_line = current_line->next;
1328 /* Calculate rest of modification rectangle */
1333 * We calculate two rectangles. One for the first line which may have
1334 * an indent with respect to the format rect. The other is a format-width
1335 * rectangle that spans the rest of the lines that changed or moved.
1337 rc.top = es->format_rect.top + nstart_line * es->line_height -
1338 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1339 rc.bottom = rc.top + es->line_height;
1340 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1341 rc.left = es->format_rect.left;
1343 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1344 es->text + nstart_index, istart - nstart_index,
1345 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1346 rc.right = es->format_rect.right;
1347 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1350 rc.left = es->format_rect.left;
1351 rc.right = es->format_rect.right;
1353 * If lines were added or removed we must re-paint the remainder of the
1354 * lines since the remaining lines were either shifted up or down.
1356 if (line_count < es->line_count) /* We added lines */
1357 rc.bottom = es->line_count * es->line_height;
1358 else if (line_count > es->line_count) /* We removed lines */
1359 rc.bottom = line_count * es->line_height;
1361 rc.bottom = line_index * es->line_height;
1362 rc.bottom += es->format_rect.top;
1363 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1364 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1365 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1366 DeleteObject(tmphrgn);
1370 SelectObject(dc, old_font);
1372 ReleaseDC(es->hwndSelf, dc);
1375 /*********************************************************************
1377 * EDIT_CalcLineWidth_SL
1380 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1387 text = EDIT_GetPasswordPointer_SL(es);
1389 dc = GetDC(es->hwndSelf);
1391 old_font = SelectObject(dc, es->font);
1393 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1396 SelectObject(dc, old_font);
1397 ReleaseDC(es->hwndSelf, dc);
1399 if (es->style & ES_PASSWORD)
1400 HeapFree(GetProcessHeap(), 0, text);
1402 es->text_width = size.cx;
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 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1422 if (es->word_break_proc16) {
1429 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1430 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1431 segptr = WOWGlobalLock16(hglob16);
1432 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1433 args[4] = SELECTOROF(segptr);
1434 args[3] = OFFSETOF(segptr);
1438 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1439 ret = LOWORD(result);
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);
1475 /*********************************************************************
1479 * Beware: This is not the function called on EM_CHARFROMPOS
1480 * The position _can_ be outside the formatting / client
1482 * The return value is only the character index
1485 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1490 INT x_high = 0, x_low = 0;
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 (es->style & ES_RIGHT)
1504 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1505 else if (es->style & ES_CENTER)
1506 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1507 if (x >= line_def->width) {
1509 *after_wrap = (line_def->ending == END_WRAP);
1510 return line_index + line_def->net_length;
1514 *after_wrap = FALSE;
1517 dc = GetDC(es->hwndSelf);
1519 old_font = SelectObject(dc, es->font);
1521 high = line_index + line_def->net_length + 1;
1522 while (low < high - 1)
1524 INT mid = (low + high) / 2;
1525 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1534 if (abs(x_high - x) + 1 <= abs(x_low - x))
1540 *after_wrap = ((index == line_index + line_def->net_length) &&
1541 (line_def->ending == END_WRAP));
1546 *after_wrap = FALSE;
1547 x -= es->format_rect.left;
1549 return es->x_offset;
1553 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1554 if (es->style & ES_RIGHT)
1556 else if (es->style & ES_CENTER)
1560 text = EDIT_GetPasswordPointer_SL(es);
1561 dc = GetDC(es->hwndSelf);
1563 old_font = SelectObject(dc, es->font);
1567 INT high = es->x_offset;
1568 while (low < high - 1)
1570 INT mid = (low + high) / 2;
1571 GetTextExtentPoint32W( dc, text + mid,
1572 es->x_offset - mid, &size );
1581 if (abs(x_high + x) <= abs(x_low + x) + 1)
1588 INT low = es->x_offset;
1589 INT high = get_text_length(es) + 1;
1590 while (low < high - 1)
1592 INT mid = (low + high) / 2;
1593 GetTextExtentPoint32W( dc, text + es->x_offset,
1594 mid - es->x_offset, &size );
1603 if (abs(x_high - x) <= abs(x_low - x) + 1)
1608 if (es->style & ES_PASSWORD)
1609 HeapFree(GetProcessHeap(), 0, text);
1612 SelectObject(dc, old_font);
1613 ReleaseDC(es->hwndSelf, dc);
1618 /*********************************************************************
1622 * adjusts the point to be within the formatting rectangle
1623 * (so CharFromPos returns the nearest _visible_ character)
1626 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1628 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1629 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1633 /*********************************************************************
1637 * Calculates the bounding rectangle for a line from a starting
1638 * column to an ending column.
1641 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1643 INT line_index = EDIT_EM_LineIndex(es, line);
1645 if (es->style & ES_MULTILINE)
1646 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1648 rc->top = es->format_rect.top;
1649 rc->bottom = rc->top + es->line_height;
1650 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1651 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1655 /*********************************************************************
1657 * EDIT_GetPasswordPointer_SL
1659 * note: caller should free the (optionally) allocated buffer
1662 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1664 if (es->style & ES_PASSWORD) {
1665 INT len = get_text_length(es);
1666 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1668 while(len) text[--len] = es->password_char;
1675 /*********************************************************************
1679 * This acts as a LocalLock16(), but it locks only once. This way
1680 * you can call it whenever you like, without unlocking.
1682 * Initially the edit control allocates a HLOCAL32 buffer
1683 * (32 bit linear memory handler). However, 16 bit application
1684 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1685 * handler). From that moment on we have to keep using this 16 bit memory
1686 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1687 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1691 static void EDIT_LockBuffer(EDITSTATE *es)
1693 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1694 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1699 BOOL _16bit = FALSE;
1705 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1706 textA = LocalLock(es->hloc32A);
1707 countA = strlen(textA) + 1;
1711 HANDLE16 oldDS = stack16->ds;
1712 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1713 stack16->ds = hInstance;
1714 textA = MapSL(LocalLock16(es->hloc16));
1715 stack16->ds = oldDS;
1716 countA = strlen(textA) + 1;
1721 ERR("no buffer ... please report\n");
1728 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1729 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1730 if(countW_new > es->buffer_size + 1)
1732 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1733 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1734 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1737 es->hloc32W = hloc32W_new;
1738 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1739 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1742 WARN("FAILED! Will synchronize partially\n");
1746 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1747 es->text = LocalLock(es->hloc32W);
1751 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1754 HANDLE16 oldDS = stack16->ds;
1755 stack16->ds = hInstance;
1756 LocalUnlock16(es->hloc16);
1757 stack16->ds = oldDS;
1760 LocalUnlock(es->hloc32A);
1763 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1768 /*********************************************************************
1770 * EDIT_SL_InvalidateText
1772 * Called from EDIT_InvalidateText().
1773 * Does the job for single-line controls only.
1776 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1781 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1782 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1783 EDIT_UpdateText(es, &rc, TRUE);
1787 /*********************************************************************
1789 * EDIT_ML_InvalidateText
1791 * Called from EDIT_InvalidateText().
1792 * Does the job for multi-line controls only.
1795 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1797 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1798 INT sl = EDIT_EM_LineFromChar(es, start);
1799 INT el = EDIT_EM_LineFromChar(es, end);
1808 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1811 sc = start - EDIT_EM_LineIndex(es, sl);
1812 ec = end - EDIT_EM_LineIndex(es, el);
1813 if (sl < es->y_offset) {
1817 if (el > es->y_offset + vlc) {
1818 el = es->y_offset + vlc;
1819 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1821 GetClientRect(es->hwndSelf, &rc1);
1822 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1824 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1825 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1826 EDIT_UpdateText(es, &rcUpdate, TRUE);
1828 EDIT_GetLineRect(es, sl, sc,
1829 EDIT_EM_LineLength(es,
1830 EDIT_EM_LineIndex(es, sl)),
1832 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1833 EDIT_UpdateText(es, &rcUpdate, TRUE);
1834 for (l = sl + 1 ; l < el ; l++) {
1835 EDIT_GetLineRect(es, l, 0,
1836 EDIT_EM_LineLength(es,
1837 EDIT_EM_LineIndex(es, l)),
1839 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1840 EDIT_UpdateText(es, &rcUpdate, TRUE);
1842 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1843 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1844 EDIT_UpdateText(es, &rcUpdate, TRUE);
1849 /*********************************************************************
1851 * EDIT_InvalidateText
1853 * Invalidate the text from offset start up to, but not including,
1854 * offset end. Useful for (re)painting the selection.
1855 * Regions outside the linewidth are not invalidated.
1856 * end == -1 means end == TextLength.
1857 * start and end need not be ordered.
1860 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1866 end = get_text_length(es);
1874 if (es->style & ES_MULTILINE)
1875 EDIT_ML_InvalidateText(es, start, end);
1877 EDIT_SL_InvalidateText(es, start, end);
1881 /*********************************************************************
1885 * Try to fit size + 1 characters in the buffer.
1887 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1891 if (size <= es->buffer_size)
1894 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1896 /* Force edit to unlock it's buffer. es->text now NULL */
1897 EDIT_UnlockBuffer(es, TRUE);
1900 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1901 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1902 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1903 es->hloc32W = hNew32W;
1904 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1908 EDIT_LockBuffer(es);
1910 if (es->buffer_size < size) {
1911 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1912 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1915 TRACE("We now have %d+1\n", es->buffer_size);
1921 /*********************************************************************
1925 * Try to fit size + 1 bytes in the undo buffer.
1928 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1932 if (size <= es->undo_buffer_size)
1935 TRACE("trying to ReAlloc to %d+1\n", size);
1937 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1938 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1939 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1944 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1950 /*********************************************************************
1955 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1957 INT e = es->selection_end;
1961 if ((es->style & ES_MULTILINE) && e &&
1962 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1964 if (e && (es->text[e - 1] == '\r'))
1968 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1969 EDIT_EM_ScrollCaret(es);
1973 /*********************************************************************
1977 * Only for multi line controls
1978 * Move the caret one line down, on a column with the nearest
1979 * x coordinate on the screen (might be a different column).
1982 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1984 INT s = es->selection_start;
1985 INT e = es->selection_end;
1986 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1987 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1988 INT x = (short)LOWORD(pos);
1989 INT y = (short)HIWORD(pos);
1991 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1994 EDIT_EM_SetSel(es, s, e, after_wrap);
1995 EDIT_EM_ScrollCaret(es);
1999 /*********************************************************************
2004 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
2006 BOOL after_wrap = FALSE;
2009 /* Pass a high value in x to make sure of receiving the end of the line */
2010 if (es->style & ES_MULTILINE)
2011 e = EDIT_CharFromPos(es, 0x3fffffff,
2012 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2014 e = get_text_length(es);
2015 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2016 EDIT_EM_ScrollCaret(es);
2020 /*********************************************************************
2025 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2027 INT e = es->selection_end;
2031 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2032 if (es->text[e] == '\n')
2034 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2038 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2039 EDIT_EM_ScrollCaret(es);
2043 /*********************************************************************
2047 * Home key: move to beginning of line.
2050 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2054 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2055 if (es->style & ES_MULTILINE)
2056 e = EDIT_CharFromPos(es, -es->x_offset,
2057 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2060 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2061 EDIT_EM_ScrollCaret(es);
2065 /*********************************************************************
2067 * EDIT_MovePageDown_ML
2069 * Only for multi line controls
2070 * Move the caret one page down, on a column with the nearest
2071 * x coordinate on the screen (might be a different column).
2074 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2076 INT s = es->selection_start;
2077 INT e = es->selection_end;
2078 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2079 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2080 INT x = (short)LOWORD(pos);
2081 INT y = (short)HIWORD(pos);
2083 e = EDIT_CharFromPos(es, x,
2084 y + (es->format_rect.bottom - es->format_rect.top),
2088 EDIT_EM_SetSel(es, s, e, after_wrap);
2089 EDIT_EM_ScrollCaret(es);
2093 /*********************************************************************
2095 * EDIT_MovePageUp_ML
2097 * Only for multi line controls
2098 * Move the caret one page up, on a column with the nearest
2099 * x coordinate on the screen (might be a different column).
2102 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2104 INT s = es->selection_start;
2105 INT e = es->selection_end;
2106 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2107 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2108 INT x = (short)LOWORD(pos);
2109 INT y = (short)HIWORD(pos);
2111 e = EDIT_CharFromPos(es, x,
2112 y - (es->format_rect.bottom - es->format_rect.top),
2116 EDIT_EM_SetSel(es, s, e, after_wrap);
2117 EDIT_EM_ScrollCaret(es);
2121 /*********************************************************************
2125 * Only for multi line controls
2126 * Move the caret one line up, on a column with the nearest
2127 * x coordinate on the screen (might be a different column).
2130 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2132 INT s = es->selection_start;
2133 INT e = es->selection_end;
2134 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2135 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2136 INT x = (short)LOWORD(pos);
2137 INT y = (short)HIWORD(pos);
2139 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2142 EDIT_EM_SetSel(es, s, e, after_wrap);
2143 EDIT_EM_ScrollCaret(es);
2147 /*********************************************************************
2149 * EDIT_MoveWordBackward
2152 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2154 INT s = es->selection_start;
2155 INT e = es->selection_end;
2160 l = EDIT_EM_LineFromChar(es, e);
2161 ll = EDIT_EM_LineLength(es, e);
2162 li = EDIT_EM_LineIndex(es, l);
2165 li = EDIT_EM_LineIndex(es, l - 1);
2166 e = li + EDIT_EM_LineLength(es, li);
2169 e = li + (INT)EDIT_CallWordBreakProc(es,
2170 li, e - li, ll, WB_LEFT);
2174 EDIT_EM_SetSel(es, s, e, FALSE);
2175 EDIT_EM_ScrollCaret(es);
2179 /*********************************************************************
2181 * EDIT_MoveWordForward
2184 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2186 INT s = es->selection_start;
2187 INT e = es->selection_end;
2192 l = EDIT_EM_LineFromChar(es, e);
2193 ll = EDIT_EM_LineLength(es, e);
2194 li = EDIT_EM_LineIndex(es, l);
2196 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2197 e = EDIT_EM_LineIndex(es, l + 1);
2199 e = li + EDIT_CallWordBreakProc(es,
2200 li, e - li + 1, ll, WB_RIGHT);
2204 EDIT_EM_SetSel(es, s, e, FALSE);
2205 EDIT_EM_ScrollCaret(es);
2209 /*********************************************************************
2214 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2216 INT s = es->selection_start;
2217 INT e = es->selection_end;
2224 if (es->style & ES_MULTILINE) {
2225 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2226 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2231 TRACE("line=%d\n", line);
2233 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2234 x = (short)LOWORD(pos);
2235 y = (short)HIWORD(pos);
2236 li = EDIT_EM_LineIndex(es, line);
2237 ll = EDIT_EM_LineLength(es, li);
2238 s = min(es->selection_start, es->selection_end);
2239 e = max(es->selection_start, es->selection_end);
2240 s = min(li + ll, max(li, s));
2241 e = min(li + ll, max(li, e));
2242 if (rev && (s != e) &&
2243 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2244 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2245 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2246 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2248 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2252 /*********************************************************************
2257 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2261 LOGFONTW underline_font;
2262 HFONT hUnderline = 0;
2271 BkMode = GetBkMode(dc);
2272 BkColor = GetBkColor(dc);
2273 TextColor = GetTextColor(dc);
2275 if (es->composition_len == 0)
2277 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2278 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2279 SetBkMode( dc, OPAQUE);
2283 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2284 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2285 underline_font.lfUnderline = TRUE;
2286 hUnderline = CreateFontIndirectW(&underline_font);
2287 old_font = SelectObject(dc,hUnderline);
2290 li = EDIT_EM_LineIndex(es, line);
2291 if (es->style & ES_MULTILINE) {
2292 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2293 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2295 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2296 TextOutW(dc, x, y, text + li + col, count);
2297 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2299 if (es->style & ES_PASSWORD)
2300 HeapFree(GetProcessHeap(), 0, text);
2303 if (es->composition_len == 0)
2305 SetBkColor(dc, BkColor);
2306 SetTextColor(dc, TextColor);
2307 SetBkMode( dc, BkMode);
2312 SelectObject(dc,old_font);
2314 DeleteObject(hUnderline);
2321 /*********************************************************************
2326 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2329 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2330 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2331 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2335 /*********************************************************************
2337 * EDIT_AdjustFormatRect
2339 * Adjusts the format rectangle for the current font and the
2340 * current client rectangle.
2343 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2347 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2348 if (es->style & ES_MULTILINE)
2350 INT fw, vlc, max_x_offset, max_y_offset;
2352 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2353 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2355 /* correct es->x_offset */
2356 fw = es->format_rect.right - es->format_rect.left;
2357 max_x_offset = es->text_width - fw;
2358 if(max_x_offset < 0) max_x_offset = 0;
2359 if(es->x_offset > max_x_offset)
2360 es->x_offset = max_x_offset;
2362 /* correct es->y_offset */
2363 max_y_offset = es->line_count - vlc;
2364 if(max_y_offset < 0) max_y_offset = 0;
2365 if(es->y_offset > max_y_offset)
2366 es->y_offset = max_y_offset;
2368 /* force scroll info update */
2369 EDIT_UpdateScrollInfo(es);
2372 /* Windows doesn't care to fix text placement for SL controls */
2373 es->format_rect.bottom = es->format_rect.top + es->line_height;
2375 /* Always stay within the client area */
2376 GetClientRect(es->hwndSelf, &ClientRect);
2377 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2379 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2380 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2382 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2386 /*********************************************************************
2390 * note: this is not (exactly) the handler called on EM_SETRECTNP
2391 * it is also used to set the rect of a single line control
2394 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2398 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2400 CopyRect(&es->format_rect, rc);
2402 if (ExStyle & WS_EX_CLIENTEDGE) {
2403 es->format_rect.left++;
2404 es->format_rect.right--;
2406 if (es->format_rect.bottom - es->format_rect.top
2407 >= es->line_height + 2)
2409 es->format_rect.top++;
2410 es->format_rect.bottom--;
2413 else if (es->style & WS_BORDER) {
2414 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2415 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2416 es->format_rect.left += bw;
2417 es->format_rect.right -= bw;
2418 if (es->format_rect.bottom - es->format_rect.top
2419 >= es->line_height + 2 * bh)
2421 es->format_rect.top += bh;
2422 es->format_rect.bottom -= bh;
2426 es->format_rect.left += es->left_margin;
2427 es->format_rect.right -= es->right_margin;
2428 EDIT_AdjustFormatRect(es);
2432 /*********************************************************************
2437 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2440 /* Edit window might be already destroyed */
2441 if(!IsWindow(es->hwndSelf))
2443 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2447 if (!es->lock_count) {
2448 ERR("lock_count == 0 ... please report\n");
2452 ERR("es->text == 0 ... please report\n");
2456 if (force || (es->lock_count == 1)) {
2460 UINT countW = get_text_length(es) + 1;
2461 STACK16FRAME* stack16 = NULL;
2466 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2467 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2468 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2469 countA = LocalSize(es->hloc32A);
2470 if(countA_new > countA)
2473 UINT alloc_size = ROUND_TO_GROW(countA_new);
2474 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2475 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2478 es->hloc32A = hloc32A_new;
2479 countA = LocalSize(hloc32A_new);
2480 TRACE("Real new size %d bytes\n", countA);
2483 WARN("FAILED! Will synchronize partially\n");
2485 textA = LocalLock(es->hloc32A);
2489 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2491 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2492 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2494 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2495 oldDS = stack16->ds;
2496 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2498 countA = LocalSize16(es->hloc16);
2499 if(countA_new > countA)
2501 HLOCAL16 hloc16_new;
2502 UINT alloc_size = ROUND_TO_GROW(countA_new);
2503 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2504 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2507 es->hloc16 = hloc16_new;
2508 countA = LocalSize16(hloc16_new);
2509 TRACE("Real new size %d bytes\n", countA);
2512 WARN("FAILED! Will synchronize partially\n");
2514 textA = MapSL(LocalLock16(es->hloc16));
2519 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2521 LocalUnlock16(es->hloc16);
2523 LocalUnlock(es->hloc32A);
2526 if (stack16) stack16->ds = oldDS;
2527 LocalUnlock(es->hloc32W);
2531 ERR("no buffer ... please report\n");
2539 /*********************************************************************
2541 * EDIT_UpdateScrollInfo
2544 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2546 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2549 si.cbSize = sizeof(SCROLLINFO);
2550 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2552 si.nMax = es->line_count - 1;
2553 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2554 si.nPos = es->y_offset;
2555 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2556 si.nMin, si.nMax, si.nPage, si.nPos);
2557 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2560 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2563 si.cbSize = sizeof(SCROLLINFO);
2564 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2566 si.nMax = es->text_width - 1;
2567 si.nPage = es->format_rect.right - es->format_rect.left;
2568 si.nPos = es->x_offset;
2569 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2570 si.nMin, si.nMax, si.nPage, si.nPos);
2571 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2575 /*********************************************************************
2577 * EDIT_WordBreakProc
2579 * Find the beginning of words.
2580 * Note: unlike the specs for a WordBreakProc, this function only
2581 * allows to be called without linebreaks between s[0] up to
2582 * s[count - 1]. Remember it is only called
2583 * internally, so we can decide this for ourselves.
2586 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2590 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2600 if (s[index] == ' ') {
2601 while (index && (s[index] == ' '))
2604 while (index && (s[index] != ' '))
2606 if (s[index] == ' ')
2610 while (index && (s[index] != ' '))
2612 if (s[index] == ' ')
2622 if (s[index] == ' ')
2623 while ((index < count) && (s[index] == ' ')) index++;
2625 while (s[index] && (s[index] != ' ') && (index < count))
2627 while ((s[index] == ' ') && (index < count)) index++;
2631 case WB_ISDELIMITER:
2632 ret = (s[index] == ' ');
2635 ERR("unknown action code, please report !\n");
2642 /*********************************************************************
2646 * returns line number (not index) in high-order word of result.
2647 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2648 * to Richedit, not to the edit control. Original documentation is valid.
2649 * FIXME: do the specs mean to return -1 if outside client area or
2650 * if outside formatting rectangle ???
2653 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2661 GetClientRect(es->hwndSelf, &rc);
2662 if (!PtInRect(&rc, pt))
2665 index = EDIT_CharFromPos(es, x, y, NULL);
2666 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2670 /*********************************************************************
2674 * Enable or disable soft breaks.
2676 * This means: insert or remove the soft linebreak character (\r\r\n).
2677 * Take care to check if the text still fits the buffer after insertion.
2678 * If not, notify with EN_ERRSPACE.
2681 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2683 es->flags &= ~EF_USE_SOFTBRK;
2685 es->flags |= EF_USE_SOFTBRK;
2686 FIXME("soft break enabled, not implemented\n");
2692 /*********************************************************************
2696 * Hopefully this won't fire back at us.
2697 * We always start with a fixed buffer in the local heap.
2698 * Despite of the documentation says that the local heap is used
2699 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2700 * buffer on the local heap.
2703 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2707 if (!(es->style & ES_MULTILINE))
2711 hLocal = es->hloc32W;
2717 UINT countA, alloc_size;
2718 TRACE("Allocating 32-bit ANSI alias buffer\n");
2719 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2720 alloc_size = ROUND_TO_GROW(countA);
2721 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2723 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2726 textA = LocalLock(es->hloc32A);
2727 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2728 LocalUnlock(es->hloc32A);
2730 hLocal = es->hloc32A;
2733 es->flags |= EF_APP_HAS_HANDLE;
2734 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2739 /*********************************************************************
2743 * Hopefully this won't fire back at us.
2744 * We always start with a buffer in 32 bit linear memory.
2745 * However, with this message a 16 bit application requests
2746 * a handle of 16 bit local heap memory, where it expects to find
2748 * It's a pitty that from this moment on we have to use this
2749 * local heap, because applications may rely on the handle
2752 * In this function we'll try to switch to local heap.
2754 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2757 UINT countA, alloc_size;
2758 STACK16FRAME* stack16;
2761 if (!(es->style & ES_MULTILINE))
2767 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2768 oldDS = stack16->ds;
2769 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2771 if (!LocalHeapSize16()) {
2773 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2774 ERR("could not initialize local heap\n");
2777 TRACE("local heap initialized\n");
2780 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2781 alloc_size = ROUND_TO_GROW(countA);
2783 TRACE("Allocating 16-bit ANSI alias buffer\n");
2784 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2785 ERR("could not allocate new 16 bit buffer\n");
2789 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2790 ERR("could not lock new 16 bit buffer\n");
2791 LocalFree16(es->hloc16);
2796 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2797 LocalUnlock16(es->hloc16);
2798 es->flags |= EF_APP_HAS_HANDLE;
2800 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2803 stack16->ds = oldDS;
2808 /*********************************************************************
2813 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2816 INT line_len, dst_len;
2819 if (es->style & ES_MULTILINE) {
2820 if (line >= es->line_count)
2824 i = EDIT_EM_LineIndex(es, line);
2826 line_len = EDIT_EM_LineLength(es, i);
2827 dst_len = *(WORD *)dst;
2830 if(dst_len <= line_len)
2832 memcpy(dst, src, dst_len * sizeof(WCHAR));
2835 else /* Append 0 if enough space */
2837 memcpy(dst, src, line_len * sizeof(WCHAR));
2844 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2845 if(!ret && line_len) /* Insufficient buffer size */
2847 if(ret < dst_len) /* Append 0 if enough space */
2848 ((LPSTR)dst)[ret] = 0;
2854 /*********************************************************************
2859 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2861 UINT s = es->selection_start;
2862 UINT e = es->selection_end;
2869 return MAKELONG(s, e);
2873 /*********************************************************************
2877 * FIXME: is this right ? (or should it be only VSCROLL)
2878 * (and maybe only for edit controls that really have their
2879 * own scrollbars) (and maybe only for multiline controls ?)
2880 * All in all: very poorly documented
2883 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2885 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2886 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2890 /*********************************************************************
2895 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2900 if (!(es->style & ES_MULTILINE))
2902 if (index > (INT)get_text_length(es))
2903 return es->line_count - 1;
2905 index = min(es->selection_start, es->selection_end);
2908 line_def = es->first_line_def;
2909 index -= line_def->length;
2910 while ((index >= 0) && line_def->next) {
2912 line_def = line_def->next;
2913 index -= line_def->length;
2919 /*********************************************************************
2924 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2929 if (!(es->style & ES_MULTILINE))
2931 if (line >= es->line_count)
2935 line_def = es->first_line_def;
2937 INT index = es->selection_end - line_def->length;
2938 while ((index >= 0) && line_def->next) {
2939 line_index += line_def->length;
2940 line_def = line_def->next;
2941 index -= line_def->length;
2945 line_index += line_def->length;
2946 line_def = line_def->next;
2954 /*********************************************************************
2959 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2963 if (!(es->style & ES_MULTILINE))
2964 return get_text_length(es);
2967 /* get the number of remaining non-selected chars of selected lines */
2968 INT32 l; /* line number */
2969 INT32 li; /* index of first char in line */
2971 l = EDIT_EM_LineFromChar(es, es->selection_start);
2972 /* # chars before start of selection area */
2973 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2974 l = EDIT_EM_LineFromChar(es, es->selection_end);
2975 /* # chars after end of selection */
2976 li = EDIT_EM_LineIndex(es, l);
2977 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2980 line_def = es->first_line_def;
2981 index -= line_def->length;
2982 while ((index >= 0) && line_def->next) {
2983 line_def = line_def->next;
2984 index -= line_def->length;
2986 return line_def->net_length;
2990 /*********************************************************************
2994 * NOTE: dx is in average character widths, dy - in lines;
2997 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2999 if (!(es->style & ES_MULTILINE))
3002 dx *= es->char_width;
3003 return EDIT_EM_LineScroll_internal(es, dx, dy);
3006 /*********************************************************************
3008 * EDIT_EM_LineScroll_internal
3010 * Version of EDIT_EM_LineScroll for internal use.
3011 * It doesn't refuse if ES_MULTILINE is set and assumes that
3012 * dx is in pixels, dy - in lines.
3015 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3018 INT x_offset_in_pixels;
3019 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3022 if (es->style & ES_MULTILINE)
3024 x_offset_in_pixels = es->x_offset;
3029 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3032 if (-dx > x_offset_in_pixels)
3033 dx = -x_offset_in_pixels;
3034 if (dx > es->text_width - x_offset_in_pixels)
3035 dx = es->text_width - x_offset_in_pixels;
3036 nyoff = max(0, es->y_offset + dy);
3037 if (nyoff >= es->line_count - lines_per_page)
3038 nyoff = max(0, es->line_count - lines_per_page);
3039 dy = (es->y_offset - nyoff) * es->line_height;
3044 es->y_offset = nyoff;
3045 if(es->style & ES_MULTILINE)
3048 es->x_offset += dx / es->char_width;
3050 GetClientRect(es->hwndSelf, &rc1);
3051 IntersectRect(&rc, &rc1, &es->format_rect);
3052 ScrollWindowEx(es->hwndSelf, -dx, dy,
3053 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3054 /* force scroll info update */
3055 EDIT_UpdateScrollInfo(es);
3057 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3058 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3059 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3060 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3065 /*********************************************************************
3070 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3072 INT len = get_text_length(es);
3085 index = min(index, len);
3086 dc = GetDC(es->hwndSelf);
3088 old_font = SelectObject(dc, es->font);
3089 if (es->style & ES_MULTILINE) {
3090 l = EDIT_EM_LineFromChar(es, index);
3091 y = (l - es->y_offset) * es->line_height;
3092 li = EDIT_EM_LineIndex(es, l);
3093 if (after_wrap && (li == index) && l) {
3095 line_def = es->first_line_def;
3097 line_def = line_def->next;
3100 if (line_def->ending == END_WRAP) {
3102 y -= es->line_height;
3103 li = EDIT_EM_LineIndex(es, l);
3107 line_def = es->first_line_def;
3108 while (line_def->index != li)
3109 line_def = line_def->next;
3111 ll = line_def->net_length;
3112 lw = line_def->width;
3114 w = es->format_rect.right - es->format_rect.left;
3115 if (es->style & ES_RIGHT)
3117 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3118 es->tabs_count, es->tabs)) - es->x_offset;
3121 else if (es->style & ES_CENTER)
3123 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3124 es->tabs_count, es->tabs)) - es->x_offset;
3129 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3130 es->tabs_count, es->tabs)) - es->x_offset;
3133 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3134 if (index < es->x_offset) {
3135 GetTextExtentPoint32W(dc, text + index,
3136 es->x_offset - index, &size);
3139 GetTextExtentPoint32W(dc, text + es->x_offset,
3140 index - es->x_offset, &size);
3143 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3145 w = es->format_rect.right - es->format_rect.left;
3146 if (w > es->text_width)
3148 if (es->style & ES_RIGHT)
3149 x += w - es->text_width;
3150 else if (es->style & ES_CENTER)
3151 x += (w - es->text_width) / 2;
3156 if (es->style & ES_PASSWORD)
3157 HeapFree(GetProcessHeap(), 0, text);
3159 x += es->format_rect.left;
3160 y += es->format_rect.top;
3162 SelectObject(dc, old_font);
3163 ReleaseDC(es->hwndSelf, dc);
3164 return MAKELONG((INT16)x, (INT16)y);
3168 /*********************************************************************
3172 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3175 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3177 UINT strl = strlenW(lpsz_replace);
3178 UINT tl = get_text_length(es);
3189 TRACE("%s, can_undo %d, send_update %d\n",
3190 debugstr_w(lpsz_replace), can_undo, send_update);
3192 s = es->selection_start;
3193 e = es->selection_end;
3195 if ((s == e) && !strl)
3200 size = tl - (e - s) + strl;
3204 /* Issue the EN_MAXTEXT notification and continue with replacing text
3205 * such that buffer limit is honored. */
3206 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3207 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3208 strl = es->buffer_limit - (tl - (e-s));
3211 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3215 /* there is something to be deleted */
3216 TRACE("deleting stuff.\n");
3218 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3220 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3221 buf[bufl] = 0; /* ensure 0 termination */
3223 strcpyW(es->text + s, es->text + e);
3224 text_buffer_changed(es);
3227 /* there is an insertion */
3228 tl = get_text_length(es);
3229 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));
3230 for (p = es->text + tl ; p >= es->text + s ; p--)
3232 for (i = 0 , p = es->text + s ; i < strl ; i++)
3233 p[i] = lpsz_replace[i];
3234 if(es->style & ES_UPPERCASE)
3235 CharUpperBuffW(p, strl);
3236 else if(es->style & ES_LOWERCASE)
3237 CharLowerBuffW(p, strl);
3238 text_buffer_changed(es);
3240 if (es->style & ES_MULTILINE)
3242 INT st = min(es->selection_start, es->selection_end);
3243 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3245 hrgn = CreateRectRgn(0, 0, 0, 0);
3246 EDIT_BuildLineDefs_ML(es, st, st + strl,
3247 strl - abs(es->selection_end - es->selection_start), hrgn);
3248 /* if text is too long undo all changes */
3249 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3251 strcpyW(es->text + e, es->text + e + strl);
3253 for (i = 0 , p = es->text ; i < e - s ; i++)
3255 text_buffer_changed(es);
3256 EDIT_BuildLineDefs_ML(es, s, e,
3257 abs(es->selection_end - es->selection_start) - strl, hrgn);
3260 hrgn = CreateRectRgn(0, 0, 0, 0);
3261 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3265 INT fw = es->format_rect.right - es->format_rect.left;
3266 EDIT_CalcLineWidth_SL(es);
3267 /* remove chars that don't fit */
3268 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3269 while ((es->text_width > fw) && s + strl >= s) {
3270 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3272 EDIT_CalcLineWidth_SL(es);
3274 text_buffer_changed(es);
3275 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3281 utl = strlenW(es->undo_text);
3282 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3283 /* undo-buffer is extended to the right */
3284 EDIT_MakeUndoFit(es, utl + e - s);
3285 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3286 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3287 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3288 /* undo-buffer is extended to the left */
3289 EDIT_MakeUndoFit(es, utl + e - s);
3290 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3292 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3294 es->undo_position = s;
3296 /* new undo-buffer */
3297 EDIT_MakeUndoFit(es, e - s);
3298 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3299 es->undo_text[e - s] = 0; /* ensure 0 termination */
3300 es->undo_position = s;
3302 /* any deletion makes the old insertion-undo invalid */
3303 es->undo_insert_count = 0;
3305 EDIT_EM_EmptyUndoBuffer(es);
3309 if ((s == es->undo_position) ||
3310 ((es->undo_insert_count) &&
3311 (s == es->undo_position + es->undo_insert_count)))
3313 * insertion is new and at delete position or
3314 * an extension to either left or right
3316 es->undo_insert_count += strl;
3318 /* new insertion undo */
3319 es->undo_position = s;
3320 es->undo_insert_count = strl;
3321 /* new insertion makes old delete-buffer invalid */
3322 *es->undo_text = '\0';
3325 EDIT_EM_EmptyUndoBuffer(es);
3329 HeapFree(GetProcessHeap(), 0, buf);
3333 /* If text has been deleted and we're right or center aligned then scroll rightward */
3334 if (es->style & (ES_RIGHT | ES_CENTER))
3336 INT delta = strl - abs(es->selection_end - es->selection_start);
3338 if (delta < 0 && es->x_offset)
3340 if (abs(delta) > es->x_offset)
3343 es->x_offset += delta;
3347 EDIT_EM_SetSel(es, s, s, FALSE);
3348 es->flags |= EF_MODIFIED;
3349 if (send_update) es->flags |= EF_UPDATE;
3352 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3356 EDIT_UpdateText(es, NULL, TRUE);
3358 EDIT_EM_ScrollCaret(es);
3360 /* force scroll info update */
3361 EDIT_UpdateScrollInfo(es);
3364 if(send_update || (es->flags & EF_UPDATE))
3366 es->flags &= ~EF_UPDATE;
3367 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3372 /*********************************************************************
3377 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3381 if (!(es->style & ES_MULTILINE))
3382 return (LRESULT)FALSE;
3392 if (es->y_offset < es->line_count - 1)
3397 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3400 if (es->y_offset < es->line_count - 1)
3401 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3404 return (LRESULT)FALSE;
3407 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3408 /* check if we are going to move too far */
3409 if(es->y_offset + dy > es->line_count - vlc)
3410 dy = es->line_count - vlc - es->y_offset;
3412 /* Notification is done in EDIT_EM_LineScroll */
3414 EDIT_EM_LineScroll(es, 0, dy);
3416 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3420 /*********************************************************************
3425 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3427 if (es->style & ES_MULTILINE) {
3432 INT cw = es->char_width;
3437 l = EDIT_EM_LineFromChar(es, es->selection_end);
3438 li = EDIT_EM_LineIndex(es, l);
3439 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3440 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3441 if (l >= es->y_offset + vlc)
3442 dy = l - vlc + 1 - es->y_offset;
3443 if (l < es->y_offset)
3444 dy = l - es->y_offset;
3445 ww = es->format_rect.right - es->format_rect.left;
3446 if (x < es->format_rect.left)
3447 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3448 if (x > es->format_rect.right)
3449 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3450 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3452 /* check if we are going to move too far */
3453 if(es->x_offset + dx + ww > es->text_width)
3454 dx = es->text_width - ww - es->x_offset;
3455 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3456 EDIT_EM_LineScroll_internal(es, dx, dy);
3463 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3464 format_width = es->format_rect.right - es->format_rect.left;
3465 if (x < es->format_rect.left) {
3466 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3469 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3470 } while ((x < goal) && es->x_offset);
3471 /* FIXME: use ScrollWindow() somehow to improve performance */
3472 EDIT_UpdateText(es, NULL, TRUE);
3473 } else if (x > es->format_rect.right) {
3475 INT len = get_text_length(es);
3476 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3479 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3480 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3481 } while ((x > goal) && (x_last > es->format_rect.right));
3482 /* FIXME: use ScrollWindow() somehow to improve performance */
3483 EDIT_UpdateText(es, NULL, TRUE);
3487 if(es->flags & EF_FOCUSED)
3488 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3492 /*********************************************************************
3496 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3499 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3501 if (!(es->style & ES_MULTILINE))
3505 WARN("called with NULL handle\n");
3509 EDIT_UnlockBuffer(es, TRUE);
3513 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3514 HANDLE16 oldDS = stack16->ds;
3516 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3517 LocalFree16(es->hloc16);
3518 stack16->ds = oldDS;
3526 LocalFree(es->hloc32A);
3538 countA = LocalSize(hloc);
3539 textA = LocalLock(hloc);
3540 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3541 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3543 ERR("Could not allocate new unicode buffer\n");
3546 textW = LocalLock(hloc32W_new);
3547 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3548 LocalUnlock(hloc32W_new);
3552 LocalFree(es->hloc32W);
3554 es->hloc32W = hloc32W_new;
3558 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3560 es->flags |= EF_APP_HAS_HANDLE;
3561 EDIT_LockBuffer(es);
3563 es->x_offset = es->y_offset = 0;
3564 es->selection_start = es->selection_end = 0;
3565 EDIT_EM_EmptyUndoBuffer(es);
3566 es->flags &= ~EF_MODIFIED;
3567 es->flags &= ~EF_UPDATE;
3568 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3569 EDIT_UpdateText(es, NULL, TRUE);
3570 EDIT_EM_ScrollCaret(es);
3571 /* force scroll info update */
3572 EDIT_UpdateScrollInfo(es);
3576 /*********************************************************************
3580 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3583 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3585 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3586 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3587 HANDLE16 oldDS = stack16->ds;
3593 if (!(es->style & ES_MULTILINE))
3597 WARN("called with NULL handle\n");
3601 EDIT_UnlockBuffer(es, TRUE);
3605 LocalFree(es->hloc32A);
3609 stack16->ds = hInstance;
3610 countA = LocalSize16(hloc);
3611 textA = MapSL(LocalLock16(hloc));
3612 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3613 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3615 ERR("Could not allocate new unicode buffer\n");
3618 textW = LocalLock(hloc32W_new);
3619 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3620 LocalUnlock(hloc32W_new);
3621 LocalUnlock16(hloc);
3622 stack16->ds = oldDS;
3625 LocalFree(es->hloc32W);
3627 es->hloc32W = hloc32W_new;
3630 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3632 es->flags |= EF_APP_HAS_HANDLE;
3633 EDIT_LockBuffer(es);
3635 es->x_offset = es->y_offset = 0;
3636 es->selection_start = es->selection_end = 0;
3637 EDIT_EM_EmptyUndoBuffer(es);
3638 es->flags &= ~EF_MODIFIED;
3639 es->flags &= ~EF_UPDATE;
3640 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3641 EDIT_UpdateText(es, NULL, TRUE);
3642 EDIT_EM_ScrollCaret(es);
3643 /* force scroll info update */
3644 EDIT_UpdateScrollInfo(es);
3648 /*********************************************************************
3652 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3653 * However, the windows version is not complied to yet in all of edit.c
3655 * Additionally as the wrapper for RichEdit controls we need larger buffers
3656 * at present -1 will represent nolimit
3658 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3660 if (limit == 0xFFFFFFFF)
3661 es->buffer_limit = -1;
3662 else if (es->style & ES_MULTILINE) {
3664 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3666 es->buffer_limit = BUFLIMIT_MULTI;
3669 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3671 es->buffer_limit = BUFLIMIT_SINGLE;
3676 /*********************************************************************
3680 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3681 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3682 * margin according to the textmetrics of the current font.
3684 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3685 * of the char's width as the margin, but this is not how Windows handles this.
3686 * For all other fonts Windows sets the margins to zero.
3688 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3689 * edit control is equal to or larger than a certain size.
3690 * Interestingly if one subtracts both the left and right margins from
3691 * this size one always seems to get an even number. The extents of
3692 * the (four character) string "'**'" match this quite closely, so
3693 * we'll use this until we come up with a better idea.
3695 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3697 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3700 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3701 return sz.cx + left + right;
3704 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3705 WORD left, WORD right, BOOL repaint)
3708 INT default_left_margin = 0; /* in pixels */
3709 INT default_right_margin = 0; /* in pixels */
3711 /* Set the default margins depending on the font */
3712 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3713 HDC dc = GetDC(es->hwndSelf);
3714 HFONT old_font = SelectObject(dc, es->font);
3715 GetTextMetricsW(dc, &tm);
3716 /* The default margins are only non zero for TrueType or Vector fonts */
3717 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3720 /* This must be calculated more exactly! But how? */
3721 default_left_margin = tm.tmAveCharWidth / 2;
3722 default_right_margin = tm.tmAveCharWidth / 2;
3723 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3724 GetClientRect(es->hwndSelf, &rc);
3725 if(rc.right - rc.left < min_size) {
3726 default_left_margin = es->left_margin;
3727 default_right_margin = es->right_margin;
3730 SelectObject(dc, old_font);
3731 ReleaseDC(es->hwndSelf, dc);
3734 if (action & EC_LEFTMARGIN) {
3735 es->format_rect.left -= es->left_margin;
3736 if (left != EC_USEFONTINFO)
3737 es->left_margin = left;
3739 es->left_margin = default_left_margin;
3740 es->format_rect.left += es->left_margin;
3743 if (action & EC_RIGHTMARGIN) {
3744 es->format_rect.right += es->right_margin;
3745 if (right != EC_USEFONTINFO)
3746 es->right_margin = right;
3748 es->right_margin = default_right_margin;
3749 es->format_rect.right -= es->right_margin;
3752 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3753 EDIT_AdjustFormatRect(es);
3754 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3757 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3761 /*********************************************************************
3763 * EM_SETPASSWORDCHAR
3766 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3770 if (es->style & ES_MULTILINE)
3773 if (es->password_char == c)
3776 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3777 es->password_char = c;
3779 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3780 es->style |= ES_PASSWORD;
3782 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3783 es->style &= ~ES_PASSWORD;
3785 EDIT_UpdateText(es, NULL, TRUE);
3789 /*********************************************************************
3793 * note: unlike the specs say: the order of start and end
3794 * _is_ preserved in Windows. (i.e. start can be > end)
3795 * In other words: this handler is OK
3798 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3800 UINT old_start = es->selection_start;
3801 UINT old_end = es->selection_end;
3802 UINT len = get_text_length(es);
3804 if (start == (UINT)-1) {
3805 start = es->selection_end;
3806 end = es->selection_end;
3808 start = min(start, len);
3809 end = min(end, len);
3811 es->selection_start = start;
3812 es->selection_end = end;
3814 es->flags |= EF_AFTER_WRAP;
3816 es->flags &= ~EF_AFTER_WRAP;
3817 /* Compute the necessary invalidation region. */
3818 /* Note that we don't need to invalidate regions which have
3819 * "never" been selected, or those which are "still" selected.
3820 * In fact, every time we hit a selection boundary, we can
3821 * *toggle* whether we need to invalidate. Thus we can optimize by
3822 * *sorting* the interval endpoints. Let's assume that we sort them
3824 * start <= end <= old_start <= old_end
3825 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3826 * in 5 comparisons; ie it's impossible to do better than the
3828 ORDER_UINT(end, old_end);
3829 ORDER_UINT(start, old_start);
3830 ORDER_UINT(old_start, old_end);
3831 ORDER_UINT(start, end);
3832 /* Note that at this point 'end' and 'old_start' are not in order, but
3833 * start is definitely the min. and old_end is definitely the max. */
3834 if (end != old_start)
3838 * ORDER_UINT32(end, old_start);
3839 * EDIT_InvalidateText(es, start, end);
3840 * EDIT_InvalidateText(es, old_start, old_end);
3841 * in place of the following if statement.
3842 * (That would complete the optimal five-comparison four-element sort.)
3844 if (old_start > end )
3846 EDIT_InvalidateText(es, start, end);
3847 EDIT_InvalidateText(es, old_start, old_end);
3851 EDIT_InvalidateText(es, start, old_start);
3852 EDIT_InvalidateText(es, end, old_end);
3855 else EDIT_InvalidateText(es, start, old_end);
3859 /*********************************************************************
3864 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3866 if (!(es->style & ES_MULTILINE))
3868 HeapFree(GetProcessHeap(), 0, es->tabs);
3869 es->tabs_count = count;
3873 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3874 memcpy(es->tabs, tabs, count * sizeof(INT));
3880 /*********************************************************************
3885 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3887 if (!(es->style & ES_MULTILINE))
3889 HeapFree(GetProcessHeap(), 0, es->tabs);
3890 es->tabs_count = count;
3895 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3896 for (i = 0 ; i < count ; i++)
3897 es->tabs[i] = *tabs++;
3903 /*********************************************************************
3905 * EM_SETWORDBREAKPROC
3908 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3910 if (es->word_break_proc == wbp)
3913 es->word_break_proc = wbp;
3914 es->word_break_proc16 = NULL;
3916 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3917 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3918 EDIT_UpdateText(es, NULL, TRUE);
3923 /*********************************************************************
3925 * EM_SETWORDBREAKPROC16
3928 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3930 if (es->word_break_proc16 == wbp)
3933 es->word_break_proc = NULL;
3934 es->word_break_proc16 = wbp;
3935 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3936 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3937 EDIT_UpdateText(es, NULL, TRUE);
3942 /*********************************************************************
3947 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3952 /* As per MSDN spec, for a single-line edit control,
3953 the return value is always TRUE */
3954 if( es->style & ES_READONLY )
3955 return !(es->style & ES_MULTILINE);
3957 ulength = strlenW(es->undo_text);
3959 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3961 strcpyW(utext, es->undo_text);
3963 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3964 es->undo_insert_count, debugstr_w(utext));
3966 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3967 EDIT_EM_EmptyUndoBuffer(es);
3968 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3969 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3970 /* send the notification after the selection start and end are set */
3971 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3972 EDIT_EM_ScrollCaret(es);
3973 HeapFree(GetProcessHeap(), 0, utext);
3975 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3976 es->undo_insert_count, debugstr_w(es->undo_text));
3981 /*********************************************************************
3986 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3990 control = GetKeyState(VK_CONTROL) & 0x8000;
3994 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3995 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3998 if (es->style & ES_MULTILINE) {
3999 if (es->style & ES_READONLY) {
4000 EDIT_MoveHome(es, FALSE);
4001 EDIT_MoveDown_ML(es, FALSE);
4003 static const WCHAR cr_lfW[] = {'\r','\n',0};
4004 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
4009 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4011 static const WCHAR tabW[] = {'\t',0};
4012 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4016 if (!(es->style & ES_READONLY) && !control) {
4017 if (es->selection_start != es->selection_end)
4020 /* delete character left of caret */
4021 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4022 EDIT_MoveBackward(es, TRUE);
4028 if (!(es->style & ES_PASSWORD))
4029 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4032 if (!(es->style & ES_READONLY))
4033 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4036 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
4037 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4040 if (!(es->style & ES_READONLY))
4041 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
4045 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4046 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4049 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4053 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4060 /*********************************************************************
4065 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4067 if (code || control)
4087 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4088 EDIT_EM_ScrollCaret(es);
4091 ERR("unknown menu item, please report\n");
4097 /*********************************************************************
4101 * Note: the resource files resource/sysres_??.rc cannot define a
4102 * single popup menu. Hence we use a (dummy) menubar
4103 * containing the single popup menu as its first item.
4105 * FIXME: the message identifiers have been chosen arbitrarily,
4106 * hence we use MF_BYPOSITION.
4107 * We might as well use the "real" values (anybody knows ?)
4108 * The menu definition is in resources/sysres_??.rc.
4109 * Once these are OK, we better use MF_BYCOMMAND here
4110 * (as we do in EDIT_WM_Command()).
4113 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4115 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4116 HMENU popup = GetSubMenu(menu, 0);
4117 UINT start = es->selection_start;
4118 UINT end = es->selection_end;
4120 ORDER_UINT(start, end);
4123 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4125 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4127 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4129 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4131 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4133 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4135 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4138 /* Windows places the menu at the edit's center in this case */
4139 GetClientRect(es->hwndSelf, &rc);
4140 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4141 x = rc.left + (rc.right - rc.left) / 2;
4142 y = rc.top + (rc.bottom - rc.top) / 2;
4145 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4150 /*********************************************************************
4155 static void EDIT_WM_Copy(EDITSTATE *es)
4157 INT s = min(es->selection_start, es->selection_end);
4158 INT e = max(es->selection_start, es->selection_end);
4166 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4167 dst = GlobalLock(hdst);
4168 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4169 dst[len] = 0; /* ensure 0 termination */
4170 TRACE("%s\n", debugstr_w(dst));
4172 OpenClipboard(es->hwndSelf);
4174 SetClipboardData(CF_UNICODETEXT, hdst);
4179 /*********************************************************************
4184 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4188 TRACE("%s\n", debugstr_w(name));
4190 * To initialize some final structure members, we call some helper
4191 * functions. However, since the EDITSTATE is not consistent (i.e.
4192 * not fully initialized), we should be very careful which
4193 * functions can be called, and in what order.
4195 EDIT_WM_SetFont(es, 0, FALSE);
4196 EDIT_EM_EmptyUndoBuffer(es);
4198 /* We need to calculate the format rect
4199 (applications may send EM_SETMARGINS before the control gets visible) */
4200 GetClientRect(es->hwndSelf, &clientRect);
4201 EDIT_SetRectNP(es, &clientRect);
4203 if (name && *name) {
4204 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4205 /* if we insert text to the editline, the text scrolls out
4206 * of the window, as the caret is placed after the insert
4207 * pos normally; thus we reset es->selection... to 0 and
4210 es->selection_start = es->selection_end = 0;
4211 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4212 * Messages are only to be sent when the USER does something to
4213 * change the contents. So I am removing this EN_CHANGE
4215 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4217 EDIT_EM_ScrollCaret(es);
4219 /* force scroll info update */
4220 EDIT_UpdateScrollInfo(es);
4221 /* The rule seems to return 1 here for success */
4222 /* Power Builder masked edit controls will crash */
4224 /* FIXME: is that in all cases so ? */
4229 /*********************************************************************
4234 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4239 while (LocalUnlock(es->hloc32W)) ;
4240 LocalFree(es->hloc32W);
4243 while (LocalUnlock(es->hloc32A)) ;
4244 LocalFree(es->hloc32A);
4247 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4248 HANDLE16 oldDS = stack16->ds;
4250 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4251 while (LocalUnlock16(es->hloc16)) ;
4252 LocalFree16(es->hloc16);
4253 stack16->ds = oldDS;
4256 pc = es->first_line_def;
4260 HeapFree(GetProcessHeap(), 0, pc);
4264 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4265 HeapFree(GetProcessHeap(), 0, es);
4271 /*********************************************************************
4276 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4278 /* we do the proper erase in EDIT_WM_Paint */
4283 /*********************************************************************
4288 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4290 if(!count) return 0;
4294 lstrcpynW(dst, es->text, count);
4295 return strlenW(dst);
4299 LPSTR textA = (LPSTR)dst;
4300 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4301 textA[count - 1] = 0; /* ensure 0 termination */
4302 return strlen(textA);
4306 /*********************************************************************
4311 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4316 if (!(es->style & ES_MULTILINE))
4319 if (!(es->style & ES_AUTOHSCROLL))
4323 fw = es->format_rect.right - es->format_rect.left;
4326 TRACE("SB_LINELEFT\n");
4328 dx = -es->char_width;
4331 TRACE("SB_LINERIGHT\n");
4332 if (es->x_offset < es->text_width)
4333 dx = es->char_width;
4336 TRACE("SB_PAGELEFT\n");
4338 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4341 TRACE("SB_PAGERIGHT\n");
4342 if (es->x_offset < es->text_width)
4343 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4351 TRACE("SB_RIGHT\n");
4352 if (es->x_offset < es->text_width)
4353 dx = es->text_width - es->x_offset;
4356 TRACE("SB_THUMBTRACK %d\n", pos);
4357 es->flags |= EF_HSCROLL_TRACK;
4358 if(es->style & WS_HSCROLL)
4359 dx = pos - es->x_offset;
4364 if(pos < 0 || pos > 100) return 0;
4365 /* Assume default scroll range 0-100 */
4366 fw = es->format_rect.right - es->format_rect.left;
4367 new_x = pos * (es->text_width - fw) / 100;
4368 dx = es->text_width ? (new_x - es->x_offset) : 0;
4371 case SB_THUMBPOSITION:
4372 TRACE("SB_THUMBPOSITION %d\n", pos);
4373 es->flags &= ~EF_HSCROLL_TRACK;
4374 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4375 dx = pos - es->x_offset;
4380 if(pos < 0 || pos > 100) return 0;
4381 /* Assume default scroll range 0-100 */
4382 fw = es->format_rect.right - es->format_rect.left;
4383 new_x = pos * (es->text_width - fw) / 100;
4384 dx = es->text_width ? (new_x - es->x_offset) : 0;
4387 /* force scroll info update */
4388 EDIT_UpdateScrollInfo(es);
4389 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4393 TRACE("SB_ENDSCROLL\n");
4396 * FIXME : the next two are undocumented !
4397 * Are we doing the right thing ?
4398 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4399 * although it's also a regular control message.
4401 case EM_GETTHUMB: /* this one is used by NT notepad */
4405 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4406 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4409 /* Assume default scroll range 0-100 */
4410 INT fw = es->format_rect.right - es->format_rect.left;
4411 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4413 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4416 case EM_LINESCROLL16:
4417 TRACE("EM_LINESCROLL16\n");
4422 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4428 INT fw = es->format_rect.right - es->format_rect.left;
4429 /* check if we are going to move too far */
4430 if(es->x_offset + dx + fw > es->text_width)
4431 dx = es->text_width - fw - es->x_offset;
4433 EDIT_EM_LineScroll_internal(es, dx, 0);
4439 /*********************************************************************
4444 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4446 HWND hLBox = es->hwndListBox;
4454 hCombo = GetParent(es->hwndSelf);
4458 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4460 if (key == VK_UP || key == VK_DOWN)
4462 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4465 if (msg == WM_KEYDOWN || nEUI)
4466 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4472 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4474 /* make sure ComboLBox pops up */
4475 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4480 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4483 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4485 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4487 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4492 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4498 /*********************************************************************
4502 * Handling of special keys that don't produce a WM_CHAR
4503 * (i.e. non-printable keys) & Backspace & Delete
4506 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4511 if (GetKeyState(VK_MENU) & 0x8000)
4514 shift = GetKeyState(VK_SHIFT) & 0x8000;
4515 control = GetKeyState(VK_CONTROL) & 0x8000;
4520 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4525 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4526 EDIT_MoveUp_ML(es, shift);
4529 EDIT_MoveWordBackward(es, shift);
4531 EDIT_MoveBackward(es, shift);
4534 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4538 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4539 EDIT_MoveDown_ML(es, shift);
4541 EDIT_MoveWordForward(es, shift);
4543 EDIT_MoveForward(es, shift);
4546 EDIT_MoveHome(es, shift);
4549 EDIT_MoveEnd(es, shift);
4552 if (es->style & ES_MULTILINE)
4553 EDIT_MovePageUp_ML(es, shift);
4555 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4558 if (es->style & ES_MULTILINE)
4559 EDIT_MovePageDown_ML(es, shift);
4561 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4564 if (!(es->style & ES_READONLY) && !(shift && control)) {
4565 if (es->selection_start != es->selection_end) {
4572 /* delete character left of caret */
4573 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4574 EDIT_MoveBackward(es, TRUE);
4576 } else if (control) {
4577 /* delete to end of line */
4578 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4579 EDIT_MoveEnd(es, TRUE);
4582 /* delete character right of caret */
4583 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4584 EDIT_MoveForward(es, TRUE);
4592 if (!(es->style & ES_READONLY))
4598 /* If the edit doesn't want the return send a message to the default object */
4599 if(!(es->style & ES_WANTRETURN))
4601 HWND hwndParent = GetParent(es->hwndSelf);
4602 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4603 if (HIWORD(dw) == DC_HASDEFID)
4605 SendMessageW( hwndParent, WM_COMMAND,
4606 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4607 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4616 /*********************************************************************
4621 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4623 es->flags &= ~EF_FOCUSED;
4625 if(!(es->style & ES_NOHIDESEL))
4626 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4627 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4632 /*********************************************************************
4636 * The caret position has been set on the WM_LBUTTONDOWN message
4639 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4642 INT e = es->selection_end;
4647 es->bCaptureState = TRUE;
4648 SetCapture(es->hwndSelf);
4650 l = EDIT_EM_LineFromChar(es, e);
4651 li = EDIT_EM_LineIndex(es, l);
4652 ll = EDIT_EM_LineLength(es, e);
4653 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4654 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4655 EDIT_EM_SetSel(es, s, e, FALSE);
4656 EDIT_EM_ScrollCaret(es);
4657 es->region_posx = es->region_posy = 0;
4658 SetTimer(es->hwndSelf, 0, 100, NULL);
4663 /*********************************************************************
4668 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4673 es->bCaptureState = TRUE;
4674 SetCapture(es->hwndSelf);
4675 EDIT_ConfinePoint(es, &x, &y);
4676 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4677 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4678 EDIT_EM_ScrollCaret(es);
4679 es->region_posx = es->region_posy = 0;
4680 SetTimer(es->hwndSelf, 0, 100, NULL);
4682 if (!(es->flags & EF_FOCUSED))
4683 SetFocus(es->hwndSelf);
4689 /*********************************************************************
4694 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4696 if (es->bCaptureState) {
4697 KillTimer(es->hwndSelf, 0);
4698 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4700 es->bCaptureState = FALSE;
4705 /*********************************************************************
4710 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4712 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4717 /*********************************************************************
4722 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4728 /* If the mouse has been captured by process other than the edit control itself,
4729 * the windows edit controls will not select the strings with mouse move.
4731 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4735 * FIXME: gotta do some scrolling if outside client
4736 * area. Maybe reset the timer ?
4739 EDIT_ConfinePoint(es, &x, &y);
4740 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4741 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4742 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4743 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4744 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4749 /*********************************************************************
4753 * See also EDIT_WM_StyleChanged
4755 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4760 TRACE("Creating %s edit control, style = %08x\n",
4761 unicode ? "Unicode" : "ANSI", lpcs->style);
4763 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4765 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4768 * Note: since the EDITSTATE has not been fully initialized yet,
4769 * we can't use any API calls that may send
4770 * WM_XXX messages before WM_NCCREATE is completed.
4773 es->is_unicode = unicode;
4774 es->style = lpcs->style;
4776 es->bEnableState = !(es->style & WS_DISABLED);
4778 es->hwndSelf = hwnd;
4779 /* Save parent, which will be notified by EN_* messages */
4780 es->hwndParent = lpcs->hwndParent;
4782 if (es->style & ES_COMBO)
4783 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4785 /* Number overrides lowercase overrides uppercase (at least it
4786 * does in Win95). However I'll bet that ES_NUMBER would be
4787 * invalid under Win 3.1.
4789 if (es->style & ES_NUMBER) {
4790 ; /* do not override the ES_NUMBER */
4791 } else if (es->style & ES_LOWERCASE) {
4792 es->style &= ~ES_UPPERCASE;
4794 if (es->style & ES_MULTILINE) {
4795 es->buffer_limit = BUFLIMIT_MULTI;
4796 if (es->style & WS_VSCROLL)
4797 es->style |= ES_AUTOVSCROLL;
4798 if (es->style & WS_HSCROLL)
4799 es->style |= ES_AUTOHSCROLL;
4800 es->style &= ~ES_PASSWORD;
4801 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4802 /* Confirmed - RIGHT overrides CENTER */
4803 if (es->style & ES_RIGHT)
4804 es->style &= ~ES_CENTER;
4805 es->style &= ~WS_HSCROLL;
4806 es->style &= ~ES_AUTOHSCROLL;
4809 es->buffer_limit = BUFLIMIT_SINGLE;
4810 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4811 es->style &= ~ES_CENTER;
4812 es->style &= ~WS_HSCROLL;
4813 es->style &= ~WS_VSCROLL;
4814 if (es->style & ES_PASSWORD)
4815 es->password_char = '*';
4818 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4819 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4821 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4823 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4825 es->undo_buffer_size = es->buffer_size;
4827 if (es->style & ES_MULTILINE)
4828 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4833 * In Win95 look and feel, the WS_BORDER style is replaced by the
4834 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4835 * control a nonclient area so we don't need to draw the border.
4836 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4837 * a nonclient area and we should handle painting the border ourselves.
4839 * When making modifications please ensure that the code still works
4840 * for edit controls created directly with style 0x50800000, exStyle 0
4841 * (which should have a single pixel border)
4843 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4844 es->style &= ~WS_BORDER;
4845 else if (es->style & WS_BORDER)
4846 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4851 /*********************************************************************
4856 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4869 BOOL rev = es->bEnableState &&
4870 ((es->flags & EF_FOCUSED) ||
4871 (es->style & ES_NOHIDESEL));
4872 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4874 GetClientRect(es->hwndSelf, &rcClient);
4876 /* get the background brush */
4877 brush = EDIT_NotifyCtlColor(es, dc);
4879 /* paint the border and the background */
4880 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4882 if(es->style & WS_BORDER) {
4883 bw = GetSystemMetrics(SM_CXBORDER);
4884 bh = GetSystemMetrics(SM_CYBORDER);
4886 if(es->style & ES_MULTILINE) {
4887 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4888 if(es->style & WS_VSCROLL) rc.right+=bw;
4891 /* Draw the frame. Same code as in nonclient.c */
4892 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4893 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4894 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4895 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4896 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4897 SelectObject(dc, old_brush);
4899 /* Keep the border clean */
4900 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4901 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4904 GetClipBox(dc, &rc);
4905 FillRect(dc, &rc, brush);
4907 IntersectClipRect(dc, es->format_rect.left,
4908 es->format_rect.top,
4909 es->format_rect.right,
4910 es->format_rect.bottom);
4911 if (es->style & ES_MULTILINE) {
4913 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4916 old_font = SelectObject(dc, es->font);
4918 if (!es->bEnableState)
4919 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4920 GetClipBox(dc, &rcRgn);
4921 if (es->style & ES_MULTILINE) {
4922 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4923 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4924 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4925 if (IntersectRect(&rc, &rcRgn, &rcLine))
4926 EDIT_PaintLine(es, dc, i, rev);
4929 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4930 if (IntersectRect(&rc, &rcRgn, &rcLine))
4931 EDIT_PaintLine(es, dc, 0, rev);
4934 SelectObject(dc, old_font);
4937 EndPaint(es->hwndSelf, &ps);
4941 /*********************************************************************
4946 static void EDIT_WM_Paste(EDITSTATE *es)
4951 /* Protect read-only edit control from modification */
4952 if(es->style & ES_READONLY)
4955 OpenClipboard(es->hwndSelf);
4956 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4957 src = (LPWSTR)GlobalLock(hsrc);
4958 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4961 else if (es->style & ES_PASSWORD) {
4962 /* clear selected text in password edit box even with empty clipboard */
4963 const WCHAR empty_strW[] = { 0 };
4964 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
4970 /*********************************************************************
4975 static void EDIT_WM_SetFocus(EDITSTATE *es)
4977 es->flags |= EF_FOCUSED;
4979 if (!(es->style & ES_NOHIDESEL))
4980 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4982 /* single line edit updates itself */
4983 if (!(es->style & ES_MULTILINE))
4985 HDC hdc = GetDC(es->hwndSelf);
4986 EDIT_WM_Paint(es, hdc);
4987 ReleaseDC(es->hwndSelf, hdc);
4990 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4991 EDIT_SetCaretPos(es, es->selection_end,
4992 es->flags & EF_AFTER_WRAP);
4993 ShowCaret(es->hwndSelf);
4994 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
4998 /*********************************************************************
5002 * With Win95 look the margins are set to default font value unless
5003 * the system font (font == 0) is being set, in which case they are left
5007 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
5015 dc = GetDC(es->hwndSelf);
5017 old_font = SelectObject(dc, font);
5018 GetTextMetricsW(dc, &tm);
5019 es->line_height = tm.tmHeight;
5020 es->char_width = tm.tmAveCharWidth;
5022 SelectObject(dc, old_font);
5023 ReleaseDC(es->hwndSelf, dc);
5025 /* Reset the format rect and the margins */
5026 GetClientRect(es->hwndSelf, &clientRect);
5027 EDIT_SetRectNP(es, &clientRect);
5028 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5029 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5031 if (es->style & ES_MULTILINE)
5032 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5034 EDIT_CalcLineWidth_SL(es);
5037 EDIT_UpdateText(es, NULL, TRUE);
5038 if (es->flags & EF_FOCUSED) {
5040 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5041 EDIT_SetCaretPos(es, es->selection_end,
5042 es->flags & EF_AFTER_WRAP);
5043 ShowCaret(es->hwndSelf);
5048 /*********************************************************************
5053 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5054 * The modified flag is reset. No notifications are sent.
5056 * For single-line controls, reception of WM_SETTEXT triggers:
5057 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5060 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5062 LPWSTR textW = NULL;
5063 if (!unicode && text)
5065 LPCSTR textA = (LPCSTR)text;
5066 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5067 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5069 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5073 if (es->flags & EF_UPDATE)
5074 /* fixed this bug once; complain if we see it about to happen again. */
5075 ERR("SetSel may generate UPDATE message whose handler may reset "
5078 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5081 TRACE("%s\n", debugstr_w(text));
5082 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5084 HeapFree(GetProcessHeap(), 0, textW);
5088 static const WCHAR empty_stringW[] = {0};
5090 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5093 es->flags &= ~EF_MODIFIED;
5094 EDIT_EM_SetSel(es, 0, 0, FALSE);
5096 /* Send the notification after the selection start and end have been set
5097 * edit control doesn't send notification on WM_SETTEXT
5098 * if it is multiline, or it is part of combobox
5100 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5102 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5103 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5105 EDIT_EM_ScrollCaret(es);
5106 EDIT_UpdateScrollInfo(es);
5110 /*********************************************************************
5115 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5117 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5119 TRACE("width = %d, height = %d\n", width, height);
5120 SetRect(&rc, 0, 0, width, height);
5121 EDIT_SetRectNP(es, &rc);
5122 EDIT_UpdateText(es, NULL, TRUE);
5127 /*********************************************************************
5131 * This message is sent by SetWindowLong on having changed either the Style
5132 * or the extended style.
5134 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5136 * See also EDIT_WM_NCCreate
5138 * It appears that the Windows version of the edit control allows the style
5139 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5140 * style variable which will generally be different. In this function we
5141 * update the internal style based on what changed in the externally visible
5144 * Much of this content as based upon the MSDN, especially:
5145 * Platform SDK Documentation -> User Interface Services ->
5146 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5147 * Edit Control Styles
5149 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5151 if (GWL_STYLE == which) {
5152 DWORD style_change_mask;
5154 /* Only a subset of changes can be applied after the control
5157 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5159 if (es->style & ES_MULTILINE)
5160 style_change_mask |= ES_WANTRETURN;
5162 new_style = style->styleNew & style_change_mask;
5164 /* Number overrides lowercase overrides uppercase (at least it
5165 * does in Win95). However I'll bet that ES_NUMBER would be
5166 * invalid under Win 3.1.
5168 if (new_style & ES_NUMBER) {
5169 ; /* do not override the ES_NUMBER */
5170 } else if (new_style & ES_LOWERCASE) {
5171 new_style &= ~ES_UPPERCASE;
5174 es->style = (es->style & ~style_change_mask) | new_style;
5175 } else if (GWL_EXSTYLE == which) {
5176 ; /* FIXME - what is needed here */
5178 WARN ("Invalid style change %ld\n",which);
5184 /*********************************************************************
5189 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5191 if ((key == VK_BACK) && (key_data & 0x2000)) {
5192 if (EDIT_EM_CanUndo(es))
5195 } else if (key == VK_UP || key == VK_DOWN) {
5196 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5199 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5203 /*********************************************************************
5208 static void EDIT_WM_Timer(EDITSTATE *es)
5210 if (es->region_posx < 0) {
5211 EDIT_MoveBackward(es, TRUE);
5212 } else if (es->region_posx > 0) {
5213 EDIT_MoveForward(es, TRUE);
5216 * FIXME: gotta do some vertical scrolling here, like
5217 * EDIT_EM_LineScroll(hwnd, 0, 1);
5221 /*********************************************************************
5226 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5230 if (!(es->style & ES_MULTILINE))
5233 if (!(es->style & ES_AUTOVSCROLL))
5242 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5243 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5244 (action == SB_PAGEUP ? "SB_PAGEUP" :
5246 EDIT_EM_Scroll(es, action);
5253 TRACE("SB_BOTTOM\n");
5254 dy = es->line_count - 1 - es->y_offset;
5257 TRACE("SB_THUMBTRACK %d\n", pos);
5258 es->flags |= EF_VSCROLL_TRACK;
5259 if(es->style & WS_VSCROLL)
5260 dy = pos - es->y_offset;
5263 /* Assume default scroll range 0-100 */
5266 if(pos < 0 || pos > 100) return 0;
5267 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5268 new_y = pos * (es->line_count - vlc) / 100;
5269 dy = es->line_count ? (new_y - es->y_offset) : 0;
5270 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5271 es->line_count, es->y_offset, pos, dy);
5274 case SB_THUMBPOSITION:
5275 TRACE("SB_THUMBPOSITION %d\n", pos);
5276 es->flags &= ~EF_VSCROLL_TRACK;
5277 if(es->style & WS_VSCROLL)
5278 dy = pos - es->y_offset;
5281 /* Assume default scroll range 0-100 */
5284 if(pos < 0 || pos > 100) return 0;
5285 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5286 new_y = pos * (es->line_count - vlc) / 100;
5287 dy = es->line_count ? (new_y - es->y_offset) : 0;
5288 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5289 es->line_count, es->y_offset, pos, dy);
5293 /* force scroll info update */
5294 EDIT_UpdateScrollInfo(es);
5295 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5299 TRACE("SB_ENDSCROLL\n");
5302 * FIXME : the next two are undocumented !
5303 * Are we doing the right thing ?
5304 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5305 * although it's also a regular control message.
5307 case EM_GETTHUMB: /* this one is used by NT notepad */
5311 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5312 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5315 /* Assume default scroll range 0-100 */
5316 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5317 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5319 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5322 case EM_LINESCROLL16:
5323 TRACE("EM_LINESCROLL16 %d\n", pos);
5328 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5333 EDIT_EM_LineScroll(es, 0, dy);
5337 /*********************************************************************
5342 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5344 if (es->flags & EF_UPDATE) {
5345 es->flags &= ~EF_UPDATE;
5346 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5348 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5352 /*********************************************************************
5357 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5359 if (es->flags & EF_UPDATE) {
5360 es->flags &= ~EF_UPDATE;
5361 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5363 InvalidateRect(es->hwndSelf, rc, bErase);
5366 /********************************************************************
5368 * The Following code is to handle inline editing from IMEs
5371 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5374 LPWSTR lpCompStr = NULL;
5376 LPSTR lpCompStrAttr = NULL;
5379 if (!(hIMC = ImmGetContext(hwnd)))
5382 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5386 ImmReleaseContext(hwnd, hIMC);
5390 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
5393 ERR("Unable to allocate IME CompositionString\n");
5394 ImmReleaseContext(hwnd,hIMC);
5399 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
5400 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
5402 if (CompFlag & GCS_COMPATTR)
5405 * We do not use the attributes yet. it would tell us what characters
5406 * are in transition and which are converted or decided upon
5408 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5412 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5415 ERR("Unable to allocate IME Attribute String\n");
5416 HeapFree(GetProcessHeap(),0,lpCompStr);
5417 ImmReleaseContext(hwnd,hIMC);
5420 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5422 lpCompStrAttr[dwBufLenAttr] = 0;
5425 lpCompStrAttr = NULL;
5428 /* check for change in composition start */
5429 if (es->selection_end < es->composition_start)
5430 es->composition_start = es->selection_end;
5432 /* replace existing selection string */
5433 es->selection_start = es->composition_start;
5435 if (es->composition_len > 0)
5436 es->selection_end = es->composition_start + es->composition_len;
5438 es->selection_end = es->selection_start;
5440 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5441 es->composition_len = abs(es->composition_start - es->selection_end);
5443 es->selection_start = es->composition_start;
5444 es->selection_end = es->selection_start + es->composition_len;
5446 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5447 HeapFree(GetProcessHeap(),0,lpCompStr);
5448 ImmReleaseContext(hwnd,hIMC);
5451 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5457 if ( !(hIMC = ImmGetContext(hwnd)))
5460 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5463 ImmReleaseContext(hwnd, hIMC);
5467 lpResultStr = HeapAlloc(GetProcessHeap(),0, dwBufLen+sizeof(WCHAR));
5470 ERR("Unable to alloc buffer for IME string\n");
5471 ImmReleaseContext(hwnd, hIMC);
5475 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, dwBufLen);
5476 lpResultStr[dwBufLen/sizeof(WCHAR)] = 0;
5478 /* check for change in composition start */
5479 if (es->selection_end < es->composition_start)
5480 es->composition_start = es->selection_end;
5482 es->selection_start = es->composition_start;
5483 es->selection_end = es->composition_start + es->composition_len;
5484 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5485 es->composition_start = es->selection_end;
5486 es->composition_len = 0;
5488 HeapFree(GetProcessHeap(),0,lpResultStr);
5489 ImmReleaseContext(hwnd, hIMC);
5492 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5494 if (CompFlag & GCS_RESULTSTR)
5495 EDIT_GetResultStr(hwnd,es);
5496 if (CompFlag & GCS_COMPSTR)
5497 EDIT_GetCompositionStr(hwnd, CompFlag, es);