4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
57 #include "wine/winbase16.h"
58 #include "wine/winuser16.h"
59 #include "wine/unicode.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
69 FIXME: BTW, new specs say 65535 (do you dare ???) */
70 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
71 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
72 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
73 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
76 * extra flags for EDITSTATE.flags field
78 #define EF_MODIFIED 0x0001 /* text has been modified */
79 #define EF_FOCUSED 0x0002 /* we have input focus */
80 #define EF_UPDATE 0x0004 /* notify parent of changed state */
81 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
82 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
83 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
84 wrapped line, instead of in front of the next character */
85 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
89 END_0 = 0, /* line ends with terminating '\0' character */
90 END_WRAP, /* line is wrapped */
91 END_HARD, /* line ends with a hard return '\r\n' */
92 END_SOFT, /* line ends with a soft return '\r\r\n' */
93 END_RICH /* line ends with a single '\n' */
96 typedef struct tagLINEDEF {
97 INT length; /* bruto length of a line in bytes */
98 INT net_length; /* netto length of a line in visible characters */
100 INT width; /* width of the line in pixels */
101 INT index; /* line index into the buffer */
102 struct tagLINEDEF *next;
107 BOOL is_unicode; /* how the control was created */
108 LPWSTR text; /* the actual contents of the control */
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(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(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);
215 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
216 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
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, LPRECT lprc);
224 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
225 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
226 static INT CALLBACK 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(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(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, INT limit);
249 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT 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, LPINT tabs);
253 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 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 void 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 LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
267 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
268 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
269 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
270 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
271 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
272 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
273 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
274 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
275 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
276 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
277 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
278 static void EDIT_WM_Paste(EDITSTATE *es);
279 static void EDIT_WM_SetFocus(EDITSTATE *es);
280 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
281 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
282 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
283 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
284 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
285 static void EDIT_WM_Timer(EDITSTATE *es);
286 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
287 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
288 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
289 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
291 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
292 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
294 /*********************************************************************
295 * edit class descriptor
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(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;
397 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
402 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
403 msg = WM_CTLCOLORSTATIC;
405 msg = WM_CTLCOLOREDIT;
407 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
408 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
410 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
414 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
417 return DefWindowProcW(hwnd, msg, wParam, lParam);
419 return DefWindowProcA(hwnd, msg, wParam, lParam);
422 /*********************************************************************
426 * The messages are in the order of the actual integer values
427 * (which can be found in include/windows.h)
428 * Wherever possible the 16 bit versions are converted to
429 * the 32 bit ones, so that we can 'fall through' to the
430 * helper functions. These are mostly 32 bit (with a few
431 * exceptions, clearly indicated by a '16' extension to their
435 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
436 WPARAM wParam, LPARAM lParam, BOOL unicode )
438 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
441 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
443 if (!es && msg != WM_NCCREATE)
444 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
446 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
454 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
458 if ((short)LOWORD(lParam) == -1)
459 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
461 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
463 EDIT_EM_ScrollCaret(es);
467 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
468 EDIT_EM_ScrollCaret(es);
475 RECT16 *r16 = MapSL(lParam);
476 r16->left = es->format_rect.left;
477 r16->top = es->format_rect.top;
478 r16->right = es->format_rect.right;
479 r16->bottom = es->format_rect.bottom;
484 CopyRect((LPRECT)lParam, &es->format_rect);
488 if ((es->style & ES_MULTILINE) && lParam) {
490 RECT16 *r16 = MapSL(lParam);
493 rc.right = r16->right;
494 rc.bottom = r16->bottom;
495 EDIT_SetRectNP(es, &rc);
496 EDIT_UpdateText(es, NULL, TRUE);
500 if ((es->style & ES_MULTILINE) && lParam) {
501 EDIT_SetRectNP(es, (LPRECT)lParam);
502 EDIT_UpdateText(es, NULL, TRUE);
507 if ((es->style & ES_MULTILINE) && lParam) {
509 RECT16 *r16 = MapSL(lParam);
512 rc.right = r16->right;
513 rc.bottom = r16->bottom;
514 EDIT_SetRectNP(es, &rc);
518 if ((es->style & ES_MULTILINE) && lParam)
519 EDIT_SetRectNP(es, (LPRECT)lParam);
524 result = EDIT_EM_Scroll(es, (INT)wParam);
527 case EM_LINESCROLL16:
528 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
529 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
532 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
535 case EM_SCROLLCARET16:
537 EDIT_EM_ScrollCaret(es);
543 result = ((es->flags & EF_MODIFIED) != 0);
549 es->flags |= EF_MODIFIED;
551 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
554 case EM_GETLINECOUNT16:
555 case EM_GETLINECOUNT:
556 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
560 if ((INT16)wParam == -1)
564 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
568 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
571 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
575 result = (LRESULT)EDIT_EM_GetHandle16(es);
578 result = (LRESULT)EDIT_EM_GetHandle(es);
583 result = EDIT_EM_GetThumb(es);
586 /* these messages missing from specs */
595 FIXME("undocumented message 0x%x, please report\n", msg);
596 result = DefWindowProcW(hwnd, msg, wParam, lParam);
599 case EM_LINELENGTH16:
601 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
604 case EM_REPLACESEL16:
605 lParam = (LPARAM)MapSL(lParam);
606 unicode = FALSE; /* 16-bit message is always ascii */
613 textW = (LPWSTR)lParam;
616 LPSTR textA = (LPSTR)lParam;
617 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
618 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
619 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
622 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
626 HeapFree(GetProcessHeap(), 0, textW);
631 lParam = (LPARAM)MapSL(lParam);
632 unicode = FALSE; /* 16-bit message is always ascii */
635 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
639 case EM_SETLIMITTEXT:
640 EDIT_EM_SetLimitText(es, (INT)wParam);
645 result = (LRESULT)EDIT_EM_CanUndo(es);
651 result = (LRESULT)EDIT_EM_Undo(es);
656 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
659 case EM_LINEFROMCHAR16:
660 case EM_LINEFROMCHAR:
661 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
664 case EM_SETTABSTOPS16:
665 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
668 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
671 case EM_SETPASSWORDCHAR16:
672 unicode = FALSE; /* 16-bit message is always ascii */
674 case EM_SETPASSWORDCHAR:
679 charW = (WCHAR)wParam;
683 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
686 EDIT_EM_SetPasswordChar(es, charW);
690 case EM_EMPTYUNDOBUFFER16:
691 case EM_EMPTYUNDOBUFFER:
692 EDIT_EM_EmptyUndoBuffer(es);
695 case EM_GETFIRSTVISIBLELINE16:
696 result = es->y_offset;
698 case EM_GETFIRSTVISIBLELINE:
699 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
702 case EM_SETREADONLY16:
705 SetWindowLongW( hwnd, GWL_STYLE,
706 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
707 es->style |= ES_READONLY;
709 SetWindowLongW( hwnd, GWL_STYLE,
710 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
711 es->style &= ~ES_READONLY;
716 case EM_SETWORDBREAKPROC16:
717 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
719 case EM_SETWORDBREAKPROC:
720 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
723 case EM_GETWORDBREAKPROC16:
724 result = (LRESULT)es->word_break_proc16;
726 case EM_GETWORDBREAKPROC:
727 result = (LRESULT)es->word_break_proc;
730 case EM_GETPASSWORDCHAR16:
731 unicode = FALSE; /* 16-bit message is always ascii */
733 case EM_GETPASSWORDCHAR:
736 result = es->password_char;
739 WCHAR charW = es->password_char;
741 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
747 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
750 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam), TRUE);
754 result = MAKELONG(es->left_margin, es->right_margin);
757 case EM_GETLIMITTEXT:
758 result = es->buffer_limit;
762 result = strlenW(es->text);
763 if ((INT)wParam >= result) result = -1;
764 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
768 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
771 /* End of the EM_ messages which were in numerical order; what order
772 * are these in? vaguely alphabetical?
776 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
780 result = EDIT_WM_Destroy(es);
785 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
787 if (es->style & ES_MULTILINE)
789 result |= DLGC_WANTALLKEYS;
793 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
795 int vk = (int)((LPMSG)lParam)->wParam;
797 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
799 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
800 result |= DLGC_WANTMESSAGE;
811 strng[0] = wParam >> 8;
812 strng[1] = wParam & 0xff;
813 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
814 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
815 EDIT_WM_Char(es, charW);
828 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
831 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
833 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
834 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
837 EDIT_WM_Char(es, charW);
846 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
850 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
859 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
862 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
866 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
867 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
868 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
870 result = EDIT_WM_Create(es, nameW);
871 HeapFree(GetProcessHeap(), 0, nameW);
880 es->bEnableState = (BOOL) wParam;
881 EDIT_UpdateText(es, NULL, TRUE);
885 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
889 result = (LRESULT)es->font;
893 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
896 case WM_GETTEXTLENGTH:
897 if (unicode) result = strlenW(es->text);
898 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
899 NULL, 0, NULL, NULL );
903 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
907 result = EDIT_WM_KeyDown(es, (INT)wParam);
911 result = EDIT_WM_KillFocus(es);
914 case WM_LBUTTONDBLCLK:
915 result = EDIT_WM_LButtonDblClk(es);
919 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
923 result = EDIT_WM_LButtonUp(es);
927 result = EDIT_WM_MButtonDown(es);
931 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
936 EDIT_WM_Paint(es, (HDC)wParam);
944 EDIT_WM_SetFocus(es);
948 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
952 /* FIXME: actually set an internal flag and behave accordingly */
956 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
961 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
964 case WM_STYLECHANGED:
965 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
968 case WM_STYLECHANGING:
969 result = 0; /* See EDIT_WM_StyleChanged */
973 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
981 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
986 int gcWheelDelta = 0;
987 UINT pulScrollLines = 3;
988 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
990 if (wParam & (MK_SHIFT | MK_CONTROL)) {
991 result = DefWindowProcW(hwnd, msg, wParam, lParam);
994 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
995 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
997 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
998 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
999 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1005 /* IME messages to make the edit control IME aware */
1006 case WM_IME_SETCONTEXT:
1009 case WM_IME_STARTCOMPOSITION:
1011 * FIXME in IME: This message is not always sent like it should be
1013 if (es->selection_start != es->selection_end)
1015 static const WCHAR empty_stringW[] = {0};
1016 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1018 es->composition_start = es->selection_end;
1019 es->composition_len = 0;
1022 case WM_IME_COMPOSITION:
1023 if (es->composition_len == 0)
1025 if (es->selection_start != es->selection_end)
1027 static const WCHAR empty_stringW[] = {0};
1028 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1031 es->composition_start = es->selection_end;
1033 EDIT_ImeComposition(hwnd,lParam,es);
1036 case WM_IME_ENDCOMPOSITION:
1037 es->composition_len= 0;
1040 case WM_IME_COMPOSITIONFULL:
1046 case WM_IME_CONTROL:
1050 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1054 if (es) EDIT_UnlockBuffer(es, FALSE);
1056 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1061 /*********************************************************************
1063 * EditWndProcW (USER32.@)
1065 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1067 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1070 /*********************************************************************
1072 * EditWndProc (USER32.@)
1074 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1076 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1079 /*********************************************************************
1081 * EDIT_BuildLineDefs_ML
1083 * Build linked list of text lines.
1084 * Lines can end with '\0' (last line), a character (if it is wrapped),
1085 * a soft return '\r\r\n' or a hard return '\r\n'
1088 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1092 LPWSTR current_position, cp;
1094 LINEDEF *current_line;
1095 LINEDEF *previous_line;
1096 LINEDEF *start_line;
1097 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1098 INT line_count = es->line_count;
1099 INT orig_net_length;
1102 if (istart == iend && delta == 0)
1105 dc = GetDC(es->hwndSelf);
1107 old_font = SelectObject(dc, es->font);
1109 previous_line = NULL;
1110 current_line = es->first_line_def;
1112 /* Find starting line. istart must lie inside an existing line or
1113 * at the end of buffer */
1115 if (istart < current_line->index + current_line->length ||
1116 current_line->ending == END_0)
1119 previous_line = current_line;
1120 current_line = current_line->next;
1122 } while (current_line);
1124 if (!current_line) /* Error occurred start is not inside previous buffer */
1126 FIXME(" modification occurred outside buffer\n");
1127 ReleaseDC(es->hwndSelf, dc);
1131 /* Remember start of modifications in order to calculate update region */
1132 nstart_line = line_index;
1133 nstart_index = current_line->index;
1135 /* We must start to reformat from the previous line since the modifications
1136 * may have caused the line to wrap upwards. */
1137 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1140 current_line = previous_line;
1142 start_line = current_line;
1144 fw = es->format_rect.right - es->format_rect.left;
1145 current_position = es->text + current_line->index;
1147 if (current_line != start_line)
1149 if (!current_line || current_line->index + delta > current_position - es->text)
1151 /* The buffer has been expanded, create a new line and
1152 insert it into the link list */
1153 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1154 new_line->next = previous_line->next;
1155 previous_line->next = new_line;
1156 current_line = new_line;
1159 else if (current_line->index + delta < current_position - es->text)
1161 /* The previous line merged with this line so we delete this extra entry */
1162 previous_line->next = current_line->next;
1163 HeapFree(GetProcessHeap(), 0, current_line);
1164 current_line = previous_line->next;
1168 else /* current_line->index + delta == current_position */
1170 if (current_position - es->text > iend)
1171 break; /* We reached end of line modifications */
1172 /* else recalulate this line */
1176 current_line->index = current_position - es->text;
1177 orig_net_length = current_line->net_length;
1179 /* Find end of line */
1180 cp = current_position;
1182 if (*cp == '\n') break;
1183 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1188 /* Mark type of line termination */
1190 current_line->ending = END_0;
1191 current_line->net_length = strlenW(current_position);
1192 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1193 current_line->ending = END_SOFT;
1194 current_line->net_length = cp - current_position - 1;
1195 } else if (*cp == '\n') {
1196 current_line->ending = END_RICH;
1197 current_line->net_length = cp - current_position;
1199 current_line->ending = END_HARD;
1200 current_line->net_length = cp - current_position;
1203 /* Calculate line width */
1204 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1205 current_position, current_line->net_length,
1206 es->tabs_count, es->tabs));
1208 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1209 if (!(es->style & ES_AUTOHSCROLL)) {
1210 if (current_line->width > fw) {
1215 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1216 prev + 1, current_line->net_length, WB_RIGHT);
1217 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1218 current_position, next, es->tabs_count, es->tabs));
1219 } while (current_line->width <= fw);
1220 if (!prev) { /* Didn't find a line break so force a break */
1225 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1226 current_position, next, es->tabs_count, es->tabs));
1227 } while (current_line->width <= fw);
1232 /* If the first line we are calculating, wrapped before istart, we must
1233 * adjust istart in order for this to be reflected in the update region. */
1234 if (current_line->index == nstart_index && istart > current_line->index + prev)
1235 istart = current_line->index + prev;
1236 /* else if we are updating the previous line before the first line we
1237 * are re-calculating and it expanded */
1238 else if (current_line == start_line &&
1239 current_line->index != nstart_index && orig_net_length < prev)
1241 /* Line expanded due to an upwards line wrap so we must partially include
1242 * previous line in update region */
1243 nstart_line = line_index;
1244 nstart_index = current_line->index;
1245 istart = current_line->index + orig_net_length;
1248 current_line->net_length = prev;
1249 current_line->ending = END_WRAP;
1250 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1251 current_line->net_length, es->tabs_count, es->tabs));
1253 else if (orig_net_length < current_line->net_length &&
1254 current_line == start_line &&
1255 current_line->index != nstart_index) {
1256 /* The previous line expanded but it's still not as wide as the client rect */
1257 /* The expansion is due to an upwards line wrap so we must partially include
1258 it in the update region */
1259 nstart_line = line_index;
1260 nstart_index = current_line->index;
1261 istart = current_line->index + orig_net_length;
1266 /* Adjust length to include line termination */
1267 switch (current_line->ending) {
1269 current_line->length = current_line->net_length + 3;
1272 current_line->length = current_line->net_length + 1;
1275 current_line->length = current_line->net_length + 2;
1279 current_line->length = current_line->net_length;
1282 es->text_width = max(es->text_width, current_line->width);
1283 current_position += current_line->length;
1284 previous_line = current_line;
1285 current_line = current_line->next;
1287 } while (previous_line->ending != END_0);
1289 /* Finish adjusting line indexes by delta or remove hanging lines */
1290 if (previous_line->ending == END_0)
1292 LINEDEF *pnext = NULL;
1294 previous_line->next = NULL;
1295 while (current_line)
1297 pnext = current_line->next;
1298 HeapFree(GetProcessHeap(), 0, current_line);
1299 current_line = pnext;
1303 else if (delta != 0)
1305 while (current_line)
1307 current_line->index += delta;
1308 current_line = current_line->next;
1312 /* Calculate rest of modification rectangle */
1317 * We calculate two rectangles. One for the first line which may have
1318 * an indent with respect to the format rect. The other is a format-width
1319 * rectangle that spans the rest of the lines that changed or moved.
1321 rc.top = es->format_rect.top + nstart_line * es->line_height -
1322 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1323 rc.bottom = rc.top + es->line_height;
1324 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1325 rc.left = es->format_rect.left;
1327 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1328 es->text + nstart_index, istart - nstart_index,
1329 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1330 rc.right = es->format_rect.right;
1331 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1334 rc.left = es->format_rect.left;
1335 rc.right = es->format_rect.right;
1337 * If lines were added or removed we must re-paint the remainder of the
1338 * lines since the remaining lines were either shifted up or down.
1340 if (line_count < es->line_count) /* We added lines */
1341 rc.bottom = es->line_count * es->line_height;
1342 else if (line_count > es->line_count) /* We removed lines */
1343 rc.bottom = line_count * es->line_height;
1345 rc.bottom = line_index * es->line_height;
1346 rc.bottom += es->format_rect.top;
1347 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1348 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1349 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1350 DeleteObject(tmphrgn);
1354 SelectObject(dc, old_font);
1356 ReleaseDC(es->hwndSelf, dc);
1359 /*********************************************************************
1361 * EDIT_CalcLineWidth_SL
1364 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1371 text = EDIT_GetPasswordPointer_SL(es);
1373 dc = GetDC(es->hwndSelf);
1375 old_font = SelectObject(dc, es->font);
1377 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1380 SelectObject(dc, old_font);
1381 ReleaseDC(es->hwndSelf, dc);
1383 if (es->style & ES_PASSWORD)
1384 HeapFree(GetProcessHeap(), 0, text);
1386 es->text_width = size.cx;
1389 /*********************************************************************
1391 * EDIT_CallWordBreakProc
1393 * Call appropriate WordBreakProc (internal or external).
1395 * Note: The "start" argument should always be an index referring
1396 * to es->text. The actual wordbreak proc might be
1397 * 16 bit, so we can't always pass any 32 bit LPSTR.
1398 * Hence we assume that es->text is the buffer that holds
1399 * the string under examination (we can decide this for ourselves).
1402 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1406 if (es->word_break_proc16) {
1413 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1414 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1415 segptr = WOWGlobalLock16(hglob16);
1416 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1417 args[4] = SELECTOROF(segptr);
1418 args[3] = OFFSETOF(segptr);
1422 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1423 ret = LOWORD(result);
1424 GlobalUnlock16(hglob16);
1425 GlobalFree16(hglob16);
1427 else if (es->word_break_proc)
1431 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1433 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1434 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1435 ret = wbpW(es->text + start, index, count, action);
1439 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1443 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1444 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1445 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1446 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1447 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1448 ret = wbpA(textA, index, countA, action);
1449 HeapFree(GetProcessHeap(), 0, textA);
1453 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1459 /*********************************************************************
1463 * Beware: This is not the function called on EM_CHARFROMPOS
1464 * The position _can_ be outside the formatting / client
1466 * The return value is only the character index
1469 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1474 INT x_high = 0, x_low = 0;
1476 if (es->style & ES_MULTILINE) {
1477 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1479 LINEDEF *line_def = es->first_line_def;
1481 while ((line > 0) && line_def->next) {
1482 line_index += line_def->length;
1483 line_def = line_def->next;
1486 x += es->x_offset - es->format_rect.left;
1487 if (es->style & ES_RIGHT)
1488 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1489 else if (es->style & ES_CENTER)
1490 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1491 if (x >= line_def->width) {
1493 *after_wrap = (line_def->ending == END_WRAP);
1494 return line_index + line_def->net_length;
1498 *after_wrap = FALSE;
1501 dc = GetDC(es->hwndSelf);
1503 old_font = SelectObject(dc, es->font);
1505 high = line_index + line_def->net_length + 1;
1506 while (low < high - 1)
1508 INT mid = (low + high) / 2;
1509 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1518 if (abs(x_high - x) + 1 <= abs(x_low - x))
1524 *after_wrap = ((index == line_index + line_def->net_length) &&
1525 (line_def->ending == END_WRAP));
1530 *after_wrap = FALSE;
1531 x -= es->format_rect.left;
1533 return es->x_offset;
1537 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1538 if (es->style & ES_RIGHT)
1540 else if (es->style & ES_CENTER)
1544 text = EDIT_GetPasswordPointer_SL(es);
1545 dc = GetDC(es->hwndSelf);
1547 old_font = SelectObject(dc, es->font);
1551 INT high = es->x_offset;
1552 while (low < high - 1)
1554 INT mid = (low + high) / 2;
1555 GetTextExtentPoint32W( dc, text + mid,
1556 es->x_offset - mid, &size );
1565 if (abs(x_high + x) <= abs(x_low + x) + 1)
1572 INT low = es->x_offset;
1573 INT high = strlenW(es->text) + 1;
1574 while (low < high - 1)
1576 INT mid = (low + high) / 2;
1577 GetTextExtentPoint32W( dc, text + es->x_offset,
1578 mid - es->x_offset, &size );
1587 if (abs(x_high - x) <= abs(x_low - x) + 1)
1592 if (es->style & ES_PASSWORD)
1593 HeapFree(GetProcessHeap(), 0, text);
1596 SelectObject(dc, old_font);
1597 ReleaseDC(es->hwndSelf, dc);
1602 /*********************************************************************
1606 * adjusts the point to be within the formatting rectangle
1607 * (so CharFromPos returns the nearest _visible_ character)
1610 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1612 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1613 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1617 /*********************************************************************
1621 * Calculates the bounding rectangle for a line from a starting
1622 * column to an ending column.
1625 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1627 INT line_index = EDIT_EM_LineIndex(es, line);
1629 if (es->style & ES_MULTILINE)
1630 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1632 rc->top = es->format_rect.top;
1633 rc->bottom = rc->top + es->line_height;
1634 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1635 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1639 /*********************************************************************
1641 * EDIT_GetPasswordPointer_SL
1643 * note: caller should free the (optionally) allocated buffer
1646 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1648 if (es->style & ES_PASSWORD) {
1649 INT len = strlenW(es->text);
1650 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1652 while(len) text[--len] = es->password_char;
1659 /*********************************************************************
1663 * This acts as a LocalLock16(), but it locks only once. This way
1664 * you can call it whenever you like, without unlocking.
1666 * Initially the edit control allocates a HLOCAL32 buffer
1667 * (32 bit linear memory handler). However, 16 bit application
1668 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1669 * handler). From that moment on we have to keep using this 16 bit memory
1670 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1671 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1675 static void EDIT_LockBuffer(EDITSTATE *es)
1677 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1678 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1683 BOOL _16bit = FALSE;
1689 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1690 textA = LocalLock(es->hloc32A);
1691 countA = strlen(textA) + 1;
1695 HANDLE16 oldDS = stack16->ds;
1696 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1697 stack16->ds = hInstance;
1698 textA = MapSL(LocalLock16(es->hloc16));
1699 stack16->ds = oldDS;
1700 countA = strlen(textA) + 1;
1705 ERR("no buffer ... please report\n");
1712 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1713 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1714 if(countW_new > es->buffer_size + 1)
1716 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1717 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1718 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1721 es->hloc32W = hloc32W_new;
1722 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1723 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1726 WARN("FAILED! Will synchronize partially\n");
1730 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1731 es->text = LocalLock(es->hloc32W);
1735 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1738 HANDLE16 oldDS = stack16->ds;
1739 stack16->ds = hInstance;
1740 LocalUnlock16(es->hloc16);
1741 stack16->ds = oldDS;
1744 LocalUnlock(es->hloc32A);
1751 /*********************************************************************
1753 * EDIT_SL_InvalidateText
1755 * Called from EDIT_InvalidateText().
1756 * Does the job for single-line controls only.
1759 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1764 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1765 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1766 EDIT_UpdateText(es, &rc, TRUE);
1770 /*********************************************************************
1772 * EDIT_ML_InvalidateText
1774 * Called from EDIT_InvalidateText().
1775 * Does the job for multi-line controls only.
1778 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1780 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1781 INT sl = EDIT_EM_LineFromChar(es, start);
1782 INT el = EDIT_EM_LineFromChar(es, end);
1791 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1794 sc = start - EDIT_EM_LineIndex(es, sl);
1795 ec = end - EDIT_EM_LineIndex(es, el);
1796 if (sl < es->y_offset) {
1800 if (el > es->y_offset + vlc) {
1801 el = es->y_offset + vlc;
1802 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1804 GetClientRect(es->hwndSelf, &rc1);
1805 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1807 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1808 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1809 EDIT_UpdateText(es, &rcUpdate, TRUE);
1811 EDIT_GetLineRect(es, sl, sc,
1812 EDIT_EM_LineLength(es,
1813 EDIT_EM_LineIndex(es, sl)),
1815 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1816 EDIT_UpdateText(es, &rcUpdate, TRUE);
1817 for (l = sl + 1 ; l < el ; l++) {
1818 EDIT_GetLineRect(es, l, 0,
1819 EDIT_EM_LineLength(es,
1820 EDIT_EM_LineIndex(es, l)),
1822 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1823 EDIT_UpdateText(es, &rcUpdate, TRUE);
1825 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1826 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1827 EDIT_UpdateText(es, &rcUpdate, TRUE);
1832 /*********************************************************************
1834 * EDIT_InvalidateText
1836 * Invalidate the text from offset start up to, but not including,
1837 * offset end. Useful for (re)painting the selection.
1838 * Regions outside the linewidth are not invalidated.
1839 * end == -1 means end == TextLength.
1840 * start and end need not be ordered.
1843 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1849 end = strlenW(es->text);
1857 if (es->style & ES_MULTILINE)
1858 EDIT_ML_InvalidateText(es, start, end);
1860 EDIT_SL_InvalidateText(es, start, end);
1864 /*********************************************************************
1868 * Try to fit size + 1 characters in the buffer.
1870 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1874 if (size <= es->buffer_size)
1877 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1879 /* Force edit to unlock it's buffer. es->text now NULL */
1880 EDIT_UnlockBuffer(es, TRUE);
1883 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1884 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1885 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1886 es->hloc32W = hNew32W;
1887 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1891 EDIT_LockBuffer(es);
1893 if (es->buffer_size < size) {
1894 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1895 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1898 TRACE("We now have %d+1\n", es->buffer_size);
1904 /*********************************************************************
1908 * Try to fit size + 1 bytes in the undo buffer.
1911 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1915 if (size <= es->undo_buffer_size)
1918 TRACE("trying to ReAlloc to %d+1\n", size);
1920 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1921 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1922 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1927 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1933 /*********************************************************************
1938 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1940 INT e = es->selection_end;
1944 if ((es->style & ES_MULTILINE) && e &&
1945 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1947 if (e && (es->text[e - 1] == '\r'))
1951 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1952 EDIT_EM_ScrollCaret(es);
1956 /*********************************************************************
1960 * Only for multi line controls
1961 * Move the caret one line down, on a column with the nearest
1962 * x coordinate on the screen (might be a different column).
1965 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1967 INT s = es->selection_start;
1968 INT e = es->selection_end;
1969 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1970 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1971 INT x = (short)LOWORD(pos);
1972 INT y = (short)HIWORD(pos);
1974 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1977 EDIT_EM_SetSel(es, s, e, after_wrap);
1978 EDIT_EM_ScrollCaret(es);
1982 /*********************************************************************
1987 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1989 BOOL after_wrap = FALSE;
1992 /* Pass a high value in x to make sure of receiving the end of the line */
1993 if (es->style & ES_MULTILINE)
1994 e = EDIT_CharFromPos(es, 0x3fffffff,
1995 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1997 e = strlenW(es->text);
1998 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1999 EDIT_EM_ScrollCaret(es);
2003 /*********************************************************************
2008 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2010 INT e = es->selection_end;
2014 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2015 if (es->text[e] == '\n')
2017 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2021 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2022 EDIT_EM_ScrollCaret(es);
2026 /*********************************************************************
2030 * Home key: move to beginning of line.
2033 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2037 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2038 if (es->style & ES_MULTILINE)
2039 e = EDIT_CharFromPos(es, -es->x_offset,
2040 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2043 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2044 EDIT_EM_ScrollCaret(es);
2048 /*********************************************************************
2050 * EDIT_MovePageDown_ML
2052 * Only for multi line controls
2053 * Move the caret one page down, on a column with the nearest
2054 * x coordinate on the screen (might be a different column).
2057 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2059 INT s = es->selection_start;
2060 INT e = es->selection_end;
2061 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2062 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2063 INT x = (short)LOWORD(pos);
2064 INT y = (short)HIWORD(pos);
2066 e = EDIT_CharFromPos(es, x,
2067 y + (es->format_rect.bottom - es->format_rect.top),
2071 EDIT_EM_SetSel(es, s, e, after_wrap);
2072 EDIT_EM_ScrollCaret(es);
2076 /*********************************************************************
2078 * EDIT_MovePageUp_ML
2080 * Only for multi line controls
2081 * Move the caret one page up, on a column with the nearest
2082 * x coordinate on the screen (might be a different column).
2085 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2087 INT s = es->selection_start;
2088 INT e = es->selection_end;
2089 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2090 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2091 INT x = (short)LOWORD(pos);
2092 INT y = (short)HIWORD(pos);
2094 e = EDIT_CharFromPos(es, x,
2095 y - (es->format_rect.bottom - es->format_rect.top),
2099 EDIT_EM_SetSel(es, s, e, after_wrap);
2100 EDIT_EM_ScrollCaret(es);
2104 /*********************************************************************
2108 * Only for multi line controls
2109 * Move the caret one line up, on a column with the nearest
2110 * x coordinate on the screen (might be a different column).
2113 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2115 INT s = es->selection_start;
2116 INT e = es->selection_end;
2117 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2118 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2119 INT x = (short)LOWORD(pos);
2120 INT y = (short)HIWORD(pos);
2122 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2125 EDIT_EM_SetSel(es, s, e, after_wrap);
2126 EDIT_EM_ScrollCaret(es);
2130 /*********************************************************************
2132 * EDIT_MoveWordBackward
2135 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2137 INT s = es->selection_start;
2138 INT e = es->selection_end;
2143 l = EDIT_EM_LineFromChar(es, e);
2144 ll = EDIT_EM_LineLength(es, e);
2145 li = EDIT_EM_LineIndex(es, l);
2148 li = EDIT_EM_LineIndex(es, l - 1);
2149 e = li + EDIT_EM_LineLength(es, li);
2152 e = li + (INT)EDIT_CallWordBreakProc(es,
2153 li, e - li, ll, WB_LEFT);
2157 EDIT_EM_SetSel(es, s, e, FALSE);
2158 EDIT_EM_ScrollCaret(es);
2162 /*********************************************************************
2164 * EDIT_MoveWordForward
2167 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2169 INT s = es->selection_start;
2170 INT e = es->selection_end;
2175 l = EDIT_EM_LineFromChar(es, e);
2176 ll = EDIT_EM_LineLength(es, e);
2177 li = EDIT_EM_LineIndex(es, l);
2179 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2180 e = EDIT_EM_LineIndex(es, l + 1);
2182 e = li + EDIT_CallWordBreakProc(es,
2183 li, e - li + 1, ll, WB_RIGHT);
2187 EDIT_EM_SetSel(es, s, e, FALSE);
2188 EDIT_EM_ScrollCaret(es);
2192 /*********************************************************************
2197 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2199 INT s = es->selection_start;
2200 INT e = es->selection_end;
2207 if (es->style & ES_MULTILINE) {
2208 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2209 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2214 TRACE("line=%d\n", line);
2216 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2217 x = (short)LOWORD(pos);
2218 y = (short)HIWORD(pos);
2219 li = EDIT_EM_LineIndex(es, line);
2220 ll = EDIT_EM_LineLength(es, li);
2221 s = min(es->selection_start, es->selection_end);
2222 e = max(es->selection_start, es->selection_end);
2223 s = min(li + ll, max(li, s));
2224 e = min(li + ll, max(li, e));
2225 if (rev && (s != e) &&
2226 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2227 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2228 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2229 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2231 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2235 /*********************************************************************
2240 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2244 LOGFONTW underline_font;
2245 HFONT hUnderline = 0;
2254 BkMode = GetBkMode(dc);
2255 BkColor = GetBkColor(dc);
2256 TextColor = GetTextColor(dc);
2258 if (es->composition_len == 0)
2260 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2261 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2262 SetBkMode( dc, OPAQUE);
2266 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2267 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2268 underline_font.lfUnderline = TRUE;
2269 hUnderline = CreateFontIndirectW(&underline_font);
2270 old_font = SelectObject(dc,hUnderline);
2273 li = EDIT_EM_LineIndex(es, line);
2274 if (es->style & ES_MULTILINE) {
2275 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2276 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2278 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2279 TextOutW(dc, x, y, text + li + col, count);
2280 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2282 if (es->style & ES_PASSWORD)
2283 HeapFree(GetProcessHeap(), 0, text);
2286 if (es->composition_len == 0)
2288 SetBkColor(dc, BkColor);
2289 SetTextColor(dc, TextColor);
2290 SetBkMode( dc, BkMode);
2295 SelectObject(dc,old_font);
2297 DeleteObject(hUnderline);
2304 /*********************************************************************
2309 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2312 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2313 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2314 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2318 /*********************************************************************
2320 * EDIT_AdjustFormatRect
2322 * Adjusts the format rectangle for the current font and the
2323 * current client rectangle.
2326 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2330 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2331 if (es->style & ES_MULTILINE)
2333 INT fw, vlc, max_x_offset, max_y_offset;
2335 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2336 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2338 /* correct es->x_offset */
2339 fw = es->format_rect.right - es->format_rect.left;
2340 max_x_offset = es->text_width - fw;
2341 if(max_x_offset < 0) max_x_offset = 0;
2342 if(es->x_offset > max_x_offset)
2343 es->x_offset = max_x_offset;
2345 /* correct es->y_offset */
2346 max_y_offset = es->line_count - vlc;
2347 if(max_y_offset < 0) max_y_offset = 0;
2348 if(es->y_offset > max_y_offset)
2349 es->y_offset = max_y_offset;
2351 /* force scroll info update */
2352 EDIT_UpdateScrollInfo(es);
2355 /* Windows doesn't care to fix text placement for SL controls */
2356 es->format_rect.bottom = es->format_rect.top + es->line_height;
2358 /* Always stay within the client area */
2359 GetClientRect(es->hwndSelf, &ClientRect);
2360 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2362 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2363 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2365 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2369 /*********************************************************************
2373 * note: this is not (exactly) the handler called on EM_SETRECTNP
2374 * it is also used to set the rect of a single line control
2377 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2381 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2383 CopyRect(&es->format_rect, rc);
2385 if (ExStyle & WS_EX_CLIENTEDGE) {
2386 es->format_rect.left++;
2387 es->format_rect.right--;
2389 if (es->format_rect.bottom - es->format_rect.top
2390 >= es->line_height + 2)
2392 es->format_rect.top++;
2393 es->format_rect.bottom--;
2396 else if (es->style & WS_BORDER) {
2397 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2398 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2399 es->format_rect.left += bw;
2400 es->format_rect.right -= bw;
2401 if (es->format_rect.bottom - es->format_rect.top
2402 >= es->line_height + 2 * bh)
2404 es->format_rect.top += bh;
2405 es->format_rect.bottom -= bh;
2409 es->format_rect.left += es->left_margin;
2410 es->format_rect.right -= es->right_margin;
2411 EDIT_AdjustFormatRect(es);
2415 /*********************************************************************
2420 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2423 /* Edit window might be already destroyed */
2424 if(!IsWindow(es->hwndSelf))
2426 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2430 if (!es->lock_count) {
2431 ERR("lock_count == 0 ... please report\n");
2435 ERR("es->text == 0 ... please report\n");
2439 if (force || (es->lock_count == 1)) {
2443 UINT countW = strlenW(es->text) + 1;
2444 STACK16FRAME* stack16 = NULL;
2449 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2450 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2451 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2452 countA = LocalSize(es->hloc32A);
2453 if(countA_new > countA)
2456 UINT alloc_size = ROUND_TO_GROW(countA_new);
2457 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2458 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2461 es->hloc32A = hloc32A_new;
2462 countA = LocalSize(hloc32A_new);
2463 TRACE("Real new size %d bytes\n", countA);
2466 WARN("FAILED! Will synchronize partially\n");
2468 textA = LocalLock(es->hloc32A);
2472 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2474 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2475 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2477 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2478 oldDS = stack16->ds;
2479 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2481 countA = LocalSize16(es->hloc16);
2482 if(countA_new > countA)
2484 HLOCAL16 hloc16_new;
2485 UINT alloc_size = ROUND_TO_GROW(countA_new);
2486 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2487 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2490 es->hloc16 = hloc16_new;
2491 countA = LocalSize16(hloc16_new);
2492 TRACE("Real new size %d bytes\n", countA);
2495 WARN("FAILED! Will synchronize partially\n");
2497 textA = MapSL(LocalLock16(es->hloc16));
2502 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2504 LocalUnlock16(es->hloc16);
2506 LocalUnlock(es->hloc32A);
2509 if (stack16) stack16->ds = oldDS;
2510 LocalUnlock(es->hloc32W);
2514 ERR("no buffer ... please report\n");
2522 /*********************************************************************
2524 * EDIT_UpdateScrollInfo
2527 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2529 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2532 si.cbSize = sizeof(SCROLLINFO);
2533 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2535 si.nMax = es->line_count - 1;
2536 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2537 si.nPos = es->y_offset;
2538 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2539 si.nMin, si.nMax, si.nPage, si.nPos);
2540 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2543 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2546 si.cbSize = sizeof(SCROLLINFO);
2547 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2549 si.nMax = es->text_width - 1;
2550 si.nPage = es->format_rect.right - es->format_rect.left;
2551 si.nPos = es->x_offset;
2552 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2553 si.nMin, si.nMax, si.nPage, si.nPos);
2554 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2558 /*********************************************************************
2560 * EDIT_WordBreakProc
2562 * Find the beginning of words.
2563 * Note: unlike the specs for a WordBreakProc, this function only
2564 * allows to be called without linebreaks between s[0] up to
2565 * s[count - 1]. Remember it is only called
2566 * internally, so we can decide this for ourselves.
2569 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2573 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2583 if (s[index] == ' ') {
2584 while (index && (s[index] == ' '))
2587 while (index && (s[index] != ' '))
2589 if (s[index] == ' ')
2593 while (index && (s[index] != ' '))
2595 if (s[index] == ' ')
2605 if (s[index] == ' ')
2606 while ((index < count) && (s[index] == ' ')) index++;
2608 while (s[index] && (s[index] != ' ') && (index < count))
2610 while ((s[index] == ' ') && (index < count)) index++;
2614 case WB_ISDELIMITER:
2615 ret = (s[index] == ' ');
2618 ERR("unknown action code, please report !\n");
2625 /*********************************************************************
2629 * returns line number (not index) in high-order word of result.
2630 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2631 * to Richedit, not to the edit control. Original documentation is valid.
2632 * FIXME: do the specs mean to return -1 if outside client area or
2633 * if outside formatting rectangle ???
2636 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2644 GetClientRect(es->hwndSelf, &rc);
2645 if (!PtInRect(&rc, pt))
2648 index = EDIT_CharFromPos(es, x, y, NULL);
2649 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2653 /*********************************************************************
2657 * Enable or disable soft breaks.
2659 * This means: insert or remove the soft linebreak character (\r\r\n).
2660 * Take care to check if the text still fits the buffer after insertion.
2661 * If not, notify with EN_ERRSPACE.
2664 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2666 es->flags &= ~EF_USE_SOFTBRK;
2668 es->flags |= EF_USE_SOFTBRK;
2669 FIXME("soft break enabled, not implemented\n");
2675 /*********************************************************************
2679 * Hopefully this won't fire back at us.
2680 * We always start with a fixed buffer in the local heap.
2681 * Despite of the documentation says that the local heap is used
2682 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2683 * buffer on the local heap.
2686 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2690 if (!(es->style & ES_MULTILINE))
2694 hLocal = es->hloc32W;
2700 UINT countA, alloc_size;
2701 TRACE("Allocating 32-bit ANSI alias buffer\n");
2702 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2703 alloc_size = ROUND_TO_GROW(countA);
2704 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2706 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2709 textA = LocalLock(es->hloc32A);
2710 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2711 LocalUnlock(es->hloc32A);
2713 hLocal = es->hloc32A;
2716 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2721 /*********************************************************************
2725 * Hopefully this won't fire back at us.
2726 * We always start with a buffer in 32 bit linear memory.
2727 * However, with this message a 16 bit application requests
2728 * a handle of 16 bit local heap memory, where it expects to find
2730 * It's a pitty that from this moment on we have to use this
2731 * local heap, because applications may rely on the handle
2734 * In this function we'll try to switch to local heap.
2736 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2739 UINT countA, alloc_size;
2740 STACK16FRAME* stack16;
2743 if (!(es->style & ES_MULTILINE))
2749 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2750 oldDS = stack16->ds;
2751 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2753 if (!LocalHeapSize16()) {
2755 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2756 ERR("could not initialize local heap\n");
2759 TRACE("local heap initialized\n");
2762 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2763 alloc_size = ROUND_TO_GROW(countA);
2765 TRACE("Allocating 16-bit ANSI alias buffer\n");
2766 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2767 ERR("could not allocate new 16 bit buffer\n");
2771 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2772 ERR("could not lock new 16 bit buffer\n");
2773 LocalFree16(es->hloc16);
2778 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2779 LocalUnlock16(es->hloc16);
2781 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2784 stack16->ds = oldDS;
2789 /*********************************************************************
2794 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2797 INT line_len, dst_len;
2800 if (es->style & ES_MULTILINE) {
2801 if (line >= es->line_count)
2805 i = EDIT_EM_LineIndex(es, line);
2807 line_len = EDIT_EM_LineLength(es, i);
2808 dst_len = *(WORD *)dst;
2811 if(dst_len <= line_len)
2813 memcpy(dst, src, dst_len * sizeof(WCHAR));
2816 else /* Append 0 if enough space */
2818 memcpy(dst, src, line_len * sizeof(WCHAR));
2825 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2826 if(!ret && line_len) /* Insufficient buffer size */
2828 if(ret < dst_len) /* Append 0 if enough space */
2829 ((LPSTR)dst)[ret] = 0;
2835 /*********************************************************************
2840 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2842 UINT s = es->selection_start;
2843 UINT e = es->selection_end;
2850 return MAKELONG(s, e);
2854 /*********************************************************************
2858 * FIXME: is this right ? (or should it be only VSCROLL)
2859 * (and maybe only for edit controls that really have their
2860 * own scrollbars) (and maybe only for multiline controls ?)
2861 * All in all: very poorly documented
2864 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2866 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2867 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2871 /*********************************************************************
2876 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2881 if (!(es->style & ES_MULTILINE))
2883 if (index > (INT)strlenW(es->text))
2884 return es->line_count - 1;
2886 index = min(es->selection_start, es->selection_end);
2889 line_def = es->first_line_def;
2890 index -= line_def->length;
2891 while ((index >= 0) && line_def->next) {
2893 line_def = line_def->next;
2894 index -= line_def->length;
2900 /*********************************************************************
2905 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2910 if (!(es->style & ES_MULTILINE))
2912 if (line >= es->line_count)
2916 line_def = es->first_line_def;
2918 INT index = es->selection_end - line_def->length;
2919 while ((index >= 0) && line_def->next) {
2920 line_index += line_def->length;
2921 line_def = line_def->next;
2922 index -= line_def->length;
2926 line_index += line_def->length;
2927 line_def = line_def->next;
2935 /*********************************************************************
2940 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2944 if (!(es->style & ES_MULTILINE))
2945 return strlenW(es->text);
2948 /* get the number of remaining non-selected chars of selected lines */
2949 INT32 l; /* line number */
2950 INT32 li; /* index of first char in line */
2952 l = EDIT_EM_LineFromChar(es, es->selection_start);
2953 /* # chars before start of selection area */
2954 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2955 l = EDIT_EM_LineFromChar(es, es->selection_end);
2956 /* # chars after end of selection */
2957 li = EDIT_EM_LineIndex(es, l);
2958 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2961 line_def = es->first_line_def;
2962 index -= line_def->length;
2963 while ((index >= 0) && line_def->next) {
2964 line_def = line_def->next;
2965 index -= line_def->length;
2967 return line_def->net_length;
2971 /*********************************************************************
2975 * NOTE: dx is in average character widths, dy - in lines;
2978 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2980 if (!(es->style & ES_MULTILINE))
2983 dx *= es->char_width;
2984 return EDIT_EM_LineScroll_internal(es, dx, dy);
2987 /*********************************************************************
2989 * EDIT_EM_LineScroll_internal
2991 * Version of EDIT_EM_LineScroll for internal use.
2992 * It doesn't refuse if ES_MULTILINE is set and assumes that
2993 * dx is in pixels, dy - in lines.
2996 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2999 INT x_offset_in_pixels;
3000 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3003 if (es->style & ES_MULTILINE)
3005 x_offset_in_pixels = es->x_offset;
3010 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3013 if (-dx > x_offset_in_pixels)
3014 dx = -x_offset_in_pixels;
3015 if (dx > es->text_width - x_offset_in_pixels)
3016 dx = es->text_width - x_offset_in_pixels;
3017 nyoff = max(0, es->y_offset + dy);
3018 if (nyoff >= es->line_count - lines_per_page)
3019 nyoff = max(0, es->line_count - lines_per_page);
3020 dy = (es->y_offset - nyoff) * es->line_height;
3025 es->y_offset = nyoff;
3026 if(es->style & ES_MULTILINE)
3029 es->x_offset += dx / es->char_width;
3031 GetClientRect(es->hwndSelf, &rc1);
3032 IntersectRect(&rc, &rc1, &es->format_rect);
3033 ScrollWindowEx(es->hwndSelf, -dx, dy,
3034 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3035 /* force scroll info update */
3036 EDIT_UpdateScrollInfo(es);
3038 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3039 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3040 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3041 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3046 /*********************************************************************
3051 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3053 INT len = strlenW(es->text);
3066 index = min(index, len);
3067 dc = GetDC(es->hwndSelf);
3069 old_font = SelectObject(dc, es->font);
3070 if (es->style & ES_MULTILINE) {
3071 l = EDIT_EM_LineFromChar(es, index);
3072 y = (l - es->y_offset) * es->line_height;
3073 li = EDIT_EM_LineIndex(es, l);
3074 if (after_wrap && (li == index) && l) {
3076 line_def = es->first_line_def;
3078 line_def = line_def->next;
3081 if (line_def->ending == END_WRAP) {
3083 y -= es->line_height;
3084 li = EDIT_EM_LineIndex(es, l);
3088 line_def = es->first_line_def;
3089 while (line_def->index != li)
3090 line_def = line_def->next;
3092 ll = line_def->net_length;
3093 lw = line_def->width;
3095 w = es->format_rect.right - es->format_rect.left;
3096 if (es->style & ES_RIGHT)
3098 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3099 es->tabs_count, es->tabs)) - es->x_offset;
3102 else if (es->style & ES_CENTER)
3104 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3105 es->tabs_count, es->tabs)) - es->x_offset;
3110 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3111 es->tabs_count, es->tabs)) - es->x_offset;
3114 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3115 if (index < es->x_offset) {
3116 GetTextExtentPoint32W(dc, text + index,
3117 es->x_offset - index, &size);
3120 GetTextExtentPoint32W(dc, text + es->x_offset,
3121 index - es->x_offset, &size);
3124 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3126 w = es->format_rect.right - es->format_rect.left;
3127 if (w > es->text_width)
3129 if (es->style & ES_RIGHT)
3130 x += w - es->text_width;
3131 else if (es->style & ES_CENTER)
3132 x += (w - es->text_width) / 2;
3137 if (es->style & ES_PASSWORD)
3138 HeapFree(GetProcessHeap(), 0, text);
3140 x += es->format_rect.left;
3141 y += es->format_rect.top;
3143 SelectObject(dc, old_font);
3144 ReleaseDC(es->hwndSelf, dc);
3145 return MAKELONG((INT16)x, (INT16)y);
3149 /*********************************************************************
3153 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3156 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3158 UINT strl = strlenW(lpsz_replace);
3159 UINT tl = strlenW(es->text);
3170 TRACE("%s, can_undo %d, send_update %d\n",
3171 debugstr_w(lpsz_replace), can_undo, send_update);
3173 s = es->selection_start;
3174 e = es->selection_end;
3176 if ((s == e) && !strl)
3181 size = tl - (e - s) + strl;
3185 /* Issue the EN_MAXTEXT notification and continue with replacing text
3186 * such that buffer limit is honored. */
3187 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3188 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3189 strl = es->buffer_limit - (tl - (e-s));
3192 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3196 /* there is something to be deleted */
3197 TRACE("deleting stuff.\n");
3199 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3201 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3202 buf[bufl] = 0; /* ensure 0 termination */
3204 strcpyW(es->text + s, es->text + e);
3207 /* there is an insertion */
3208 tl = strlenW(es->text);
3209 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));
3210 for (p = es->text + tl ; p >= es->text + s ; p--)
3212 for (i = 0 , p = es->text + s ; i < strl ; i++)
3213 p[i] = lpsz_replace[i];
3214 if(es->style & ES_UPPERCASE)
3215 CharUpperBuffW(p, strl);
3216 else if(es->style & ES_LOWERCASE)
3217 CharLowerBuffW(p, strl);
3219 if (es->style & ES_MULTILINE)
3221 INT st = min(es->selection_start, es->selection_end);
3222 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3224 hrgn = CreateRectRgn(0, 0, 0, 0);
3225 EDIT_BuildLineDefs_ML(es, st, st + strl,
3226 strl - abs(es->selection_end - es->selection_start), hrgn);
3227 /* if text is too long undo all changes */
3228 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3230 strcpyW(es->text + e, es->text + e + strl);
3232 for (i = 0 , p = es->text ; i < e - s ; i++)
3234 EDIT_BuildLineDefs_ML(es, s, e,
3235 abs(es->selection_end - es->selection_start) - strl, hrgn);
3238 hrgn = CreateRectRgn(0, 0, 0, 0);
3239 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3243 INT fw = es->format_rect.right - es->format_rect.left;
3244 EDIT_CalcLineWidth_SL(es);
3245 /* remove chars that don't fit */
3246 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3247 while ((es->text_width > fw) && s + strl >= s) {
3248 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3250 EDIT_CalcLineWidth_SL(es);
3252 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3258 utl = strlenW(es->undo_text);
3259 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3260 /* undo-buffer is extended to the right */
3261 EDIT_MakeUndoFit(es, utl + e - s);
3262 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3263 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3264 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3265 /* undo-buffer is extended to the left */
3266 EDIT_MakeUndoFit(es, utl + e - s);
3267 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3269 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3271 es->undo_position = s;
3273 /* new undo-buffer */
3274 EDIT_MakeUndoFit(es, e - s);
3275 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3276 es->undo_text[e - s] = 0; /* ensure 0 termination */
3277 es->undo_position = s;
3279 /* any deletion makes the old insertion-undo invalid */
3280 es->undo_insert_count = 0;
3282 EDIT_EM_EmptyUndoBuffer(es);
3286 if ((s == es->undo_position) ||
3287 ((es->undo_insert_count) &&
3288 (s == es->undo_position + es->undo_insert_count)))
3290 * insertion is new and at delete position or
3291 * an extension to either left or right
3293 es->undo_insert_count += strl;
3295 /* new insertion undo */
3296 es->undo_position = s;
3297 es->undo_insert_count = strl;
3298 /* new insertion makes old delete-buffer invalid */
3299 *es->undo_text = '\0';
3302 EDIT_EM_EmptyUndoBuffer(es);
3306 HeapFree(GetProcessHeap(), 0, buf);
3310 /* If text has been deleted and we're right or center aligned then scroll rightward */
3311 if (es->style & (ES_RIGHT | ES_CENTER))
3313 INT delta = strl - abs(es->selection_end - es->selection_start);
3315 if (delta < 0 && es->x_offset)
3317 if (abs(delta) > es->x_offset)
3320 es->x_offset += delta;
3324 EDIT_EM_SetSel(es, s, s, FALSE);
3325 es->flags |= EF_MODIFIED;
3326 if (send_update) es->flags |= EF_UPDATE;
3329 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3333 EDIT_UpdateText(es, NULL, TRUE);
3335 EDIT_EM_ScrollCaret(es);
3337 /* force scroll info update */
3338 EDIT_UpdateScrollInfo(es);
3341 if(send_update || (es->flags & EF_UPDATE))
3343 es->flags &= ~EF_UPDATE;
3344 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3349 /*********************************************************************
3354 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3358 if (!(es->style & ES_MULTILINE))
3359 return (LRESULT)FALSE;
3369 if (es->y_offset < es->line_count - 1)
3374 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3377 if (es->y_offset < es->line_count - 1)
3378 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3381 return (LRESULT)FALSE;
3384 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3385 /* check if we are going to move too far */
3386 if(es->y_offset + dy > es->line_count - vlc)
3387 dy = es->line_count - vlc - es->y_offset;
3389 /* Notification is done in EDIT_EM_LineScroll */
3391 EDIT_EM_LineScroll(es, 0, dy);
3393 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3397 /*********************************************************************
3402 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3404 if (es->style & ES_MULTILINE) {
3409 INT cw = es->char_width;
3414 l = EDIT_EM_LineFromChar(es, es->selection_end);
3415 li = EDIT_EM_LineIndex(es, l);
3416 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3417 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3418 if (l >= es->y_offset + vlc)
3419 dy = l - vlc + 1 - es->y_offset;
3420 if (l < es->y_offset)
3421 dy = l - es->y_offset;
3422 ww = es->format_rect.right - es->format_rect.left;
3423 if (x < es->format_rect.left)
3424 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3425 if (x > es->format_rect.right)
3426 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3427 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3429 /* check if we are going to move too far */
3430 if(es->x_offset + dx + ww > es->text_width)
3431 dx = es->text_width - ww - es->x_offset;
3432 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3433 EDIT_EM_LineScroll_internal(es, dx, dy);
3440 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3441 format_width = es->format_rect.right - es->format_rect.left;
3442 if (x < es->format_rect.left) {
3443 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3446 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3447 } while ((x < goal) && es->x_offset);
3448 /* FIXME: use ScrollWindow() somehow to improve performance */
3449 EDIT_UpdateText(es, NULL, TRUE);
3450 } else if (x > es->format_rect.right) {
3452 INT len = strlenW(es->text);
3453 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3456 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3457 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3458 } while ((x > goal) && (x_last > es->format_rect.right));
3459 /* FIXME: use ScrollWindow() somehow to improve performance */
3460 EDIT_UpdateText(es, NULL, TRUE);
3464 if(es->flags & EF_FOCUSED)
3465 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3469 /*********************************************************************
3473 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3476 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3478 if (!(es->style & ES_MULTILINE))
3482 WARN("called with NULL handle\n");
3486 EDIT_UnlockBuffer(es, TRUE);
3490 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3491 HANDLE16 oldDS = stack16->ds;
3493 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3494 LocalFree16(es->hloc16);
3495 stack16->ds = oldDS;
3503 LocalFree(es->hloc32A);
3515 countA = LocalSize(hloc);
3516 textA = LocalLock(hloc);
3517 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3518 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3520 ERR("Could not allocate new unicode buffer\n");
3523 textW = LocalLock(hloc32W_new);
3524 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3525 LocalUnlock(hloc32W_new);
3529 LocalFree(es->hloc32W);
3531 es->hloc32W = hloc32W_new;
3535 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3537 EDIT_LockBuffer(es);
3539 es->x_offset = es->y_offset = 0;
3540 es->selection_start = es->selection_end = 0;
3541 EDIT_EM_EmptyUndoBuffer(es);
3542 es->flags &= ~EF_MODIFIED;
3543 es->flags &= ~EF_UPDATE;
3544 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3545 EDIT_UpdateText(es, NULL, TRUE);
3546 EDIT_EM_ScrollCaret(es);
3547 /* force scroll info update */
3548 EDIT_UpdateScrollInfo(es);
3552 /*********************************************************************
3556 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3559 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3561 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3562 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3563 HANDLE16 oldDS = stack16->ds;
3569 if (!(es->style & ES_MULTILINE))
3573 WARN("called with NULL handle\n");
3577 EDIT_UnlockBuffer(es, TRUE);
3581 LocalFree(es->hloc32A);
3585 stack16->ds = hInstance;
3586 countA = LocalSize16(hloc);
3587 textA = MapSL(LocalLock16(hloc));
3588 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3589 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3591 ERR("Could not allocate new unicode buffer\n");
3594 textW = LocalLock(hloc32W_new);
3595 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3596 LocalUnlock(hloc32W_new);
3597 LocalUnlock16(hloc);
3598 stack16->ds = oldDS;
3601 LocalFree(es->hloc32W);
3603 es->hloc32W = hloc32W_new;
3606 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3608 EDIT_LockBuffer(es);
3610 es->x_offset = es->y_offset = 0;
3611 es->selection_start = es->selection_end = 0;
3612 EDIT_EM_EmptyUndoBuffer(es);
3613 es->flags &= ~EF_MODIFIED;
3614 es->flags &= ~EF_UPDATE;
3615 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3616 EDIT_UpdateText(es, NULL, TRUE);
3617 EDIT_EM_ScrollCaret(es);
3618 /* force scroll info update */
3619 EDIT_UpdateScrollInfo(es);
3623 /*********************************************************************
3627 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3628 * However, the windows version is not complied to yet in all of edit.c
3630 * Additionally as the wrapper for RichEdit controls we need larger buffers
3631 * at present -1 will represent nolimit
3633 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3635 if (limit == 0xFFFFFFFF)
3636 es->buffer_limit = -1;
3637 else if (es->style & ES_MULTILINE) {
3639 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3641 es->buffer_limit = BUFLIMIT_MULTI;
3644 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3646 es->buffer_limit = BUFLIMIT_SINGLE;
3651 /*********************************************************************
3655 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3656 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3657 * margin according to the textmetrics of the current font.
3659 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3660 * of the char's width as the margin, but this is not how Windows handles this.
3661 * For all other fonts Windows sets the margins to zero.
3664 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3665 INT left, INT right, BOOL repaint)
3668 INT default_left_margin = 0; /* in pixels */
3669 INT default_right_margin = 0; /* in pixels */
3671 /* Set the default margins depending on the font */
3672 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3673 HDC dc = GetDC(es->hwndSelf);
3674 HFONT old_font = SelectObject(dc, es->font);
3675 GetTextMetricsW(dc, &tm);
3676 /* The default margins are only non zero for TrueType or Vector fonts */
3677 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3678 /* This must be calculated more exactly! But how? */
3679 default_left_margin = tm.tmAveCharWidth / 3;
3680 default_right_margin = tm.tmAveCharWidth / 3;
3682 SelectObject(dc, old_font);
3683 ReleaseDC(es->hwndSelf, dc);
3686 if (action & EC_LEFTMARGIN) {
3687 es->format_rect.left -= es->left_margin;
3688 if (left != EC_USEFONTINFO)
3689 es->left_margin = left;
3691 es->left_margin = default_left_margin;
3692 es->format_rect.left += es->left_margin;
3695 if (action & EC_RIGHTMARGIN) {
3696 es->format_rect.right += es->right_margin;
3697 if (right != EC_USEFONTINFO)
3698 es->right_margin = right;
3700 es->right_margin = default_right_margin;
3701 es->format_rect.right -= es->right_margin;
3704 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3705 EDIT_AdjustFormatRect(es);
3706 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3709 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3713 /*********************************************************************
3715 * EM_SETPASSWORDCHAR
3718 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3722 if (es->style & ES_MULTILINE)
3725 if (es->password_char == c)
3728 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3729 es->password_char = c;
3731 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3732 es->style |= ES_PASSWORD;
3734 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3735 es->style &= ~ES_PASSWORD;
3737 EDIT_UpdateText(es, NULL, TRUE);
3741 /*********************************************************************
3745 * note: unlike the specs say: the order of start and end
3746 * _is_ preserved in Windows. (i.e. start can be > end)
3747 * In other words: this handler is OK
3750 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3752 UINT old_start = es->selection_start;
3753 UINT old_end = es->selection_end;
3754 UINT len = strlenW(es->text);
3756 if (start == (UINT)-1) {
3757 start = es->selection_end;
3758 end = es->selection_end;
3760 start = min(start, len);
3761 end = min(end, len);
3763 es->selection_start = start;
3764 es->selection_end = end;
3766 es->flags |= EF_AFTER_WRAP;
3768 es->flags &= ~EF_AFTER_WRAP;
3769 /* Compute the necessary invalidation region. */
3770 /* Note that we don't need to invalidate regions which have
3771 * "never" been selected, or those which are "still" selected.
3772 * In fact, every time we hit a selection boundary, we can
3773 * *toggle* whether we need to invalidate. Thus we can optimize by
3774 * *sorting* the interval endpoints. Let's assume that we sort them
3776 * start <= end <= old_start <= old_end
3777 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3778 * in 5 comparisons; ie it's impossible to do better than the
3780 ORDER_UINT(end, old_end);
3781 ORDER_UINT(start, old_start);
3782 ORDER_UINT(old_start, old_end);
3783 ORDER_UINT(start, end);
3784 /* Note that at this point 'end' and 'old_start' are not in order, but
3785 * start is definitely the min. and old_end is definitely the max. */
3786 if (end != old_start)
3790 * ORDER_UINT32(end, old_start);
3791 * EDIT_InvalidateText(es, start, end);
3792 * EDIT_InvalidateText(es, old_start, old_end);
3793 * in place of the following if statement.
3794 * (That would complete the optimal five-comparison four-element sort.)
3796 if (old_start > end )
3798 EDIT_InvalidateText(es, start, end);
3799 EDIT_InvalidateText(es, old_start, old_end);
3803 EDIT_InvalidateText(es, start, old_start);
3804 EDIT_InvalidateText(es, end, old_end);
3807 else EDIT_InvalidateText(es, start, old_end);
3811 /*********************************************************************
3816 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3818 if (!(es->style & ES_MULTILINE))
3820 HeapFree(GetProcessHeap(), 0, es->tabs);
3821 es->tabs_count = count;
3825 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3826 memcpy(es->tabs, tabs, count * sizeof(INT));
3832 /*********************************************************************
3837 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3839 if (!(es->style & ES_MULTILINE))
3841 HeapFree(GetProcessHeap(), 0, es->tabs);
3842 es->tabs_count = count;
3847 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3848 for (i = 0 ; i < count ; i++)
3849 es->tabs[i] = *tabs++;
3855 /*********************************************************************
3857 * EM_SETWORDBREAKPROC
3860 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3862 if (es->word_break_proc == wbp)
3865 es->word_break_proc = wbp;
3866 es->word_break_proc16 = NULL;
3868 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3869 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3870 EDIT_UpdateText(es, NULL, TRUE);
3875 /*********************************************************************
3877 * EM_SETWORDBREAKPROC16
3880 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3882 if (es->word_break_proc16 == wbp)
3885 es->word_break_proc = NULL;
3886 es->word_break_proc16 = wbp;
3887 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3888 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3889 EDIT_UpdateText(es, NULL, TRUE);
3894 /*********************************************************************
3899 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3904 /* As per MSDN spec, for a single-line edit control,
3905 the return value is always TRUE */
3906 if( es->style & ES_READONLY )
3907 return !(es->style & ES_MULTILINE);
3909 ulength = strlenW(es->undo_text);
3911 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3913 strcpyW(utext, es->undo_text);
3915 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3916 es->undo_insert_count, debugstr_w(utext));
3918 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3919 EDIT_EM_EmptyUndoBuffer(es);
3920 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3921 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3922 /* send the notification after the selection start and end are set */
3923 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3924 EDIT_EM_ScrollCaret(es);
3925 HeapFree(GetProcessHeap(), 0, utext);
3927 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3928 es->undo_insert_count, debugstr_w(es->undo_text));
3933 /*********************************************************************
3938 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3942 control = GetKeyState(VK_CONTROL) & 0x8000;
3946 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3947 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3950 if (es->style & ES_MULTILINE) {
3951 if (es->style & ES_READONLY) {
3952 EDIT_MoveHome(es, FALSE);
3953 EDIT_MoveDown_ML(es, FALSE);
3955 static const WCHAR cr_lfW[] = {'\r','\n',0};
3956 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3961 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3963 static const WCHAR tabW[] = {'\t',0};
3964 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3968 if (!(es->style & ES_READONLY) && !control) {
3969 if (es->selection_start != es->selection_end)
3972 /* delete character left of caret */
3973 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3974 EDIT_MoveBackward(es, TRUE);
3980 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3983 if (!(es->style & ES_READONLY))
3984 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3987 if (!(es->style & ES_READONLY))
3988 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3992 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3993 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3996 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4000 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4007 /*********************************************************************
4012 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4014 if (code || control)
4034 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4035 EDIT_EM_ScrollCaret(es);
4038 ERR("unknown menu item, please report\n");
4044 /*********************************************************************
4048 * Note: the resource files resource/sysres_??.rc cannot define a
4049 * single popup menu. Hence we use a (dummy) menubar
4050 * containing the single popup menu as its first item.
4052 * FIXME: the message identifiers have been chosen arbitrarily,
4053 * hence we use MF_BYPOSITION.
4054 * We might as well use the "real" values (anybody knows ?)
4055 * The menu definition is in resources/sysres_??.rc.
4056 * Once these are OK, we better use MF_BYCOMMAND here
4057 * (as we do in EDIT_WM_Command()).
4060 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4062 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4063 HMENU popup = GetSubMenu(menu, 0);
4064 UINT start = es->selection_start;
4065 UINT end = es->selection_end;
4067 ORDER_UINT(start, end);
4070 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4072 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4074 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4076 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4078 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4080 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
4082 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4087 /*********************************************************************
4092 static void EDIT_WM_Copy(EDITSTATE *es)
4094 INT s = min(es->selection_start, es->selection_end);
4095 INT e = max(es->selection_start, es->selection_end);
4103 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4104 dst = GlobalLock(hdst);
4105 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4106 dst[len] = 0; /* ensure 0 termination */
4107 TRACE("%s\n", debugstr_w(dst));
4109 OpenClipboard(es->hwndSelf);
4111 SetClipboardData(CF_UNICODETEXT, hdst);
4116 /*********************************************************************
4121 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4125 TRACE("%s\n", debugstr_w(name));
4127 * To initialize some final structure members, we call some helper
4128 * functions. However, since the EDITSTATE is not consistent (i.e.
4129 * not fully initialized), we should be very careful which
4130 * functions can be called, and in what order.
4132 EDIT_WM_SetFont(es, 0, FALSE);
4133 EDIT_EM_EmptyUndoBuffer(es);
4135 /* We need to calculate the format rect
4136 (applications may send EM_SETMARGINS before the control gets visible) */
4137 GetClientRect(es->hwndSelf, &clientRect);
4138 EDIT_SetRectNP(es, &clientRect);
4140 if (name && *name) {
4141 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4142 /* if we insert text to the editline, the text scrolls out
4143 * of the window, as the caret is placed after the insert
4144 * pos normally; thus we reset es->selection... to 0 and
4147 es->selection_start = es->selection_end = 0;
4148 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4149 * Messages are only to be sent when the USER does something to
4150 * change the contents. So I am removing this EN_CHANGE
4152 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4154 EDIT_EM_ScrollCaret(es);
4156 /* force scroll info update */
4157 EDIT_UpdateScrollInfo(es);
4158 /* The rule seems to return 1 here for success */
4159 /* Power Builder masked edit controls will crash */
4161 /* FIXME: is that in all cases so ? */
4166 /*********************************************************************
4171 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4176 while (LocalUnlock(es->hloc32W)) ;
4177 LocalFree(es->hloc32W);
4180 while (LocalUnlock(es->hloc32A)) ;
4181 LocalFree(es->hloc32A);
4184 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
4185 HANDLE16 oldDS = stack16->ds;
4187 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4188 while (LocalUnlock16(es->hloc16)) ;
4189 LocalFree16(es->hloc16);
4190 stack16->ds = oldDS;
4193 pc = es->first_line_def;
4197 HeapFree(GetProcessHeap(), 0, pc);
4201 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4202 HeapFree(GetProcessHeap(), 0, es);
4208 /*********************************************************************
4213 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4215 /* we do the proper erase in EDIT_WM_Paint */
4220 /*********************************************************************
4225 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4227 if(!count) return 0;
4231 lstrcpynW(dst, es->text, count);
4232 return strlenW(dst);
4236 LPSTR textA = (LPSTR)dst;
4237 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4238 textA[count - 1] = 0; /* ensure 0 termination */
4239 return strlen(textA);
4243 /*********************************************************************
4248 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4253 if (!(es->style & ES_MULTILINE))
4256 if (!(es->style & ES_AUTOHSCROLL))
4260 fw = es->format_rect.right - es->format_rect.left;
4263 TRACE("SB_LINELEFT\n");
4265 dx = -es->char_width;
4268 TRACE("SB_LINERIGHT\n");
4269 if (es->x_offset < es->text_width)
4270 dx = es->char_width;
4273 TRACE("SB_PAGELEFT\n");
4275 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4278 TRACE("SB_PAGERIGHT\n");
4279 if (es->x_offset < es->text_width)
4280 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4288 TRACE("SB_RIGHT\n");
4289 if (es->x_offset < es->text_width)
4290 dx = es->text_width - es->x_offset;
4293 TRACE("SB_THUMBTRACK %d\n", pos);
4294 es->flags |= EF_HSCROLL_TRACK;
4295 if(es->style & WS_HSCROLL)
4296 dx = pos - es->x_offset;
4301 if(pos < 0 || pos > 100) return 0;
4302 /* Assume default scroll range 0-100 */
4303 fw = es->format_rect.right - es->format_rect.left;
4304 new_x = pos * (es->text_width - fw) / 100;
4305 dx = es->text_width ? (new_x - es->x_offset) : 0;
4308 case SB_THUMBPOSITION:
4309 TRACE("SB_THUMBPOSITION %d\n", pos);
4310 es->flags &= ~EF_HSCROLL_TRACK;
4311 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4312 dx = pos - es->x_offset;
4317 if(pos < 0 || pos > 100) return 0;
4318 /* Assume default scroll range 0-100 */
4319 fw = es->format_rect.right - es->format_rect.left;
4320 new_x = pos * (es->text_width - fw) / 100;
4321 dx = es->text_width ? (new_x - es->x_offset) : 0;
4324 /* force scroll info update */
4325 EDIT_UpdateScrollInfo(es);
4326 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4330 TRACE("SB_ENDSCROLL\n");
4333 * FIXME : the next two are undocumented !
4334 * Are we doing the right thing ?
4335 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4336 * although it's also a regular control message.
4338 case EM_GETTHUMB: /* this one is used by NT notepad */
4342 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4343 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4346 /* Assume default scroll range 0-100 */
4347 INT fw = es->format_rect.right - es->format_rect.left;
4348 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4350 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4353 case EM_LINESCROLL16:
4354 TRACE("EM_LINESCROLL16\n");
4359 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4365 INT fw = es->format_rect.right - es->format_rect.left;
4366 /* check if we are going to move too far */
4367 if(es->x_offset + dx + fw > es->text_width)
4368 dx = es->text_width - fw - es->x_offset;
4370 EDIT_EM_LineScroll_internal(es, dx, 0);
4376 /*********************************************************************
4381 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4383 HWND hLBox = es->hwndListBox;
4391 hCombo = GetParent(es->hwndSelf);
4395 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4397 if (key == VK_UP || key == VK_DOWN)
4399 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4402 if (msg == WM_KEYDOWN || nEUI)
4403 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4409 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4411 /* make sure ComboLBox pops up */
4412 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4417 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4420 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4422 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4424 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4429 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4435 /*********************************************************************
4439 * Handling of special keys that don't produce a WM_CHAR
4440 * (i.e. non-printable keys) & Backspace & Delete
4443 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4448 if (GetKeyState(VK_MENU) & 0x8000)
4451 shift = GetKeyState(VK_SHIFT) & 0x8000;
4452 control = GetKeyState(VK_CONTROL) & 0x8000;
4457 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4462 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4463 EDIT_MoveUp_ML(es, shift);
4466 EDIT_MoveWordBackward(es, shift);
4468 EDIT_MoveBackward(es, shift);
4471 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4475 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4476 EDIT_MoveDown_ML(es, shift);
4478 EDIT_MoveWordForward(es, shift);
4480 EDIT_MoveForward(es, shift);
4483 EDIT_MoveHome(es, shift);
4486 EDIT_MoveEnd(es, shift);
4489 if (es->style & ES_MULTILINE)
4490 EDIT_MovePageUp_ML(es, shift);
4492 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4495 if (es->style & ES_MULTILINE)
4496 EDIT_MovePageDown_ML(es, shift);
4498 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4501 if (!(es->style & ES_READONLY) && !(shift && control)) {
4502 if (es->selection_start != es->selection_end) {
4509 /* delete character left of caret */
4510 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4511 EDIT_MoveBackward(es, TRUE);
4513 } else if (control) {
4514 /* delete to end of line */
4515 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4516 EDIT_MoveEnd(es, TRUE);
4519 /* delete character right of caret */
4520 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4521 EDIT_MoveForward(es, TRUE);
4529 if (!(es->style & ES_READONLY))
4535 /* If the edit doesn't want the return send a message to the default object */
4536 if(!(es->style & ES_WANTRETURN))
4538 HWND hwndParent = GetParent(es->hwndSelf);
4539 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4540 if (HIWORD(dw) == DC_HASDEFID)
4542 SendMessageW( hwndParent, WM_COMMAND,
4543 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4544 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4553 /*********************************************************************
4558 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4560 es->flags &= ~EF_FOCUSED;
4562 if(!(es->style & ES_NOHIDESEL))
4563 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4564 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4569 /*********************************************************************
4573 * The caret position has been set on the WM_LBUTTONDOWN message
4576 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4579 INT e = es->selection_end;
4584 es->bCaptureState = TRUE;
4585 SetCapture(es->hwndSelf);
4587 l = EDIT_EM_LineFromChar(es, e);
4588 li = EDIT_EM_LineIndex(es, l);
4589 ll = EDIT_EM_LineLength(es, e);
4590 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4591 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4592 EDIT_EM_SetSel(es, s, e, FALSE);
4593 EDIT_EM_ScrollCaret(es);
4594 es->region_posx = es->region_posy = 0;
4595 SetTimer(es->hwndSelf, 0, 100, NULL);
4600 /*********************************************************************
4605 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4610 es->bCaptureState = TRUE;
4611 SetCapture(es->hwndSelf);
4612 EDIT_ConfinePoint(es, &x, &y);
4613 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4614 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4615 EDIT_EM_ScrollCaret(es);
4616 es->region_posx = es->region_posy = 0;
4617 SetTimer(es->hwndSelf, 0, 100, NULL);
4619 if (!(es->flags & EF_FOCUSED))
4620 SetFocus(es->hwndSelf);
4626 /*********************************************************************
4631 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4633 if (es->bCaptureState) {
4634 KillTimer(es->hwndSelf, 0);
4635 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4637 es->bCaptureState = FALSE;
4642 /*********************************************************************
4647 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4649 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4654 /*********************************************************************
4659 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4665 /* If the mouse has been captured by process other than the edit control itself,
4666 * the windows edit controls will not select the strings with mouse move.
4668 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4672 * FIXME: gotta do some scrolling if outside client
4673 * area. Maybe reset the timer ?
4676 EDIT_ConfinePoint(es, &x, &y);
4677 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4678 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4679 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4680 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4681 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4686 /*********************************************************************
4690 * See also EDIT_WM_StyleChanged
4692 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4697 TRACE("Creating %s edit control, style = %08lx\n",
4698 unicode ? "Unicode" : "ANSI", lpcs->style);
4700 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4702 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4705 * Note: since the EDITSTATE has not been fully initialized yet,
4706 * we can't use any API calls that may send
4707 * WM_XXX messages before WM_NCCREATE is completed.
4710 es->is_unicode = unicode;
4711 es->style = lpcs->style;
4713 es->bEnableState = !(es->style & WS_DISABLED);
4715 es->hwndSelf = hwnd;
4716 /* Save parent, which will be notified by EN_* messages */
4717 es->hwndParent = lpcs->hwndParent;
4719 if (es->style & ES_COMBO)
4720 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4722 /* Number overrides lowercase overrides uppercase (at least it
4723 * does in Win95). However I'll bet that ES_NUMBER would be
4724 * invalid under Win 3.1.
4726 if (es->style & ES_NUMBER) {
4727 ; /* do not override the ES_NUMBER */
4728 } else if (es->style & ES_LOWERCASE) {
4729 es->style &= ~ES_UPPERCASE;
4731 if (es->style & ES_MULTILINE) {
4732 es->buffer_limit = BUFLIMIT_MULTI;
4733 if (es->style & WS_VSCROLL)
4734 es->style |= ES_AUTOVSCROLL;
4735 if (es->style & WS_HSCROLL)
4736 es->style |= ES_AUTOHSCROLL;
4737 es->style &= ~ES_PASSWORD;
4738 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4739 /* Confirmed - RIGHT overrides CENTER */
4740 if (es->style & ES_RIGHT)
4741 es->style &= ~ES_CENTER;
4742 es->style &= ~WS_HSCROLL;
4743 es->style &= ~ES_AUTOHSCROLL;
4746 es->buffer_limit = BUFLIMIT_SINGLE;
4747 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4748 es->style &= ~ES_CENTER;
4749 es->style &= ~WS_HSCROLL;
4750 es->style &= ~WS_VSCROLL;
4751 if (es->style & ES_PASSWORD)
4752 es->password_char = '*';
4755 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4756 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4758 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4760 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4762 es->undo_buffer_size = es->buffer_size;
4764 if (es->style & ES_MULTILINE)
4765 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4770 * In Win95 look and feel, the WS_BORDER style is replaced by the
4771 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4772 * control a nonclient area so we don't need to draw the border.
4773 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4774 * a nonclient area and we should handle painting the border ourselves.
4776 * When making modifications please ensure that the code still works
4777 * for edit controls created directly with style 0x50800000, exStyle 0
4778 * (which should have a single pixel border)
4780 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4781 es->style &= ~WS_BORDER;
4782 else if (es->style & WS_BORDER)
4783 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4788 /*********************************************************************
4793 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4806 BOOL rev = es->bEnableState &&
4807 ((es->flags & EF_FOCUSED) ||
4808 (es->style & ES_NOHIDESEL));
4809 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4811 GetClientRect(es->hwndSelf, &rcClient);
4813 /* get the background brush */
4814 brush = EDIT_NotifyCtlColor(es, dc);
4816 /* paint the border and the background */
4817 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4819 if(es->style & WS_BORDER) {
4820 bw = GetSystemMetrics(SM_CXBORDER);
4821 bh = GetSystemMetrics(SM_CYBORDER);
4823 if(es->style & ES_MULTILINE) {
4824 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4825 if(es->style & WS_VSCROLL) rc.right+=bw;
4828 /* Draw the frame. Same code as in nonclient.c */
4829 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4830 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4831 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4832 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4833 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4834 SelectObject(dc, old_brush);
4836 /* Keep the border clean */
4837 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4838 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4841 GetClipBox(dc, &rc);
4842 FillRect(dc, &rc, brush);
4844 IntersectClipRect(dc, es->format_rect.left,
4845 es->format_rect.top,
4846 es->format_rect.right,
4847 es->format_rect.bottom);
4848 if (es->style & ES_MULTILINE) {
4850 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4853 old_font = SelectObject(dc, es->font);
4855 if (!es->bEnableState)
4856 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4857 GetClipBox(dc, &rcRgn);
4858 if (es->style & ES_MULTILINE) {
4859 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4860 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4861 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4862 if (IntersectRect(&rc, &rcRgn, &rcLine))
4863 EDIT_PaintLine(es, dc, i, rev);
4866 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4867 if (IntersectRect(&rc, &rcRgn, &rcLine))
4868 EDIT_PaintLine(es, dc, 0, rev);
4871 SelectObject(dc, old_font);
4874 EndPaint(es->hwndSelf, &ps);
4878 /*********************************************************************
4883 static void EDIT_WM_Paste(EDITSTATE *es)
4888 /* Protect read-only edit control from modification */
4889 if(es->style & ES_READONLY)
4892 OpenClipboard(es->hwndSelf);
4893 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4894 src = (LPWSTR)GlobalLock(hsrc);
4895 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4902 /*********************************************************************
4907 static void EDIT_WM_SetFocus(EDITSTATE *es)
4909 es->flags |= EF_FOCUSED;
4911 if (!(es->style & ES_NOHIDESEL))
4912 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4914 /* single line edit updates itself */
4915 if (!(es->style & ES_MULTILINE))
4917 HDC hdc = GetDC(es->hwndSelf);
4918 EDIT_WM_Paint(es, hdc);
4919 ReleaseDC(es->hwndSelf, hdc);
4922 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4923 EDIT_SetCaretPos(es, es->selection_end,
4924 es->flags & EF_AFTER_WRAP);
4925 ShowCaret(es->hwndSelf);
4926 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
4930 /*********************************************************************
4934 * With Win95 look the margins are set to default font value unless
4935 * the system font (font == 0) is being set, in which case they are left
4939 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4947 dc = GetDC(es->hwndSelf);
4949 old_font = SelectObject(dc, font);
4950 GetTextMetricsW(dc, &tm);
4951 es->line_height = tm.tmHeight;
4952 es->char_width = tm.tmAveCharWidth;
4954 SelectObject(dc, old_font);
4955 ReleaseDC(es->hwndSelf, dc);
4957 /* Reset the format rect and the margins */
4958 GetClientRect(es->hwndSelf, &clientRect);
4959 EDIT_SetRectNP(es, &clientRect);
4960 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4961 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
4963 if (es->style & ES_MULTILINE)
4964 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4966 EDIT_CalcLineWidth_SL(es);
4969 EDIT_UpdateText(es, NULL, TRUE);
4970 if (es->flags & EF_FOCUSED) {
4972 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4973 EDIT_SetCaretPos(es, es->selection_end,
4974 es->flags & EF_AFTER_WRAP);
4975 ShowCaret(es->hwndSelf);
4980 /*********************************************************************
4985 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4986 * The modified flag is reset. No notifications are sent.
4988 * For single-line controls, reception of WM_SETTEXT triggers:
4989 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4992 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4994 if (!unicode && text)
4996 LPCSTR textA = (LPCSTR)text;
4997 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4998 LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5000 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5004 if (es->flags & EF_UPDATE)
5005 /* fixed this bug once; complain if we see it about to happen again. */
5006 ERR("SetSel may generate UPDATE message whose handler may reset "
5009 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5012 TRACE("%s\n", debugstr_w(text));
5013 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5015 HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
5019 static const WCHAR empty_stringW[] = {0};
5021 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5024 es->flags &= ~EF_MODIFIED;
5025 EDIT_EM_SetSel(es, 0, 0, FALSE);
5027 /* Send the notification after the selection start and end have been set
5028 * edit control doesn't send notification on WM_SETTEXT
5029 * if it is multiline, or it is part of combobox
5031 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5033 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5034 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5036 EDIT_EM_ScrollCaret(es);
5037 EDIT_UpdateScrollInfo(es);
5041 /*********************************************************************
5046 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5048 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5050 TRACE("width = %d, height = %d\n", width, height);
5051 SetRect(&rc, 0, 0, width, height);
5052 EDIT_SetRectNP(es, &rc);
5053 EDIT_UpdateText(es, NULL, TRUE);
5058 /*********************************************************************
5062 * This message is sent by SetWindowLong on having changed either the Style
5063 * or the extended style.
5065 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5067 * See also EDIT_WM_NCCreate
5069 * It appears that the Windows version of the edit control allows the style
5070 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5071 * style variable which will generally be different. In this function we
5072 * update the internal style based on what changed in the externally visible
5075 * Much of this content as based upon the MSDN, especially:
5076 * Platform SDK Documentation -> User Interface Services ->
5077 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5078 * Edit Control Styles
5080 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5082 if (GWL_STYLE == which) {
5083 DWORD style_change_mask;
5085 /* Only a subset of changes can be applied after the control
5088 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5090 if (es->style & ES_MULTILINE)
5091 style_change_mask |= ES_WANTRETURN;
5093 new_style = style->styleNew & style_change_mask;
5095 /* Number overrides lowercase overrides uppercase (at least it
5096 * does in Win95). However I'll bet that ES_NUMBER would be
5097 * invalid under Win 3.1.
5099 if (new_style & ES_NUMBER) {
5100 ; /* do not override the ES_NUMBER */
5101 } else if (new_style & ES_LOWERCASE) {
5102 new_style &= ~ES_UPPERCASE;
5105 es->style = (es->style & ~style_change_mask) | new_style;
5106 } else if (GWL_EXSTYLE == which) {
5107 ; /* FIXME - what is needed here */
5109 WARN ("Invalid style change %d\n",which);
5115 /*********************************************************************
5120 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5122 if ((key == VK_BACK) && (key_data & 0x2000)) {
5123 if (EDIT_EM_CanUndo(es))
5126 } else if (key == VK_UP || key == VK_DOWN) {
5127 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5130 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5134 /*********************************************************************
5139 static void EDIT_WM_Timer(EDITSTATE *es)
5141 if (es->region_posx < 0) {
5142 EDIT_MoveBackward(es, TRUE);
5143 } else if (es->region_posx > 0) {
5144 EDIT_MoveForward(es, TRUE);
5147 * FIXME: gotta do some vertical scrolling here, like
5148 * EDIT_EM_LineScroll(hwnd, 0, 1);
5152 /*********************************************************************
5157 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5161 if (!(es->style & ES_MULTILINE))
5164 if (!(es->style & ES_AUTOVSCROLL))
5173 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5174 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5175 (action == SB_PAGEUP ? "SB_PAGEUP" :
5177 EDIT_EM_Scroll(es, action);
5184 TRACE("SB_BOTTOM\n");
5185 dy = es->line_count - 1 - es->y_offset;
5188 TRACE("SB_THUMBTRACK %d\n", pos);
5189 es->flags |= EF_VSCROLL_TRACK;
5190 if(es->style & WS_VSCROLL)
5191 dy = pos - es->y_offset;
5194 /* Assume default scroll range 0-100 */
5197 if(pos < 0 || pos > 100) return 0;
5198 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5199 new_y = pos * (es->line_count - vlc) / 100;
5200 dy = es->line_count ? (new_y - es->y_offset) : 0;
5201 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5202 es->line_count, es->y_offset, pos, dy);
5205 case SB_THUMBPOSITION:
5206 TRACE("SB_THUMBPOSITION %d\n", pos);
5207 es->flags &= ~EF_VSCROLL_TRACK;
5208 if(es->style & WS_VSCROLL)
5209 dy = pos - es->y_offset;
5212 /* Assume default scroll range 0-100 */
5215 if(pos < 0 || pos > 100) return 0;
5216 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5217 new_y = pos * (es->line_count - vlc) / 100;
5218 dy = es->line_count ? (new_y - es->y_offset) : 0;
5219 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5220 es->line_count, es->y_offset, pos, dy);
5224 /* force scroll info update */
5225 EDIT_UpdateScrollInfo(es);
5226 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5230 TRACE("SB_ENDSCROLL\n");
5233 * FIXME : the next two are undocumented !
5234 * Are we doing the right thing ?
5235 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5236 * although it's also a regular control message.
5238 case EM_GETTHUMB: /* this one is used by NT notepad */
5242 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5243 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5246 /* Assume default scroll range 0-100 */
5247 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5248 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5250 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5253 case EM_LINESCROLL16:
5254 TRACE("EM_LINESCROLL16 %d\n", pos);
5259 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5264 EDIT_EM_LineScroll(es, 0, dy);
5268 /*********************************************************************
5273 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5275 if (es->flags & EF_UPDATE) {
5276 es->flags &= ~EF_UPDATE;
5277 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5279 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5283 /*********************************************************************
5288 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5290 if (es->flags & EF_UPDATE) {
5291 es->flags &= ~EF_UPDATE;
5292 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5294 InvalidateRect(es->hwndSelf, rc, bErase);
5297 /********************************************************************
5299 * The Following code is to handle inline editing from IMEs
5302 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5305 LPWSTR lpCompStr = NULL;
5307 LPSTR lpCompStrAttr = NULL;
5310 if (!(hIMC = ImmGetContext(hwnd)))
5313 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5317 ImmReleaseContext(hwnd, hIMC);
5321 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen);
5324 ERR("Unable to allocate IME CompositionString\n");
5325 ImmReleaseContext(hwnd,hIMC);
5329 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
5330 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
5332 if (CompFlag & GCS_COMPATTR)
5335 * We do not use the attributes yet. it would tell us what characters
5336 * are in transition and which are converted or decided upon
5338 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5342 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr);
5345 ERR("Unable to allocate IME Attribute String\n");
5346 HeapFree(GetProcessHeap(),0,lpCompStr);
5347 ImmReleaseContext(hwnd,hIMC);
5350 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5352 lpCompStrAttr[dwBufLenAttr] = 0;
5355 lpCompStrAttr = NULL;
5358 /* check for change in composition start */
5359 if (es->selection_end < es->composition_start)
5360 es->composition_start = es->selection_end;
5362 /* replace existing selection string */
5363 es->selection_start = es->composition_start;
5365 if (es->composition_len > 0)
5366 es->selection_end = es->composition_start + es->composition_len;
5368 es->selection_end = es->selection_start;
5370 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5371 es->composition_len = abs(es->composition_start - es->selection_end);
5373 es->selection_start = es->composition_start;
5374 es->selection_end = es->selection_start + es->composition_len;
5376 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5377 HeapFree(GetProcessHeap(),0,lpCompStr);
5378 ImmReleaseContext(hwnd,hIMC);
5381 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5387 if ( !(hIMC = ImmGetContext(hwnd)))
5390 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5393 ImmReleaseContext(hwnd, hIMC);
5397 lpResultStr = HeapAlloc(GetProcessHeap(),0, dwBufLen);
5400 ERR("Unable to alloc buffer for IME string\n");
5401 ImmReleaseContext(hwnd, hIMC);
5405 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, dwBufLen);
5406 lpResultStr[dwBufLen/sizeof(WCHAR)] = 0;
5408 /* check for change in composition start */
5409 if (es->selection_end < es->composition_start)
5410 es->composition_start = es->selection_end;
5412 es->selection_start = es->composition_start;
5413 es->selection_end = es->composition_start + es->composition_len;
5414 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5415 es->composition_start = es->selection_end;
5416 es->composition_len = 0;
5418 HeapFree(GetProcessHeap(),0,lpResultStr);
5419 ImmReleaseContext(hwnd, hIMC);
5422 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5424 if (CompFlag & GCS_RESULTSTR)
5425 EDIT_GetResultStr(hwnd,es);
5426 if (CompFlag & GCS_COMPSTR)
5427 EDIT_GetCompositionStr(hwnd, CompFlag, es);