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"
62 #include "user_private.h"
63 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(edit);
66 WINE_DECLARE_DEBUG_CHANNEL(combo);
67 WINE_DECLARE_DEBUG_CHANNEL(relay);
69 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
70 FIXME: BTW, new specs say 65535 (do you dare ???) */
71 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
72 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
73 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
74 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
77 * extra flags for EDITSTATE.flags field
79 #define EF_MODIFIED 0x0001 /* text has been modified */
80 #define EF_FOCUSED 0x0002 /* we have input focus */
81 #define EF_UPDATE 0x0004 /* notify parent of changed state */
82 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
83 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
84 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
85 wrapped line, instead of in front of the next character */
86 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
90 END_0 = 0, /* line ends with terminating '\0' character */
91 END_WRAP, /* line is wrapped */
92 END_HARD, /* line ends with a hard return '\r\n' */
93 END_SOFT, /* line ends with a soft return '\r\r\n' */
94 END_RICH /* line ends with a single '\n' */
97 typedef struct tagLINEDEF {
98 INT length; /* bruto length of a line in bytes */
99 INT net_length; /* netto length of a line in visible characters */
101 INT width; /* width of the line in pixels */
102 INT index; /* line index into the buffer */
103 struct tagLINEDEF *next;
108 BOOL is_unicode; /* how the control was created */
109 LPWSTR text; /* the actual contents of the control */
110 UINT buffer_size; /* the size of the buffer in characters */
111 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
112 HFONT font; /* NULL means standard system font */
113 INT x_offset; /* scroll offset for multi lines this is in pixels
114 for single lines it's in characters */
115 INT line_height; /* height of a screen line in pixels */
116 INT char_width; /* average character width in pixels */
117 DWORD style; /* sane version of wnd->dwStyle */
118 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
119 INT undo_insert_count; /* number of characters inserted in sequence */
120 UINT undo_position; /* character index of the insertion and deletion */
121 LPWSTR undo_text; /* deleted text */
122 UINT undo_buffer_size; /* size of the deleted text buffer */
123 INT selection_start; /* == selection_end if no selection */
124 INT selection_end; /* == current caret position */
125 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
126 INT left_margin; /* in pixels */
127 INT right_margin; /* in pixels */
129 INT text_width; /* width of the widest line in pixels for multi line controls
130 and just line width for single line controls */
131 INT region_posx; /* Position of cursor relative to region: */
132 INT region_posy; /* -1: to left, 0: within, 1: to right */
133 EDITWORDBREAKPROC16 word_break_proc16;
134 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
135 INT line_count; /* number of lines */
136 INT y_offset; /* scroll offset in number of lines */
137 BOOL bCaptureState; /* flag indicating whether mouse was captured */
138 BOOL bEnableState; /* flag keeping the enable state */
139 HWND hwndSelf; /* the our window handle */
140 HWND hwndParent; /* Handle of parent for sending EN_* messages.
141 Even if parent will change, EN_* messages
142 should be sent to the first parent. */
143 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
145 * only for multi line controls
147 INT lock_count; /* amount of re-entries in the EditWndProc */
150 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
151 HLOCAL hloc32W; /* our unicode local memory block */
152 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
154 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
159 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
160 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
162 /* used for disabled or read-only edit control */
163 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
165 { /* Notify parent which has created this edit control */ \
166 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
167 SendMessageW(es->hwndParent, WM_COMMAND, \
168 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
169 (LPARAM)(es->hwndSelf)); \
172 /*********************************************************************
179 * These functions have trivial implementations
180 * We still like to call them internally
181 * "static inline" makes them more like macro's
183 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
184 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
185 static inline void EDIT_WM_Clear(EDITSTATE *es);
186 static inline void EDIT_WM_Cut(EDITSTATE *es);
189 * Helper functions only valid for one type of control
191 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
192 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
193 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
194 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
195 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
196 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
197 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
199 * Helper functions valid for both single line _and_ multi line controls
201 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
202 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
203 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
204 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
205 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
206 static void EDIT_LockBuffer(EDITSTATE *es);
207 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
208 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
209 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
210 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
211 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
212 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
213 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
214 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
215 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
216 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
217 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
218 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
219 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
220 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
221 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
223 * EM_XXX message handlers
225 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
226 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
227 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
228 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
229 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
230 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
231 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
232 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
233 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
234 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
235 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
236 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
237 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
238 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
239 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
240 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
241 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
242 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
243 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
244 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
245 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
246 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
247 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
248 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
249 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
250 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
251 static BOOL EDIT_EM_Undo(EDITSTATE *es);
253 * WM_XXX message handlers
255 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
256 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
257 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
258 static void EDIT_WM_Copy(EDITSTATE *es);
259 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
260 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
261 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
262 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
263 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
264 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
265 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
266 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
267 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
268 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
269 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
270 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
271 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
272 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
273 static void EDIT_WM_Paste(EDITSTATE *es);
274 static void EDIT_WM_SetFocus(EDITSTATE *es);
275 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
276 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
277 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
278 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
279 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
280 static void EDIT_WM_Timer(EDITSTATE *es);
281 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
282 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
283 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
285 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
286 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
288 /*********************************************************************
289 * edit class descriptor
291 const struct builtin_class_descr EDIT_builtin_class =
294 CS_DBLCLKS | CS_PARENTDC, /* style */
295 EditWndProcA, /* procA */
296 EditWndProcW, /* procW */
297 sizeof(EDITSTATE *), /* extra */
298 IDC_IBEAM, /* cursor */
303 /*********************************************************************
308 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
310 return (es->undo_insert_count || strlenW(es->undo_text));
314 /*********************************************************************
319 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
321 es->undo_insert_count = 0;
322 *es->undo_text = '\0';
326 /*********************************************************************
331 static inline void EDIT_WM_Clear(EDITSTATE *es)
333 static const WCHAR empty_stringW[] = {0};
335 /* Protect read-only edit control from modification */
336 if(es->style & ES_READONLY)
339 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
343 /*********************************************************************
348 static inline void EDIT_WM_Cut(EDITSTATE *es)
355 /**********************************************************************
358 * Returns the window version in case Wine emulates a later version
359 * of windows than the application expects.
361 * In a number of cases when windows runs an application that was
362 * designed for an earlier windows version, windows reverts
363 * to "old" behaviour of that earlier version.
365 * An example is a disabled edit control that needs to be painted.
366 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
367 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
368 * applications with an expected version 0f 4.0 or higher.
371 static DWORD get_app_version(void)
373 static DWORD version;
376 DWORD dwEmulatedVersion;
378 DWORD dwProcVersion = GetProcessVersion(0);
380 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
381 GetVersionExW( &info );
382 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
383 /* FIXME: this may not be 100% correct; see discussion on the
384 * wine developer list in Nov 1999 */
385 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
391 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 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
404 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
407 return DefWindowProcW(hwnd, msg, wParam, lParam);
409 return DefWindowProcA(hwnd, msg, wParam, lParam);
412 /*********************************************************************
416 * The messages are in the order of the actual integer values
417 * (which can be found in include/windows.h)
418 * Wherever possible the 16 bit versions are converted to
419 * the 32 bit ones, so that we can 'fall through' to the
420 * helper functions. These are mostly 32 bit (with a few
421 * exceptions, clearly indicated by a '16' extension to their
425 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
426 WPARAM wParam, LPARAM lParam, BOOL unicode )
428 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
431 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
433 if (!es && msg != WM_NCCREATE)
434 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
435 else if (msg == WM_NCCREATE)
436 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
437 else if (msg == WM_DESTROY)
438 return EDIT_WM_Destroy(es);
441 if (es) EDIT_LockBuffer(es);
449 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
453 if ((short)LOWORD(lParam) == -1)
454 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
456 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
458 EDIT_EM_ScrollCaret(es);
462 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
463 EDIT_EM_ScrollCaret(es);
470 RECT16 *r16 = MapSL(lParam);
471 r16->left = es->format_rect.left;
472 r16->top = es->format_rect.top;
473 r16->right = es->format_rect.right;
474 r16->bottom = es->format_rect.bottom;
479 CopyRect((LPRECT)lParam, &es->format_rect);
483 if ((es->style & ES_MULTILINE) && lParam) {
485 RECT16 *r16 = MapSL(lParam);
488 rc.right = r16->right;
489 rc.bottom = r16->bottom;
490 EDIT_SetRectNP(es, &rc);
491 EDIT_UpdateText(es, NULL, TRUE);
495 if ((es->style & ES_MULTILINE) && lParam) {
496 EDIT_SetRectNP(es, (LPRECT)lParam);
497 EDIT_UpdateText(es, NULL, TRUE);
502 if ((es->style & ES_MULTILINE) && lParam) {
504 RECT16 *r16 = MapSL(lParam);
507 rc.right = r16->right;
508 rc.bottom = r16->bottom;
509 EDIT_SetRectNP(es, &rc);
513 if ((es->style & ES_MULTILINE) && lParam)
514 EDIT_SetRectNP(es, (LPRECT)lParam);
519 result = EDIT_EM_Scroll(es, (INT)wParam);
522 case EM_LINESCROLL16:
523 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
524 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
527 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
530 case EM_SCROLLCARET16:
532 EDIT_EM_ScrollCaret(es);
538 result = ((es->flags & EF_MODIFIED) != 0);
544 es->flags |= EF_MODIFIED;
546 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
549 case EM_GETLINECOUNT16:
550 case EM_GETLINECOUNT:
551 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
555 if ((INT16)wParam == -1)
559 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
563 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
566 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
570 result = (LRESULT)EDIT_EM_GetHandle16(es);
573 result = (LRESULT)EDIT_EM_GetHandle(es);
578 result = EDIT_EM_GetThumb(es);
581 /* these messages missing from specs */
590 FIXME("undocumented message 0x%x, please report\n", msg);
591 result = DefWindowProcW(hwnd, msg, wParam, lParam);
594 case EM_LINELENGTH16:
596 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
599 case EM_REPLACESEL16:
600 lParam = (LPARAM)MapSL(lParam);
601 unicode = FALSE; /* 16-bit message is always ascii */
608 textW = (LPWSTR)lParam;
611 LPSTR textA = (LPSTR)lParam;
612 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
613 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
614 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
617 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
621 HeapFree(GetProcessHeap(), 0, textW);
626 lParam = (LPARAM)MapSL(lParam);
627 unicode = FALSE; /* 16-bit message is always ascii */
630 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
634 case EM_SETLIMITTEXT:
635 EDIT_EM_SetLimitText(es, (INT)wParam);
640 result = (LRESULT)EDIT_EM_CanUndo(es);
646 result = (LRESULT)EDIT_EM_Undo(es);
651 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
654 case EM_LINEFROMCHAR16:
655 case EM_LINEFROMCHAR:
656 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
659 case EM_SETTABSTOPS16:
660 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
663 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
666 case EM_SETPASSWORDCHAR16:
667 unicode = FALSE; /* 16-bit message is always ascii */
669 case EM_SETPASSWORDCHAR:
674 charW = (WCHAR)wParam;
678 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
681 EDIT_EM_SetPasswordChar(es, charW);
685 case EM_EMPTYUNDOBUFFER16:
686 case EM_EMPTYUNDOBUFFER:
687 EDIT_EM_EmptyUndoBuffer(es);
690 case EM_GETFIRSTVISIBLELINE16:
691 result = es->y_offset;
693 case EM_GETFIRSTVISIBLELINE:
694 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
697 case EM_SETREADONLY16:
700 SetWindowLongW( hwnd, GWL_STYLE,
701 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
702 es->style |= ES_READONLY;
704 SetWindowLongW( hwnd, GWL_STYLE,
705 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
706 es->style &= ~ES_READONLY;
711 case EM_SETWORDBREAKPROC16:
712 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
714 case EM_SETWORDBREAKPROC:
715 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
718 case EM_GETWORDBREAKPROC16:
719 result = (LRESULT)es->word_break_proc16;
721 case EM_GETWORDBREAKPROC:
722 result = (LRESULT)es->word_break_proc;
725 case EM_GETPASSWORDCHAR16:
726 unicode = FALSE; /* 16-bit message is always ascii */
728 case EM_GETPASSWORDCHAR:
731 result = es->password_char;
734 WCHAR charW = es->password_char;
736 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
742 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
745 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
749 result = MAKELONG(es->left_margin, es->right_margin);
752 case EM_GETLIMITTEXT:
753 result = es->buffer_limit;
757 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 = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
771 if (es->style & ES_MULTILINE)
773 result |= DLGC_WANTALLKEYS;
777 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
779 int vk = (int)((LPMSG)lParam)->wParam;
781 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
783 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
784 result |= DLGC_WANTMESSAGE;
795 strng[0] = wParam >> 8;
796 strng[1] = wParam & 0xff;
797 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
798 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
799 EDIT_WM_Char(es, charW);
812 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
815 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
817 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
818 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
821 EDIT_WM_Char(es, charW);
830 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
834 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
843 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
846 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
850 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
851 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
852 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
854 result = EDIT_WM_Create(es, nameW);
855 HeapFree(GetProcessHeap(), 0, nameW);
864 es->bEnableState = (BOOL) wParam;
865 EDIT_UpdateText(es, NULL, TRUE);
869 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
873 result = (LRESULT)es->font;
877 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
880 case WM_GETTEXTLENGTH:
881 if (unicode) result = strlenW(es->text);
882 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
883 NULL, 0, NULL, NULL );
887 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
891 result = EDIT_WM_KeyDown(es, (INT)wParam);
895 result = EDIT_WM_KillFocus(es);
898 case WM_LBUTTONDBLCLK:
899 result = EDIT_WM_LButtonDblClk(es);
903 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
907 result = EDIT_WM_LButtonUp(es);
911 result = EDIT_WM_MButtonDown(es);
914 case WM_MOUSEACTIVATE:
915 result = MA_ACTIVATE;
919 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
923 EDIT_WM_Paint(es, (HDC)wParam);
931 EDIT_WM_SetFocus(es);
935 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
939 /* FIXME: actually set an internal flag and behave accordingly */
943 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
948 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
951 case WM_STYLECHANGED:
952 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
955 case WM_STYLECHANGING:
956 result = 0; /* See EDIT_WM_StyleChanged */
960 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
968 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
973 int gcWheelDelta = 0;
974 UINT pulScrollLines = 3;
975 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
977 if (wParam & (MK_SHIFT | MK_CONTROL)) {
978 result = DefWindowProcW(hwnd, msg, wParam, lParam);
981 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
982 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
984 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
985 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
986 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
991 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
995 if (es) EDIT_UnlockBuffer(es, FALSE);
997 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1002 /*********************************************************************
1004 * EditWndProcW (USER32.@)
1006 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1008 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1011 /*********************************************************************
1013 * EditWndProc (USER32.@)
1015 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1017 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1020 /*********************************************************************
1022 * EDIT_BuildLineDefs_ML
1024 * Build linked list of text lines.
1025 * Lines can end with '\0' (last line), a character (if it is wrapped),
1026 * a soft return '\r\r\n' or a hard return '\r\n'
1029 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1033 LPWSTR current_position, cp;
1035 LINEDEF *current_line;
1036 LINEDEF *previous_line;
1037 LINEDEF *start_line;
1038 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1039 INT line_count = es->line_count;
1040 INT orig_net_length;
1043 if (istart == iend && delta == 0)
1046 dc = GetDC(es->hwndSelf);
1048 old_font = SelectObject(dc, es->font);
1050 previous_line = NULL;
1051 current_line = es->first_line_def;
1053 /* Find starting line. istart must lie inside an existing line or
1054 * at the end of buffer */
1056 if (istart < current_line->index + current_line->length ||
1057 current_line->ending == END_0)
1060 previous_line = current_line;
1061 current_line = current_line->next;
1063 } while (current_line);
1065 if (!current_line) /* Error occurred start is not inside previous buffer */
1067 FIXME(" modification occurred outside buffer\n");
1068 ReleaseDC(es->hwndSelf, dc);
1072 /* Remember start of modifications in order to calculate update region */
1073 nstart_line = line_index;
1074 nstart_index = current_line->index;
1076 /* We must start to reformat from the previous line since the modifications
1077 * may have caused the line to wrap upwards. */
1078 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1081 current_line = previous_line;
1083 start_line = current_line;
1085 fw = es->format_rect.right - es->format_rect.left;
1086 current_position = es->text + current_line->index;
1088 if (current_line != start_line)
1090 if (!current_line || current_line->index + delta > current_position - es->text)
1092 /* The buffer has been expanded, create a new line and
1093 insert it into the link list */
1094 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1095 new_line->next = previous_line->next;
1096 previous_line->next = new_line;
1097 current_line = new_line;
1100 else if (current_line->index + delta < current_position - es->text)
1102 /* The previous line merged with this line so we delete this extra entry */
1103 previous_line->next = current_line->next;
1104 HeapFree(GetProcessHeap(), 0, current_line);
1105 current_line = previous_line->next;
1109 else /* current_line->index + delta == current_position */
1111 if (current_position - es->text > iend)
1112 break; /* We reached end of line modifications */
1113 /* else recalulate this line */
1117 current_line->index = current_position - es->text;
1118 orig_net_length = current_line->net_length;
1120 /* Find end of line */
1121 cp = current_position;
1123 if (*cp == '\n') break;
1124 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1129 /* Mark type of line termination */
1131 current_line->ending = END_0;
1132 current_line->net_length = strlenW(current_position);
1133 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1134 current_line->ending = END_SOFT;
1135 current_line->net_length = cp - current_position - 1;
1136 } else if (*cp == '\n') {
1137 current_line->ending = END_RICH;
1138 current_line->net_length = cp - current_position;
1140 current_line->ending = END_HARD;
1141 current_line->net_length = cp - current_position;
1144 /* Calculate line width */
1145 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1146 current_position, current_line->net_length,
1147 es->tabs_count, es->tabs));
1149 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1150 if (!(es->style & ES_AUTOHSCROLL)) {
1151 if (current_line->width > fw) {
1156 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1157 prev + 1, current_line->net_length, WB_RIGHT);
1158 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1159 current_position, next, es->tabs_count, es->tabs));
1160 } while (current_line->width <= fw);
1161 if (!prev) { /* Didn't find a line break so force a break */
1166 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1167 current_position, next, es->tabs_count, es->tabs));
1168 } while (current_line->width <= fw);
1173 /* If the first line we are calculating, wrapped before istart, we must
1174 * adjust istart in order for this to be reflected in the update region. */
1175 if (current_line->index == nstart_index && istart > current_line->index + prev)
1176 istart = current_line->index + prev;
1177 /* else if we are updating the previous line before the first line we
1178 * are re-calculating and it expanded */
1179 else if (current_line == start_line &&
1180 current_line->index != nstart_index && orig_net_length < prev)
1182 /* Line expanded due to an upwards line wrap so we must partially include
1183 * previous line in update region */
1184 nstart_line = line_index;
1185 nstart_index = current_line->index;
1186 istart = current_line->index + orig_net_length;
1189 current_line->net_length = prev;
1190 current_line->ending = END_WRAP;
1191 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1192 current_line->net_length, es->tabs_count, es->tabs));
1194 else if (orig_net_length < current_line->net_length &&
1195 current_line == start_line &&
1196 current_line->index != nstart_index) {
1197 /* The previous line expanded but it's still not as wide as the client rect */
1198 /* The expansion is due to an upwards line wrap so we must partially include
1199 it in the update region */
1200 nstart_line = line_index;
1201 nstart_index = current_line->index;
1202 istart = current_line->index + orig_net_length;
1207 /* Adjust length to include line termination */
1208 switch (current_line->ending) {
1210 current_line->length = current_line->net_length + 3;
1213 current_line->length = current_line->net_length + 1;
1216 current_line->length = current_line->net_length + 2;
1220 current_line->length = current_line->net_length;
1223 es->text_width = max(es->text_width, current_line->width);
1224 current_position += current_line->length;
1225 previous_line = current_line;
1226 current_line = current_line->next;
1228 } while (previous_line->ending != END_0);
1230 /* Finish adjusting line indexes by delta or remove hanging lines */
1231 if (previous_line->ending == END_0)
1233 LINEDEF *pnext = NULL;
1235 previous_line->next = NULL;
1236 while (current_line)
1238 pnext = current_line->next;
1239 HeapFree(GetProcessHeap(), 0, current_line);
1240 current_line = pnext;
1244 else if (delta != 0)
1246 while (current_line)
1248 current_line->index += delta;
1249 current_line = current_line->next;
1253 /* Calculate rest of modification rectangle */
1258 * We calculate two rectangles. One for the first line which may have
1259 * an indent with respect to the format rect. The other is a format-width
1260 * rectangle that spans the rest of the lines that changed or moved.
1262 rc.top = es->format_rect.top + nstart_line * es->line_height -
1263 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1264 rc.bottom = rc.top + es->line_height;
1265 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1266 rc.left = es->format_rect.left;
1268 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1269 es->text + nstart_index, istart - nstart_index,
1270 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1271 rc.right = es->format_rect.right;
1272 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1275 rc.left = es->format_rect.left;
1276 rc.right = es->format_rect.right;
1278 * If lines were added or removed we must re-paint the remainder of the
1279 * lines since the remaining lines were either shifted up or down.
1281 if (line_count < es->line_count) /* We added lines */
1282 rc.bottom = es->line_count * es->line_height;
1283 else if (line_count > es->line_count) /* We removed lines */
1284 rc.bottom = line_count * es->line_height;
1286 rc.bottom = line_index * es->line_height;
1287 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1288 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1289 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1290 DeleteObject(tmphrgn);
1294 SelectObject(dc, old_font);
1296 ReleaseDC(es->hwndSelf, dc);
1299 /*********************************************************************
1301 * EDIT_CalcLineWidth_SL
1304 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1311 text = EDIT_GetPasswordPointer_SL(es);
1313 dc = GetDC(es->hwndSelf);
1315 old_font = SelectObject(dc, es->font);
1317 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1320 SelectObject(dc, old_font);
1321 ReleaseDC(es->hwndSelf, dc);
1323 if (es->style & ES_PASSWORD)
1324 HeapFree(GetProcessHeap(), 0, text);
1326 es->text_width = size.cx;
1329 /*********************************************************************
1331 * EDIT_CallWordBreakProc
1333 * Call appropriate WordBreakProc (internal or external).
1335 * Note: The "start" argument should always be an index referring
1336 * to es->text. The actual wordbreak proc might be
1337 * 16 bit, so we can't always pass any 32 bit LPSTR.
1338 * Hence we assume that es->text is the buffer that holds
1339 * the string under examination (we can decide this for ourselves).
1342 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1346 if (es->word_break_proc16) {
1353 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1354 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1355 segptr = K32WOWGlobalLock16(hglob16);
1356 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1357 args[4] = SELECTOROF(segptr);
1358 args[3] = OFFSETOF(segptr);
1362 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1363 ret = LOWORD(result);
1364 GlobalUnlock16(hglob16);
1365 GlobalFree16(hglob16);
1367 else if (es->word_break_proc)
1371 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1373 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1374 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1375 ret = wbpW(es->text + start, index, count, action);
1379 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1383 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1384 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1385 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1386 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1387 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1388 ret = wbpA(textA, index, countA, action);
1389 HeapFree(GetProcessHeap(), 0, textA);
1393 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1399 /*********************************************************************
1403 * Beware: This is not the function called on EM_CHARFROMPOS
1404 * The position _can_ be outside the formatting / client
1406 * The return value is only the character index
1409 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1415 if (es->style & ES_MULTILINE) {
1416 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1418 LINEDEF *line_def = es->first_line_def;
1420 while ((line > 0) && line_def->next) {
1421 line_index += line_def->length;
1422 line_def = line_def->next;
1425 x += es->x_offset - es->format_rect.left;
1426 if (es->style & ES_RIGHT)
1427 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1428 else if (es->style & ES_CENTER)
1429 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1430 if (x >= line_def->width) {
1432 *after_wrap = (line_def->ending == END_WRAP);
1433 return line_index + line_def->net_length;
1437 *after_wrap = FALSE;
1440 dc = GetDC(es->hwndSelf);
1442 old_font = SelectObject(dc, es->font);
1443 low = line_index + 1;
1444 high = line_index + line_def->net_length + 1;
1445 while (low < high - 1)
1447 INT mid = (low + high) / 2;
1448 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1454 *after_wrap = ((index == line_index + line_def->net_length) &&
1455 (line_def->ending == END_WRAP));
1460 *after_wrap = FALSE;
1461 x -= es->format_rect.left;
1463 return es->x_offset;
1467 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1468 if (es->style & ES_RIGHT)
1470 else if (es->style & ES_CENTER)
1474 text = EDIT_GetPasswordPointer_SL(es);
1475 dc = GetDC(es->hwndSelf);
1477 old_font = SelectObject(dc, es->font);
1481 INT high = es->x_offset;
1482 while (low < high - 1)
1484 INT mid = (low + high) / 2;
1485 GetTextExtentPoint32W( dc, text + mid,
1486 es->x_offset - mid, &size );
1487 if (size.cx > -x) low = mid;
1494 INT low = es->x_offset;
1495 INT high = strlenW(es->text) + 1;
1496 while (low < high - 1)
1498 INT mid = (low + high) / 2;
1499 GetTextExtentPoint32W( dc, text + es->x_offset,
1500 mid - es->x_offset, &size );
1501 if (size.cx > x) high = mid;
1506 if (es->style & ES_PASSWORD)
1507 HeapFree(GetProcessHeap(), 0, text);
1510 SelectObject(dc, old_font);
1511 ReleaseDC(es->hwndSelf, dc);
1516 /*********************************************************************
1520 * adjusts the point to be within the formatting rectangle
1521 * (so CharFromPos returns the nearest _visible_ character)
1524 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1526 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1527 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1531 /*********************************************************************
1535 * Calculates the bounding rectangle for a line from a starting
1536 * column to an ending column.
1539 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1541 INT line_index = EDIT_EM_LineIndex(es, line);
1543 if (es->style & ES_MULTILINE)
1544 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1546 rc->top = es->format_rect.top;
1547 rc->bottom = rc->top + es->line_height;
1548 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1549 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1553 /*********************************************************************
1555 * EDIT_GetPasswordPointer_SL
1557 * note: caller should free the (optionally) allocated buffer
1560 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1562 if (es->style & ES_PASSWORD) {
1563 INT len = strlenW(es->text);
1564 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1566 while(len) text[--len] = es->password_char;
1573 /*********************************************************************
1577 * This acts as a LOCAL_Lock(), but it locks only once. This way
1578 * you can call it whenever you like, without unlocking.
1580 * Initially the edit control allocates a HLOCAL32 buffer
1581 * (32 bit linear memory handler). However, 16 bit application
1582 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1583 * handler). From that moment on we have to keep using this 16 bit memory
1584 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1585 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1589 static void EDIT_LockBuffer(EDITSTATE *es)
1591 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1595 BOOL _16bit = FALSE;
1601 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1602 textA = LocalLock(es->hloc32A);
1603 countA = strlen(textA) + 1;
1607 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1608 textA = LOCAL_Lock(hInstance, es->hloc16);
1609 countA = strlen(textA) + 1;
1614 ERR("no buffer ... please report\n");
1621 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1622 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1623 if(countW_new > es->buffer_size + 1)
1625 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1626 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1627 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1630 es->hloc32W = hloc32W_new;
1631 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1632 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1635 WARN("FAILED! Will synchronize partially\n");
1639 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1640 es->text = LocalLock(es->hloc32W);
1644 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1646 LOCAL_Unlock(hInstance, es->hloc16);
1648 LocalUnlock(es->hloc32A);
1655 /*********************************************************************
1657 * EDIT_SL_InvalidateText
1659 * Called from EDIT_InvalidateText().
1660 * Does the job for single-line controls only.
1663 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1668 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1669 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1670 EDIT_UpdateText(es, &rc, TRUE);
1674 /*********************************************************************
1676 * EDIT_ML_InvalidateText
1678 * Called from EDIT_InvalidateText().
1679 * Does the job for multi-line controls only.
1682 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1684 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1685 INT sl = EDIT_EM_LineFromChar(es, start);
1686 INT el = EDIT_EM_LineFromChar(es, end);
1695 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1698 sc = start - EDIT_EM_LineIndex(es, sl);
1699 ec = end - EDIT_EM_LineIndex(es, el);
1700 if (sl < es->y_offset) {
1704 if (el > es->y_offset + vlc) {
1705 el = es->y_offset + vlc;
1706 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1708 GetClientRect(es->hwndSelf, &rc1);
1709 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1711 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1712 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1713 EDIT_UpdateText(es, &rcUpdate, TRUE);
1715 EDIT_GetLineRect(es, sl, sc,
1716 EDIT_EM_LineLength(es,
1717 EDIT_EM_LineIndex(es, sl)),
1719 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1720 EDIT_UpdateText(es, &rcUpdate, TRUE);
1721 for (l = sl + 1 ; l < el ; l++) {
1722 EDIT_GetLineRect(es, l, 0,
1723 EDIT_EM_LineLength(es,
1724 EDIT_EM_LineIndex(es, l)),
1726 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1727 EDIT_UpdateText(es, &rcUpdate, TRUE);
1729 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1730 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1731 EDIT_UpdateText(es, &rcUpdate, TRUE);
1736 /*********************************************************************
1738 * EDIT_InvalidateText
1740 * Invalidate the text from offset start upto, but not including,
1741 * offset end. Useful for (re)painting the selection.
1742 * Regions outside the linewidth are not invalidated.
1743 * end == -1 means end == TextLength.
1744 * start and end need not be ordered.
1747 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1753 end = strlenW(es->text);
1761 if (es->style & ES_MULTILINE)
1762 EDIT_ML_InvalidateText(es, start, end);
1764 EDIT_SL_InvalidateText(es, start, end);
1768 /*********************************************************************
1772 * Try to fit size + 1 characters in the buffer.
1774 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1778 if (size <= es->buffer_size)
1781 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1783 /* Force edit to unlock it's buffer. es->text now NULL */
1784 EDIT_UnlockBuffer(es, TRUE);
1787 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1788 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1789 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1790 es->hloc32W = hNew32W;
1791 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1795 EDIT_LockBuffer(es);
1797 if (es->buffer_size < size) {
1798 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1799 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1802 TRACE("We now have %d+1\n", es->buffer_size);
1808 /*********************************************************************
1812 * Try to fit size + 1 bytes in the undo buffer.
1815 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1819 if (size <= es->undo_buffer_size)
1822 TRACE("trying to ReAlloc to %d+1\n", size);
1824 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1825 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1826 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1831 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1837 /*********************************************************************
1842 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1844 INT e = es->selection_end;
1848 if ((es->style & ES_MULTILINE) && e &&
1849 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1851 if (e && (es->text[e - 1] == '\r'))
1855 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1856 EDIT_EM_ScrollCaret(es);
1860 /*********************************************************************
1864 * Only for multi line controls
1865 * Move the caret one line down, on a column with the nearest
1866 * x coordinate on the screen (might be a different column).
1869 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1871 INT s = es->selection_start;
1872 INT e = es->selection_end;
1873 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1874 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1875 INT x = (short)LOWORD(pos);
1876 INT y = (short)HIWORD(pos);
1878 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1881 EDIT_EM_SetSel(es, s, e, after_wrap);
1882 EDIT_EM_ScrollCaret(es);
1886 /*********************************************************************
1891 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1893 BOOL after_wrap = FALSE;
1896 /* Pass a high value in x to make sure of receiving the end of the line */
1897 if (es->style & ES_MULTILINE)
1898 e = EDIT_CharFromPos(es, 0x3fffffff,
1899 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1901 e = strlenW(es->text);
1902 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1903 EDIT_EM_ScrollCaret(es);
1907 /*********************************************************************
1912 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1914 INT e = es->selection_end;
1918 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1919 if (es->text[e] == '\n')
1921 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1925 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1926 EDIT_EM_ScrollCaret(es);
1930 /*********************************************************************
1934 * Home key: move to beginning of line.
1937 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1941 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1942 if (es->style & ES_MULTILINE)
1943 e = EDIT_CharFromPos(es, -es->x_offset,
1944 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1947 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1948 EDIT_EM_ScrollCaret(es);
1952 /*********************************************************************
1954 * EDIT_MovePageDown_ML
1956 * Only for multi line controls
1957 * Move the caret one page down, on a column with the nearest
1958 * x coordinate on the screen (might be a different column).
1961 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1963 INT s = es->selection_start;
1964 INT e = es->selection_end;
1965 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1966 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1967 INT x = (short)LOWORD(pos);
1968 INT y = (short)HIWORD(pos);
1970 e = EDIT_CharFromPos(es, x,
1971 y + (es->format_rect.bottom - es->format_rect.top),
1975 EDIT_EM_SetSel(es, s, e, after_wrap);
1976 EDIT_EM_ScrollCaret(es);
1980 /*********************************************************************
1982 * EDIT_MovePageUp_ML
1984 * Only for multi line controls
1985 * Move the caret one page up, on a column with the nearest
1986 * x coordinate on the screen (might be a different column).
1989 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1991 INT s = es->selection_start;
1992 INT e = es->selection_end;
1993 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1994 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1995 INT x = (short)LOWORD(pos);
1996 INT y = (short)HIWORD(pos);
1998 e = EDIT_CharFromPos(es, x,
1999 y - (es->format_rect.bottom - es->format_rect.top),
2003 EDIT_EM_SetSel(es, s, e, after_wrap);
2004 EDIT_EM_ScrollCaret(es);
2008 /*********************************************************************
2012 * Only for multi line controls
2013 * Move the caret one line up, on a column with the nearest
2014 * x coordinate on the screen (might be a different column).
2017 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2019 INT s = es->selection_start;
2020 INT e = es->selection_end;
2021 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2022 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2023 INT x = (short)LOWORD(pos);
2024 INT y = (short)HIWORD(pos);
2026 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2029 EDIT_EM_SetSel(es, s, e, after_wrap);
2030 EDIT_EM_ScrollCaret(es);
2034 /*********************************************************************
2036 * EDIT_MoveWordBackward
2039 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2041 INT s = es->selection_start;
2042 INT e = es->selection_end;
2047 l = EDIT_EM_LineFromChar(es, e);
2048 ll = EDIT_EM_LineLength(es, e);
2049 li = EDIT_EM_LineIndex(es, l);
2052 li = EDIT_EM_LineIndex(es, l - 1);
2053 e = li + EDIT_EM_LineLength(es, li);
2056 e = li + (INT)EDIT_CallWordBreakProc(es,
2057 li, e - li, ll, WB_LEFT);
2061 EDIT_EM_SetSel(es, s, e, FALSE);
2062 EDIT_EM_ScrollCaret(es);
2066 /*********************************************************************
2068 * EDIT_MoveWordForward
2071 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2073 INT s = es->selection_start;
2074 INT e = es->selection_end;
2079 l = EDIT_EM_LineFromChar(es, e);
2080 ll = EDIT_EM_LineLength(es, e);
2081 li = EDIT_EM_LineIndex(es, l);
2083 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2084 e = EDIT_EM_LineIndex(es, l + 1);
2086 e = li + EDIT_CallWordBreakProc(es,
2087 li, e - li + 1, ll, WB_RIGHT);
2091 EDIT_EM_SetSel(es, s, e, FALSE);
2092 EDIT_EM_ScrollCaret(es);
2096 /*********************************************************************
2101 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2103 INT s = es->selection_start;
2104 INT e = es->selection_end;
2111 if (es->style & ES_MULTILINE) {
2112 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2113 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2118 TRACE("line=%d\n", line);
2120 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2121 x = (short)LOWORD(pos);
2122 y = (short)HIWORD(pos);
2123 li = EDIT_EM_LineIndex(es, line);
2124 ll = EDIT_EM_LineLength(es, li);
2125 s = min(es->selection_start, es->selection_end);
2126 e = max(es->selection_start, es->selection_end);
2127 s = min(li + ll, max(li, s));
2128 e = min(li + ll, max(li, e));
2129 if (rev && (s != e) &&
2130 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2131 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2132 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2133 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2135 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2139 /*********************************************************************
2144 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2155 BkMode = GetBkMode(dc);
2156 BkColor = GetBkColor(dc);
2157 TextColor = GetTextColor(dc);
2159 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2160 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2161 SetBkMode( dc, OPAQUE);
2163 li = EDIT_EM_LineIndex(es, line);
2164 if (es->style & ES_MULTILINE) {
2165 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2166 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2168 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2169 TextOutW(dc, x, y, text + li + col, count);
2170 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2172 if (es->style & ES_PASSWORD)
2173 HeapFree(GetProcessHeap(), 0, text);
2176 SetBkColor(dc, BkColor);
2177 SetTextColor(dc, TextColor);
2178 SetBkMode( dc, BkMode);
2184 /*********************************************************************
2189 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2192 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2193 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2194 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2198 /*********************************************************************
2202 * note: this is not (exactly) the handler called on EM_SETRECTNP
2203 * it is also used to set the rect of a single line control
2206 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2211 CopyRect(&es->format_rect, rc);
2212 if (es->style & ES_MULTILINE)
2214 if (es->style & WS_BORDER) {
2215 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2216 es->format_rect.left += bw;
2217 es->format_rect.right -= bw;
2218 es->format_rect.top += bw;
2219 es->format_rect.bottom -= bw;
2224 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2225 if (ExStyle & WS_EX_CLIENTEDGE) {
2226 if (es->line_height + 2 <=
2227 es->format_rect.bottom - es->format_rect.top) {
2228 es->format_rect.top++;
2229 es->format_rect.bottom--;
2231 } else if (es->style & WS_BORDER) {
2232 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2233 es->format_rect.left += bw;
2234 es->format_rect.right -= bw;
2235 if (es->line_height + 2 * bw <=
2236 es->format_rect.bottom - es->format_rect.top) {
2237 es->format_rect.top += bw;
2238 es->format_rect.bottom -= bw;
2242 es->format_rect.left += es->left_margin;
2243 es->format_rect.right -= es->right_margin;
2244 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2245 if (es->style & ES_MULTILINE)
2247 INT fw, vlc, max_x_offset, max_y_offset;
2249 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2250 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2252 /* correct es->x_offset */
2253 fw = es->format_rect.right - es->format_rect.left;
2254 max_x_offset = es->text_width - fw;
2255 if(max_x_offset < 0) max_x_offset = 0;
2256 if(es->x_offset > max_x_offset)
2257 es->x_offset = max_x_offset;
2259 /* correct es->y_offset */
2260 max_y_offset = es->line_count - vlc;
2261 if(max_y_offset < 0) max_y_offset = 0;
2262 if(es->y_offset > max_y_offset)
2263 es->y_offset = max_y_offset;
2265 /* force scroll info update */
2266 EDIT_UpdateScrollInfo(es);
2269 /* Windows doesn't care to fix text placement for SL controls */
2270 es->format_rect.bottom = es->format_rect.top + es->line_height;
2272 /* Always stay within the client area */
2273 GetClientRect(es->hwndSelf, &ClientRect);
2274 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2276 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2277 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2279 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2283 /*********************************************************************
2288 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2290 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2292 /* Edit window might be already destroyed */
2293 if(!IsWindow(es->hwndSelf))
2295 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2299 if (!es->lock_count) {
2300 ERR("lock_count == 0 ... please report\n");
2304 ERR("es->text == 0 ... please report\n");
2308 if (force || (es->lock_count == 1)) {
2311 BOOL _16bit = FALSE;
2313 UINT countW = strlenW(es->text) + 1;
2317 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2318 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2319 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2320 countA = LocalSize(es->hloc32A);
2321 if(countA_new > countA)
2324 UINT alloc_size = ROUND_TO_GROW(countA_new);
2325 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2326 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2329 es->hloc32A = hloc32A_new;
2330 countA = LocalSize(hloc32A_new);
2331 TRACE("Real new size %d bytes\n", countA);
2334 WARN("FAILED! Will synchronize partially\n");
2336 textA = LocalLock(es->hloc32A);
2340 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2341 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2342 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2343 countA = LOCAL_Size(hInstance, es->hloc16);
2344 if(countA_new > countA)
2346 HLOCAL16 hloc16_new;
2347 UINT alloc_size = ROUND_TO_GROW(countA_new);
2348 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2349 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2352 es->hloc16 = hloc16_new;
2353 countA = LOCAL_Size(hInstance, hloc16_new);
2354 TRACE("Real new size %d bytes\n", countA);
2357 WARN("FAILED! Will synchronize partially\n");
2359 textA = LOCAL_Lock(hInstance, es->hloc16);
2365 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2367 LOCAL_Unlock(hInstance, es->hloc16);
2369 LocalUnlock(es->hloc32A);
2372 LocalUnlock(es->hloc32W);
2376 ERR("no buffer ... please report\n");
2384 /*********************************************************************
2386 * EDIT_UpdateScrollInfo
2389 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2391 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2394 si.cbSize = sizeof(SCROLLINFO);
2395 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2397 si.nMax = es->line_count - 1;
2398 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2399 si.nPos = es->y_offset;
2400 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2401 si.nMin, si.nMax, si.nPage, si.nPos);
2402 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2405 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2408 si.cbSize = sizeof(SCROLLINFO);
2409 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2411 si.nMax = es->text_width - 1;
2412 si.nPage = es->format_rect.right - es->format_rect.left;
2413 si.nPos = es->x_offset;
2414 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2415 si.nMin, si.nMax, si.nPage, si.nPos);
2416 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2420 /*********************************************************************
2422 * EDIT_WordBreakProc
2424 * Find the beginning of words.
2425 * Note: unlike the specs for a WordBreakProc, this function only
2426 * allows to be called without linebreaks between s[0] upto
2427 * s[count - 1]. Remember it is only called
2428 * internally, so we can decide this for ourselves.
2431 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2435 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2445 if (s[index] == ' ') {
2446 while (index && (s[index] == ' '))
2449 while (index && (s[index] != ' '))
2451 if (s[index] == ' ')
2455 while (index && (s[index] != ' '))
2457 if (s[index] == ' ')
2467 if (s[index] == ' ')
2468 while ((index < count) && (s[index] == ' ')) index++;
2470 while (s[index] && (s[index] != ' ') && (index < count))
2472 while ((s[index] == ' ') && (index < count)) index++;
2476 case WB_ISDELIMITER:
2477 ret = (s[index] == ' ');
2480 ERR("unknown action code, please report !\n");
2487 /*********************************************************************
2491 * returns line number (not index) in high-order word of result.
2492 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2493 * to Richedit, not to the edit control. Original documentation is valid.
2494 * FIXME: do the specs mean to return -1 if outside client area or
2495 * if outside formatting rectangle ???
2498 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2506 GetClientRect(es->hwndSelf, &rc);
2507 if (!PtInRect(&rc, pt))
2510 index = EDIT_CharFromPos(es, x, y, NULL);
2511 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2515 /*********************************************************************
2519 * Enable or disable soft breaks.
2521 * This means: insert or remove the soft linebreak character (\r\r\n).
2522 * Take care to check if the text still fits the buffer after insertion.
2523 * If not, notify with EN_ERRSPACE.
2526 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2528 es->flags &= ~EF_USE_SOFTBRK;
2530 es->flags |= EF_USE_SOFTBRK;
2531 FIXME("soft break enabled, not implemented\n");
2537 /*********************************************************************
2541 * Hopefully this won't fire back at us.
2542 * We always start with a fixed buffer in the local heap.
2543 * Despite of the documentation says that the local heap is used
2544 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2545 * buffer on the local heap.
2548 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2552 if (!(es->style & ES_MULTILINE))
2556 hLocal = es->hloc32W;
2562 UINT countA, alloc_size;
2563 TRACE("Allocating 32-bit ANSI alias buffer\n");
2564 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2565 alloc_size = ROUND_TO_GROW(countA);
2566 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2568 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2571 textA = LocalLock(es->hloc32A);
2572 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2573 LocalUnlock(es->hloc32A);
2575 hLocal = es->hloc32A;
2578 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2583 /*********************************************************************
2587 * Hopefully this won't fire back at us.
2588 * We always start with a buffer in 32 bit linear memory.
2589 * However, with this message a 16 bit application requests
2590 * a handle of 16 bit local heap memory, where it expects to find
2592 * It's a pitty that from this moment on we have to use this
2593 * local heap, because applications may rely on the handle
2596 * In this function we'll try to switch to local heap.
2598 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2600 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2602 UINT countA, alloc_size;
2604 if (!(es->style & ES_MULTILINE))
2610 if (!LOCAL_HeapSize(hInstance)) {
2611 if (!LocalInit16(hInstance, 0,
2612 GlobalSize16(hInstance))) {
2613 ERR("could not initialize local heap\n");
2616 TRACE("local heap initialized\n");
2619 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2620 alloc_size = ROUND_TO_GROW(countA);
2622 TRACE("Allocating 16-bit ANSI alias buffer\n");
2623 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2624 ERR("could not allocate new 16 bit buffer\n");
2628 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2629 ERR("could not lock new 16 bit buffer\n");
2630 LOCAL_Free(hInstance, es->hloc16);
2635 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2636 LOCAL_Unlock(hInstance, es->hloc16);
2638 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2643 /*********************************************************************
2648 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2651 INT line_len, dst_len;
2654 if (es->style & ES_MULTILINE) {
2655 if (line >= es->line_count)
2659 i = EDIT_EM_LineIndex(es, line);
2661 line_len = EDIT_EM_LineLength(es, i);
2662 dst_len = *(WORD *)dst;
2665 if(dst_len <= line_len)
2667 memcpy(dst, src, dst_len * sizeof(WCHAR));
2670 else /* Append 0 if enough space */
2672 memcpy(dst, src, line_len * sizeof(WCHAR));
2679 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2680 if(!ret) /* Insufficient buffer size */
2682 if(ret < dst_len) /* Append 0 if enough space */
2683 ((LPSTR)dst)[ret] = 0;
2689 /*********************************************************************
2694 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2696 UINT s = es->selection_start;
2697 UINT e = es->selection_end;
2704 return MAKELONG(s, e);
2708 /*********************************************************************
2712 * FIXME: is this right ? (or should it be only VSCROLL)
2713 * (and maybe only for edit controls that really have their
2714 * own scrollbars) (and maybe only for multiline controls ?)
2715 * All in all: very poorly documented
2718 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2720 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2721 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2725 /*********************************************************************
2730 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2735 if (!(es->style & ES_MULTILINE))
2737 if (index > (INT)strlenW(es->text))
2738 return es->line_count - 1;
2740 index = min(es->selection_start, es->selection_end);
2743 line_def = es->first_line_def;
2744 index -= line_def->length;
2745 while ((index >= 0) && line_def->next) {
2747 line_def = line_def->next;
2748 index -= line_def->length;
2754 /*********************************************************************
2759 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2764 if (!(es->style & ES_MULTILINE))
2766 if (line >= es->line_count)
2770 line_def = es->first_line_def;
2772 INT index = es->selection_end - line_def->length;
2773 while ((index >= 0) && line_def->next) {
2774 line_index += line_def->length;
2775 line_def = line_def->next;
2776 index -= line_def->length;
2780 line_index += line_def->length;
2781 line_def = line_def->next;
2789 /*********************************************************************
2794 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2798 if (!(es->style & ES_MULTILINE))
2799 return strlenW(es->text);
2802 /* get the number of remaining non-selected chars of selected lines */
2803 INT32 l; /* line number */
2804 INT32 li; /* index of first char in line */
2806 l = EDIT_EM_LineFromChar(es, es->selection_start);
2807 /* # chars before start of selection area */
2808 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2809 l = EDIT_EM_LineFromChar(es, es->selection_end);
2810 /* # chars after end of selection */
2811 li = EDIT_EM_LineIndex(es, l);
2812 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2815 line_def = es->first_line_def;
2816 index -= line_def->length;
2817 while ((index >= 0) && line_def->next) {
2818 line_def = line_def->next;
2819 index -= line_def->length;
2821 return line_def->net_length;
2825 /*********************************************************************
2829 * NOTE: dx is in average character widths, dy - in lines;
2832 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2834 if (!(es->style & ES_MULTILINE))
2837 dx *= es->char_width;
2838 return EDIT_EM_LineScroll_internal(es, dx, dy);
2841 /*********************************************************************
2843 * EDIT_EM_LineScroll_internal
2845 * Version of EDIT_EM_LineScroll for internal use.
2846 * It doesn't refuse if ES_MULTILINE is set and assumes that
2847 * dx is in pixels, dy - in lines.
2850 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2853 INT x_offset_in_pixels;
2854 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2857 if (es->style & ES_MULTILINE)
2859 x_offset_in_pixels = es->x_offset;
2864 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2867 if (-dx > x_offset_in_pixels)
2868 dx = -x_offset_in_pixels;
2869 if (dx > es->text_width - x_offset_in_pixels)
2870 dx = es->text_width - x_offset_in_pixels;
2871 nyoff = max(0, es->y_offset + dy);
2872 if (nyoff >= es->line_count - lines_per_page)
2873 nyoff = max(0, es->line_count - lines_per_page);
2874 dy = (es->y_offset - nyoff) * es->line_height;
2879 es->y_offset = nyoff;
2880 if(es->style & ES_MULTILINE)
2883 es->x_offset += dx / es->char_width;
2885 GetClientRect(es->hwndSelf, &rc1);
2886 IntersectRect(&rc, &rc1, &es->format_rect);
2887 ScrollWindowEx(es->hwndSelf, -dx, dy,
2888 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2889 /* force scroll info update */
2890 EDIT_UpdateScrollInfo(es);
2892 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2893 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2894 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2895 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2900 /*********************************************************************
2905 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2907 INT len = strlenW(es->text);
2920 index = min(index, len);
2921 dc = GetDC(es->hwndSelf);
2923 old_font = SelectObject(dc, es->font);
2924 if (es->style & ES_MULTILINE) {
2925 l = EDIT_EM_LineFromChar(es, index);
2926 y = (l - es->y_offset) * es->line_height;
2927 li = EDIT_EM_LineIndex(es, l);
2928 if (after_wrap && (li == index) && l) {
2930 line_def = es->first_line_def;
2932 line_def = line_def->next;
2935 if (line_def->ending == END_WRAP) {
2937 y -= es->line_height;
2938 li = EDIT_EM_LineIndex(es, l);
2942 line_def = es->first_line_def;
2943 while (line_def->index != li)
2944 line_def = line_def->next;
2946 ll = line_def->net_length;
2947 lw = line_def->width;
2949 w = es->format_rect.right - es->format_rect.left;
2950 if (es->style & ES_RIGHT)
2952 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
2953 es->tabs_count, es->tabs)) - es->x_offset;
2956 else if (es->style & ES_CENTER)
2958 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2959 es->tabs_count, es->tabs)) - es->x_offset;
2964 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2965 es->tabs_count, es->tabs)) - es->x_offset;
2968 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2969 if (index < es->x_offset) {
2970 GetTextExtentPoint32W(dc, text + index,
2971 es->x_offset - index, &size);
2974 GetTextExtentPoint32W(dc, text + es->x_offset,
2975 index - es->x_offset, &size);
2978 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
2980 w = es->format_rect.right - es->format_rect.left;
2981 if (w > es->text_width)
2983 if (es->style & ES_RIGHT)
2984 x += w - es->text_width;
2985 else if (es->style & ES_CENTER)
2986 x += (w - es->text_width) / 2;
2991 if (es->style & ES_PASSWORD)
2992 HeapFree(GetProcessHeap(), 0, text);
2994 x += es->format_rect.left;
2995 y += es->format_rect.top;
2997 SelectObject(dc, old_font);
2998 ReleaseDC(es->hwndSelf, dc);
2999 return MAKELONG((INT16)x, (INT16)y);
3003 /*********************************************************************
3007 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3010 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3012 UINT strl = strlenW(lpsz_replace);
3013 UINT tl = strlenW(es->text);
3024 TRACE("%s, can_undo %d, send_update %d\n",
3025 debugstr_w(lpsz_replace), can_undo, send_update);
3027 s = es->selection_start;
3028 e = es->selection_end;
3030 if ((s == e) && !strl)
3035 size = tl - (e - s) + strl;
3039 /* Issue the EN_MAXTEXT notification and continue with replacing text
3040 * such that buffer limit is honored. */
3041 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3042 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3043 strl = es->buffer_limit - (tl - (e-s));
3046 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3050 /* there is something to be deleted */
3051 TRACE("deleting stuff.\n");
3053 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3055 strncpyW(buf, es->text + s, bufl);
3056 buf[bufl] = 0; /* ensure 0 termination */
3058 strcpyW(es->text + s, es->text + e);
3061 /* there is an insertion */
3062 tl = strlenW(es->text);
3063 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));
3064 for (p = es->text + tl ; p >= es->text + s ; p--)
3066 for (i = 0 , p = es->text + s ; i < strl ; i++)
3067 p[i] = lpsz_replace[i];
3068 if(es->style & ES_UPPERCASE)
3069 CharUpperBuffW(p, strl);
3070 else if(es->style & ES_LOWERCASE)
3071 CharLowerBuffW(p, strl);
3073 if (es->style & ES_MULTILINE)
3075 INT st = min(es->selection_start, es->selection_end);
3076 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3078 hrgn = CreateRectRgn(0, 0, 0, 0);
3079 EDIT_BuildLineDefs_ML(es, st, st + strl,
3080 strl - abs(es->selection_end - es->selection_start), hrgn);
3081 /* if text is too long undo all changes */
3082 if (!(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3084 strcpyW(es->text + e, es->text + e + strl);
3086 for (i = 0 , p = es->text ; i < e - s ; i++)
3088 EDIT_BuildLineDefs_ML(es, s, e,
3089 abs(es->selection_end - es->selection_start) - strl, hrgn);
3092 hrgn = CreateRectRgn(0, 0, 0, 0);
3093 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3097 INT fw = es->format_rect.right - es->format_rect.left;
3098 EDIT_CalcLineWidth_SL(es);
3099 /* remove chars that don't fit */
3100 if (!(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3101 while ((es->text_width > fw) && s + strl >= s) {
3102 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3104 EDIT_CalcLineWidth_SL(es);
3106 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3112 utl = strlenW(es->undo_text);
3113 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3114 /* undo-buffer is extended to the right */
3115 EDIT_MakeUndoFit(es, utl + e - s);
3116 strncpyW(es->undo_text + utl, buf, e - s + 1);
3117 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3118 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3119 /* undo-buffer is extended to the left */
3120 EDIT_MakeUndoFit(es, utl + e - s);
3121 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3123 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3125 es->undo_position = s;
3127 /* new undo-buffer */
3128 EDIT_MakeUndoFit(es, e - s);
3129 strncpyW(es->undo_text, buf, e - s + 1);
3130 es->undo_text[e - s] = 0; /* ensure 0 termination */
3131 es->undo_position = s;
3133 /* any deletion makes the old insertion-undo invalid */
3134 es->undo_insert_count = 0;
3136 EDIT_EM_EmptyUndoBuffer(es);
3140 if ((s == es->undo_position) ||
3141 ((es->undo_insert_count) &&
3142 (s == es->undo_position + es->undo_insert_count)))
3144 * insertion is new and at delete position or
3145 * an extension to either left or right
3147 es->undo_insert_count += strl;
3149 /* new insertion undo */
3150 es->undo_position = s;
3151 es->undo_insert_count = strl;
3152 /* new insertion makes old delete-buffer invalid */
3153 *es->undo_text = '\0';
3156 EDIT_EM_EmptyUndoBuffer(es);
3160 HeapFree(GetProcessHeap(), 0, buf);
3164 /* If text has been deleted and we're right or center aligned then scroll rightward */
3165 if (es->style & (ES_RIGHT | ES_CENTER))
3167 INT delta = strl - abs(es->selection_end - es->selection_start);
3169 if (delta < 0 && es->x_offset)
3171 if (abs(delta) > es->x_offset)
3174 es->x_offset += delta;
3178 EDIT_EM_SetSel(es, s, s, FALSE);
3179 es->flags |= EF_MODIFIED;
3180 if (send_update) es->flags |= EF_UPDATE;
3183 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3187 EDIT_UpdateText(es, NULL, TRUE);
3189 EDIT_EM_ScrollCaret(es);
3191 /* force scroll info update */
3192 EDIT_UpdateScrollInfo(es);
3195 if(es->flags & EF_UPDATE)
3197 es->flags &= ~EF_UPDATE;
3198 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3203 /*********************************************************************
3208 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3212 if (!(es->style & ES_MULTILINE))
3213 return (LRESULT)FALSE;
3223 if (es->y_offset < es->line_count - 1)
3228 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3231 if (es->y_offset < es->line_count - 1)
3232 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3235 return (LRESULT)FALSE;
3238 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3239 /* check if we are going to move too far */
3240 if(es->y_offset + dy > es->line_count - vlc)
3241 dy = es->line_count - vlc - es->y_offset;
3243 /* Notification is done in EDIT_EM_LineScroll */
3245 EDIT_EM_LineScroll(es, 0, dy);
3247 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3251 /*********************************************************************
3256 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3258 if (es->style & ES_MULTILINE) {
3263 INT cw = es->char_width;
3268 l = EDIT_EM_LineFromChar(es, es->selection_end);
3269 li = EDIT_EM_LineIndex(es, l);
3270 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3271 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3272 if (l >= es->y_offset + vlc)
3273 dy = l - vlc + 1 - es->y_offset;
3274 if (l < es->y_offset)
3275 dy = l - es->y_offset;
3276 ww = es->format_rect.right - es->format_rect.left;
3277 if (x < es->format_rect.left)
3278 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3279 if (x > es->format_rect.right)
3280 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3281 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3283 /* check if we are going to move too far */
3284 if(es->x_offset + dx + ww > es->text_width)
3285 dx = es->text_width - ww - es->x_offset;
3286 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3287 EDIT_EM_LineScroll_internal(es, dx, dy);
3294 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3295 format_width = es->format_rect.right - es->format_rect.left;
3296 if (x < es->format_rect.left) {
3297 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3300 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3301 } while ((x < goal) && es->x_offset);
3302 /* FIXME: use ScrollWindow() somehow to improve performance */
3303 EDIT_UpdateText(es, NULL, TRUE);
3304 } else if (x > es->format_rect.right) {
3306 INT len = strlenW(es->text);
3307 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3310 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3311 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3312 } while ((x > goal) && (x_last > es->format_rect.right));
3313 /* FIXME: use ScrollWindow() somehow to improve performance */
3314 EDIT_UpdateText(es, NULL, TRUE);
3318 if(es->flags & EF_FOCUSED)
3319 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3323 /*********************************************************************
3327 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3330 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3332 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3334 if (!(es->style & ES_MULTILINE))
3338 WARN("called with NULL handle\n");
3342 EDIT_UnlockBuffer(es, TRUE);
3346 LOCAL_Free(hInstance, es->hloc16);
3347 es->hloc16 = (HLOCAL16)NULL;
3354 LocalFree(es->hloc32A);
3366 countA = LocalSize(hloc);
3367 textA = LocalLock(hloc);
3368 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3369 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3371 ERR("Could not allocate new unicode buffer\n");
3374 textW = LocalLock(hloc32W_new);
3375 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3376 LocalUnlock(hloc32W_new);
3380 LocalFree(es->hloc32W);
3382 es->hloc32W = hloc32W_new;
3386 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3388 EDIT_LockBuffer(es);
3390 es->x_offset = es->y_offset = 0;
3391 es->selection_start = es->selection_end = 0;
3392 EDIT_EM_EmptyUndoBuffer(es);
3393 es->flags &= ~EF_MODIFIED;
3394 es->flags &= ~EF_UPDATE;
3395 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3396 EDIT_UpdateText(es, NULL, TRUE);
3397 EDIT_EM_ScrollCaret(es);
3398 /* force scroll info update */
3399 EDIT_UpdateScrollInfo(es);
3403 /*********************************************************************
3407 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3410 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3412 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3418 if (!(es->style & ES_MULTILINE))
3422 WARN("called with NULL handle\n");
3426 EDIT_UnlockBuffer(es, TRUE);
3430 LocalFree(es->hloc32A);
3434 countA = LOCAL_Size(hInstance, hloc);
3435 textA = LOCAL_Lock(hInstance, hloc);
3436 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3437 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3439 ERR("Could not allocate new unicode buffer\n");
3442 textW = LocalLock(hloc32W_new);
3443 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3444 LocalUnlock(hloc32W_new);
3445 LOCAL_Unlock(hInstance, hloc);
3448 LocalFree(es->hloc32W);
3450 es->hloc32W = hloc32W_new;
3453 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3455 EDIT_LockBuffer(es);
3457 es->x_offset = es->y_offset = 0;
3458 es->selection_start = es->selection_end = 0;
3459 EDIT_EM_EmptyUndoBuffer(es);
3460 es->flags &= ~EF_MODIFIED;
3461 es->flags &= ~EF_UPDATE;
3462 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3463 EDIT_UpdateText(es, NULL, TRUE);
3464 EDIT_EM_ScrollCaret(es);
3465 /* force scroll info update */
3466 EDIT_UpdateScrollInfo(es);
3470 /*********************************************************************
3474 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3475 * However, the windows version is not complied to yet in all of edit.c
3477 * Additionally as the wrapper for RichEdit controls we need larger buffers
3478 * at present -1 will represent nolimit
3480 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3482 if (limit == 0xFFFFFFFF)
3483 es->buffer_limit = -1;
3484 else if (es->style & ES_MULTILINE) {
3486 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3488 es->buffer_limit = BUFLIMIT_MULTI;
3491 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3493 es->buffer_limit = BUFLIMIT_SINGLE;
3498 /*********************************************************************
3502 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3503 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3504 * margin according to the textmetrics of the current font.
3506 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3507 * of the char's width as the margin, but this is not how Windows handles this.
3508 * For all other fonts Windows sets the margins to zero.
3511 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3512 INT left, INT right)
3515 INT default_left_margin = 0; /* in pixels */
3516 INT default_right_margin = 0; /* in pixels */
3518 /* Set the default margins depending on the font */
3519 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3520 HDC dc = GetDC(es->hwndSelf);
3521 HFONT old_font = SelectObject(dc, es->font);
3522 GetTextMetricsW(dc, &tm);
3523 /* The default margins are only non zero for TrueType or Vector fonts */
3524 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3525 /* This must be calculated more exactly! But how? */
3526 default_left_margin = tm.tmAveCharWidth / 3;
3527 default_right_margin = tm.tmAveCharWidth / 3;
3529 SelectObject(dc, old_font);
3530 ReleaseDC(es->hwndSelf, dc);
3533 if (action & EC_LEFTMARGIN) {
3534 if (left != EC_USEFONTINFO)
3535 es->left_margin = left;
3537 es->left_margin = default_left_margin;
3540 if (action & EC_RIGHTMARGIN) {
3541 if (right != EC_USEFONTINFO)
3542 es->right_margin = right;
3544 es->right_margin = default_right_margin;
3546 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3550 /*********************************************************************
3552 * EM_SETPASSWORDCHAR
3555 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3559 if (es->style & ES_MULTILINE)
3562 if (es->password_char == c)
3565 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3566 es->password_char = c;
3568 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3569 es->style |= ES_PASSWORD;
3571 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3572 es->style &= ~ES_PASSWORD;
3574 EDIT_UpdateText(es, NULL, TRUE);
3578 /*********************************************************************
3582 * note: unlike the specs say: the order of start and end
3583 * _is_ preserved in Windows. (i.e. start can be > end)
3584 * In other words: this handler is OK
3587 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3589 UINT old_start = es->selection_start;
3590 UINT old_end = es->selection_end;
3591 UINT len = strlenW(es->text);
3593 if (start == (UINT)-1) {
3594 start = es->selection_end;
3595 end = es->selection_end;
3597 start = min(start, len);
3598 end = min(end, len);
3600 es->selection_start = start;
3601 es->selection_end = end;
3603 es->flags |= EF_AFTER_WRAP;
3605 es->flags &= ~EF_AFTER_WRAP;
3606 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3607 ORDER_UINT(start, end);
3608 ORDER_UINT(end, old_end);
3609 ORDER_UINT(start, old_start);
3610 ORDER_UINT(old_start, old_end);
3611 if (end != old_start)
3615 * ORDER_UINT32(end, old_start);
3616 * EDIT_InvalidateText(es, start, end);
3617 * EDIT_InvalidateText(es, old_start, old_end);
3618 * in place of the following if statement.
3620 if (old_start > end )
3622 EDIT_InvalidateText(es, start, end);
3623 EDIT_InvalidateText(es, old_start, old_end);
3627 EDIT_InvalidateText(es, start, old_start);
3628 EDIT_InvalidateText(es, end, old_end);
3631 else EDIT_InvalidateText(es, start, old_end);
3635 /*********************************************************************
3640 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3642 if (!(es->style & ES_MULTILINE))
3644 HeapFree(GetProcessHeap(), 0, es->tabs);
3645 es->tabs_count = count;
3649 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3650 memcpy(es->tabs, tabs, count * sizeof(INT));
3656 /*********************************************************************
3661 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3663 if (!(es->style & ES_MULTILINE))
3665 HeapFree(GetProcessHeap(), 0, es->tabs);
3666 es->tabs_count = count;
3671 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3672 for (i = 0 ; i < count ; i++)
3673 es->tabs[i] = *tabs++;
3679 /*********************************************************************
3681 * EM_SETWORDBREAKPROC
3684 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3686 if (es->word_break_proc == wbp)
3689 es->word_break_proc = wbp;
3690 es->word_break_proc16 = NULL;
3692 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3693 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3694 EDIT_UpdateText(es, NULL, TRUE);
3699 /*********************************************************************
3701 * EM_SETWORDBREAKPROC16
3704 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3706 if (es->word_break_proc16 == wbp)
3709 es->word_break_proc = NULL;
3710 es->word_break_proc16 = wbp;
3711 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3712 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3713 EDIT_UpdateText(es, NULL, TRUE);
3718 /*********************************************************************
3723 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3728 /* As per MSDN spec, for a single-line edit control,
3729 the return value is always TRUE */
3730 if( es->style & ES_READONLY )
3731 return !(es->style & ES_MULTILINE);
3733 ulength = strlenW(es->undo_text);
3735 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3737 strcpyW(utext, es->undo_text);
3739 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3740 es->undo_insert_count, debugstr_w(utext));
3742 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3743 EDIT_EM_EmptyUndoBuffer(es);
3744 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3745 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3746 /* send the notification after the selection start and end are set */
3747 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3748 EDIT_EM_ScrollCaret(es);
3749 HeapFree(GetProcessHeap(), 0, utext);
3751 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3752 es->undo_insert_count, debugstr_w(es->undo_text));
3757 /*********************************************************************
3762 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3766 control = GetKeyState(VK_CONTROL) & 0x8000;
3770 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3771 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3774 if (es->style & ES_MULTILINE) {
3775 if (es->style & ES_READONLY) {
3776 EDIT_MoveHome(es, FALSE);
3777 EDIT_MoveDown_ML(es, FALSE);
3779 static const WCHAR cr_lfW[] = {'\r','\n',0};
3780 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3785 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3787 static const WCHAR tabW[] = {'\t',0};
3788 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3792 if (!(es->style & ES_READONLY) && !control) {
3793 if (es->selection_start != es->selection_end)
3796 /* delete character left of caret */
3797 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3798 EDIT_MoveBackward(es, TRUE);
3804 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3807 if (!(es->style & ES_READONLY))
3808 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3811 if (!(es->style & ES_READONLY))
3812 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3816 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3817 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3820 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3824 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3831 /*********************************************************************
3836 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3838 if (code || control)
3858 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3859 EDIT_EM_ScrollCaret(es);
3862 ERR("unknown menu item, please report\n");
3868 /*********************************************************************
3872 * Note: the resource files resource/sysres_??.rc cannot define a
3873 * single popup menu. Hence we use a (dummy) menubar
3874 * containing the single popup menu as its first item.
3876 * FIXME: the message identifiers have been chosen arbitrarily,
3877 * hence we use MF_BYPOSITION.
3878 * We might as well use the "real" values (anybody knows ?)
3879 * The menu definition is in resources/sysres_??.rc.
3880 * Once these are OK, we better use MF_BYCOMMAND here
3881 * (as we do in EDIT_WM_Command()).
3884 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3886 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3887 HMENU popup = GetSubMenu(menu, 0);
3888 UINT start = es->selection_start;
3889 UINT end = es->selection_end;
3891 ORDER_UINT(start, end);
3894 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3896 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3898 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3900 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3902 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3904 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3906 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3911 /*********************************************************************
3916 static void EDIT_WM_Copy(EDITSTATE *es)
3918 INT s = min(es->selection_start, es->selection_end);
3919 INT e = max(es->selection_start, es->selection_end);
3925 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3926 dst = GlobalLock(hdst);
3927 strncpyW(dst, es->text + s, e - s);
3928 dst[e - s] = 0; /* ensure 0 termination */
3929 TRACE("%s\n", debugstr_w(dst));
3931 OpenClipboard(es->hwndSelf);
3933 SetClipboardData(CF_UNICODETEXT, hdst);
3938 /*********************************************************************
3943 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3945 TRACE("%s\n", debugstr_w(name));
3947 * To initialize some final structure members, we call some helper
3948 * functions. However, since the EDITSTATE is not consistent (i.e.
3949 * not fully initialized), we should be very careful which
3950 * functions can be called, and in what order.
3952 EDIT_WM_SetFont(es, 0, FALSE);
3953 EDIT_EM_EmptyUndoBuffer(es);
3955 if (name && *name) {
3956 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3957 /* if we insert text to the editline, the text scrolls out
3958 * of the window, as the caret is placed after the insert
3959 * pos normally; thus we reset es->selection... to 0 and
3962 es->selection_start = es->selection_end = 0;
3963 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3964 * Messages are only to be sent when the USER does something to
3965 * change the contents. So I am removing this EN_CHANGE
3967 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3969 EDIT_EM_ScrollCaret(es);
3971 /* force scroll info update */
3972 EDIT_UpdateScrollInfo(es);
3973 /* The rule seems to return 1 here for success */
3974 /* Power Builder masked edit controls will crash */
3976 /* FIXME: is that in all cases so ? */
3981 /*********************************************************************
3986 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3991 while (LocalUnlock(es->hloc32W)) ;
3992 LocalFree(es->hloc32W);
3995 while (LocalUnlock(es->hloc32A)) ;
3996 LocalFree(es->hloc32A);
3999 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4000 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
4001 LOCAL_Free(hInstance, es->hloc16);
4004 pc = es->first_line_def;
4008 HeapFree(GetProcessHeap(), 0, pc);
4012 SetWindowLongW( es->hwndSelf, 0, 0 );
4013 HeapFree(GetProcessHeap(), 0, es);
4019 /*********************************************************************
4024 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4026 /* we do the proper erase in EDIT_WM_Paint */
4031 /*********************************************************************
4036 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4038 if(!count) return 0;
4042 lstrcpynW(dst, es->text, count);
4043 return strlenW(dst);
4047 LPSTR textA = (LPSTR)dst;
4048 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4049 textA[count - 1] = 0; /* ensure 0 termination */
4050 return strlen(textA);
4054 /*********************************************************************
4059 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4064 if (!(es->style & ES_MULTILINE))
4067 if (!(es->style & ES_AUTOHSCROLL))
4071 fw = es->format_rect.right - es->format_rect.left;
4074 TRACE("SB_LINELEFT\n");
4076 dx = -es->char_width;
4079 TRACE("SB_LINERIGHT\n");
4080 if (es->x_offset < es->text_width)
4081 dx = es->char_width;
4084 TRACE("SB_PAGELEFT\n");
4086 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4089 TRACE("SB_PAGERIGHT\n");
4090 if (es->x_offset < es->text_width)
4091 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4099 TRACE("SB_RIGHT\n");
4100 if (es->x_offset < es->text_width)
4101 dx = es->text_width - es->x_offset;
4104 TRACE("SB_THUMBTRACK %d\n", pos);
4105 es->flags |= EF_HSCROLL_TRACK;
4106 if(es->style & WS_HSCROLL)
4107 dx = pos - es->x_offset;
4112 if(pos < 0 || pos > 100) return 0;
4113 /* Assume default scroll range 0-100 */
4114 fw = es->format_rect.right - es->format_rect.left;
4115 new_x = pos * (es->text_width - fw) / 100;
4116 dx = es->text_width ? (new_x - es->x_offset) : 0;
4119 case SB_THUMBPOSITION:
4120 TRACE("SB_THUMBPOSITION %d\n", pos);
4121 es->flags &= ~EF_HSCROLL_TRACK;
4122 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4123 dx = pos - es->x_offset;
4128 if(pos < 0 || pos > 100) return 0;
4129 /* Assume default scroll range 0-100 */
4130 fw = es->format_rect.right - es->format_rect.left;
4131 new_x = pos * (es->text_width - fw) / 100;
4132 dx = es->text_width ? (new_x - es->x_offset) : 0;
4135 /* force scroll info update */
4136 EDIT_UpdateScrollInfo(es);
4137 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
4141 TRACE("SB_ENDSCROLL\n");
4144 * FIXME : the next two are undocumented !
4145 * Are we doing the right thing ?
4146 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4147 * although it's also a regular control message.
4149 case EM_GETTHUMB: /* this one is used by NT notepad */
4153 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4154 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4157 /* Assume default scroll range 0-100 */
4158 INT fw = es->format_rect.right - es->format_rect.left;
4159 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4161 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4164 case EM_LINESCROLL16:
4165 TRACE("EM_LINESCROLL16\n");
4170 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4176 INT fw = es->format_rect.right - es->format_rect.left;
4177 /* check if we are going to move too far */
4178 if(es->x_offset + dx + fw > es->text_width)
4179 dx = es->text_width - fw - es->x_offset;
4181 EDIT_EM_LineScroll_internal(es, dx, 0);
4187 /*********************************************************************
4192 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4194 HWND hLBox = es->hwndListBox;
4202 hCombo = GetParent(es->hwndSelf);
4206 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4208 if (key == VK_UP || key == VK_DOWN)
4210 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4213 if (msg == WM_KEYDOWN || nEUI)
4214 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4220 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4222 /* make sure ComboLBox pops up */
4223 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4228 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4231 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4233 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4235 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4240 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4246 /*********************************************************************
4250 * Handling of special keys that don't produce a WM_CHAR
4251 * (i.e. non-printable keys) & Backspace & Delete
4254 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4259 if (GetKeyState(VK_MENU) & 0x8000)
4262 shift = GetKeyState(VK_SHIFT) & 0x8000;
4263 control = GetKeyState(VK_CONTROL) & 0x8000;
4268 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4273 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4274 EDIT_MoveUp_ML(es, shift);
4277 EDIT_MoveWordBackward(es, shift);
4279 EDIT_MoveBackward(es, shift);
4282 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4286 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4287 EDIT_MoveDown_ML(es, shift);
4289 EDIT_MoveWordForward(es, shift);
4291 EDIT_MoveForward(es, shift);
4294 EDIT_MoveHome(es, shift);
4297 EDIT_MoveEnd(es, shift);
4300 if (es->style & ES_MULTILINE)
4301 EDIT_MovePageUp_ML(es, shift);
4303 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4306 if (es->style & ES_MULTILINE)
4307 EDIT_MovePageDown_ML(es, shift);
4309 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4312 if (!(es->style & ES_READONLY) && !(shift && control)) {
4313 if (es->selection_start != es->selection_end) {
4320 /* delete character left of caret */
4321 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4322 EDIT_MoveBackward(es, TRUE);
4324 } else if (control) {
4325 /* delete to end of line */
4326 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4327 EDIT_MoveEnd(es, TRUE);
4330 /* delete character right of caret */
4331 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4332 EDIT_MoveForward(es, TRUE);
4340 if (!(es->style & ES_READONLY))
4346 /* If the edit doesn't want the return send a message to the default object */
4347 if(!(es->style & ES_WANTRETURN))
4349 HWND hwndParent = GetParent(es->hwndSelf);
4350 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4351 if (HIWORD(dw) == DC_HASDEFID)
4353 SendMessageW( hwndParent, WM_COMMAND,
4354 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4355 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4364 /*********************************************************************
4369 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4371 es->flags &= ~EF_FOCUSED;
4373 if(!(es->style & ES_NOHIDESEL))
4374 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4375 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4380 /*********************************************************************
4384 * The caret position has been set on the WM_LBUTTONDOWN message
4387 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4390 INT e = es->selection_end;
4395 if (!(es->flags & EF_FOCUSED))
4398 l = EDIT_EM_LineFromChar(es, e);
4399 li = EDIT_EM_LineIndex(es, l);
4400 ll = EDIT_EM_LineLength(es, e);
4401 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4402 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4403 EDIT_EM_SetSel(es, s, e, FALSE);
4404 EDIT_EM_ScrollCaret(es);
4409 /*********************************************************************
4414 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4419 SetFocus(es->hwndSelf);
4420 if (!(es->flags & EF_FOCUSED))
4423 es->bCaptureState = TRUE;
4424 SetCapture(es->hwndSelf);
4425 EDIT_ConfinePoint(es, &x, &y);
4426 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4427 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4428 EDIT_EM_ScrollCaret(es);
4429 es->region_posx = es->region_posy = 0;
4430 SetTimer(es->hwndSelf, 0, 100, NULL);
4435 /*********************************************************************
4440 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4442 if (es->bCaptureState) {
4443 KillTimer(es->hwndSelf, 0);
4444 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4446 es->bCaptureState = FALSE;
4451 /*********************************************************************
4456 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4458 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4463 /*********************************************************************
4468 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4474 if (GetCapture() != es->hwndSelf)
4478 * FIXME: gotta do some scrolling if outside client
4479 * area. Maybe reset the timer ?
4482 EDIT_ConfinePoint(es, &x, &y);
4483 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4484 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4485 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4486 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4487 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4492 /*********************************************************************
4496 * See also EDIT_WM_StyleChanged
4498 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4503 TRACE("Creating %s edit control, style = %08lx\n",
4504 unicode ? "Unicode" : "ANSI", lpcs->style);
4506 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4508 SetWindowLongW( hwnd, 0, (LONG)es );
4511 * Note: since the EDITSTATE has not been fully initialized yet,
4512 * we can't use any API calls that may send
4513 * WM_XXX messages before WM_NCCREATE is completed.
4516 es->is_unicode = unicode;
4517 es->style = lpcs->style;
4519 es->bEnableState = !(es->style & WS_DISABLED);
4521 es->hwndSelf = hwnd;
4522 /* Save parent, which will be notified by EN_* messages */
4523 es->hwndParent = lpcs->hwndParent;
4525 if (es->style & ES_COMBO)
4526 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4528 /* Number overrides lowercase overrides uppercase (at least it
4529 * does in Win95). However I'll bet that ES_NUMBER would be
4530 * invalid under Win 3.1.
4532 if (es->style & ES_NUMBER) {
4533 ; /* do not override the ES_NUMBER */
4534 } else if (es->style & ES_LOWERCASE) {
4535 es->style &= ~ES_UPPERCASE;
4537 if (es->style & ES_MULTILINE) {
4538 es->buffer_limit = BUFLIMIT_MULTI;
4539 if (es->style & WS_VSCROLL)
4540 es->style |= ES_AUTOVSCROLL;
4541 if (es->style & WS_HSCROLL)
4542 es->style |= ES_AUTOHSCROLL;
4543 es->style &= ~ES_PASSWORD;
4544 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4545 /* Confirmed - RIGHT overrides CENTER */
4546 if (es->style & ES_RIGHT)
4547 es->style &= ~ES_CENTER;
4548 es->style &= ~WS_HSCROLL;
4549 es->style &= ~ES_AUTOHSCROLL;
4552 es->buffer_limit = BUFLIMIT_SINGLE;
4553 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4554 es->style &= ~ES_CENTER;
4555 es->style &= ~WS_HSCROLL;
4556 es->style &= ~WS_VSCROLL;
4557 if (es->style & ES_PASSWORD)
4558 es->password_char = '*';
4561 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4562 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4564 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4566 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4568 es->undo_buffer_size = es->buffer_size;
4570 if (es->style & ES_MULTILINE)
4571 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4576 * In Win95 look and feel, the WS_BORDER style is replaced by the
4577 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4578 * control a nonclient area so we don't need to draw the border.
4579 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4580 * a nonclient area and we should handle painting the border ourselves.
4582 * When making modifications please ensure that the code still works
4583 * for edit controls created directly with style 0x50800000, exStyle 0
4584 * (which should have a single pixel border)
4586 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4587 es->style &= ~WS_BORDER;
4588 else if (es->style & WS_BORDER)
4589 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4594 /*********************************************************************
4599 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4610 BOOL rev = es->bEnableState &&
4611 ((es->flags & EF_FOCUSED) ||
4612 (es->style & ES_NOHIDESEL));
4613 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4615 GetClientRect(es->hwndSelf, &rcClient);
4617 /* paint the background */
4618 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
4619 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
4620 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4621 GetClipBox(dc, &rc);
4622 FillRect(dc, &rc, brush);
4624 /* draw the border */
4625 if(es->style & WS_BORDER) {
4627 if(es->style & ES_MULTILINE) {
4628 if(es->style & WS_HSCROLL) rc.bottom++;
4629 if(es->style & WS_VSCROLL) rc.right++;
4631 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4633 IntersectClipRect(dc, es->format_rect.left,
4634 es->format_rect.top,
4635 es->format_rect.right,
4636 es->format_rect.bottom);
4637 if (es->style & ES_MULTILINE) {
4639 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4642 old_font = SelectObject(dc, es->font);
4643 EDIT_NotifyCtlColor(es, dc);
4645 if (!es->bEnableState)
4646 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4647 GetClipBox(dc, &rcRgn);
4648 if (es->style & ES_MULTILINE) {
4649 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4650 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4651 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4652 if (IntersectRect(&rc, &rcRgn, &rcLine))
4653 EDIT_PaintLine(es, dc, i, rev);
4656 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4657 if (IntersectRect(&rc, &rcRgn, &rcLine))
4658 EDIT_PaintLine(es, dc, 0, rev);
4661 SelectObject(dc, old_font);
4664 EndPaint(es->hwndSelf, &ps);
4668 /*********************************************************************
4673 static void EDIT_WM_Paste(EDITSTATE *es)
4678 /* Protect read-only edit control from modification */
4679 if(es->style & ES_READONLY)
4682 OpenClipboard(es->hwndSelf);
4683 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4684 src = (LPWSTR)GlobalLock(hsrc);
4685 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4692 /*********************************************************************
4697 static void EDIT_WM_SetFocus(EDITSTATE *es)
4699 es->flags |= EF_FOCUSED;
4700 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4701 EDIT_SetCaretPos(es, es->selection_end,
4702 es->flags & EF_AFTER_WRAP);
4703 if(!(es->style & ES_NOHIDESEL))
4704 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4705 ShowCaret(es->hwndSelf);
4706 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4710 /*********************************************************************
4714 * With Win95 look the margins are set to default font value unless
4715 * the system font (font == 0) is being set, in which case they are left
4719 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4727 dc = GetDC(es->hwndSelf);
4729 old_font = SelectObject(dc, font);
4730 GetTextMetricsW(dc, &tm);
4731 es->line_height = tm.tmHeight;
4732 es->char_width = tm.tmAveCharWidth;
4734 SelectObject(dc, old_font);
4735 ReleaseDC(es->hwndSelf, dc);
4736 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4737 EC_USEFONTINFO, EC_USEFONTINFO);
4739 /* Force the recalculation of the format rect for each font change */
4740 GetClientRect(es->hwndSelf, &r);
4741 EDIT_SetRectNP(es, &r);
4743 if (es->style & ES_MULTILINE)
4744 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4746 EDIT_CalcLineWidth_SL(es);
4749 EDIT_UpdateText(es, NULL, TRUE);
4750 if (es->flags & EF_FOCUSED) {
4752 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4753 EDIT_SetCaretPos(es, es->selection_end,
4754 es->flags & EF_AFTER_WRAP);
4755 ShowCaret(es->hwndSelf);
4760 /*********************************************************************
4765 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4766 * The modified flag is reset. No notifications are sent.
4768 * For single-line controls, reception of WM_SETTEXT triggers:
4769 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4772 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4774 if (!unicode && text)
4776 LPCSTR textA = (LPCSTR)text;
4777 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4778 LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
4780 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4784 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4787 TRACE("%s\n", debugstr_w(text));
4788 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4790 HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
4794 static const WCHAR empty_stringW[] = {0};
4796 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4799 es->flags &= ~EF_MODIFIED;
4800 EDIT_EM_SetSel(es, 0, 0, FALSE);
4802 /* Send the notification after the selection start and end have been set
4803 * edit control doesn't send notification on WM_SETTEXT
4804 * if it is multiline, or it is part of combobox
4806 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4808 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4809 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4811 EDIT_EM_ScrollCaret(es);
4812 EDIT_UpdateScrollInfo(es);
4816 /*********************************************************************
4821 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4823 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4825 TRACE("width = %d, height = %d\n", width, height);
4826 SetRect(&rc, 0, 0, width, height);
4827 EDIT_SetRectNP(es, &rc);
4828 EDIT_UpdateText(es, NULL, TRUE);
4833 /*********************************************************************
4837 * This message is sent by SetWindowLong on having changed either the Style
4838 * or the extended style.
4840 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4842 * See also EDIT_WM_NCCreate
4844 * It appears that the Windows version of the edit control allows the style
4845 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4846 * style variable which will generally be different. In this function we
4847 * update the internal style based on what changed in the externally visible
4850 * Much of this content as based upon the MSDN, especially:
4851 * Platform SDK Documentation -> User Interface Services ->
4852 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4853 * Edit Control Styles
4855 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4857 if (GWL_STYLE == which) {
4858 DWORD style_change_mask;
4860 /* Only a subset of changes can be applied after the control
4863 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4865 if (es->style & ES_MULTILINE)
4866 style_change_mask |= ES_WANTRETURN;
4868 new_style = style->styleNew & style_change_mask;
4870 /* Number overrides lowercase overrides uppercase (at least it
4871 * does in Win95). However I'll bet that ES_NUMBER would be
4872 * invalid under Win 3.1.
4874 if (new_style & ES_NUMBER) {
4875 ; /* do not override the ES_NUMBER */
4876 } else if (new_style & ES_LOWERCASE) {
4877 new_style &= ~ES_UPPERCASE;
4880 es->style = (es->style & ~style_change_mask) | new_style;
4881 } else if (GWL_EXSTYLE == which) {
4882 ; /* FIXME - what is needed here */
4884 WARN ("Invalid style change %d\n",which);
4890 /*********************************************************************
4895 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4897 if ((key == VK_BACK) && (key_data & 0x2000)) {
4898 if (EDIT_EM_CanUndo(es))
4901 } else if (key == VK_UP || key == VK_DOWN) {
4902 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4905 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4909 /*********************************************************************
4914 static void EDIT_WM_Timer(EDITSTATE *es)
4916 if (es->region_posx < 0) {
4917 EDIT_MoveBackward(es, TRUE);
4918 } else if (es->region_posx > 0) {
4919 EDIT_MoveForward(es, TRUE);
4922 * FIXME: gotta do some vertical scrolling here, like
4923 * EDIT_EM_LineScroll(hwnd, 0, 1);
4927 /*********************************************************************
4932 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4936 if (!(es->style & ES_MULTILINE))
4939 if (!(es->style & ES_AUTOVSCROLL))
4948 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4949 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4950 (action == SB_PAGEUP ? "SB_PAGEUP" :
4952 EDIT_EM_Scroll(es, action);
4959 TRACE("SB_BOTTOM\n");
4960 dy = es->line_count - 1 - es->y_offset;
4963 TRACE("SB_THUMBTRACK %d\n", pos);
4964 es->flags |= EF_VSCROLL_TRACK;
4965 if(es->style & WS_VSCROLL)
4966 dy = pos - es->y_offset;
4969 /* Assume default scroll range 0-100 */
4972 if(pos < 0 || pos > 100) return 0;
4973 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4974 new_y = pos * (es->line_count - vlc) / 100;
4975 dy = es->line_count ? (new_y - es->y_offset) : 0;
4976 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4977 es->line_count, es->y_offset, pos, dy);
4980 case SB_THUMBPOSITION:
4981 TRACE("SB_THUMBPOSITION %d\n", pos);
4982 es->flags &= ~EF_VSCROLL_TRACK;
4983 if(es->style & WS_VSCROLL)
4984 dy = pos - es->y_offset;
4987 /* Assume default scroll range 0-100 */
4990 if(pos < 0 || pos > 100) return 0;
4991 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4992 new_y = pos * (es->line_count - vlc) / 100;
4993 dy = es->line_count ? (new_y - es->y_offset) : 0;
4994 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4995 es->line_count, es->y_offset, pos, dy);
4999 /* force scroll info update */
5000 EDIT_UpdateScrollInfo(es);
5001 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
5005 TRACE("SB_ENDSCROLL\n");
5008 * FIXME : the next two are undocumented !
5009 * Are we doing the right thing ?
5010 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5011 * although it's also a regular control message.
5013 case EM_GETTHUMB: /* this one is used by NT notepad */
5017 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5018 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5021 /* Assume default scroll range 0-100 */
5022 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5023 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5025 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5028 case EM_LINESCROLL16:
5029 TRACE("EM_LINESCROLL16 %d\n", pos);
5034 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5039 EDIT_EM_LineScroll(es, 0, dy);
5043 /*********************************************************************
5048 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5050 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5051 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5055 /*********************************************************************
5060 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5062 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5063 InvalidateRect(es->hwndSelf, rc, bErase);