4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
56 #include "wine/winbase16.h"
57 #include "wine/winuser16.h"
58 #include "wine/unicode.h"
60 #include "user_private.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(edit);
64 WINE_DECLARE_DEBUG_CHANNEL(combo);
65 WINE_DECLARE_DEBUG_CHANNEL(relay);
67 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
68 FIXME: BTW, new specs say 65535 (do you dare ???) */
69 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
70 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
71 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
72 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
75 * extra flags for EDITSTATE.flags field
77 #define EF_MODIFIED 0x0001 /* text has been modified */
78 #define EF_FOCUSED 0x0002 /* we have input focus */
79 #define EF_UPDATE 0x0004 /* notify parent of changed state */
80 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
82 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
83 wrapped line, instead of in front of the next character */
84 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT buffer_size; /* the size of the buffer in characters */
109 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
110 HFONT font; /* NULL means standard system font */
111 INT x_offset; /* scroll offset for multi lines this is in pixels
112 for single lines it's in characters */
113 INT line_height; /* height of a screen line in pixels */
114 INT char_width; /* average character width in pixels */
115 DWORD style; /* sane version of wnd->dwStyle */
116 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
117 INT undo_insert_count; /* number of characters inserted in sequence */
118 UINT undo_position; /* character index of the insertion and deletion */
119 LPWSTR undo_text; /* deleted text */
120 UINT undo_buffer_size; /* size of the deleted text buffer */
121 INT selection_start; /* == selection_end if no selection */
122 INT selection_end; /* == current caret position */
123 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
124 INT left_margin; /* in pixels */
125 INT right_margin; /* in pixels */
127 INT text_width; /* width of the widest line in pixels for multi line controls
128 and just line width for single line controls */
129 INT region_posx; /* Position of cursor relative to region: */
130 INT region_posy; /* -1: to left, 0: within, 1: to right */
131 EDITWORDBREAKPROC16 word_break_proc16;
132 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
133 INT line_count; /* number of lines */
134 INT y_offset; /* scroll offset in number of lines */
135 BOOL bCaptureState; /* flag indicating whether mouse was captured */
136 BOOL bEnableState; /* flag keeping the enable state */
137 HWND hwndSelf; /* the our window handle */
138 HWND hwndParent; /* Handle of parent for sending EN_* messages.
139 Even if parent will change, EN_* messages
140 should be sent to the first parent. */
141 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
143 * only for multi line controls
145 INT lock_count; /* amount of re-entries in the EditWndProc */
148 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
149 HLOCAL hloc32W; /* our unicode local memory block */
150 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
152 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
157 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
158 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
160 /* used for disabled or read-only edit control */
161 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
163 { /* Notify parent which has created this edit control */ \
164 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
165 SendMessageW(es->hwndParent, WM_COMMAND, \
166 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
167 (LPARAM)(es->hwndSelf)); \
170 /*********************************************************************
177 * These functions have trivial implementations
178 * We still like to call them internally
179 * "static inline" makes them more like macro's
181 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
182 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
183 static inline void EDIT_WM_Clear(EDITSTATE *es);
184 static inline void EDIT_WM_Cut(EDITSTATE *es);
187 * Helper functions only valid for one type of control
189 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
190 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
191 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
192 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
193 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
194 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
195 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
197 * Helper functions valid for both single line _and_ multi line controls
199 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
200 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
201 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
202 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
203 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
204 static void EDIT_LockBuffer(EDITSTATE *es);
205 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
206 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
207 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
208 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
209 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
210 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
211 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
212 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
213 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
214 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
215 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
216 static void EDIT_AdjustFormatRect(EDITSTATE *es);
217 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
218 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
219 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
220 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
222 * EM_XXX message handlers
224 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
225 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
226 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
227 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
228 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
229 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
230 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
231 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
232 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
233 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
234 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
235 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
236 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
237 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
238 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
239 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
240 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
241 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
242 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
243 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right, BOOL repaint);
244 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
245 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
246 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
247 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
248 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
249 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
250 static BOOL EDIT_EM_Undo(EDITSTATE *es);
252 * WM_XXX message handlers
254 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
255 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
256 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
257 static void EDIT_WM_Copy(EDITSTATE *es);
258 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
259 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
260 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
261 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
262 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
263 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
264 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
265 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
266 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
267 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
268 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
269 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
270 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
271 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
272 static void EDIT_WM_Paste(EDITSTATE *es);
273 static void EDIT_WM_SetFocus(EDITSTATE *es);
274 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
275 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
276 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
277 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
278 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
279 static void EDIT_WM_Timer(EDITSTATE *es);
280 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
281 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
282 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
284 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
285 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
287 /*********************************************************************
288 * edit class descriptor
290 const struct builtin_class_descr EDIT_builtin_class =
293 CS_DBLCLKS | CS_PARENTDC, /* style */
294 EditWndProcA, /* procA */
295 EditWndProcW, /* procW */
296 sizeof(EDITSTATE *), /* extra */
297 IDC_IBEAM, /* cursor */
302 /*********************************************************************
307 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
309 return (es->undo_insert_count || strlenW(es->undo_text));
313 /*********************************************************************
318 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
320 es->undo_insert_count = 0;
321 *es->undo_text = '\0';
325 /*********************************************************************
330 static inline void EDIT_WM_Clear(EDITSTATE *es)
332 static const WCHAR empty_stringW[] = {0};
334 /* Protect read-only edit control from modification */
335 if(es->style & ES_READONLY)
338 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
342 /*********************************************************************
347 static inline void EDIT_WM_Cut(EDITSTATE *es)
354 /**********************************************************************
357 * Returns the window version in case Wine emulates a later version
358 * of windows than the application expects.
360 * In a number of cases when windows runs an application that was
361 * designed for an earlier windows version, windows reverts
362 * to "old" behaviour of that earlier version.
364 * An example is a disabled edit control that needs to be painted.
365 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
366 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
367 * applications with an expected version 0f 4.0 or higher.
370 static DWORD get_app_version(void)
372 static DWORD version;
375 DWORD dwEmulatedVersion;
377 DWORD dwProcVersion = GetProcessVersion(0);
379 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
380 GetVersionExW( &info );
381 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
382 /* FIXME: this may not be 100% correct; see discussion on the
383 * wine developer list in Nov 1999 */
384 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
390 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
395 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
396 msg = WM_CTLCOLORSTATIC;
398 msg = WM_CTLCOLOREDIT;
400 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
401 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
403 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
407 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
410 return DefWindowProcW(hwnd, msg, wParam, lParam);
412 return DefWindowProcA(hwnd, msg, wParam, lParam);
415 /*********************************************************************
419 * The messages are in the order of the actual integer values
420 * (which can be found in include/windows.h)
421 * Wherever possible the 16 bit versions are converted to
422 * the 32 bit ones, so that we can 'fall through' to the
423 * helper functions. These are mostly 32 bit (with a few
424 * exceptions, clearly indicated by a '16' extension to their
428 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
429 WPARAM wParam, LPARAM lParam, BOOL unicode )
431 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
434 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
436 if (!es && msg != WM_NCCREATE)
437 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
439 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
447 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
451 if ((short)LOWORD(lParam) == -1)
452 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
454 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
456 EDIT_EM_ScrollCaret(es);
460 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
461 EDIT_EM_ScrollCaret(es);
468 RECT16 *r16 = MapSL(lParam);
469 r16->left = es->format_rect.left;
470 r16->top = es->format_rect.top;
471 r16->right = es->format_rect.right;
472 r16->bottom = es->format_rect.bottom;
477 CopyRect((LPRECT)lParam, &es->format_rect);
481 if ((es->style & ES_MULTILINE) && lParam) {
483 RECT16 *r16 = MapSL(lParam);
486 rc.right = r16->right;
487 rc.bottom = r16->bottom;
488 EDIT_SetRectNP(es, &rc);
489 EDIT_UpdateText(es, NULL, TRUE);
493 if ((es->style & ES_MULTILINE) && lParam) {
494 EDIT_SetRectNP(es, (LPRECT)lParam);
495 EDIT_UpdateText(es, NULL, TRUE);
500 if ((es->style & ES_MULTILINE) && lParam) {
502 RECT16 *r16 = MapSL(lParam);
505 rc.right = r16->right;
506 rc.bottom = r16->bottom;
507 EDIT_SetRectNP(es, &rc);
511 if ((es->style & ES_MULTILINE) && lParam)
512 EDIT_SetRectNP(es, (LPRECT)lParam);
517 result = EDIT_EM_Scroll(es, (INT)wParam);
520 case EM_LINESCROLL16:
521 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
522 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
525 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
528 case EM_SCROLLCARET16:
530 EDIT_EM_ScrollCaret(es);
536 result = ((es->flags & EF_MODIFIED) != 0);
542 es->flags |= EF_MODIFIED;
544 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
547 case EM_GETLINECOUNT16:
548 case EM_GETLINECOUNT:
549 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
553 if ((INT16)wParam == -1)
557 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
561 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
564 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
568 result = (LRESULT)EDIT_EM_GetHandle16(es);
571 result = (LRESULT)EDIT_EM_GetHandle(es);
576 result = EDIT_EM_GetThumb(es);
579 /* these messages missing from specs */
588 FIXME("undocumented message 0x%x, please report\n", msg);
589 result = DefWindowProcW(hwnd, msg, wParam, lParam);
592 case EM_LINELENGTH16:
594 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
597 case EM_REPLACESEL16:
598 lParam = (LPARAM)MapSL(lParam);
599 unicode = FALSE; /* 16-bit message is always ascii */
606 textW = (LPWSTR)lParam;
609 LPSTR textA = (LPSTR)lParam;
610 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
611 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
612 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
615 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
619 HeapFree(GetProcessHeap(), 0, textW);
624 lParam = (LPARAM)MapSL(lParam);
625 unicode = FALSE; /* 16-bit message is always ascii */
628 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
632 case EM_SETLIMITTEXT:
633 EDIT_EM_SetLimitText(es, (INT)wParam);
638 result = (LRESULT)EDIT_EM_CanUndo(es);
644 result = (LRESULT)EDIT_EM_Undo(es);
649 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
652 case EM_LINEFROMCHAR16:
653 case EM_LINEFROMCHAR:
654 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
657 case EM_SETTABSTOPS16:
658 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
661 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
664 case EM_SETPASSWORDCHAR16:
665 unicode = FALSE; /* 16-bit message is always ascii */
667 case EM_SETPASSWORDCHAR:
672 charW = (WCHAR)wParam;
676 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
679 EDIT_EM_SetPasswordChar(es, charW);
683 case EM_EMPTYUNDOBUFFER16:
684 case EM_EMPTYUNDOBUFFER:
685 EDIT_EM_EmptyUndoBuffer(es);
688 case EM_GETFIRSTVISIBLELINE16:
689 result = es->y_offset;
691 case EM_GETFIRSTVISIBLELINE:
692 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
695 case EM_SETREADONLY16:
698 SetWindowLongW( hwnd, GWL_STYLE,
699 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
700 es->style |= ES_READONLY;
702 SetWindowLongW( hwnd, GWL_STYLE,
703 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
704 es->style &= ~ES_READONLY;
709 case EM_SETWORDBREAKPROC16:
710 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
712 case EM_SETWORDBREAKPROC:
713 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
716 case EM_GETWORDBREAKPROC16:
717 result = (LRESULT)es->word_break_proc16;
719 case EM_GETWORDBREAKPROC:
720 result = (LRESULT)es->word_break_proc;
723 case EM_GETPASSWORDCHAR16:
724 unicode = FALSE; /* 16-bit message is always ascii */
726 case EM_GETPASSWORDCHAR:
729 result = es->password_char;
732 WCHAR charW = es->password_char;
734 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
740 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
743 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam), TRUE);
747 result = MAKELONG(es->left_margin, es->right_margin);
750 case EM_GETLIMITTEXT:
751 result = es->buffer_limit;
755 result = strlenW(es->text);
756 if ((INT)wParam >= result) result = -1;
757 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
761 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
764 /* End of the EM_ messages which were in numerical order; what order
765 * are these in? vaguely alphabetical?
769 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
773 result = EDIT_WM_Destroy(es);
778 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
780 if (es->style & ES_MULTILINE)
782 result |= DLGC_WANTALLKEYS;
786 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
788 int vk = (int)((LPMSG)lParam)->wParam;
790 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
792 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
793 result |= DLGC_WANTMESSAGE;
804 strng[0] = wParam >> 8;
805 strng[1] = wParam & 0xff;
806 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
807 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
808 EDIT_WM_Char(es, charW);
821 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
824 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
826 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
827 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
830 EDIT_WM_Char(es, charW);
839 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
843 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
852 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
855 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
859 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
860 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
861 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
863 result = EDIT_WM_Create(es, nameW);
864 HeapFree(GetProcessHeap(), 0, nameW);
873 es->bEnableState = (BOOL) wParam;
874 EDIT_UpdateText(es, NULL, TRUE);
878 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
882 result = (LRESULT)es->font;
886 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
889 case WM_GETTEXTLENGTH:
890 if (unicode) result = strlenW(es->text);
891 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
892 NULL, 0, NULL, NULL );
896 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
900 result = EDIT_WM_KeyDown(es, (INT)wParam);
904 result = EDIT_WM_KillFocus(es);
907 case WM_LBUTTONDBLCLK:
908 result = EDIT_WM_LButtonDblClk(es);
912 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
916 result = EDIT_WM_LButtonUp(es);
920 result = EDIT_WM_MButtonDown(es);
924 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
929 EDIT_WM_Paint(es, (HDC)wParam);
937 EDIT_WM_SetFocus(es);
941 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
945 /* FIXME: actually set an internal flag and behave accordingly */
949 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
954 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
957 case WM_STYLECHANGED:
958 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
961 case WM_STYLECHANGING:
962 result = 0; /* See EDIT_WM_StyleChanged */
966 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
974 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
979 int gcWheelDelta = 0;
980 UINT pulScrollLines = 3;
981 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
983 if (wParam & (MK_SHIFT | MK_CONTROL)) {
984 result = DefWindowProcW(hwnd, msg, wParam, lParam);
987 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
988 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
990 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
991 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
992 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
997 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1001 if (es) EDIT_UnlockBuffer(es, FALSE);
1003 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1008 /*********************************************************************
1010 * EditWndProcW (USER32.@)
1012 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1014 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1017 /*********************************************************************
1019 * EditWndProc (USER32.@)
1021 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1023 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1026 /*********************************************************************
1028 * EDIT_BuildLineDefs_ML
1030 * Build linked list of text lines.
1031 * Lines can end with '\0' (last line), a character (if it is wrapped),
1032 * a soft return '\r\r\n' or a hard return '\r\n'
1035 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1039 LPWSTR current_position, cp;
1041 LINEDEF *current_line;
1042 LINEDEF *previous_line;
1043 LINEDEF *start_line;
1044 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1045 INT line_count = es->line_count;
1046 INT orig_net_length;
1049 if (istart == iend && delta == 0)
1052 dc = GetDC(es->hwndSelf);
1054 old_font = SelectObject(dc, es->font);
1056 previous_line = NULL;
1057 current_line = es->first_line_def;
1059 /* Find starting line. istart must lie inside an existing line or
1060 * at the end of buffer */
1062 if (istart < current_line->index + current_line->length ||
1063 current_line->ending == END_0)
1066 previous_line = current_line;
1067 current_line = current_line->next;
1069 } while (current_line);
1071 if (!current_line) /* Error occurred start is not inside previous buffer */
1073 FIXME(" modification occurred outside buffer\n");
1074 ReleaseDC(es->hwndSelf, dc);
1078 /* Remember start of modifications in order to calculate update region */
1079 nstart_line = line_index;
1080 nstart_index = current_line->index;
1082 /* We must start to reformat from the previous line since the modifications
1083 * may have caused the line to wrap upwards. */
1084 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1087 current_line = previous_line;
1089 start_line = current_line;
1091 fw = es->format_rect.right - es->format_rect.left;
1092 current_position = es->text + current_line->index;
1094 if (current_line != start_line)
1096 if (!current_line || current_line->index + delta > current_position - es->text)
1098 /* The buffer has been expanded, create a new line and
1099 insert it into the link list */
1100 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1101 new_line->next = previous_line->next;
1102 previous_line->next = new_line;
1103 current_line = new_line;
1106 else if (current_line->index + delta < current_position - es->text)
1108 /* The previous line merged with this line so we delete this extra entry */
1109 previous_line->next = current_line->next;
1110 HeapFree(GetProcessHeap(), 0, current_line);
1111 current_line = previous_line->next;
1115 else /* current_line->index + delta == current_position */
1117 if (current_position - es->text > iend)
1118 break; /* We reached end of line modifications */
1119 /* else recalulate this line */
1123 current_line->index = current_position - es->text;
1124 orig_net_length = current_line->net_length;
1126 /* Find end of line */
1127 cp = current_position;
1129 if (*cp == '\n') break;
1130 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1135 /* Mark type of line termination */
1137 current_line->ending = END_0;
1138 current_line->net_length = strlenW(current_position);
1139 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1140 current_line->ending = END_SOFT;
1141 current_line->net_length = cp - current_position - 1;
1142 } else if (*cp == '\n') {
1143 current_line->ending = END_RICH;
1144 current_line->net_length = cp - current_position;
1146 current_line->ending = END_HARD;
1147 current_line->net_length = cp - current_position;
1150 /* Calculate line width */
1151 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1152 current_position, current_line->net_length,
1153 es->tabs_count, es->tabs));
1155 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1156 if (!(es->style & ES_AUTOHSCROLL)) {
1157 if (current_line->width > fw) {
1162 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1163 prev + 1, current_line->net_length, WB_RIGHT);
1164 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1165 current_position, next, es->tabs_count, es->tabs));
1166 } while (current_line->width <= fw);
1167 if (!prev) { /* Didn't find a line break so force a break */
1172 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1173 current_position, next, es->tabs_count, es->tabs));
1174 } while (current_line->width <= fw);
1179 /* If the first line we are calculating, wrapped before istart, we must
1180 * adjust istart in order for this to be reflected in the update region. */
1181 if (current_line->index == nstart_index && istart > current_line->index + prev)
1182 istart = current_line->index + prev;
1183 /* else if we are updating the previous line before the first line we
1184 * are re-calculating and it expanded */
1185 else if (current_line == start_line &&
1186 current_line->index != nstart_index && orig_net_length < prev)
1188 /* Line expanded due to an upwards line wrap so we must partially include
1189 * previous line in update region */
1190 nstart_line = line_index;
1191 nstart_index = current_line->index;
1192 istart = current_line->index + orig_net_length;
1195 current_line->net_length = prev;
1196 current_line->ending = END_WRAP;
1197 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1198 current_line->net_length, es->tabs_count, es->tabs));
1200 else if (orig_net_length < current_line->net_length &&
1201 current_line == start_line &&
1202 current_line->index != nstart_index) {
1203 /* The previous line expanded but it's still not as wide as the client rect */
1204 /* The expansion is due to an upwards line wrap so we must partially include
1205 it in the update region */
1206 nstart_line = line_index;
1207 nstart_index = current_line->index;
1208 istart = current_line->index + orig_net_length;
1213 /* Adjust length to include line termination */
1214 switch (current_line->ending) {
1216 current_line->length = current_line->net_length + 3;
1219 current_line->length = current_line->net_length + 1;
1222 current_line->length = current_line->net_length + 2;
1226 current_line->length = current_line->net_length;
1229 es->text_width = max(es->text_width, current_line->width);
1230 current_position += current_line->length;
1231 previous_line = current_line;
1232 current_line = current_line->next;
1234 } while (previous_line->ending != END_0);
1236 /* Finish adjusting line indexes by delta or remove hanging lines */
1237 if (previous_line->ending == END_0)
1239 LINEDEF *pnext = NULL;
1241 previous_line->next = NULL;
1242 while (current_line)
1244 pnext = current_line->next;
1245 HeapFree(GetProcessHeap(), 0, current_line);
1246 current_line = pnext;
1250 else if (delta != 0)
1252 while (current_line)
1254 current_line->index += delta;
1255 current_line = current_line->next;
1259 /* Calculate rest of modification rectangle */
1264 * We calculate two rectangles. One for the first line which may have
1265 * an indent with respect to the format rect. The other is a format-width
1266 * rectangle that spans the rest of the lines that changed or moved.
1268 rc.top = es->format_rect.top + nstart_line * es->line_height -
1269 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1270 rc.bottom = rc.top + es->line_height;
1271 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1272 rc.left = es->format_rect.left;
1274 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1275 es->text + nstart_index, istart - nstart_index,
1276 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1277 rc.right = es->format_rect.right;
1278 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1281 rc.left = es->format_rect.left;
1282 rc.right = es->format_rect.right;
1284 * If lines were added or removed we must re-paint the remainder of the
1285 * lines since the remaining lines were either shifted up or down.
1287 if (line_count < es->line_count) /* We added lines */
1288 rc.bottom = es->line_count * es->line_height;
1289 else if (line_count > es->line_count) /* We removed lines */
1290 rc.bottom = line_count * es->line_height;
1292 rc.bottom = line_index * es->line_height;
1293 rc.bottom += es->format_rect.top;
1294 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1295 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1296 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1297 DeleteObject(tmphrgn);
1301 SelectObject(dc, old_font);
1303 ReleaseDC(es->hwndSelf, dc);
1306 /*********************************************************************
1308 * EDIT_CalcLineWidth_SL
1311 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1318 text = EDIT_GetPasswordPointer_SL(es);
1320 dc = GetDC(es->hwndSelf);
1322 old_font = SelectObject(dc, es->font);
1324 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1327 SelectObject(dc, old_font);
1328 ReleaseDC(es->hwndSelf, dc);
1330 if (es->style & ES_PASSWORD)
1331 HeapFree(GetProcessHeap(), 0, text);
1333 es->text_width = size.cx;
1336 /*********************************************************************
1338 * EDIT_CallWordBreakProc
1340 * Call appropriate WordBreakProc (internal or external).
1342 * Note: The "start" argument should always be an index referring
1343 * to es->text. The actual wordbreak proc might be
1344 * 16 bit, so we can't always pass any 32 bit LPSTR.
1345 * Hence we assume that es->text is the buffer that holds
1346 * the string under examination (we can decide this for ourselves).
1349 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1353 if (es->word_break_proc16) {
1360 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1361 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1362 segptr = WOWGlobalLock16(hglob16);
1363 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1364 args[4] = SELECTOROF(segptr);
1365 args[3] = OFFSETOF(segptr);
1369 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1370 ret = LOWORD(result);
1371 GlobalUnlock16(hglob16);
1372 GlobalFree16(hglob16);
1374 else if (es->word_break_proc)
1378 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1380 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1381 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1382 ret = wbpW(es->text + start, index, count, action);
1386 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1390 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1391 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1392 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1393 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1394 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1395 ret = wbpA(textA, index, countA, action);
1396 HeapFree(GetProcessHeap(), 0, textA);
1400 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1406 /*********************************************************************
1410 * Beware: This is not the function called on EM_CHARFROMPOS
1411 * The position _can_ be outside the formatting / client
1413 * The return value is only the character index
1416 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1421 INT x_high = 0, x_low = 0;
1423 if (es->style & ES_MULTILINE) {
1424 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1426 LINEDEF *line_def = es->first_line_def;
1428 while ((line > 0) && line_def->next) {
1429 line_index += line_def->length;
1430 line_def = line_def->next;
1433 x += es->x_offset - es->format_rect.left;
1434 if (es->style & ES_RIGHT)
1435 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1436 else if (es->style & ES_CENTER)
1437 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1438 if (x >= line_def->width) {
1440 *after_wrap = (line_def->ending == END_WRAP);
1441 return line_index + line_def->net_length;
1445 *after_wrap = FALSE;
1448 dc = GetDC(es->hwndSelf);
1450 old_font = SelectObject(dc, es->font);
1452 high = line_index + line_def->net_length + 1;
1453 while (low < high - 1)
1455 INT mid = (low + high) / 2;
1456 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1465 if (abs(x_high - x) + 1 <= abs(x_low - x))
1471 *after_wrap = ((index == line_index + line_def->net_length) &&
1472 (line_def->ending == END_WRAP));
1477 *after_wrap = FALSE;
1478 x -= es->format_rect.left;
1480 return es->x_offset;
1484 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1485 if (es->style & ES_RIGHT)
1487 else if (es->style & ES_CENTER)
1491 text = EDIT_GetPasswordPointer_SL(es);
1492 dc = GetDC(es->hwndSelf);
1494 old_font = SelectObject(dc, es->font);
1498 INT high = es->x_offset;
1499 while (low < high - 1)
1501 INT mid = (low + high) / 2;
1502 GetTextExtentPoint32W( dc, text + mid,
1503 es->x_offset - mid, &size );
1512 if (abs(x_high + x) <= abs(x_low + x) + 1)
1519 INT low = es->x_offset;
1520 INT high = strlenW(es->text) + 1;
1521 while (low < high - 1)
1523 INT mid = (low + high) / 2;
1524 GetTextExtentPoint32W( dc, text + es->x_offset,
1525 mid - es->x_offset, &size );
1534 if (abs(x_high - x) <= abs(x_low - x) + 1)
1539 if (es->style & ES_PASSWORD)
1540 HeapFree(GetProcessHeap(), 0, text);
1543 SelectObject(dc, old_font);
1544 ReleaseDC(es->hwndSelf, dc);
1549 /*********************************************************************
1553 * adjusts the point to be within the formatting rectangle
1554 * (so CharFromPos returns the nearest _visible_ character)
1557 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1559 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1560 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1564 /*********************************************************************
1568 * Calculates the bounding rectangle for a line from a starting
1569 * column to an ending column.
1572 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1574 INT line_index = EDIT_EM_LineIndex(es, line);
1576 if (es->style & ES_MULTILINE)
1577 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1579 rc->top = es->format_rect.top;
1580 rc->bottom = rc->top + es->line_height;
1581 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1582 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1586 /*********************************************************************
1588 * EDIT_GetPasswordPointer_SL
1590 * note: caller should free the (optionally) allocated buffer
1593 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1595 if (es->style & ES_PASSWORD) {
1596 INT len = strlenW(es->text);
1597 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1599 while(len) text[--len] = es->password_char;
1606 /*********************************************************************
1610 * This acts as a LocalLock16(), but it locks only once. This way
1611 * you can call it whenever you like, without unlocking.
1613 * Initially the edit control allocates a HLOCAL32 buffer
1614 * (32 bit linear memory handler). However, 16 bit application
1615 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1616 * handler). From that moment on we have to keep using this 16 bit memory
1617 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1618 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1622 static void EDIT_LockBuffer(EDITSTATE *es)
1624 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1625 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1630 BOOL _16bit = FALSE;
1636 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1637 textA = LocalLock(es->hloc32A);
1638 countA = strlen(textA) + 1;
1642 HANDLE16 oldDS = stack16->ds;
1643 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1644 stack16->ds = hInstance;
1645 textA = MapSL(LocalLock16(es->hloc16));
1646 stack16->ds = oldDS;
1647 countA = strlen(textA) + 1;
1652 ERR("no buffer ... please report\n");
1659 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1660 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1661 if(countW_new > es->buffer_size + 1)
1663 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1664 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1665 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1668 es->hloc32W = hloc32W_new;
1669 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1670 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1673 WARN("FAILED! Will synchronize partially\n");
1677 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1678 es->text = LocalLock(es->hloc32W);
1682 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1685 HANDLE16 oldDS = stack16->ds;
1686 stack16->ds = hInstance;
1687 LocalUnlock16(es->hloc16);
1688 stack16->ds = oldDS;
1691 LocalUnlock(es->hloc32A);
1698 /*********************************************************************
1700 * EDIT_SL_InvalidateText
1702 * Called from EDIT_InvalidateText().
1703 * Does the job for single-line controls only.
1706 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1711 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1712 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1713 EDIT_UpdateText(es, &rc, TRUE);
1717 /*********************************************************************
1719 * EDIT_ML_InvalidateText
1721 * Called from EDIT_InvalidateText().
1722 * Does the job for multi-line controls only.
1725 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1727 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1728 INT sl = EDIT_EM_LineFromChar(es, start);
1729 INT el = EDIT_EM_LineFromChar(es, end);
1738 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1741 sc = start - EDIT_EM_LineIndex(es, sl);
1742 ec = end - EDIT_EM_LineIndex(es, el);
1743 if (sl < es->y_offset) {
1747 if (el > es->y_offset + vlc) {
1748 el = es->y_offset + vlc;
1749 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1751 GetClientRect(es->hwndSelf, &rc1);
1752 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1754 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1755 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1756 EDIT_UpdateText(es, &rcUpdate, TRUE);
1758 EDIT_GetLineRect(es, sl, sc,
1759 EDIT_EM_LineLength(es,
1760 EDIT_EM_LineIndex(es, sl)),
1762 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1763 EDIT_UpdateText(es, &rcUpdate, TRUE);
1764 for (l = sl + 1 ; l < el ; l++) {
1765 EDIT_GetLineRect(es, l, 0,
1766 EDIT_EM_LineLength(es,
1767 EDIT_EM_LineIndex(es, l)),
1769 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1770 EDIT_UpdateText(es, &rcUpdate, TRUE);
1772 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1773 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1774 EDIT_UpdateText(es, &rcUpdate, TRUE);
1779 /*********************************************************************
1781 * EDIT_InvalidateText
1783 * Invalidate the text from offset start up to, but not including,
1784 * offset end. Useful for (re)painting the selection.
1785 * Regions outside the linewidth are not invalidated.
1786 * end == -1 means end == TextLength.
1787 * start and end need not be ordered.
1790 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1796 end = strlenW(es->text);
1804 if (es->style & ES_MULTILINE)
1805 EDIT_ML_InvalidateText(es, start, end);
1807 EDIT_SL_InvalidateText(es, start, end);
1811 /*********************************************************************
1815 * Try to fit size + 1 characters in the buffer.
1817 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1821 if (size <= es->buffer_size)
1824 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1826 /* Force edit to unlock it's buffer. es->text now NULL */
1827 EDIT_UnlockBuffer(es, TRUE);
1830 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1831 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1832 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1833 es->hloc32W = hNew32W;
1834 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1838 EDIT_LockBuffer(es);
1840 if (es->buffer_size < size) {
1841 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1842 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1845 TRACE("We now have %d+1\n", es->buffer_size);
1851 /*********************************************************************
1855 * Try to fit size + 1 bytes in the undo buffer.
1858 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1862 if (size <= es->undo_buffer_size)
1865 TRACE("trying to ReAlloc to %d+1\n", size);
1867 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1868 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1869 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1874 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1880 /*********************************************************************
1885 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1887 INT e = es->selection_end;
1891 if ((es->style & ES_MULTILINE) && e &&
1892 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1894 if (e && (es->text[e - 1] == '\r'))
1898 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1899 EDIT_EM_ScrollCaret(es);
1903 /*********************************************************************
1907 * Only for multi line controls
1908 * Move the caret one line down, on a column with the nearest
1909 * x coordinate on the screen (might be a different column).
1912 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1914 INT s = es->selection_start;
1915 INT e = es->selection_end;
1916 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1917 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1918 INT x = (short)LOWORD(pos);
1919 INT y = (short)HIWORD(pos);
1921 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1924 EDIT_EM_SetSel(es, s, e, after_wrap);
1925 EDIT_EM_ScrollCaret(es);
1929 /*********************************************************************
1934 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1936 BOOL after_wrap = FALSE;
1939 /* Pass a high value in x to make sure of receiving the end of the line */
1940 if (es->style & ES_MULTILINE)
1941 e = EDIT_CharFromPos(es, 0x3fffffff,
1942 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1944 e = strlenW(es->text);
1945 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1946 EDIT_EM_ScrollCaret(es);
1950 /*********************************************************************
1955 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1957 INT e = es->selection_end;
1961 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1962 if (es->text[e] == '\n')
1964 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1968 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1969 EDIT_EM_ScrollCaret(es);
1973 /*********************************************************************
1977 * Home key: move to beginning of line.
1980 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1984 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1985 if (es->style & ES_MULTILINE)
1986 e = EDIT_CharFromPos(es, -es->x_offset,
1987 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1990 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1991 EDIT_EM_ScrollCaret(es);
1995 /*********************************************************************
1997 * EDIT_MovePageDown_ML
1999 * Only for multi line controls
2000 * Move the caret one page down, on a column with the nearest
2001 * x coordinate on the screen (might be a different column).
2004 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2006 INT s = es->selection_start;
2007 INT e = es->selection_end;
2008 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2009 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2010 INT x = (short)LOWORD(pos);
2011 INT y = (short)HIWORD(pos);
2013 e = EDIT_CharFromPos(es, x,
2014 y + (es->format_rect.bottom - es->format_rect.top),
2018 EDIT_EM_SetSel(es, s, e, after_wrap);
2019 EDIT_EM_ScrollCaret(es);
2023 /*********************************************************************
2025 * EDIT_MovePageUp_ML
2027 * Only for multi line controls
2028 * Move the caret one page up, on a column with the nearest
2029 * x coordinate on the screen (might be a different column).
2032 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2034 INT s = es->selection_start;
2035 INT e = es->selection_end;
2036 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2037 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2038 INT x = (short)LOWORD(pos);
2039 INT y = (short)HIWORD(pos);
2041 e = EDIT_CharFromPos(es, x,
2042 y - (es->format_rect.bottom - es->format_rect.top),
2046 EDIT_EM_SetSel(es, s, e, after_wrap);
2047 EDIT_EM_ScrollCaret(es);
2051 /*********************************************************************
2055 * Only for multi line controls
2056 * Move the caret one line up, on a column with the nearest
2057 * x coordinate on the screen (might be a different column).
2060 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2062 INT s = es->selection_start;
2063 INT e = es->selection_end;
2064 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2065 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2066 INT x = (short)LOWORD(pos);
2067 INT y = (short)HIWORD(pos);
2069 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2072 EDIT_EM_SetSel(es, s, e, after_wrap);
2073 EDIT_EM_ScrollCaret(es);
2077 /*********************************************************************
2079 * EDIT_MoveWordBackward
2082 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2084 INT s = es->selection_start;
2085 INT e = es->selection_end;
2090 l = EDIT_EM_LineFromChar(es, e);
2091 ll = EDIT_EM_LineLength(es, e);
2092 li = EDIT_EM_LineIndex(es, l);
2095 li = EDIT_EM_LineIndex(es, l - 1);
2096 e = li + EDIT_EM_LineLength(es, li);
2099 e = li + (INT)EDIT_CallWordBreakProc(es,
2100 li, e - li, ll, WB_LEFT);
2104 EDIT_EM_SetSel(es, s, e, FALSE);
2105 EDIT_EM_ScrollCaret(es);
2109 /*********************************************************************
2111 * EDIT_MoveWordForward
2114 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2116 INT s = es->selection_start;
2117 INT e = es->selection_end;
2122 l = EDIT_EM_LineFromChar(es, e);
2123 ll = EDIT_EM_LineLength(es, e);
2124 li = EDIT_EM_LineIndex(es, l);
2126 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2127 e = EDIT_EM_LineIndex(es, l + 1);
2129 e = li + EDIT_CallWordBreakProc(es,
2130 li, e - li + 1, ll, WB_RIGHT);
2134 EDIT_EM_SetSel(es, s, e, FALSE);
2135 EDIT_EM_ScrollCaret(es);
2139 /*********************************************************************
2144 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2146 INT s = es->selection_start;
2147 INT e = es->selection_end;
2154 if (es->style & ES_MULTILINE) {
2155 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2156 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2161 TRACE("line=%d\n", line);
2163 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2164 x = (short)LOWORD(pos);
2165 y = (short)HIWORD(pos);
2166 li = EDIT_EM_LineIndex(es, line);
2167 ll = EDIT_EM_LineLength(es, li);
2168 s = min(es->selection_start, es->selection_end);
2169 e = max(es->selection_start, es->selection_end);
2170 s = min(li + ll, max(li, s));
2171 e = min(li + ll, max(li, e));
2172 if (rev && (s != e) &&
2173 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2174 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2175 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2176 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2178 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2182 /*********************************************************************
2187 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2198 BkMode = GetBkMode(dc);
2199 BkColor = GetBkColor(dc);
2200 TextColor = GetTextColor(dc);
2202 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2203 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2204 SetBkMode( dc, OPAQUE);
2206 li = EDIT_EM_LineIndex(es, line);
2207 if (es->style & ES_MULTILINE) {
2208 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2209 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2211 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2212 TextOutW(dc, x, y, text + li + col, count);
2213 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2215 if (es->style & ES_PASSWORD)
2216 HeapFree(GetProcessHeap(), 0, text);
2219 SetBkColor(dc, BkColor);
2220 SetTextColor(dc, TextColor);
2221 SetBkMode( dc, BkMode);
2227 /*********************************************************************
2232 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2235 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2236 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2237 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2241 /*********************************************************************
2243 * EDIT_AdjustFormatRect
2245 * Adjusts the format rectangle for the current font and the
2246 * current client rectangle.
2249 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2253 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2254 if (es->style & ES_MULTILINE)
2256 INT fw, vlc, max_x_offset, max_y_offset;
2258 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2259 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2261 /* correct es->x_offset */
2262 fw = es->format_rect.right - es->format_rect.left;
2263 max_x_offset = es->text_width - fw;
2264 if(max_x_offset < 0) max_x_offset = 0;
2265 if(es->x_offset > max_x_offset)
2266 es->x_offset = max_x_offset;
2268 /* correct es->y_offset */
2269 max_y_offset = es->line_count - vlc;
2270 if(max_y_offset < 0) max_y_offset = 0;
2271 if(es->y_offset > max_y_offset)
2272 es->y_offset = max_y_offset;
2274 /* force scroll info update */
2275 EDIT_UpdateScrollInfo(es);
2278 /* Windows doesn't care to fix text placement for SL controls */
2279 es->format_rect.bottom = es->format_rect.top + es->line_height;
2281 /* Always stay within the client area */
2282 GetClientRect(es->hwndSelf, &ClientRect);
2283 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2285 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2286 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2288 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2292 /*********************************************************************
2296 * note: this is not (exactly) the handler called on EM_SETRECTNP
2297 * it is also used to set the rect of a single line control
2300 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2304 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2306 CopyRect(&es->format_rect, rc);
2308 if (ExStyle & WS_EX_CLIENTEDGE) {
2309 es->format_rect.left++;
2310 es->format_rect.right--;
2312 if (es->format_rect.bottom - es->format_rect.top
2313 >= es->line_height + 2)
2315 es->format_rect.top++;
2316 es->format_rect.bottom--;
2319 else if (es->style & WS_BORDER) {
2320 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2321 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2322 es->format_rect.left += bw;
2323 es->format_rect.right -= bw;
2324 if (es->format_rect.bottom - es->format_rect.top
2325 >= es->line_height + 2 * bh)
2327 es->format_rect.top += bh;
2328 es->format_rect.bottom -= bh;
2332 es->format_rect.left += es->left_margin;
2333 es->format_rect.right -= es->right_margin;
2334 EDIT_AdjustFormatRect(es);
2338 /*********************************************************************
2343 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2346 /* Edit window might be already destroyed */
2347 if(!IsWindow(es->hwndSelf))
2349 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2353 if (!es->lock_count) {
2354 ERR("lock_count == 0 ... please report\n");
2358 ERR("es->text == 0 ... please report\n");
2362 if (force || (es->lock_count == 1)) {
2366 UINT countW = strlenW(es->text) + 1;
2367 STACK16FRAME* stack16 = NULL;
2372 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2373 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2374 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2375 countA = LocalSize(es->hloc32A);
2376 if(countA_new > countA)
2379 UINT alloc_size = ROUND_TO_GROW(countA_new);
2380 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2381 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2384 es->hloc32A = hloc32A_new;
2385 countA = LocalSize(hloc32A_new);
2386 TRACE("Real new size %d bytes\n", countA);
2389 WARN("FAILED! Will synchronize partially\n");
2391 textA = LocalLock(es->hloc32A);
2395 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2397 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2398 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2400 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2401 oldDS = stack16->ds;
2402 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2404 countA = LocalSize16(es->hloc16);
2405 if(countA_new > countA)
2407 HLOCAL16 hloc16_new;
2408 UINT alloc_size = ROUND_TO_GROW(countA_new);
2409 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2410 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2413 es->hloc16 = hloc16_new;
2414 countA = LocalSize16(hloc16_new);
2415 TRACE("Real new size %d bytes\n", countA);
2418 WARN("FAILED! Will synchronize partially\n");
2420 textA = MapSL(LocalLock16(es->hloc16));
2425 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2427 LocalUnlock16(es->hloc16);
2429 LocalUnlock(es->hloc32A);
2432 if (stack16) stack16->ds = oldDS;
2433 LocalUnlock(es->hloc32W);
2437 ERR("no buffer ... please report\n");
2445 /*********************************************************************
2447 * EDIT_UpdateScrollInfo
2450 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2452 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2455 si.cbSize = sizeof(SCROLLINFO);
2456 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2458 si.nMax = es->line_count - 1;
2459 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2460 si.nPos = es->y_offset;
2461 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2462 si.nMin, si.nMax, si.nPage, si.nPos);
2463 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2466 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2469 si.cbSize = sizeof(SCROLLINFO);
2470 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2472 si.nMax = es->text_width - 1;
2473 si.nPage = es->format_rect.right - es->format_rect.left;
2474 si.nPos = es->x_offset;
2475 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2476 si.nMin, si.nMax, si.nPage, si.nPos);
2477 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2481 /*********************************************************************
2483 * EDIT_WordBreakProc
2485 * Find the beginning of words.
2486 * Note: unlike the specs for a WordBreakProc, this function only
2487 * allows to be called without linebreaks between s[0] up to
2488 * s[count - 1]. Remember it is only called
2489 * internally, so we can decide this for ourselves.
2492 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2496 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2506 if (s[index] == ' ') {
2507 while (index && (s[index] == ' '))
2510 while (index && (s[index] != ' '))
2512 if (s[index] == ' ')
2516 while (index && (s[index] != ' '))
2518 if (s[index] == ' ')
2528 if (s[index] == ' ')
2529 while ((index < count) && (s[index] == ' ')) index++;
2531 while (s[index] && (s[index] != ' ') && (index < count))
2533 while ((s[index] == ' ') && (index < count)) index++;
2537 case WB_ISDELIMITER:
2538 ret = (s[index] == ' ');
2541 ERR("unknown action code, please report !\n");
2548 /*********************************************************************
2552 * returns line number (not index) in high-order word of result.
2553 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2554 * to Richedit, not to the edit control. Original documentation is valid.
2555 * FIXME: do the specs mean to return -1 if outside client area or
2556 * if outside formatting rectangle ???
2559 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2567 GetClientRect(es->hwndSelf, &rc);
2568 if (!PtInRect(&rc, pt))
2571 index = EDIT_CharFromPos(es, x, y, NULL);
2572 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2576 /*********************************************************************
2580 * Enable or disable soft breaks.
2582 * This means: insert or remove the soft linebreak character (\r\r\n).
2583 * Take care to check if the text still fits the buffer after insertion.
2584 * If not, notify with EN_ERRSPACE.
2587 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2589 es->flags &= ~EF_USE_SOFTBRK;
2591 es->flags |= EF_USE_SOFTBRK;
2592 FIXME("soft break enabled, not implemented\n");
2598 /*********************************************************************
2602 * Hopefully this won't fire back at us.
2603 * We always start with a fixed buffer in the local heap.
2604 * Despite of the documentation says that the local heap is used
2605 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2606 * buffer on the local heap.
2609 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2613 if (!(es->style & ES_MULTILINE))
2617 hLocal = es->hloc32W;
2623 UINT countA, alloc_size;
2624 TRACE("Allocating 32-bit ANSI alias buffer\n");
2625 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2626 alloc_size = ROUND_TO_GROW(countA);
2627 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2629 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2632 textA = LocalLock(es->hloc32A);
2633 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2634 LocalUnlock(es->hloc32A);
2636 hLocal = es->hloc32A;
2639 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2644 /*********************************************************************
2648 * Hopefully this won't fire back at us.
2649 * We always start with a buffer in 32 bit linear memory.
2650 * However, with this message a 16 bit application requests
2651 * a handle of 16 bit local heap memory, where it expects to find
2653 * It's a pitty that from this moment on we have to use this
2654 * local heap, because applications may rely on the handle
2657 * In this function we'll try to switch to local heap.
2659 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2662 UINT countA, alloc_size;
2663 STACK16FRAME* stack16;
2666 if (!(es->style & ES_MULTILINE))
2672 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2673 oldDS = stack16->ds;
2674 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2676 if (!LocalHeapSize16()) {
2678 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2679 ERR("could not initialize local heap\n");
2682 TRACE("local heap initialized\n");
2685 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2686 alloc_size = ROUND_TO_GROW(countA);
2688 TRACE("Allocating 16-bit ANSI alias buffer\n");
2689 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2690 ERR("could not allocate new 16 bit buffer\n");
2694 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2695 ERR("could not lock new 16 bit buffer\n");
2696 LocalFree16(es->hloc16);
2701 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2702 LocalUnlock16(es->hloc16);
2704 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2707 stack16->ds = oldDS;
2712 /*********************************************************************
2717 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2720 INT line_len, dst_len;
2723 if (es->style & ES_MULTILINE) {
2724 if (line >= es->line_count)
2728 i = EDIT_EM_LineIndex(es, line);
2730 line_len = EDIT_EM_LineLength(es, i);
2731 dst_len = *(WORD *)dst;
2734 if(dst_len <= line_len)
2736 memcpy(dst, src, dst_len * sizeof(WCHAR));
2739 else /* Append 0 if enough space */
2741 memcpy(dst, src, line_len * sizeof(WCHAR));
2748 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2749 if(!ret && line_len) /* Insufficient buffer size */
2751 if(ret < dst_len) /* Append 0 if enough space */
2752 ((LPSTR)dst)[ret] = 0;
2758 /*********************************************************************
2763 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2765 UINT s = es->selection_start;
2766 UINT e = es->selection_end;
2773 return MAKELONG(s, e);
2777 /*********************************************************************
2781 * FIXME: is this right ? (or should it be only VSCROLL)
2782 * (and maybe only for edit controls that really have their
2783 * own scrollbars) (and maybe only for multiline controls ?)
2784 * All in all: very poorly documented
2787 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2789 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2790 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2794 /*********************************************************************
2799 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2804 if (!(es->style & ES_MULTILINE))
2806 if (index > (INT)strlenW(es->text))
2807 return es->line_count - 1;
2809 index = min(es->selection_start, es->selection_end);
2812 line_def = es->first_line_def;
2813 index -= line_def->length;
2814 while ((index >= 0) && line_def->next) {
2816 line_def = line_def->next;
2817 index -= line_def->length;
2823 /*********************************************************************
2828 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2833 if (!(es->style & ES_MULTILINE))
2835 if (line >= es->line_count)
2839 line_def = es->first_line_def;
2841 INT index = es->selection_end - line_def->length;
2842 while ((index >= 0) && line_def->next) {
2843 line_index += line_def->length;
2844 line_def = line_def->next;
2845 index -= line_def->length;
2849 line_index += line_def->length;
2850 line_def = line_def->next;
2858 /*********************************************************************
2863 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2867 if (!(es->style & ES_MULTILINE))
2868 return strlenW(es->text);
2871 /* get the number of remaining non-selected chars of selected lines */
2872 INT32 l; /* line number */
2873 INT32 li; /* index of first char in line */
2875 l = EDIT_EM_LineFromChar(es, es->selection_start);
2876 /* # chars before start of selection area */
2877 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2878 l = EDIT_EM_LineFromChar(es, es->selection_end);
2879 /* # chars after end of selection */
2880 li = EDIT_EM_LineIndex(es, l);
2881 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2884 line_def = es->first_line_def;
2885 index -= line_def->length;
2886 while ((index >= 0) && line_def->next) {
2887 line_def = line_def->next;
2888 index -= line_def->length;
2890 return line_def->net_length;
2894 /*********************************************************************
2898 * NOTE: dx is in average character widths, dy - in lines;
2901 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2903 if (!(es->style & ES_MULTILINE))
2906 dx *= es->char_width;
2907 return EDIT_EM_LineScroll_internal(es, dx, dy);
2910 /*********************************************************************
2912 * EDIT_EM_LineScroll_internal
2914 * Version of EDIT_EM_LineScroll for internal use.
2915 * It doesn't refuse if ES_MULTILINE is set and assumes that
2916 * dx is in pixels, dy - in lines.
2919 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2922 INT x_offset_in_pixels;
2923 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2926 if (es->style & ES_MULTILINE)
2928 x_offset_in_pixels = es->x_offset;
2933 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2936 if (-dx > x_offset_in_pixels)
2937 dx = -x_offset_in_pixels;
2938 if (dx > es->text_width - x_offset_in_pixels)
2939 dx = es->text_width - x_offset_in_pixels;
2940 nyoff = max(0, es->y_offset + dy);
2941 if (nyoff >= es->line_count - lines_per_page)
2942 nyoff = max(0, es->line_count - lines_per_page);
2943 dy = (es->y_offset - nyoff) * es->line_height;
2948 es->y_offset = nyoff;
2949 if(es->style & ES_MULTILINE)
2952 es->x_offset += dx / es->char_width;
2954 GetClientRect(es->hwndSelf, &rc1);
2955 IntersectRect(&rc, &rc1, &es->format_rect);
2956 ScrollWindowEx(es->hwndSelf, -dx, dy,
2957 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2958 /* force scroll info update */
2959 EDIT_UpdateScrollInfo(es);
2961 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2962 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
2963 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2964 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
2969 /*********************************************************************
2974 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2976 INT len = strlenW(es->text);
2989 index = min(index, len);
2990 dc = GetDC(es->hwndSelf);
2992 old_font = SelectObject(dc, es->font);
2993 if (es->style & ES_MULTILINE) {
2994 l = EDIT_EM_LineFromChar(es, index);
2995 y = (l - es->y_offset) * es->line_height;
2996 li = EDIT_EM_LineIndex(es, l);
2997 if (after_wrap && (li == index) && l) {
2999 line_def = es->first_line_def;
3001 line_def = line_def->next;
3004 if (line_def->ending == END_WRAP) {
3006 y -= es->line_height;
3007 li = EDIT_EM_LineIndex(es, l);
3011 line_def = es->first_line_def;
3012 while (line_def->index != li)
3013 line_def = line_def->next;
3015 ll = line_def->net_length;
3016 lw = line_def->width;
3018 w = es->format_rect.right - es->format_rect.left;
3019 if (es->style & ES_RIGHT)
3021 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3022 es->tabs_count, es->tabs)) - es->x_offset;
3025 else if (es->style & ES_CENTER)
3027 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3028 es->tabs_count, es->tabs)) - es->x_offset;
3033 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3034 es->tabs_count, es->tabs)) - es->x_offset;
3037 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3038 if (index < es->x_offset) {
3039 GetTextExtentPoint32W(dc, text + index,
3040 es->x_offset - index, &size);
3043 GetTextExtentPoint32W(dc, text + es->x_offset,
3044 index - es->x_offset, &size);
3047 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3049 w = es->format_rect.right - es->format_rect.left;
3050 if (w > es->text_width)
3052 if (es->style & ES_RIGHT)
3053 x += w - es->text_width;
3054 else if (es->style & ES_CENTER)
3055 x += (w - es->text_width) / 2;
3060 if (es->style & ES_PASSWORD)
3061 HeapFree(GetProcessHeap(), 0, text);
3063 x += es->format_rect.left;
3064 y += es->format_rect.top;
3066 SelectObject(dc, old_font);
3067 ReleaseDC(es->hwndSelf, dc);
3068 return MAKELONG((INT16)x, (INT16)y);
3072 /*********************************************************************
3076 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3079 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3081 UINT strl = strlenW(lpsz_replace);
3082 UINT tl = strlenW(es->text);
3093 TRACE("%s, can_undo %d, send_update %d\n",
3094 debugstr_w(lpsz_replace), can_undo, send_update);
3096 s = es->selection_start;
3097 e = es->selection_end;
3099 if ((s == e) && !strl)
3104 size = tl - (e - s) + strl;
3108 /* Issue the EN_MAXTEXT notification and continue with replacing text
3109 * such that buffer limit is honored. */
3110 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3111 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3112 strl = es->buffer_limit - (tl - (e-s));
3115 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3119 /* there is something to be deleted */
3120 TRACE("deleting stuff.\n");
3122 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3124 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3125 buf[bufl] = 0; /* ensure 0 termination */
3127 strcpyW(es->text + s, es->text + e);
3130 /* there is an insertion */
3131 tl = strlenW(es->text);
3132 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));
3133 for (p = es->text + tl ; p >= es->text + s ; p--)
3135 for (i = 0 , p = es->text + s ; i < strl ; i++)
3136 p[i] = lpsz_replace[i];
3137 if(es->style & ES_UPPERCASE)
3138 CharUpperBuffW(p, strl);
3139 else if(es->style & ES_LOWERCASE)
3140 CharLowerBuffW(p, strl);
3142 if (es->style & ES_MULTILINE)
3144 INT st = min(es->selection_start, es->selection_end);
3145 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3147 hrgn = CreateRectRgn(0, 0, 0, 0);
3148 EDIT_BuildLineDefs_ML(es, st, st + strl,
3149 strl - abs(es->selection_end - es->selection_start), hrgn);
3150 /* if text is too long undo all changes */
3151 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3153 strcpyW(es->text + e, es->text + e + strl);
3155 for (i = 0 , p = es->text ; i < e - s ; i++)
3157 EDIT_BuildLineDefs_ML(es, s, e,
3158 abs(es->selection_end - es->selection_start) - strl, hrgn);
3161 hrgn = CreateRectRgn(0, 0, 0, 0);
3162 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3166 INT fw = es->format_rect.right - es->format_rect.left;
3167 EDIT_CalcLineWidth_SL(es);
3168 /* remove chars that don't fit */
3169 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3170 while ((es->text_width > fw) && s + strl >= s) {
3171 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3173 EDIT_CalcLineWidth_SL(es);
3175 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3181 utl = strlenW(es->undo_text);
3182 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3183 /* undo-buffer is extended to the right */
3184 EDIT_MakeUndoFit(es, utl + e - s);
3185 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3186 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3187 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3188 /* undo-buffer is extended to the left */
3189 EDIT_MakeUndoFit(es, utl + e - s);
3190 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3192 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3194 es->undo_position = s;
3196 /* new undo-buffer */
3197 EDIT_MakeUndoFit(es, e - s);
3198 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3199 es->undo_text[e - s] = 0; /* ensure 0 termination */
3200 es->undo_position = s;
3202 /* any deletion makes the old insertion-undo invalid */
3203 es->undo_insert_count = 0;
3205 EDIT_EM_EmptyUndoBuffer(es);
3209 if ((s == es->undo_position) ||
3210 ((es->undo_insert_count) &&
3211 (s == es->undo_position + es->undo_insert_count)))
3213 * insertion is new and at delete position or
3214 * an extension to either left or right
3216 es->undo_insert_count += strl;
3218 /* new insertion undo */
3219 es->undo_position = s;
3220 es->undo_insert_count = strl;
3221 /* new insertion makes old delete-buffer invalid */
3222 *es->undo_text = '\0';
3225 EDIT_EM_EmptyUndoBuffer(es);
3229 HeapFree(GetProcessHeap(), 0, buf);
3233 /* If text has been deleted and we're right or center aligned then scroll rightward */
3234 if (es->style & (ES_RIGHT | ES_CENTER))
3236 INT delta = strl - abs(es->selection_end - es->selection_start);
3238 if (delta < 0 && es->x_offset)
3240 if (abs(delta) > es->x_offset)
3243 es->x_offset += delta;
3247 EDIT_EM_SetSel(es, s, s, FALSE);
3248 es->flags |= EF_MODIFIED;
3249 if (send_update) es->flags |= EF_UPDATE;
3252 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3256 EDIT_UpdateText(es, NULL, TRUE);
3258 EDIT_EM_ScrollCaret(es);
3260 /* force scroll info update */
3261 EDIT_UpdateScrollInfo(es);
3264 if(send_update || (es->flags & EF_UPDATE))
3266 es->flags &= ~EF_UPDATE;
3267 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3272 /*********************************************************************
3277 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3281 if (!(es->style & ES_MULTILINE))
3282 return (LRESULT)FALSE;
3292 if (es->y_offset < es->line_count - 1)
3297 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3300 if (es->y_offset < es->line_count - 1)
3301 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3304 return (LRESULT)FALSE;
3307 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3308 /* check if we are going to move too far */
3309 if(es->y_offset + dy > es->line_count - vlc)
3310 dy = es->line_count - vlc - es->y_offset;
3312 /* Notification is done in EDIT_EM_LineScroll */
3314 EDIT_EM_LineScroll(es, 0, dy);
3316 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3320 /*********************************************************************
3325 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3327 if (es->style & ES_MULTILINE) {
3332 INT cw = es->char_width;
3337 l = EDIT_EM_LineFromChar(es, es->selection_end);
3338 li = EDIT_EM_LineIndex(es, l);
3339 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3340 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3341 if (l >= es->y_offset + vlc)
3342 dy = l - vlc + 1 - es->y_offset;
3343 if (l < es->y_offset)
3344 dy = l - es->y_offset;
3345 ww = es->format_rect.right - es->format_rect.left;
3346 if (x < es->format_rect.left)
3347 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3348 if (x > es->format_rect.right)
3349 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3350 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3352 /* check if we are going to move too far */
3353 if(es->x_offset + dx + ww > es->text_width)
3354 dx = es->text_width - ww - es->x_offset;
3355 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3356 EDIT_EM_LineScroll_internal(es, dx, dy);
3363 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3364 format_width = es->format_rect.right - es->format_rect.left;
3365 if (x < es->format_rect.left) {
3366 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3369 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3370 } while ((x < goal) && es->x_offset);
3371 /* FIXME: use ScrollWindow() somehow to improve performance */
3372 EDIT_UpdateText(es, NULL, TRUE);
3373 } else if (x > es->format_rect.right) {
3375 INT len = strlenW(es->text);
3376 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3379 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3380 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3381 } while ((x > goal) && (x_last > es->format_rect.right));
3382 /* FIXME: use ScrollWindow() somehow to improve performance */
3383 EDIT_UpdateText(es, NULL, TRUE);
3387 if(es->flags & EF_FOCUSED)
3388 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3392 /*********************************************************************
3396 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3399 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3401 if (!(es->style & ES_MULTILINE))
3405 WARN("called with NULL handle\n");
3409 EDIT_UnlockBuffer(es, TRUE);
3413 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3414 HANDLE16 oldDS = stack16->ds;
3416 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3417 LocalFree16(es->hloc16);
3418 stack16->ds = oldDS;
3426 LocalFree(es->hloc32A);
3438 countA = LocalSize(hloc);
3439 textA = LocalLock(hloc);
3440 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3441 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3443 ERR("Could not allocate new unicode buffer\n");
3446 textW = LocalLock(hloc32W_new);
3447 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3448 LocalUnlock(hloc32W_new);
3452 LocalFree(es->hloc32W);
3454 es->hloc32W = hloc32W_new;
3458 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3460 EDIT_LockBuffer(es);
3462 es->x_offset = es->y_offset = 0;
3463 es->selection_start = es->selection_end = 0;
3464 EDIT_EM_EmptyUndoBuffer(es);
3465 es->flags &= ~EF_MODIFIED;
3466 es->flags &= ~EF_UPDATE;
3467 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3468 EDIT_UpdateText(es, NULL, TRUE);
3469 EDIT_EM_ScrollCaret(es);
3470 /* force scroll info update */
3471 EDIT_UpdateScrollInfo(es);
3475 /*********************************************************************
3479 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3482 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3484 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3485 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3486 HANDLE16 oldDS = stack16->ds;
3492 if (!(es->style & ES_MULTILINE))
3496 WARN("called with NULL handle\n");
3500 EDIT_UnlockBuffer(es, TRUE);
3504 LocalFree(es->hloc32A);
3508 stack16->ds = hInstance;
3509 countA = LocalSize16(hloc);
3510 textA = MapSL(LocalLock16(hloc));
3511 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3512 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3514 ERR("Could not allocate new unicode buffer\n");
3517 textW = LocalLock(hloc32W_new);
3518 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3519 LocalUnlock(hloc32W_new);
3520 LocalUnlock16(hloc);
3521 stack16->ds = oldDS;
3524 LocalFree(es->hloc32W);
3526 es->hloc32W = hloc32W_new;
3529 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3531 EDIT_LockBuffer(es);
3533 es->x_offset = es->y_offset = 0;
3534 es->selection_start = es->selection_end = 0;
3535 EDIT_EM_EmptyUndoBuffer(es);
3536 es->flags &= ~EF_MODIFIED;
3537 es->flags &= ~EF_UPDATE;
3538 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3539 EDIT_UpdateText(es, NULL, TRUE);
3540 EDIT_EM_ScrollCaret(es);
3541 /* force scroll info update */
3542 EDIT_UpdateScrollInfo(es);
3546 /*********************************************************************
3550 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3551 * However, the windows version is not complied to yet in all of edit.c
3553 * Additionally as the wrapper for RichEdit controls we need larger buffers
3554 * at present -1 will represent nolimit
3556 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3558 if (limit == 0xFFFFFFFF)
3559 es->buffer_limit = -1;
3560 else if (es->style & ES_MULTILINE) {
3562 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3564 es->buffer_limit = BUFLIMIT_MULTI;
3567 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3569 es->buffer_limit = BUFLIMIT_SINGLE;
3574 /*********************************************************************
3578 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3579 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3580 * margin according to the textmetrics of the current font.
3582 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3583 * of the char's width as the margin, but this is not how Windows handles this.
3584 * For all other fonts Windows sets the margins to zero.
3587 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3588 INT left, INT right, BOOL repaint)
3591 INT default_left_margin = 0; /* in pixels */
3592 INT default_right_margin = 0; /* in pixels */
3594 /* Set the default margins depending on the font */
3595 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3596 HDC dc = GetDC(es->hwndSelf);
3597 HFONT old_font = SelectObject(dc, es->font);
3598 GetTextMetricsW(dc, &tm);
3599 /* The default margins are only non zero for TrueType or Vector fonts */
3600 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3601 /* This must be calculated more exactly! But how? */
3602 default_left_margin = tm.tmAveCharWidth / 3;
3603 default_right_margin = tm.tmAveCharWidth / 3;
3605 SelectObject(dc, old_font);
3606 ReleaseDC(es->hwndSelf, dc);
3609 if (action & EC_LEFTMARGIN) {
3610 es->format_rect.left -= es->left_margin;
3611 if (left != EC_USEFONTINFO)
3612 es->left_margin = left;
3614 es->left_margin = default_left_margin;
3615 es->format_rect.left += es->left_margin;
3618 if (action & EC_RIGHTMARGIN) {
3619 es->format_rect.right += es->right_margin;
3620 if (right != EC_USEFONTINFO)
3621 es->right_margin = right;
3623 es->right_margin = default_right_margin;
3624 es->format_rect.right -= es->right_margin;
3627 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3628 EDIT_AdjustFormatRect(es);
3629 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3632 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3636 /*********************************************************************
3638 * EM_SETPASSWORDCHAR
3641 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3645 if (es->style & ES_MULTILINE)
3648 if (es->password_char == c)
3651 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3652 es->password_char = c;
3654 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3655 es->style |= ES_PASSWORD;
3657 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3658 es->style &= ~ES_PASSWORD;
3660 EDIT_UpdateText(es, NULL, TRUE);
3664 /*********************************************************************
3668 * note: unlike the specs say: the order of start and end
3669 * _is_ preserved in Windows. (i.e. start can be > end)
3670 * In other words: this handler is OK
3673 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3675 UINT old_start = es->selection_start;
3676 UINT old_end = es->selection_end;
3677 UINT len = strlenW(es->text);
3679 if (start == (UINT)-1) {
3680 start = es->selection_end;
3681 end = es->selection_end;
3683 start = min(start, len);
3684 end = min(end, len);
3686 es->selection_start = start;
3687 es->selection_end = end;
3689 es->flags |= EF_AFTER_WRAP;
3691 es->flags &= ~EF_AFTER_WRAP;
3692 /* Compute the necessary invalidation region. */
3693 /* Note that we don't need to invalidate regions which have
3694 * "never" been selected, or those which are "still" selected.
3695 * In fact, every time we hit a selection boundary, we can
3696 * *toggle* whether we need to invalidate. Thus we can optimize by
3697 * *sorting* the interval endpoints. Let's assume that we sort them
3699 * start <= end <= old_start <= old_end
3700 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3701 * in 5 comparisons; ie it's impossible to do better than the
3703 ORDER_UINT(end, old_end);
3704 ORDER_UINT(start, old_start);
3705 ORDER_UINT(old_start, old_end);
3706 ORDER_UINT(start, end);
3707 /* Note that at this point 'end' and 'old_start' are not in order, but
3708 * start is definitely the min. and old_end is definitely the max. */
3709 if (end != old_start)
3713 * ORDER_UINT32(end, old_start);
3714 * EDIT_InvalidateText(es, start, end);
3715 * EDIT_InvalidateText(es, old_start, old_end);
3716 * in place of the following if statement.
3717 * (That would complete the optimal five-comparison four-element sort.)
3719 if (old_start > end )
3721 EDIT_InvalidateText(es, start, end);
3722 EDIT_InvalidateText(es, old_start, old_end);
3726 EDIT_InvalidateText(es, start, old_start);
3727 EDIT_InvalidateText(es, end, old_end);
3730 else EDIT_InvalidateText(es, start, old_end);
3734 /*********************************************************************
3739 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3741 if (!(es->style & ES_MULTILINE))
3743 HeapFree(GetProcessHeap(), 0, es->tabs);
3744 es->tabs_count = count;
3748 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3749 memcpy(es->tabs, tabs, count * sizeof(INT));
3755 /*********************************************************************
3760 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3762 if (!(es->style & ES_MULTILINE))
3764 HeapFree(GetProcessHeap(), 0, es->tabs);
3765 es->tabs_count = count;
3770 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3771 for (i = 0 ; i < count ; i++)
3772 es->tabs[i] = *tabs++;
3778 /*********************************************************************
3780 * EM_SETWORDBREAKPROC
3783 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3785 if (es->word_break_proc == wbp)
3788 es->word_break_proc = wbp;
3789 es->word_break_proc16 = NULL;
3791 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3792 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3793 EDIT_UpdateText(es, NULL, TRUE);
3798 /*********************************************************************
3800 * EM_SETWORDBREAKPROC16
3803 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3805 if (es->word_break_proc16 == wbp)
3808 es->word_break_proc = NULL;
3809 es->word_break_proc16 = wbp;
3810 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3811 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3812 EDIT_UpdateText(es, NULL, TRUE);
3817 /*********************************************************************
3822 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3827 /* As per MSDN spec, for a single-line edit control,
3828 the return value is always TRUE */
3829 if( es->style & ES_READONLY )
3830 return !(es->style & ES_MULTILINE);
3832 ulength = strlenW(es->undo_text);
3834 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3836 strcpyW(utext, es->undo_text);
3838 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3839 es->undo_insert_count, debugstr_w(utext));
3841 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3842 EDIT_EM_EmptyUndoBuffer(es);
3843 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3844 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3845 /* send the notification after the selection start and end are set */
3846 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3847 EDIT_EM_ScrollCaret(es);
3848 HeapFree(GetProcessHeap(), 0, utext);
3850 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3851 es->undo_insert_count, debugstr_w(es->undo_text));
3856 /*********************************************************************
3861 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3865 control = GetKeyState(VK_CONTROL) & 0x8000;
3869 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3870 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3873 if (es->style & ES_MULTILINE) {
3874 if (es->style & ES_READONLY) {
3875 EDIT_MoveHome(es, FALSE);
3876 EDIT_MoveDown_ML(es, FALSE);
3878 static const WCHAR cr_lfW[] = {'\r','\n',0};
3879 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3884 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3886 static const WCHAR tabW[] = {'\t',0};
3887 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3891 if (!(es->style & ES_READONLY) && !control) {
3892 if (es->selection_start != es->selection_end)
3895 /* delete character left of caret */
3896 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3897 EDIT_MoveBackward(es, TRUE);
3903 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3906 if (!(es->style & ES_READONLY))
3907 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3910 if (!(es->style & ES_READONLY))
3911 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3915 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3916 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3919 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3923 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3930 /*********************************************************************
3935 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3937 if (code || control)
3957 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3958 EDIT_EM_ScrollCaret(es);
3961 ERR("unknown menu item, please report\n");
3967 /*********************************************************************
3971 * Note: the resource files resource/sysres_??.rc cannot define a
3972 * single popup menu. Hence we use a (dummy) menubar
3973 * containing the single popup menu as its first item.
3975 * FIXME: the message identifiers have been chosen arbitrarily,
3976 * hence we use MF_BYPOSITION.
3977 * We might as well use the "real" values (anybody knows ?)
3978 * The menu definition is in resources/sysres_??.rc.
3979 * Once these are OK, we better use MF_BYCOMMAND here
3980 * (as we do in EDIT_WM_Command()).
3983 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3985 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3986 HMENU popup = GetSubMenu(menu, 0);
3987 UINT start = es->selection_start;
3988 UINT end = es->selection_end;
3990 ORDER_UINT(start, end);
3993 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3995 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3997 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3999 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4001 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4003 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
4005 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4010 /*********************************************************************
4015 static void EDIT_WM_Copy(EDITSTATE *es)
4017 INT s = min(es->selection_start, es->selection_end);
4018 INT e = max(es->selection_start, es->selection_end);
4026 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4027 dst = GlobalLock(hdst);
4028 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4029 dst[len] = 0; /* ensure 0 termination */
4030 TRACE("%s\n", debugstr_w(dst));
4032 OpenClipboard(es->hwndSelf);
4034 SetClipboardData(CF_UNICODETEXT, hdst);
4039 /*********************************************************************
4044 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4048 TRACE("%s\n", debugstr_w(name));
4050 * To initialize some final structure members, we call some helper
4051 * functions. However, since the EDITSTATE is not consistent (i.e.
4052 * not fully initialized), we should be very careful which
4053 * functions can be called, and in what order.
4055 EDIT_WM_SetFont(es, 0, FALSE);
4056 EDIT_EM_EmptyUndoBuffer(es);
4058 /* We need to calculate the format rect
4059 (applications may send EM_SETMARGINS before the control gets visible) */
4060 GetClientRect(es->hwndSelf, &clientRect);
4061 EDIT_SetRectNP(es, &clientRect);
4063 if (name && *name) {
4064 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
4065 /* if we insert text to the editline, the text scrolls out
4066 * of the window, as the caret is placed after the insert
4067 * pos normally; thus we reset es->selection... to 0 and
4070 es->selection_start = es->selection_end = 0;
4071 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4072 * Messages are only to be sent when the USER does something to
4073 * change the contents. So I am removing this EN_CHANGE
4075 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4077 EDIT_EM_ScrollCaret(es);
4079 /* force scroll info update */
4080 EDIT_UpdateScrollInfo(es);
4081 /* The rule seems to return 1 here for success */
4082 /* Power Builder masked edit controls will crash */
4084 /* FIXME: is that in all cases so ? */
4089 /*********************************************************************
4094 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4099 while (LocalUnlock(es->hloc32W)) ;
4100 LocalFree(es->hloc32W);
4103 while (LocalUnlock(es->hloc32A)) ;
4104 LocalFree(es->hloc32A);
4107 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
4108 HANDLE16 oldDS = stack16->ds;
4110 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4111 while (LocalUnlock16(es->hloc16)) ;
4112 LocalFree16(es->hloc16);
4113 stack16->ds = oldDS;
4116 pc = es->first_line_def;
4120 HeapFree(GetProcessHeap(), 0, pc);
4124 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4125 HeapFree(GetProcessHeap(), 0, es);
4131 /*********************************************************************
4136 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4138 /* we do the proper erase in EDIT_WM_Paint */
4143 /*********************************************************************
4148 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4150 if(!count) return 0;
4154 lstrcpynW(dst, es->text, count);
4155 return strlenW(dst);
4159 LPSTR textA = (LPSTR)dst;
4160 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4161 textA[count - 1] = 0; /* ensure 0 termination */
4162 return strlen(textA);
4166 /*********************************************************************
4171 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4176 if (!(es->style & ES_MULTILINE))
4179 if (!(es->style & ES_AUTOHSCROLL))
4183 fw = es->format_rect.right - es->format_rect.left;
4186 TRACE("SB_LINELEFT\n");
4188 dx = -es->char_width;
4191 TRACE("SB_LINERIGHT\n");
4192 if (es->x_offset < es->text_width)
4193 dx = es->char_width;
4196 TRACE("SB_PAGELEFT\n");
4198 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4201 TRACE("SB_PAGERIGHT\n");
4202 if (es->x_offset < es->text_width)
4203 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4211 TRACE("SB_RIGHT\n");
4212 if (es->x_offset < es->text_width)
4213 dx = es->text_width - es->x_offset;
4216 TRACE("SB_THUMBTRACK %d\n", pos);
4217 es->flags |= EF_HSCROLL_TRACK;
4218 if(es->style & WS_HSCROLL)
4219 dx = pos - es->x_offset;
4224 if(pos < 0 || pos > 100) return 0;
4225 /* Assume default scroll range 0-100 */
4226 fw = es->format_rect.right - es->format_rect.left;
4227 new_x = pos * (es->text_width - fw) / 100;
4228 dx = es->text_width ? (new_x - es->x_offset) : 0;
4231 case SB_THUMBPOSITION:
4232 TRACE("SB_THUMBPOSITION %d\n", pos);
4233 es->flags &= ~EF_HSCROLL_TRACK;
4234 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4235 dx = pos - es->x_offset;
4240 if(pos < 0 || pos > 100) return 0;
4241 /* Assume default scroll range 0-100 */
4242 fw = es->format_rect.right - es->format_rect.left;
4243 new_x = pos * (es->text_width - fw) / 100;
4244 dx = es->text_width ? (new_x - es->x_offset) : 0;
4247 /* force scroll info update */
4248 EDIT_UpdateScrollInfo(es);
4249 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4253 TRACE("SB_ENDSCROLL\n");
4256 * FIXME : the next two are undocumented !
4257 * Are we doing the right thing ?
4258 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4259 * although it's also a regular control message.
4261 case EM_GETTHUMB: /* this one is used by NT notepad */
4265 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4266 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4269 /* Assume default scroll range 0-100 */
4270 INT fw = es->format_rect.right - es->format_rect.left;
4271 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4273 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4276 case EM_LINESCROLL16:
4277 TRACE("EM_LINESCROLL16\n");
4282 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4288 INT fw = es->format_rect.right - es->format_rect.left;
4289 /* check if we are going to move too far */
4290 if(es->x_offset + dx + fw > es->text_width)
4291 dx = es->text_width - fw - es->x_offset;
4293 EDIT_EM_LineScroll_internal(es, dx, 0);
4299 /*********************************************************************
4304 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4306 HWND hLBox = es->hwndListBox;
4314 hCombo = GetParent(es->hwndSelf);
4318 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4320 if (key == VK_UP || key == VK_DOWN)
4322 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4325 if (msg == WM_KEYDOWN || nEUI)
4326 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4332 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4334 /* make sure ComboLBox pops up */
4335 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4340 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4343 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4345 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4347 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4352 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4358 /*********************************************************************
4362 * Handling of special keys that don't produce a WM_CHAR
4363 * (i.e. non-printable keys) & Backspace & Delete
4366 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4371 if (GetKeyState(VK_MENU) & 0x8000)
4374 shift = GetKeyState(VK_SHIFT) & 0x8000;
4375 control = GetKeyState(VK_CONTROL) & 0x8000;
4380 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4385 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4386 EDIT_MoveUp_ML(es, shift);
4389 EDIT_MoveWordBackward(es, shift);
4391 EDIT_MoveBackward(es, shift);
4394 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4398 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4399 EDIT_MoveDown_ML(es, shift);
4401 EDIT_MoveWordForward(es, shift);
4403 EDIT_MoveForward(es, shift);
4406 EDIT_MoveHome(es, shift);
4409 EDIT_MoveEnd(es, shift);
4412 if (es->style & ES_MULTILINE)
4413 EDIT_MovePageUp_ML(es, shift);
4415 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4418 if (es->style & ES_MULTILINE)
4419 EDIT_MovePageDown_ML(es, shift);
4421 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4424 if (!(es->style & ES_READONLY) && !(shift && control)) {
4425 if (es->selection_start != es->selection_end) {
4432 /* delete character left of caret */
4433 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4434 EDIT_MoveBackward(es, TRUE);
4436 } else if (control) {
4437 /* delete to end of line */
4438 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4439 EDIT_MoveEnd(es, TRUE);
4442 /* delete character right of caret */
4443 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4444 EDIT_MoveForward(es, TRUE);
4452 if (!(es->style & ES_READONLY))
4458 /* If the edit doesn't want the return send a message to the default object */
4459 if(!(es->style & ES_WANTRETURN))
4461 HWND hwndParent = GetParent(es->hwndSelf);
4462 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4463 if (HIWORD(dw) == DC_HASDEFID)
4465 SendMessageW( hwndParent, WM_COMMAND,
4466 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4467 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4476 /*********************************************************************
4481 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4483 es->flags &= ~EF_FOCUSED;
4485 if(!(es->style & ES_NOHIDESEL))
4486 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4487 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4492 /*********************************************************************
4496 * The caret position has been set on the WM_LBUTTONDOWN message
4499 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4502 INT e = es->selection_end;
4507 es->bCaptureState = TRUE;
4508 SetCapture(es->hwndSelf);
4510 l = EDIT_EM_LineFromChar(es, e);
4511 li = EDIT_EM_LineIndex(es, l);
4512 ll = EDIT_EM_LineLength(es, e);
4513 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4514 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4515 EDIT_EM_SetSel(es, s, e, FALSE);
4516 EDIT_EM_ScrollCaret(es);
4517 es->region_posx = es->region_posy = 0;
4518 SetTimer(es->hwndSelf, 0, 100, NULL);
4523 /*********************************************************************
4528 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4533 es->bCaptureState = TRUE;
4534 SetCapture(es->hwndSelf);
4535 EDIT_ConfinePoint(es, &x, &y);
4536 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4537 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4538 EDIT_EM_ScrollCaret(es);
4539 es->region_posx = es->region_posy = 0;
4540 SetTimer(es->hwndSelf, 0, 100, NULL);
4542 if (!(es->flags & EF_FOCUSED))
4543 SetFocus(es->hwndSelf);
4549 /*********************************************************************
4554 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4556 if (es->bCaptureState) {
4557 KillTimer(es->hwndSelf, 0);
4558 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4560 es->bCaptureState = FALSE;
4565 /*********************************************************************
4570 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4572 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4577 /*********************************************************************
4582 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4588 /* If the mouse has been captured by process other than the edit control itself,
4589 * the windows edit controls will not select the strings with mouse move.
4591 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4595 * FIXME: gotta do some scrolling if outside client
4596 * area. Maybe reset the timer ?
4599 EDIT_ConfinePoint(es, &x, &y);
4600 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4601 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4602 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4603 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4604 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4609 /*********************************************************************
4613 * See also EDIT_WM_StyleChanged
4615 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4620 TRACE("Creating %s edit control, style = %08lx\n",
4621 unicode ? "Unicode" : "ANSI", lpcs->style);
4623 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4625 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4628 * Note: since the EDITSTATE has not been fully initialized yet,
4629 * we can't use any API calls that may send
4630 * WM_XXX messages before WM_NCCREATE is completed.
4633 es->is_unicode = unicode;
4634 es->style = lpcs->style;
4636 es->bEnableState = !(es->style & WS_DISABLED);
4638 es->hwndSelf = hwnd;
4639 /* Save parent, which will be notified by EN_* messages */
4640 es->hwndParent = lpcs->hwndParent;
4642 if (es->style & ES_COMBO)
4643 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4645 /* Number overrides lowercase overrides uppercase (at least it
4646 * does in Win95). However I'll bet that ES_NUMBER would be
4647 * invalid under Win 3.1.
4649 if (es->style & ES_NUMBER) {
4650 ; /* do not override the ES_NUMBER */
4651 } else if (es->style & ES_LOWERCASE) {
4652 es->style &= ~ES_UPPERCASE;
4654 if (es->style & ES_MULTILINE) {
4655 es->buffer_limit = BUFLIMIT_MULTI;
4656 if (es->style & WS_VSCROLL)
4657 es->style |= ES_AUTOVSCROLL;
4658 if (es->style & WS_HSCROLL)
4659 es->style |= ES_AUTOHSCROLL;
4660 es->style &= ~ES_PASSWORD;
4661 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4662 /* Confirmed - RIGHT overrides CENTER */
4663 if (es->style & ES_RIGHT)
4664 es->style &= ~ES_CENTER;
4665 es->style &= ~WS_HSCROLL;
4666 es->style &= ~ES_AUTOHSCROLL;
4669 es->buffer_limit = BUFLIMIT_SINGLE;
4670 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4671 es->style &= ~ES_CENTER;
4672 es->style &= ~WS_HSCROLL;
4673 es->style &= ~WS_VSCROLL;
4674 if (es->style & ES_PASSWORD)
4675 es->password_char = '*';
4678 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4679 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4681 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4683 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4685 es->undo_buffer_size = es->buffer_size;
4687 if (es->style & ES_MULTILINE)
4688 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4693 * In Win95 look and feel, the WS_BORDER style is replaced by the
4694 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4695 * control a nonclient area so we don't need to draw the border.
4696 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4697 * a nonclient area and we should handle painting the border ourselves.
4699 * When making modifications please ensure that the code still works
4700 * for edit controls created directly with style 0x50800000, exStyle 0
4701 * (which should have a single pixel border)
4703 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4704 es->style &= ~WS_BORDER;
4705 else if (es->style & WS_BORDER)
4706 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4711 /*********************************************************************
4716 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4729 BOOL rev = es->bEnableState &&
4730 ((es->flags & EF_FOCUSED) ||
4731 (es->style & ES_NOHIDESEL));
4732 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4734 GetClientRect(es->hwndSelf, &rcClient);
4736 /* get the background brush */
4737 brush = EDIT_NotifyCtlColor(es, dc);
4739 /* paint the border and the background */
4740 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4742 if(es->style & WS_BORDER) {
4743 bw = GetSystemMetrics(SM_CXBORDER);
4744 bh = GetSystemMetrics(SM_CYBORDER);
4746 if(es->style & ES_MULTILINE) {
4747 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4748 if(es->style & WS_VSCROLL) rc.right+=bw;
4751 /* Draw the frame. Same code as in nonclient.c */
4752 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4753 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4754 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4755 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4756 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4757 SelectObject(dc, old_brush);
4759 /* Keep the border clean */
4760 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4761 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4764 GetClipBox(dc, &rc);
4765 FillRect(dc, &rc, brush);
4767 IntersectClipRect(dc, es->format_rect.left,
4768 es->format_rect.top,
4769 es->format_rect.right,
4770 es->format_rect.bottom);
4771 if (es->style & ES_MULTILINE) {
4773 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4776 old_font = SelectObject(dc, es->font);
4778 if (!es->bEnableState)
4779 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4780 GetClipBox(dc, &rcRgn);
4781 if (es->style & ES_MULTILINE) {
4782 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4783 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4784 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4785 if (IntersectRect(&rc, &rcRgn, &rcLine))
4786 EDIT_PaintLine(es, dc, i, rev);
4789 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4790 if (IntersectRect(&rc, &rcRgn, &rcLine))
4791 EDIT_PaintLine(es, dc, 0, rev);
4794 SelectObject(dc, old_font);
4797 EndPaint(es->hwndSelf, &ps);
4801 /*********************************************************************
4806 static void EDIT_WM_Paste(EDITSTATE *es)
4811 /* Protect read-only edit control from modification */
4812 if(es->style & ES_READONLY)
4815 OpenClipboard(es->hwndSelf);
4816 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4817 src = (LPWSTR)GlobalLock(hsrc);
4818 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4825 /*********************************************************************
4830 static void EDIT_WM_SetFocus(EDITSTATE *es)
4832 es->flags |= EF_FOCUSED;
4834 if (!(es->style & ES_NOHIDESEL))
4835 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4837 /* single line edit updates itself */
4838 if (!(es->style & ES_MULTILINE))
4840 HDC hdc = GetDC(es->hwndSelf);
4841 EDIT_WM_Paint(es, hdc);
4842 ReleaseDC(es->hwndSelf, hdc);
4845 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4846 EDIT_SetCaretPos(es, es->selection_end,
4847 es->flags & EF_AFTER_WRAP);
4848 ShowCaret(es->hwndSelf);
4849 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
4853 /*********************************************************************
4857 * With Win95 look the margins are set to default font value unless
4858 * the system font (font == 0) is being set, in which case they are left
4862 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4870 dc = GetDC(es->hwndSelf);
4872 old_font = SelectObject(dc, font);
4873 GetTextMetricsW(dc, &tm);
4874 es->line_height = tm.tmHeight;
4875 es->char_width = tm.tmAveCharWidth;
4877 SelectObject(dc, old_font);
4878 ReleaseDC(es->hwndSelf, dc);
4880 /* Reset the format rect and the margins */
4881 GetClientRect(es->hwndSelf, &clientRect);
4882 EDIT_SetRectNP(es, &clientRect);
4883 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4884 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
4886 if (es->style & ES_MULTILINE)
4887 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4889 EDIT_CalcLineWidth_SL(es);
4892 EDIT_UpdateText(es, NULL, TRUE);
4893 if (es->flags & EF_FOCUSED) {
4895 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4896 EDIT_SetCaretPos(es, es->selection_end,
4897 es->flags & EF_AFTER_WRAP);
4898 ShowCaret(es->hwndSelf);
4903 /*********************************************************************
4908 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4909 * The modified flag is reset. No notifications are sent.
4911 * For single-line controls, reception of WM_SETTEXT triggers:
4912 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4915 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4917 if (!unicode && text)
4919 LPCSTR textA = (LPCSTR)text;
4920 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4921 LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
4923 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4927 if (es->flags & EF_UPDATE)
4928 /* fixed this bug once; complain if we see it about to happen again. */
4929 ERR("SetSel may generate UPDATE message whose handler may reset "
4932 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4935 TRACE("%s\n", debugstr_w(text));
4936 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4938 HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
4942 static const WCHAR empty_stringW[] = {0};
4944 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4947 es->flags &= ~EF_MODIFIED;
4948 EDIT_EM_SetSel(es, 0, 0, FALSE);
4950 /* Send the notification after the selection start and end have been set
4951 * edit control doesn't send notification on WM_SETTEXT
4952 * if it is multiline, or it is part of combobox
4954 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4956 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
4957 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4959 EDIT_EM_ScrollCaret(es);
4960 EDIT_UpdateScrollInfo(es);
4964 /*********************************************************************
4969 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4971 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4973 TRACE("width = %d, height = %d\n", width, height);
4974 SetRect(&rc, 0, 0, width, height);
4975 EDIT_SetRectNP(es, &rc);
4976 EDIT_UpdateText(es, NULL, TRUE);
4981 /*********************************************************************
4985 * This message is sent by SetWindowLong on having changed either the Style
4986 * or the extended style.
4988 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4990 * See also EDIT_WM_NCCreate
4992 * It appears that the Windows version of the edit control allows the style
4993 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4994 * style variable which will generally be different. In this function we
4995 * update the internal style based on what changed in the externally visible
4998 * Much of this content as based upon the MSDN, especially:
4999 * Platform SDK Documentation -> User Interface Services ->
5000 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5001 * Edit Control Styles
5003 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5005 if (GWL_STYLE == which) {
5006 DWORD style_change_mask;
5008 /* Only a subset of changes can be applied after the control
5011 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5013 if (es->style & ES_MULTILINE)
5014 style_change_mask |= ES_WANTRETURN;
5016 new_style = style->styleNew & style_change_mask;
5018 /* Number overrides lowercase overrides uppercase (at least it
5019 * does in Win95). However I'll bet that ES_NUMBER would be
5020 * invalid under Win 3.1.
5022 if (new_style & ES_NUMBER) {
5023 ; /* do not override the ES_NUMBER */
5024 } else if (new_style & ES_LOWERCASE) {
5025 new_style &= ~ES_UPPERCASE;
5028 es->style = (es->style & ~style_change_mask) | new_style;
5029 } else if (GWL_EXSTYLE == which) {
5030 ; /* FIXME - what is needed here */
5032 WARN ("Invalid style change %d\n",which);
5038 /*********************************************************************
5043 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5045 if ((key == VK_BACK) && (key_data & 0x2000)) {
5046 if (EDIT_EM_CanUndo(es))
5049 } else if (key == VK_UP || key == VK_DOWN) {
5050 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5053 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5057 /*********************************************************************
5062 static void EDIT_WM_Timer(EDITSTATE *es)
5064 if (es->region_posx < 0) {
5065 EDIT_MoveBackward(es, TRUE);
5066 } else if (es->region_posx > 0) {
5067 EDIT_MoveForward(es, TRUE);
5070 * FIXME: gotta do some vertical scrolling here, like
5071 * EDIT_EM_LineScroll(hwnd, 0, 1);
5075 /*********************************************************************
5080 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5084 if (!(es->style & ES_MULTILINE))
5087 if (!(es->style & ES_AUTOVSCROLL))
5096 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5097 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5098 (action == SB_PAGEUP ? "SB_PAGEUP" :
5100 EDIT_EM_Scroll(es, action);
5107 TRACE("SB_BOTTOM\n");
5108 dy = es->line_count - 1 - es->y_offset;
5111 TRACE("SB_THUMBTRACK %d\n", pos);
5112 es->flags |= EF_VSCROLL_TRACK;
5113 if(es->style & WS_VSCROLL)
5114 dy = pos - es->y_offset;
5117 /* Assume default scroll range 0-100 */
5120 if(pos < 0 || pos > 100) return 0;
5121 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5122 new_y = pos * (es->line_count - vlc) / 100;
5123 dy = es->line_count ? (new_y - es->y_offset) : 0;
5124 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5125 es->line_count, es->y_offset, pos, dy);
5128 case SB_THUMBPOSITION:
5129 TRACE("SB_THUMBPOSITION %d\n", pos);
5130 es->flags &= ~EF_VSCROLL_TRACK;
5131 if(es->style & WS_VSCROLL)
5132 dy = pos - es->y_offset;
5135 /* Assume default scroll range 0-100 */
5138 if(pos < 0 || pos > 100) return 0;
5139 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5140 new_y = pos * (es->line_count - vlc) / 100;
5141 dy = es->line_count ? (new_y - es->y_offset) : 0;
5142 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5143 es->line_count, es->y_offset, pos, dy);
5147 /* force scroll info update */
5148 EDIT_UpdateScrollInfo(es);
5149 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5153 TRACE("SB_ENDSCROLL\n");
5156 * FIXME : the next two are undocumented !
5157 * Are we doing the right thing ?
5158 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5159 * although it's also a regular control message.
5161 case EM_GETTHUMB: /* this one is used by NT notepad */
5165 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5166 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5169 /* Assume default scroll range 0-100 */
5170 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5171 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5173 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5176 case EM_LINESCROLL16:
5177 TRACE("EM_LINESCROLL16 %d\n", pos);
5182 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5187 EDIT_EM_LineScroll(es, 0, dy);
5191 /*********************************************************************
5196 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5198 if (es->flags & EF_UPDATE) {
5199 es->flags &= ~EF_UPDATE;
5200 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5202 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5206 /*********************************************************************
5211 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5213 if (es->flags & EF_UPDATE) {
5214 es->flags &= ~EF_UPDATE;
5215 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5217 InvalidateRect(es->hwndSelf, rc, bErase);