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
42 * -!ES_AUTOVSCROLL (every multi line control *is* auto vscroll)
43 * -!ES_AUTOHSCROLL (every single line control *is* auto hscroll)
45 * When there is no autoscrolling, the control should first check whether
46 * the new text would fit. If not, an EN_MAXTEXT should be sent.
47 * However, currently this would require the actual change to be made,
48 * then call EDIT_BuildLineDefs() and then find out that the new text doesn't
49 * fit. After all this, things should be put back in the state before the
50 * changes. Note that for multi line controls !ES_AUTOHSCROLL works : wordwrap.
65 #include "wine/winbase16.h"
66 #include "wine/winuser16.h"
67 #include "wine/unicode.h"
71 #include "user_private.h"
72 #include "wine/debug.h"
74 WINE_DEFAULT_DEBUG_CHANNEL(edit);
75 WINE_DECLARE_DEBUG_CHANNEL(combo);
76 WINE_DECLARE_DEBUG_CHANNEL(relay);
78 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
79 FIXME: BTW, new specs say 65535 (do you dare ???) */
80 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
81 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
82 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
83 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
86 * extra flags for EDITSTATE.flags field
88 #define EF_MODIFIED 0x0001 /* text has been modified */
89 #define EF_FOCUSED 0x0002 /* we have input focus */
90 #define EF_UPDATE 0x0004 /* notify parent of changed state */
91 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
92 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
93 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
94 wrapped line, instead of in front of the next character */
95 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
99 END_0 = 0, /* line ends with terminating '\0' character */
100 END_WRAP, /* line is wrapped */
101 END_HARD, /* line ends with a hard return '\r\n' */
102 END_SOFT, /* line ends with a soft return '\r\r\n' */
103 END_RICH /* line ends with a single '\n' */
106 typedef struct tagLINEDEF {
107 INT length; /* bruto length of a line in bytes */
108 INT net_length; /* netto length of a line in visible characters */
110 INT width; /* width of the line in pixels */
111 INT index; /* line index into the buffer */
112 struct tagLINEDEF *next;
117 BOOL is_unicode; /* how the control was created */
118 LPWSTR text; /* the actual contents of the control */
119 UINT buffer_size; /* the size of the buffer in characters */
120 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
121 HFONT font; /* NULL means standard system font */
122 INT x_offset; /* scroll offset for multi lines this is in pixels
123 for single lines it's in characters */
124 INT line_height; /* height of a screen line in pixels */
125 INT char_width; /* average character width in pixels */
126 DWORD style; /* sane version of wnd->dwStyle */
127 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
128 INT undo_insert_count; /* number of characters inserted in sequence */
129 UINT undo_position; /* character index of the insertion and deletion */
130 LPWSTR undo_text; /* deleted text */
131 UINT undo_buffer_size; /* size of the deleted text buffer */
132 INT selection_start; /* == selection_end if no selection */
133 INT selection_end; /* == current caret position */
134 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
135 INT left_margin; /* in pixels */
136 INT right_margin; /* in pixels */
138 INT text_width; /* width of the widest line in pixels for multi line controls
139 and just line width for single line controls */
140 INT region_posx; /* Position of cursor relative to region: */
141 INT region_posy; /* -1: to left, 0: within, 1: to right */
142 EDITWORDBREAKPROC16 word_break_proc16;
143 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
144 INT line_count; /* number of lines */
145 INT y_offset; /* scroll offset in number of lines */
146 BOOL bCaptureState; /* flag indicating whether mouse was captured */
147 BOOL bEnableState; /* flag keeping the enable state */
148 HWND hwndSelf; /* the our window handle */
149 HWND hwndParent; /* Handle of parent for sending EN_* messages.
150 Even if parent will change, EN_* messages
151 should be sent to the first parent. */
152 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
154 * only for multi line controls
156 INT lock_count; /* amount of re-entries in the EditWndProc */
159 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
160 HLOCAL hloc32W; /* our unicode local memory block */
161 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
163 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
168 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
169 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
171 /* used for disabled or read-only edit control */
172 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
174 { /* Notify parent which has created this edit control */ \
175 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
176 SendMessageW(es->hwndParent, WM_COMMAND, \
177 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
178 (LPARAM)(es->hwndSelf)); \
181 /*********************************************************************
188 * These functions have trivial implementations
189 * We still like to call them internally
190 * "static inline" makes them more like macro's
192 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
193 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
194 static inline void EDIT_WM_Clear(EDITSTATE *es);
195 static inline void EDIT_WM_Cut(EDITSTATE *es);
198 * Helper functions only valid for one type of control
200 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
201 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
202 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
203 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
204 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
205 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
206 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
208 * Helper functions valid for both single line _and_ multi line controls
210 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
211 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
212 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
213 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
214 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
215 static void EDIT_LockBuffer(EDITSTATE *es);
216 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
217 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
218 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
219 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
220 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
221 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
222 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
223 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
224 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
225 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
226 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
227 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
228 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
229 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
230 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
232 * EM_XXX message handlers
234 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
235 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
236 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
237 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
238 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
239 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
240 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
241 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
242 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
243 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
244 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
245 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
246 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
247 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
248 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
249 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
250 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
251 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
252 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
253 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
254 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
255 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
256 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
257 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
258 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
259 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
260 static BOOL EDIT_EM_Undo(EDITSTATE *es);
262 * WM_XXX message handlers
264 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
265 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
266 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
267 static void EDIT_WM_Copy(EDITSTATE *es);
268 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
269 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
270 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
271 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
272 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
273 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
274 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
275 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
276 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
277 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
278 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
279 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
280 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
281 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
282 static void EDIT_WM_Paste(EDITSTATE *es);
283 static void EDIT_WM_SetFocus(EDITSTATE *es);
284 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
285 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
286 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
287 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
288 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
289 static void EDIT_WM_Timer(EDITSTATE *es);
290 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
291 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
292 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
294 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
295 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
297 /*********************************************************************
298 * edit class descriptor
300 const struct builtin_class_descr EDIT_builtin_class =
303 CS_DBLCLKS | CS_PARENTDC, /* style */
304 EditWndProcA, /* procA */
305 EditWndProcW, /* procW */
306 sizeof(EDITSTATE *), /* extra */
307 IDC_IBEAM, /* cursor */
312 /*********************************************************************
317 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
319 return (es->undo_insert_count || strlenW(es->undo_text));
323 /*********************************************************************
328 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
330 es->undo_insert_count = 0;
331 *es->undo_text = '\0';
335 /*********************************************************************
340 static inline void EDIT_WM_Clear(EDITSTATE *es)
342 static const WCHAR empty_stringW[] = {0};
344 /* Protect read-only edit control from modification */
345 if(es->style & ES_READONLY)
348 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
352 /*********************************************************************
357 static inline void EDIT_WM_Cut(EDITSTATE *es)
364 /**********************************************************************
367 * Returns the window version in case Wine emulates a later version
368 * of windows than the application expects.
370 * In a number of cases when windows runs an application that was
371 * designed for an earlier windows version, windows reverts
372 * to "old" behaviour of that earlier version.
374 * An example is a disabled edit control that needs to be painted.
375 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
376 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
377 * applications with an expected version 0f 4.0 or higher.
380 static DWORD get_app_version(void)
382 static DWORD version;
385 DWORD dwEmulatedVersion;
387 DWORD dwProcVersion = GetProcessVersion(0);
389 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
390 GetVersionExW( &info );
391 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
392 /* FIXME: this may not be 100% correct; see discussion on the
393 * wine developer list in Nov 1999 */
394 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
400 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
404 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
405 msg = WM_CTLCOLORSTATIC;
407 msg = WM_CTLCOLOREDIT;
409 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
410 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
413 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
416 return DefWindowProcW(hwnd, msg, wParam, lParam);
418 return DefWindowProcA(hwnd, msg, wParam, lParam);
421 /*********************************************************************
425 * The messages are in the order of the actual integer values
426 * (which can be found in include/windows.h)
427 * Wherever possible the 16 bit versions are converted to
428 * the 32 bit ones, so that we can 'fall through' to the
429 * helper functions. These are mostly 32 bit (with a few
430 * exceptions, clearly indicated by a '16' extension to their
434 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
435 WPARAM wParam, LPARAM lParam, BOOL unicode )
437 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
440 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
442 if (!es && msg != WM_NCCREATE)
443 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
444 else if (msg == WM_NCCREATE)
445 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
446 else if (msg == WM_DESTROY)
447 return EDIT_WM_Destroy(es);
450 if (es) EDIT_LockBuffer(es);
458 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
462 if ((short)LOWORD(lParam) == -1)
463 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
465 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
467 EDIT_EM_ScrollCaret(es);
471 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
472 EDIT_EM_ScrollCaret(es);
479 RECT16 *r16 = MapSL(lParam);
480 r16->left = es->format_rect.left;
481 r16->top = es->format_rect.top;
482 r16->right = es->format_rect.right;
483 r16->bottom = es->format_rect.bottom;
488 CopyRect((LPRECT)lParam, &es->format_rect);
492 if ((es->style & ES_MULTILINE) && lParam) {
494 RECT16 *r16 = MapSL(lParam);
497 rc.right = r16->right;
498 rc.bottom = r16->bottom;
499 EDIT_SetRectNP(es, &rc);
500 EDIT_UpdateText(es, NULL, TRUE);
504 if ((es->style & ES_MULTILINE) && lParam) {
505 EDIT_SetRectNP(es, (LPRECT)lParam);
506 EDIT_UpdateText(es, NULL, TRUE);
511 if ((es->style & ES_MULTILINE) && lParam) {
513 RECT16 *r16 = MapSL(lParam);
516 rc.right = r16->right;
517 rc.bottom = r16->bottom;
518 EDIT_SetRectNP(es, &rc);
522 if ((es->style & ES_MULTILINE) && lParam)
523 EDIT_SetRectNP(es, (LPRECT)lParam);
528 result = EDIT_EM_Scroll(es, (INT)wParam);
531 case EM_LINESCROLL16:
532 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
533 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
536 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
539 case EM_SCROLLCARET16:
541 EDIT_EM_ScrollCaret(es);
547 result = ((es->flags & EF_MODIFIED) != 0);
553 es->flags |= EF_MODIFIED;
555 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
558 case EM_GETLINECOUNT16:
559 case EM_GETLINECOUNT:
560 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
564 if ((INT16)wParam == -1)
568 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
572 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
575 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
579 result = (LRESULT)EDIT_EM_GetHandle16(es);
582 result = (LRESULT)EDIT_EM_GetHandle(es);
587 result = EDIT_EM_GetThumb(es);
590 /* these messages missing from specs */
599 FIXME("undocumented message 0x%x, please report\n", msg);
600 result = DefWindowProcW(hwnd, msg, wParam, lParam);
603 case EM_LINELENGTH16:
605 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
608 case EM_REPLACESEL16:
609 lParam = (LPARAM)MapSL(lParam);
610 unicode = FALSE; /* 16-bit message is always ascii */
617 textW = (LPWSTR)lParam;
620 LPSTR textA = (LPSTR)lParam;
621 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
622 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
623 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
626 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
630 HeapFree(GetProcessHeap(), 0, textW);
635 lParam = (LPARAM)MapSL(lParam);
636 unicode = FALSE; /* 16-bit message is always ascii */
639 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
643 case EM_SETLIMITTEXT:
644 EDIT_EM_SetLimitText(es, (INT)wParam);
649 result = (LRESULT)EDIT_EM_CanUndo(es);
655 result = (LRESULT)EDIT_EM_Undo(es);
660 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
663 case EM_LINEFROMCHAR16:
664 case EM_LINEFROMCHAR:
665 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
668 case EM_SETTABSTOPS16:
669 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
672 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
675 case EM_SETPASSWORDCHAR16:
676 unicode = FALSE; /* 16-bit message is always ascii */
678 case EM_SETPASSWORDCHAR:
683 charW = (WCHAR)wParam;
687 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
690 EDIT_EM_SetPasswordChar(es, charW);
694 case EM_EMPTYUNDOBUFFER16:
695 case EM_EMPTYUNDOBUFFER:
696 EDIT_EM_EmptyUndoBuffer(es);
699 case EM_GETFIRSTVISIBLELINE16:
700 result = es->y_offset;
702 case EM_GETFIRSTVISIBLELINE:
703 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
706 case EM_SETREADONLY16:
709 SetWindowLongW( hwnd, GWL_STYLE,
710 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
711 es->style |= ES_READONLY;
713 SetWindowLongW( hwnd, GWL_STYLE,
714 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
715 es->style &= ~ES_READONLY;
720 case EM_SETWORDBREAKPROC16:
721 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
723 case EM_SETWORDBREAKPROC:
724 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
727 case EM_GETWORDBREAKPROC16:
728 result = (LRESULT)es->word_break_proc16;
730 case EM_GETWORDBREAKPROC:
731 result = (LRESULT)es->word_break_proc;
734 case EM_GETPASSWORDCHAR16:
735 unicode = FALSE; /* 16-bit message is always ascii */
737 case EM_GETPASSWORDCHAR:
740 result = es->password_char;
743 WCHAR charW = es->password_char;
745 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
751 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
754 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
758 result = MAKELONG(es->left_margin, es->right_margin);
761 case EM_GETLIMITTEXT:
762 result = es->buffer_limit;
766 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
770 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
773 /* End of the EM_ messages which were in numerical order; what order
774 * are these in? vaguely alphabetical?
778 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
780 if (es->style & ES_MULTILINE)
782 result |= DLGC_WANTALLKEYS;
786 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
788 int vk = (int)((LPMSG)lParam)->wParam;
790 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
792 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
793 result |= DLGC_WANTMESSAGE;
804 strng[0] = wParam >> 8;
805 strng[1] = wParam & 0xff;
806 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
807 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
808 EDIT_WM_Char(es, charW);
821 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
824 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
826 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
827 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
830 EDIT_WM_Char(es, charW);
839 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
843 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
852 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
855 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
859 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
860 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
861 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
863 result = EDIT_WM_Create(es, nameW);
864 HeapFree(GetProcessHeap(), 0, nameW);
873 es->bEnableState = (BOOL) wParam;
874 EDIT_UpdateText(es, NULL, TRUE);
878 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
882 result = (LRESULT)es->font;
886 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
889 case WM_GETTEXTLENGTH:
890 if (unicode) result = strlenW(es->text);
891 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
892 NULL, 0, NULL, NULL );
896 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
900 result = EDIT_WM_KeyDown(es, (INT)wParam);
904 result = EDIT_WM_KillFocus(es);
907 case WM_LBUTTONDBLCLK:
908 result = EDIT_WM_LButtonDblClk(es);
912 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
916 result = EDIT_WM_LButtonUp(es);
920 result = EDIT_WM_MButtonDown(es);
923 case WM_MOUSEACTIVATE:
924 result = MA_ACTIVATE;
928 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
932 EDIT_WM_Paint(es, (HDC)wParam);
940 EDIT_WM_SetFocus(es);
944 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
948 /* FIXME: actually set an internal flag and behave accordingly */
952 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
957 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
960 case WM_STYLECHANGED:
961 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
964 case WM_STYLECHANGING:
965 result = 0; /* See EDIT_WM_StyleChanged */
969 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
977 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
982 int gcWheelDelta = 0;
983 UINT pulScrollLines = 3;
984 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
986 if (wParam & (MK_SHIFT | MK_CONTROL)) {
987 result = DefWindowProcW(hwnd, msg, wParam, lParam);
990 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
991 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
993 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
994 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
995 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1000 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1004 if (es) EDIT_UnlockBuffer(es, FALSE);
1006 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1011 /*********************************************************************
1013 * EditWndProcW (USER32.@)
1015 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1017 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1020 /*********************************************************************
1022 * EditWndProc (USER32.@)
1024 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1026 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1029 /*********************************************************************
1031 * EDIT_BuildLineDefs_ML
1033 * Build linked list of text lines.
1034 * Lines can end with '\0' (last line), a character (if it is wrapped),
1035 * a soft return '\r\r\n' or a hard return '\r\n'
1038 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1042 LPWSTR current_position, cp;
1044 LINEDEF *current_line;
1045 LINEDEF *previous_line;
1046 LINEDEF *start_line;
1047 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1048 INT line_count = es->line_count;
1049 INT orig_net_length;
1052 if (istart == iend && delta == 0)
1055 dc = GetDC(es->hwndSelf);
1057 old_font = SelectObject(dc, es->font);
1059 previous_line = NULL;
1060 current_line = es->first_line_def;
1062 /* Find starting line. istart must lie inside an existing line or
1063 * at the end of buffer */
1065 if (istart < current_line->index + current_line->length ||
1066 current_line->ending == END_0)
1069 previous_line = current_line;
1070 current_line = current_line->next;
1072 } while (current_line);
1074 if (!current_line) /* Error occurred start is not inside previous buffer */
1076 FIXME(" modification occurred outside buffer\n");
1077 ReleaseDC(es->hwndSelf, dc);
1081 /* Remember start of modifications in order to calculate update region */
1082 nstart_line = line_index;
1083 nstart_index = current_line->index;
1085 /* We must start to reformat from the previous line since the modifications
1086 * may have caused the line to wrap upwards. */
1087 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1090 current_line = previous_line;
1092 start_line = current_line;
1094 fw = es->format_rect.right - es->format_rect.left;
1095 current_position = es->text + current_line->index;
1097 if (current_line != start_line)
1099 if (!current_line || current_line->index + delta > current_position - es->text)
1101 /* The buffer has been expanded, create a new line and
1102 insert it into the link list */
1103 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1104 new_line->next = previous_line->next;
1105 previous_line->next = new_line;
1106 current_line = new_line;
1109 else if (current_line->index + delta < current_position - es->text)
1111 /* The previous line merged with this line so we delete this extra entry */
1112 previous_line->next = current_line->next;
1113 HeapFree(GetProcessHeap(), 0, current_line);
1114 current_line = previous_line->next;
1118 else /* current_line->index + delta == current_position */
1120 if (current_position - es->text > iend)
1121 break; /* We reached end of line modifications */
1122 /* else recalulate this line */
1126 current_line->index = current_position - es->text;
1127 orig_net_length = current_line->net_length;
1129 /* Find end of line */
1130 cp = current_position;
1132 if (*cp == '\n') break;
1133 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1138 /* Mark type of line termination */
1140 current_line->ending = END_0;
1141 current_line->net_length = strlenW(current_position);
1142 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1143 current_line->ending = END_SOFT;
1144 current_line->net_length = cp - current_position - 1;
1145 } else if (*cp == '\n') {
1146 current_line->ending = END_RICH;
1147 current_line->net_length = cp - current_position;
1149 current_line->ending = END_HARD;
1150 current_line->net_length = cp - current_position;
1153 /* Calculate line width */
1154 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1155 current_position, current_line->net_length,
1156 es->tabs_count, es->tabs));
1158 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1159 if (!(es->style & ES_AUTOHSCROLL)) {
1160 if (current_line->width > fw) {
1165 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1166 prev + 1, current_line->net_length, WB_RIGHT);
1167 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1168 current_position, next, es->tabs_count, es->tabs));
1169 } while (current_line->width <= fw);
1170 if (!prev) { /* Didn't find a line break so force a break */
1175 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1176 current_position, next, es->tabs_count, es->tabs));
1177 } while (current_line->width <= fw);
1182 /* If the first line we are calculating, wrapped before istart, we must
1183 * adjust istart in order for this to be reflected in the update region. */
1184 if (current_line->index == nstart_index && istart > current_line->index + prev)
1185 istart = current_line->index + prev;
1186 /* else if we are updating the previous line before the first line we
1187 * are re-calculating and it expanded */
1188 else if (current_line == start_line &&
1189 current_line->index != nstart_index && orig_net_length < prev)
1191 /* Line expanded due to an upwards line wrap so we must partially include
1192 * previous line in update region */
1193 nstart_line = line_index;
1194 nstart_index = current_line->index;
1195 istart = current_line->index + orig_net_length;
1198 current_line->net_length = prev;
1199 current_line->ending = END_WRAP;
1200 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1201 current_line->net_length, es->tabs_count, es->tabs));
1203 else if (orig_net_length < current_line->net_length &&
1204 current_line == start_line &&
1205 current_line->index != nstart_index) {
1206 /* The previous line expanded but it's still not as wide as the client rect */
1207 /* The expansion is due to an upwards line wrap so we must partially include
1208 it in the update region */
1209 nstart_line = line_index;
1210 nstart_index = current_line->index;
1211 istart = current_line->index + orig_net_length;
1216 /* Adjust length to include line termination */
1217 switch (current_line->ending) {
1219 current_line->length = current_line->net_length + 3;
1222 current_line->length = current_line->net_length + 1;
1225 current_line->length = current_line->net_length + 2;
1229 current_line->length = current_line->net_length;
1232 es->text_width = max(es->text_width, current_line->width);
1233 current_position += current_line->length;
1234 previous_line = current_line;
1235 current_line = current_line->next;
1237 } while (previous_line->ending != END_0);
1239 /* Finish adjusting line indexes by delta or remove hanging lines */
1240 if (previous_line->ending == END_0)
1242 LINEDEF *pnext = NULL;
1244 previous_line->next = NULL;
1245 while (current_line)
1247 pnext = current_line->next;
1248 HeapFree(GetProcessHeap(), 0, current_line);
1249 current_line = pnext;
1253 else if (delta != 0)
1255 while (current_line)
1257 current_line->index += delta;
1258 current_line = current_line->next;
1262 /* Calculate rest of modification rectangle */
1267 * We calculate two rectangles. One for the first line which may have
1268 * an indent with respect to the format rect. The other is a format-width
1269 * rectangle that spans the rest of the lines that changed or moved.
1271 rc.top = es->format_rect.top + nstart_line * es->line_height -
1272 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1273 rc.bottom = rc.top + es->line_height;
1274 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1275 rc.left = es->format_rect.left;
1277 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1278 es->text + nstart_index, istart - nstart_index,
1279 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1280 rc.right = es->format_rect.right;
1281 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1284 rc.left = es->format_rect.left;
1285 rc.right = es->format_rect.right;
1287 * If lines were added or removed we must re-paint the remainder of the
1288 * lines since the remaining lines were either shifted up or down.
1290 if (line_count < es->line_count) /* We added lines */
1291 rc.bottom = es->line_count * es->line_height;
1292 else if (line_count > es->line_count) /* We removed lines */
1293 rc.bottom = line_count * es->line_height;
1295 rc.bottom = line_index * es->line_height;
1296 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1297 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1298 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1299 DeleteObject(tmphrgn);
1303 SelectObject(dc, old_font);
1305 ReleaseDC(es->hwndSelf, dc);
1308 /*********************************************************************
1310 * EDIT_CalcLineWidth_SL
1313 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1320 text = EDIT_GetPasswordPointer_SL(es);
1322 dc = GetDC(es->hwndSelf);
1324 old_font = SelectObject(dc, es->font);
1326 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1329 SelectObject(dc, old_font);
1330 ReleaseDC(es->hwndSelf, dc);
1332 if (es->style & ES_PASSWORD)
1333 HeapFree(GetProcessHeap(), 0, text);
1335 es->text_width = size.cx;
1338 /*********************************************************************
1340 * EDIT_CallWordBreakProc
1342 * Call appropriate WordBreakProc (internal or external).
1344 * Note: The "start" argument should always be an index referring
1345 * to es->text. The actual wordbreak proc might be
1346 * 16 bit, so we can't always pass any 32 bit LPSTR.
1347 * Hence we assume that es->text is the buffer that holds
1348 * the string under examination (we can decide this for ourselves).
1351 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1355 if (es->word_break_proc16) {
1362 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1363 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1364 segptr = K32WOWGlobalLock16(hglob16);
1365 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1366 args[4] = SELECTOROF(segptr);
1367 args[3] = OFFSETOF(segptr);
1371 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1372 ret = LOWORD(result);
1373 GlobalUnlock16(hglob16);
1374 GlobalFree16(hglob16);
1376 else if (es->word_break_proc)
1380 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1382 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1383 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1384 ret = wbpW(es->text + start, index, count, action);
1388 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1392 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1393 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1394 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1395 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1396 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1397 ret = wbpA(textA, index, countA, action);
1398 HeapFree(GetProcessHeap(), 0, textA);
1402 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1408 /*********************************************************************
1412 * Beware: This is not the function called on EM_CHARFROMPOS
1413 * The position _can_ be outside the formatting / client
1415 * The return value is only the character index
1418 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1424 if (es->style & ES_MULTILINE) {
1425 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1427 LINEDEF *line_def = es->first_line_def;
1429 while ((line > 0) && line_def->next) {
1430 line_index += line_def->length;
1431 line_def = line_def->next;
1434 x += es->x_offset - es->format_rect.left;
1435 if (es->style & ES_RIGHT)
1436 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1437 else if (es->style & ES_CENTER)
1438 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1439 if (x >= line_def->width) {
1441 *after_wrap = (line_def->ending == END_WRAP);
1442 return line_index + line_def->net_length;
1446 *after_wrap = FALSE;
1449 dc = GetDC(es->hwndSelf);
1451 old_font = SelectObject(dc, es->font);
1452 low = line_index + 1;
1453 high = line_index + line_def->net_length + 1;
1454 while (low < high - 1)
1456 INT mid = (low + high) / 2;
1457 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1463 *after_wrap = ((index == line_index + line_def->net_length) &&
1464 (line_def->ending == END_WRAP));
1469 *after_wrap = FALSE;
1470 x -= es->format_rect.left;
1472 return es->x_offset;
1476 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1477 if (es->style & ES_RIGHT)
1479 else if (es->style & ES_CENTER)
1483 text = EDIT_GetPasswordPointer_SL(es);
1484 dc = GetDC(es->hwndSelf);
1486 old_font = SelectObject(dc, es->font);
1490 INT high = es->x_offset;
1491 while (low < high - 1)
1493 INT mid = (low + high) / 2;
1494 GetTextExtentPoint32W( dc, text + mid,
1495 es->x_offset - mid, &size );
1496 if (size.cx > -x) low = mid;
1503 INT low = es->x_offset;
1504 INT high = strlenW(es->text) + 1;
1505 while (low < high - 1)
1507 INT mid = (low + high) / 2;
1508 GetTextExtentPoint32W( dc, text + es->x_offset,
1509 mid - es->x_offset, &size );
1510 if (size.cx > x) high = mid;
1515 if (es->style & ES_PASSWORD)
1516 HeapFree(GetProcessHeap(), 0, text);
1519 SelectObject(dc, old_font);
1520 ReleaseDC(es->hwndSelf, dc);
1525 /*********************************************************************
1529 * adjusts the point to be within the formatting rectangle
1530 * (so CharFromPos returns the nearest _visible_ character)
1533 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1535 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1536 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1540 /*********************************************************************
1544 * Calculates the bounding rectangle for a line from a starting
1545 * column to an ending column.
1548 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1550 INT line_index = EDIT_EM_LineIndex(es, line);
1552 if (es->style & ES_MULTILINE)
1553 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1555 rc->top = es->format_rect.top;
1556 rc->bottom = rc->top + es->line_height;
1557 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1558 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1562 /*********************************************************************
1564 * EDIT_GetPasswordPointer_SL
1566 * note: caller should free the (optionally) allocated buffer
1569 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1571 if (es->style & ES_PASSWORD) {
1572 INT len = strlenW(es->text);
1573 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1575 while(len) text[--len] = es->password_char;
1582 /*********************************************************************
1586 * This acts as a LOCAL_Lock(), but it locks only once. This way
1587 * you can call it whenever you like, without unlocking.
1589 * Initially the edit control allocates a HLOCAL32 buffer
1590 * (32 bit linear memory handler). However, 16 bit application
1591 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1592 * handler). From that moment on we have to keep using this 16 bit memory
1593 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1594 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1598 static void EDIT_LockBuffer(EDITSTATE *es)
1600 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1604 BOOL _16bit = FALSE;
1610 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1611 textA = LocalLock(es->hloc32A);
1612 countA = strlen(textA) + 1;
1616 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1617 textA = LOCAL_Lock(hInstance, es->hloc16);
1618 countA = strlen(textA) + 1;
1623 ERR("no buffer ... please report\n");
1630 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1631 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1632 if(countW_new > es->buffer_size + 1)
1634 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1635 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1636 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1639 es->hloc32W = hloc32W_new;
1640 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1641 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1644 WARN("FAILED! Will synchronize partially\n");
1648 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1649 es->text = LocalLock(es->hloc32W);
1653 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1655 LOCAL_Unlock(hInstance, es->hloc16);
1657 LocalUnlock(es->hloc32A);
1664 /*********************************************************************
1666 * EDIT_SL_InvalidateText
1668 * Called from EDIT_InvalidateText().
1669 * Does the job for single-line controls only.
1672 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1677 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1678 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1679 EDIT_UpdateText(es, &rc, TRUE);
1683 /*********************************************************************
1685 * EDIT_ML_InvalidateText
1687 * Called from EDIT_InvalidateText().
1688 * Does the job for multi-line controls only.
1691 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1693 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1694 INT sl = EDIT_EM_LineFromChar(es, start);
1695 INT el = EDIT_EM_LineFromChar(es, end);
1704 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1707 sc = start - EDIT_EM_LineIndex(es, sl);
1708 ec = end - EDIT_EM_LineIndex(es, el);
1709 if (sl < es->y_offset) {
1713 if (el > es->y_offset + vlc) {
1714 el = es->y_offset + vlc;
1715 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1717 GetClientRect(es->hwndSelf, &rc1);
1718 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1720 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1721 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1722 EDIT_UpdateText(es, &rcUpdate, TRUE);
1724 EDIT_GetLineRect(es, sl, sc,
1725 EDIT_EM_LineLength(es,
1726 EDIT_EM_LineIndex(es, sl)),
1728 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1729 EDIT_UpdateText(es, &rcUpdate, TRUE);
1730 for (l = sl + 1 ; l < el ; l++) {
1731 EDIT_GetLineRect(es, l, 0,
1732 EDIT_EM_LineLength(es,
1733 EDIT_EM_LineIndex(es, l)),
1735 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1736 EDIT_UpdateText(es, &rcUpdate, TRUE);
1738 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1739 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1740 EDIT_UpdateText(es, &rcUpdate, TRUE);
1745 /*********************************************************************
1747 * EDIT_InvalidateText
1749 * Invalidate the text from offset start upto, but not including,
1750 * offset end. Useful for (re)painting the selection.
1751 * Regions outside the linewidth are not invalidated.
1752 * end == -1 means end == TextLength.
1753 * start and end need not be ordered.
1756 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1762 end = strlenW(es->text);
1770 if (es->style & ES_MULTILINE)
1771 EDIT_ML_InvalidateText(es, start, end);
1773 EDIT_SL_InvalidateText(es, start, end);
1777 /*********************************************************************
1781 * Try to fit size + 1 characters in the buffer.
1783 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1787 if (size <= es->buffer_size)
1790 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1792 /* Force edit to unlock it's buffer. es->text now NULL */
1793 EDIT_UnlockBuffer(es, TRUE);
1796 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1797 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1798 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1799 es->hloc32W = hNew32W;
1800 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1804 EDIT_LockBuffer(es);
1806 if (es->buffer_size < size) {
1807 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1808 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1811 TRACE("We now have %d+1\n", es->buffer_size);
1817 /*********************************************************************
1821 * Try to fit size + 1 bytes in the undo buffer.
1824 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1828 if (size <= es->undo_buffer_size)
1831 TRACE("trying to ReAlloc to %d+1\n", size);
1833 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1834 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1835 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1840 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1846 /*********************************************************************
1851 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1853 INT e = es->selection_end;
1857 if ((es->style & ES_MULTILINE) && e &&
1858 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1860 if (e && (es->text[e - 1] == '\r'))
1864 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1865 EDIT_EM_ScrollCaret(es);
1869 /*********************************************************************
1873 * Only for multi line controls
1874 * Move the caret one line down, on a column with the nearest
1875 * x coordinate on the screen (might be a different column).
1878 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1880 INT s = es->selection_start;
1881 INT e = es->selection_end;
1882 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1883 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1884 INT x = (short)LOWORD(pos);
1885 INT y = (short)HIWORD(pos);
1887 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1890 EDIT_EM_SetSel(es, s, e, after_wrap);
1891 EDIT_EM_ScrollCaret(es);
1895 /*********************************************************************
1900 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1902 BOOL after_wrap = FALSE;
1905 /* Pass a high value in x to make sure of receiving the end of the line */
1906 if (es->style & ES_MULTILINE)
1907 e = EDIT_CharFromPos(es, 0x3fffffff,
1908 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1910 e = strlenW(es->text);
1911 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1912 EDIT_EM_ScrollCaret(es);
1916 /*********************************************************************
1921 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1923 INT e = es->selection_end;
1927 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1928 if (es->text[e] == '\n')
1930 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1934 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1935 EDIT_EM_ScrollCaret(es);
1939 /*********************************************************************
1943 * Home key: move to beginning of line.
1946 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1950 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1951 if (es->style & ES_MULTILINE)
1952 e = EDIT_CharFromPos(es, -es->x_offset,
1953 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1956 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1957 EDIT_EM_ScrollCaret(es);
1961 /*********************************************************************
1963 * EDIT_MovePageDown_ML
1965 * Only for multi line controls
1966 * Move the caret one page down, on a column with the nearest
1967 * x coordinate on the screen (might be a different column).
1970 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1972 INT s = es->selection_start;
1973 INT e = es->selection_end;
1974 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1975 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1976 INT x = (short)LOWORD(pos);
1977 INT y = (short)HIWORD(pos);
1979 e = EDIT_CharFromPos(es, x,
1980 y + (es->format_rect.bottom - es->format_rect.top),
1984 EDIT_EM_SetSel(es, s, e, after_wrap);
1985 EDIT_EM_ScrollCaret(es);
1989 /*********************************************************************
1991 * EDIT_MovePageUp_ML
1993 * Only for multi line controls
1994 * Move the caret one page up, on a column with the nearest
1995 * x coordinate on the screen (might be a different column).
1998 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2000 INT s = es->selection_start;
2001 INT e = es->selection_end;
2002 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2003 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2004 INT x = (short)LOWORD(pos);
2005 INT y = (short)HIWORD(pos);
2007 e = EDIT_CharFromPos(es, x,
2008 y - (es->format_rect.bottom - es->format_rect.top),
2012 EDIT_EM_SetSel(es, s, e, after_wrap);
2013 EDIT_EM_ScrollCaret(es);
2017 /*********************************************************************
2021 * Only for multi line controls
2022 * Move the caret one line up, on a column with the nearest
2023 * x coordinate on the screen (might be a different column).
2026 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2028 INT s = es->selection_start;
2029 INT e = es->selection_end;
2030 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2031 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2032 INT x = (short)LOWORD(pos);
2033 INT y = (short)HIWORD(pos);
2035 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2038 EDIT_EM_SetSel(es, s, e, after_wrap);
2039 EDIT_EM_ScrollCaret(es);
2043 /*********************************************************************
2045 * EDIT_MoveWordBackward
2048 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2050 INT s = es->selection_start;
2051 INT e = es->selection_end;
2056 l = EDIT_EM_LineFromChar(es, e);
2057 ll = EDIT_EM_LineLength(es, e);
2058 li = EDIT_EM_LineIndex(es, l);
2061 li = EDIT_EM_LineIndex(es, l - 1);
2062 e = li + EDIT_EM_LineLength(es, li);
2065 e = li + (INT)EDIT_CallWordBreakProc(es,
2066 li, e - li, ll, WB_LEFT);
2070 EDIT_EM_SetSel(es, s, e, FALSE);
2071 EDIT_EM_ScrollCaret(es);
2075 /*********************************************************************
2077 * EDIT_MoveWordForward
2080 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2082 INT s = es->selection_start;
2083 INT e = es->selection_end;
2088 l = EDIT_EM_LineFromChar(es, e);
2089 ll = EDIT_EM_LineLength(es, e);
2090 li = EDIT_EM_LineIndex(es, l);
2092 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2093 e = EDIT_EM_LineIndex(es, l + 1);
2095 e = li + EDIT_CallWordBreakProc(es,
2096 li, e - li + 1, ll, WB_RIGHT);
2100 EDIT_EM_SetSel(es, s, e, FALSE);
2101 EDIT_EM_ScrollCaret(es);
2105 /*********************************************************************
2110 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2112 INT s = es->selection_start;
2113 INT e = es->selection_end;
2120 if (es->style & ES_MULTILINE) {
2121 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2122 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2127 TRACE("line=%d\n", line);
2129 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2130 x = (short)LOWORD(pos);
2131 y = (short)HIWORD(pos);
2132 li = EDIT_EM_LineIndex(es, line);
2133 ll = EDIT_EM_LineLength(es, li);
2134 s = min(es->selection_start, es->selection_end);
2135 e = max(es->selection_start, es->selection_end);
2136 s = min(li + ll, max(li, s));
2137 e = min(li + ll, max(li, e));
2138 if (rev && (s != e) &&
2139 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2140 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2141 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2142 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2144 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2148 /*********************************************************************
2153 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2164 BkMode = GetBkMode(dc);
2165 BkColor = GetBkColor(dc);
2166 TextColor = GetTextColor(dc);
2168 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2169 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2170 SetBkMode( dc, OPAQUE);
2172 li = EDIT_EM_LineIndex(es, line);
2173 if (es->style & ES_MULTILINE) {
2174 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2175 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2177 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2178 TextOutW(dc, x, y, text + li + col, count);
2179 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2181 if (es->style & ES_PASSWORD)
2182 HeapFree(GetProcessHeap(), 0, text);
2185 SetBkColor(dc, BkColor);
2186 SetTextColor(dc, TextColor);
2187 SetBkMode( dc, BkMode);
2193 /*********************************************************************
2198 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2201 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2202 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2203 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2207 /*********************************************************************
2211 * note: this is not (exactly) the handler called on EM_SETRECTNP
2212 * it is also used to set the rect of a single line control
2215 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2220 CopyRect(&es->format_rect, rc);
2221 if (es->style & ES_MULTILINE)
2223 if (es->style & WS_BORDER) {
2224 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2225 es->format_rect.left += bw;
2226 es->format_rect.right -= bw;
2227 es->format_rect.top += bw;
2228 es->format_rect.bottom -= bw;
2233 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2234 if (ExStyle & WS_EX_CLIENTEDGE) {
2235 if (es->line_height + 2 <=
2236 es->format_rect.bottom - es->format_rect.top) {
2237 es->format_rect.top++;
2238 es->format_rect.bottom--;
2240 } else if (es->style & WS_BORDER) {
2241 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2242 es->format_rect.left += bw;
2243 es->format_rect.right -= bw;
2244 if (es->line_height + 2 * bw <=
2245 es->format_rect.bottom - es->format_rect.top) {
2246 es->format_rect.top += bw;
2247 es->format_rect.bottom -= bw;
2251 es->format_rect.left += es->left_margin;
2252 es->format_rect.right -= es->right_margin;
2253 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2254 if (es->style & ES_MULTILINE)
2256 INT fw, vlc, max_x_offset, max_y_offset;
2258 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2259 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2261 /* correct es->x_offset */
2262 fw = es->format_rect.right - es->format_rect.left;
2263 max_x_offset = es->text_width - fw;
2264 if(max_x_offset < 0) max_x_offset = 0;
2265 if(es->x_offset > max_x_offset)
2266 es->x_offset = max_x_offset;
2268 /* correct es->y_offset */
2269 max_y_offset = es->line_count - vlc;
2270 if(max_y_offset < 0) max_y_offset = 0;
2271 if(es->y_offset > max_y_offset)
2272 es->y_offset = max_y_offset;
2274 /* force scroll info update */
2275 EDIT_UpdateScrollInfo(es);
2278 /* Windows doesn't care to fix text placement for SL controls */
2279 es->format_rect.bottom = es->format_rect.top + es->line_height;
2281 /* Always stay within the client area */
2282 GetClientRect(es->hwndSelf, &ClientRect);
2283 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2285 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2286 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2288 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2292 /*********************************************************************
2297 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2299 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2301 /* Edit window might be already destroyed */
2302 if(!IsWindow(es->hwndSelf))
2304 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2308 if (!es->lock_count) {
2309 ERR("lock_count == 0 ... please report\n");
2313 ERR("es->text == 0 ... please report\n");
2317 if (force || (es->lock_count == 1)) {
2320 BOOL _16bit = FALSE;
2322 UINT countW = strlenW(es->text) + 1;
2326 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2327 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2328 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2329 countA = LocalSize(es->hloc32A);
2330 if(countA_new > countA)
2333 UINT alloc_size = ROUND_TO_GROW(countA_new);
2334 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2335 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2338 es->hloc32A = hloc32A_new;
2339 countA = LocalSize(hloc32A_new);
2340 TRACE("Real new size %d bytes\n", countA);
2343 WARN("FAILED! Will synchronize partially\n");
2345 textA = LocalLock(es->hloc32A);
2349 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2350 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2351 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2352 countA = LOCAL_Size(hInstance, es->hloc16);
2353 if(countA_new > countA)
2355 HLOCAL16 hloc16_new;
2356 UINT alloc_size = ROUND_TO_GROW(countA_new);
2357 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2358 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2361 es->hloc16 = hloc16_new;
2362 countA = LOCAL_Size(hInstance, hloc16_new);
2363 TRACE("Real new size %d bytes\n", countA);
2366 WARN("FAILED! Will synchronize partially\n");
2368 textA = LOCAL_Lock(hInstance, es->hloc16);
2374 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2376 LOCAL_Unlock(hInstance, es->hloc16);
2378 LocalUnlock(es->hloc32A);
2381 LocalUnlock(es->hloc32W);
2385 ERR("no buffer ... please report\n");
2393 /*********************************************************************
2395 * EDIT_UpdateScrollInfo
2398 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2400 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2403 si.cbSize = sizeof(SCROLLINFO);
2404 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2406 si.nMax = es->line_count - 1;
2407 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2408 si.nPos = es->y_offset;
2409 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2410 si.nMin, si.nMax, si.nPage, si.nPos);
2411 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2414 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2417 si.cbSize = sizeof(SCROLLINFO);
2418 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2420 si.nMax = es->text_width - 1;
2421 si.nPage = es->format_rect.right - es->format_rect.left;
2422 si.nPos = es->x_offset;
2423 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2424 si.nMin, si.nMax, si.nPage, si.nPos);
2425 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2429 /*********************************************************************
2431 * EDIT_WordBreakProc
2433 * Find the beginning of words.
2434 * Note: unlike the specs for a WordBreakProc, this function only
2435 * allows to be called without linebreaks between s[0] upto
2436 * s[count - 1]. Remember it is only called
2437 * internally, so we can decide this for ourselves.
2440 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2444 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2454 if (s[index] == ' ') {
2455 while (index && (s[index] == ' '))
2458 while (index && (s[index] != ' '))
2460 if (s[index] == ' ')
2464 while (index && (s[index] != ' '))
2466 if (s[index] == ' ')
2476 if (s[index] == ' ')
2477 while ((index < count) && (s[index] == ' ')) index++;
2479 while (s[index] && (s[index] != ' ') && (index < count))
2481 while ((s[index] == ' ') && (index < count)) index++;
2485 case WB_ISDELIMITER:
2486 ret = (s[index] == ' ');
2489 ERR("unknown action code, please report !\n");
2496 /*********************************************************************
2500 * returns line number (not index) in high-order word of result.
2501 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2502 * to Richedit, not to the edit control. Original documentation is valid.
2503 * FIXME: do the specs mean to return -1 if outside client area or
2504 * if outside formatting rectangle ???
2507 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2515 GetClientRect(es->hwndSelf, &rc);
2516 if (!PtInRect(&rc, pt))
2519 index = EDIT_CharFromPos(es, x, y, NULL);
2520 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2524 /*********************************************************************
2528 * Enable or disable soft breaks.
2530 * This means: insert or remove the soft linebreak character (\r\r\n).
2531 * Take care to check if the text still fits the buffer after insertion.
2532 * If not, notify with EN_ERRSPACE.
2535 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2537 es->flags &= ~EF_USE_SOFTBRK;
2539 es->flags |= EF_USE_SOFTBRK;
2540 FIXME("soft break enabled, not implemented\n");
2546 /*********************************************************************
2550 * Hopefully this won't fire back at us.
2551 * We always start with a fixed buffer in the local heap.
2552 * Despite of the documentation says that the local heap is used
2553 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2554 * buffer on the local heap.
2557 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2561 if (!(es->style & ES_MULTILINE))
2565 hLocal = es->hloc32W;
2571 UINT countA, alloc_size;
2572 TRACE("Allocating 32-bit ANSI alias buffer\n");
2573 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2574 alloc_size = ROUND_TO_GROW(countA);
2575 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2577 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2580 textA = LocalLock(es->hloc32A);
2581 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2582 LocalUnlock(es->hloc32A);
2584 hLocal = es->hloc32A;
2587 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2592 /*********************************************************************
2596 * Hopefully this won't fire back at us.
2597 * We always start with a buffer in 32 bit linear memory.
2598 * However, with this message a 16 bit application requests
2599 * a handle of 16 bit local heap memory, where it expects to find
2601 * It's a pitty that from this moment on we have to use this
2602 * local heap, because applications may rely on the handle
2605 * In this function we'll try to switch to local heap.
2607 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2609 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2611 UINT countA, alloc_size;
2613 if (!(es->style & ES_MULTILINE))
2619 if (!LOCAL_HeapSize(hInstance)) {
2620 if (!LocalInit16(hInstance, 0,
2621 GlobalSize16(hInstance))) {
2622 ERR("could not initialize local heap\n");
2625 TRACE("local heap initialized\n");
2628 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2629 alloc_size = ROUND_TO_GROW(countA);
2631 TRACE("Allocating 16-bit ANSI alias buffer\n");
2632 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2633 ERR("could not allocate new 16 bit buffer\n");
2637 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2638 ERR("could not lock new 16 bit buffer\n");
2639 LOCAL_Free(hInstance, es->hloc16);
2644 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2645 LOCAL_Unlock(hInstance, es->hloc16);
2647 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2652 /*********************************************************************
2657 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2660 INT line_len, dst_len;
2663 if (es->style & ES_MULTILINE) {
2664 if (line >= es->line_count)
2668 i = EDIT_EM_LineIndex(es, line);
2670 line_len = EDIT_EM_LineLength(es, i);
2671 dst_len = *(WORD *)dst;
2674 if(dst_len <= line_len)
2676 memcpy(dst, src, dst_len * sizeof(WCHAR));
2679 else /* Append 0 if enough space */
2681 memcpy(dst, src, line_len * sizeof(WCHAR));
2688 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2689 if(!ret) /* Insufficient buffer size */
2691 if(ret < dst_len) /* Append 0 if enough space */
2692 ((LPSTR)dst)[ret] = 0;
2698 /*********************************************************************
2703 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2705 UINT s = es->selection_start;
2706 UINT e = es->selection_end;
2713 return MAKELONG(s, e);
2717 /*********************************************************************
2721 * FIXME: is this right ? (or should it be only VSCROLL)
2722 * (and maybe only for edit controls that really have their
2723 * own scrollbars) (and maybe only for multiline controls ?)
2724 * All in all: very poorly documented
2727 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2729 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2730 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2734 /*********************************************************************
2739 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2744 if (!(es->style & ES_MULTILINE))
2746 if (index > (INT)strlenW(es->text))
2747 return es->line_count - 1;
2749 index = min(es->selection_start, es->selection_end);
2752 line_def = es->first_line_def;
2753 index -= line_def->length;
2754 while ((index >= 0) && line_def->next) {
2756 line_def = line_def->next;
2757 index -= line_def->length;
2763 /*********************************************************************
2768 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2773 if (!(es->style & ES_MULTILINE))
2775 if (line >= es->line_count)
2779 line_def = es->first_line_def;
2781 INT index = es->selection_end - line_def->length;
2782 while ((index >= 0) && line_def->next) {
2783 line_index += line_def->length;
2784 line_def = line_def->next;
2785 index -= line_def->length;
2789 line_index += line_def->length;
2790 line_def = line_def->next;
2798 /*********************************************************************
2803 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2807 if (!(es->style & ES_MULTILINE))
2808 return strlenW(es->text);
2811 /* get the number of remaining non-selected chars of selected lines */
2812 INT32 l; /* line number */
2813 INT32 li; /* index of first char in line */
2815 l = EDIT_EM_LineFromChar(es, es->selection_start);
2816 /* # chars before start of selection area */
2817 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2818 l = EDIT_EM_LineFromChar(es, es->selection_end);
2819 /* # chars after end of selection */
2820 li = EDIT_EM_LineIndex(es, l);
2821 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2824 line_def = es->first_line_def;
2825 index -= line_def->length;
2826 while ((index >= 0) && line_def->next) {
2827 line_def = line_def->next;
2828 index -= line_def->length;
2830 return line_def->net_length;
2834 /*********************************************************************
2838 * NOTE: dx is in average character widths, dy - in lines;
2841 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2843 if (!(es->style & ES_MULTILINE))
2846 dx *= es->char_width;
2847 return EDIT_EM_LineScroll_internal(es, dx, dy);
2850 /*********************************************************************
2852 * EDIT_EM_LineScroll_internal
2854 * Version of EDIT_EM_LineScroll for internal use.
2855 * It doesn't refuse if ES_MULTILINE is set and assumes that
2856 * dx is in pixels, dy - in lines.
2859 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2862 INT x_offset_in_pixels;
2863 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2866 if (es->style & ES_MULTILINE)
2868 x_offset_in_pixels = es->x_offset;
2873 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2876 if (-dx > x_offset_in_pixels)
2877 dx = -x_offset_in_pixels;
2878 if (dx > es->text_width - x_offset_in_pixels)
2879 dx = es->text_width - x_offset_in_pixels;
2880 nyoff = max(0, es->y_offset + dy);
2881 if (nyoff >= es->line_count - lines_per_page)
2882 nyoff = max(0, es->line_count - lines_per_page);
2883 dy = (es->y_offset - nyoff) * es->line_height;
2888 es->y_offset = nyoff;
2889 if(es->style & ES_MULTILINE)
2892 es->x_offset += dx / es->char_width;
2894 GetClientRect(es->hwndSelf, &rc1);
2895 IntersectRect(&rc, &rc1, &es->format_rect);
2896 ScrollWindowEx(es->hwndSelf, -dx, dy,
2897 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2898 /* force scroll info update */
2899 EDIT_UpdateScrollInfo(es);
2901 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2902 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2903 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2904 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2909 /*********************************************************************
2914 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2916 INT len = strlenW(es->text);
2929 index = min(index, len);
2930 dc = GetDC(es->hwndSelf);
2932 old_font = SelectObject(dc, es->font);
2933 if (es->style & ES_MULTILINE) {
2934 l = EDIT_EM_LineFromChar(es, index);
2935 y = (l - es->y_offset) * es->line_height;
2936 li = EDIT_EM_LineIndex(es, l);
2937 if (after_wrap && (li == index) && l) {
2939 line_def = es->first_line_def;
2941 line_def = line_def->next;
2944 if (line_def->ending == END_WRAP) {
2946 y -= es->line_height;
2947 li = EDIT_EM_LineIndex(es, l);
2951 line_def = es->first_line_def;
2952 while (line_def->index != li)
2953 line_def = line_def->next;
2955 ll = line_def->net_length;
2956 lw = line_def->width;
2958 w = es->format_rect.right - es->format_rect.left;
2959 if (es->style & ES_RIGHT)
2961 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
2962 es->tabs_count, es->tabs)) - es->x_offset;
2965 else if (es->style & ES_CENTER)
2967 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2968 es->tabs_count, es->tabs)) - es->x_offset;
2973 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2974 es->tabs_count, es->tabs)) - es->x_offset;
2977 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2978 if (index < es->x_offset) {
2979 GetTextExtentPoint32W(dc, text + index,
2980 es->x_offset - index, &size);
2983 GetTextExtentPoint32W(dc, text + es->x_offset,
2984 index - es->x_offset, &size);
2987 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
2989 w = es->format_rect.right - es->format_rect.left;
2990 if (w > es->text_width)
2992 if (es->style & ES_RIGHT)
2993 x += w - es->text_width;
2994 else if (es->style & ES_CENTER)
2995 x += (w - es->text_width) / 2;
3000 if (es->style & ES_PASSWORD)
3001 HeapFree(GetProcessHeap(), 0, text);
3003 x += es->format_rect.left;
3004 y += es->format_rect.top;
3006 SelectObject(dc, old_font);
3007 ReleaseDC(es->hwndSelf, dc);
3008 return MAKELONG((INT16)x, (INT16)y);
3012 /*********************************************************************
3016 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3019 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3021 UINT strl = strlenW(lpsz_replace);
3022 UINT tl = strlenW(es->text);
3031 TRACE("%s, can_undo %d, send_update %d\n",
3032 debugstr_w(lpsz_replace), can_undo, send_update);
3034 s = es->selection_start;
3035 e = es->selection_end;
3037 if ((s == e) && !strl)
3042 /* Issue the EN_MAXTEXT notification and continue with replacing text
3043 * such that buffer limit is honored. */
3044 size = tl - (e - s) + strl;
3045 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3046 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3047 strl = es->buffer_limit - (tl - (e-s));
3050 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3054 /* there is something to be deleted */
3055 TRACE("deleting stuff.\n");
3057 utl = strlenW(es->undo_text);
3058 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3059 /* undo-buffer is extended to the right */
3060 EDIT_MakeUndoFit(es, utl + e - s);
3061 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
3062 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3063 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3064 /* undo-buffer is extended to the left */
3065 EDIT_MakeUndoFit(es, utl + e - s);
3066 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3068 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3069 p[i] = (es->text + s)[i];
3070 es->undo_position = s;
3072 /* new undo-buffer */
3073 EDIT_MakeUndoFit(es, e - s);
3074 strncpyW(es->undo_text, es->text + s, e - s + 1);
3075 es->undo_text[e - s] = 0; /* ensure 0 termination */
3076 es->undo_position = s;
3078 /* any deletion makes the old insertion-undo invalid */
3079 es->undo_insert_count = 0;
3081 EDIT_EM_EmptyUndoBuffer(es);
3084 strcpyW(es->text + s, es->text + e);
3087 /* there is an insertion */
3089 if ((s == es->undo_position) ||
3090 ((es->undo_insert_count) &&
3091 (s == es->undo_position + es->undo_insert_count)))
3093 * insertion is new and at delete position or
3094 * an extension to either left or right
3096 es->undo_insert_count += strl;
3098 /* new insertion undo */
3099 es->undo_position = s;
3100 es->undo_insert_count = strl;
3101 /* new insertion makes old delete-buffer invalid */
3102 *es->undo_text = '\0';
3105 EDIT_EM_EmptyUndoBuffer(es);
3108 tl = strlenW(es->text);
3109 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));
3110 for (p = es->text + tl ; p >= es->text + s ; p--)
3112 for (i = 0 , p = es->text + s ; i < strl ; i++)
3113 p[i] = lpsz_replace[i];
3114 if(es->style & ES_UPPERCASE)
3115 CharUpperBuffW(p, strl);
3116 else if(es->style & ES_LOWERCASE)
3117 CharLowerBuffW(p, strl);
3120 if (es->style & ES_MULTILINE)
3122 INT s = min(es->selection_start, es->selection_end);
3124 hrgn = CreateRectRgn(0, 0, 0, 0);
3125 EDIT_BuildLineDefs_ML(es, s, s + strl,
3126 strl - abs(es->selection_end - es->selection_start), hrgn);
3129 EDIT_CalcLineWidth_SL(es);
3131 /* If text has been deleted and we're right or center aligned then scroll rightward */
3132 if (es->style & (ES_RIGHT | ES_CENTER))
3134 INT delta = strl - abs(es->selection_end - es->selection_start);
3136 if (delta < 0 && es->x_offset)
3138 if (abs(delta) > es->x_offset)
3141 es->x_offset += delta;
3145 EDIT_EM_SetSel(es, s, s, FALSE);
3146 es->flags |= EF_MODIFIED;
3147 if (send_update) es->flags |= EF_UPDATE;
3150 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3154 EDIT_UpdateText(es, NULL, TRUE);
3156 EDIT_EM_ScrollCaret(es);
3158 /* force scroll info update */
3159 EDIT_UpdateScrollInfo(es);
3162 if(es->flags & EF_UPDATE)
3164 es->flags &= ~EF_UPDATE;
3165 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3170 /*********************************************************************
3175 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3179 if (!(es->style & ES_MULTILINE))
3180 return (LRESULT)FALSE;
3190 if (es->y_offset < es->line_count - 1)
3195 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3198 if (es->y_offset < es->line_count - 1)
3199 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3202 return (LRESULT)FALSE;
3205 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3206 /* check if we are going to move too far */
3207 if(es->y_offset + dy > es->line_count - vlc)
3208 dy = es->line_count - vlc - es->y_offset;
3210 /* Notification is done in EDIT_EM_LineScroll */
3212 EDIT_EM_LineScroll(es, 0, dy);
3214 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3218 /*********************************************************************
3223 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3225 if (es->style & ES_MULTILINE) {
3230 INT cw = es->char_width;
3235 l = EDIT_EM_LineFromChar(es, es->selection_end);
3236 li = EDIT_EM_LineIndex(es, l);
3237 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3238 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3239 if (l >= es->y_offset + vlc)
3240 dy = l - vlc + 1 - es->y_offset;
3241 if (l < es->y_offset)
3242 dy = l - es->y_offset;
3243 ww = es->format_rect.right - es->format_rect.left;
3244 if (x < es->format_rect.left)
3245 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3246 if (x > es->format_rect.right)
3247 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3250 /* check if we are going to move too far */
3251 if(es->x_offset + dx + ww > es->text_width)
3252 dx = es->text_width - ww - es->x_offset;
3254 EDIT_EM_LineScroll_internal(es, dx, dy);
3261 if (!(es->style & ES_AUTOHSCROLL))
3264 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3265 format_width = es->format_rect.right - es->format_rect.left;
3266 if (x < es->format_rect.left) {
3267 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3270 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3271 } while ((x < goal) && es->x_offset);
3272 /* FIXME: use ScrollWindow() somehow to improve performance */
3273 EDIT_UpdateText(es, NULL, TRUE);
3274 } else if (x > es->format_rect.right) {
3276 INT len = strlenW(es->text);
3277 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3280 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3281 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3282 } while ((x > goal) && (x_last > es->format_rect.right));
3283 /* FIXME: use ScrollWindow() somehow to improve performance */
3284 EDIT_UpdateText(es, NULL, TRUE);
3288 if(es->flags & EF_FOCUSED)
3289 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3293 /*********************************************************************
3297 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3300 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3302 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3304 if (!(es->style & ES_MULTILINE))
3308 WARN("called with NULL handle\n");
3312 EDIT_UnlockBuffer(es, TRUE);
3316 LOCAL_Free(hInstance, es->hloc16);
3317 es->hloc16 = (HLOCAL16)NULL;
3324 LocalFree(es->hloc32A);
3336 countA = LocalSize(hloc);
3337 textA = LocalLock(hloc);
3338 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3339 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3341 ERR("Could not allocate new unicode buffer\n");
3344 textW = LocalLock(hloc32W_new);
3345 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3346 LocalUnlock(hloc32W_new);
3350 LocalFree(es->hloc32W);
3352 es->hloc32W = hloc32W_new;
3356 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3358 EDIT_LockBuffer(es);
3360 es->x_offset = es->y_offset = 0;
3361 es->selection_start = es->selection_end = 0;
3362 EDIT_EM_EmptyUndoBuffer(es);
3363 es->flags &= ~EF_MODIFIED;
3364 es->flags &= ~EF_UPDATE;
3365 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3366 EDIT_UpdateText(es, NULL, TRUE);
3367 EDIT_EM_ScrollCaret(es);
3368 /* force scroll info update */
3369 EDIT_UpdateScrollInfo(es);
3373 /*********************************************************************
3377 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3380 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3382 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3388 if (!(es->style & ES_MULTILINE))
3392 WARN("called with NULL handle\n");
3396 EDIT_UnlockBuffer(es, TRUE);
3400 LocalFree(es->hloc32A);
3404 countA = LOCAL_Size(hInstance, hloc);
3405 textA = LOCAL_Lock(hInstance, hloc);
3406 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3407 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3409 ERR("Could not allocate new unicode buffer\n");
3412 textW = LocalLock(hloc32W_new);
3413 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3414 LocalUnlock(hloc32W_new);
3415 LOCAL_Unlock(hInstance, hloc);
3418 LocalFree(es->hloc32W);
3420 es->hloc32W = hloc32W_new;
3423 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3425 EDIT_LockBuffer(es);
3427 es->x_offset = es->y_offset = 0;
3428 es->selection_start = es->selection_end = 0;
3429 EDIT_EM_EmptyUndoBuffer(es);
3430 es->flags &= ~EF_MODIFIED;
3431 es->flags &= ~EF_UPDATE;
3432 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3433 EDIT_UpdateText(es, NULL, TRUE);
3434 EDIT_EM_ScrollCaret(es);
3435 /* force scroll info update */
3436 EDIT_UpdateScrollInfo(es);
3440 /*********************************************************************
3444 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3445 * However, the windows version is not complied to yet in all of edit.c
3447 * Additionally as the wrapper for RichEdit controls we need larger buffers
3448 * at present -1 will represent nolimit
3450 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3452 if (limit == 0xFFFFFFFF)
3453 es->buffer_limit = -1;
3454 else if (es->style & ES_MULTILINE) {
3456 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3458 es->buffer_limit = BUFLIMIT_MULTI;
3461 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3463 es->buffer_limit = BUFLIMIT_SINGLE;
3468 /*********************************************************************
3472 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3473 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3474 * margin according to the textmetrics of the current font.
3476 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3477 * of the char's width as the margin, but this is not how Windows handles this.
3478 * For all other fonts Windows sets the margins to zero.
3481 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3482 INT left, INT right)
3485 INT default_left_margin = 0; /* in pixels */
3486 INT default_right_margin = 0; /* in pixels */
3488 /* Set the default margins depending on the font */
3489 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3490 HDC dc = GetDC(es->hwndSelf);
3491 HFONT old_font = SelectObject(dc, es->font);
3492 GetTextMetricsW(dc, &tm);
3493 /* The default margins are only non zero for TrueType or Vector fonts */
3494 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3495 /* This must be calculated more exactly! But how? */
3496 default_left_margin = tm.tmAveCharWidth / 3;
3497 default_right_margin = tm.tmAveCharWidth / 3;
3499 SelectObject(dc, old_font);
3500 ReleaseDC(es->hwndSelf, dc);
3503 if (action & EC_LEFTMARGIN) {
3504 if (left != EC_USEFONTINFO)
3505 es->left_margin = left;
3507 es->left_margin = default_left_margin;
3510 if (action & EC_RIGHTMARGIN) {
3511 if (right != EC_USEFONTINFO)
3512 es->right_margin = right;
3514 es->right_margin = default_right_margin;
3516 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3520 /*********************************************************************
3522 * EM_SETPASSWORDCHAR
3525 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3529 if (es->style & ES_MULTILINE)
3532 if (es->password_char == c)
3535 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3536 es->password_char = c;
3538 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3539 es->style |= ES_PASSWORD;
3541 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3542 es->style &= ~ES_PASSWORD;
3544 EDIT_UpdateText(es, NULL, TRUE);
3548 /*********************************************************************
3552 * note: unlike the specs say: the order of start and end
3553 * _is_ preserved in Windows. (i.e. start can be > end)
3554 * In other words: this handler is OK
3557 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3559 UINT old_start = es->selection_start;
3560 UINT old_end = es->selection_end;
3561 UINT len = strlenW(es->text);
3563 if (start == (UINT)-1) {
3564 start = es->selection_end;
3565 end = es->selection_end;
3567 start = min(start, len);
3568 end = min(end, len);
3570 es->selection_start = start;
3571 es->selection_end = end;
3573 es->flags |= EF_AFTER_WRAP;
3575 es->flags &= ~EF_AFTER_WRAP;
3576 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3577 ORDER_UINT(start, end);
3578 ORDER_UINT(end, old_end);
3579 ORDER_UINT(start, old_start);
3580 ORDER_UINT(old_start, old_end);
3581 if (end != old_start)
3585 * ORDER_UINT32(end, old_start);
3586 * EDIT_InvalidateText(es, start, end);
3587 * EDIT_InvalidateText(es, old_start, old_end);
3588 * in place of the following if statement.
3590 if (old_start > end )
3592 EDIT_InvalidateText(es, start, end);
3593 EDIT_InvalidateText(es, old_start, old_end);
3597 EDIT_InvalidateText(es, start, old_start);
3598 EDIT_InvalidateText(es, end, old_end);
3601 else EDIT_InvalidateText(es, start, old_end);
3605 /*********************************************************************
3610 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3612 if (!(es->style & ES_MULTILINE))
3614 HeapFree(GetProcessHeap(), 0, es->tabs);
3615 es->tabs_count = count;
3619 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3620 memcpy(es->tabs, tabs, count * sizeof(INT));
3626 /*********************************************************************
3631 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3633 if (!(es->style & ES_MULTILINE))
3635 HeapFree(GetProcessHeap(), 0, es->tabs);
3636 es->tabs_count = count;
3641 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3642 for (i = 0 ; i < count ; i++)
3643 es->tabs[i] = *tabs++;
3649 /*********************************************************************
3651 * EM_SETWORDBREAKPROC
3654 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3656 if (es->word_break_proc == wbp)
3659 es->word_break_proc = wbp;
3660 es->word_break_proc16 = NULL;
3662 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3663 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3664 EDIT_UpdateText(es, NULL, TRUE);
3669 /*********************************************************************
3671 * EM_SETWORDBREAKPROC16
3674 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3676 if (es->word_break_proc16 == wbp)
3679 es->word_break_proc = NULL;
3680 es->word_break_proc16 = wbp;
3681 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3682 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3683 EDIT_UpdateText(es, NULL, TRUE);
3688 /*********************************************************************
3693 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3698 /* As per MSDN spec, for a single-line edit control,
3699 the return value is always TRUE */
3700 if( es->style & ES_READONLY )
3701 return !(es->style & ES_MULTILINE);
3703 ulength = strlenW(es->undo_text);
3705 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3707 strcpyW(utext, es->undo_text);
3709 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3710 es->undo_insert_count, debugstr_w(utext));
3712 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3713 EDIT_EM_EmptyUndoBuffer(es);
3714 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3715 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3716 /* send the notification after the selection start and end are set */
3717 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3718 EDIT_EM_ScrollCaret(es);
3719 HeapFree(GetProcessHeap(), 0, utext);
3721 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3722 es->undo_insert_count, debugstr_w(es->undo_text));
3727 /*********************************************************************
3732 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3736 control = GetKeyState(VK_CONTROL) & 0x8000;
3740 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3741 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3744 if (es->style & ES_MULTILINE) {
3745 if (es->style & ES_READONLY) {
3746 EDIT_MoveHome(es, FALSE);
3747 EDIT_MoveDown_ML(es, FALSE);
3749 static const WCHAR cr_lfW[] = {'\r','\n',0};
3750 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3755 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3757 static const WCHAR tabW[] = {'\t',0};
3758 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3762 if (!(es->style & ES_READONLY) && !control) {
3763 if (es->selection_start != es->selection_end)
3766 /* delete character left of caret */
3767 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3768 EDIT_MoveBackward(es, TRUE);
3774 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3777 if (!(es->style & ES_READONLY))
3778 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3781 if (!(es->style & ES_READONLY))
3782 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3786 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3787 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3790 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3794 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3801 /*********************************************************************
3806 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3808 if (code || control)
3828 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3829 EDIT_EM_ScrollCaret(es);
3832 ERR("unknown menu item, please report\n");
3838 /*********************************************************************
3842 * Note: the resource files resource/sysres_??.rc cannot define a
3843 * single popup menu. Hence we use a (dummy) menubar
3844 * containing the single popup menu as its first item.
3846 * FIXME: the message identifiers have been chosen arbitrarily,
3847 * hence we use MF_BYPOSITION.
3848 * We might as well use the "real" values (anybody knows ?)
3849 * The menu definition is in resources/sysres_??.rc.
3850 * Once these are OK, we better use MF_BYCOMMAND here
3851 * (as we do in EDIT_WM_Command()).
3854 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3856 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3857 HMENU popup = GetSubMenu(menu, 0);
3858 UINT start = es->selection_start;
3859 UINT end = es->selection_end;
3861 ORDER_UINT(start, end);
3864 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3866 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3868 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3870 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3872 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3874 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3876 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3881 /*********************************************************************
3886 static void EDIT_WM_Copy(EDITSTATE *es)
3888 INT s = min(es->selection_start, es->selection_end);
3889 INT e = max(es->selection_start, es->selection_end);
3895 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3896 dst = GlobalLock(hdst);
3897 strncpyW(dst, es->text + s, e - s);
3898 dst[e - s] = 0; /* ensure 0 termination */
3899 TRACE("%s\n", debugstr_w(dst));
3901 OpenClipboard(es->hwndSelf);
3903 SetClipboardData(CF_UNICODETEXT, hdst);
3908 /*********************************************************************
3913 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3915 TRACE("%s\n", debugstr_w(name));
3917 * To initialize some final structure members, we call some helper
3918 * functions. However, since the EDITSTATE is not consistent (i.e.
3919 * not fully initialized), we should be very careful which
3920 * functions can be called, and in what order.
3922 EDIT_WM_SetFont(es, 0, FALSE);
3923 EDIT_EM_EmptyUndoBuffer(es);
3925 if (name && *name) {
3926 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3927 /* if we insert text to the editline, the text scrolls out
3928 * of the window, as the caret is placed after the insert
3929 * pos normally; thus we reset es->selection... to 0 and
3932 es->selection_start = es->selection_end = 0;
3933 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3934 * Messages are only to be sent when the USER does something to
3935 * change the contents. So I am removing this EN_CHANGE
3937 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3939 EDIT_EM_ScrollCaret(es);
3941 /* force scroll info update */
3942 EDIT_UpdateScrollInfo(es);
3943 /* The rule seems to return 1 here for success */
3944 /* Power Builder masked edit controls will crash */
3946 /* FIXME: is that in all cases so ? */
3951 /*********************************************************************
3956 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3961 while (LocalUnlock(es->hloc32W)) ;
3962 LocalFree(es->hloc32W);
3965 while (LocalUnlock(es->hloc32A)) ;
3966 LocalFree(es->hloc32A);
3969 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3970 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3971 LOCAL_Free(hInstance, es->hloc16);
3974 pc = es->first_line_def;
3978 HeapFree(GetProcessHeap(), 0, pc);
3982 SetWindowLongW( es->hwndSelf, 0, 0 );
3983 HeapFree(GetProcessHeap(), 0, es);
3989 /*********************************************************************
3994 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3996 /* we do the proper erase in EDIT_WM_Paint */
4001 /*********************************************************************
4006 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4008 if(!count) return 0;
4012 lstrcpynW(dst, es->text, count);
4013 return strlenW(dst);
4017 LPSTR textA = (LPSTR)dst;
4018 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4019 textA[count - 1] = 0; /* ensure 0 termination */
4020 return strlen(textA);
4024 /*********************************************************************
4029 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4034 if (!(es->style & ES_MULTILINE))
4037 if (!(es->style & ES_AUTOHSCROLL))
4041 fw = es->format_rect.right - es->format_rect.left;
4044 TRACE("SB_LINELEFT\n");
4046 dx = -es->char_width;
4049 TRACE("SB_LINERIGHT\n");
4050 if (es->x_offset < es->text_width)
4051 dx = es->char_width;
4054 TRACE("SB_PAGELEFT\n");
4056 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4059 TRACE("SB_PAGERIGHT\n");
4060 if (es->x_offset < es->text_width)
4061 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4069 TRACE("SB_RIGHT\n");
4070 if (es->x_offset < es->text_width)
4071 dx = es->text_width - es->x_offset;
4074 TRACE("SB_THUMBTRACK %d\n", pos);
4075 es->flags |= EF_HSCROLL_TRACK;
4076 if(es->style & WS_HSCROLL)
4077 dx = pos - es->x_offset;
4082 if(pos < 0 || pos > 100) return 0;
4083 /* Assume default scroll range 0-100 */
4084 fw = es->format_rect.right - es->format_rect.left;
4085 new_x = pos * (es->text_width - fw) / 100;
4086 dx = es->text_width ? (new_x - es->x_offset) : 0;
4089 case SB_THUMBPOSITION:
4090 TRACE("SB_THUMBPOSITION %d\n", pos);
4091 es->flags &= ~EF_HSCROLL_TRACK;
4092 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4093 dx = pos - es->x_offset;
4098 if(pos < 0 || pos > 100) return 0;
4099 /* Assume default scroll range 0-100 */
4100 fw = es->format_rect.right - es->format_rect.left;
4101 new_x = pos * (es->text_width - fw) / 100;
4102 dx = es->text_width ? (new_x - es->x_offset) : 0;
4105 /* force scroll info update */
4106 EDIT_UpdateScrollInfo(es);
4107 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
4111 TRACE("SB_ENDSCROLL\n");
4114 * FIXME : the next two are undocumented !
4115 * Are we doing the right thing ?
4116 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4117 * although it's also a regular control message.
4119 case EM_GETTHUMB: /* this one is used by NT notepad */
4123 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4124 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4127 /* Assume default scroll range 0-100 */
4128 INT fw = es->format_rect.right - es->format_rect.left;
4129 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4131 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4134 case EM_LINESCROLL16:
4135 TRACE("EM_LINESCROLL16\n");
4140 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4146 INT fw = es->format_rect.right - es->format_rect.left;
4147 /* check if we are going to move too far */
4148 if(es->x_offset + dx + fw > es->text_width)
4149 dx = es->text_width - fw - es->x_offset;
4151 EDIT_EM_LineScroll_internal(es, dx, 0);
4157 /*********************************************************************
4162 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4164 HWND hLBox = es->hwndListBox;
4172 hCombo = GetParent(es->hwndSelf);
4176 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4178 if (key == VK_UP || key == VK_DOWN)
4180 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4183 if (msg == WM_KEYDOWN || nEUI)
4184 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4190 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4192 /* make sure ComboLBox pops up */
4193 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4198 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4201 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4203 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4205 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4210 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4216 /*********************************************************************
4220 * Handling of special keys that don't produce a WM_CHAR
4221 * (i.e. non-printable keys) & Backspace & Delete
4224 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4229 if (GetKeyState(VK_MENU) & 0x8000)
4232 shift = GetKeyState(VK_SHIFT) & 0x8000;
4233 control = GetKeyState(VK_CONTROL) & 0x8000;
4238 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4243 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4244 EDIT_MoveUp_ML(es, shift);
4247 EDIT_MoveWordBackward(es, shift);
4249 EDIT_MoveBackward(es, shift);
4252 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4256 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4257 EDIT_MoveDown_ML(es, shift);
4259 EDIT_MoveWordForward(es, shift);
4261 EDIT_MoveForward(es, shift);
4264 EDIT_MoveHome(es, shift);
4267 EDIT_MoveEnd(es, shift);
4270 if (es->style & ES_MULTILINE)
4271 EDIT_MovePageUp_ML(es, shift);
4273 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4276 if (es->style & ES_MULTILINE)
4277 EDIT_MovePageDown_ML(es, shift);
4279 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4282 if (!(es->style & ES_READONLY) && !(shift && control)) {
4283 if (es->selection_start != es->selection_end) {
4290 /* delete character left of caret */
4291 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4292 EDIT_MoveBackward(es, TRUE);
4294 } else if (control) {
4295 /* delete to end of line */
4296 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4297 EDIT_MoveEnd(es, TRUE);
4300 /* delete character right of caret */
4301 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4302 EDIT_MoveForward(es, TRUE);
4310 if (!(es->style & ES_READONLY))
4316 /* If the edit doesn't want the return send a message to the default object */
4317 if(!(es->style & ES_WANTRETURN))
4319 HWND hwndParent = GetParent(es->hwndSelf);
4320 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4321 if (HIWORD(dw) == DC_HASDEFID)
4323 SendMessageW( hwndParent, WM_COMMAND,
4324 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4325 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4334 /*********************************************************************
4339 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4341 es->flags &= ~EF_FOCUSED;
4343 if(!(es->style & ES_NOHIDESEL))
4344 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4345 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4350 /*********************************************************************
4354 * The caret position has been set on the WM_LBUTTONDOWN message
4357 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4360 INT e = es->selection_end;
4365 if (!(es->flags & EF_FOCUSED))
4368 l = EDIT_EM_LineFromChar(es, e);
4369 li = EDIT_EM_LineIndex(es, l);
4370 ll = EDIT_EM_LineLength(es, e);
4371 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4372 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4373 EDIT_EM_SetSel(es, s, e, FALSE);
4374 EDIT_EM_ScrollCaret(es);
4379 /*********************************************************************
4384 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4389 SetFocus(es->hwndSelf);
4390 if (!(es->flags & EF_FOCUSED))
4393 es->bCaptureState = TRUE;
4394 SetCapture(es->hwndSelf);
4395 EDIT_ConfinePoint(es, &x, &y);
4396 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4397 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4398 EDIT_EM_ScrollCaret(es);
4399 es->region_posx = es->region_posy = 0;
4400 SetTimer(es->hwndSelf, 0, 100, NULL);
4405 /*********************************************************************
4410 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4412 if (es->bCaptureState) {
4413 KillTimer(es->hwndSelf, 0);
4414 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4416 es->bCaptureState = FALSE;
4421 /*********************************************************************
4426 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4428 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4433 /*********************************************************************
4438 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4444 if (GetCapture() != es->hwndSelf)
4448 * FIXME: gotta do some scrolling if outside client
4449 * area. Maybe reset the timer ?
4452 EDIT_ConfinePoint(es, &x, &y);
4453 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4454 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4455 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4456 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4457 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4462 /*********************************************************************
4466 * See also EDIT_WM_StyleChanged
4468 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4473 TRACE("Creating %s edit control, style = %08lx\n",
4474 unicode ? "Unicode" : "ANSI", lpcs->style);
4476 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4478 SetWindowLongW( hwnd, 0, (LONG)es );
4481 * Note: since the EDITSTATE has not been fully initialized yet,
4482 * we can't use any API calls that may send
4483 * WM_XXX messages before WM_NCCREATE is completed.
4486 es->is_unicode = unicode;
4487 es->style = lpcs->style;
4489 es->bEnableState = !(es->style & WS_DISABLED);
4491 es->hwndSelf = hwnd;
4492 /* Save parent, which will be notified by EN_* messages */
4493 es->hwndParent = lpcs->hwndParent;
4495 if (es->style & ES_COMBO)
4496 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4498 /* Number overrides lowercase overrides uppercase (at least it
4499 * does in Win95). However I'll bet that ES_NUMBER would be
4500 * invalid under Win 3.1.
4502 if (es->style & ES_NUMBER) {
4503 ; /* do not override the ES_NUMBER */
4504 } else if (es->style & ES_LOWERCASE) {
4505 es->style &= ~ES_UPPERCASE;
4507 if (es->style & ES_MULTILINE) {
4508 es->buffer_limit = BUFLIMIT_MULTI;
4509 if (es->style & WS_VSCROLL)
4510 es->style |= ES_AUTOVSCROLL;
4511 if (es->style & WS_HSCROLL)
4512 es->style |= ES_AUTOHSCROLL;
4513 es->style &= ~ES_PASSWORD;
4514 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4515 /* Confirmed - RIGHT overrides CENTER */
4516 if (es->style & ES_RIGHT)
4517 es->style &= ~ES_CENTER;
4518 es->style &= ~WS_HSCROLL;
4521 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4522 es->style |= ES_AUTOVSCROLL;
4524 es->buffer_limit = BUFLIMIT_SINGLE;
4525 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4526 es->style &= ~ES_CENTER;
4527 es->style &= ~WS_HSCROLL;
4528 es->style &= ~WS_VSCROLL;
4529 if (es->style & ES_PASSWORD)
4530 es->password_char = '*';
4532 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4533 es->style |= ES_AUTOHSCROLL;
4536 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4537 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4539 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4541 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4543 es->undo_buffer_size = es->buffer_size;
4545 if (es->style & ES_MULTILINE)
4546 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4551 * In Win95 look and feel, the WS_BORDER style is replaced by the
4552 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4553 * control a nonclient area so we don't need to draw the border.
4554 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4555 * a nonclient area and we should handle painting the border ourselves.
4557 * When making modifications please ensure that the code still works
4558 * for edit controls created directly with style 0x50800000, exStyle 0
4559 * (which should have a single pixel border)
4561 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4562 es->style &= ~WS_BORDER;
4563 else if (es->style & WS_BORDER)
4564 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4569 /*********************************************************************
4574 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4585 BOOL rev = es->bEnableState &&
4586 ((es->flags & EF_FOCUSED) ||
4587 (es->style & ES_NOHIDESEL));
4588 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4590 GetClientRect(es->hwndSelf, &rcClient);
4592 /* paint the background */
4593 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
4594 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
4595 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4596 GetClipBox(dc, &rc);
4597 FillRect(dc, &rc, brush);
4599 /* draw the border */
4600 if(es->style & WS_BORDER) {
4602 if(es->style & ES_MULTILINE) {
4603 if(es->style & WS_HSCROLL) rc.bottom++;
4604 if(es->style & WS_VSCROLL) rc.right++;
4606 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4608 IntersectClipRect(dc, es->format_rect.left,
4609 es->format_rect.top,
4610 es->format_rect.right,
4611 es->format_rect.bottom);
4612 if (es->style & ES_MULTILINE) {
4614 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4617 old_font = SelectObject(dc, es->font);
4618 EDIT_NotifyCtlColor(es, dc);
4620 if (!es->bEnableState)
4621 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4622 GetClipBox(dc, &rcRgn);
4623 if (es->style & ES_MULTILINE) {
4624 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4625 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4626 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4627 if (IntersectRect(&rc, &rcRgn, &rcLine))
4628 EDIT_PaintLine(es, dc, i, rev);
4631 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4632 if (IntersectRect(&rc, &rcRgn, &rcLine))
4633 EDIT_PaintLine(es, dc, 0, rev);
4636 SelectObject(dc, old_font);
4639 EndPaint(es->hwndSelf, &ps);
4643 /*********************************************************************
4648 static void EDIT_WM_Paste(EDITSTATE *es)
4653 /* Protect read-only edit control from modification */
4654 if(es->style & ES_READONLY)
4657 OpenClipboard(es->hwndSelf);
4658 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4659 src = (LPWSTR)GlobalLock(hsrc);
4660 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4667 /*********************************************************************
4672 static void EDIT_WM_SetFocus(EDITSTATE *es)
4674 es->flags |= EF_FOCUSED;
4675 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4676 EDIT_SetCaretPos(es, es->selection_end,
4677 es->flags & EF_AFTER_WRAP);
4678 if(!(es->style & ES_NOHIDESEL))
4679 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4680 ShowCaret(es->hwndSelf);
4681 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4685 /*********************************************************************
4689 * With Win95 look the margins are set to default font value unless
4690 * the system font (font == 0) is being set, in which case they are left
4694 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4702 dc = GetDC(es->hwndSelf);
4704 old_font = SelectObject(dc, font);
4705 GetTextMetricsW(dc, &tm);
4706 es->line_height = tm.tmHeight;
4707 es->char_width = tm.tmAveCharWidth;
4709 SelectObject(dc, old_font);
4710 ReleaseDC(es->hwndSelf, dc);
4711 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4712 EC_USEFONTINFO, EC_USEFONTINFO);
4714 /* Force the recalculation of the format rect for each font change */
4715 GetClientRect(es->hwndSelf, &r);
4716 EDIT_SetRectNP(es, &r);
4718 if (es->style & ES_MULTILINE)
4719 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4721 EDIT_CalcLineWidth_SL(es);
4724 EDIT_UpdateText(es, NULL, TRUE);
4725 if (es->flags & EF_FOCUSED) {
4727 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4728 EDIT_SetCaretPos(es, es->selection_end,
4729 es->flags & EF_AFTER_WRAP);
4730 ShowCaret(es->hwndSelf);
4735 /*********************************************************************
4740 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4741 * The modified flag is reset. No notifications are sent.
4743 * For single-line controls, reception of WM_SETTEXT triggers:
4744 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4747 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4749 if (!unicode && text)
4751 LPCSTR textA = (LPCSTR)text;
4752 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4753 LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
4755 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4759 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4762 TRACE("%s\n", debugstr_w(text));
4763 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4765 HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
4769 static const WCHAR empty_stringW[] = {0};
4771 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4774 es->flags &= ~EF_MODIFIED;
4775 EDIT_EM_SetSel(es, 0, 0, FALSE);
4777 /* Send the notification after the selection start and end have been set
4778 * edit control doesn't send notification on WM_SETTEXT
4779 * if it is multiline, or it is part of combobox
4781 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4783 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4784 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4786 EDIT_EM_ScrollCaret(es);
4787 EDIT_UpdateScrollInfo(es);
4791 /*********************************************************************
4796 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4798 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4800 TRACE("width = %d, height = %d\n", width, height);
4801 SetRect(&rc, 0, 0, width, height);
4802 EDIT_SetRectNP(es, &rc);
4803 EDIT_UpdateText(es, NULL, TRUE);
4808 /*********************************************************************
4812 * This message is sent by SetWindowLong on having changed either the Style
4813 * or the extended style.
4815 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4817 * See also EDIT_WM_NCCreate
4819 * It appears that the Windows version of the edit control allows the style
4820 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4821 * style variable which will generally be different. In this function we
4822 * update the internal style based on what changed in the externally visible
4825 * Much of this content as based upon the MSDN, especially:
4826 * Platform SDK Documentation -> User Interface Services ->
4827 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4828 * Edit Control Styles
4830 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4832 if (GWL_STYLE == which) {
4833 DWORD style_change_mask;
4835 /* Only a subset of changes can be applied after the control
4838 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4840 if (es->style & ES_MULTILINE)
4841 style_change_mask |= ES_WANTRETURN;
4843 new_style = style->styleNew & style_change_mask;
4845 /* Number overrides lowercase overrides uppercase (at least it
4846 * does in Win95). However I'll bet that ES_NUMBER would be
4847 * invalid under Win 3.1.
4849 if (new_style & ES_NUMBER) {
4850 ; /* do not override the ES_NUMBER */
4851 } else if (new_style & ES_LOWERCASE) {
4852 new_style &= ~ES_UPPERCASE;
4855 es->style = (es->style & ~style_change_mask) | new_style;
4856 } else if (GWL_EXSTYLE == which) {
4857 ; /* FIXME - what is needed here */
4859 WARN ("Invalid style change %d\n",which);
4865 /*********************************************************************
4870 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4872 if ((key == VK_BACK) && (key_data & 0x2000)) {
4873 if (EDIT_EM_CanUndo(es))
4876 } else if (key == VK_UP || key == VK_DOWN) {
4877 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4880 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4884 /*********************************************************************
4889 static void EDIT_WM_Timer(EDITSTATE *es)
4891 if (es->region_posx < 0) {
4892 EDIT_MoveBackward(es, TRUE);
4893 } else if (es->region_posx > 0) {
4894 EDIT_MoveForward(es, TRUE);
4897 * FIXME: gotta do some vertical scrolling here, like
4898 * EDIT_EM_LineScroll(hwnd, 0, 1);
4902 /*********************************************************************
4907 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4911 if (!(es->style & ES_MULTILINE))
4914 if (!(es->style & ES_AUTOVSCROLL))
4923 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4924 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4925 (action == SB_PAGEUP ? "SB_PAGEUP" :
4927 EDIT_EM_Scroll(es, action);
4934 TRACE("SB_BOTTOM\n");
4935 dy = es->line_count - 1 - es->y_offset;
4938 TRACE("SB_THUMBTRACK %d\n", pos);
4939 es->flags |= EF_VSCROLL_TRACK;
4940 if(es->style & WS_VSCROLL)
4941 dy = pos - es->y_offset;
4944 /* Assume default scroll range 0-100 */
4947 if(pos < 0 || pos > 100) return 0;
4948 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4949 new_y = pos * (es->line_count - vlc) / 100;
4950 dy = es->line_count ? (new_y - es->y_offset) : 0;
4951 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4952 es->line_count, es->y_offset, pos, dy);
4955 case SB_THUMBPOSITION:
4956 TRACE("SB_THUMBPOSITION %d\n", pos);
4957 es->flags &= ~EF_VSCROLL_TRACK;
4958 if(es->style & WS_VSCROLL)
4959 dy = pos - es->y_offset;
4962 /* Assume default scroll range 0-100 */
4965 if(pos < 0 || pos > 100) return 0;
4966 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4967 new_y = pos * (es->line_count - vlc) / 100;
4968 dy = es->line_count ? (new_y - es->y_offset) : 0;
4969 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4970 es->line_count, es->y_offset, pos, dy);
4974 /* force scroll info update */
4975 EDIT_UpdateScrollInfo(es);
4976 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4980 TRACE("SB_ENDSCROLL\n");
4983 * FIXME : the next two are undocumented !
4984 * Are we doing the right thing ?
4985 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4986 * although it's also a regular control message.
4988 case EM_GETTHUMB: /* this one is used by NT notepad */
4992 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4993 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4996 /* Assume default scroll range 0-100 */
4997 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4998 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5000 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5003 case EM_LINESCROLL16:
5004 TRACE("EM_LINESCROLL16 %d\n", pos);
5009 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5014 EDIT_EM_LineScroll(es, 0, dy);
5018 /*********************************************************************
5023 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5025 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5026 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5030 /*********************************************************************
5035 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5037 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5038 InvalidateRect(es->hwndSelf, rc, bErase);