4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
56 #include "wine/winbase16.h"
57 #include "wine/winuser16.h"
58 #include "wine/unicode.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
69 FIXME: BTW, new specs say 65535 (do you dare ???) */
70 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
71 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
72 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
73 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
76 * extra flags for EDITSTATE.flags field
78 #define EF_MODIFIED 0x0001 /* text has been modified */
79 #define EF_FOCUSED 0x0002 /* we have input focus */
80 #define EF_UPDATE 0x0004 /* notify parent of changed state */
81 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
82 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
83 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
84 wrapped line, instead of in front of the next character */
85 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
89 END_0 = 0, /* line ends with terminating '\0' character */
90 END_WRAP, /* line is wrapped */
91 END_HARD, /* line ends with a hard return '\r\n' */
92 END_SOFT, /* line ends with a soft return '\r\r\n' */
93 END_RICH /* line ends with a single '\n' */
96 typedef struct tagLINEDEF {
97 INT length; /* bruto length of a line in bytes */
98 INT net_length; /* netto length of a line in visible characters */
100 INT width; /* width of the line in pixels */
101 INT index; /* line index into the buffer */
102 struct tagLINEDEF *next;
107 BOOL is_unicode; /* how the control was created */
108 LPWSTR text; /* the actual contents of the control */
109 UINT buffer_size; /* the size of the buffer in characters */
110 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
111 HFONT font; /* NULL means standard system font */
112 INT x_offset; /* scroll offset for multi lines this is in pixels
113 for single lines it's in characters */
114 INT line_height; /* height of a screen line in pixels */
115 INT char_width; /* average character width in pixels */
116 DWORD style; /* sane version of wnd->dwStyle */
117 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
118 INT undo_insert_count; /* number of characters inserted in sequence */
119 UINT undo_position; /* character index of the insertion and deletion */
120 LPWSTR undo_text; /* deleted text */
121 UINT undo_buffer_size; /* size of the deleted text buffer */
122 INT selection_start; /* == selection_end if no selection */
123 INT selection_end; /* == current caret position */
124 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
125 INT left_margin; /* in pixels */
126 INT right_margin; /* in pixels */
128 INT text_width; /* width of the widest line in pixels for multi line controls
129 and just line width for single line controls */
130 INT region_posx; /* Position of cursor relative to region: */
131 INT region_posy; /* -1: to left, 0: within, 1: to right */
132 EDITWORDBREAKPROC16 word_break_proc16;
133 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
134 INT line_count; /* number of lines */
135 INT y_offset; /* scroll offset in number of lines */
136 BOOL bCaptureState; /* flag indicating whether mouse was captured */
137 BOOL bEnableState; /* flag keeping the enable state */
138 HWND hwndSelf; /* the our window handle */
139 HWND hwndParent; /* Handle of parent for sending EN_* messages.
140 Even if parent will change, EN_* messages
141 should be sent to the first parent. */
142 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
144 * only for multi line controls
146 INT lock_count; /* amount of re-entries in the EditWndProc */
149 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
150 HLOCAL hloc32W; /* our unicode local memory block */
151 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
153 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
158 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
159 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
161 /* used for disabled or read-only edit control */
162 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
164 { /* Notify parent which has created this edit control */ \
165 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
166 SendMessageW(es->hwndParent, WM_COMMAND, \
167 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
168 (LPARAM)(es->hwndSelf)); \
171 /*********************************************************************
178 * These functions have trivial implementations
179 * We still like to call them internally
180 * "static inline" makes them more like macro's
182 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
183 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
184 static inline void EDIT_WM_Clear(EDITSTATE *es);
185 static inline void EDIT_WM_Cut(EDITSTATE *es);
188 * Helper functions only valid for one type of control
190 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
191 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
192 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
193 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
194 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
195 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
196 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
198 * Helper functions valid for both single line _and_ multi line controls
200 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
201 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
202 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
203 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
204 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
205 static void EDIT_LockBuffer(EDITSTATE *es);
206 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
207 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
208 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
209 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
210 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
211 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
212 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
213 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
214 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
215 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
216 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
217 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
218 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
219 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
220 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
222 * EM_XXX message handlers
224 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
225 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
226 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
227 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
228 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
229 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
230 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
231 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
232 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
233 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
234 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
235 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
236 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
237 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
238 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
239 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
240 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
241 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
242 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
243 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
244 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
245 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
246 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
247 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
248 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
249 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
250 static BOOL EDIT_EM_Undo(EDITSTATE *es);
252 * WM_XXX message handlers
254 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
255 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
256 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
257 static void EDIT_WM_Copy(EDITSTATE *es);
258 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
259 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
260 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
261 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
262 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
263 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
264 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
265 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
266 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
267 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
268 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
269 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
270 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
271 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
272 static void EDIT_WM_Paste(EDITSTATE *es);
273 static void EDIT_WM_SetFocus(EDITSTATE *es);
274 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
275 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
276 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
277 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
278 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
279 static void EDIT_WM_Timer(EDITSTATE *es);
280 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
281 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
282 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
284 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
285 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
287 /*********************************************************************
288 * edit class descriptor
290 const struct builtin_class_descr EDIT_builtin_class =
293 CS_DBLCLKS | CS_PARENTDC, /* style */
294 EditWndProcA, /* procA */
295 EditWndProcW, /* procW */
296 sizeof(EDITSTATE *), /* extra */
297 IDC_IBEAM, /* cursor */
302 /*********************************************************************
307 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
309 return (es->undo_insert_count || strlenW(es->undo_text));
313 /*********************************************************************
318 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
320 es->undo_insert_count = 0;
321 *es->undo_text = '\0';
325 /*********************************************************************
330 static inline void EDIT_WM_Clear(EDITSTATE *es)
332 static const WCHAR empty_stringW[] = {0};
334 /* Protect read-only edit control from modification */
335 if(es->style & ES_READONLY)
338 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
342 /*********************************************************************
347 static inline void EDIT_WM_Cut(EDITSTATE *es)
354 /**********************************************************************
357 * Returns the window version in case Wine emulates a later version
358 * of windows than the application expects.
360 * In a number of cases when windows runs an application that was
361 * designed for an earlier windows version, windows reverts
362 * to "old" behaviour of that earlier version.
364 * An example is a disabled edit control that needs to be painted.
365 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
366 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
367 * applications with an expected version 0f 4.0 or higher.
370 static DWORD get_app_version(void)
372 static DWORD version;
375 DWORD dwEmulatedVersion;
377 DWORD dwProcVersion = GetProcessVersion(0);
379 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
380 GetVersionExW( &info );
381 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
382 /* FIXME: this may not be 100% correct; see discussion on the
383 * wine developer list in Nov 1999 */
384 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
390 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
394 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
395 msg = WM_CTLCOLORSTATIC;
397 msg = WM_CTLCOLOREDIT;
399 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
400 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
403 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
406 return DefWindowProcW(hwnd, msg, wParam, lParam);
408 return DefWindowProcA(hwnd, msg, wParam, lParam);
411 /*********************************************************************
415 * The messages are in the order of the actual integer values
416 * (which can be found in include/windows.h)
417 * Wherever possible the 16 bit versions are converted to
418 * the 32 bit ones, so that we can 'fall through' to the
419 * helper functions. These are mostly 32 bit (with a few
420 * exceptions, clearly indicated by a '16' extension to their
424 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
425 WPARAM wParam, LPARAM lParam, BOOL unicode )
427 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
430 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
432 if (!es && msg != WM_NCCREATE)
433 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
434 else if (msg == WM_NCCREATE)
435 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
436 else if (msg == WM_DESTROY)
437 return EDIT_WM_Destroy(es);
440 if (es) EDIT_LockBuffer(es);
448 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
452 if ((short)LOWORD(lParam) == -1)
453 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
455 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
457 EDIT_EM_ScrollCaret(es);
461 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
462 EDIT_EM_ScrollCaret(es);
469 RECT16 *r16 = MapSL(lParam);
470 r16->left = es->format_rect.left;
471 r16->top = es->format_rect.top;
472 r16->right = es->format_rect.right;
473 r16->bottom = es->format_rect.bottom;
478 CopyRect((LPRECT)lParam, &es->format_rect);
482 if ((es->style & ES_MULTILINE) && lParam) {
484 RECT16 *r16 = MapSL(lParam);
487 rc.right = r16->right;
488 rc.bottom = r16->bottom;
489 EDIT_SetRectNP(es, &rc);
490 EDIT_UpdateText(es, NULL, TRUE);
494 if ((es->style & ES_MULTILINE) && lParam) {
495 EDIT_SetRectNP(es, (LPRECT)lParam);
496 EDIT_UpdateText(es, NULL, TRUE);
501 if ((es->style & ES_MULTILINE) && lParam) {
503 RECT16 *r16 = MapSL(lParam);
506 rc.right = r16->right;
507 rc.bottom = r16->bottom;
508 EDIT_SetRectNP(es, &rc);
512 if ((es->style & ES_MULTILINE) && lParam)
513 EDIT_SetRectNP(es, (LPRECT)lParam);
518 result = EDIT_EM_Scroll(es, (INT)wParam);
521 case EM_LINESCROLL16:
522 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
523 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
526 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
529 case EM_SCROLLCARET16:
531 EDIT_EM_ScrollCaret(es);
537 result = ((es->flags & EF_MODIFIED) != 0);
543 es->flags |= EF_MODIFIED;
545 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
548 case EM_GETLINECOUNT16:
549 case EM_GETLINECOUNT:
550 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
554 if ((INT16)wParam == -1)
558 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
562 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
565 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
569 result = (LRESULT)EDIT_EM_GetHandle16(es);
572 result = (LRESULT)EDIT_EM_GetHandle(es);
577 result = EDIT_EM_GetThumb(es);
580 /* these messages missing from specs */
589 FIXME("undocumented message 0x%x, please report\n", msg);
590 result = DefWindowProcW(hwnd, msg, wParam, lParam);
593 case EM_LINELENGTH16:
595 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
598 case EM_REPLACESEL16:
599 lParam = (LPARAM)MapSL(lParam);
600 unicode = FALSE; /* 16-bit message is always ascii */
607 textW = (LPWSTR)lParam;
610 LPSTR textA = (LPSTR)lParam;
611 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
612 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
613 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
616 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
620 HeapFree(GetProcessHeap(), 0, textW);
625 lParam = (LPARAM)MapSL(lParam);
626 unicode = FALSE; /* 16-bit message is always ascii */
629 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
633 case EM_SETLIMITTEXT:
634 EDIT_EM_SetLimitText(es, (INT)wParam);
639 result = (LRESULT)EDIT_EM_CanUndo(es);
645 result = (LRESULT)EDIT_EM_Undo(es);
650 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
653 case EM_LINEFROMCHAR16:
654 case EM_LINEFROMCHAR:
655 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
658 case EM_SETTABSTOPS16:
659 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
662 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
665 case EM_SETPASSWORDCHAR16:
666 unicode = FALSE; /* 16-bit message is always ascii */
668 case EM_SETPASSWORDCHAR:
673 charW = (WCHAR)wParam;
677 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
680 EDIT_EM_SetPasswordChar(es, charW);
684 case EM_EMPTYUNDOBUFFER16:
685 case EM_EMPTYUNDOBUFFER:
686 EDIT_EM_EmptyUndoBuffer(es);
689 case EM_GETFIRSTVISIBLELINE16:
690 result = es->y_offset;
692 case EM_GETFIRSTVISIBLELINE:
693 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
696 case EM_SETREADONLY16:
699 SetWindowLongW( hwnd, GWL_STYLE,
700 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
701 es->style |= ES_READONLY;
703 SetWindowLongW( hwnd, GWL_STYLE,
704 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
705 es->style &= ~ES_READONLY;
710 case EM_SETWORDBREAKPROC16:
711 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
713 case EM_SETWORDBREAKPROC:
714 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
717 case EM_GETWORDBREAKPROC16:
718 result = (LRESULT)es->word_break_proc16;
720 case EM_GETWORDBREAKPROC:
721 result = (LRESULT)es->word_break_proc;
724 case EM_GETPASSWORDCHAR16:
725 unicode = FALSE; /* 16-bit message is always ascii */
727 case EM_GETPASSWORDCHAR:
730 result = es->password_char;
733 WCHAR charW = es->password_char;
735 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
741 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
744 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
748 result = MAKELONG(es->left_margin, es->right_margin);
751 case EM_GETLIMITTEXT:
752 result = es->buffer_limit;
756 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
760 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
763 /* End of the EM_ messages which were in numerical order; what order
764 * are these in? vaguely alphabetical?
768 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
770 if (es->style & ES_MULTILINE)
772 result |= DLGC_WANTALLKEYS;
776 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
778 int vk = (int)((LPMSG)lParam)->wParam;
780 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
782 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
783 result |= DLGC_WANTMESSAGE;
794 strng[0] = wParam >> 8;
795 strng[1] = wParam & 0xff;
796 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
797 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
798 EDIT_WM_Char(es, charW);
811 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
814 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
816 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
817 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
820 EDIT_WM_Char(es, charW);
829 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
833 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
842 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
845 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
849 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
850 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
851 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
853 result = EDIT_WM_Create(es, nameW);
854 HeapFree(GetProcessHeap(), 0, nameW);
863 es->bEnableState = (BOOL) wParam;
864 EDIT_UpdateText(es, NULL, TRUE);
868 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
872 result = (LRESULT)es->font;
876 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
879 case WM_GETTEXTLENGTH:
880 if (unicode) result = strlenW(es->text);
881 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
882 NULL, 0, NULL, NULL );
886 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
890 result = EDIT_WM_KeyDown(es, (INT)wParam);
894 result = EDIT_WM_KillFocus(es);
897 case WM_LBUTTONDBLCLK:
898 result = EDIT_WM_LButtonDblClk(es);
902 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
906 result = EDIT_WM_LButtonUp(es);
910 result = EDIT_WM_MButtonDown(es);
914 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
918 EDIT_WM_Paint(es, (HDC)wParam);
926 EDIT_WM_SetFocus(es);
930 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
934 /* FIXME: actually set an internal flag and behave accordingly */
938 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
943 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
946 case WM_STYLECHANGED:
947 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
950 case WM_STYLECHANGING:
951 result = 0; /* See EDIT_WM_StyleChanged */
955 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
963 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
968 int gcWheelDelta = 0;
969 UINT pulScrollLines = 3;
970 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
972 if (wParam & (MK_SHIFT | MK_CONTROL)) {
973 result = DefWindowProcW(hwnd, msg, wParam, lParam);
976 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
977 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
979 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
980 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
981 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
986 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
990 if (es) EDIT_UnlockBuffer(es, FALSE);
992 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
997 /*********************************************************************
999 * EditWndProcW (USER32.@)
1001 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1003 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1006 /*********************************************************************
1008 * EditWndProc (USER32.@)
1010 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1012 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1015 /*********************************************************************
1017 * EDIT_BuildLineDefs_ML
1019 * Build linked list of text lines.
1020 * Lines can end with '\0' (last line), a character (if it is wrapped),
1021 * a soft return '\r\r\n' or a hard return '\r\n'
1024 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1028 LPWSTR current_position, cp;
1030 LINEDEF *current_line;
1031 LINEDEF *previous_line;
1032 LINEDEF *start_line;
1033 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1034 INT line_count = es->line_count;
1035 INT orig_net_length;
1038 if (istart == iend && delta == 0)
1041 dc = GetDC(es->hwndSelf);
1043 old_font = SelectObject(dc, es->font);
1045 previous_line = NULL;
1046 current_line = es->first_line_def;
1048 /* Find starting line. istart must lie inside an existing line or
1049 * at the end of buffer */
1051 if (istart < current_line->index + current_line->length ||
1052 current_line->ending == END_0)
1055 previous_line = current_line;
1056 current_line = current_line->next;
1058 } while (current_line);
1060 if (!current_line) /* Error occurred start is not inside previous buffer */
1062 FIXME(" modification occurred outside buffer\n");
1063 ReleaseDC(es->hwndSelf, dc);
1067 /* Remember start of modifications in order to calculate update region */
1068 nstart_line = line_index;
1069 nstart_index = current_line->index;
1071 /* We must start to reformat from the previous line since the modifications
1072 * may have caused the line to wrap upwards. */
1073 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1076 current_line = previous_line;
1078 start_line = current_line;
1080 fw = es->format_rect.right - es->format_rect.left;
1081 current_position = es->text + current_line->index;
1083 if (current_line != start_line)
1085 if (!current_line || current_line->index + delta > current_position - es->text)
1087 /* The buffer has been expanded, create a new line and
1088 insert it into the link list */
1089 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1090 new_line->next = previous_line->next;
1091 previous_line->next = new_line;
1092 current_line = new_line;
1095 else if (current_line->index + delta < current_position - es->text)
1097 /* The previous line merged with this line so we delete this extra entry */
1098 previous_line->next = current_line->next;
1099 HeapFree(GetProcessHeap(), 0, current_line);
1100 current_line = previous_line->next;
1104 else /* current_line->index + delta == current_position */
1106 if (current_position - es->text > iend)
1107 break; /* We reached end of line modifications */
1108 /* else recalulate this line */
1112 current_line->index = current_position - es->text;
1113 orig_net_length = current_line->net_length;
1115 /* Find end of line */
1116 cp = current_position;
1118 if (*cp == '\n') break;
1119 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1124 /* Mark type of line termination */
1126 current_line->ending = END_0;
1127 current_line->net_length = strlenW(current_position);
1128 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1129 current_line->ending = END_SOFT;
1130 current_line->net_length = cp - current_position - 1;
1131 } else if (*cp == '\n') {
1132 current_line->ending = END_RICH;
1133 current_line->net_length = cp - current_position;
1135 current_line->ending = END_HARD;
1136 current_line->net_length = cp - current_position;
1139 /* Calculate line width */
1140 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1141 current_position, current_line->net_length,
1142 es->tabs_count, es->tabs));
1144 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1145 if (!(es->style & ES_AUTOHSCROLL)) {
1146 if (current_line->width > fw) {
1151 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1152 prev + 1, current_line->net_length, WB_RIGHT);
1153 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1154 current_position, next, es->tabs_count, es->tabs));
1155 } while (current_line->width <= fw);
1156 if (!prev) { /* Didn't find a line break so force a break */
1161 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1162 current_position, next, es->tabs_count, es->tabs));
1163 } while (current_line->width <= fw);
1168 /* If the first line we are calculating, wrapped before istart, we must
1169 * adjust istart in order for this to be reflected in the update region. */
1170 if (current_line->index == nstart_index && istart > current_line->index + prev)
1171 istart = current_line->index + prev;
1172 /* else if we are updating the previous line before the first line we
1173 * are re-calculating and it expanded */
1174 else if (current_line == start_line &&
1175 current_line->index != nstart_index && orig_net_length < prev)
1177 /* Line expanded due to an upwards line wrap so we must partially include
1178 * previous line in update region */
1179 nstart_line = line_index;
1180 nstart_index = current_line->index;
1181 istart = current_line->index + orig_net_length;
1184 current_line->net_length = prev;
1185 current_line->ending = END_WRAP;
1186 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1187 current_line->net_length, es->tabs_count, es->tabs));
1189 else if (orig_net_length < current_line->net_length &&
1190 current_line == start_line &&
1191 current_line->index != nstart_index) {
1192 /* The previous line expanded but it's still not as wide as the client rect */
1193 /* The expansion is due to an upwards line wrap so we must partially include
1194 it in the update region */
1195 nstart_line = line_index;
1196 nstart_index = current_line->index;
1197 istart = current_line->index + orig_net_length;
1202 /* Adjust length to include line termination */
1203 switch (current_line->ending) {
1205 current_line->length = current_line->net_length + 3;
1208 current_line->length = current_line->net_length + 1;
1211 current_line->length = current_line->net_length + 2;
1215 current_line->length = current_line->net_length;
1218 es->text_width = max(es->text_width, current_line->width);
1219 current_position += current_line->length;
1220 previous_line = current_line;
1221 current_line = current_line->next;
1223 } while (previous_line->ending != END_0);
1225 /* Finish adjusting line indexes by delta or remove hanging lines */
1226 if (previous_line->ending == END_0)
1228 LINEDEF *pnext = NULL;
1230 previous_line->next = NULL;
1231 while (current_line)
1233 pnext = current_line->next;
1234 HeapFree(GetProcessHeap(), 0, current_line);
1235 current_line = pnext;
1239 else if (delta != 0)
1241 while (current_line)
1243 current_line->index += delta;
1244 current_line = current_line->next;
1248 /* Calculate rest of modification rectangle */
1253 * We calculate two rectangles. One for the first line which may have
1254 * an indent with respect to the format rect. The other is a format-width
1255 * rectangle that spans the rest of the lines that changed or moved.
1257 rc.top = es->format_rect.top + nstart_line * es->line_height -
1258 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1259 rc.bottom = rc.top + es->line_height;
1260 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1261 rc.left = es->format_rect.left;
1263 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1264 es->text + nstart_index, istart - nstart_index,
1265 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1266 rc.right = es->format_rect.right;
1267 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1270 rc.left = es->format_rect.left;
1271 rc.right = es->format_rect.right;
1273 * If lines were added or removed we must re-paint the remainder of the
1274 * lines since the remaining lines were either shifted up or down.
1276 if (line_count < es->line_count) /* We added lines */
1277 rc.bottom = es->line_count * es->line_height;
1278 else if (line_count > es->line_count) /* We removed lines */
1279 rc.bottom = line_count * es->line_height;
1281 rc.bottom = line_index * es->line_height;
1282 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1283 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1284 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1285 DeleteObject(tmphrgn);
1289 SelectObject(dc, old_font);
1291 ReleaseDC(es->hwndSelf, dc);
1294 /*********************************************************************
1296 * EDIT_CalcLineWidth_SL
1299 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1306 text = EDIT_GetPasswordPointer_SL(es);
1308 dc = GetDC(es->hwndSelf);
1310 old_font = SelectObject(dc, es->font);
1312 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1315 SelectObject(dc, old_font);
1316 ReleaseDC(es->hwndSelf, dc);
1318 if (es->style & ES_PASSWORD)
1319 HeapFree(GetProcessHeap(), 0, text);
1321 es->text_width = size.cx;
1324 /*********************************************************************
1326 * EDIT_CallWordBreakProc
1328 * Call appropriate WordBreakProc (internal or external).
1330 * Note: The "start" argument should always be an index referring
1331 * to es->text. The actual wordbreak proc might be
1332 * 16 bit, so we can't always pass any 32 bit LPSTR.
1333 * Hence we assume that es->text is the buffer that holds
1334 * the string under examination (we can decide this for ourselves).
1337 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1341 if (es->word_break_proc16) {
1348 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1349 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1350 segptr = K32WOWGlobalLock16(hglob16);
1351 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1352 args[4] = SELECTOROF(segptr);
1353 args[3] = OFFSETOF(segptr);
1357 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1358 ret = LOWORD(result);
1359 GlobalUnlock16(hglob16);
1360 GlobalFree16(hglob16);
1362 else if (es->word_break_proc)
1366 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1368 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1369 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1370 ret = wbpW(es->text + start, index, count, action);
1374 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1378 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1379 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1380 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1381 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1382 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1383 ret = wbpA(textA, index, countA, action);
1384 HeapFree(GetProcessHeap(), 0, textA);
1388 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1394 /*********************************************************************
1398 * Beware: This is not the function called on EM_CHARFROMPOS
1399 * The position _can_ be outside the formatting / client
1401 * The return value is only the character index
1404 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1410 if (es->style & ES_MULTILINE) {
1411 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1413 LINEDEF *line_def = es->first_line_def;
1415 while ((line > 0) && line_def->next) {
1416 line_index += line_def->length;
1417 line_def = line_def->next;
1420 x += es->x_offset - es->format_rect.left;
1421 if (es->style & ES_RIGHT)
1422 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1423 else if (es->style & ES_CENTER)
1424 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1425 if (x >= line_def->width) {
1427 *after_wrap = (line_def->ending == END_WRAP);
1428 return line_index + line_def->net_length;
1432 *after_wrap = FALSE;
1435 dc = GetDC(es->hwndSelf);
1437 old_font = SelectObject(dc, es->font);
1438 low = line_index + 1;
1439 high = line_index + line_def->net_length + 1;
1440 while (low < high - 1)
1442 INT mid = (low + high) / 2;
1443 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1449 *after_wrap = ((index == line_index + line_def->net_length) &&
1450 (line_def->ending == END_WRAP));
1455 *after_wrap = FALSE;
1456 x -= es->format_rect.left;
1458 return es->x_offset;
1462 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1463 if (es->style & ES_RIGHT)
1465 else if (es->style & ES_CENTER)
1469 text = EDIT_GetPasswordPointer_SL(es);
1470 dc = GetDC(es->hwndSelf);
1472 old_font = SelectObject(dc, es->font);
1476 INT high = es->x_offset;
1477 while (low < high - 1)
1479 INT mid = (low + high) / 2;
1480 GetTextExtentPoint32W( dc, text + mid,
1481 es->x_offset - mid, &size );
1482 if (size.cx > -x) low = mid;
1489 INT low = es->x_offset;
1490 INT high = strlenW(es->text) + 1;
1491 while (low < high - 1)
1493 INT mid = (low + high) / 2;
1494 GetTextExtentPoint32W( dc, text + es->x_offset,
1495 mid - es->x_offset, &size );
1496 if (size.cx > x) high = mid;
1501 if (es->style & ES_PASSWORD)
1502 HeapFree(GetProcessHeap(), 0, text);
1505 SelectObject(dc, old_font);
1506 ReleaseDC(es->hwndSelf, dc);
1511 /*********************************************************************
1515 * adjusts the point to be within the formatting rectangle
1516 * (so CharFromPos returns the nearest _visible_ character)
1519 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1521 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1522 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1526 /*********************************************************************
1530 * Calculates the bounding rectangle for a line from a starting
1531 * column to an ending column.
1534 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1536 INT line_index = EDIT_EM_LineIndex(es, line);
1538 if (es->style & ES_MULTILINE)
1539 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1541 rc->top = es->format_rect.top;
1542 rc->bottom = rc->top + es->line_height;
1543 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1544 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1548 /*********************************************************************
1550 * EDIT_GetPasswordPointer_SL
1552 * note: caller should free the (optionally) allocated buffer
1555 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1557 if (es->style & ES_PASSWORD) {
1558 INT len = strlenW(es->text);
1559 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1561 while(len) text[--len] = es->password_char;
1568 /*********************************************************************
1572 * This acts as a LOCAL_Lock(), but it locks only once. This way
1573 * you can call it whenever you like, without unlocking.
1575 * Initially the edit control allocates a HLOCAL32 buffer
1576 * (32 bit linear memory handler). However, 16 bit application
1577 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1578 * handler). From that moment on we have to keep using this 16 bit memory
1579 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1580 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1584 static void EDIT_LockBuffer(EDITSTATE *es)
1586 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1590 BOOL _16bit = FALSE;
1596 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1597 textA = LocalLock(es->hloc32A);
1598 countA = strlen(textA) + 1;
1602 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1603 textA = LOCAL_Lock(hInstance, es->hloc16);
1604 countA = strlen(textA) + 1;
1609 ERR("no buffer ... please report\n");
1616 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1617 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1618 if(countW_new > es->buffer_size + 1)
1620 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1621 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1622 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1625 es->hloc32W = hloc32W_new;
1626 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1627 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1630 WARN("FAILED! Will synchronize partially\n");
1634 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1635 es->text = LocalLock(es->hloc32W);
1639 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1641 LOCAL_Unlock(hInstance, es->hloc16);
1643 LocalUnlock(es->hloc32A);
1650 /*********************************************************************
1652 * EDIT_SL_InvalidateText
1654 * Called from EDIT_InvalidateText().
1655 * Does the job for single-line controls only.
1658 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1663 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1664 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1665 EDIT_UpdateText(es, &rc, TRUE);
1669 /*********************************************************************
1671 * EDIT_ML_InvalidateText
1673 * Called from EDIT_InvalidateText().
1674 * Does the job for multi-line controls only.
1677 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1679 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1680 INT sl = EDIT_EM_LineFromChar(es, start);
1681 INT el = EDIT_EM_LineFromChar(es, end);
1690 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1693 sc = start - EDIT_EM_LineIndex(es, sl);
1694 ec = end - EDIT_EM_LineIndex(es, el);
1695 if (sl < es->y_offset) {
1699 if (el > es->y_offset + vlc) {
1700 el = es->y_offset + vlc;
1701 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1703 GetClientRect(es->hwndSelf, &rc1);
1704 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1706 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1707 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1708 EDIT_UpdateText(es, &rcUpdate, TRUE);
1710 EDIT_GetLineRect(es, sl, sc,
1711 EDIT_EM_LineLength(es,
1712 EDIT_EM_LineIndex(es, sl)),
1714 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1715 EDIT_UpdateText(es, &rcUpdate, TRUE);
1716 for (l = sl + 1 ; l < el ; l++) {
1717 EDIT_GetLineRect(es, l, 0,
1718 EDIT_EM_LineLength(es,
1719 EDIT_EM_LineIndex(es, l)),
1721 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1722 EDIT_UpdateText(es, &rcUpdate, TRUE);
1724 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1725 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1726 EDIT_UpdateText(es, &rcUpdate, TRUE);
1731 /*********************************************************************
1733 * EDIT_InvalidateText
1735 * Invalidate the text from offset start upto, but not including,
1736 * offset end. Useful for (re)painting the selection.
1737 * Regions outside the linewidth are not invalidated.
1738 * end == -1 means end == TextLength.
1739 * start and end need not be ordered.
1742 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1748 end = strlenW(es->text);
1756 if (es->style & ES_MULTILINE)
1757 EDIT_ML_InvalidateText(es, start, end);
1759 EDIT_SL_InvalidateText(es, start, end);
1763 /*********************************************************************
1767 * Try to fit size + 1 characters in the buffer.
1769 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1773 if (size <= es->buffer_size)
1776 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1778 /* Force edit to unlock it's buffer. es->text now NULL */
1779 EDIT_UnlockBuffer(es, TRUE);
1782 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1783 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1784 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1785 es->hloc32W = hNew32W;
1786 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1790 EDIT_LockBuffer(es);
1792 if (es->buffer_size < size) {
1793 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1794 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1797 TRACE("We now have %d+1\n", es->buffer_size);
1803 /*********************************************************************
1807 * Try to fit size + 1 bytes in the undo buffer.
1810 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1814 if (size <= es->undo_buffer_size)
1817 TRACE("trying to ReAlloc to %d+1\n", size);
1819 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1820 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1821 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1826 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1832 /*********************************************************************
1837 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1839 INT e = es->selection_end;
1843 if ((es->style & ES_MULTILINE) && e &&
1844 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1846 if (e && (es->text[e - 1] == '\r'))
1850 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1851 EDIT_EM_ScrollCaret(es);
1855 /*********************************************************************
1859 * Only for multi line controls
1860 * Move the caret one line down, on a column with the nearest
1861 * x coordinate on the screen (might be a different column).
1864 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1866 INT s = es->selection_start;
1867 INT e = es->selection_end;
1868 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1869 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1870 INT x = (short)LOWORD(pos);
1871 INT y = (short)HIWORD(pos);
1873 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1876 EDIT_EM_SetSel(es, s, e, after_wrap);
1877 EDIT_EM_ScrollCaret(es);
1881 /*********************************************************************
1886 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1888 BOOL after_wrap = FALSE;
1891 /* Pass a high value in x to make sure of receiving the end of the line */
1892 if (es->style & ES_MULTILINE)
1893 e = EDIT_CharFromPos(es, 0x3fffffff,
1894 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1896 e = strlenW(es->text);
1897 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1898 EDIT_EM_ScrollCaret(es);
1902 /*********************************************************************
1907 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1909 INT e = es->selection_end;
1913 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1914 if (es->text[e] == '\n')
1916 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1920 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1921 EDIT_EM_ScrollCaret(es);
1925 /*********************************************************************
1929 * Home key: move to beginning of line.
1932 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1936 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1937 if (es->style & ES_MULTILINE)
1938 e = EDIT_CharFromPos(es, -es->x_offset,
1939 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1942 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1943 EDIT_EM_ScrollCaret(es);
1947 /*********************************************************************
1949 * EDIT_MovePageDown_ML
1951 * Only for multi line controls
1952 * Move the caret one page down, on a column with the nearest
1953 * x coordinate on the screen (might be a different column).
1956 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1958 INT s = es->selection_start;
1959 INT e = es->selection_end;
1960 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1961 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1962 INT x = (short)LOWORD(pos);
1963 INT y = (short)HIWORD(pos);
1965 e = EDIT_CharFromPos(es, x,
1966 y + (es->format_rect.bottom - es->format_rect.top),
1970 EDIT_EM_SetSel(es, s, e, after_wrap);
1971 EDIT_EM_ScrollCaret(es);
1975 /*********************************************************************
1977 * EDIT_MovePageUp_ML
1979 * Only for multi line controls
1980 * Move the caret one page up, on a column with the nearest
1981 * x coordinate on the screen (might be a different column).
1984 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1986 INT s = es->selection_start;
1987 INT e = es->selection_end;
1988 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1989 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1990 INT x = (short)LOWORD(pos);
1991 INT y = (short)HIWORD(pos);
1993 e = EDIT_CharFromPos(es, x,
1994 y - (es->format_rect.bottom - es->format_rect.top),
1998 EDIT_EM_SetSel(es, s, e, after_wrap);
1999 EDIT_EM_ScrollCaret(es);
2003 /*********************************************************************
2007 * Only for multi line controls
2008 * Move the caret one line up, on a column with the nearest
2009 * x coordinate on the screen (might be a different column).
2012 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2014 INT s = es->selection_start;
2015 INT e = es->selection_end;
2016 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2017 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2018 INT x = (short)LOWORD(pos);
2019 INT y = (short)HIWORD(pos);
2021 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2024 EDIT_EM_SetSel(es, s, e, after_wrap);
2025 EDIT_EM_ScrollCaret(es);
2029 /*********************************************************************
2031 * EDIT_MoveWordBackward
2034 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2036 INT s = es->selection_start;
2037 INT e = es->selection_end;
2042 l = EDIT_EM_LineFromChar(es, e);
2043 ll = EDIT_EM_LineLength(es, e);
2044 li = EDIT_EM_LineIndex(es, l);
2047 li = EDIT_EM_LineIndex(es, l - 1);
2048 e = li + EDIT_EM_LineLength(es, li);
2051 e = li + (INT)EDIT_CallWordBreakProc(es,
2052 li, e - li, ll, WB_LEFT);
2056 EDIT_EM_SetSel(es, s, e, FALSE);
2057 EDIT_EM_ScrollCaret(es);
2061 /*********************************************************************
2063 * EDIT_MoveWordForward
2066 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2068 INT s = es->selection_start;
2069 INT e = es->selection_end;
2074 l = EDIT_EM_LineFromChar(es, e);
2075 ll = EDIT_EM_LineLength(es, e);
2076 li = EDIT_EM_LineIndex(es, l);
2078 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2079 e = EDIT_EM_LineIndex(es, l + 1);
2081 e = li + EDIT_CallWordBreakProc(es,
2082 li, e - li + 1, ll, WB_RIGHT);
2086 EDIT_EM_SetSel(es, s, e, FALSE);
2087 EDIT_EM_ScrollCaret(es);
2091 /*********************************************************************
2096 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2098 INT s = es->selection_start;
2099 INT e = es->selection_end;
2106 if (es->style & ES_MULTILINE) {
2107 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2108 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2113 TRACE("line=%d\n", line);
2115 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2116 x = (short)LOWORD(pos);
2117 y = (short)HIWORD(pos);
2118 li = EDIT_EM_LineIndex(es, line);
2119 ll = EDIT_EM_LineLength(es, li);
2120 s = min(es->selection_start, es->selection_end);
2121 e = max(es->selection_start, es->selection_end);
2122 s = min(li + ll, max(li, s));
2123 e = min(li + ll, max(li, e));
2124 if (rev && (s != e) &&
2125 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2126 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2127 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2128 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2130 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2134 /*********************************************************************
2139 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2150 BkMode = GetBkMode(dc);
2151 BkColor = GetBkColor(dc);
2152 TextColor = GetTextColor(dc);
2154 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2155 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2156 SetBkMode( dc, OPAQUE);
2158 li = EDIT_EM_LineIndex(es, line);
2159 if (es->style & ES_MULTILINE) {
2160 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2161 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2163 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2164 TextOutW(dc, x, y, text + li + col, count);
2165 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2167 if (es->style & ES_PASSWORD)
2168 HeapFree(GetProcessHeap(), 0, text);
2171 SetBkColor(dc, BkColor);
2172 SetTextColor(dc, TextColor);
2173 SetBkMode( dc, BkMode);
2179 /*********************************************************************
2184 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2187 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2188 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2189 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2193 /*********************************************************************
2197 * note: this is not (exactly) the handler called on EM_SETRECTNP
2198 * it is also used to set the rect of a single line control
2201 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2206 CopyRect(&es->format_rect, rc);
2207 if (es->style & ES_MULTILINE)
2209 if (es->style & WS_BORDER) {
2210 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2211 es->format_rect.left += bw;
2212 es->format_rect.right -= bw;
2213 es->format_rect.top += bw;
2214 es->format_rect.bottom -= bw;
2219 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2220 if (ExStyle & WS_EX_CLIENTEDGE) {
2221 if (es->line_height + 2 <=
2222 es->format_rect.bottom - es->format_rect.top) {
2223 es->format_rect.top++;
2224 es->format_rect.bottom--;
2226 } else if (es->style & WS_BORDER) {
2227 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2228 es->format_rect.left += bw;
2229 es->format_rect.right -= bw;
2230 if (es->line_height + 2 * bw <=
2231 es->format_rect.bottom - es->format_rect.top) {
2232 es->format_rect.top += bw;
2233 es->format_rect.bottom -= bw;
2237 es->format_rect.left += es->left_margin;
2238 es->format_rect.right -= es->right_margin;
2239 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2240 if (es->style & ES_MULTILINE)
2242 INT fw, vlc, max_x_offset, max_y_offset;
2244 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2245 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2247 /* correct es->x_offset */
2248 fw = es->format_rect.right - es->format_rect.left;
2249 max_x_offset = es->text_width - fw;
2250 if(max_x_offset < 0) max_x_offset = 0;
2251 if(es->x_offset > max_x_offset)
2252 es->x_offset = max_x_offset;
2254 /* correct es->y_offset */
2255 max_y_offset = es->line_count - vlc;
2256 if(max_y_offset < 0) max_y_offset = 0;
2257 if(es->y_offset > max_y_offset)
2258 es->y_offset = max_y_offset;
2260 /* force scroll info update */
2261 EDIT_UpdateScrollInfo(es);
2264 /* Windows doesn't care to fix text placement for SL controls */
2265 es->format_rect.bottom = es->format_rect.top + es->line_height;
2267 /* Always stay within the client area */
2268 GetClientRect(es->hwndSelf, &ClientRect);
2269 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2271 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2272 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2274 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2278 /*********************************************************************
2283 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2285 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2287 /* Edit window might be already destroyed */
2288 if(!IsWindow(es->hwndSelf))
2290 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2294 if (!es->lock_count) {
2295 ERR("lock_count == 0 ... please report\n");
2299 ERR("es->text == 0 ... please report\n");
2303 if (force || (es->lock_count == 1)) {
2306 BOOL _16bit = FALSE;
2308 UINT countW = strlenW(es->text) + 1;
2312 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2313 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2314 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2315 countA = LocalSize(es->hloc32A);
2316 if(countA_new > countA)
2319 UINT alloc_size = ROUND_TO_GROW(countA_new);
2320 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2321 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2324 es->hloc32A = hloc32A_new;
2325 countA = LocalSize(hloc32A_new);
2326 TRACE("Real new size %d bytes\n", countA);
2329 WARN("FAILED! Will synchronize partially\n");
2331 textA = LocalLock(es->hloc32A);
2335 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2336 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2337 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2338 countA = LOCAL_Size(hInstance, es->hloc16);
2339 if(countA_new > countA)
2341 HLOCAL16 hloc16_new;
2342 UINT alloc_size = ROUND_TO_GROW(countA_new);
2343 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2344 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2347 es->hloc16 = hloc16_new;
2348 countA = LOCAL_Size(hInstance, hloc16_new);
2349 TRACE("Real new size %d bytes\n", countA);
2352 WARN("FAILED! Will synchronize partially\n");
2354 textA = LOCAL_Lock(hInstance, es->hloc16);
2360 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2362 LOCAL_Unlock(hInstance, es->hloc16);
2364 LocalUnlock(es->hloc32A);
2367 LocalUnlock(es->hloc32W);
2371 ERR("no buffer ... please report\n");
2379 /*********************************************************************
2381 * EDIT_UpdateScrollInfo
2384 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2386 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2389 si.cbSize = sizeof(SCROLLINFO);
2390 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2392 si.nMax = es->line_count - 1;
2393 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2394 si.nPos = es->y_offset;
2395 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2396 si.nMin, si.nMax, si.nPage, si.nPos);
2397 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2400 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2403 si.cbSize = sizeof(SCROLLINFO);
2404 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2406 si.nMax = es->text_width - 1;
2407 si.nPage = es->format_rect.right - es->format_rect.left;
2408 si.nPos = es->x_offset;
2409 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2410 si.nMin, si.nMax, si.nPage, si.nPos);
2411 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2415 /*********************************************************************
2417 * EDIT_WordBreakProc
2419 * Find the beginning of words.
2420 * Note: unlike the specs for a WordBreakProc, this function only
2421 * allows to be called without linebreaks between s[0] upto
2422 * s[count - 1]. Remember it is only called
2423 * internally, so we can decide this for ourselves.
2426 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2430 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2440 if (s[index] == ' ') {
2441 while (index && (s[index] == ' '))
2444 while (index && (s[index] != ' '))
2446 if (s[index] == ' ')
2450 while (index && (s[index] != ' '))
2452 if (s[index] == ' ')
2462 if (s[index] == ' ')
2463 while ((index < count) && (s[index] == ' ')) index++;
2465 while (s[index] && (s[index] != ' ') && (index < count))
2467 while ((s[index] == ' ') && (index < count)) index++;
2471 case WB_ISDELIMITER:
2472 ret = (s[index] == ' ');
2475 ERR("unknown action code, please report !\n");
2482 /*********************************************************************
2486 * returns line number (not index) in high-order word of result.
2487 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2488 * to Richedit, not to the edit control. Original documentation is valid.
2489 * FIXME: do the specs mean to return -1 if outside client area or
2490 * if outside formatting rectangle ???
2493 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2501 GetClientRect(es->hwndSelf, &rc);
2502 if (!PtInRect(&rc, pt))
2505 index = EDIT_CharFromPos(es, x, y, NULL);
2506 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2510 /*********************************************************************
2514 * Enable or disable soft breaks.
2516 * This means: insert or remove the soft linebreak character (\r\r\n).
2517 * Take care to check if the text still fits the buffer after insertion.
2518 * If not, notify with EN_ERRSPACE.
2521 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2523 es->flags &= ~EF_USE_SOFTBRK;
2525 es->flags |= EF_USE_SOFTBRK;
2526 FIXME("soft break enabled, not implemented\n");
2532 /*********************************************************************
2536 * Hopefully this won't fire back at us.
2537 * We always start with a fixed buffer in the local heap.
2538 * Despite of the documentation says that the local heap is used
2539 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2540 * buffer on the local heap.
2543 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2547 if (!(es->style & ES_MULTILINE))
2551 hLocal = es->hloc32W;
2557 UINT countA, alloc_size;
2558 TRACE("Allocating 32-bit ANSI alias buffer\n");
2559 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2560 alloc_size = ROUND_TO_GROW(countA);
2561 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2563 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2566 textA = LocalLock(es->hloc32A);
2567 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2568 LocalUnlock(es->hloc32A);
2570 hLocal = es->hloc32A;
2573 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2578 /*********************************************************************
2582 * Hopefully this won't fire back at us.
2583 * We always start with a buffer in 32 bit linear memory.
2584 * However, with this message a 16 bit application requests
2585 * a handle of 16 bit local heap memory, where it expects to find
2587 * It's a pitty that from this moment on we have to use this
2588 * local heap, because applications may rely on the handle
2591 * In this function we'll try to switch to local heap.
2593 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2595 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2597 UINT countA, alloc_size;
2599 if (!(es->style & ES_MULTILINE))
2605 if (!LOCAL_HeapSize(hInstance)) {
2606 if (!LocalInit16(hInstance, 0,
2607 GlobalSize16(hInstance))) {
2608 ERR("could not initialize local heap\n");
2611 TRACE("local heap initialized\n");
2614 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2615 alloc_size = ROUND_TO_GROW(countA);
2617 TRACE("Allocating 16-bit ANSI alias buffer\n");
2618 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2619 ERR("could not allocate new 16 bit buffer\n");
2623 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2624 ERR("could not lock new 16 bit buffer\n");
2625 LOCAL_Free(hInstance, es->hloc16);
2630 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2631 LOCAL_Unlock(hInstance, es->hloc16);
2633 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2638 /*********************************************************************
2643 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2646 INT line_len, dst_len;
2649 if (es->style & ES_MULTILINE) {
2650 if (line >= es->line_count)
2654 i = EDIT_EM_LineIndex(es, line);
2656 line_len = EDIT_EM_LineLength(es, i);
2657 dst_len = *(WORD *)dst;
2660 if(dst_len <= line_len)
2662 memcpy(dst, src, dst_len * sizeof(WCHAR));
2665 else /* Append 0 if enough space */
2667 memcpy(dst, src, line_len * sizeof(WCHAR));
2674 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2675 if(!ret) /* Insufficient buffer size */
2677 if(ret < dst_len) /* Append 0 if enough space */
2678 ((LPSTR)dst)[ret] = 0;
2684 /*********************************************************************
2689 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2691 UINT s = es->selection_start;
2692 UINT e = es->selection_end;
2699 return MAKELONG(s, e);
2703 /*********************************************************************
2707 * FIXME: is this right ? (or should it be only VSCROLL)
2708 * (and maybe only for edit controls that really have their
2709 * own scrollbars) (and maybe only for multiline controls ?)
2710 * All in all: very poorly documented
2713 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2715 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2716 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2720 /*********************************************************************
2725 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2730 if (!(es->style & ES_MULTILINE))
2732 if (index > (INT)strlenW(es->text))
2733 return es->line_count - 1;
2735 index = min(es->selection_start, es->selection_end);
2738 line_def = es->first_line_def;
2739 index -= line_def->length;
2740 while ((index >= 0) && line_def->next) {
2742 line_def = line_def->next;
2743 index -= line_def->length;
2749 /*********************************************************************
2754 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2759 if (!(es->style & ES_MULTILINE))
2761 if (line >= es->line_count)
2765 line_def = es->first_line_def;
2767 INT index = es->selection_end - line_def->length;
2768 while ((index >= 0) && line_def->next) {
2769 line_index += line_def->length;
2770 line_def = line_def->next;
2771 index -= line_def->length;
2775 line_index += line_def->length;
2776 line_def = line_def->next;
2784 /*********************************************************************
2789 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2793 if (!(es->style & ES_MULTILINE))
2794 return strlenW(es->text);
2797 /* get the number of remaining non-selected chars of selected lines */
2798 INT32 l; /* line number */
2799 INT32 li; /* index of first char in line */
2801 l = EDIT_EM_LineFromChar(es, es->selection_start);
2802 /* # chars before start of selection area */
2803 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2804 l = EDIT_EM_LineFromChar(es, es->selection_end);
2805 /* # chars after end of selection */
2806 li = EDIT_EM_LineIndex(es, l);
2807 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2810 line_def = es->first_line_def;
2811 index -= line_def->length;
2812 while ((index >= 0) && line_def->next) {
2813 line_def = line_def->next;
2814 index -= line_def->length;
2816 return line_def->net_length;
2820 /*********************************************************************
2824 * NOTE: dx is in average character widths, dy - in lines;
2827 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2829 if (!(es->style & ES_MULTILINE))
2832 dx *= es->char_width;
2833 return EDIT_EM_LineScroll_internal(es, dx, dy);
2836 /*********************************************************************
2838 * EDIT_EM_LineScroll_internal
2840 * Version of EDIT_EM_LineScroll for internal use.
2841 * It doesn't refuse if ES_MULTILINE is set and assumes that
2842 * dx is in pixels, dy - in lines.
2845 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2848 INT x_offset_in_pixels;
2849 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2852 if (es->style & ES_MULTILINE)
2854 x_offset_in_pixels = es->x_offset;
2859 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2862 if (-dx > x_offset_in_pixels)
2863 dx = -x_offset_in_pixels;
2864 if (dx > es->text_width - x_offset_in_pixels)
2865 dx = es->text_width - x_offset_in_pixels;
2866 nyoff = max(0, es->y_offset + dy);
2867 if (nyoff >= es->line_count - lines_per_page)
2868 nyoff = max(0, es->line_count - lines_per_page);
2869 dy = (es->y_offset - nyoff) * es->line_height;
2874 es->y_offset = nyoff;
2875 if(es->style & ES_MULTILINE)
2878 es->x_offset += dx / es->char_width;
2880 GetClientRect(es->hwndSelf, &rc1);
2881 IntersectRect(&rc, &rc1, &es->format_rect);
2882 ScrollWindowEx(es->hwndSelf, -dx, dy,
2883 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2884 /* force scroll info update */
2885 EDIT_UpdateScrollInfo(es);
2887 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2888 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2889 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2890 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2895 /*********************************************************************
2900 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2902 INT len = strlenW(es->text);
2915 index = min(index, len);
2916 dc = GetDC(es->hwndSelf);
2918 old_font = SelectObject(dc, es->font);
2919 if (es->style & ES_MULTILINE) {
2920 l = EDIT_EM_LineFromChar(es, index);
2921 y = (l - es->y_offset) * es->line_height;
2922 li = EDIT_EM_LineIndex(es, l);
2923 if (after_wrap && (li == index) && l) {
2925 line_def = es->first_line_def;
2927 line_def = line_def->next;
2930 if (line_def->ending == END_WRAP) {
2932 y -= es->line_height;
2933 li = EDIT_EM_LineIndex(es, l);
2937 line_def = es->first_line_def;
2938 while (line_def->index != li)
2939 line_def = line_def->next;
2941 ll = line_def->net_length;
2942 lw = line_def->width;
2944 w = es->format_rect.right - es->format_rect.left;
2945 if (es->style & ES_RIGHT)
2947 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
2948 es->tabs_count, es->tabs)) - es->x_offset;
2951 else if (es->style & ES_CENTER)
2953 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2954 es->tabs_count, es->tabs)) - es->x_offset;
2959 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2960 es->tabs_count, es->tabs)) - es->x_offset;
2963 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2964 if (index < es->x_offset) {
2965 GetTextExtentPoint32W(dc, text + index,
2966 es->x_offset - index, &size);
2969 GetTextExtentPoint32W(dc, text + es->x_offset,
2970 index - es->x_offset, &size);
2973 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
2975 w = es->format_rect.right - es->format_rect.left;
2976 if (w > es->text_width)
2978 if (es->style & ES_RIGHT)
2979 x += w - es->text_width;
2980 else if (es->style & ES_CENTER)
2981 x += (w - es->text_width) / 2;
2986 if (es->style & ES_PASSWORD)
2987 HeapFree(GetProcessHeap(), 0, text);
2989 x += es->format_rect.left;
2990 y += es->format_rect.top;
2992 SelectObject(dc, old_font);
2993 ReleaseDC(es->hwndSelf, dc);
2994 return MAKELONG((INT16)x, (INT16)y);
2998 /*********************************************************************
3002 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3005 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3007 UINT strl = strlenW(lpsz_replace);
3008 UINT tl = strlenW(es->text);
3019 TRACE("%s, can_undo %d, send_update %d\n",
3020 debugstr_w(lpsz_replace), can_undo, send_update);
3022 s = es->selection_start;
3023 e = es->selection_end;
3025 if ((s == e) && !strl)
3030 size = tl - (e - s) + strl;
3034 /* Issue the EN_MAXTEXT notification and continue with replacing text
3035 * such that buffer limit is honored. */
3036 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3037 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3038 strl = es->buffer_limit - (tl - (e-s));
3041 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3045 /* there is something to be deleted */
3046 TRACE("deleting stuff.\n");
3048 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3050 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3051 buf[bufl] = 0; /* ensure 0 termination */
3053 strcpyW(es->text + s, es->text + e);
3056 /* there is an insertion */
3057 tl = strlenW(es->text);
3058 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));
3059 for (p = es->text + tl ; p >= es->text + s ; p--)
3061 for (i = 0 , p = es->text + s ; i < strl ; i++)
3062 p[i] = lpsz_replace[i];
3063 if(es->style & ES_UPPERCASE)
3064 CharUpperBuffW(p, strl);
3065 else if(es->style & ES_LOWERCASE)
3066 CharLowerBuffW(p, strl);
3068 if (es->style & ES_MULTILINE)
3070 INT st = min(es->selection_start, es->selection_end);
3071 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3073 hrgn = CreateRectRgn(0, 0, 0, 0);
3074 EDIT_BuildLineDefs_ML(es, st, st + strl,
3075 strl - abs(es->selection_end - es->selection_start), hrgn);
3076 /* if text is too long undo all changes */
3077 if (!(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3079 strcpyW(es->text + e, es->text + e + strl);
3081 for (i = 0 , p = es->text ; i < e - s ; i++)
3083 EDIT_BuildLineDefs_ML(es, s, e,
3084 abs(es->selection_end - es->selection_start) - strl, hrgn);
3087 hrgn = CreateRectRgn(0, 0, 0, 0);
3088 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3092 INT fw = es->format_rect.right - es->format_rect.left;
3093 EDIT_CalcLineWidth_SL(es);
3094 /* remove chars that don't fit */
3095 if (!(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3096 while ((es->text_width > fw) && s + strl >= s) {
3097 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3099 EDIT_CalcLineWidth_SL(es);
3101 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3107 utl = strlenW(es->undo_text);
3108 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3109 /* undo-buffer is extended to the right */
3110 EDIT_MakeUndoFit(es, utl + e - s);
3111 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3112 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3113 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3114 /* undo-buffer is extended to the left */
3115 EDIT_MakeUndoFit(es, utl + e - s);
3116 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3118 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3120 es->undo_position = s;
3122 /* new undo-buffer */
3123 EDIT_MakeUndoFit(es, e - s);
3124 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3125 es->undo_text[e - s] = 0; /* ensure 0 termination */
3126 es->undo_position = s;
3128 /* any deletion makes the old insertion-undo invalid */
3129 es->undo_insert_count = 0;
3131 EDIT_EM_EmptyUndoBuffer(es);
3135 if ((s == es->undo_position) ||
3136 ((es->undo_insert_count) &&
3137 (s == es->undo_position + es->undo_insert_count)))
3139 * insertion is new and at delete position or
3140 * an extension to either left or right
3142 es->undo_insert_count += strl;
3144 /* new insertion undo */
3145 es->undo_position = s;
3146 es->undo_insert_count = strl;
3147 /* new insertion makes old delete-buffer invalid */
3148 *es->undo_text = '\0';
3151 EDIT_EM_EmptyUndoBuffer(es);
3155 HeapFree(GetProcessHeap(), 0, buf);
3159 /* If text has been deleted and we're right or center aligned then scroll rightward */
3160 if (es->style & (ES_RIGHT | ES_CENTER))
3162 INT delta = strl - abs(es->selection_end - es->selection_start);
3164 if (delta < 0 && es->x_offset)
3166 if (abs(delta) > es->x_offset)
3169 es->x_offset += delta;
3173 EDIT_EM_SetSel(es, s, s, FALSE);
3174 es->flags |= EF_MODIFIED;
3175 if (send_update) es->flags |= EF_UPDATE;
3178 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3182 EDIT_UpdateText(es, NULL, TRUE);
3184 EDIT_EM_ScrollCaret(es);
3186 /* force scroll info update */
3187 EDIT_UpdateScrollInfo(es);
3190 if(send_update || (es->flags & EF_UPDATE))
3192 es->flags &= ~EF_UPDATE;
3193 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3198 /*********************************************************************
3203 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3207 if (!(es->style & ES_MULTILINE))
3208 return (LRESULT)FALSE;
3218 if (es->y_offset < es->line_count - 1)
3223 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3226 if (es->y_offset < es->line_count - 1)
3227 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3230 return (LRESULT)FALSE;
3233 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3234 /* check if we are going to move too far */
3235 if(es->y_offset + dy > es->line_count - vlc)
3236 dy = es->line_count - vlc - es->y_offset;
3238 /* Notification is done in EDIT_EM_LineScroll */
3240 EDIT_EM_LineScroll(es, 0, dy);
3242 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3246 /*********************************************************************
3251 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3253 if (es->style & ES_MULTILINE) {
3258 INT cw = es->char_width;
3263 l = EDIT_EM_LineFromChar(es, es->selection_end);
3264 li = EDIT_EM_LineIndex(es, l);
3265 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3266 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3267 if (l >= es->y_offset + vlc)
3268 dy = l - vlc + 1 - es->y_offset;
3269 if (l < es->y_offset)
3270 dy = l - es->y_offset;
3271 ww = es->format_rect.right - es->format_rect.left;
3272 if (x < es->format_rect.left)
3273 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3274 if (x > es->format_rect.right)
3275 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3276 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3278 /* check if we are going to move too far */
3279 if(es->x_offset + dx + ww > es->text_width)
3280 dx = es->text_width - ww - es->x_offset;
3281 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3282 EDIT_EM_LineScroll_internal(es, dx, dy);
3289 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3290 format_width = es->format_rect.right - es->format_rect.left;
3291 if (x < es->format_rect.left) {
3292 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3295 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3296 } while ((x < goal) && es->x_offset);
3297 /* FIXME: use ScrollWindow() somehow to improve performance */
3298 EDIT_UpdateText(es, NULL, TRUE);
3299 } else if (x > es->format_rect.right) {
3301 INT len = strlenW(es->text);
3302 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3305 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3306 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3307 } while ((x > goal) && (x_last > es->format_rect.right));
3308 /* FIXME: use ScrollWindow() somehow to improve performance */
3309 EDIT_UpdateText(es, NULL, TRUE);
3313 if(es->flags & EF_FOCUSED)
3314 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3318 /*********************************************************************
3322 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3325 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3327 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3329 if (!(es->style & ES_MULTILINE))
3333 WARN("called with NULL handle\n");
3337 EDIT_UnlockBuffer(es, TRUE);
3341 LOCAL_Free(hInstance, es->hloc16);
3342 es->hloc16 = (HLOCAL16)NULL;
3349 LocalFree(es->hloc32A);
3361 countA = LocalSize(hloc);
3362 textA = LocalLock(hloc);
3363 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3364 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3366 ERR("Could not allocate new unicode buffer\n");
3369 textW = LocalLock(hloc32W_new);
3370 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3371 LocalUnlock(hloc32W_new);
3375 LocalFree(es->hloc32W);
3377 es->hloc32W = hloc32W_new;
3381 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3383 EDIT_LockBuffer(es);
3385 es->x_offset = es->y_offset = 0;
3386 es->selection_start = es->selection_end = 0;
3387 EDIT_EM_EmptyUndoBuffer(es);
3388 es->flags &= ~EF_MODIFIED;
3389 es->flags &= ~EF_UPDATE;
3390 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3391 EDIT_UpdateText(es, NULL, TRUE);
3392 EDIT_EM_ScrollCaret(es);
3393 /* force scroll info update */
3394 EDIT_UpdateScrollInfo(es);
3398 /*********************************************************************
3402 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3405 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3407 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3413 if (!(es->style & ES_MULTILINE))
3417 WARN("called with NULL handle\n");
3421 EDIT_UnlockBuffer(es, TRUE);
3425 LocalFree(es->hloc32A);
3429 countA = LOCAL_Size(hInstance, hloc);
3430 textA = LOCAL_Lock(hInstance, hloc);
3431 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3432 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3434 ERR("Could not allocate new unicode buffer\n");
3437 textW = LocalLock(hloc32W_new);
3438 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3439 LocalUnlock(hloc32W_new);
3440 LOCAL_Unlock(hInstance, hloc);
3443 LocalFree(es->hloc32W);
3445 es->hloc32W = hloc32W_new;
3448 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3450 EDIT_LockBuffer(es);
3452 es->x_offset = es->y_offset = 0;
3453 es->selection_start = es->selection_end = 0;
3454 EDIT_EM_EmptyUndoBuffer(es);
3455 es->flags &= ~EF_MODIFIED;
3456 es->flags &= ~EF_UPDATE;
3457 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3458 EDIT_UpdateText(es, NULL, TRUE);
3459 EDIT_EM_ScrollCaret(es);
3460 /* force scroll info update */
3461 EDIT_UpdateScrollInfo(es);
3465 /*********************************************************************
3469 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3470 * However, the windows version is not complied to yet in all of edit.c
3472 * Additionally as the wrapper for RichEdit controls we need larger buffers
3473 * at present -1 will represent nolimit
3475 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3477 if (limit == 0xFFFFFFFF)
3478 es->buffer_limit = -1;
3479 else if (es->style & ES_MULTILINE) {
3481 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3483 es->buffer_limit = BUFLIMIT_MULTI;
3486 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3488 es->buffer_limit = BUFLIMIT_SINGLE;
3493 /*********************************************************************
3497 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3498 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3499 * margin according to the textmetrics of the current font.
3501 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3502 * of the char's width as the margin, but this is not how Windows handles this.
3503 * For all other fonts Windows sets the margins to zero.
3506 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3507 INT left, INT right)
3510 INT default_left_margin = 0; /* in pixels */
3511 INT default_right_margin = 0; /* in pixels */
3513 /* Set the default margins depending on the font */
3514 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3515 HDC dc = GetDC(es->hwndSelf);
3516 HFONT old_font = SelectObject(dc, es->font);
3517 GetTextMetricsW(dc, &tm);
3518 /* The default margins are only non zero for TrueType or Vector fonts */
3519 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3520 /* This must be calculated more exactly! But how? */
3521 default_left_margin = tm.tmAveCharWidth / 3;
3522 default_right_margin = tm.tmAveCharWidth / 3;
3524 SelectObject(dc, old_font);
3525 ReleaseDC(es->hwndSelf, dc);
3528 if (action & EC_LEFTMARGIN) {
3529 if (left != EC_USEFONTINFO)
3530 es->left_margin = left;
3532 es->left_margin = default_left_margin;
3535 if (action & EC_RIGHTMARGIN) {
3536 if (right != EC_USEFONTINFO)
3537 es->right_margin = right;
3539 es->right_margin = default_right_margin;
3541 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3545 /*********************************************************************
3547 * EM_SETPASSWORDCHAR
3550 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3554 if (es->style & ES_MULTILINE)
3557 if (es->password_char == c)
3560 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3561 es->password_char = c;
3563 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3564 es->style |= ES_PASSWORD;
3566 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3567 es->style &= ~ES_PASSWORD;
3569 EDIT_UpdateText(es, NULL, TRUE);
3573 /*********************************************************************
3577 * note: unlike the specs say: the order of start and end
3578 * _is_ preserved in Windows. (i.e. start can be > end)
3579 * In other words: this handler is OK
3582 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3584 UINT old_start = es->selection_start;
3585 UINT old_end = es->selection_end;
3586 UINT len = strlenW(es->text);
3588 if (start == (UINT)-1) {
3589 start = es->selection_end;
3590 end = es->selection_end;
3592 start = min(start, len);
3593 end = min(end, len);
3595 es->selection_start = start;
3596 es->selection_end = end;
3598 es->flags |= EF_AFTER_WRAP;
3600 es->flags &= ~EF_AFTER_WRAP;
3601 /* Compute the necessary invalidation region. */
3602 /* Note that we don't need to invalidate regions which have
3603 * "never" been selected, or those which are "still" selected.
3604 * In fact, every time we hit a selection boundary, we can
3605 * *toggle* whether we need to invalidate. Thus we can optimize by
3606 * *sorting* the interval endpoints. Let's assume that we sort them
3608 * start <= end <= old_start <= old_end
3609 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3610 * in 5 comparisons; ie it's impossible to do better than the
3612 ORDER_UINT(end, old_end);
3613 ORDER_UINT(start, old_start);
3614 ORDER_UINT(old_start, old_end);
3615 ORDER_UINT(start, end);
3616 /* Note that at this point 'end' and 'old_start' are not in order, but
3617 * start is definitely the min. and old_end is definitely the max. */
3618 if (end != old_start)
3622 * ORDER_UINT32(end, old_start);
3623 * EDIT_InvalidateText(es, start, end);
3624 * EDIT_InvalidateText(es, old_start, old_end);
3625 * in place of the following if statement.
3626 * (That would complete the optimal five-comparison four-element sort.)
3628 if (old_start > end )
3630 EDIT_InvalidateText(es, start, end);
3631 EDIT_InvalidateText(es, old_start, old_end);
3635 EDIT_InvalidateText(es, start, old_start);
3636 EDIT_InvalidateText(es, end, old_end);
3639 else EDIT_InvalidateText(es, start, old_end);
3643 /*********************************************************************
3648 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3650 if (!(es->style & ES_MULTILINE))
3652 HeapFree(GetProcessHeap(), 0, es->tabs);
3653 es->tabs_count = count;
3657 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3658 memcpy(es->tabs, tabs, count * sizeof(INT));
3664 /*********************************************************************
3669 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3671 if (!(es->style & ES_MULTILINE))
3673 HeapFree(GetProcessHeap(), 0, es->tabs);
3674 es->tabs_count = count;
3679 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3680 for (i = 0 ; i < count ; i++)
3681 es->tabs[i] = *tabs++;
3687 /*********************************************************************
3689 * EM_SETWORDBREAKPROC
3692 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3694 if (es->word_break_proc == wbp)
3697 es->word_break_proc = wbp;
3698 es->word_break_proc16 = NULL;
3700 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3701 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3702 EDIT_UpdateText(es, NULL, TRUE);
3707 /*********************************************************************
3709 * EM_SETWORDBREAKPROC16
3712 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3714 if (es->word_break_proc16 == wbp)
3717 es->word_break_proc = NULL;
3718 es->word_break_proc16 = wbp;
3719 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3720 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3721 EDIT_UpdateText(es, NULL, TRUE);
3726 /*********************************************************************
3731 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3736 /* As per MSDN spec, for a single-line edit control,
3737 the return value is always TRUE */
3738 if( es->style & ES_READONLY )
3739 return !(es->style & ES_MULTILINE);
3741 ulength = strlenW(es->undo_text);
3743 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3745 strcpyW(utext, es->undo_text);
3747 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3748 es->undo_insert_count, debugstr_w(utext));
3750 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3751 EDIT_EM_EmptyUndoBuffer(es);
3752 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3753 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3754 /* send the notification after the selection start and end are set */
3755 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3756 EDIT_EM_ScrollCaret(es);
3757 HeapFree(GetProcessHeap(), 0, utext);
3759 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3760 es->undo_insert_count, debugstr_w(es->undo_text));
3765 /*********************************************************************
3770 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3774 control = GetKeyState(VK_CONTROL) & 0x8000;
3778 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3779 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3782 if (es->style & ES_MULTILINE) {
3783 if (es->style & ES_READONLY) {
3784 EDIT_MoveHome(es, FALSE);
3785 EDIT_MoveDown_ML(es, FALSE);
3787 static const WCHAR cr_lfW[] = {'\r','\n',0};
3788 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3793 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3795 static const WCHAR tabW[] = {'\t',0};
3796 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3800 if (!(es->style & ES_READONLY) && !control) {
3801 if (es->selection_start != es->selection_end)
3804 /* delete character left of caret */
3805 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3806 EDIT_MoveBackward(es, TRUE);
3812 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3815 if (!(es->style & ES_READONLY))
3816 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3819 if (!(es->style & ES_READONLY))
3820 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3824 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3825 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3828 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3832 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3839 /*********************************************************************
3844 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3846 if (code || control)
3866 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3867 EDIT_EM_ScrollCaret(es);
3870 ERR("unknown menu item, please report\n");
3876 /*********************************************************************
3880 * Note: the resource files resource/sysres_??.rc cannot define a
3881 * single popup menu. Hence we use a (dummy) menubar
3882 * containing the single popup menu as its first item.
3884 * FIXME: the message identifiers have been chosen arbitrarily,
3885 * hence we use MF_BYPOSITION.
3886 * We might as well use the "real" values (anybody knows ?)
3887 * The menu definition is in resources/sysres_??.rc.
3888 * Once these are OK, we better use MF_BYCOMMAND here
3889 * (as we do in EDIT_WM_Command()).
3892 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3894 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3895 HMENU popup = GetSubMenu(menu, 0);
3896 UINT start = es->selection_start;
3897 UINT end = es->selection_end;
3899 ORDER_UINT(start, end);
3902 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3904 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3906 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3908 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3910 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3912 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3914 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3919 /*********************************************************************
3924 static void EDIT_WM_Copy(EDITSTATE *es)
3926 INT s = min(es->selection_start, es->selection_end);
3927 INT e = max(es->selection_start, es->selection_end);
3935 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3936 dst = GlobalLock(hdst);
3937 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3938 dst[len] = 0; /* ensure 0 termination */
3939 TRACE("%s\n", debugstr_w(dst));
3941 OpenClipboard(es->hwndSelf);
3943 SetClipboardData(CF_UNICODETEXT, hdst);
3948 /*********************************************************************
3953 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3955 TRACE("%s\n", debugstr_w(name));
3957 * To initialize some final structure members, we call some helper
3958 * functions. However, since the EDITSTATE is not consistent (i.e.
3959 * not fully initialized), we should be very careful which
3960 * functions can be called, and in what order.
3962 EDIT_WM_SetFont(es, 0, FALSE);
3963 EDIT_EM_EmptyUndoBuffer(es);
3965 if (name && *name) {
3966 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3967 /* if we insert text to the editline, the text scrolls out
3968 * of the window, as the caret is placed after the insert
3969 * pos normally; thus we reset es->selection... to 0 and
3972 es->selection_start = es->selection_end = 0;
3973 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3974 * Messages are only to be sent when the USER does something to
3975 * change the contents. So I am removing this EN_CHANGE
3977 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3979 EDIT_EM_ScrollCaret(es);
3981 /* force scroll info update */
3982 EDIT_UpdateScrollInfo(es);
3983 /* The rule seems to return 1 here for success */
3984 /* Power Builder masked edit controls will crash */
3986 /* FIXME: is that in all cases so ? */
3991 /*********************************************************************
3996 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4001 while (LocalUnlock(es->hloc32W)) ;
4002 LocalFree(es->hloc32W);
4005 while (LocalUnlock(es->hloc32A)) ;
4006 LocalFree(es->hloc32A);
4009 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4010 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
4011 LOCAL_Free(hInstance, es->hloc16);
4014 pc = es->first_line_def;
4018 HeapFree(GetProcessHeap(), 0, pc);
4022 SetWindowLongW( es->hwndSelf, 0, 0 );
4023 HeapFree(GetProcessHeap(), 0, es);
4029 /*********************************************************************
4034 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4036 /* we do the proper erase in EDIT_WM_Paint */
4041 /*********************************************************************
4046 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4048 if(!count) return 0;
4052 lstrcpynW(dst, es->text, count);
4053 return strlenW(dst);
4057 LPSTR textA = (LPSTR)dst;
4058 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4059 textA[count - 1] = 0; /* ensure 0 termination */
4060 return strlen(textA);
4064 /*********************************************************************
4069 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4074 if (!(es->style & ES_MULTILINE))
4077 if (!(es->style & ES_AUTOHSCROLL))
4081 fw = es->format_rect.right - es->format_rect.left;
4084 TRACE("SB_LINELEFT\n");
4086 dx = -es->char_width;
4089 TRACE("SB_LINERIGHT\n");
4090 if (es->x_offset < es->text_width)
4091 dx = es->char_width;
4094 TRACE("SB_PAGELEFT\n");
4096 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4099 TRACE("SB_PAGERIGHT\n");
4100 if (es->x_offset < es->text_width)
4101 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4109 TRACE("SB_RIGHT\n");
4110 if (es->x_offset < es->text_width)
4111 dx = es->text_width - es->x_offset;
4114 TRACE("SB_THUMBTRACK %d\n", pos);
4115 es->flags |= EF_HSCROLL_TRACK;
4116 if(es->style & WS_HSCROLL)
4117 dx = pos - es->x_offset;
4122 if(pos < 0 || pos > 100) return 0;
4123 /* Assume default scroll range 0-100 */
4124 fw = es->format_rect.right - es->format_rect.left;
4125 new_x = pos * (es->text_width - fw) / 100;
4126 dx = es->text_width ? (new_x - es->x_offset) : 0;
4129 case SB_THUMBPOSITION:
4130 TRACE("SB_THUMBPOSITION %d\n", pos);
4131 es->flags &= ~EF_HSCROLL_TRACK;
4132 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4133 dx = pos - es->x_offset;
4138 if(pos < 0 || pos > 100) return 0;
4139 /* Assume default scroll range 0-100 */
4140 fw = es->format_rect.right - es->format_rect.left;
4141 new_x = pos * (es->text_width - fw) / 100;
4142 dx = es->text_width ? (new_x - es->x_offset) : 0;
4145 /* force scroll info update */
4146 EDIT_UpdateScrollInfo(es);
4147 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
4151 TRACE("SB_ENDSCROLL\n");
4154 * FIXME : the next two are undocumented !
4155 * Are we doing the right thing ?
4156 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4157 * although it's also a regular control message.
4159 case EM_GETTHUMB: /* this one is used by NT notepad */
4163 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4164 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4167 /* Assume default scroll range 0-100 */
4168 INT fw = es->format_rect.right - es->format_rect.left;
4169 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4171 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4174 case EM_LINESCROLL16:
4175 TRACE("EM_LINESCROLL16\n");
4180 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4186 INT fw = es->format_rect.right - es->format_rect.left;
4187 /* check if we are going to move too far */
4188 if(es->x_offset + dx + fw > es->text_width)
4189 dx = es->text_width - fw - es->x_offset;
4191 EDIT_EM_LineScroll_internal(es, dx, 0);
4197 /*********************************************************************
4202 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4204 HWND hLBox = es->hwndListBox;
4212 hCombo = GetParent(es->hwndSelf);
4216 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4218 if (key == VK_UP || key == VK_DOWN)
4220 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4223 if (msg == WM_KEYDOWN || nEUI)
4224 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4230 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4232 /* make sure ComboLBox pops up */
4233 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4238 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4241 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4243 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4245 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4250 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4256 /*********************************************************************
4260 * Handling of special keys that don't produce a WM_CHAR
4261 * (i.e. non-printable keys) & Backspace & Delete
4264 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4269 if (GetKeyState(VK_MENU) & 0x8000)
4272 shift = GetKeyState(VK_SHIFT) & 0x8000;
4273 control = GetKeyState(VK_CONTROL) & 0x8000;
4278 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4283 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4284 EDIT_MoveUp_ML(es, shift);
4287 EDIT_MoveWordBackward(es, shift);
4289 EDIT_MoveBackward(es, shift);
4292 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4296 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4297 EDIT_MoveDown_ML(es, shift);
4299 EDIT_MoveWordForward(es, shift);
4301 EDIT_MoveForward(es, shift);
4304 EDIT_MoveHome(es, shift);
4307 EDIT_MoveEnd(es, shift);
4310 if (es->style & ES_MULTILINE)
4311 EDIT_MovePageUp_ML(es, shift);
4313 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4316 if (es->style & ES_MULTILINE)
4317 EDIT_MovePageDown_ML(es, shift);
4319 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4322 if (!(es->style & ES_READONLY) && !(shift && control)) {
4323 if (es->selection_start != es->selection_end) {
4330 /* delete character left of caret */
4331 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4332 EDIT_MoveBackward(es, TRUE);
4334 } else if (control) {
4335 /* delete to end of line */
4336 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4337 EDIT_MoveEnd(es, TRUE);
4340 /* delete character right of caret */
4341 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4342 EDIT_MoveForward(es, TRUE);
4350 if (!(es->style & ES_READONLY))
4356 /* If the edit doesn't want the return send a message to the default object */
4357 if(!(es->style & ES_WANTRETURN))
4359 HWND hwndParent = GetParent(es->hwndSelf);
4360 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4361 if (HIWORD(dw) == DC_HASDEFID)
4363 SendMessageW( hwndParent, WM_COMMAND,
4364 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4365 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4374 /*********************************************************************
4379 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4381 es->flags &= ~EF_FOCUSED;
4383 if(!(es->style & ES_NOHIDESEL))
4384 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4385 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4390 /*********************************************************************
4394 * The caret position has been set on the WM_LBUTTONDOWN message
4397 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4400 INT e = es->selection_end;
4405 if (!(es->flags & EF_FOCUSED))
4408 l = EDIT_EM_LineFromChar(es, e);
4409 li = EDIT_EM_LineIndex(es, l);
4410 ll = EDIT_EM_LineLength(es, e);
4411 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4412 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4413 EDIT_EM_SetSel(es, s, e, FALSE);
4414 EDIT_EM_ScrollCaret(es);
4419 /*********************************************************************
4424 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4429 SetFocus(es->hwndSelf);
4430 if (!(es->flags & EF_FOCUSED))
4433 es->bCaptureState = TRUE;
4434 SetCapture(es->hwndSelf);
4435 EDIT_ConfinePoint(es, &x, &y);
4436 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4437 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4438 EDIT_EM_ScrollCaret(es);
4439 es->region_posx = es->region_posy = 0;
4440 SetTimer(es->hwndSelf, 0, 100, NULL);
4445 /*********************************************************************
4450 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4452 if (es->bCaptureState) {
4453 KillTimer(es->hwndSelf, 0);
4454 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4456 es->bCaptureState = FALSE;
4461 /*********************************************************************
4466 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4468 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4473 /*********************************************************************
4478 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4484 if (GetCapture() != es->hwndSelf)
4488 * FIXME: gotta do some scrolling if outside client
4489 * area. Maybe reset the timer ?
4492 EDIT_ConfinePoint(es, &x, &y);
4493 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4494 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4495 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4496 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4497 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4502 /*********************************************************************
4506 * See also EDIT_WM_StyleChanged
4508 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4513 TRACE("Creating %s edit control, style = %08lx\n",
4514 unicode ? "Unicode" : "ANSI", lpcs->style);
4516 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4518 SetWindowLongW( hwnd, 0, (LONG)es );
4521 * Note: since the EDITSTATE has not been fully initialized yet,
4522 * we can't use any API calls that may send
4523 * WM_XXX messages before WM_NCCREATE is completed.
4526 es->is_unicode = unicode;
4527 es->style = lpcs->style;
4529 es->bEnableState = !(es->style & WS_DISABLED);
4531 es->hwndSelf = hwnd;
4532 /* Save parent, which will be notified by EN_* messages */
4533 es->hwndParent = lpcs->hwndParent;
4535 if (es->style & ES_COMBO)
4536 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4538 /* Number overrides lowercase overrides uppercase (at least it
4539 * does in Win95). However I'll bet that ES_NUMBER would be
4540 * invalid under Win 3.1.
4542 if (es->style & ES_NUMBER) {
4543 ; /* do not override the ES_NUMBER */
4544 } else if (es->style & ES_LOWERCASE) {
4545 es->style &= ~ES_UPPERCASE;
4547 if (es->style & ES_MULTILINE) {
4548 es->buffer_limit = BUFLIMIT_MULTI;
4549 if (es->style & WS_VSCROLL)
4550 es->style |= ES_AUTOVSCROLL;
4551 if (es->style & WS_HSCROLL)
4552 es->style |= ES_AUTOHSCROLL;
4553 es->style &= ~ES_PASSWORD;
4554 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4555 /* Confirmed - RIGHT overrides CENTER */
4556 if (es->style & ES_RIGHT)
4557 es->style &= ~ES_CENTER;
4558 es->style &= ~WS_HSCROLL;
4559 es->style &= ~ES_AUTOHSCROLL;
4562 es->buffer_limit = BUFLIMIT_SINGLE;
4563 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4564 es->style &= ~ES_CENTER;
4565 es->style &= ~WS_HSCROLL;
4566 es->style &= ~WS_VSCROLL;
4567 if (es->style & ES_PASSWORD)
4568 es->password_char = '*';
4571 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4572 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4574 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4576 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4578 es->undo_buffer_size = es->buffer_size;
4580 if (es->style & ES_MULTILINE)
4581 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4586 * In Win95 look and feel, the WS_BORDER style is replaced by the
4587 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4588 * control a nonclient area so we don't need to draw the border.
4589 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4590 * a nonclient area and we should handle painting the border ourselves.
4592 * When making modifications please ensure that the code still works
4593 * for edit controls created directly with style 0x50800000, exStyle 0
4594 * (which should have a single pixel border)
4596 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4597 es->style &= ~WS_BORDER;
4598 else if (es->style & WS_BORDER)
4599 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4604 /*********************************************************************
4609 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4620 BOOL rev = es->bEnableState &&
4621 ((es->flags & EF_FOCUSED) ||
4622 (es->style & ES_NOHIDESEL));
4623 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4625 GetClientRect(es->hwndSelf, &rcClient);
4627 /* paint the background */
4628 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
4629 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
4630 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4631 GetClipBox(dc, &rc);
4632 FillRect(dc, &rc, brush);
4634 /* draw the border */
4635 if(es->style & WS_BORDER) {
4637 if(es->style & ES_MULTILINE) {
4638 if(es->style & WS_HSCROLL) rc.bottom++;
4639 if(es->style & WS_VSCROLL) rc.right++;
4641 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4643 IntersectClipRect(dc, es->format_rect.left,
4644 es->format_rect.top,
4645 es->format_rect.right,
4646 es->format_rect.bottom);
4647 if (es->style & ES_MULTILINE) {
4649 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4652 old_font = SelectObject(dc, es->font);
4653 EDIT_NotifyCtlColor(es, dc);
4655 if (!es->bEnableState)
4656 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4657 GetClipBox(dc, &rcRgn);
4658 if (es->style & ES_MULTILINE) {
4659 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4660 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4661 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4662 if (IntersectRect(&rc, &rcRgn, &rcLine))
4663 EDIT_PaintLine(es, dc, i, rev);
4666 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4667 if (IntersectRect(&rc, &rcRgn, &rcLine))
4668 EDIT_PaintLine(es, dc, 0, rev);
4671 SelectObject(dc, old_font);
4674 EndPaint(es->hwndSelf, &ps);
4678 /*********************************************************************
4683 static void EDIT_WM_Paste(EDITSTATE *es)
4688 /* Protect read-only edit control from modification */
4689 if(es->style & ES_READONLY)
4692 OpenClipboard(es->hwndSelf);
4693 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4694 src = (LPWSTR)GlobalLock(hsrc);
4695 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4702 /*********************************************************************
4707 static void EDIT_WM_SetFocus(EDITSTATE *es)
4709 es->flags |= EF_FOCUSED;
4710 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4711 EDIT_SetCaretPos(es, es->selection_end,
4712 es->flags & EF_AFTER_WRAP);
4713 if(!(es->style & ES_NOHIDESEL))
4714 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4715 ShowCaret(es->hwndSelf);
4716 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4720 /*********************************************************************
4724 * With Win95 look the margins are set to default font value unless
4725 * the system font (font == 0) is being set, in which case they are left
4729 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4737 dc = GetDC(es->hwndSelf);
4739 old_font = SelectObject(dc, font);
4740 GetTextMetricsW(dc, &tm);
4741 es->line_height = tm.tmHeight;
4742 es->char_width = tm.tmAveCharWidth;
4744 SelectObject(dc, old_font);
4745 ReleaseDC(es->hwndSelf, dc);
4746 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4747 EC_USEFONTINFO, EC_USEFONTINFO);
4749 /* Force the recalculation of the format rect for each font change */
4750 GetClientRect(es->hwndSelf, &r);
4751 EDIT_SetRectNP(es, &r);
4753 if (es->style & ES_MULTILINE)
4754 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4756 EDIT_CalcLineWidth_SL(es);
4759 EDIT_UpdateText(es, NULL, TRUE);
4760 if (es->flags & EF_FOCUSED) {
4762 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4763 EDIT_SetCaretPos(es, es->selection_end,
4764 es->flags & EF_AFTER_WRAP);
4765 ShowCaret(es->hwndSelf);
4770 /*********************************************************************
4775 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4776 * The modified flag is reset. No notifications are sent.
4778 * For single-line controls, reception of WM_SETTEXT triggers:
4779 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4782 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4784 if (!unicode && text)
4786 LPCSTR textA = (LPCSTR)text;
4787 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4788 LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
4790 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4794 if (es->flags & EF_UPDATE)
4795 /* fixed this bug once; complain if we see it about to happen again. */
4796 ERR("SetSel may generate UPDATE message whose handler may reset "
4799 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4802 TRACE("%s\n", debugstr_w(text));
4803 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4805 HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
4809 static const WCHAR empty_stringW[] = {0};
4811 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4814 es->flags &= ~EF_MODIFIED;
4815 EDIT_EM_SetSel(es, 0, 0, FALSE);
4817 /* Send the notification after the selection start and end have been set
4818 * edit control doesn't send notification on WM_SETTEXT
4819 * if it is multiline, or it is part of combobox
4821 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4823 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4824 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4826 EDIT_EM_ScrollCaret(es);
4827 EDIT_UpdateScrollInfo(es);
4831 /*********************************************************************
4836 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4838 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4840 TRACE("width = %d, height = %d\n", width, height);
4841 SetRect(&rc, 0, 0, width, height);
4842 EDIT_SetRectNP(es, &rc);
4843 EDIT_UpdateText(es, NULL, TRUE);
4848 /*********************************************************************
4852 * This message is sent by SetWindowLong on having changed either the Style
4853 * or the extended style.
4855 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4857 * See also EDIT_WM_NCCreate
4859 * It appears that the Windows version of the edit control allows the style
4860 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4861 * style variable which will generally be different. In this function we
4862 * update the internal style based on what changed in the externally visible
4865 * Much of this content as based upon the MSDN, especially:
4866 * Platform SDK Documentation -> User Interface Services ->
4867 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4868 * Edit Control Styles
4870 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4872 if (GWL_STYLE == which) {
4873 DWORD style_change_mask;
4875 /* Only a subset of changes can be applied after the control
4878 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4880 if (es->style & ES_MULTILINE)
4881 style_change_mask |= ES_WANTRETURN;
4883 new_style = style->styleNew & style_change_mask;
4885 /* Number overrides lowercase overrides uppercase (at least it
4886 * does in Win95). However I'll bet that ES_NUMBER would be
4887 * invalid under Win 3.1.
4889 if (new_style & ES_NUMBER) {
4890 ; /* do not override the ES_NUMBER */
4891 } else if (new_style & ES_LOWERCASE) {
4892 new_style &= ~ES_UPPERCASE;
4895 es->style = (es->style & ~style_change_mask) | new_style;
4896 } else if (GWL_EXSTYLE == which) {
4897 ; /* FIXME - what is needed here */
4899 WARN ("Invalid style change %d\n",which);
4905 /*********************************************************************
4910 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4912 if ((key == VK_BACK) && (key_data & 0x2000)) {
4913 if (EDIT_EM_CanUndo(es))
4916 } else if (key == VK_UP || key == VK_DOWN) {
4917 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4920 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4924 /*********************************************************************
4929 static void EDIT_WM_Timer(EDITSTATE *es)
4931 if (es->region_posx < 0) {
4932 EDIT_MoveBackward(es, TRUE);
4933 } else if (es->region_posx > 0) {
4934 EDIT_MoveForward(es, TRUE);
4937 * FIXME: gotta do some vertical scrolling here, like
4938 * EDIT_EM_LineScroll(hwnd, 0, 1);
4942 /*********************************************************************
4947 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4951 if (!(es->style & ES_MULTILINE))
4954 if (!(es->style & ES_AUTOVSCROLL))
4963 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4964 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4965 (action == SB_PAGEUP ? "SB_PAGEUP" :
4967 EDIT_EM_Scroll(es, action);
4974 TRACE("SB_BOTTOM\n");
4975 dy = es->line_count - 1 - es->y_offset;
4978 TRACE("SB_THUMBTRACK %d\n", pos);
4979 es->flags |= EF_VSCROLL_TRACK;
4980 if(es->style & WS_VSCROLL)
4981 dy = pos - es->y_offset;
4984 /* Assume default scroll range 0-100 */
4987 if(pos < 0 || pos > 100) return 0;
4988 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4989 new_y = pos * (es->line_count - vlc) / 100;
4990 dy = es->line_count ? (new_y - es->y_offset) : 0;
4991 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4992 es->line_count, es->y_offset, pos, dy);
4995 case SB_THUMBPOSITION:
4996 TRACE("SB_THUMBPOSITION %d\n", pos);
4997 es->flags &= ~EF_VSCROLL_TRACK;
4998 if(es->style & WS_VSCROLL)
4999 dy = pos - es->y_offset;
5002 /* Assume default scroll range 0-100 */
5005 if(pos < 0 || pos > 100) return 0;
5006 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5007 new_y = pos * (es->line_count - vlc) / 100;
5008 dy = es->line_count ? (new_y - es->y_offset) : 0;
5009 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5010 es->line_count, es->y_offset, pos, dy);
5014 /* force scroll info update */
5015 EDIT_UpdateScrollInfo(es);
5016 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
5020 TRACE("SB_ENDSCROLL\n");
5023 * FIXME : the next two are undocumented !
5024 * Are we doing the right thing ?
5025 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5026 * although it's also a regular control message.
5028 case EM_GETTHUMB: /* this one is used by NT notepad */
5032 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5033 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5036 /* Assume default scroll range 0-100 */
5037 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5038 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5040 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5043 case EM_LINESCROLL16:
5044 TRACE("EM_LINESCROLL16 %d\n", pos);
5049 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5054 EDIT_EM_LineScroll(es, 0, dy);
5058 /*********************************************************************
5063 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5065 if (es->flags & EF_UPDATE) {
5066 es->flags &= ~EF_UPDATE;
5067 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5069 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5073 /*********************************************************************
5078 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5080 if (es->flags & EF_UPDATE) {
5081 es->flags &= ~EF_UPDATE;
5082 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5084 InvalidateRect(es->hwndSelf, rc, bErase);