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_INITIAL 30000 /* initial buffer size */
69 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
70 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
71 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
74 * extra flags for EDITSTATE.flags field
76 #define EF_MODIFIED 0x0001 /* text has been modified */
77 #define EF_FOCUSED 0x0002 /* we have input focus */
78 #define EF_UPDATE 0x0004 /* notify parent of changed state */
79 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
80 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
82 wrapped line, instead of in front of the next character */
83 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
84 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
85 the text buffer if this is clear. */
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
109 UINT buffer_size; /* the size of the buffer in characters */
110 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
111 HFONT font; /* NULL means standard system font */
112 INT x_offset; /* scroll offset for multi lines this is in pixels
113 for single lines it's in characters */
114 INT line_height; /* height of a screen line in pixels */
115 INT char_width; /* average character width in pixels */
116 DWORD style; /* sane version of wnd->dwStyle */
117 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
118 INT undo_insert_count; /* number of characters inserted in sequence */
119 UINT undo_position; /* character index of the insertion and deletion */
120 LPWSTR undo_text; /* deleted text */
121 UINT undo_buffer_size; /* size of the deleted text buffer */
122 INT selection_start; /* == selection_end if no selection */
123 INT selection_end; /* == current caret position */
124 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
125 INT left_margin; /* in pixels */
126 INT right_margin; /* in pixels */
128 INT text_width; /* width of the widest line in pixels for multi line controls
129 and just line width for single line controls */
130 INT region_posx; /* Position of cursor relative to region: */
131 INT region_posy; /* -1: to left, 0: within, 1: to right */
132 EDITWORDBREAKPROC16 word_break_proc16;
133 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
134 INT line_count; /* number of lines */
135 INT y_offset; /* scroll offset in number of lines */
136 BOOL bCaptureState; /* flag indicating whether mouse was captured */
137 BOOL bEnableState; /* flag keeping the enable state */
138 HWND hwndSelf; /* the our window handle */
139 HWND hwndParent; /* Handle of parent for sending EN_* messages.
140 Even if parent will change, EN_* messages
141 should be sent to the first parent. */
142 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
144 * only for multi line controls
146 INT lock_count; /* amount of re-entries in the EditWndProc */
149 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
150 HLOCAL hloc32W; /* our unicode local memory block */
151 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
153 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
158 UINT composition_len; /* length of composition, 0 == no composition */
159 int composition_start; /* the character position for the composition */
163 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
164 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
166 /* used for disabled or read-only edit control */
167 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
169 { /* Notify parent which has created this edit control */ \
170 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
171 SendMessageW(es->hwndParent, WM_COMMAND, \
172 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
173 (LPARAM)(es->hwndSelf)); \
176 /*********************************************************************
183 * These functions have trivial implementations
184 * We still like to call them internally
185 * "static inline" makes them more like macro's
187 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es);
188 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
189 static inline void EDIT_WM_Clear(EDITSTATE *es);
190 static inline void EDIT_WM_Cut(EDITSTATE *es);
193 * Helper functions only valid for one type of control
195 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
196 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
197 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
198 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
199 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
200 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
201 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
203 * Helper functions valid for both single line _and_ multi line controls
205 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
206 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
207 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y);
208 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
209 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
210 static void EDIT_LockBuffer(EDITSTATE *es);
211 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
212 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
213 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
214 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl);
215 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
216 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl);
217 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
218 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
219 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
220 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
221 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
222 static void EDIT_AdjustFormatRect(EDITSTATE *es);
223 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *lprc);
224 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
225 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
226 static INT EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
228 * EM_XXX message handlers
230 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
231 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
232 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
233 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
234 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
235 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end);
236 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
237 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
238 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line);
239 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
240 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
241 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
242 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
243 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
244 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
245 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
246 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
247 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
248 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit);
249 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, WORD left, WORD right, BOOL repaint);
250 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
251 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
252 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs);
253 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs);
254 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
255 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
256 static BOOL EDIT_EM_Undo(EDITSTATE *es);
258 * WM_XXX message handlers
260 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c);
261 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
262 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
263 static void EDIT_WM_Copy(EDITSTATE *es);
264 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
265 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
266 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
267 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
268 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
269 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
270 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
271 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
272 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
273 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
274 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
275 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
276 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
277 static void EDIT_WM_Paste(EDITSTATE *es);
278 static void EDIT_WM_SetFocus(EDITSTATE *es);
279 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
280 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
281 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
282 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
283 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
284 static void EDIT_WM_Timer(EDITSTATE *es);
285 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
286 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase);
287 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
288 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
290 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
291 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
293 /*********************************************************************
294 * edit class descriptor
296 static const WCHAR editW[] = {'E','d','i','t',0};
297 const struct builtin_class_descr EDIT_builtin_class =
300 CS_DBLCLKS | CS_PARENTDC, /* style */
301 EditWndProcA, /* procA */
302 EditWndProcW, /* procW */
303 sizeof(EDITSTATE *), /* extra */
304 IDC_IBEAM, /* cursor */
309 /*********************************************************************
314 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
316 return (es->undo_insert_count || strlenW(es->undo_text));
320 /*********************************************************************
325 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
327 es->undo_insert_count = 0;
328 *es->undo_text = '\0';
332 /*********************************************************************
337 static inline void EDIT_WM_Clear(EDITSTATE *es)
339 static const WCHAR empty_stringW[] = {0};
341 /* Protect read-only edit control from modification */
342 if(es->style & ES_READONLY)
345 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
349 /*********************************************************************
354 static inline void EDIT_WM_Cut(EDITSTATE *es)
361 /**********************************************************************
364 * Returns the window version in case Wine emulates a later version
365 * of windows than the application expects.
367 * In a number of cases when windows runs an application that was
368 * designed for an earlier windows version, windows reverts
369 * to "old" behaviour of that earlier version.
371 * An example is a disabled edit control that needs to be painted.
372 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
373 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
374 * applications with an expected version 0f 4.0 or higher.
377 static DWORD get_app_version(void)
379 static DWORD version;
382 DWORD dwEmulatedVersion;
384 DWORD dwProcVersion = GetProcessVersion(0);
386 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
387 GetVersionExW( &info );
388 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
389 /* FIXME: this may not be 100% correct; see discussion on the
390 * wine developer list in Nov 1999 */
391 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
396 static inline UINT get_text_length(EDITSTATE *es)
398 if(es->text_length == (UINT)-1)
399 es->text_length = strlenW(es->text);
400 return es->text_length;
403 static inline void text_buffer_changed(EDITSTATE *es)
405 es->text_length = (UINT)-1;
408 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
413 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
414 msg = WM_CTLCOLORSTATIC;
416 msg = WM_CTLCOLOREDIT;
418 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
419 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
421 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
425 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
428 return DefWindowProcW(hwnd, msg, wParam, lParam);
430 return DefWindowProcA(hwnd, msg, wParam, lParam);
433 static inline INT get_vertical_line_count(EDITSTATE *es)
435 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
439 /*********************************************************************
443 * The messages are in the order of the actual integer values
444 * (which can be found in include/windows.h)
445 * Wherever possible the 16 bit versions are converted to
446 * the 32 bit ones, so that we can 'fall through' to the
447 * helper functions. These are mostly 32 bit (with a few
448 * exceptions, clearly indicated by a '16' extension to their
452 static LRESULT EditWndProc_common( HWND hwnd, UINT msg,
453 WPARAM wParam, LPARAM lParam, BOOL unicode )
455 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
458 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
460 if (!es && msg != WM_NCCREATE)
461 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
463 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
471 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
475 if ((short)LOWORD(lParam) == -1)
476 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
478 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
480 EDIT_EM_ScrollCaret(es);
484 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
485 EDIT_EM_ScrollCaret(es);
492 RECT16 *r16 = MapSL(lParam);
493 r16->left = es->format_rect.left;
494 r16->top = es->format_rect.top;
495 r16->right = es->format_rect.right;
496 r16->bottom = es->format_rect.bottom;
501 CopyRect((LPRECT)lParam, &es->format_rect);
505 if ((es->style & ES_MULTILINE) && lParam) {
507 RECT16 *r16 = MapSL(lParam);
510 rc.right = r16->right;
511 rc.bottom = r16->bottom;
512 EDIT_SetRectNP(es, &rc);
513 EDIT_UpdateText(es, NULL, TRUE);
517 if ((es->style & ES_MULTILINE) && lParam) {
518 EDIT_SetRectNP(es, (LPRECT)lParam);
519 EDIT_UpdateText(es, NULL, TRUE);
524 if ((es->style & ES_MULTILINE) && lParam) {
526 RECT16 *r16 = MapSL(lParam);
529 rc.right = r16->right;
530 rc.bottom = r16->bottom;
531 EDIT_SetRectNP(es, &rc);
535 if ((es->style & ES_MULTILINE) && lParam)
536 EDIT_SetRectNP(es, (LPRECT)lParam);
541 result = EDIT_EM_Scroll(es, (INT)wParam);
544 case EM_LINESCROLL16:
545 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
546 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
549 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
552 case EM_SCROLLCARET16:
554 EDIT_EM_ScrollCaret(es);
560 result = ((es->flags & EF_MODIFIED) != 0);
566 es->flags |= EF_MODIFIED;
568 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
571 case EM_GETLINECOUNT16:
572 case EM_GETLINECOUNT:
573 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
577 if ((INT16)wParam == -1)
581 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
585 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
588 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
592 result = (LRESULT)EDIT_EM_GetHandle16(es);
595 result = (LRESULT)EDIT_EM_GetHandle(es);
600 result = EDIT_EM_GetThumb(es);
603 /* these messages missing from specs */
612 FIXME("undocumented message 0x%x, please report\n", msg);
613 result = DefWindowProcW(hwnd, msg, wParam, lParam);
616 case EM_LINELENGTH16:
618 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
621 case EM_REPLACESEL16:
622 lParam = (LPARAM)MapSL(lParam);
623 unicode = FALSE; /* 16-bit message is always ascii */
630 textW = (LPWSTR)lParam;
633 LPSTR textA = (LPSTR)lParam;
634 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
635 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
636 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
639 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
643 HeapFree(GetProcessHeap(), 0, textW);
648 lParam = (LPARAM)MapSL(lParam);
649 unicode = FALSE; /* 16-bit message is always ascii */
652 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
656 case EM_SETLIMITTEXT:
657 EDIT_EM_SetLimitText(es, wParam);
662 result = (LRESULT)EDIT_EM_CanUndo(es);
668 result = (LRESULT)EDIT_EM_Undo(es);
673 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
676 case EM_LINEFROMCHAR16:
677 case EM_LINEFROMCHAR:
678 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
681 case EM_SETTABSTOPS16:
682 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
685 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
688 case EM_SETPASSWORDCHAR16:
689 unicode = FALSE; /* 16-bit message is always ascii */
691 case EM_SETPASSWORDCHAR:
696 charW = (WCHAR)wParam;
700 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
703 EDIT_EM_SetPasswordChar(es, charW);
707 case EM_EMPTYUNDOBUFFER16:
708 case EM_EMPTYUNDOBUFFER:
709 EDIT_EM_EmptyUndoBuffer(es);
712 case EM_GETFIRSTVISIBLELINE16:
713 result = es->y_offset;
715 case EM_GETFIRSTVISIBLELINE:
716 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
719 case EM_SETREADONLY16:
722 SetWindowLongW( hwnd, GWL_STYLE,
723 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
724 es->style |= ES_READONLY;
726 SetWindowLongW( hwnd, GWL_STYLE,
727 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
728 es->style &= ~ES_READONLY;
733 case EM_SETWORDBREAKPROC16:
734 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
736 case EM_SETWORDBREAKPROC:
737 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
740 case EM_GETWORDBREAKPROC16:
741 result = (LRESULT)es->word_break_proc16;
743 case EM_GETWORDBREAKPROC:
744 result = (LRESULT)es->word_break_proc;
747 case EM_GETPASSWORDCHAR16:
748 unicode = FALSE; /* 16-bit message is always ascii */
750 case EM_GETPASSWORDCHAR:
753 result = es->password_char;
756 WCHAR charW = es->password_char;
758 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
764 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
767 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
771 result = MAKELONG(es->left_margin, es->right_margin);
774 case EM_GETLIMITTEXT:
775 result = es->buffer_limit;
779 if ((INT)wParam >= get_text_length(es)) result = -1;
780 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
784 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
787 /* End of the EM_ messages which were in numerical order; what order
788 * are these in? vaguely alphabetical?
792 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
796 result = EDIT_WM_Destroy(es);
801 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
803 if (es->style & ES_MULTILINE)
804 result |= DLGC_WANTALLKEYS;
806 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
808 int vk = (int)((LPMSG)lParam)->wParam;
812 if (vk == VK_RETURN || vk == VK_ESCAPE)
813 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
814 result |= DLGC_WANTMESSAGE;
821 SendMessageW(GetParent(hwnd), WM_CLOSE, 0, 0);
836 strng[0] = wParam >> 8;
837 strng[1] = wParam & 0xff;
838 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
839 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
840 result = EDIT_WM_Char(es, charW);
853 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
858 if (charW == VK_RETURN || charW == VK_ESCAPE)
860 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
861 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
865 result = EDIT_WM_Char(es, charW);
872 if (wParam == UNICODE_NOCHAR) return TRUE;
873 if (wParam <= 0x000fffff)
875 if(wParam > 0xffff) /* convert to surrogates */
878 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
879 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
881 else EDIT_WM_Char(es, wParam);
892 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
896 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
905 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
908 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
912 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
913 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
914 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
916 result = EDIT_WM_Create(es, nameW);
917 HeapFree(GetProcessHeap(), 0, nameW);
926 es->bEnableState = (BOOL) wParam;
927 EDIT_UpdateText(es, NULL, TRUE);
931 /* we do the proper erase in EDIT_WM_Paint */
936 result = (LRESULT)es->font;
940 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
943 case WM_GETTEXTLENGTH:
944 if (unicode) result = get_text_length(es);
945 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
946 NULL, 0, NULL, NULL );
950 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
954 result = EDIT_WM_KeyDown(es, (INT)wParam);
958 result = EDIT_WM_KillFocus(es);
961 case WM_LBUTTONDBLCLK:
962 result = EDIT_WM_LButtonDblClk(es);
966 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
970 result = EDIT_WM_LButtonUp(es);
974 result = EDIT_WM_MButtonDown(es);
978 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
983 EDIT_WM_Paint(es, (HDC)wParam);
991 EDIT_WM_SetFocus(es);
995 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
999 /* FIXME: actually set an internal flag and behave accordingly */
1003 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
1008 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
1011 case WM_STYLECHANGED:
1012 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
1015 case WM_STYLECHANGING:
1016 result = 0; /* See EDIT_WM_StyleChanged */
1020 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
1028 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
1033 int gcWheelDelta = 0;
1034 UINT pulScrollLines = 3;
1035 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1037 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1038 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1041 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1042 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1044 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1045 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1046 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1052 /* IME messages to make the edit control IME aware */
1053 case WM_IME_SETCONTEXT:
1056 case WM_IME_STARTCOMPOSITION:
1057 es->composition_start = es->selection_end;
1058 es->composition_len = 0;
1061 case WM_IME_COMPOSITION:
1062 EDIT_ImeComposition(hwnd, lParam, es);
1065 case WM_IME_ENDCOMPOSITION:
1066 if (es->composition_len > 0)
1068 static const WCHAR empty_stringW[] = {0};
1069 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1070 es->selection_end = es->selection_start;
1071 es->composition_len= 0;
1075 case WM_IME_COMPOSITIONFULL:
1081 case WM_IME_CONTROL:
1085 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1089 if (es) EDIT_UnlockBuffer(es, FALSE);
1091 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1096 /*********************************************************************
1098 * EditWndProcW (USER32.@)
1100 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1102 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1105 /*********************************************************************
1107 * EditWndProc (USER32.@)
1109 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1111 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1114 /*********************************************************************
1116 * EDIT_BuildLineDefs_ML
1118 * Build linked list of text lines.
1119 * Lines can end with '\0' (last line), a character (if it is wrapped),
1120 * a soft return '\r\r\n' or a hard return '\r\n'
1123 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1127 LPWSTR current_position, cp;
1129 LINEDEF *current_line;
1130 LINEDEF *previous_line;
1131 LINEDEF *start_line;
1132 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1133 INT line_count = es->line_count;
1134 INT orig_net_length;
1137 if (istart == iend && delta == 0)
1140 dc = GetDC(es->hwndSelf);
1142 old_font = SelectObject(dc, es->font);
1144 previous_line = NULL;
1145 current_line = es->first_line_def;
1147 /* Find starting line. istart must lie inside an existing line or
1148 * at the end of buffer */
1150 if (istart < current_line->index + current_line->length ||
1151 current_line->ending == END_0)
1154 previous_line = current_line;
1155 current_line = current_line->next;
1157 } while (current_line);
1159 if (!current_line) /* Error occurred start is not inside previous buffer */
1161 FIXME(" modification occurred outside buffer\n");
1162 ReleaseDC(es->hwndSelf, dc);
1166 /* Remember start of modifications in order to calculate update region */
1167 nstart_line = line_index;
1168 nstart_index = current_line->index;
1170 /* We must start to reformat from the previous line since the modifications
1171 * may have caused the line to wrap upwards. */
1172 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1175 current_line = previous_line;
1177 start_line = current_line;
1179 fw = es->format_rect.right - es->format_rect.left;
1180 current_position = es->text + current_line->index;
1182 if (current_line != start_line)
1184 if (!current_line || current_line->index + delta > current_position - es->text)
1186 /* The buffer has been expanded, create a new line and
1187 insert it into the link list */
1188 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1189 new_line->next = previous_line->next;
1190 previous_line->next = new_line;
1191 current_line = new_line;
1194 else if (current_line->index + delta < current_position - es->text)
1196 /* The previous line merged with this line so we delete this extra entry */
1197 previous_line->next = current_line->next;
1198 HeapFree(GetProcessHeap(), 0, current_line);
1199 current_line = previous_line->next;
1203 else /* current_line->index + delta == current_position */
1205 if (current_position - es->text > iend)
1206 break; /* We reached end of line modifications */
1207 /* else recalculate this line */
1211 current_line->index = current_position - es->text;
1212 orig_net_length = current_line->net_length;
1214 /* Find end of line */
1215 cp = current_position;
1217 if (*cp == '\n') break;
1218 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1223 /* Mark type of line termination */
1225 current_line->ending = END_0;
1226 current_line->net_length = strlenW(current_position);
1227 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1228 current_line->ending = END_SOFT;
1229 current_line->net_length = cp - current_position - 1;
1230 } else if (*cp == '\n') {
1231 current_line->ending = END_RICH;
1232 current_line->net_length = cp - current_position;
1234 current_line->ending = END_HARD;
1235 current_line->net_length = cp - current_position;
1238 /* Calculate line width */
1239 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1240 current_position, current_line->net_length,
1241 es->tabs_count, es->tabs));
1243 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1244 if (!(es->style & ES_AUTOHSCROLL)) {
1245 if (current_line->width > fw) {
1250 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1251 prev + 1, current_line->net_length, WB_RIGHT);
1252 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1253 current_position, next, es->tabs_count, es->tabs));
1254 } while (current_line->width <= fw);
1255 if (!prev) { /* Didn't find a line break so force a break */
1260 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1261 current_position, next, es->tabs_count, es->tabs));
1262 } while (current_line->width <= fw);
1267 /* If the first line we are calculating, wrapped before istart, we must
1268 * adjust istart in order for this to be reflected in the update region. */
1269 if (current_line->index == nstart_index && istart > current_line->index + prev)
1270 istart = current_line->index + prev;
1271 /* else if we are updating the previous line before the first line we
1272 * are re-calculating and it expanded */
1273 else if (current_line == start_line &&
1274 current_line->index != nstart_index && orig_net_length < prev)
1276 /* Line expanded due to an upwards line wrap so we must partially include
1277 * previous line in update region */
1278 nstart_line = line_index;
1279 nstart_index = current_line->index;
1280 istart = current_line->index + orig_net_length;
1283 current_line->net_length = prev;
1284 current_line->ending = END_WRAP;
1285 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1286 current_line->net_length, es->tabs_count, es->tabs));
1288 else if (current_line == start_line &&
1289 current_line->index != nstart_index &&
1290 orig_net_length < current_line->net_length) {
1291 /* The previous line expanded but it's still not as wide as the client rect */
1292 /* The expansion is due to an upwards line wrap so we must partially include
1293 it in the update region */
1294 nstart_line = line_index;
1295 nstart_index = current_line->index;
1296 istart = current_line->index + orig_net_length;
1301 /* Adjust length to include line termination */
1302 switch (current_line->ending) {
1304 current_line->length = current_line->net_length + 3;
1307 current_line->length = current_line->net_length + 1;
1310 current_line->length = current_line->net_length + 2;
1314 current_line->length = current_line->net_length;
1317 es->text_width = max(es->text_width, current_line->width);
1318 current_position += current_line->length;
1319 previous_line = current_line;
1320 current_line = current_line->next;
1322 } while (previous_line->ending != END_0);
1324 /* Finish adjusting line indexes by delta or remove hanging lines */
1325 if (previous_line->ending == END_0)
1327 LINEDEF *pnext = NULL;
1329 previous_line->next = NULL;
1330 while (current_line)
1332 pnext = current_line->next;
1333 HeapFree(GetProcessHeap(), 0, current_line);
1334 current_line = pnext;
1338 else if (delta != 0)
1340 while (current_line)
1342 current_line->index += delta;
1343 current_line = current_line->next;
1347 /* Calculate rest of modification rectangle */
1352 * We calculate two rectangles. One for the first line which may have
1353 * an indent with respect to the format rect. The other is a format-width
1354 * rectangle that spans the rest of the lines that changed or moved.
1356 rc.top = es->format_rect.top + nstart_line * es->line_height -
1357 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1358 rc.bottom = rc.top + es->line_height;
1359 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1360 rc.left = es->format_rect.left;
1362 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1363 es->text + nstart_index, istart - nstart_index,
1364 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1365 rc.right = es->format_rect.right;
1366 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1369 rc.left = es->format_rect.left;
1370 rc.right = es->format_rect.right;
1372 * If lines were added or removed we must re-paint the remainder of the
1373 * lines since the remaining lines were either shifted up or down.
1375 if (line_count < es->line_count) /* We added lines */
1376 rc.bottom = es->line_count * es->line_height;
1377 else if (line_count > es->line_count) /* We removed lines */
1378 rc.bottom = line_count * es->line_height;
1380 rc.bottom = line_index * es->line_height;
1381 rc.bottom += es->format_rect.top;
1382 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1383 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1384 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1385 DeleteObject(tmphrgn);
1389 SelectObject(dc, old_font);
1391 ReleaseDC(es->hwndSelf, dc);
1394 /*********************************************************************
1396 * EDIT_CalcLineWidth_SL
1399 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1406 text = EDIT_GetPasswordPointer_SL(es);
1408 dc = GetDC(es->hwndSelf);
1410 old_font = SelectObject(dc, es->font);
1412 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1415 SelectObject(dc, old_font);
1416 ReleaseDC(es->hwndSelf, dc);
1418 if (es->style & ES_PASSWORD)
1419 HeapFree(GetProcessHeap(), 0, text);
1421 es->text_width = size.cx;
1424 /*********************************************************************
1426 * EDIT_CallWordBreakProc
1428 * Call appropriate WordBreakProc (internal or external).
1430 * Note: The "start" argument should always be an index referring
1431 * to es->text. The actual wordbreak proc might be
1432 * 16 bit, so we can't always pass any 32 bit LPSTR.
1433 * Hence we assume that es->text is the buffer that holds
1434 * the string under examination (we can decide this for ourselves).
1437 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1441 if (es->word_break_proc16) {
1448 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1449 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1450 segptr = WOWGlobalLock16(hglob16);
1451 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1452 args[4] = SELECTOROF(segptr);
1453 args[3] = OFFSETOF(segptr);
1457 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1458 ret = LOWORD(result);
1459 GlobalUnlock16(hglob16);
1460 GlobalFree16(hglob16);
1462 else if (es->word_break_proc)
1466 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1468 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1469 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1470 ret = wbpW(es->text + start, index, count, action);
1474 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1478 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1479 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1480 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1481 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1482 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1483 ret = wbpA(textA, index, countA, action);
1484 HeapFree(GetProcessHeap(), 0, textA);
1488 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1494 /*********************************************************************
1498 * Beware: This is not the function called on EM_CHARFROMPOS
1499 * The position _can_ be outside the formatting / client
1501 * The return value is only the character index
1504 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1509 INT x_high = 0, x_low = 0;
1511 if (es->style & ES_MULTILINE) {
1512 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1514 LINEDEF *line_def = es->first_line_def;
1516 while ((line > 0) && line_def->next) {
1517 line_index += line_def->length;
1518 line_def = line_def->next;
1521 x += es->x_offset - es->format_rect.left;
1522 if (es->style & ES_RIGHT)
1523 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1524 else if (es->style & ES_CENTER)
1525 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1526 if (x >= line_def->width) {
1528 *after_wrap = (line_def->ending == END_WRAP);
1529 return line_index + line_def->net_length;
1533 *after_wrap = FALSE;
1536 dc = GetDC(es->hwndSelf);
1538 old_font = SelectObject(dc, es->font);
1540 high = line_index + line_def->net_length + 1;
1541 while (low < high - 1)
1543 INT mid = (low + high) / 2;
1544 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1553 if (abs(x_high - x) + 1 <= abs(x_low - x))
1559 *after_wrap = ((index == line_index + line_def->net_length) &&
1560 (line_def->ending == END_WRAP));
1565 *after_wrap = FALSE;
1566 x -= es->format_rect.left;
1568 return es->x_offset;
1572 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1573 if (es->style & ES_RIGHT)
1575 else if (es->style & ES_CENTER)
1579 text = EDIT_GetPasswordPointer_SL(es);
1580 dc = GetDC(es->hwndSelf);
1582 old_font = SelectObject(dc, es->font);
1586 INT high = es->x_offset;
1587 while (low < high - 1)
1589 INT mid = (low + high) / 2;
1590 GetTextExtentPoint32W( dc, text + mid,
1591 es->x_offset - mid, &size );
1600 if (abs(x_high + x) <= abs(x_low + x) + 1)
1607 INT low = es->x_offset;
1608 INT high = get_text_length(es) + 1;
1609 while (low < high - 1)
1611 INT mid = (low + high) / 2;
1612 GetTextExtentPoint32W( dc, text + es->x_offset,
1613 mid - es->x_offset, &size );
1622 if (abs(x_high - x) <= abs(x_low - x) + 1)
1627 if (es->style & ES_PASSWORD)
1628 HeapFree(GetProcessHeap(), 0, text);
1631 SelectObject(dc, old_font);
1632 ReleaseDC(es->hwndSelf, dc);
1637 /*********************************************************************
1641 * adjusts the point to be within the formatting rectangle
1642 * (so CharFromPos returns the nearest _visible_ character)
1645 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
1647 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1648 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1652 /*********************************************************************
1656 * Calculates the bounding rectangle for a line from a starting
1657 * column to an ending column.
1660 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1662 INT line_index = EDIT_EM_LineIndex(es, line);
1664 if (es->style & ES_MULTILINE)
1665 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1667 rc->top = es->format_rect.top;
1668 rc->bottom = rc->top + es->line_height;
1669 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1670 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1674 /*********************************************************************
1676 * EDIT_GetPasswordPointer_SL
1678 * note: caller should free the (optionally) allocated buffer
1681 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1683 if (es->style & ES_PASSWORD) {
1684 INT len = get_text_length(es);
1685 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1687 while(len) text[--len] = es->password_char;
1694 /*********************************************************************
1698 * This acts as a LocalLock16(), but it locks only once. This way
1699 * you can call it whenever you like, without unlocking.
1701 * Initially the edit control allocates a HLOCAL32 buffer
1702 * (32 bit linear memory handler). However, 16 bit application
1703 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1704 * handler). From that moment on we have to keep using this 16 bit memory
1705 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1706 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1710 static void EDIT_LockBuffer(EDITSTATE *es)
1712 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1713 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1718 BOOL _16bit = FALSE;
1724 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1725 textA = LocalLock(es->hloc32A);
1726 countA = strlen(textA) + 1;
1730 HANDLE16 oldDS = stack16->ds;
1731 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1732 stack16->ds = hInstance;
1733 textA = MapSL(LocalLock16(es->hloc16));
1734 stack16->ds = oldDS;
1735 countA = strlen(textA) + 1;
1740 ERR("no buffer ... please report\n");
1747 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1748 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1749 if(countW_new > es->buffer_size + 1)
1751 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1752 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1753 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1756 es->hloc32W = hloc32W_new;
1757 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1758 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1761 WARN("FAILED! Will synchronize partially\n");
1765 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1766 es->text = LocalLock(es->hloc32W);
1770 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1773 HANDLE16 oldDS = stack16->ds;
1774 stack16->ds = hInstance;
1775 LocalUnlock16(es->hloc16);
1776 stack16->ds = oldDS;
1779 LocalUnlock(es->hloc32A);
1782 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1787 /*********************************************************************
1789 * EDIT_SL_InvalidateText
1791 * Called from EDIT_InvalidateText().
1792 * Does the job for single-line controls only.
1795 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1800 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1801 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1802 EDIT_UpdateText(es, &rc, TRUE);
1806 /*********************************************************************
1808 * EDIT_ML_InvalidateText
1810 * Called from EDIT_InvalidateText().
1811 * Does the job for multi-line controls only.
1814 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1816 INT vlc = get_vertical_line_count(es);
1817 INT sl = EDIT_EM_LineFromChar(es, start);
1818 INT el = EDIT_EM_LineFromChar(es, end);
1827 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1830 sc = start - EDIT_EM_LineIndex(es, sl);
1831 ec = end - EDIT_EM_LineIndex(es, el);
1832 if (sl < es->y_offset) {
1836 if (el > es->y_offset + vlc) {
1837 el = es->y_offset + vlc;
1838 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1840 GetClientRect(es->hwndSelf, &rc1);
1841 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1843 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1844 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1845 EDIT_UpdateText(es, &rcUpdate, TRUE);
1847 EDIT_GetLineRect(es, sl, sc,
1848 EDIT_EM_LineLength(es,
1849 EDIT_EM_LineIndex(es, sl)),
1851 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1852 EDIT_UpdateText(es, &rcUpdate, TRUE);
1853 for (l = sl + 1 ; l < el ; l++) {
1854 EDIT_GetLineRect(es, l, 0,
1855 EDIT_EM_LineLength(es,
1856 EDIT_EM_LineIndex(es, l)),
1858 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1859 EDIT_UpdateText(es, &rcUpdate, TRUE);
1861 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1862 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1863 EDIT_UpdateText(es, &rcUpdate, TRUE);
1868 /*********************************************************************
1870 * EDIT_InvalidateText
1872 * Invalidate the text from offset start up to, but not including,
1873 * offset end. Useful for (re)painting the selection.
1874 * Regions outside the linewidth are not invalidated.
1875 * end == -1 means end == TextLength.
1876 * start and end need not be ordered.
1879 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1885 end = get_text_length(es);
1893 if (es->style & ES_MULTILINE)
1894 EDIT_ML_InvalidateText(es, start, end);
1896 EDIT_SL_InvalidateText(es, start, end);
1900 /*********************************************************************
1904 * Try to fit size + 1 characters in the buffer.
1906 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1910 if (size <= es->buffer_size)
1913 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1915 /* Force edit to unlock it's buffer. es->text now NULL */
1916 EDIT_UnlockBuffer(es, TRUE);
1919 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1920 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1921 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1922 es->hloc32W = hNew32W;
1923 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1927 EDIT_LockBuffer(es);
1929 if (es->buffer_size < size) {
1930 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1931 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1934 TRACE("We now have %d+1\n", es->buffer_size);
1940 /*********************************************************************
1944 * Try to fit size + 1 bytes in the undo buffer.
1947 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1951 if (size <= es->undo_buffer_size)
1954 TRACE("trying to ReAlloc to %d+1\n", size);
1956 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1957 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1958 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1963 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1969 /*********************************************************************
1974 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1976 INT e = es->selection_end;
1980 if ((es->style & ES_MULTILINE) && e &&
1981 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1983 if (e && (es->text[e - 1] == '\r'))
1987 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1988 EDIT_EM_ScrollCaret(es);
1992 /*********************************************************************
1996 * Only for multi line controls
1997 * Move the caret one line down, on a column with the nearest
1998 * x coordinate on the screen (might be a different column).
2001 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
2003 INT s = es->selection_start;
2004 INT e = es->selection_end;
2005 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2006 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2007 INT x = (short)LOWORD(pos);
2008 INT y = (short)HIWORD(pos);
2010 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
2013 EDIT_EM_SetSel(es, s, e, after_wrap);
2014 EDIT_EM_ScrollCaret(es);
2018 /*********************************************************************
2023 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
2025 BOOL after_wrap = FALSE;
2028 /* Pass a high value in x to make sure of receiving the end of the line */
2029 if (!ctrl && (es->style & ES_MULTILINE))
2030 e = EDIT_CharFromPos(es, 0x3fffffff,
2031 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2033 e = get_text_length(es);
2034 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2035 EDIT_EM_ScrollCaret(es);
2039 /*********************************************************************
2044 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2046 INT e = es->selection_end;
2050 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2051 if (es->text[e] == '\n')
2053 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2057 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2058 EDIT_EM_ScrollCaret(es);
2062 /*********************************************************************
2066 * Home key: move to beginning of line.
2069 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
2073 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2074 if (!ctrl && (es->style & ES_MULTILINE))
2075 e = EDIT_CharFromPos(es, -es->x_offset,
2076 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2079 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2080 EDIT_EM_ScrollCaret(es);
2084 /*********************************************************************
2086 * EDIT_MovePageDown_ML
2088 * Only for multi line controls
2089 * Move the caret one page down, on a column with the nearest
2090 * x coordinate on the screen (might be a different column).
2093 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2095 INT s = es->selection_start;
2096 INT e = es->selection_end;
2097 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2098 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2099 INT x = (short)LOWORD(pos);
2100 INT y = (short)HIWORD(pos);
2102 e = EDIT_CharFromPos(es, x,
2103 y + (es->format_rect.bottom - es->format_rect.top),
2107 EDIT_EM_SetSel(es, s, e, after_wrap);
2108 EDIT_EM_ScrollCaret(es);
2112 /*********************************************************************
2114 * EDIT_MovePageUp_ML
2116 * Only for multi line controls
2117 * Move the caret one page up, on a column with the nearest
2118 * x coordinate on the screen (might be a different column).
2121 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2123 INT s = es->selection_start;
2124 INT e = es->selection_end;
2125 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2126 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2127 INT x = (short)LOWORD(pos);
2128 INT y = (short)HIWORD(pos);
2130 e = EDIT_CharFromPos(es, x,
2131 y - (es->format_rect.bottom - es->format_rect.top),
2135 EDIT_EM_SetSel(es, s, e, after_wrap);
2136 EDIT_EM_ScrollCaret(es);
2140 /*********************************************************************
2144 * Only for multi line controls
2145 * Move the caret one line up, on a column with the nearest
2146 * x coordinate on the screen (might be a different column).
2149 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2151 INT s = es->selection_start;
2152 INT e = es->selection_end;
2153 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2154 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2155 INT x = (short)LOWORD(pos);
2156 INT y = (short)HIWORD(pos);
2158 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2161 EDIT_EM_SetSel(es, s, e, after_wrap);
2162 EDIT_EM_ScrollCaret(es);
2166 /*********************************************************************
2168 * EDIT_MoveWordBackward
2171 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2173 INT s = es->selection_start;
2174 INT e = es->selection_end;
2179 l = EDIT_EM_LineFromChar(es, e);
2180 ll = EDIT_EM_LineLength(es, e);
2181 li = EDIT_EM_LineIndex(es, l);
2184 li = EDIT_EM_LineIndex(es, l - 1);
2185 e = li + EDIT_EM_LineLength(es, li);
2188 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2192 EDIT_EM_SetSel(es, s, e, FALSE);
2193 EDIT_EM_ScrollCaret(es);
2197 /*********************************************************************
2199 * EDIT_MoveWordForward
2202 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2204 INT s = es->selection_start;
2205 INT e = es->selection_end;
2210 l = EDIT_EM_LineFromChar(es, e);
2211 ll = EDIT_EM_LineLength(es, e);
2212 li = EDIT_EM_LineIndex(es, l);
2214 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2215 e = EDIT_EM_LineIndex(es, l + 1);
2217 e = li + EDIT_CallWordBreakProc(es,
2218 li, e - li + 1, ll, WB_RIGHT);
2222 EDIT_EM_SetSel(es, s, e, FALSE);
2223 EDIT_EM_ScrollCaret(es);
2227 /*********************************************************************
2232 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2234 INT s = es->selection_start;
2235 INT e = es->selection_end;
2242 if (es->style & ES_MULTILINE) {
2243 INT vlc = get_vertical_line_count(es);
2245 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2250 TRACE("line=%d\n", line);
2252 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2253 x = (short)LOWORD(pos);
2254 y = (short)HIWORD(pos);
2255 li = EDIT_EM_LineIndex(es, line);
2256 ll = EDIT_EM_LineLength(es, li);
2257 s = min(es->selection_start, es->selection_end);
2258 e = max(es->selection_start, es->selection_end);
2259 s = min(li + ll, max(li, s));
2260 e = min(li + ll, max(li, e));
2261 if (rev && (s != e) &&
2262 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2263 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2264 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2265 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2267 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2271 /*********************************************************************
2276 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2280 LOGFONTW underline_font;
2281 HFONT hUnderline = 0;
2290 BkMode = GetBkMode(dc);
2291 BkColor = GetBkColor(dc);
2292 TextColor = GetTextColor(dc);
2294 if (es->composition_len == 0)
2296 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2297 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2298 SetBkMode( dc, OPAQUE);
2302 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2303 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2304 underline_font.lfUnderline = TRUE;
2305 hUnderline = CreateFontIndirectW(&underline_font);
2306 old_font = SelectObject(dc,hUnderline);
2309 li = EDIT_EM_LineIndex(es, line);
2310 if (es->style & ES_MULTILINE) {
2311 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2312 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2314 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2315 TextOutW(dc, x, y, text + li + col, count);
2316 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2318 if (es->style & ES_PASSWORD)
2319 HeapFree(GetProcessHeap(), 0, text);
2322 if (es->composition_len == 0)
2324 SetBkColor(dc, BkColor);
2325 SetTextColor(dc, TextColor);
2326 SetBkMode( dc, BkMode);
2331 SelectObject(dc,old_font);
2333 DeleteObject(hUnderline);
2340 /*********************************************************************
2345 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2348 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2349 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2350 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2354 /*********************************************************************
2356 * EDIT_AdjustFormatRect
2358 * Adjusts the format rectangle for the current font and the
2359 * current client rectangle.
2362 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2366 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2367 if (es->style & ES_MULTILINE)
2369 INT fw, vlc, max_x_offset, max_y_offset;
2371 vlc = get_vertical_line_count(es);
2372 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2374 /* correct es->x_offset */
2375 fw = es->format_rect.right - es->format_rect.left;
2376 max_x_offset = es->text_width - fw;
2377 if(max_x_offset < 0) max_x_offset = 0;
2378 if(es->x_offset > max_x_offset)
2379 es->x_offset = max_x_offset;
2381 /* correct es->y_offset */
2382 max_y_offset = es->line_count - vlc;
2383 if(max_y_offset < 0) max_y_offset = 0;
2384 if(es->y_offset > max_y_offset)
2385 es->y_offset = max_y_offset;
2387 /* force scroll info update */
2388 EDIT_UpdateScrollInfo(es);
2391 /* Windows doesn't care to fix text placement for SL controls */
2392 es->format_rect.bottom = es->format_rect.top + es->line_height;
2394 /* Always stay within the client area */
2395 GetClientRect(es->hwndSelf, &ClientRect);
2396 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2398 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2399 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2401 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2405 /*********************************************************************
2409 * note: this is not (exactly) the handler called on EM_SETRECTNP
2410 * it is also used to set the rect of a single line control
2413 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2417 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2419 CopyRect(&es->format_rect, rc);
2421 if (ExStyle & WS_EX_CLIENTEDGE) {
2422 es->format_rect.left++;
2423 es->format_rect.right--;
2425 if (es->format_rect.bottom - es->format_rect.top
2426 >= es->line_height + 2)
2428 es->format_rect.top++;
2429 es->format_rect.bottom--;
2432 else if (es->style & WS_BORDER) {
2433 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2434 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2435 es->format_rect.left += bw;
2436 es->format_rect.right -= bw;
2437 if (es->format_rect.bottom - es->format_rect.top
2438 >= es->line_height + 2 * bh)
2440 es->format_rect.top += bh;
2441 es->format_rect.bottom -= bh;
2445 es->format_rect.left += es->left_margin;
2446 es->format_rect.right -= es->right_margin;
2447 EDIT_AdjustFormatRect(es);
2451 /*********************************************************************
2456 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2459 /* Edit window might be already destroyed */
2460 if(!IsWindow(es->hwndSelf))
2462 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2466 if (!es->lock_count) {
2467 ERR("lock_count == 0 ... please report\n");
2471 ERR("es->text == 0 ... please report\n");
2475 if (force || (es->lock_count == 1)) {
2479 UINT countW = get_text_length(es) + 1;
2480 STACK16FRAME* stack16 = NULL;
2485 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2486 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2487 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2488 countA = LocalSize(es->hloc32A);
2489 if(countA_new > countA)
2492 UINT alloc_size = ROUND_TO_GROW(countA_new);
2493 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2494 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2497 es->hloc32A = hloc32A_new;
2498 countA = LocalSize(hloc32A_new);
2499 TRACE("Real new size %d bytes\n", countA);
2502 WARN("FAILED! Will synchronize partially\n");
2504 textA = LocalLock(es->hloc32A);
2508 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2510 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2511 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2513 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2514 oldDS = stack16->ds;
2515 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2517 countA = LocalSize16(es->hloc16);
2518 if(countA_new > countA)
2520 HLOCAL16 hloc16_new;
2521 UINT alloc_size = ROUND_TO_GROW(countA_new);
2522 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2523 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2526 es->hloc16 = hloc16_new;
2527 countA = LocalSize16(hloc16_new);
2528 TRACE("Real new size %d bytes\n", countA);
2531 WARN("FAILED! Will synchronize partially\n");
2533 textA = MapSL(LocalLock16(es->hloc16));
2538 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2540 LocalUnlock16(es->hloc16);
2542 LocalUnlock(es->hloc32A);
2545 if (stack16) stack16->ds = oldDS;
2546 LocalUnlock(es->hloc32W);
2550 ERR("no buffer ... please report\n");
2558 /*********************************************************************
2560 * EDIT_UpdateScrollInfo
2563 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2565 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2568 si.cbSize = sizeof(SCROLLINFO);
2569 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2571 si.nMax = es->line_count - 1;
2572 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2573 si.nPos = es->y_offset;
2574 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2575 si.nMin, si.nMax, si.nPage, si.nPos);
2576 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2579 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2582 si.cbSize = sizeof(SCROLLINFO);
2583 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2585 si.nMax = es->text_width - 1;
2586 si.nPage = es->format_rect.right - es->format_rect.left;
2587 si.nPos = es->x_offset;
2588 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2589 si.nMin, si.nMax, si.nPage, si.nPos);
2590 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2594 /*********************************************************************
2596 * EDIT_WordBreakProc
2598 * Find the beginning of words.
2599 * Note: unlike the specs for a WordBreakProc, this function only
2600 * allows to be called without linebreaks between s[0] up to
2601 * s[count - 1]. Remember it is only called
2602 * internally, so we can decide this for ourselves.
2605 static INT EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2609 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2619 if (s[index] == ' ') {
2620 while (index && (s[index] == ' '))
2623 while (index && (s[index] != ' '))
2625 if (s[index] == ' ')
2629 while (index && (s[index] != ' '))
2631 if (s[index] == ' ')
2641 if (s[index] == ' ')
2642 while ((index < count) && (s[index] == ' ')) index++;
2644 while (s[index] && (s[index] != ' ') && (index < count))
2646 while ((s[index] == ' ') && (index < count)) index++;
2650 case WB_ISDELIMITER:
2651 ret = (s[index] == ' ');
2654 ERR("unknown action code, please report !\n");
2661 /*********************************************************************
2665 * returns line number (not index) in high-order word of result.
2666 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2667 * to Richedit, not to the edit control. Original documentation is valid.
2668 * FIXME: do the specs mean to return -1 if outside client area or
2669 * if outside formatting rectangle ???
2672 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2680 GetClientRect(es->hwndSelf, &rc);
2681 if (!PtInRect(&rc, pt))
2684 index = EDIT_CharFromPos(es, x, y, NULL);
2685 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2689 /*********************************************************************
2693 * Enable or disable soft breaks.
2695 * This means: insert or remove the soft linebreak character (\r\r\n).
2696 * Take care to check if the text still fits the buffer after insertion.
2697 * If not, notify with EN_ERRSPACE.
2700 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2702 es->flags &= ~EF_USE_SOFTBRK;
2704 es->flags |= EF_USE_SOFTBRK;
2705 FIXME("soft break enabled, not implemented\n");
2711 /*********************************************************************
2715 * Hopefully this won't fire back at us.
2716 * We always start with a fixed buffer in the local heap.
2717 * Despite of the documentation says that the local heap is used
2718 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2719 * buffer on the local heap.
2722 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2726 if (!(es->style & ES_MULTILINE))
2730 hLocal = es->hloc32W;
2736 UINT countA, alloc_size;
2737 TRACE("Allocating 32-bit ANSI alias buffer\n");
2738 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2739 alloc_size = ROUND_TO_GROW(countA);
2740 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2742 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2745 textA = LocalLock(es->hloc32A);
2746 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2747 LocalUnlock(es->hloc32A);
2749 hLocal = es->hloc32A;
2752 es->flags |= EF_APP_HAS_HANDLE;
2753 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2758 /*********************************************************************
2762 * Hopefully this won't fire back at us.
2763 * We always start with a buffer in 32 bit linear memory.
2764 * However, with this message a 16 bit application requests
2765 * a handle of 16 bit local heap memory, where it expects to find
2767 * It's a pitty that from this moment on we have to use this
2768 * local heap, because applications may rely on the handle
2771 * In this function we'll try to switch to local heap.
2773 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2776 UINT countA, alloc_size;
2777 STACK16FRAME* stack16;
2780 if (!(es->style & ES_MULTILINE))
2786 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2787 oldDS = stack16->ds;
2788 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2790 if (!LocalHeapSize16()) {
2792 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2793 ERR("could not initialize local heap\n");
2796 TRACE("local heap initialized\n");
2799 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2800 alloc_size = ROUND_TO_GROW(countA);
2802 TRACE("Allocating 16-bit ANSI alias buffer\n");
2803 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2804 ERR("could not allocate new 16 bit buffer\n");
2808 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2809 ERR("could not lock new 16 bit buffer\n");
2810 LocalFree16(es->hloc16);
2815 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2816 LocalUnlock16(es->hloc16);
2817 es->flags |= EF_APP_HAS_HANDLE;
2819 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2822 stack16->ds = oldDS;
2827 /*********************************************************************
2832 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2835 INT line_len, dst_len;
2838 if (es->style & ES_MULTILINE) {
2839 if (line >= es->line_count)
2843 i = EDIT_EM_LineIndex(es, line);
2845 line_len = EDIT_EM_LineLength(es, i);
2846 dst_len = *(WORD *)dst;
2849 if(dst_len <= line_len)
2851 memcpy(dst, src, dst_len * sizeof(WCHAR));
2854 else /* Append 0 if enough space */
2856 memcpy(dst, src, line_len * sizeof(WCHAR));
2863 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2864 if(!ret && line_len) /* Insufficient buffer size */
2866 if(ret < dst_len) /* Append 0 if enough space */
2867 ((LPSTR)dst)[ret] = 0;
2873 /*********************************************************************
2878 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2880 UINT s = es->selection_start;
2881 UINT e = es->selection_end;
2888 return MAKELONG(s, e);
2892 /*********************************************************************
2896 * FIXME: is this right ? (or should it be only VSCROLL)
2897 * (and maybe only for edit controls that really have their
2898 * own scrollbars) (and maybe only for multiline controls ?)
2899 * All in all: very poorly documented
2902 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2904 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2905 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2909 /*********************************************************************
2914 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2919 if (!(es->style & ES_MULTILINE))
2921 if (index > (INT)get_text_length(es))
2922 return es->line_count - 1;
2924 index = min(es->selection_start, es->selection_end);
2927 line_def = es->first_line_def;
2928 index -= line_def->length;
2929 while ((index >= 0) && line_def->next) {
2931 line_def = line_def->next;
2932 index -= line_def->length;
2938 /*********************************************************************
2943 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
2946 const LINEDEF *line_def;
2948 if (!(es->style & ES_MULTILINE))
2950 if (line >= es->line_count)
2954 line_def = es->first_line_def;
2956 INT index = es->selection_end - line_def->length;
2957 while ((index >= 0) && line_def->next) {
2958 line_index += line_def->length;
2959 line_def = line_def->next;
2960 index -= line_def->length;
2964 line_index += line_def->length;
2965 line_def = line_def->next;
2973 /*********************************************************************
2978 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2982 if (!(es->style & ES_MULTILINE))
2983 return get_text_length(es);
2986 /* get the number of remaining non-selected chars of selected lines */
2987 INT32 l; /* line number */
2988 INT32 li; /* index of first char in line */
2990 l = EDIT_EM_LineFromChar(es, es->selection_start);
2991 /* # chars before start of selection area */
2992 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2993 l = EDIT_EM_LineFromChar(es, es->selection_end);
2994 /* # chars after end of selection */
2995 li = EDIT_EM_LineIndex(es, l);
2996 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2999 line_def = es->first_line_def;
3000 index -= line_def->length;
3001 while ((index >= 0) && line_def->next) {
3002 line_def = line_def->next;
3003 index -= line_def->length;
3005 return line_def->net_length;
3009 /*********************************************************************
3013 * NOTE: dx is in average character widths, dy - in lines;
3016 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
3018 if (!(es->style & ES_MULTILINE))
3021 dx *= es->char_width;
3022 return EDIT_EM_LineScroll_internal(es, dx, dy);
3025 /*********************************************************************
3027 * EDIT_EM_LineScroll_internal
3029 * Version of EDIT_EM_LineScroll for internal use.
3030 * It doesn't refuse if ES_MULTILINE is set and assumes that
3031 * dx is in pixels, dy - in lines.
3034 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3037 INT x_offset_in_pixels;
3038 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3041 if (es->style & ES_MULTILINE)
3043 x_offset_in_pixels = es->x_offset;
3048 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3051 if (-dx > x_offset_in_pixels)
3052 dx = -x_offset_in_pixels;
3053 if (dx > es->text_width - x_offset_in_pixels)
3054 dx = es->text_width - x_offset_in_pixels;
3055 nyoff = max(0, es->y_offset + dy);
3056 if (nyoff >= es->line_count - lines_per_page)
3057 nyoff = max(0, es->line_count - lines_per_page);
3058 dy = (es->y_offset - nyoff) * es->line_height;
3063 es->y_offset = nyoff;
3064 if(es->style & ES_MULTILINE)
3067 es->x_offset += dx / es->char_width;
3069 GetClientRect(es->hwndSelf, &rc1);
3070 IntersectRect(&rc, &rc1, &es->format_rect);
3071 ScrollWindowEx(es->hwndSelf, -dx, dy,
3072 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3073 /* force scroll info update */
3074 EDIT_UpdateScrollInfo(es);
3076 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3077 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3078 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3079 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3084 /*********************************************************************
3089 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3091 INT len = get_text_length(es);
3104 index = min(index, len);
3105 dc = GetDC(es->hwndSelf);
3107 old_font = SelectObject(dc, es->font);
3108 if (es->style & ES_MULTILINE) {
3109 l = EDIT_EM_LineFromChar(es, index);
3110 y = (l - es->y_offset) * es->line_height;
3111 li = EDIT_EM_LineIndex(es, l);
3112 if (after_wrap && (li == index) && l) {
3114 line_def = es->first_line_def;
3116 line_def = line_def->next;
3119 if (line_def->ending == END_WRAP) {
3121 y -= es->line_height;
3122 li = EDIT_EM_LineIndex(es, l);
3126 line_def = es->first_line_def;
3127 while (line_def->index != li)
3128 line_def = line_def->next;
3130 ll = line_def->net_length;
3131 lw = line_def->width;
3133 w = es->format_rect.right - es->format_rect.left;
3134 if (es->style & ES_RIGHT)
3136 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3137 es->tabs_count, es->tabs)) - es->x_offset;
3140 else if (es->style & ES_CENTER)
3142 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3143 es->tabs_count, es->tabs)) - es->x_offset;
3148 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3149 es->tabs_count, es->tabs)) - es->x_offset;
3152 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3153 if (index < es->x_offset) {
3154 GetTextExtentPoint32W(dc, text + index,
3155 es->x_offset - index, &size);
3158 GetTextExtentPoint32W(dc, text + es->x_offset,
3159 index - es->x_offset, &size);
3162 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3164 w = es->format_rect.right - es->format_rect.left;
3165 if (w > es->text_width)
3167 if (es->style & ES_RIGHT)
3168 x += w - es->text_width;
3169 else if (es->style & ES_CENTER)
3170 x += (w - es->text_width) / 2;
3175 if (es->style & ES_PASSWORD)
3176 HeapFree(GetProcessHeap(), 0, text);
3178 x += es->format_rect.left;
3179 y += es->format_rect.top;
3181 SelectObject(dc, old_font);
3182 ReleaseDC(es->hwndSelf, dc);
3183 return MAKELONG((INT16)x, (INT16)y);
3187 /*********************************************************************
3191 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3194 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3196 UINT strl = strlenW(lpsz_replace);
3197 UINT tl = get_text_length(es);
3208 TRACE("%s, can_undo %d, send_update %d\n",
3209 debugstr_w(lpsz_replace), can_undo, send_update);
3211 s = es->selection_start;
3212 e = es->selection_end;
3214 if ((s == e) && !strl)
3219 size = tl - (e - s) + strl;
3223 /* Issue the EN_MAXTEXT notification and continue with replacing text
3224 * such that buffer limit is honored. */
3225 if ((honor_limit) && (size > es->buffer_limit)) {
3226 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3227 /* Buffer limit can be smaller than the actual length of text in combobox */
3228 if (es->buffer_limit < (tl - (e-s)))
3231 strl = es->buffer_limit - (tl - (e-s));
3234 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3238 /* there is something to be deleted */
3239 TRACE("deleting stuff.\n");
3241 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3243 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3244 buf[bufl] = 0; /* ensure 0 termination */
3246 strcpyW(es->text + s, es->text + e);
3247 text_buffer_changed(es);
3250 /* there is an insertion */
3251 tl = get_text_length(es);
3252 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));
3253 for (p = es->text + tl ; p >= es->text + s ; p--)
3255 for (i = 0 , p = es->text + s ; i < strl ; i++)
3256 p[i] = lpsz_replace[i];
3257 if(es->style & ES_UPPERCASE)
3258 CharUpperBuffW(p, strl);
3259 else if(es->style & ES_LOWERCASE)
3260 CharLowerBuffW(p, strl);
3261 text_buffer_changed(es);
3263 if (es->style & ES_MULTILINE)
3265 INT st = min(es->selection_start, es->selection_end);
3266 INT vlc = get_vertical_line_count(es);
3268 hrgn = CreateRectRgn(0, 0, 0, 0);
3269 EDIT_BuildLineDefs_ML(es, st, st + strl,
3270 strl - abs(es->selection_end - es->selection_start), hrgn);
3271 /* if text is too long undo all changes */
3272 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3274 strcpyW(es->text + e, es->text + e + strl);
3276 for (i = 0 , p = es->text ; i < e - s ; i++)
3278 text_buffer_changed(es);
3279 EDIT_BuildLineDefs_ML(es, s, e,
3280 abs(es->selection_end - es->selection_start) - strl, hrgn);
3283 hrgn = CreateRectRgn(0, 0, 0, 0);
3284 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3288 INT fw = es->format_rect.right - es->format_rect.left;
3289 EDIT_CalcLineWidth_SL(es);
3290 /* remove chars that don't fit */
3291 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3292 while ((es->text_width > fw) && s + strl >= s) {
3293 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3295 EDIT_CalcLineWidth_SL(es);
3297 text_buffer_changed(es);
3298 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3304 utl = strlenW(es->undo_text);
3305 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3306 /* undo-buffer is extended to the right */
3307 EDIT_MakeUndoFit(es, utl + e - s);
3308 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3309 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3310 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3311 /* undo-buffer is extended to the left */
3312 EDIT_MakeUndoFit(es, utl + e - s);
3313 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3315 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3317 es->undo_position = s;
3319 /* new undo-buffer */
3320 EDIT_MakeUndoFit(es, e - s);
3321 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3322 es->undo_text[e - s] = 0; /* ensure 0 termination */
3323 es->undo_position = s;
3325 /* any deletion makes the old insertion-undo invalid */
3326 es->undo_insert_count = 0;
3328 EDIT_EM_EmptyUndoBuffer(es);
3332 if ((s == es->undo_position) ||
3333 ((es->undo_insert_count) &&
3334 (s == es->undo_position + es->undo_insert_count)))
3336 * insertion is new and at delete position or
3337 * an extension to either left or right
3339 es->undo_insert_count += strl;
3341 /* new insertion undo */
3342 es->undo_position = s;
3343 es->undo_insert_count = strl;
3344 /* new insertion makes old delete-buffer invalid */
3345 *es->undo_text = '\0';
3348 EDIT_EM_EmptyUndoBuffer(es);
3352 HeapFree(GetProcessHeap(), 0, buf);
3356 /* If text has been deleted and we're right or center aligned then scroll rightward */
3357 if (es->style & (ES_RIGHT | ES_CENTER))
3359 INT delta = strl - abs(es->selection_end - es->selection_start);
3361 if (delta < 0 && es->x_offset)
3363 if (abs(delta) > es->x_offset)
3366 es->x_offset += delta;
3370 EDIT_EM_SetSel(es, s, s, FALSE);
3371 es->flags |= EF_MODIFIED;
3372 if (send_update) es->flags |= EF_UPDATE;
3375 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3379 EDIT_UpdateText(es, NULL, TRUE);
3381 EDIT_EM_ScrollCaret(es);
3383 /* force scroll info update */
3384 EDIT_UpdateScrollInfo(es);
3387 if(send_update || (es->flags & EF_UPDATE))
3389 es->flags &= ~EF_UPDATE;
3390 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3395 /*********************************************************************
3400 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3404 if (!(es->style & ES_MULTILINE))
3405 return (LRESULT)FALSE;
3415 if (es->y_offset < es->line_count - 1)
3420 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3423 if (es->y_offset < es->line_count - 1)
3424 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3427 return (LRESULT)FALSE;
3430 INT vlc = get_vertical_line_count(es);
3431 /* check if we are going to move too far */
3432 if(es->y_offset + dy > es->line_count - vlc)
3433 dy = es->line_count - vlc - es->y_offset;
3435 /* Notification is done in EDIT_EM_LineScroll */
3437 EDIT_EM_LineScroll(es, 0, dy);
3439 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3443 /*********************************************************************
3448 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3450 if (es->style & ES_MULTILINE) {
3454 INT cw = es->char_width;
3459 l = EDIT_EM_LineFromChar(es, es->selection_end);
3460 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3461 vlc = get_vertical_line_count(es);
3462 if (l >= es->y_offset + vlc)
3463 dy = l - vlc + 1 - es->y_offset;
3464 if (l < es->y_offset)
3465 dy = l - es->y_offset;
3466 ww = es->format_rect.right - es->format_rect.left;
3467 if (x < es->format_rect.left)
3468 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3469 if (x > es->format_rect.right)
3470 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3471 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3473 /* check if we are going to move too far */
3474 if(es->x_offset + dx + ww > es->text_width)
3475 dx = es->text_width - ww - es->x_offset;
3476 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3477 EDIT_EM_LineScroll_internal(es, dx, dy);
3484 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3485 format_width = es->format_rect.right - es->format_rect.left;
3486 if (x < es->format_rect.left) {
3487 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3490 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3491 } while ((x < goal) && es->x_offset);
3492 /* FIXME: use ScrollWindow() somehow to improve performance */
3493 EDIT_UpdateText(es, NULL, TRUE);
3494 } else if (x > es->format_rect.right) {
3496 INT len = get_text_length(es);
3497 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3500 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3501 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3502 } while ((x > goal) && (x_last > es->format_rect.right));
3503 /* FIXME: use ScrollWindow() somehow to improve performance */
3504 EDIT_UpdateText(es, NULL, TRUE);
3508 if(es->flags & EF_FOCUSED)
3509 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3513 /*********************************************************************
3517 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3520 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3522 if (!(es->style & ES_MULTILINE))
3526 WARN("called with NULL handle\n");
3530 EDIT_UnlockBuffer(es, TRUE);
3534 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3535 HANDLE16 oldDS = stack16->ds;
3537 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3538 LocalFree16(es->hloc16);
3539 stack16->ds = oldDS;
3547 LocalFree(es->hloc32A);
3559 countA = LocalSize(hloc);
3560 textA = LocalLock(hloc);
3561 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3562 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3564 ERR("Could not allocate new unicode buffer\n");
3567 textW = LocalLock(hloc32W_new);
3568 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3569 LocalUnlock(hloc32W_new);
3573 LocalFree(es->hloc32W);
3575 es->hloc32W = hloc32W_new;
3579 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3581 es->flags |= EF_APP_HAS_HANDLE;
3582 EDIT_LockBuffer(es);
3584 es->x_offset = es->y_offset = 0;
3585 es->selection_start = es->selection_end = 0;
3586 EDIT_EM_EmptyUndoBuffer(es);
3587 es->flags &= ~EF_MODIFIED;
3588 es->flags &= ~EF_UPDATE;
3589 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3590 EDIT_UpdateText(es, NULL, TRUE);
3591 EDIT_EM_ScrollCaret(es);
3592 /* force scroll info update */
3593 EDIT_UpdateScrollInfo(es);
3597 /*********************************************************************
3601 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3604 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3606 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3607 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3608 HANDLE16 oldDS = stack16->ds;
3614 if (!(es->style & ES_MULTILINE))
3618 WARN("called with NULL handle\n");
3622 EDIT_UnlockBuffer(es, TRUE);
3626 LocalFree(es->hloc32A);
3630 stack16->ds = hInstance;
3631 countA = LocalSize16(hloc);
3632 textA = MapSL(LocalLock16(hloc));
3633 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3634 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3636 ERR("Could not allocate new unicode buffer\n");
3639 textW = LocalLock(hloc32W_new);
3640 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3641 LocalUnlock(hloc32W_new);
3642 LocalUnlock16(hloc);
3643 stack16->ds = oldDS;
3646 LocalFree(es->hloc32W);
3648 es->hloc32W = hloc32W_new;
3651 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3653 es->flags |= EF_APP_HAS_HANDLE;
3654 EDIT_LockBuffer(es);
3656 es->x_offset = es->y_offset = 0;
3657 es->selection_start = es->selection_end = 0;
3658 EDIT_EM_EmptyUndoBuffer(es);
3659 es->flags &= ~EF_MODIFIED;
3660 es->flags &= ~EF_UPDATE;
3661 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3662 EDIT_UpdateText(es, NULL, TRUE);
3663 EDIT_EM_ScrollCaret(es);
3664 /* force scroll info update */
3665 EDIT_UpdateScrollInfo(es);
3669 /*********************************************************************
3673 * NOTE: this version currently implements WinNT limits
3676 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
3678 if (!limit) limit = ~0u;
3679 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
3680 es->buffer_limit = limit;
3684 /*********************************************************************
3688 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3689 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3690 * margin according to the textmetrics of the current font.
3692 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3693 * of the char's width as the margin, but this is not how Windows handles this.
3694 * For all other fonts Windows sets the margins to zero.
3696 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3697 * edit control is equal to or larger than a certain size.
3698 * Interestingly if one subtracts both the left and right margins from
3699 * this size one always seems to get an even number. The extents of
3700 * the (four character) string "'**'" match this quite closely, so
3701 * we'll use this until we come up with a better idea.
3703 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3705 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3708 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3709 return sz.cx + left + right;
3712 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3713 WORD left, WORD right, BOOL repaint)
3716 INT default_left_margin = 0; /* in pixels */
3717 INT default_right_margin = 0; /* in pixels */
3719 /* Set the default margins depending on the font */
3720 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3721 HDC dc = GetDC(es->hwndSelf);
3722 HFONT old_font = SelectObject(dc, es->font);
3723 GetTextMetricsW(dc, &tm);
3724 /* The default margins are only non zero for TrueType or Vector fonts */
3725 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3728 /* This must be calculated more exactly! But how? */
3729 default_left_margin = tm.tmAveCharWidth / 2;
3730 default_right_margin = tm.tmAveCharWidth / 2;
3731 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3732 GetClientRect(es->hwndSelf, &rc);
3733 if(rc.right - rc.left < min_size) {
3734 default_left_margin = es->left_margin;
3735 default_right_margin = es->right_margin;
3738 SelectObject(dc, old_font);
3739 ReleaseDC(es->hwndSelf, dc);
3742 if (action & EC_LEFTMARGIN) {
3743 es->format_rect.left -= es->left_margin;
3744 if (left != EC_USEFONTINFO)
3745 es->left_margin = left;
3747 es->left_margin = default_left_margin;
3748 es->format_rect.left += es->left_margin;
3751 if (action & EC_RIGHTMARGIN) {
3752 es->format_rect.right += es->right_margin;
3753 if (right != EC_USEFONTINFO)
3754 es->right_margin = right;
3756 es->right_margin = default_right_margin;
3757 es->format_rect.right -= es->right_margin;
3760 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3761 EDIT_AdjustFormatRect(es);
3762 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3765 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3769 /*********************************************************************
3771 * EM_SETPASSWORDCHAR
3774 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3778 if (es->style & ES_MULTILINE)
3781 if (es->password_char == c)
3784 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3785 es->password_char = c;
3787 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3788 es->style |= ES_PASSWORD;
3790 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3791 es->style &= ~ES_PASSWORD;
3793 EDIT_UpdateText(es, NULL, TRUE);
3797 /*********************************************************************
3801 * note: unlike the specs say: the order of start and end
3802 * _is_ preserved in Windows. (i.e. start can be > end)
3803 * In other words: this handler is OK
3806 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3808 UINT old_start = es->selection_start;
3809 UINT old_end = es->selection_end;
3810 UINT len = get_text_length(es);
3812 if (start == (UINT)-1) {
3813 start = es->selection_end;
3814 end = es->selection_end;
3816 start = min(start, len);
3817 end = min(end, len);
3819 es->selection_start = start;
3820 es->selection_end = end;
3822 es->flags |= EF_AFTER_WRAP;
3824 es->flags &= ~EF_AFTER_WRAP;
3825 /* Compute the necessary invalidation region. */
3826 /* Note that we don't need to invalidate regions which have
3827 * "never" been selected, or those which are "still" selected.
3828 * In fact, every time we hit a selection boundary, we can
3829 * *toggle* whether we need to invalidate. Thus we can optimize by
3830 * *sorting* the interval endpoints. Let's assume that we sort them
3832 * start <= end <= old_start <= old_end
3833 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
3834 * in 5 comparisons; i.e. it is impossible to do better than the
3836 ORDER_UINT(end, old_end);
3837 ORDER_UINT(start, old_start);
3838 ORDER_UINT(old_start, old_end);
3839 ORDER_UINT(start, end);
3840 /* Note that at this point 'end' and 'old_start' are not in order, but
3841 * start is definitely the min. and old_end is definitely the max. */
3842 if (end != old_start)
3846 * ORDER_UINT32(end, old_start);
3847 * EDIT_InvalidateText(es, start, end);
3848 * EDIT_InvalidateText(es, old_start, old_end);
3849 * in place of the following if statement.
3850 * (That would complete the optimal five-comparison four-element sort.)
3852 if (old_start > end )
3854 EDIT_InvalidateText(es, start, end);
3855 EDIT_InvalidateText(es, old_start, old_end);
3859 EDIT_InvalidateText(es, start, old_start);
3860 EDIT_InvalidateText(es, end, old_end);
3863 else EDIT_InvalidateText(es, start, old_end);
3867 /*********************************************************************
3872 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3874 if (!(es->style & ES_MULTILINE))
3876 HeapFree(GetProcessHeap(), 0, es->tabs);
3877 es->tabs_count = count;
3881 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3882 memcpy(es->tabs, tabs, count * sizeof(INT));
3888 /*********************************************************************
3893 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs)
3895 if (!(es->style & ES_MULTILINE))
3897 HeapFree(GetProcessHeap(), 0, es->tabs);
3898 es->tabs_count = count;
3903 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3904 for (i = 0 ; i < count ; i++)
3905 es->tabs[i] = *tabs++;
3911 /*********************************************************************
3913 * EM_SETWORDBREAKPROC
3916 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3918 if (es->word_break_proc == wbp)
3921 es->word_break_proc = wbp;
3922 es->word_break_proc16 = NULL;
3924 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3925 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3926 EDIT_UpdateText(es, NULL, TRUE);
3931 /*********************************************************************
3933 * EM_SETWORDBREAKPROC16
3936 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3938 if (es->word_break_proc16 == wbp)
3941 es->word_break_proc = NULL;
3942 es->word_break_proc16 = wbp;
3943 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3944 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3945 EDIT_UpdateText(es, NULL, TRUE);
3950 /*********************************************************************
3955 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3960 /* As per MSDN spec, for a single-line edit control,
3961 the return value is always TRUE */
3962 if( es->style & ES_READONLY )
3963 return !(es->style & ES_MULTILINE);
3965 ulength = strlenW(es->undo_text);
3967 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3969 strcpyW(utext, es->undo_text);
3971 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3972 es->undo_insert_count, debugstr_w(utext));
3974 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3975 EDIT_EM_EmptyUndoBuffer(es);
3976 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3977 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3978 /* send the notification after the selection start and end are set */
3979 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3980 EDIT_EM_ScrollCaret(es);
3981 HeapFree(GetProcessHeap(), 0, utext);
3983 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3984 es->undo_insert_count, debugstr_w(es->undo_text));
3989 /* Helper function for WM_CHAR
3991 * According to an MSDN blog article titled "Just because you're a control
3992 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3993 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3994 * a dialog box or not.
3996 static BOOL EDIT_IsInsideDialog(EDITSTATE *es)
4003 pParent = WIN_GetPtr(es->hwndParent);
4004 if (pParent && pParent != WND_OTHER_PROCESS && pParent != WND_DESKTOP)
4006 if (pParent->flags & WIN_ISDIALOG)
4008 WIN_ReleasePtr(pParent);
4015 /*********************************************************************
4020 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
4024 control = GetKeyState(VK_CONTROL) & 0x8000;
4028 /* If it's not a multiline edit box, it would be ignored below.
4029 * For multiline edit without ES_WANTRETURN, we have to make a
4032 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
4033 if (EDIT_IsInsideDialog(es))
4036 if (es->style & ES_MULTILINE) {
4037 if (es->style & ES_READONLY) {
4038 EDIT_MoveHome(es, FALSE, FALSE);
4039 EDIT_MoveDown_ML(es, FALSE);
4041 static const WCHAR cr_lfW[] = {'\r','\n',0};
4042 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
4047 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4049 static const WCHAR tabW[] = {'\t',0};
4050 if (EDIT_IsInsideDialog(es))
4052 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4056 if (!(es->style & ES_READONLY) && !control) {
4057 if (es->selection_start != es->selection_end)
4060 /* delete character left of caret */
4061 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4062 EDIT_MoveBackward(es, TRUE);
4068 if (!(es->style & ES_PASSWORD))
4069 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4072 if (!(es->style & ES_READONLY))
4073 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4076 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
4077 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4080 if (!(es->style & ES_READONLY))
4081 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
4085 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4086 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4089 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4093 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4101 /*********************************************************************
4106 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4108 if (code || control)
4113 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
4116 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4119 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4122 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4125 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
4128 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4129 EDIT_EM_ScrollCaret(es);
4132 ERR("unknown menu item, please report\n");
4138 /*********************************************************************
4142 * Note: the resource files resource/sysres_??.rc cannot define a
4143 * single popup menu. Hence we use a (dummy) menubar
4144 * containing the single popup menu as its first item.
4146 * FIXME: the message identifiers have been chosen arbitrarily,
4147 * hence we use MF_BYPOSITION.
4148 * We might as well use the "real" values (anybody knows ?)
4149 * The menu definition is in resources/sysres_??.rc.
4150 * Once these are OK, we better use MF_BYCOMMAND here
4151 * (as we do in EDIT_WM_Command()).
4154 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4156 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4157 HMENU popup = GetSubMenu(menu, 0);
4158 UINT start = es->selection_start;
4159 UINT end = es->selection_end;
4161 ORDER_UINT(start, end);
4164 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4166 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4168 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4170 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4172 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4174 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4176 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4179 /* Windows places the menu at the edit's center in this case */
4180 GetClientRect(es->hwndSelf, &rc);
4181 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4182 x = rc.left + (rc.right - rc.left) / 2;
4183 y = rc.top + (rc.bottom - rc.top) / 2;
4186 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4191 /*********************************************************************
4196 static void EDIT_WM_Copy(EDITSTATE *es)
4198 INT s = min(es->selection_start, es->selection_end);
4199 INT e = max(es->selection_start, es->selection_end);
4207 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4208 dst = GlobalLock(hdst);
4209 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4210 dst[len] = 0; /* ensure 0 termination */
4211 TRACE("%s\n", debugstr_w(dst));
4213 OpenClipboard(es->hwndSelf);
4215 SetClipboardData(CF_UNICODETEXT, hdst);
4220 /*********************************************************************
4225 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4229 TRACE("%s\n", debugstr_w(name));
4231 * To initialize some final structure members, we call some helper
4232 * functions. However, since the EDITSTATE is not consistent (i.e.
4233 * not fully initialized), we should be very careful which
4234 * functions can be called, and in what order.
4236 EDIT_WM_SetFont(es, 0, FALSE);
4237 EDIT_EM_EmptyUndoBuffer(es);
4239 /* We need to calculate the format rect
4240 (applications may send EM_SETMARGINS before the control gets visible) */
4241 GetClientRect(es->hwndSelf, &clientRect);
4242 EDIT_SetRectNP(es, &clientRect);
4244 if (name && *name) {
4245 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4246 /* if we insert text to the editline, the text scrolls out
4247 * of the window, as the caret is placed after the insert
4248 * pos normally; thus we reset es->selection... to 0 and
4251 es->selection_start = es->selection_end = 0;
4252 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4253 * Messages are only to be sent when the USER does something to
4254 * change the contents. So I am removing this EN_CHANGE
4256 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4258 EDIT_EM_ScrollCaret(es);
4260 /* force scroll info update */
4261 EDIT_UpdateScrollInfo(es);
4262 /* The rule seems to return 1 here for success */
4263 /* Power Builder masked edit controls will crash */
4265 /* FIXME: is that in all cases so ? */
4270 /*********************************************************************
4275 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4280 LocalFree(es->hloc32W);
4283 LocalFree(es->hloc32A);
4286 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4287 HANDLE16 oldDS = stack16->ds;
4289 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4290 while (LocalUnlock16(es->hloc16)) ;
4291 LocalFree16(es->hloc16);
4292 stack16->ds = oldDS;
4295 pc = es->first_line_def;
4299 HeapFree(GetProcessHeap(), 0, pc);
4303 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4304 HeapFree(GetProcessHeap(), 0, es);
4310 /*********************************************************************
4315 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4317 if(!count) return 0;
4321 lstrcpynW(dst, es->text, count);
4322 return strlenW(dst);
4326 LPSTR textA = (LPSTR)dst;
4327 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4328 textA[count - 1] = 0; /* ensure 0 termination */
4329 return strlen(textA);
4333 /*********************************************************************
4338 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4343 if (!(es->style & ES_MULTILINE))
4346 if (!(es->style & ES_AUTOHSCROLL))
4350 fw = es->format_rect.right - es->format_rect.left;
4353 TRACE("SB_LINELEFT\n");
4355 dx = -es->char_width;
4358 TRACE("SB_LINERIGHT\n");
4359 if (es->x_offset < es->text_width)
4360 dx = es->char_width;
4363 TRACE("SB_PAGELEFT\n");
4365 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4368 TRACE("SB_PAGERIGHT\n");
4369 if (es->x_offset < es->text_width)
4370 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4378 TRACE("SB_RIGHT\n");
4379 if (es->x_offset < es->text_width)
4380 dx = es->text_width - es->x_offset;
4383 TRACE("SB_THUMBTRACK %d\n", pos);
4384 es->flags |= EF_HSCROLL_TRACK;
4385 if(es->style & WS_HSCROLL)
4386 dx = pos - es->x_offset;
4391 if(pos < 0 || pos > 100) return 0;
4392 /* Assume default scroll range 0-100 */
4393 fw = es->format_rect.right - es->format_rect.left;
4394 new_x = pos * (es->text_width - fw) / 100;
4395 dx = es->text_width ? (new_x - es->x_offset) : 0;
4398 case SB_THUMBPOSITION:
4399 TRACE("SB_THUMBPOSITION %d\n", pos);
4400 es->flags &= ~EF_HSCROLL_TRACK;
4401 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4402 dx = pos - es->x_offset;
4407 if(pos < 0 || pos > 100) return 0;
4408 /* Assume default scroll range 0-100 */
4409 fw = es->format_rect.right - es->format_rect.left;
4410 new_x = pos * (es->text_width - fw) / 100;
4411 dx = es->text_width ? (new_x - es->x_offset) : 0;
4414 /* force scroll info update */
4415 EDIT_UpdateScrollInfo(es);
4416 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4420 TRACE("SB_ENDSCROLL\n");
4423 * FIXME : the next two are undocumented !
4424 * Are we doing the right thing ?
4425 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4426 * although it's also a regular control message.
4428 case EM_GETTHUMB: /* this one is used by NT notepad */
4432 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4433 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4436 /* Assume default scroll range 0-100 */
4437 INT fw = es->format_rect.right - es->format_rect.left;
4438 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4440 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4443 case EM_LINESCROLL16:
4444 TRACE("EM_LINESCROLL16\n");
4449 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4455 INT fw = es->format_rect.right - es->format_rect.left;
4456 /* check if we are going to move too far */
4457 if(es->x_offset + dx + fw > es->text_width)
4458 dx = es->text_width - fw - es->x_offset;
4460 EDIT_EM_LineScroll_internal(es, dx, 0);
4466 /*********************************************************************
4471 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4473 HWND hLBox = es->hwndListBox;
4481 hCombo = GetParent(es->hwndSelf);
4485 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4487 if (key == VK_UP || key == VK_DOWN)
4489 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4492 if (msg == WM_KEYDOWN || nEUI)
4493 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4499 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4501 /* make sure ComboLBox pops up */
4502 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4507 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4510 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4512 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4514 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4519 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4525 /*********************************************************************
4529 * Handling of special keys that don't produce a WM_CHAR
4530 * (i.e. non-printable keys) & Backspace & Delete
4533 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4538 if (GetKeyState(VK_MENU) & 0x8000)
4541 shift = GetKeyState(VK_SHIFT) & 0x8000;
4542 control = GetKeyState(VK_CONTROL) & 0x8000;
4547 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4552 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4553 EDIT_MoveUp_ML(es, shift);
4556 EDIT_MoveWordBackward(es, shift);
4558 EDIT_MoveBackward(es, shift);
4561 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4565 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4566 EDIT_MoveDown_ML(es, shift);
4568 EDIT_MoveWordForward(es, shift);
4570 EDIT_MoveForward(es, shift);
4573 EDIT_MoveHome(es, shift, control);
4576 EDIT_MoveEnd(es, shift, control);
4579 if (es->style & ES_MULTILINE)
4580 EDIT_MovePageUp_ML(es, shift);
4582 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4585 if (es->style & ES_MULTILINE)
4586 EDIT_MovePageDown_ML(es, shift);
4588 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4591 if (!(es->style & ES_READONLY) && !(shift && control)) {
4592 if (es->selection_start != es->selection_end) {
4599 /* delete character left of caret */
4600 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4601 EDIT_MoveBackward(es, TRUE);
4603 } else if (control) {
4604 /* delete to end of line */
4605 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4606 EDIT_MoveEnd(es, TRUE, FALSE);
4609 /* delete character right of caret */
4610 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4611 EDIT_MoveForward(es, TRUE);
4619 if (!(es->style & ES_READONLY))
4625 /* If the edit doesn't want the return send a message to the default object */
4626 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
4631 if (!EDIT_IsInsideDialog(es)) return 1;
4633 hwndParent = GetParent(es->hwndSelf);
4634 dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4635 if (HIWORD(dw) == DC_HASDEFID)
4637 SendMessageW( hwndParent, WM_COMMAND,
4638 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4639 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4642 SendMessageW( hwndParent, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndParent, IDOK ) );
4646 if (!(es->style & ES_MULTILINE))
4647 SendMessageW(GetParent(es->hwndSelf), WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( GetParent(es->hwndSelf), IDCANCEL ) );
4650 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
4657 /*********************************************************************
4662 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4664 es->flags &= ~EF_FOCUSED;
4666 if(!(es->style & ES_NOHIDESEL))
4667 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4668 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4673 /*********************************************************************
4677 * The caret position has been set on the WM_LBUTTONDOWN message
4680 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4683 INT e = es->selection_end;
4688 es->bCaptureState = TRUE;
4689 SetCapture(es->hwndSelf);
4691 l = EDIT_EM_LineFromChar(es, e);
4692 li = EDIT_EM_LineIndex(es, l);
4693 ll = EDIT_EM_LineLength(es, e);
4694 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4695 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4696 EDIT_EM_SetSel(es, s, e, FALSE);
4697 EDIT_EM_ScrollCaret(es);
4698 es->region_posx = es->region_posy = 0;
4699 SetTimer(es->hwndSelf, 0, 100, NULL);
4704 /*********************************************************************
4709 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4714 es->bCaptureState = TRUE;
4715 SetCapture(es->hwndSelf);
4716 EDIT_ConfinePoint(es, &x, &y);
4717 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4718 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4719 EDIT_EM_ScrollCaret(es);
4720 es->region_posx = es->region_posy = 0;
4721 SetTimer(es->hwndSelf, 0, 100, NULL);
4723 if (!(es->flags & EF_FOCUSED))
4724 SetFocus(es->hwndSelf);
4730 /*********************************************************************
4735 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4737 if (es->bCaptureState) {
4738 KillTimer(es->hwndSelf, 0);
4739 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4741 es->bCaptureState = FALSE;
4746 /*********************************************************************
4751 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4753 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4758 /*********************************************************************
4763 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4769 /* If the mouse has been captured by process other than the edit control itself,
4770 * the windows edit controls will not select the strings with mouse move.
4772 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4776 * FIXME: gotta do some scrolling if outside client
4777 * area. Maybe reset the timer ?
4780 EDIT_ConfinePoint(es, &x, &y);
4781 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4782 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4783 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4784 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4785 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4790 /*********************************************************************
4794 * See also EDIT_WM_StyleChanged
4796 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4801 TRACE("Creating %s edit control, style = %08x\n",
4802 unicode ? "Unicode" : "ANSI", lpcs->style);
4804 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4806 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4809 * Note: since the EDITSTATE has not been fully initialized yet,
4810 * we can't use any API calls that may send
4811 * WM_XXX messages before WM_NCCREATE is completed.
4814 es->is_unicode = unicode;
4815 es->style = lpcs->style;
4817 es->bEnableState = !(es->style & WS_DISABLED);
4819 es->hwndSelf = hwnd;
4820 /* Save parent, which will be notified by EN_* messages */
4821 es->hwndParent = lpcs->hwndParent;
4823 if (es->style & ES_COMBO)
4824 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4826 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4827 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4829 /* Number overrides lowercase overrides uppercase (at least it
4830 * does in Win95). However I'll bet that ES_NUMBER would be
4831 * invalid under Win 3.1.
4833 if (es->style & ES_NUMBER) {
4834 ; /* do not override the ES_NUMBER */
4835 } else if (es->style & ES_LOWERCASE) {
4836 es->style &= ~ES_UPPERCASE;
4838 if (es->style & ES_MULTILINE) {
4839 es->buffer_limit = BUFLIMIT_INITIAL;
4840 if (es->style & WS_VSCROLL)
4841 es->style |= ES_AUTOVSCROLL;
4842 if (es->style & WS_HSCROLL)
4843 es->style |= ES_AUTOHSCROLL;
4844 es->style &= ~ES_PASSWORD;
4845 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4846 /* Confirmed - RIGHT overrides CENTER */
4847 if (es->style & ES_RIGHT)
4848 es->style &= ~ES_CENTER;
4849 es->style &= ~WS_HSCROLL;
4850 es->style &= ~ES_AUTOHSCROLL;
4853 es->buffer_limit = BUFLIMIT_INITIAL;
4854 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4855 es->style &= ~ES_CENTER;
4856 es->style &= ~WS_HSCROLL;
4857 es->style &= ~WS_VSCROLL;
4858 if (es->style & ES_PASSWORD)
4859 es->password_char = '*';
4862 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4863 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4865 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4867 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4869 es->undo_buffer_size = es->buffer_size;
4871 if (es->style & ES_MULTILINE)
4872 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4877 * In Win95 look and feel, the WS_BORDER style is replaced by the
4878 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4879 * control a nonclient area so we don't need to draw the border.
4880 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4881 * a nonclient area and we should handle painting the border ourselves.
4883 * When making modifications please ensure that the code still works
4884 * for edit controls created directly with style 0x50800000, exStyle 0
4885 * (which should have a single pixel border)
4887 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4888 es->style &= ~WS_BORDER;
4889 else if (es->style & WS_BORDER)
4890 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4895 /*********************************************************************
4900 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4913 BOOL rev = es->bEnableState &&
4914 ((es->flags & EF_FOCUSED) ||
4915 (es->style & ES_NOHIDESEL));
4916 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4918 GetClientRect(es->hwndSelf, &rcClient);
4920 /* get the background brush */
4921 brush = EDIT_NotifyCtlColor(es, dc);
4923 /* paint the border and the background */
4924 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4926 if(es->style & WS_BORDER) {
4927 bw = GetSystemMetrics(SM_CXBORDER);
4928 bh = GetSystemMetrics(SM_CYBORDER);
4930 if(es->style & ES_MULTILINE) {
4931 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4932 if(es->style & WS_VSCROLL) rc.right+=bw;
4935 /* Draw the frame. Same code as in nonclient.c */
4936 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4937 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4938 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4939 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4940 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4941 SelectObject(dc, old_brush);
4943 /* Keep the border clean */
4944 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4945 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4948 GetClipBox(dc, &rc);
4949 FillRect(dc, &rc, brush);
4951 IntersectClipRect(dc, es->format_rect.left,
4952 es->format_rect.top,
4953 es->format_rect.right,
4954 es->format_rect.bottom);
4955 if (es->style & ES_MULTILINE) {
4957 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4960 old_font = SelectObject(dc, es->font);
4962 if (!es->bEnableState)
4963 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4964 GetClipBox(dc, &rcRgn);
4965 if (es->style & ES_MULTILINE) {
4966 INT vlc = get_vertical_line_count(es);
4967 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4968 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4969 if (IntersectRect(&rc, &rcRgn, &rcLine))
4970 EDIT_PaintLine(es, dc, i, rev);
4973 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4974 if (IntersectRect(&rc, &rcRgn, &rcLine))
4975 EDIT_PaintLine(es, dc, 0, rev);
4978 SelectObject(dc, old_font);
4981 EndPaint(es->hwndSelf, &ps);
4985 /*********************************************************************
4990 static void EDIT_WM_Paste(EDITSTATE *es)
4995 /* Protect read-only edit control from modification */
4996 if(es->style & ES_READONLY)
4999 OpenClipboard(es->hwndSelf);
5000 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
5001 src = GlobalLock(hsrc);
5002 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
5005 else if (es->style & ES_PASSWORD) {
5006 /* clear selected text in password edit box even with empty clipboard */
5007 const WCHAR empty_strW[] = { 0 };
5008 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
5014 /*********************************************************************
5019 static void EDIT_WM_SetFocus(EDITSTATE *es)
5021 es->flags |= EF_FOCUSED;
5023 if (!(es->style & ES_NOHIDESEL))
5024 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
5026 /* single line edit updates itself */
5027 if (!(es->style & ES_MULTILINE))
5029 HDC hdc = GetDC(es->hwndSelf);
5030 EDIT_WM_Paint(es, hdc);
5031 ReleaseDC(es->hwndSelf, hdc);
5034 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5035 EDIT_SetCaretPos(es, es->selection_end,
5036 es->flags & EF_AFTER_WRAP);
5037 ShowCaret(es->hwndSelf);
5038 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
5042 /*********************************************************************
5046 * With Win95 look the margins are set to default font value unless
5047 * the system font (font == 0) is being set, in which case they are left
5051 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
5059 dc = GetDC(es->hwndSelf);
5061 old_font = SelectObject(dc, font);
5062 GetTextMetricsW(dc, &tm);
5063 es->line_height = tm.tmHeight;
5064 es->char_width = tm.tmAveCharWidth;
5066 SelectObject(dc, old_font);
5067 ReleaseDC(es->hwndSelf, dc);
5069 /* Reset the format rect and the margins */
5070 GetClientRect(es->hwndSelf, &clientRect);
5071 EDIT_SetRectNP(es, &clientRect);
5072 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5073 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5075 if (es->style & ES_MULTILINE)
5076 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5078 EDIT_CalcLineWidth_SL(es);
5081 EDIT_UpdateText(es, NULL, TRUE);
5082 if (es->flags & EF_FOCUSED) {
5084 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5085 EDIT_SetCaretPos(es, es->selection_end,
5086 es->flags & EF_AFTER_WRAP);
5087 ShowCaret(es->hwndSelf);
5092 /*********************************************************************
5097 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5098 * The modified flag is reset. No notifications are sent.
5100 * For single-line controls, reception of WM_SETTEXT triggers:
5101 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5104 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5106 LPWSTR textW = NULL;
5107 if (!unicode && text)
5109 LPCSTR textA = (LPCSTR)text;
5110 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5111 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5113 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5117 if (es->flags & EF_UPDATE)
5118 /* fixed this bug once; complain if we see it about to happen again. */
5119 ERR("SetSel may generate UPDATE message whose handler may reset "
5122 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5125 TRACE("%s\n", debugstr_w(text));
5126 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5128 HeapFree(GetProcessHeap(), 0, textW);
5132 static const WCHAR empty_stringW[] = {0};
5134 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5137 es->flags &= ~EF_MODIFIED;
5138 EDIT_EM_SetSel(es, 0, 0, FALSE);
5140 /* Send the notification after the selection start and end have been set
5141 * edit control doesn't send notification on WM_SETTEXT
5142 * if it is multiline, or it is part of combobox
5144 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5146 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5147 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5149 EDIT_EM_ScrollCaret(es);
5150 EDIT_UpdateScrollInfo(es);
5154 /*********************************************************************
5159 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5161 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5163 TRACE("width = %d, height = %d\n", width, height);
5164 SetRect(&rc, 0, 0, width, height);
5165 EDIT_SetRectNP(es, &rc);
5166 EDIT_UpdateText(es, NULL, TRUE);
5171 /*********************************************************************
5175 * This message is sent by SetWindowLong on having changed either the Style
5176 * or the extended style.
5178 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5180 * See also EDIT_WM_NCCreate
5182 * It appears that the Windows version of the edit control allows the style
5183 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5184 * style variable which will generally be different. In this function we
5185 * update the internal style based on what changed in the externally visible
5188 * Much of this content as based upon the MSDN, especially:
5189 * Platform SDK Documentation -> User Interface Services ->
5190 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5191 * Edit Control Styles
5193 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5195 if (GWL_STYLE == which) {
5196 DWORD style_change_mask;
5198 /* Only a subset of changes can be applied after the control
5201 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5203 if (es->style & ES_MULTILINE)
5204 style_change_mask |= ES_WANTRETURN;
5206 new_style = style->styleNew & style_change_mask;
5208 /* Number overrides lowercase overrides uppercase (at least it
5209 * does in Win95). However I'll bet that ES_NUMBER would be
5210 * invalid under Win 3.1.
5212 if (new_style & ES_NUMBER) {
5213 ; /* do not override the ES_NUMBER */
5214 } else if (new_style & ES_LOWERCASE) {
5215 new_style &= ~ES_UPPERCASE;
5218 es->style = (es->style & ~style_change_mask) | new_style;
5219 } else if (GWL_EXSTYLE == which) {
5220 ; /* FIXME - what is needed here */
5222 WARN ("Invalid style change %ld\n",which);
5228 /*********************************************************************
5233 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5235 if ((key == VK_BACK) && (key_data & 0x2000)) {
5236 if (EDIT_EM_CanUndo(es))
5239 } else if (key == VK_UP || key == VK_DOWN) {
5240 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5243 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5247 /*********************************************************************
5252 static void EDIT_WM_Timer(EDITSTATE *es)
5254 if (es->region_posx < 0) {
5255 EDIT_MoveBackward(es, TRUE);
5256 } else if (es->region_posx > 0) {
5257 EDIT_MoveForward(es, TRUE);
5260 * FIXME: gotta do some vertical scrolling here, like
5261 * EDIT_EM_LineScroll(hwnd, 0, 1);
5265 /*********************************************************************
5270 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5274 if (!(es->style & ES_MULTILINE))
5277 if (!(es->style & ES_AUTOVSCROLL))
5286 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5287 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5288 (action == SB_PAGEUP ? "SB_PAGEUP" :
5290 EDIT_EM_Scroll(es, action);
5297 TRACE("SB_BOTTOM\n");
5298 dy = es->line_count - 1 - es->y_offset;
5301 TRACE("SB_THUMBTRACK %d\n", pos);
5302 es->flags |= EF_VSCROLL_TRACK;
5303 if(es->style & WS_VSCROLL)
5304 dy = pos - es->y_offset;
5307 /* Assume default scroll range 0-100 */
5310 if(pos < 0 || pos > 100) return 0;
5311 vlc = get_vertical_line_count(es);
5312 new_y = pos * (es->line_count - vlc) / 100;
5313 dy = es->line_count ? (new_y - es->y_offset) : 0;
5314 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5315 es->line_count, es->y_offset, pos, dy);
5318 case SB_THUMBPOSITION:
5319 TRACE("SB_THUMBPOSITION %d\n", pos);
5320 es->flags &= ~EF_VSCROLL_TRACK;
5321 if(es->style & WS_VSCROLL)
5322 dy = pos - es->y_offset;
5325 /* Assume default scroll range 0-100 */
5328 if(pos < 0 || pos > 100) return 0;
5329 vlc = get_vertical_line_count(es);
5330 new_y = pos * (es->line_count - vlc) / 100;
5331 dy = es->line_count ? (new_y - es->y_offset) : 0;
5332 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5333 es->line_count, es->y_offset, pos, dy);
5337 /* force scroll info update */
5338 EDIT_UpdateScrollInfo(es);
5339 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5343 TRACE("SB_ENDSCROLL\n");
5346 * FIXME : the next two are undocumented !
5347 * Are we doing the right thing ?
5348 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5349 * although it's also a regular control message.
5351 case EM_GETTHUMB: /* this one is used by NT notepad */
5355 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5356 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5359 /* Assume default scroll range 0-100 */
5360 INT vlc = get_vertical_line_count(es);
5361 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5363 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5366 case EM_LINESCROLL16:
5367 TRACE("EM_LINESCROLL16 %d\n", pos);
5372 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5377 EDIT_EM_LineScroll(es, 0, dy);
5381 /*********************************************************************
5386 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5388 if (es->flags & EF_UPDATE) {
5389 es->flags &= ~EF_UPDATE;
5390 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5392 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5396 /*********************************************************************
5401 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
5403 if (es->flags & EF_UPDATE) {
5404 es->flags &= ~EF_UPDATE;
5405 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5407 InvalidateRect(es->hwndSelf, rc, bErase);
5410 /********************************************************************
5412 * The Following code is to handle inline editing from IMEs
5415 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
5418 LPWSTR lpCompStr = NULL;
5419 LPSTR lpCompStrAttr = NULL;
5422 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5429 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
5432 ERR("Unable to allocate IME CompositionString\n");
5437 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
5438 lpCompStr[buflen/sizeof(WCHAR)] = 0;
5440 if (CompFlag & GCS_COMPATTR)
5443 * We do not use the attributes yet. it would tell us what characters
5444 * are in transition and which are converted or decided upon
5446 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5450 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5453 ERR("Unable to allocate IME Attribute String\n");
5454 HeapFree(GetProcessHeap(),0,lpCompStr);
5457 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5459 lpCompStrAttr[dwBufLenAttr] = 0;
5462 lpCompStrAttr = NULL;
5465 /* check for change in composition start */
5466 if (es->selection_end < es->composition_start)
5467 es->composition_start = es->selection_end;
5469 /* replace existing selection string */
5470 es->selection_start = es->composition_start;
5472 if (es->composition_len > 0)
5473 es->selection_end = es->composition_start + es->composition_len;
5475 es->selection_end = es->selection_start;
5477 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5478 es->composition_len = abs(es->composition_start - es->selection_end);
5480 es->selection_start = es->composition_start;
5481 es->selection_end = es->selection_start + es->composition_len;
5483 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5484 HeapFree(GetProcessHeap(),0,lpCompStr);
5487 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
5492 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5498 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
5501 ERR("Unable to alloc buffer for IME string\n");
5505 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
5506 lpResultStr[buflen/sizeof(WCHAR)] = 0;
5508 /* check for change in composition start */
5509 if (es->selection_end < es->composition_start)
5510 es->composition_start = es->selection_end;
5512 es->selection_start = es->composition_start;
5513 es->selection_end = es->composition_start + es->composition_len;
5514 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5515 es->composition_start = es->selection_end;
5516 es->composition_len = 0;
5518 HeapFree(GetProcessHeap(),0,lpResultStr);
5521 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5526 if (es->composition_len == 0 && es->selection_start != es->selection_end)
5528 static const WCHAR empty_stringW[] = {0};
5529 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
5530 es->composition_start = es->selection_end;
5533 hIMC = ImmGetContext(hwnd);
5537 if (CompFlag & GCS_RESULTSTR)
5538 EDIT_GetResultStr(hIMC, es);
5539 if (CompFlag & GCS_COMPSTR)
5540 EDIT_GetCompositionStr(hIMC, CompFlag, es);
5541 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
5542 ImmReleaseContext(hwnd, hIMC);
5543 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);