4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
57 #include "wine/winbase16.h"
58 #include "wine/winuser16.h"
59 #include "wine/unicode.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
69 FIXME: BTW, new specs say 65535 (do you dare ???) */
70 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
71 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
72 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
73 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
76 * extra flags for EDITSTATE.flags field
78 #define EF_MODIFIED 0x0001 /* text has been modified */
79 #define EF_FOCUSED 0x0002 /* we have input focus */
80 #define EF_UPDATE 0x0004 /* notify parent of changed state */
81 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
82 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
83 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
84 wrapped line, instead of in front of the next character */
85 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
86 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
87 the text buffer if this is clear. */
90 END_0 = 0, /* line ends with terminating '\0' character */
91 END_WRAP, /* line is wrapped */
92 END_HARD, /* line ends with a hard return '\r\n' */
93 END_SOFT, /* line ends with a soft return '\r\r\n' */
94 END_RICH /* line ends with a single '\n' */
97 typedef struct tagLINEDEF {
98 INT length; /* bruto length of a line in bytes */
99 INT net_length; /* netto length of a line in visible characters */
101 INT width; /* width of the line in pixels */
102 INT index; /* line index into the buffer */
103 struct tagLINEDEF *next;
108 BOOL is_unicode; /* how the control was created */
109 LPWSTR text; /* the actual contents of the control */
110 UINT buffer_size; /* the size of the buffer in characters */
111 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
112 HFONT font; /* NULL means standard system font */
113 INT x_offset; /* scroll offset for multi lines this is in pixels
114 for single lines it's in characters */
115 INT line_height; /* height of a screen line in pixels */
116 INT char_width; /* average character width in pixels */
117 DWORD style; /* sane version of wnd->dwStyle */
118 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
119 INT undo_insert_count; /* number of characters inserted in sequence */
120 UINT undo_position; /* character index of the insertion and deletion */
121 LPWSTR undo_text; /* deleted text */
122 UINT undo_buffer_size; /* size of the deleted text buffer */
123 INT selection_start; /* == selection_end if no selection */
124 INT selection_end; /* == current caret position */
125 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
126 INT left_margin; /* in pixels */
127 INT right_margin; /* in pixels */
129 INT text_width; /* width of the widest line in pixels for multi line controls
130 and just line width for single line controls */
131 INT region_posx; /* Position of cursor relative to region: */
132 INT region_posy; /* -1: to left, 0: within, 1: to right */
133 EDITWORDBREAKPROC16 word_break_proc16;
134 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
135 INT line_count; /* number of lines */
136 INT y_offset; /* scroll offset in number of lines */
137 BOOL bCaptureState; /* flag indicating whether mouse was captured */
138 BOOL bEnableState; /* flag keeping the enable state */
139 HWND hwndSelf; /* the our window handle */
140 HWND hwndParent; /* Handle of parent for sending EN_* messages.
141 Even if parent will change, EN_* messages
142 should be sent to the first parent. */
143 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
145 * only for multi line controls
147 INT lock_count; /* amount of re-entries in the EditWndProc */
150 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
151 HLOCAL hloc32W; /* our unicode local memory block */
152 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
154 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
159 UINT composition_len; /* length of composition, 0 == no composition */
160 int composition_start; /* the character position for the composition */
164 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
165 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
167 /* used for disabled or read-only edit control */
168 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
170 { /* Notify parent which has created this edit control */ \
171 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
172 SendMessageW(es->hwndParent, WM_COMMAND, \
173 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
174 (LPARAM)(es->hwndSelf)); \
177 /*********************************************************************
184 * These functions have trivial implementations
185 * We still like to call them internally
186 * "static inline" makes them more like macro's
188 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
189 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
190 static inline void EDIT_WM_Clear(EDITSTATE *es);
191 static inline void EDIT_WM_Cut(EDITSTATE *es);
194 * Helper functions only valid for one type of control
196 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
197 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
198 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
199 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
200 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
201 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
202 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
204 * Helper functions valid for both single line _and_ multi line controls
206 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
207 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
208 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
209 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
210 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
211 static void EDIT_LockBuffer(EDITSTATE *es);
212 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
213 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
214 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
215 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
216 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
217 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
218 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
219 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
220 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
221 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
222 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
223 static void EDIT_AdjustFormatRect(EDITSTATE *es);
224 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
225 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
226 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
227 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
229 * EM_XXX message handlers
231 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
232 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
233 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
234 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
235 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
236 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
237 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
238 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
239 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
240 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
241 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
242 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
243 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
244 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
245 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
246 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
247 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
248 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
249 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
250 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, WORD left, WORD right, BOOL repaint);
251 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
252 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
253 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
254 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
255 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
256 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
257 static BOOL EDIT_EM_Undo(EDITSTATE *es);
259 * WM_XXX message handlers
261 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
262 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
263 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
264 static void EDIT_WM_Copy(EDITSTATE *es);
265 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
266 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
267 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
268 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
269 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
270 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
271 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
272 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
273 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
274 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
275 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
276 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
277 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
278 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
279 static void EDIT_WM_Paste(EDITSTATE *es);
280 static void EDIT_WM_SetFocus(EDITSTATE *es);
281 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
282 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
283 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
284 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
285 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
286 static void EDIT_WM_Timer(EDITSTATE *es);
287 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
288 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
289 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
290 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
292 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
293 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
295 /*********************************************************************
296 * edit class descriptor
298 const struct builtin_class_descr EDIT_builtin_class =
301 CS_DBLCLKS | CS_PARENTDC, /* style */
302 EditWndProcA, /* procA */
303 EditWndProcW, /* procW */
304 sizeof(EDITSTATE *), /* extra */
305 IDC_IBEAM, /* cursor */
310 /*********************************************************************
315 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
317 return (es->undo_insert_count || strlenW(es->undo_text));
321 /*********************************************************************
326 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
328 es->undo_insert_count = 0;
329 *es->undo_text = '\0';
333 /*********************************************************************
338 static inline void EDIT_WM_Clear(EDITSTATE *es)
340 static const WCHAR empty_stringW[] = {0};
342 /* Protect read-only edit control from modification */
343 if(es->style & ES_READONLY)
346 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
350 /*********************************************************************
355 static inline void EDIT_WM_Cut(EDITSTATE *es)
362 /**********************************************************************
365 * Returns the window version in case Wine emulates a later version
366 * of windows than the application expects.
368 * In a number of cases when windows runs an application that was
369 * designed for an earlier windows version, windows reverts
370 * to "old" behaviour of that earlier version.
372 * An example is a disabled edit control that needs to be painted.
373 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
374 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
375 * applications with an expected version 0f 4.0 or higher.
378 static DWORD get_app_version(void)
380 static DWORD version;
383 DWORD dwEmulatedVersion;
385 DWORD dwProcVersion = GetProcessVersion(0);
387 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
388 GetVersionExW( &info );
389 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
390 /* FIXME: this may not be 100% correct; see discussion on the
391 * wine developer list in Nov 1999 */
392 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
397 static inline UINT get_text_length(EDITSTATE *es)
399 return strlenW(es->text);
402 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
407 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
408 msg = WM_CTLCOLORSTATIC;
410 msg = WM_CTLCOLOREDIT;
412 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
413 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
415 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
419 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
422 return DefWindowProcW(hwnd, msg, wParam, lParam);
424 return DefWindowProcA(hwnd, msg, wParam, lParam);
427 /*********************************************************************
431 * The messages are in the order of the actual integer values
432 * (which can be found in include/windows.h)
433 * Wherever possible the 16 bit versions are converted to
434 * the 32 bit ones, so that we can 'fall through' to the
435 * helper functions. These are mostly 32 bit (with a few
436 * exceptions, clearly indicated by a '16' extension to their
440 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
441 WPARAM wParam, LPARAM lParam, BOOL unicode )
443 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
446 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
448 if (!es && msg != WM_NCCREATE)
449 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
451 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
459 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
463 if ((short)LOWORD(lParam) == -1)
464 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
466 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
468 EDIT_EM_ScrollCaret(es);
472 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
473 EDIT_EM_ScrollCaret(es);
480 RECT16 *r16 = MapSL(lParam);
481 r16->left = es->format_rect.left;
482 r16->top = es->format_rect.top;
483 r16->right = es->format_rect.right;
484 r16->bottom = es->format_rect.bottom;
489 CopyRect((LPRECT)lParam, &es->format_rect);
493 if ((es->style & ES_MULTILINE) && lParam) {
495 RECT16 *r16 = MapSL(lParam);
498 rc.right = r16->right;
499 rc.bottom = r16->bottom;
500 EDIT_SetRectNP(es, &rc);
501 EDIT_UpdateText(es, NULL, TRUE);
505 if ((es->style & ES_MULTILINE) && lParam) {
506 EDIT_SetRectNP(es, (LPRECT)lParam);
507 EDIT_UpdateText(es, NULL, TRUE);
512 if ((es->style & ES_MULTILINE) && lParam) {
514 RECT16 *r16 = MapSL(lParam);
517 rc.right = r16->right;
518 rc.bottom = r16->bottom;
519 EDIT_SetRectNP(es, &rc);
523 if ((es->style & ES_MULTILINE) && lParam)
524 EDIT_SetRectNP(es, (LPRECT)lParam);
529 result = EDIT_EM_Scroll(es, (INT)wParam);
532 case EM_LINESCROLL16:
533 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
534 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
537 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
540 case EM_SCROLLCARET16:
542 EDIT_EM_ScrollCaret(es);
548 result = ((es->flags & EF_MODIFIED) != 0);
554 es->flags |= EF_MODIFIED;
556 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
559 case EM_GETLINECOUNT16:
560 case EM_GETLINECOUNT:
561 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
565 if ((INT16)wParam == -1)
569 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
573 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
576 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
580 result = (LRESULT)EDIT_EM_GetHandle16(es);
583 result = (LRESULT)EDIT_EM_GetHandle(es);
588 result = EDIT_EM_GetThumb(es);
591 /* these messages missing from specs */
600 FIXME("undocumented message 0x%x, please report\n", msg);
601 result = DefWindowProcW(hwnd, msg, wParam, lParam);
604 case EM_LINELENGTH16:
606 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
609 case EM_REPLACESEL16:
610 lParam = (LPARAM)MapSL(lParam);
611 unicode = FALSE; /* 16-bit message is always ascii */
618 textW = (LPWSTR)lParam;
621 LPSTR textA = (LPSTR)lParam;
622 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
623 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
624 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
627 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
631 HeapFree(GetProcessHeap(), 0, textW);
636 lParam = (LPARAM)MapSL(lParam);
637 unicode = FALSE; /* 16-bit message is always ascii */
640 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
644 case EM_SETLIMITTEXT:
645 EDIT_EM_SetLimitText(es, (INT)wParam);
650 result = (LRESULT)EDIT_EM_CanUndo(es);
656 result = (LRESULT)EDIT_EM_Undo(es);
661 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
664 case EM_LINEFROMCHAR16:
665 case EM_LINEFROMCHAR:
666 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
669 case EM_SETTABSTOPS16:
670 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
673 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
676 case EM_SETPASSWORDCHAR16:
677 unicode = FALSE; /* 16-bit message is always ascii */
679 case EM_SETPASSWORDCHAR:
684 charW = (WCHAR)wParam;
688 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
691 EDIT_EM_SetPasswordChar(es, charW);
695 case EM_EMPTYUNDOBUFFER16:
696 case EM_EMPTYUNDOBUFFER:
697 EDIT_EM_EmptyUndoBuffer(es);
700 case EM_GETFIRSTVISIBLELINE16:
701 result = es->y_offset;
703 case EM_GETFIRSTVISIBLELINE:
704 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
707 case EM_SETREADONLY16:
710 SetWindowLongW( hwnd, GWL_STYLE,
711 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
712 es->style |= ES_READONLY;
714 SetWindowLongW( hwnd, GWL_STYLE,
715 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
716 es->style &= ~ES_READONLY;
721 case EM_SETWORDBREAKPROC16:
722 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
724 case EM_SETWORDBREAKPROC:
725 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
728 case EM_GETWORDBREAKPROC16:
729 result = (LRESULT)es->word_break_proc16;
731 case EM_GETWORDBREAKPROC:
732 result = (LRESULT)es->word_break_proc;
735 case EM_GETPASSWORDCHAR16:
736 unicode = FALSE; /* 16-bit message is always ascii */
738 case EM_GETPASSWORDCHAR:
741 result = es->password_char;
744 WCHAR charW = es->password_char;
746 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
752 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
755 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
759 result = MAKELONG(es->left_margin, es->right_margin);
762 case EM_GETLIMITTEXT:
763 result = es->buffer_limit;
767 if ((INT)wParam >= get_text_length(es)) result = -1;
768 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
772 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
775 /* End of the EM_ messages which were in numerical order; what order
776 * are these in? vaguely alphabetical?
780 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
784 result = EDIT_WM_Destroy(es);
789 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
791 if (es->style & ES_MULTILINE)
793 result |= DLGC_WANTALLKEYS;
797 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
799 int vk = (int)((LPMSG)lParam)->wParam;
801 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
803 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
804 result |= DLGC_WANTMESSAGE;
815 strng[0] = wParam >> 8;
816 strng[1] = wParam & 0xff;
817 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
818 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
819 EDIT_WM_Char(es, charW);
832 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
835 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
837 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
838 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
841 EDIT_WM_Char(es, charW);
850 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
854 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
863 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
866 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
870 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
871 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
872 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
874 result = EDIT_WM_Create(es, nameW);
875 HeapFree(GetProcessHeap(), 0, nameW);
884 es->bEnableState = (BOOL) wParam;
885 EDIT_UpdateText(es, NULL, TRUE);
889 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
893 result = (LRESULT)es->font;
897 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
900 case WM_GETTEXTLENGTH:
901 if (unicode) result = get_text_length(es);
902 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
903 NULL, 0, NULL, NULL );
907 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
911 result = EDIT_WM_KeyDown(es, (INT)wParam);
915 result = EDIT_WM_KillFocus(es);
918 case WM_LBUTTONDBLCLK:
919 result = EDIT_WM_LButtonDblClk(es);
923 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
927 result = EDIT_WM_LButtonUp(es);
931 result = EDIT_WM_MButtonDown(es);
935 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
940 EDIT_WM_Paint(es, (HDC)wParam);
948 EDIT_WM_SetFocus(es);
952 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
956 /* FIXME: actually set an internal flag and behave accordingly */
960 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
965 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
968 case WM_STYLECHANGED:
969 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
972 case WM_STYLECHANGING:
973 result = 0; /* See EDIT_WM_StyleChanged */
977 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
985 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
990 int gcWheelDelta = 0;
991 UINT pulScrollLines = 3;
992 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
994 if (wParam & (MK_SHIFT | MK_CONTROL)) {
995 result = DefWindowProcW(hwnd, msg, wParam, lParam);
998 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
999 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1001 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1002 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1003 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1009 /* IME messages to make the edit control IME aware */
1010 case WM_IME_SETCONTEXT:
1013 case WM_IME_STARTCOMPOSITION:
1015 * FIXME in IME: This message is not always sent like it should be
1017 if (es->selection_start != es->selection_end)
1019 static const WCHAR empty_stringW[] = {0};
1020 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1022 es->composition_start = es->selection_end;
1023 es->composition_len = 0;
1026 case WM_IME_COMPOSITION:
1027 if (es->composition_len == 0)
1029 if (es->selection_start != es->selection_end)
1031 static const WCHAR empty_stringW[] = {0};
1032 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1035 es->composition_start = es->selection_end;
1037 EDIT_ImeComposition(hwnd,lParam,es);
1040 case WM_IME_ENDCOMPOSITION:
1041 es->composition_len= 0;
1044 case WM_IME_COMPOSITIONFULL:
1050 case WM_IME_CONTROL:
1054 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1058 if (es) EDIT_UnlockBuffer(es, FALSE);
1060 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1065 /*********************************************************************
1067 * EditWndProcW (USER32.@)
1069 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1071 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1074 /*********************************************************************
1076 * EditWndProc (USER32.@)
1078 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1080 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1083 /*********************************************************************
1085 * EDIT_BuildLineDefs_ML
1087 * Build linked list of text lines.
1088 * Lines can end with '\0' (last line), a character (if it is wrapped),
1089 * a soft return '\r\r\n' or a hard return '\r\n'
1092 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1096 LPWSTR current_position, cp;
1098 LINEDEF *current_line;
1099 LINEDEF *previous_line;
1100 LINEDEF *start_line;
1101 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1102 INT line_count = es->line_count;
1103 INT orig_net_length;
1106 if (istart == iend && delta == 0)
1109 dc = GetDC(es->hwndSelf);
1111 old_font = SelectObject(dc, es->font);
1113 previous_line = NULL;
1114 current_line = es->first_line_def;
1116 /* Find starting line. istart must lie inside an existing line or
1117 * at the end of buffer */
1119 if (istart < current_line->index + current_line->length ||
1120 current_line->ending == END_0)
1123 previous_line = current_line;
1124 current_line = current_line->next;
1126 } while (current_line);
1128 if (!current_line) /* Error occurred start is not inside previous buffer */
1130 FIXME(" modification occurred outside buffer\n");
1131 ReleaseDC(es->hwndSelf, dc);
1135 /* Remember start of modifications in order to calculate update region */
1136 nstart_line = line_index;
1137 nstart_index = current_line->index;
1139 /* We must start to reformat from the previous line since the modifications
1140 * may have caused the line to wrap upwards. */
1141 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1144 current_line = previous_line;
1146 start_line = current_line;
1148 fw = es->format_rect.right - es->format_rect.left;
1149 current_position = es->text + current_line->index;
1151 if (current_line != start_line)
1153 if (!current_line || current_line->index + delta > current_position - es->text)
1155 /* The buffer has been expanded, create a new line and
1156 insert it into the link list */
1157 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1158 new_line->next = previous_line->next;
1159 previous_line->next = new_line;
1160 current_line = new_line;
1163 else if (current_line->index + delta < current_position - es->text)
1165 /* The previous line merged with this line so we delete this extra entry */
1166 previous_line->next = current_line->next;
1167 HeapFree(GetProcessHeap(), 0, current_line);
1168 current_line = previous_line->next;
1172 else /* current_line->index + delta == current_position */
1174 if (current_position - es->text > iend)
1175 break; /* We reached end of line modifications */
1176 /* else recalulate this line */
1180 current_line->index = current_position - es->text;
1181 orig_net_length = current_line->net_length;
1183 /* Find end of line */
1184 cp = current_position;
1186 if (*cp == '\n') break;
1187 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1192 /* Mark type of line termination */
1194 current_line->ending = END_0;
1195 current_line->net_length = strlenW(current_position);
1196 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1197 current_line->ending = END_SOFT;
1198 current_line->net_length = cp - current_position - 1;
1199 } else if (*cp == '\n') {
1200 current_line->ending = END_RICH;
1201 current_line->net_length = cp - current_position;
1203 current_line->ending = END_HARD;
1204 current_line->net_length = cp - current_position;
1207 /* Calculate line width */
1208 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1209 current_position, current_line->net_length,
1210 es->tabs_count, es->tabs));
1212 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1213 if (!(es->style & ES_AUTOHSCROLL)) {
1214 if (current_line->width > fw) {
1219 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1220 prev + 1, current_line->net_length, WB_RIGHT);
1221 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1222 current_position, next, es->tabs_count, es->tabs));
1223 } while (current_line->width <= fw);
1224 if (!prev) { /* Didn't find a line break so force a break */
1229 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1230 current_position, next, es->tabs_count, es->tabs));
1231 } while (current_line->width <= fw);
1236 /* If the first line we are calculating, wrapped before istart, we must
1237 * adjust istart in order for this to be reflected in the update region. */
1238 if (current_line->index == nstart_index && istart > current_line->index + prev)
1239 istart = current_line->index + prev;
1240 /* else if we are updating the previous line before the first line we
1241 * are re-calculating and it expanded */
1242 else if (current_line == start_line &&
1243 current_line->index != nstart_index && orig_net_length < prev)
1245 /* Line expanded due to an upwards line wrap so we must partially include
1246 * previous line in update region */
1247 nstart_line = line_index;
1248 nstart_index = current_line->index;
1249 istart = current_line->index + orig_net_length;
1252 current_line->net_length = prev;
1253 current_line->ending = END_WRAP;
1254 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1255 current_line->net_length, es->tabs_count, es->tabs));
1257 else if (orig_net_length < current_line->net_length &&
1258 current_line == start_line &&
1259 current_line->index != nstart_index) {
1260 /* The previous line expanded but it's still not as wide as the client rect */
1261 /* The expansion is due to an upwards line wrap so we must partially include
1262 it in the update region */
1263 nstart_line = line_index;
1264 nstart_index = current_line->index;
1265 istart = current_line->index + orig_net_length;
1270 /* Adjust length to include line termination */
1271 switch (current_line->ending) {
1273 current_line->length = current_line->net_length + 3;
1276 current_line->length = current_line->net_length + 1;
1279 current_line->length = current_line->net_length + 2;
1283 current_line->length = current_line->net_length;
1286 es->text_width = max(es->text_width, current_line->width);
1287 current_position += current_line->length;
1288 previous_line = current_line;
1289 current_line = current_line->next;
1291 } while (previous_line->ending != END_0);
1293 /* Finish adjusting line indexes by delta or remove hanging lines */
1294 if (previous_line->ending == END_0)
1296 LINEDEF *pnext = NULL;
1298 previous_line->next = NULL;
1299 while (current_line)
1301 pnext = current_line->next;
1302 HeapFree(GetProcessHeap(), 0, current_line);
1303 current_line = pnext;
1307 else if (delta != 0)
1309 while (current_line)
1311 current_line->index += delta;
1312 current_line = current_line->next;
1316 /* Calculate rest of modification rectangle */
1321 * We calculate two rectangles. One for the first line which may have
1322 * an indent with respect to the format rect. The other is a format-width
1323 * rectangle that spans the rest of the lines that changed or moved.
1325 rc.top = es->format_rect.top + nstart_line * es->line_height -
1326 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1327 rc.bottom = rc.top + es->line_height;
1328 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1329 rc.left = es->format_rect.left;
1331 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1332 es->text + nstart_index, istart - nstart_index,
1333 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1334 rc.right = es->format_rect.right;
1335 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1338 rc.left = es->format_rect.left;
1339 rc.right = es->format_rect.right;
1341 * If lines were added or removed we must re-paint the remainder of the
1342 * lines since the remaining lines were either shifted up or down.
1344 if (line_count < es->line_count) /* We added lines */
1345 rc.bottom = es->line_count * es->line_height;
1346 else if (line_count > es->line_count) /* We removed lines */
1347 rc.bottom = line_count * es->line_height;
1349 rc.bottom = line_index * es->line_height;
1350 rc.bottom += es->format_rect.top;
1351 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1352 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1353 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1354 DeleteObject(tmphrgn);
1358 SelectObject(dc, old_font);
1360 ReleaseDC(es->hwndSelf, dc);
1363 /*********************************************************************
1365 * EDIT_CalcLineWidth_SL
1368 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1375 text = EDIT_GetPasswordPointer_SL(es);
1377 dc = GetDC(es->hwndSelf);
1379 old_font = SelectObject(dc, es->font);
1381 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1384 SelectObject(dc, old_font);
1385 ReleaseDC(es->hwndSelf, dc);
1387 if (es->style & ES_PASSWORD)
1388 HeapFree(GetProcessHeap(), 0, text);
1390 es->text_width = size.cx;
1393 /*********************************************************************
1395 * EDIT_CallWordBreakProc
1397 * Call appropriate WordBreakProc (internal or external).
1399 * Note: The "start" argument should always be an index referring
1400 * to es->text. The actual wordbreak proc might be
1401 * 16 bit, so we can't always pass any 32 bit LPSTR.
1402 * Hence we assume that es->text is the buffer that holds
1403 * the string under examination (we can decide this for ourselves).
1406 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1410 if (es->word_break_proc16) {
1417 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1418 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1419 segptr = WOWGlobalLock16(hglob16);
1420 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1421 args[4] = SELECTOROF(segptr);
1422 args[3] = OFFSETOF(segptr);
1426 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1427 ret = LOWORD(result);
1428 GlobalUnlock16(hglob16);
1429 GlobalFree16(hglob16);
1431 else if (es->word_break_proc)
1435 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1437 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1438 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1439 ret = wbpW(es->text + start, index, count, action);
1443 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1447 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1448 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1449 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1450 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1451 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1452 ret = wbpA(textA, index, countA, action);
1453 HeapFree(GetProcessHeap(), 0, textA);
1457 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1463 /*********************************************************************
1467 * Beware: This is not the function called on EM_CHARFROMPOS
1468 * The position _can_ be outside the formatting / client
1470 * The return value is only the character index
1473 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1478 INT x_high = 0, x_low = 0;
1480 if (es->style & ES_MULTILINE) {
1481 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1483 LINEDEF *line_def = es->first_line_def;
1485 while ((line > 0) && line_def->next) {
1486 line_index += line_def->length;
1487 line_def = line_def->next;
1490 x += es->x_offset - es->format_rect.left;
1491 if (es->style & ES_RIGHT)
1492 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1493 else if (es->style & ES_CENTER)
1494 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1495 if (x >= line_def->width) {
1497 *after_wrap = (line_def->ending == END_WRAP);
1498 return line_index + line_def->net_length;
1502 *after_wrap = FALSE;
1505 dc = GetDC(es->hwndSelf);
1507 old_font = SelectObject(dc, es->font);
1509 high = line_index + line_def->net_length + 1;
1510 while (low < high - 1)
1512 INT mid = (low + high) / 2;
1513 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1522 if (abs(x_high - x) + 1 <= abs(x_low - x))
1528 *after_wrap = ((index == line_index + line_def->net_length) &&
1529 (line_def->ending == END_WRAP));
1534 *after_wrap = FALSE;
1535 x -= es->format_rect.left;
1537 return es->x_offset;
1541 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1542 if (es->style & ES_RIGHT)
1544 else if (es->style & ES_CENTER)
1548 text = EDIT_GetPasswordPointer_SL(es);
1549 dc = GetDC(es->hwndSelf);
1551 old_font = SelectObject(dc, es->font);
1555 INT high = es->x_offset;
1556 while (low < high - 1)
1558 INT mid = (low + high) / 2;
1559 GetTextExtentPoint32W( dc, text + mid,
1560 es->x_offset - mid, &size );
1569 if (abs(x_high + x) <= abs(x_low + x) + 1)
1576 INT low = es->x_offset;
1577 INT high = get_text_length(es) + 1;
1578 while (low < high - 1)
1580 INT mid = (low + high) / 2;
1581 GetTextExtentPoint32W( dc, text + es->x_offset,
1582 mid - es->x_offset, &size );
1591 if (abs(x_high - x) <= abs(x_low - x) + 1)
1596 if (es->style & ES_PASSWORD)
1597 HeapFree(GetProcessHeap(), 0, text);
1600 SelectObject(dc, old_font);
1601 ReleaseDC(es->hwndSelf, dc);
1606 /*********************************************************************
1610 * adjusts the point to be within the formatting rectangle
1611 * (so CharFromPos returns the nearest _visible_ character)
1614 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1616 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1617 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1621 /*********************************************************************
1625 * Calculates the bounding rectangle for a line from a starting
1626 * column to an ending column.
1629 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1631 INT line_index = EDIT_EM_LineIndex(es, line);
1633 if (es->style & ES_MULTILINE)
1634 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1636 rc->top = es->format_rect.top;
1637 rc->bottom = rc->top + es->line_height;
1638 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1639 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1643 /*********************************************************************
1645 * EDIT_GetPasswordPointer_SL
1647 * note: caller should free the (optionally) allocated buffer
1650 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1652 if (es->style & ES_PASSWORD) {
1653 INT len = get_text_length(es);
1654 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1656 while(len) text[--len] = es->password_char;
1663 /*********************************************************************
1667 * This acts as a LocalLock16(), but it locks only once. This way
1668 * you can call it whenever you like, without unlocking.
1670 * Initially the edit control allocates a HLOCAL32 buffer
1671 * (32 bit linear memory handler). However, 16 bit application
1672 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1673 * handler). From that moment on we have to keep using this 16 bit memory
1674 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1675 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1679 static void EDIT_LockBuffer(EDITSTATE *es)
1681 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1682 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1687 BOOL _16bit = FALSE;
1693 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1694 textA = LocalLock(es->hloc32A);
1695 countA = strlen(textA) + 1;
1699 HANDLE16 oldDS = stack16->ds;
1700 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1701 stack16->ds = hInstance;
1702 textA = MapSL(LocalLock16(es->hloc16));
1703 stack16->ds = oldDS;
1704 countA = strlen(textA) + 1;
1709 ERR("no buffer ... please report\n");
1716 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1717 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1718 if(countW_new > es->buffer_size + 1)
1720 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1721 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1722 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1725 es->hloc32W = hloc32W_new;
1726 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1727 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1730 WARN("FAILED! Will synchronize partially\n");
1734 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1735 es->text = LocalLock(es->hloc32W);
1739 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1742 HANDLE16 oldDS = stack16->ds;
1743 stack16->ds = hInstance;
1744 LocalUnlock16(es->hloc16);
1745 stack16->ds = oldDS;
1748 LocalUnlock(es->hloc32A);
1755 /*********************************************************************
1757 * EDIT_SL_InvalidateText
1759 * Called from EDIT_InvalidateText().
1760 * Does the job for single-line controls only.
1763 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1768 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1769 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1770 EDIT_UpdateText(es, &rc, TRUE);
1774 /*********************************************************************
1776 * EDIT_ML_InvalidateText
1778 * Called from EDIT_InvalidateText().
1779 * Does the job for multi-line controls only.
1782 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1784 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1785 INT sl = EDIT_EM_LineFromChar(es, start);
1786 INT el = EDIT_EM_LineFromChar(es, end);
1795 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1798 sc = start - EDIT_EM_LineIndex(es, sl);
1799 ec = end - EDIT_EM_LineIndex(es, el);
1800 if (sl < es->y_offset) {
1804 if (el > es->y_offset + vlc) {
1805 el = es->y_offset + vlc;
1806 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1808 GetClientRect(es->hwndSelf, &rc1);
1809 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1811 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1812 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1813 EDIT_UpdateText(es, &rcUpdate, TRUE);
1815 EDIT_GetLineRect(es, sl, sc,
1816 EDIT_EM_LineLength(es,
1817 EDIT_EM_LineIndex(es, sl)),
1819 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1820 EDIT_UpdateText(es, &rcUpdate, TRUE);
1821 for (l = sl + 1 ; l < el ; l++) {
1822 EDIT_GetLineRect(es, l, 0,
1823 EDIT_EM_LineLength(es,
1824 EDIT_EM_LineIndex(es, l)),
1826 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1827 EDIT_UpdateText(es, &rcUpdate, TRUE);
1829 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1830 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1831 EDIT_UpdateText(es, &rcUpdate, TRUE);
1836 /*********************************************************************
1838 * EDIT_InvalidateText
1840 * Invalidate the text from offset start up to, but not including,
1841 * offset end. Useful for (re)painting the selection.
1842 * Regions outside the linewidth are not invalidated.
1843 * end == -1 means end == TextLength.
1844 * start and end need not be ordered.
1847 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1853 end = get_text_length(es);
1861 if (es->style & ES_MULTILINE)
1862 EDIT_ML_InvalidateText(es, start, end);
1864 EDIT_SL_InvalidateText(es, start, end);
1868 /*********************************************************************
1872 * Try to fit size + 1 characters in the buffer.
1874 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1878 if (size <= es->buffer_size)
1881 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1883 /* Force edit to unlock it's buffer. es->text now NULL */
1884 EDIT_UnlockBuffer(es, TRUE);
1887 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1888 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1889 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1890 es->hloc32W = hNew32W;
1891 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1895 EDIT_LockBuffer(es);
1897 if (es->buffer_size < size) {
1898 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1899 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1902 TRACE("We now have %d+1\n", es->buffer_size);
1908 /*********************************************************************
1912 * Try to fit size + 1 bytes in the undo buffer.
1915 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1919 if (size <= es->undo_buffer_size)
1922 TRACE("trying to ReAlloc to %d+1\n", size);
1924 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1925 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1926 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1931 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1937 /*********************************************************************
1942 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1944 INT e = es->selection_end;
1948 if ((es->style & ES_MULTILINE) && e &&
1949 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1951 if (e && (es->text[e - 1] == '\r'))
1955 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1956 EDIT_EM_ScrollCaret(es);
1960 /*********************************************************************
1964 * Only for multi line controls
1965 * Move the caret one line down, on a column with the nearest
1966 * x coordinate on the screen (might be a different column).
1969 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1971 INT s = es->selection_start;
1972 INT e = es->selection_end;
1973 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1974 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1975 INT x = (short)LOWORD(pos);
1976 INT y = (short)HIWORD(pos);
1978 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1981 EDIT_EM_SetSel(es, s, e, after_wrap);
1982 EDIT_EM_ScrollCaret(es);
1986 /*********************************************************************
1991 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1993 BOOL after_wrap = FALSE;
1996 /* Pass a high value in x to make sure of receiving the end of the line */
1997 if (es->style & ES_MULTILINE)
1998 e = EDIT_CharFromPos(es, 0x3fffffff,
1999 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2001 e = get_text_length(es);
2002 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2003 EDIT_EM_ScrollCaret(es);
2007 /*********************************************************************
2012 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2014 INT e = es->selection_end;
2018 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2019 if (es->text[e] == '\n')
2021 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2025 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2026 EDIT_EM_ScrollCaret(es);
2030 /*********************************************************************
2034 * Home key: move to beginning of line.
2037 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2041 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2042 if (es->style & ES_MULTILINE)
2043 e = EDIT_CharFromPos(es, -es->x_offset,
2044 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2047 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2048 EDIT_EM_ScrollCaret(es);
2052 /*********************************************************************
2054 * EDIT_MovePageDown_ML
2056 * Only for multi line controls
2057 * Move the caret one page down, on a column with the nearest
2058 * x coordinate on the screen (might be a different column).
2061 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2063 INT s = es->selection_start;
2064 INT e = es->selection_end;
2065 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2066 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2067 INT x = (short)LOWORD(pos);
2068 INT y = (short)HIWORD(pos);
2070 e = EDIT_CharFromPos(es, x,
2071 y + (es->format_rect.bottom - es->format_rect.top),
2075 EDIT_EM_SetSel(es, s, e, after_wrap);
2076 EDIT_EM_ScrollCaret(es);
2080 /*********************************************************************
2082 * EDIT_MovePageUp_ML
2084 * Only for multi line controls
2085 * Move the caret one page up, on a column with the nearest
2086 * x coordinate on the screen (might be a different column).
2089 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2091 INT s = es->selection_start;
2092 INT e = es->selection_end;
2093 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2094 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2095 INT x = (short)LOWORD(pos);
2096 INT y = (short)HIWORD(pos);
2098 e = EDIT_CharFromPos(es, x,
2099 y - (es->format_rect.bottom - es->format_rect.top),
2103 EDIT_EM_SetSel(es, s, e, after_wrap);
2104 EDIT_EM_ScrollCaret(es);
2108 /*********************************************************************
2112 * Only for multi line controls
2113 * Move the caret one line up, on a column with the nearest
2114 * x coordinate on the screen (might be a different column).
2117 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2119 INT s = es->selection_start;
2120 INT e = es->selection_end;
2121 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2122 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2123 INT x = (short)LOWORD(pos);
2124 INT y = (short)HIWORD(pos);
2126 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2129 EDIT_EM_SetSel(es, s, e, after_wrap);
2130 EDIT_EM_ScrollCaret(es);
2134 /*********************************************************************
2136 * EDIT_MoveWordBackward
2139 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2141 INT s = es->selection_start;
2142 INT e = es->selection_end;
2147 l = EDIT_EM_LineFromChar(es, e);
2148 ll = EDIT_EM_LineLength(es, e);
2149 li = EDIT_EM_LineIndex(es, l);
2152 li = EDIT_EM_LineIndex(es, l - 1);
2153 e = li + EDIT_EM_LineLength(es, li);
2156 e = li + (INT)EDIT_CallWordBreakProc(es,
2157 li, e - li, ll, WB_LEFT);
2161 EDIT_EM_SetSel(es, s, e, FALSE);
2162 EDIT_EM_ScrollCaret(es);
2166 /*********************************************************************
2168 * EDIT_MoveWordForward
2171 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2173 INT s = es->selection_start;
2174 INT e = es->selection_end;
2179 l = EDIT_EM_LineFromChar(es, e);
2180 ll = EDIT_EM_LineLength(es, e);
2181 li = EDIT_EM_LineIndex(es, l);
2183 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2184 e = EDIT_EM_LineIndex(es, l + 1);
2186 e = li + EDIT_CallWordBreakProc(es,
2187 li, e - li + 1, ll, WB_RIGHT);
2191 EDIT_EM_SetSel(es, s, e, FALSE);
2192 EDIT_EM_ScrollCaret(es);
2196 /*********************************************************************
2201 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2203 INT s = es->selection_start;
2204 INT e = es->selection_end;
2211 if (es->style & ES_MULTILINE) {
2212 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2213 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2218 TRACE("line=%d\n", line);
2220 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2221 x = (short)LOWORD(pos);
2222 y = (short)HIWORD(pos);
2223 li = EDIT_EM_LineIndex(es, line);
2224 ll = EDIT_EM_LineLength(es, li);
2225 s = min(es->selection_start, es->selection_end);
2226 e = max(es->selection_start, es->selection_end);
2227 s = min(li + ll, max(li, s));
2228 e = min(li + ll, max(li, e));
2229 if (rev && (s != e) &&
2230 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2231 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2232 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2233 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2235 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2239 /*********************************************************************
2244 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2248 LOGFONTW underline_font;
2249 HFONT hUnderline = 0;
2258 BkMode = GetBkMode(dc);
2259 BkColor = GetBkColor(dc);
2260 TextColor = GetTextColor(dc);
2262 if (es->composition_len == 0)
2264 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2265 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2266 SetBkMode( dc, OPAQUE);
2270 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2271 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2272 underline_font.lfUnderline = TRUE;
2273 hUnderline = CreateFontIndirectW(&underline_font);
2274 old_font = SelectObject(dc,hUnderline);
2277 li = EDIT_EM_LineIndex(es, line);
2278 if (es->style & ES_MULTILINE) {
2279 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2280 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2282 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2283 TextOutW(dc, x, y, text + li + col, count);
2284 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2286 if (es->style & ES_PASSWORD)
2287 HeapFree(GetProcessHeap(), 0, text);
2290 if (es->composition_len == 0)
2292 SetBkColor(dc, BkColor);
2293 SetTextColor(dc, TextColor);
2294 SetBkMode( dc, BkMode);
2299 SelectObject(dc,old_font);
2301 DeleteObject(hUnderline);
2308 /*********************************************************************
2313 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2316 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2317 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2318 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2322 /*********************************************************************
2324 * EDIT_AdjustFormatRect
2326 * Adjusts the format rectangle for the current font and the
2327 * current client rectangle.
2330 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2334 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2335 if (es->style & ES_MULTILINE)
2337 INT fw, vlc, max_x_offset, max_y_offset;
2339 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2340 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2342 /* correct es->x_offset */
2343 fw = es->format_rect.right - es->format_rect.left;
2344 max_x_offset = es->text_width - fw;
2345 if(max_x_offset < 0) max_x_offset = 0;
2346 if(es->x_offset > max_x_offset)
2347 es->x_offset = max_x_offset;
2349 /* correct es->y_offset */
2350 max_y_offset = es->line_count - vlc;
2351 if(max_y_offset < 0) max_y_offset = 0;
2352 if(es->y_offset > max_y_offset)
2353 es->y_offset = max_y_offset;
2355 /* force scroll info update */
2356 EDIT_UpdateScrollInfo(es);
2359 /* Windows doesn't care to fix text placement for SL controls */
2360 es->format_rect.bottom = es->format_rect.top + es->line_height;
2362 /* Always stay within the client area */
2363 GetClientRect(es->hwndSelf, &ClientRect);
2364 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2366 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2367 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2369 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2373 /*********************************************************************
2377 * note: this is not (exactly) the handler called on EM_SETRECTNP
2378 * it is also used to set the rect of a single line control
2381 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2385 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2387 CopyRect(&es->format_rect, rc);
2389 if (ExStyle & WS_EX_CLIENTEDGE) {
2390 es->format_rect.left++;
2391 es->format_rect.right--;
2393 if (es->format_rect.bottom - es->format_rect.top
2394 >= es->line_height + 2)
2396 es->format_rect.top++;
2397 es->format_rect.bottom--;
2400 else if (es->style & WS_BORDER) {
2401 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2402 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2403 es->format_rect.left += bw;
2404 es->format_rect.right -= bw;
2405 if (es->format_rect.bottom - es->format_rect.top
2406 >= es->line_height + 2 * bh)
2408 es->format_rect.top += bh;
2409 es->format_rect.bottom -= bh;
2413 es->format_rect.left += es->left_margin;
2414 es->format_rect.right -= es->right_margin;
2415 EDIT_AdjustFormatRect(es);
2419 /*********************************************************************
2424 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2427 /* Edit window might be already destroyed */
2428 if(!IsWindow(es->hwndSelf))
2430 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2434 if (!es->lock_count) {
2435 ERR("lock_count == 0 ... please report\n");
2439 ERR("es->text == 0 ... please report\n");
2443 if (force || (es->lock_count == 1)) {
2447 UINT countW = get_text_length(es) + 1;
2448 STACK16FRAME* stack16 = NULL;
2453 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2454 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2455 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2456 countA = LocalSize(es->hloc32A);
2457 if(countA_new > countA)
2460 UINT alloc_size = ROUND_TO_GROW(countA_new);
2461 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2462 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2465 es->hloc32A = hloc32A_new;
2466 countA = LocalSize(hloc32A_new);
2467 TRACE("Real new size %d bytes\n", countA);
2470 WARN("FAILED! Will synchronize partially\n");
2472 textA = LocalLock(es->hloc32A);
2476 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2478 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2479 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2481 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2482 oldDS = stack16->ds;
2483 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2485 countA = LocalSize16(es->hloc16);
2486 if(countA_new > countA)
2488 HLOCAL16 hloc16_new;
2489 UINT alloc_size = ROUND_TO_GROW(countA_new);
2490 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2491 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2494 es->hloc16 = hloc16_new;
2495 countA = LocalSize16(hloc16_new);
2496 TRACE("Real new size %d bytes\n", countA);
2499 WARN("FAILED! Will synchronize partially\n");
2501 textA = MapSL(LocalLock16(es->hloc16));
2506 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2508 LocalUnlock16(es->hloc16);
2510 LocalUnlock(es->hloc32A);
2513 if (stack16) stack16->ds = oldDS;
2514 LocalUnlock(es->hloc32W);
2518 ERR("no buffer ... please report\n");
2526 /*********************************************************************
2528 * EDIT_UpdateScrollInfo
2531 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2533 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2536 si.cbSize = sizeof(SCROLLINFO);
2537 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2539 si.nMax = es->line_count - 1;
2540 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2541 si.nPos = es->y_offset;
2542 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2543 si.nMin, si.nMax, si.nPage, si.nPos);
2544 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2547 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2550 si.cbSize = sizeof(SCROLLINFO);
2551 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2553 si.nMax = es->text_width - 1;
2554 si.nPage = es->format_rect.right - es->format_rect.left;
2555 si.nPos = es->x_offset;
2556 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2557 si.nMin, si.nMax, si.nPage, si.nPos);
2558 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2562 /*********************************************************************
2564 * EDIT_WordBreakProc
2566 * Find the beginning of words.
2567 * Note: unlike the specs for a WordBreakProc, this function only
2568 * allows to be called without linebreaks between s[0] up to
2569 * s[count - 1]. Remember it is only called
2570 * internally, so we can decide this for ourselves.
2573 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2577 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2587 if (s[index] == ' ') {
2588 while (index && (s[index] == ' '))
2591 while (index && (s[index] != ' '))
2593 if (s[index] == ' ')
2597 while (index && (s[index] != ' '))
2599 if (s[index] == ' ')
2609 if (s[index] == ' ')
2610 while ((index < count) && (s[index] == ' ')) index++;
2612 while (s[index] && (s[index] != ' ') && (index < count))
2614 while ((s[index] == ' ') && (index < count)) index++;
2618 case WB_ISDELIMITER:
2619 ret = (s[index] == ' ');
2622 ERR("unknown action code, please report !\n");
2629 /*********************************************************************
2633 * returns line number (not index) in high-order word of result.
2634 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2635 * to Richedit, not to the edit control. Original documentation is valid.
2636 * FIXME: do the specs mean to return -1 if outside client area or
2637 * if outside formatting rectangle ???
2640 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2648 GetClientRect(es->hwndSelf, &rc);
2649 if (!PtInRect(&rc, pt))
2652 index = EDIT_CharFromPos(es, x, y, NULL);
2653 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2657 /*********************************************************************
2661 * Enable or disable soft breaks.
2663 * This means: insert or remove the soft linebreak character (\r\r\n).
2664 * Take care to check if the text still fits the buffer after insertion.
2665 * If not, notify with EN_ERRSPACE.
2668 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2670 es->flags &= ~EF_USE_SOFTBRK;
2672 es->flags |= EF_USE_SOFTBRK;
2673 FIXME("soft break enabled, not implemented\n");
2679 /*********************************************************************
2683 * Hopefully this won't fire back at us.
2684 * We always start with a fixed buffer in the local heap.
2685 * Despite of the documentation says that the local heap is used
2686 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2687 * buffer on the local heap.
2690 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2694 if (!(es->style & ES_MULTILINE))
2698 hLocal = es->hloc32W;
2704 UINT countA, alloc_size;
2705 TRACE("Allocating 32-bit ANSI alias buffer\n");
2706 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2707 alloc_size = ROUND_TO_GROW(countA);
2708 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2710 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2713 textA = LocalLock(es->hloc32A);
2714 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2715 LocalUnlock(es->hloc32A);
2717 hLocal = es->hloc32A;
2720 es->flags |= EF_APP_HAS_HANDLE;
2721 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2726 /*********************************************************************
2730 * Hopefully this won't fire back at us.
2731 * We always start with a buffer in 32 bit linear memory.
2732 * However, with this message a 16 bit application requests
2733 * a handle of 16 bit local heap memory, where it expects to find
2735 * It's a pitty that from this moment on we have to use this
2736 * local heap, because applications may rely on the handle
2739 * In this function we'll try to switch to local heap.
2741 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2744 UINT countA, alloc_size;
2745 STACK16FRAME* stack16;
2748 if (!(es->style & ES_MULTILINE))
2754 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2755 oldDS = stack16->ds;
2756 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2758 if (!LocalHeapSize16()) {
2760 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2761 ERR("could not initialize local heap\n");
2764 TRACE("local heap initialized\n");
2767 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2768 alloc_size = ROUND_TO_GROW(countA);
2770 TRACE("Allocating 16-bit ANSI alias buffer\n");
2771 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2772 ERR("could not allocate new 16 bit buffer\n");
2776 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2777 ERR("could not lock new 16 bit buffer\n");
2778 LocalFree16(es->hloc16);
2783 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2784 LocalUnlock16(es->hloc16);
2785 es->flags |= EF_APP_HAS_HANDLE;
2787 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2790 stack16->ds = oldDS;
2795 /*********************************************************************
2800 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2803 INT line_len, dst_len;
2806 if (es->style & ES_MULTILINE) {
2807 if (line >= es->line_count)
2811 i = EDIT_EM_LineIndex(es, line);
2813 line_len = EDIT_EM_LineLength(es, i);
2814 dst_len = *(WORD *)dst;
2817 if(dst_len <= line_len)
2819 memcpy(dst, src, dst_len * sizeof(WCHAR));
2822 else /* Append 0 if enough space */
2824 memcpy(dst, src, line_len * sizeof(WCHAR));
2831 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2832 if(!ret && line_len) /* Insufficient buffer size */
2834 if(ret < dst_len) /* Append 0 if enough space */
2835 ((LPSTR)dst)[ret] = 0;
2841 /*********************************************************************
2846 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2848 UINT s = es->selection_start;
2849 UINT e = es->selection_end;
2856 return MAKELONG(s, e);
2860 /*********************************************************************
2864 * FIXME: is this right ? (or should it be only VSCROLL)
2865 * (and maybe only for edit controls that really have their
2866 * own scrollbars) (and maybe only for multiline controls ?)
2867 * All in all: very poorly documented
2870 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2872 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2873 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2877 /*********************************************************************
2882 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2887 if (!(es->style & ES_MULTILINE))
2889 if (index > (INT)get_text_length(es))
2890 return es->line_count - 1;
2892 index = min(es->selection_start, es->selection_end);
2895 line_def = es->first_line_def;
2896 index -= line_def->length;
2897 while ((index >= 0) && line_def->next) {
2899 line_def = line_def->next;
2900 index -= line_def->length;
2906 /*********************************************************************
2911 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2916 if (!(es->style & ES_MULTILINE))
2918 if (line >= es->line_count)
2922 line_def = es->first_line_def;
2924 INT index = es->selection_end - line_def->length;
2925 while ((index >= 0) && line_def->next) {
2926 line_index += line_def->length;
2927 line_def = line_def->next;
2928 index -= line_def->length;
2932 line_index += line_def->length;
2933 line_def = line_def->next;
2941 /*********************************************************************
2946 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2950 if (!(es->style & ES_MULTILINE))
2951 return get_text_length(es);
2954 /* get the number of remaining non-selected chars of selected lines */
2955 INT32 l; /* line number */
2956 INT32 li; /* index of first char in line */
2958 l = EDIT_EM_LineFromChar(es, es->selection_start);
2959 /* # chars before start of selection area */
2960 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2961 l = EDIT_EM_LineFromChar(es, es->selection_end);
2962 /* # chars after end of selection */
2963 li = EDIT_EM_LineIndex(es, l);
2964 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2967 line_def = es->first_line_def;
2968 index -= line_def->length;
2969 while ((index >= 0) && line_def->next) {
2970 line_def = line_def->next;
2971 index -= line_def->length;
2973 return line_def->net_length;
2977 /*********************************************************************
2981 * NOTE: dx is in average character widths, dy - in lines;
2984 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2986 if (!(es->style & ES_MULTILINE))
2989 dx *= es->char_width;
2990 return EDIT_EM_LineScroll_internal(es, dx, dy);
2993 /*********************************************************************
2995 * EDIT_EM_LineScroll_internal
2997 * Version of EDIT_EM_LineScroll for internal use.
2998 * It doesn't refuse if ES_MULTILINE is set and assumes that
2999 * dx is in pixels, dy - in lines.
3002 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3005 INT x_offset_in_pixels;
3006 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3009 if (es->style & ES_MULTILINE)
3011 x_offset_in_pixels = es->x_offset;
3016 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3019 if (-dx > x_offset_in_pixels)
3020 dx = -x_offset_in_pixels;
3021 if (dx > es->text_width - x_offset_in_pixels)
3022 dx = es->text_width - x_offset_in_pixels;
3023 nyoff = max(0, es->y_offset + dy);
3024 if (nyoff >= es->line_count - lines_per_page)
3025 nyoff = max(0, es->line_count - lines_per_page);
3026 dy = (es->y_offset - nyoff) * es->line_height;
3031 es->y_offset = nyoff;
3032 if(es->style & ES_MULTILINE)
3035 es->x_offset += dx / es->char_width;
3037 GetClientRect(es->hwndSelf, &rc1);
3038 IntersectRect(&rc, &rc1, &es->format_rect);
3039 ScrollWindowEx(es->hwndSelf, -dx, dy,
3040 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3041 /* force scroll info update */
3042 EDIT_UpdateScrollInfo(es);
3044 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3045 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3046 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3047 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3052 /*********************************************************************
3057 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3059 INT len = get_text_length(es);
3072 index = min(index, len);
3073 dc = GetDC(es->hwndSelf);
3075 old_font = SelectObject(dc, es->font);
3076 if (es->style & ES_MULTILINE) {
3077 l = EDIT_EM_LineFromChar(es, index);
3078 y = (l - es->y_offset) * es->line_height;
3079 li = EDIT_EM_LineIndex(es, l);
3080 if (after_wrap && (li == index) && l) {
3082 line_def = es->first_line_def;
3084 line_def = line_def->next;
3087 if (line_def->ending == END_WRAP) {
3089 y -= es->line_height;
3090 li = EDIT_EM_LineIndex(es, l);
3094 line_def = es->first_line_def;
3095 while (line_def->index != li)
3096 line_def = line_def->next;
3098 ll = line_def->net_length;
3099 lw = line_def->width;
3101 w = es->format_rect.right - es->format_rect.left;
3102 if (es->style & ES_RIGHT)
3104 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3105 es->tabs_count, es->tabs)) - es->x_offset;
3108 else if (es->style & ES_CENTER)
3110 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3111 es->tabs_count, es->tabs)) - es->x_offset;
3116 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3117 es->tabs_count, es->tabs)) - es->x_offset;
3120 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3121 if (index < es->x_offset) {
3122 GetTextExtentPoint32W(dc, text + index,
3123 es->x_offset - index, &size);
3126 GetTextExtentPoint32W(dc, text + es->x_offset,
3127 index - es->x_offset, &size);
3130 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3132 w = es->format_rect.right - es->format_rect.left;
3133 if (w > es->text_width)
3135 if (es->style & ES_RIGHT)
3136 x += w - es->text_width;
3137 else if (es->style & ES_CENTER)
3138 x += (w - es->text_width) / 2;
3143 if (es->style & ES_PASSWORD)
3144 HeapFree(GetProcessHeap(), 0, text);
3146 x += es->format_rect.left;
3147 y += es->format_rect.top;
3149 SelectObject(dc, old_font);
3150 ReleaseDC(es->hwndSelf, dc);
3151 return MAKELONG((INT16)x, (INT16)y);
3155 /*********************************************************************
3159 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3162 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3164 UINT strl = strlenW(lpsz_replace);
3165 UINT tl = get_text_length(es);
3176 TRACE("%s, can_undo %d, send_update %d\n",
3177 debugstr_w(lpsz_replace), can_undo, send_update);
3179 s = es->selection_start;
3180 e = es->selection_end;
3182 if ((s == e) && !strl)
3187 size = tl - (e - s) + strl;
3191 /* Issue the EN_MAXTEXT notification and continue with replacing text
3192 * such that buffer limit is honored. */
3193 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3194 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3195 strl = es->buffer_limit - (tl - (e-s));
3198 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3202 /* there is something to be deleted */
3203 TRACE("deleting stuff.\n");
3205 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3207 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3208 buf[bufl] = 0; /* ensure 0 termination */
3210 strcpyW(es->text + s, es->text + e);
3213 /* there is an insertion */
3214 tl = get_text_length(es);
3215 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));
3216 for (p = es->text + tl ; p >= es->text + s ; p--)
3218 for (i = 0 , p = es->text + s ; i < strl ; i++)
3219 p[i] = lpsz_replace[i];
3220 if(es->style & ES_UPPERCASE)
3221 CharUpperBuffW(p, strl);
3222 else if(es->style & ES_LOWERCASE)
3223 CharLowerBuffW(p, strl);
3225 if (es->style & ES_MULTILINE)
3227 INT st = min(es->selection_start, es->selection_end);
3228 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3230 hrgn = CreateRectRgn(0, 0, 0, 0);
3231 EDIT_BuildLineDefs_ML(es, st, st + strl,
3232 strl - abs(es->selection_end - es->selection_start), hrgn);
3233 /* if text is too long undo all changes */
3234 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3236 strcpyW(es->text + e, es->text + e + strl);
3238 for (i = 0 , p = es->text ; i < e - s ; i++)
3240 EDIT_BuildLineDefs_ML(es, s, e,
3241 abs(es->selection_end - es->selection_start) - strl, hrgn);
3244 hrgn = CreateRectRgn(0, 0, 0, 0);
3245 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3249 INT fw = es->format_rect.right - es->format_rect.left;
3250 EDIT_CalcLineWidth_SL(es);
3251 /* remove chars that don't fit */
3252 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3253 while ((es->text_width > fw) && s + strl >= s) {
3254 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3256 EDIT_CalcLineWidth_SL(es);
3258 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3264 utl = strlenW(es->undo_text);
3265 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3266 /* undo-buffer is extended to the right */
3267 EDIT_MakeUndoFit(es, utl + e - s);
3268 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3269 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3270 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3271 /* undo-buffer is extended to the left */
3272 EDIT_MakeUndoFit(es, utl + e - s);
3273 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3275 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3277 es->undo_position = s;
3279 /* new undo-buffer */
3280 EDIT_MakeUndoFit(es, e - s);
3281 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3282 es->undo_text[e - s] = 0; /* ensure 0 termination */
3283 es->undo_position = s;
3285 /* any deletion makes the old insertion-undo invalid */
3286 es->undo_insert_count = 0;
3288 EDIT_EM_EmptyUndoBuffer(es);
3292 if ((s == es->undo_position) ||
3293 ((es->undo_insert_count) &&
3294 (s == es->undo_position + es->undo_insert_count)))
3296 * insertion is new and at delete position or
3297 * an extension to either left or right
3299 es->undo_insert_count += strl;
3301 /* new insertion undo */
3302 es->undo_position = s;
3303 es->undo_insert_count = strl;
3304 /* new insertion makes old delete-buffer invalid */
3305 *es->undo_text = '\0';
3308 EDIT_EM_EmptyUndoBuffer(es);
3312 HeapFree(GetProcessHeap(), 0, buf);
3316 /* If text has been deleted and we're right or center aligned then scroll rightward */
3317 if (es->style & (ES_RIGHT | ES_CENTER))
3319 INT delta = strl - abs(es->selection_end - es->selection_start);
3321 if (delta < 0 && es->x_offset)
3323 if (abs(delta) > es->x_offset)
3326 es->x_offset += delta;
3330 EDIT_EM_SetSel(es, s, s, FALSE);
3331 es->flags |= EF_MODIFIED;
3332 if (send_update) es->flags |= EF_UPDATE;
3335 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3339 EDIT_UpdateText(es, NULL, TRUE);
3341 EDIT_EM_ScrollCaret(es);
3343 /* force scroll info update */
3344 EDIT_UpdateScrollInfo(es);
3347 if(send_update || (es->flags & EF_UPDATE))
3349 es->flags &= ~EF_UPDATE;
3350 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3355 /*********************************************************************
3360 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3364 if (!(es->style & ES_MULTILINE))
3365 return (LRESULT)FALSE;
3375 if (es->y_offset < es->line_count - 1)
3380 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3383 if (es->y_offset < es->line_count - 1)
3384 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3387 return (LRESULT)FALSE;
3390 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3391 /* check if we are going to move too far */
3392 if(es->y_offset + dy > es->line_count - vlc)
3393 dy = es->line_count - vlc - es->y_offset;
3395 /* Notification is done in EDIT_EM_LineScroll */
3397 EDIT_EM_LineScroll(es, 0, dy);
3399 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3403 /*********************************************************************
3408 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3410 if (es->style & ES_MULTILINE) {
3415 INT cw = es->char_width;
3420 l = EDIT_EM_LineFromChar(es, es->selection_end);
3421 li = EDIT_EM_LineIndex(es, l);
3422 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3423 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3424 if (l >= es->y_offset + vlc)
3425 dy = l - vlc + 1 - es->y_offset;
3426 if (l < es->y_offset)
3427 dy = l - es->y_offset;
3428 ww = es->format_rect.right - es->format_rect.left;
3429 if (x < es->format_rect.left)
3430 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3431 if (x > es->format_rect.right)
3432 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3433 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3435 /* check if we are going to move too far */
3436 if(es->x_offset + dx + ww > es->text_width)
3437 dx = es->text_width - ww - es->x_offset;
3438 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3439 EDIT_EM_LineScroll_internal(es, dx, dy);
3446 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3447 format_width = es->format_rect.right - es->format_rect.left;
3448 if (x < es->format_rect.left) {
3449 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3452 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3453 } while ((x < goal) && es->x_offset);
3454 /* FIXME: use ScrollWindow() somehow to improve performance */
3455 EDIT_UpdateText(es, NULL, TRUE);
3456 } else if (x > es->format_rect.right) {
3458 INT len = get_text_length(es);
3459 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3462 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3463 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3464 } while ((x > goal) && (x_last > es->format_rect.right));
3465 /* FIXME: use ScrollWindow() somehow to improve performance */
3466 EDIT_UpdateText(es, NULL, TRUE);
3470 if(es->flags & EF_FOCUSED)
3471 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3475 /*********************************************************************
3479 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3482 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3484 if (!(es->style & ES_MULTILINE))
3488 WARN("called with NULL handle\n");
3492 EDIT_UnlockBuffer(es, TRUE);
3496 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3497 HANDLE16 oldDS = stack16->ds;
3499 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3500 LocalFree16(es->hloc16);
3501 stack16->ds = oldDS;
3509 LocalFree(es->hloc32A);
3521 countA = LocalSize(hloc);
3522 textA = LocalLock(hloc);
3523 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3524 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3526 ERR("Could not allocate new unicode buffer\n");
3529 textW = LocalLock(hloc32W_new);
3530 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3531 LocalUnlock(hloc32W_new);
3535 LocalFree(es->hloc32W);
3537 es->hloc32W = hloc32W_new;
3541 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3543 es->flags |= EF_APP_HAS_HANDLE;
3544 EDIT_LockBuffer(es);
3546 es->x_offset = es->y_offset = 0;
3547 es->selection_start = es->selection_end = 0;
3548 EDIT_EM_EmptyUndoBuffer(es);
3549 es->flags &= ~EF_MODIFIED;
3550 es->flags &= ~EF_UPDATE;
3551 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3552 EDIT_UpdateText(es, NULL, TRUE);
3553 EDIT_EM_ScrollCaret(es);
3554 /* force scroll info update */
3555 EDIT_UpdateScrollInfo(es);
3559 /*********************************************************************
3563 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3566 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3568 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3569 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3570 HANDLE16 oldDS = stack16->ds;
3576 if (!(es->style & ES_MULTILINE))
3580 WARN("called with NULL handle\n");
3584 EDIT_UnlockBuffer(es, TRUE);
3588 LocalFree(es->hloc32A);
3592 stack16->ds = hInstance;
3593 countA = LocalSize16(hloc);
3594 textA = MapSL(LocalLock16(hloc));
3595 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3596 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3598 ERR("Could not allocate new unicode buffer\n");
3601 textW = LocalLock(hloc32W_new);
3602 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3603 LocalUnlock(hloc32W_new);
3604 LocalUnlock16(hloc);
3605 stack16->ds = oldDS;
3608 LocalFree(es->hloc32W);
3610 es->hloc32W = hloc32W_new;
3613 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3615 es->flags |= EF_APP_HAS_HANDLE;
3616 EDIT_LockBuffer(es);
3618 es->x_offset = es->y_offset = 0;
3619 es->selection_start = es->selection_end = 0;
3620 EDIT_EM_EmptyUndoBuffer(es);
3621 es->flags &= ~EF_MODIFIED;
3622 es->flags &= ~EF_UPDATE;
3623 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3624 EDIT_UpdateText(es, NULL, TRUE);
3625 EDIT_EM_ScrollCaret(es);
3626 /* force scroll info update */
3627 EDIT_UpdateScrollInfo(es);
3631 /*********************************************************************
3635 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3636 * However, the windows version is not complied to yet in all of edit.c
3638 * Additionally as the wrapper for RichEdit controls we need larger buffers
3639 * at present -1 will represent nolimit
3641 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3643 if (limit == 0xFFFFFFFF)
3644 es->buffer_limit = -1;
3645 else if (es->style & ES_MULTILINE) {
3647 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3649 es->buffer_limit = BUFLIMIT_MULTI;
3652 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3654 es->buffer_limit = BUFLIMIT_SINGLE;
3659 /*********************************************************************
3663 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3664 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3665 * margin according to the textmetrics of the current font.
3667 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3668 * of the char's width as the margin, but this is not how Windows handles this.
3669 * For all other fonts Windows sets the margins to zero.
3671 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3672 * edit control is equal to or larger than a certain size.
3673 * Interestingly if one subtracts both the left and right margins from
3674 * this size one always seems to get an even number. The extents of
3675 * the (four character) string "'**'" match this quite closely, so
3676 * we'll use this until we come up with a better idea.
3678 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3680 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3683 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3684 return sz.cx + left + right;
3687 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3688 WORD left, WORD right, BOOL repaint)
3691 INT default_left_margin = 0; /* in pixels */
3692 INT default_right_margin = 0; /* in pixels */
3694 /* Set the default margins depending on the font */
3695 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3696 HDC dc = GetDC(es->hwndSelf);
3697 HFONT old_font = SelectObject(dc, es->font);
3698 GetTextMetricsW(dc, &tm);
3699 /* The default margins are only non zero for TrueType or Vector fonts */
3700 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3703 /* This must be calculated more exactly! But how? */
3704 default_left_margin = tm.tmAveCharWidth / 2;
3705 default_right_margin = tm.tmAveCharWidth / 2;
3706 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3707 GetClientRect(es->hwndSelf, &rc);
3708 if(rc.right - rc.left < min_size) {
3709 default_left_margin = es->left_margin;
3710 default_right_margin = es->right_margin;
3713 SelectObject(dc, old_font);
3714 ReleaseDC(es->hwndSelf, dc);
3717 if (action & EC_LEFTMARGIN) {
3718 es->format_rect.left -= es->left_margin;
3719 if (left != EC_USEFONTINFO)
3720 es->left_margin = left;
3722 es->left_margin = default_left_margin;
3723 es->format_rect.left += es->left_margin;
3726 if (action & EC_RIGHTMARGIN) {
3727 es->format_rect.right += es->right_margin;
3728 if (right != EC_USEFONTINFO)
3729 es->right_margin = right;
3731 es->right_margin = default_right_margin;
3732 es->format_rect.right -= es->right_margin;
3735 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3736 EDIT_AdjustFormatRect(es);
3737 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3740 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3744 /*********************************************************************
3746 * EM_SETPASSWORDCHAR
3749 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3753 if (es->style & ES_MULTILINE)
3756 if (es->password_char == c)
3759 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3760 es->password_char = c;
3762 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3763 es->style |= ES_PASSWORD;
3765 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3766 es->style &= ~ES_PASSWORD;
3768 EDIT_UpdateText(es, NULL, TRUE);
3772 /*********************************************************************
3776 * note: unlike the specs say: the order of start and end
3777 * _is_ preserved in Windows. (i.e. start can be > end)
3778 * In other words: this handler is OK
3781 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3783 UINT old_start = es->selection_start;
3784 UINT old_end = es->selection_end;
3785 UINT len = get_text_length(es);
3787 if (start == (UINT)-1) {
3788 start = es->selection_end;
3789 end = es->selection_end;
3791 start = min(start, len);
3792 end = min(end, len);
3794 es->selection_start = start;
3795 es->selection_end = end;
3797 es->flags |= EF_AFTER_WRAP;
3799 es->flags &= ~EF_AFTER_WRAP;
3800 /* Compute the necessary invalidation region. */
3801 /* Note that we don't need to invalidate regions which have
3802 * "never" been selected, or those which are "still" selected.
3803 * In fact, every time we hit a selection boundary, we can
3804 * *toggle* whether we need to invalidate. Thus we can optimize by
3805 * *sorting* the interval endpoints. Let's assume that we sort them
3807 * start <= end <= old_start <= old_end
3808 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3809 * in 5 comparisons; ie it's impossible to do better than the
3811 ORDER_UINT(end, old_end);
3812 ORDER_UINT(start, old_start);
3813 ORDER_UINT(old_start, old_end);
3814 ORDER_UINT(start, end);
3815 /* Note that at this point 'end' and 'old_start' are not in order, but
3816 * start is definitely the min. and old_end is definitely the max. */
3817 if (end != old_start)
3821 * ORDER_UINT32(end, old_start);
3822 * EDIT_InvalidateText(es, start, end);
3823 * EDIT_InvalidateText(es, old_start, old_end);
3824 * in place of the following if statement.
3825 * (That would complete the optimal five-comparison four-element sort.)
3827 if (old_start > end )
3829 EDIT_InvalidateText(es, start, end);
3830 EDIT_InvalidateText(es, old_start, old_end);
3834 EDIT_InvalidateText(es, start, old_start);
3835 EDIT_InvalidateText(es, end, old_end);
3838 else EDIT_InvalidateText(es, start, old_end);
3842 /*********************************************************************
3847 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3849 if (!(es->style & ES_MULTILINE))
3851 HeapFree(GetProcessHeap(), 0, es->tabs);
3852 es->tabs_count = count;
3856 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3857 memcpy(es->tabs, tabs, count * sizeof(INT));
3863 /*********************************************************************
3868 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3870 if (!(es->style & ES_MULTILINE))
3872 HeapFree(GetProcessHeap(), 0, es->tabs);
3873 es->tabs_count = count;
3878 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3879 for (i = 0 ; i < count ; i++)
3880 es->tabs[i] = *tabs++;
3886 /*********************************************************************
3888 * EM_SETWORDBREAKPROC
3891 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3893 if (es->word_break_proc == wbp)
3896 es->word_break_proc = wbp;
3897 es->word_break_proc16 = NULL;
3899 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3900 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3901 EDIT_UpdateText(es, NULL, TRUE);
3906 /*********************************************************************
3908 * EM_SETWORDBREAKPROC16
3911 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3913 if (es->word_break_proc16 == wbp)
3916 es->word_break_proc = NULL;
3917 es->word_break_proc16 = wbp;
3918 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3919 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3920 EDIT_UpdateText(es, NULL, TRUE);
3925 /*********************************************************************
3930 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3935 /* As per MSDN spec, for a single-line edit control,
3936 the return value is always TRUE */
3937 if( es->style & ES_READONLY )
3938 return !(es->style & ES_MULTILINE);
3940 ulength = strlenW(es->undo_text);
3942 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3944 strcpyW(utext, es->undo_text);
3946 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3947 es->undo_insert_count, debugstr_w(utext));
3949 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3950 EDIT_EM_EmptyUndoBuffer(es);
3951 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3952 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3953 /* send the notification after the selection start and end are set */
3954 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3955 EDIT_EM_ScrollCaret(es);
3956 HeapFree(GetProcessHeap(), 0, utext);
3958 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3959 es->undo_insert_count, debugstr_w(es->undo_text));
3964 /*********************************************************************
3969 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3973 control = GetKeyState(VK_CONTROL) & 0x8000;
3977 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3978 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3981 if (es->style & ES_MULTILINE) {
3982 if (es->style & ES_READONLY) {
3983 EDIT_MoveHome(es, FALSE);
3984 EDIT_MoveDown_ML(es, FALSE);
3986 static const WCHAR cr_lfW[] = {'\r','\n',0};
3987 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3992 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3994 static const WCHAR tabW[] = {'\t',0};
3995 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3999 if (!(es->style & ES_READONLY) && !control) {
4000 if (es->selection_start != es->selection_end)
4003 /* delete character left of caret */
4004 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4005 EDIT_MoveBackward(es, TRUE);
4011 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4014 if (!(es->style & ES_READONLY))
4015 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4018 if (!(es->style & ES_READONLY))
4019 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4023 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4024 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4027 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4031 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4038 /*********************************************************************
4043 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4045 if (code || control)
4065 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4066 EDIT_EM_ScrollCaret(es);
4069 ERR("unknown menu item, please report\n");
4075 /*********************************************************************
4079 * Note: the resource files resource/sysres_??.rc cannot define a
4080 * single popup menu. Hence we use a (dummy) menubar
4081 * containing the single popup menu as its first item.
4083 * FIXME: the message identifiers have been chosen arbitrarily,
4084 * hence we use MF_BYPOSITION.
4085 * We might as well use the "real" values (anybody knows ?)
4086 * The menu definition is in resources/sysres_??.rc.
4087 * Once these are OK, we better use MF_BYCOMMAND here
4088 * (as we do in EDIT_WM_Command()).
4091 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4093 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4094 HMENU popup = GetSubMenu(menu, 0);
4095 UINT start = es->selection_start;
4096 UINT end = es->selection_end;
4098 ORDER_UINT(start, end);
4101 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4103 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4105 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4107 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4109 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4111 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4113 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4116 /* Windows places the menu at the edit's center in this case */
4117 GetClientRect(es->hwndSelf, &rc);
4118 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4119 x = rc.left + (rc.right - rc.left) / 2;
4120 y = rc.top + (rc.bottom - rc.top) / 2;
4123 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4128 /*********************************************************************
4133 static void EDIT_WM_Copy(EDITSTATE *es)
4135 INT s = min(es->selection_start, es->selection_end);
4136 INT e = max(es->selection_start, es->selection_end);
4144 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4145 dst = GlobalLock(hdst);
4146 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4147 dst[len] = 0; /* ensure 0 termination */
4148 TRACE("%s\n", debugstr_w(dst));
4150 OpenClipboard(es->hwndSelf);
4152 SetClipboardData(CF_UNICODETEXT, hdst);
4157 /*********************************************************************
4162 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4166 TRACE("%s\n", debugstr_w(name));
4168 * To initialize some final structure members, we call some helper
4169 * functions. However, since the EDITSTATE is not consistent (i.e.
4170 * not fully initialized), we should be very careful which
4171 * functions can be called, and in what order.
4173 EDIT_WM_SetFont(es, 0, FALSE);
4174 EDIT_EM_EmptyUndoBuffer(es);
4176 /* We need to calculate the format rect
4177 (applications may send EM_SETMARGINS before the control gets visible) */
4178 GetClientRect(es->hwndSelf, &clientRect);
4179 EDIT_SetRectNP(es, &clientRect);
4181 if (name && *name) {
4182 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4183 /* if we insert text to the editline, the text scrolls out
4184 * of the window, as the caret is placed after the insert
4185 * pos normally; thus we reset es->selection... to 0 and
4188 es->selection_start = es->selection_end = 0;
4189 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4190 * Messages are only to be sent when the USER does something to
4191 * change the contents. So I am removing this EN_CHANGE
4193 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4195 EDIT_EM_ScrollCaret(es);
4197 /* force scroll info update */
4198 EDIT_UpdateScrollInfo(es);
4199 /* The rule seems to return 1 here for success */
4200 /* Power Builder masked edit controls will crash */
4202 /* FIXME: is that in all cases so ? */
4207 /*********************************************************************
4212 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4217 while (LocalUnlock(es->hloc32W)) ;
4218 LocalFree(es->hloc32W);
4221 while (LocalUnlock(es->hloc32A)) ;
4222 LocalFree(es->hloc32A);
4225 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
4226 HANDLE16 oldDS = stack16->ds;
4228 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4229 while (LocalUnlock16(es->hloc16)) ;
4230 LocalFree16(es->hloc16);
4231 stack16->ds = oldDS;
4234 pc = es->first_line_def;
4238 HeapFree(GetProcessHeap(), 0, pc);
4242 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4243 HeapFree(GetProcessHeap(), 0, es);
4249 /*********************************************************************
4254 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4256 /* we do the proper erase in EDIT_WM_Paint */
4261 /*********************************************************************
4266 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4268 if(!count) return 0;
4272 lstrcpynW(dst, es->text, count);
4273 return strlenW(dst);
4277 LPSTR textA = (LPSTR)dst;
4278 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4279 textA[count - 1] = 0; /* ensure 0 termination */
4280 return strlen(textA);
4284 /*********************************************************************
4289 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4294 if (!(es->style & ES_MULTILINE))
4297 if (!(es->style & ES_AUTOHSCROLL))
4301 fw = es->format_rect.right - es->format_rect.left;
4304 TRACE("SB_LINELEFT\n");
4306 dx = -es->char_width;
4309 TRACE("SB_LINERIGHT\n");
4310 if (es->x_offset < es->text_width)
4311 dx = es->char_width;
4314 TRACE("SB_PAGELEFT\n");
4316 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4319 TRACE("SB_PAGERIGHT\n");
4320 if (es->x_offset < es->text_width)
4321 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4329 TRACE("SB_RIGHT\n");
4330 if (es->x_offset < es->text_width)
4331 dx = es->text_width - es->x_offset;
4334 TRACE("SB_THUMBTRACK %d\n", pos);
4335 es->flags |= EF_HSCROLL_TRACK;
4336 if(es->style & WS_HSCROLL)
4337 dx = pos - es->x_offset;
4342 if(pos < 0 || pos > 100) return 0;
4343 /* Assume default scroll range 0-100 */
4344 fw = es->format_rect.right - es->format_rect.left;
4345 new_x = pos * (es->text_width - fw) / 100;
4346 dx = es->text_width ? (new_x - es->x_offset) : 0;
4349 case SB_THUMBPOSITION:
4350 TRACE("SB_THUMBPOSITION %d\n", pos);
4351 es->flags &= ~EF_HSCROLL_TRACK;
4352 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4353 dx = pos - es->x_offset;
4358 if(pos < 0 || pos > 100) return 0;
4359 /* Assume default scroll range 0-100 */
4360 fw = es->format_rect.right - es->format_rect.left;
4361 new_x = pos * (es->text_width - fw) / 100;
4362 dx = es->text_width ? (new_x - es->x_offset) : 0;
4365 /* force scroll info update */
4366 EDIT_UpdateScrollInfo(es);
4367 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4371 TRACE("SB_ENDSCROLL\n");
4374 * FIXME : the next two are undocumented !
4375 * Are we doing the right thing ?
4376 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4377 * although it's also a regular control message.
4379 case EM_GETTHUMB: /* this one is used by NT notepad */
4383 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4384 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4387 /* Assume default scroll range 0-100 */
4388 INT fw = es->format_rect.right - es->format_rect.left;
4389 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4391 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4394 case EM_LINESCROLL16:
4395 TRACE("EM_LINESCROLL16\n");
4400 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4406 INT fw = es->format_rect.right - es->format_rect.left;
4407 /* check if we are going to move too far */
4408 if(es->x_offset + dx + fw > es->text_width)
4409 dx = es->text_width - fw - es->x_offset;
4411 EDIT_EM_LineScroll_internal(es, dx, 0);
4417 /*********************************************************************
4422 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4424 HWND hLBox = es->hwndListBox;
4432 hCombo = GetParent(es->hwndSelf);
4436 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4438 if (key == VK_UP || key == VK_DOWN)
4440 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4443 if (msg == WM_KEYDOWN || nEUI)
4444 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4450 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4452 /* make sure ComboLBox pops up */
4453 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4458 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4461 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4463 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4465 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4470 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4476 /*********************************************************************
4480 * Handling of special keys that don't produce a WM_CHAR
4481 * (i.e. non-printable keys) & Backspace & Delete
4484 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4489 if (GetKeyState(VK_MENU) & 0x8000)
4492 shift = GetKeyState(VK_SHIFT) & 0x8000;
4493 control = GetKeyState(VK_CONTROL) & 0x8000;
4498 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4503 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4504 EDIT_MoveUp_ML(es, shift);
4507 EDIT_MoveWordBackward(es, shift);
4509 EDIT_MoveBackward(es, shift);
4512 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4516 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4517 EDIT_MoveDown_ML(es, shift);
4519 EDIT_MoveWordForward(es, shift);
4521 EDIT_MoveForward(es, shift);
4524 EDIT_MoveHome(es, shift);
4527 EDIT_MoveEnd(es, shift);
4530 if (es->style & ES_MULTILINE)
4531 EDIT_MovePageUp_ML(es, shift);
4533 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4536 if (es->style & ES_MULTILINE)
4537 EDIT_MovePageDown_ML(es, shift);
4539 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4542 if (!(es->style & ES_READONLY) && !(shift && control)) {
4543 if (es->selection_start != es->selection_end) {
4550 /* delete character left of caret */
4551 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4552 EDIT_MoveBackward(es, TRUE);
4554 } else if (control) {
4555 /* delete to end of line */
4556 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4557 EDIT_MoveEnd(es, TRUE);
4560 /* delete character right of caret */
4561 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4562 EDIT_MoveForward(es, TRUE);
4570 if (!(es->style & ES_READONLY))
4576 /* If the edit doesn't want the return send a message to the default object */
4577 if(!(es->style & ES_WANTRETURN))
4579 HWND hwndParent = GetParent(es->hwndSelf);
4580 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4581 if (HIWORD(dw) == DC_HASDEFID)
4583 SendMessageW( hwndParent, WM_COMMAND,
4584 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4585 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4594 /*********************************************************************
4599 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4601 es->flags &= ~EF_FOCUSED;
4603 if(!(es->style & ES_NOHIDESEL))
4604 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4605 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4610 /*********************************************************************
4614 * The caret position has been set on the WM_LBUTTONDOWN message
4617 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4620 INT e = es->selection_end;
4625 es->bCaptureState = TRUE;
4626 SetCapture(es->hwndSelf);
4628 l = EDIT_EM_LineFromChar(es, e);
4629 li = EDIT_EM_LineIndex(es, l);
4630 ll = EDIT_EM_LineLength(es, e);
4631 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4632 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4633 EDIT_EM_SetSel(es, s, e, FALSE);
4634 EDIT_EM_ScrollCaret(es);
4635 es->region_posx = es->region_posy = 0;
4636 SetTimer(es->hwndSelf, 0, 100, NULL);
4641 /*********************************************************************
4646 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4651 es->bCaptureState = TRUE;
4652 SetCapture(es->hwndSelf);
4653 EDIT_ConfinePoint(es, &x, &y);
4654 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4655 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4656 EDIT_EM_ScrollCaret(es);
4657 es->region_posx = es->region_posy = 0;
4658 SetTimer(es->hwndSelf, 0, 100, NULL);
4660 if (!(es->flags & EF_FOCUSED))
4661 SetFocus(es->hwndSelf);
4667 /*********************************************************************
4672 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4674 if (es->bCaptureState) {
4675 KillTimer(es->hwndSelf, 0);
4676 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4678 es->bCaptureState = FALSE;
4683 /*********************************************************************
4688 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4690 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4695 /*********************************************************************
4700 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4706 /* If the mouse has been captured by process other than the edit control itself,
4707 * the windows edit controls will not select the strings with mouse move.
4709 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4713 * FIXME: gotta do some scrolling if outside client
4714 * area. Maybe reset the timer ?
4717 EDIT_ConfinePoint(es, &x, &y);
4718 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4719 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4720 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4721 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4722 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4727 /*********************************************************************
4731 * See also EDIT_WM_StyleChanged
4733 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4738 TRACE("Creating %s edit control, style = %08x\n",
4739 unicode ? "Unicode" : "ANSI", lpcs->style);
4741 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4743 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4746 * Note: since the EDITSTATE has not been fully initialized yet,
4747 * we can't use any API calls that may send
4748 * WM_XXX messages before WM_NCCREATE is completed.
4751 es->is_unicode = unicode;
4752 es->style = lpcs->style;
4754 es->bEnableState = !(es->style & WS_DISABLED);
4756 es->hwndSelf = hwnd;
4757 /* Save parent, which will be notified by EN_* messages */
4758 es->hwndParent = lpcs->hwndParent;
4760 if (es->style & ES_COMBO)
4761 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4763 /* Number overrides lowercase overrides uppercase (at least it
4764 * does in Win95). However I'll bet that ES_NUMBER would be
4765 * invalid under Win 3.1.
4767 if (es->style & ES_NUMBER) {
4768 ; /* do not override the ES_NUMBER */
4769 } else if (es->style & ES_LOWERCASE) {
4770 es->style &= ~ES_UPPERCASE;
4772 if (es->style & ES_MULTILINE) {
4773 es->buffer_limit = BUFLIMIT_MULTI;
4774 if (es->style & WS_VSCROLL)
4775 es->style |= ES_AUTOVSCROLL;
4776 if (es->style & WS_HSCROLL)
4777 es->style |= ES_AUTOHSCROLL;
4778 es->style &= ~ES_PASSWORD;
4779 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4780 /* Confirmed - RIGHT overrides CENTER */
4781 if (es->style & ES_RIGHT)
4782 es->style &= ~ES_CENTER;
4783 es->style &= ~WS_HSCROLL;
4784 es->style &= ~ES_AUTOHSCROLL;
4787 es->buffer_limit = BUFLIMIT_SINGLE;
4788 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4789 es->style &= ~ES_CENTER;
4790 es->style &= ~WS_HSCROLL;
4791 es->style &= ~WS_VSCROLL;
4792 if (es->style & ES_PASSWORD)
4793 es->password_char = '*';
4796 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4797 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4799 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4801 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4803 es->undo_buffer_size = es->buffer_size;
4805 if (es->style & ES_MULTILINE)
4806 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4811 * In Win95 look and feel, the WS_BORDER style is replaced by the
4812 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4813 * control a nonclient area so we don't need to draw the border.
4814 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4815 * a nonclient area and we should handle painting the border ourselves.
4817 * When making modifications please ensure that the code still works
4818 * for edit controls created directly with style 0x50800000, exStyle 0
4819 * (which should have a single pixel border)
4821 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4822 es->style &= ~WS_BORDER;
4823 else if (es->style & WS_BORDER)
4824 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4829 /*********************************************************************
4834 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4847 BOOL rev = es->bEnableState &&
4848 ((es->flags & EF_FOCUSED) ||
4849 (es->style & ES_NOHIDESEL));
4850 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4852 GetClientRect(es->hwndSelf, &rcClient);
4854 /* get the background brush */
4855 brush = EDIT_NotifyCtlColor(es, dc);
4857 /* paint the border and the background */
4858 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4860 if(es->style & WS_BORDER) {
4861 bw = GetSystemMetrics(SM_CXBORDER);
4862 bh = GetSystemMetrics(SM_CYBORDER);
4864 if(es->style & ES_MULTILINE) {
4865 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4866 if(es->style & WS_VSCROLL) rc.right+=bw;
4869 /* Draw the frame. Same code as in nonclient.c */
4870 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4871 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4872 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4873 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4874 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4875 SelectObject(dc, old_brush);
4877 /* Keep the border clean */
4878 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4879 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4882 GetClipBox(dc, &rc);
4883 FillRect(dc, &rc, brush);
4885 IntersectClipRect(dc, es->format_rect.left,
4886 es->format_rect.top,
4887 es->format_rect.right,
4888 es->format_rect.bottom);
4889 if (es->style & ES_MULTILINE) {
4891 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4894 old_font = SelectObject(dc, es->font);
4896 if (!es->bEnableState)
4897 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4898 GetClipBox(dc, &rcRgn);
4899 if (es->style & ES_MULTILINE) {
4900 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4901 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4902 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4903 if (IntersectRect(&rc, &rcRgn, &rcLine))
4904 EDIT_PaintLine(es, dc, i, rev);
4907 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4908 if (IntersectRect(&rc, &rcRgn, &rcLine))
4909 EDIT_PaintLine(es, dc, 0, rev);
4912 SelectObject(dc, old_font);
4915 EndPaint(es->hwndSelf, &ps);
4919 /*********************************************************************
4924 static void EDIT_WM_Paste(EDITSTATE *es)
4929 /* Protect read-only edit control from modification */
4930 if(es->style & ES_READONLY)
4933 OpenClipboard(es->hwndSelf);
4934 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4935 src = (LPWSTR)GlobalLock(hsrc);
4936 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4943 /*********************************************************************
4948 static void EDIT_WM_SetFocus(EDITSTATE *es)
4950 es->flags |= EF_FOCUSED;
4952 if (!(es->style & ES_NOHIDESEL))
4953 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4955 /* single line edit updates itself */
4956 if (!(es->style & ES_MULTILINE))
4958 HDC hdc = GetDC(es->hwndSelf);
4959 EDIT_WM_Paint(es, hdc);
4960 ReleaseDC(es->hwndSelf, hdc);
4963 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4964 EDIT_SetCaretPos(es, es->selection_end,
4965 es->flags & EF_AFTER_WRAP);
4966 ShowCaret(es->hwndSelf);
4967 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
4971 /*********************************************************************
4975 * With Win95 look the margins are set to default font value unless
4976 * the system font (font == 0) is being set, in which case they are left
4980 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4988 dc = GetDC(es->hwndSelf);
4990 old_font = SelectObject(dc, font);
4991 GetTextMetricsW(dc, &tm);
4992 es->line_height = tm.tmHeight;
4993 es->char_width = tm.tmAveCharWidth;
4995 SelectObject(dc, old_font);
4996 ReleaseDC(es->hwndSelf, dc);
4998 /* Reset the format rect and the margins */
4999 GetClientRect(es->hwndSelf, &clientRect);
5000 EDIT_SetRectNP(es, &clientRect);
5001 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5002 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5004 if (es->style & ES_MULTILINE)
5005 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5007 EDIT_CalcLineWidth_SL(es);
5010 EDIT_UpdateText(es, NULL, TRUE);
5011 if (es->flags & EF_FOCUSED) {
5013 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5014 EDIT_SetCaretPos(es, es->selection_end,
5015 es->flags & EF_AFTER_WRAP);
5016 ShowCaret(es->hwndSelf);
5021 /*********************************************************************
5026 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5027 * The modified flag is reset. No notifications are sent.
5029 * For single-line controls, reception of WM_SETTEXT triggers:
5030 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5033 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5035 LPWSTR textW = NULL;
5036 if (!unicode && text)
5038 LPCSTR textA = (LPCSTR)text;
5039 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5040 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5042 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5046 if (es->flags & EF_UPDATE)
5047 /* fixed this bug once; complain if we see it about to happen again. */
5048 ERR("SetSel may generate UPDATE message whose handler may reset "
5051 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5054 TRACE("%s\n", debugstr_w(text));
5055 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5057 HeapFree(GetProcessHeap(), 0, textW);
5061 static const WCHAR empty_stringW[] = {0};
5063 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5066 es->flags &= ~EF_MODIFIED;
5067 EDIT_EM_SetSel(es, 0, 0, FALSE);
5069 /* Send the notification after the selection start and end have been set
5070 * edit control doesn't send notification on WM_SETTEXT
5071 * if it is multiline, or it is part of combobox
5073 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5075 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5076 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5078 EDIT_EM_ScrollCaret(es);
5079 EDIT_UpdateScrollInfo(es);
5083 /*********************************************************************
5088 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5090 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5092 TRACE("width = %d, height = %d\n", width, height);
5093 SetRect(&rc, 0, 0, width, height);
5094 EDIT_SetRectNP(es, &rc);
5095 EDIT_UpdateText(es, NULL, TRUE);
5100 /*********************************************************************
5104 * This message is sent by SetWindowLong on having changed either the Style
5105 * or the extended style.
5107 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5109 * See also EDIT_WM_NCCreate
5111 * It appears that the Windows version of the edit control allows the style
5112 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5113 * style variable which will generally be different. In this function we
5114 * update the internal style based on what changed in the externally visible
5117 * Much of this content as based upon the MSDN, especially:
5118 * Platform SDK Documentation -> User Interface Services ->
5119 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5120 * Edit Control Styles
5122 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5124 if (GWL_STYLE == which) {
5125 DWORD style_change_mask;
5127 /* Only a subset of changes can be applied after the control
5130 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5132 if (es->style & ES_MULTILINE)
5133 style_change_mask |= ES_WANTRETURN;
5135 new_style = style->styleNew & style_change_mask;
5137 /* Number overrides lowercase overrides uppercase (at least it
5138 * does in Win95). However I'll bet that ES_NUMBER would be
5139 * invalid under Win 3.1.
5141 if (new_style & ES_NUMBER) {
5142 ; /* do not override the ES_NUMBER */
5143 } else if (new_style & ES_LOWERCASE) {
5144 new_style &= ~ES_UPPERCASE;
5147 es->style = (es->style & ~style_change_mask) | new_style;
5148 } else if (GWL_EXSTYLE == which) {
5149 ; /* FIXME - what is needed here */
5151 WARN ("Invalid style change %d\n",which);
5157 /*********************************************************************
5162 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5164 if ((key == VK_BACK) && (key_data & 0x2000)) {
5165 if (EDIT_EM_CanUndo(es))
5168 } else if (key == VK_UP || key == VK_DOWN) {
5169 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5172 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5176 /*********************************************************************
5181 static void EDIT_WM_Timer(EDITSTATE *es)
5183 if (es->region_posx < 0) {
5184 EDIT_MoveBackward(es, TRUE);
5185 } else if (es->region_posx > 0) {
5186 EDIT_MoveForward(es, TRUE);
5189 * FIXME: gotta do some vertical scrolling here, like
5190 * EDIT_EM_LineScroll(hwnd, 0, 1);
5194 /*********************************************************************
5199 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5203 if (!(es->style & ES_MULTILINE))
5206 if (!(es->style & ES_AUTOVSCROLL))
5215 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5216 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5217 (action == SB_PAGEUP ? "SB_PAGEUP" :
5219 EDIT_EM_Scroll(es, action);
5226 TRACE("SB_BOTTOM\n");
5227 dy = es->line_count - 1 - es->y_offset;
5230 TRACE("SB_THUMBTRACK %d\n", pos);
5231 es->flags |= EF_VSCROLL_TRACK;
5232 if(es->style & WS_VSCROLL)
5233 dy = pos - es->y_offset;
5236 /* Assume default scroll range 0-100 */
5239 if(pos < 0 || pos > 100) return 0;
5240 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5241 new_y = pos * (es->line_count - vlc) / 100;
5242 dy = es->line_count ? (new_y - es->y_offset) : 0;
5243 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5244 es->line_count, es->y_offset, pos, dy);
5247 case SB_THUMBPOSITION:
5248 TRACE("SB_THUMBPOSITION %d\n", pos);
5249 es->flags &= ~EF_VSCROLL_TRACK;
5250 if(es->style & WS_VSCROLL)
5251 dy = pos - es->y_offset;
5254 /* Assume default scroll range 0-100 */
5257 if(pos < 0 || pos > 100) return 0;
5258 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5259 new_y = pos * (es->line_count - vlc) / 100;
5260 dy = es->line_count ? (new_y - es->y_offset) : 0;
5261 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5262 es->line_count, es->y_offset, pos, dy);
5266 /* force scroll info update */
5267 EDIT_UpdateScrollInfo(es);
5268 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5272 TRACE("SB_ENDSCROLL\n");
5275 * FIXME : the next two are undocumented !
5276 * Are we doing the right thing ?
5277 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5278 * although it's also a regular control message.
5280 case EM_GETTHUMB: /* this one is used by NT notepad */
5284 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5285 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5288 /* Assume default scroll range 0-100 */
5289 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5290 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5292 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5295 case EM_LINESCROLL16:
5296 TRACE("EM_LINESCROLL16 %d\n", pos);
5301 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5306 EDIT_EM_LineScroll(es, 0, dy);
5310 /*********************************************************************
5315 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5317 if (es->flags & EF_UPDATE) {
5318 es->flags &= ~EF_UPDATE;
5319 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5321 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5325 /*********************************************************************
5330 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5332 if (es->flags & EF_UPDATE) {
5333 es->flags &= ~EF_UPDATE;
5334 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5336 InvalidateRect(es->hwndSelf, rc, bErase);
5339 /********************************************************************
5341 * The Following code is to handle inline editing from IMEs
5344 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5347 LPWSTR lpCompStr = NULL;
5349 LPSTR lpCompStrAttr = NULL;
5352 if (!(hIMC = ImmGetContext(hwnd)))
5355 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5359 ImmReleaseContext(hwnd, hIMC);
5363 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
5366 ERR("Unable to allocate IME CompositionString\n");
5367 ImmReleaseContext(hwnd,hIMC);
5372 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
5373 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
5375 if (CompFlag & GCS_COMPATTR)
5378 * We do not use the attributes yet. it would tell us what characters
5379 * are in transition and which are converted or decided upon
5381 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5385 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5388 ERR("Unable to allocate IME Attribute String\n");
5389 HeapFree(GetProcessHeap(),0,lpCompStr);
5390 ImmReleaseContext(hwnd,hIMC);
5393 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5395 lpCompStrAttr[dwBufLenAttr] = 0;
5398 lpCompStrAttr = NULL;
5401 /* check for change in composition start */
5402 if (es->selection_end < es->composition_start)
5403 es->composition_start = es->selection_end;
5405 /* replace existing selection string */
5406 es->selection_start = es->composition_start;
5408 if (es->composition_len > 0)
5409 es->selection_end = es->composition_start + es->composition_len;
5411 es->selection_end = es->selection_start;
5413 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5414 es->composition_len = abs(es->composition_start - es->selection_end);
5416 es->selection_start = es->composition_start;
5417 es->selection_end = es->selection_start + es->composition_len;
5419 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5420 HeapFree(GetProcessHeap(),0,lpCompStr);
5421 ImmReleaseContext(hwnd,hIMC);
5424 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5430 if ( !(hIMC = ImmGetContext(hwnd)))
5433 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5436 ImmReleaseContext(hwnd, hIMC);
5440 lpResultStr = HeapAlloc(GetProcessHeap(),0, dwBufLen+sizeof(WCHAR));
5443 ERR("Unable to alloc buffer for IME string\n");
5444 ImmReleaseContext(hwnd, hIMC);
5448 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, dwBufLen);
5449 lpResultStr[dwBufLen/sizeof(WCHAR)] = 0;
5451 /* check for change in composition start */
5452 if (es->selection_end < es->composition_start)
5453 es->composition_start = es->selection_end;
5455 es->selection_start = es->composition_start;
5456 es->selection_end = es->composition_start + es->composition_len;
5457 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5458 es->composition_start = es->selection_end;
5459 es->composition_len = 0;
5461 HeapFree(GetProcessHeap(),0,lpResultStr);
5462 ImmReleaseContext(hwnd, hIMC);
5465 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5467 if (CompFlag & GCS_RESULTSTR)
5468 EDIT_GetResultStr(hwnd,es);
5469 if (CompFlag & GCS_COMPSTR)
5470 EDIT_GetCompositionStr(hwnd, CompFlag, es);