4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
56 #include "wine/winbase16.h"
57 #include "wine/winuser16.h"
58 #include "wine/unicode.h"
60 #include "user_private.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(edit);
64 WINE_DECLARE_DEBUG_CHANNEL(combo);
65 WINE_DECLARE_DEBUG_CHANNEL(relay);
67 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
68 FIXME: BTW, new specs say 65535 (do you dare ???) */
69 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
70 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
71 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
72 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
75 * extra flags for EDITSTATE.flags field
77 #define EF_MODIFIED 0x0001 /* text has been modified */
78 #define EF_FOCUSED 0x0002 /* we have input focus */
79 #define EF_UPDATE 0x0004 /* notify parent of changed state */
80 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
82 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
83 wrapped line, instead of in front of the next character */
84 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT buffer_size; /* the size of the buffer in characters */
109 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
110 HFONT font; /* NULL means standard system font */
111 INT x_offset; /* scroll offset for multi lines this is in pixels
112 for single lines it's in characters */
113 INT line_height; /* height of a screen line in pixels */
114 INT char_width; /* average character width in pixels */
115 DWORD style; /* sane version of wnd->dwStyle */
116 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
117 INT undo_insert_count; /* number of characters inserted in sequence */
118 UINT undo_position; /* character index of the insertion and deletion */
119 LPWSTR undo_text; /* deleted text */
120 UINT undo_buffer_size; /* size of the deleted text buffer */
121 INT selection_start; /* == selection_end if no selection */
122 INT selection_end; /* == current caret position */
123 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
124 INT left_margin; /* in pixels */
125 INT right_margin; /* in pixels */
127 INT text_width; /* width of the widest line in pixels for multi line controls
128 and just line width for single line controls */
129 INT region_posx; /* Position of cursor relative to region: */
130 INT region_posy; /* -1: to left, 0: within, 1: to right */
131 EDITWORDBREAKPROC16 word_break_proc16;
132 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
133 INT line_count; /* number of lines */
134 INT y_offset; /* scroll offset in number of lines */
135 BOOL bCaptureState; /* flag indicating whether mouse was captured */
136 BOOL bEnableState; /* flag keeping the enable state */
137 HWND hwndSelf; /* the our window handle */
138 HWND hwndParent; /* Handle of parent for sending EN_* messages.
139 Even if parent will change, EN_* messages
140 should be sent to the first parent. */
141 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
143 * only for multi line controls
145 INT lock_count; /* amount of re-entries in the EditWndProc */
148 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
149 HLOCAL hloc32W; /* our unicode local memory block */
150 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
152 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
157 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
158 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
160 /* used for disabled or read-only edit control */
161 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
163 { /* Notify parent which has created this edit control */ \
164 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
165 SendMessageW(es->hwndParent, WM_COMMAND, \
166 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
167 (LPARAM)(es->hwndSelf)); \
170 /*********************************************************************
177 * These functions have trivial implementations
178 * We still like to call them internally
179 * "static inline" makes them more like macro's
181 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
182 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
183 static inline void EDIT_WM_Clear(EDITSTATE *es);
184 static inline void EDIT_WM_Cut(EDITSTATE *es);
187 * Helper functions only valid for one type of control
189 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
190 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
191 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
192 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
193 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
194 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
195 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
197 * Helper functions valid for both single line _and_ multi line controls
199 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
200 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
201 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
202 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
203 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
204 static void EDIT_LockBuffer(EDITSTATE *es);
205 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
206 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
207 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
208 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
209 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
210 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
211 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
212 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
213 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
214 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
215 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
216 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
217 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
218 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
219 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
221 * EM_XXX message handlers
223 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
224 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
225 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
226 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
227 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
228 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
229 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
230 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
231 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
232 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
233 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
234 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
235 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
236 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
237 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
238 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
239 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
240 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
241 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
242 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
243 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
244 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
245 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
246 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
247 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
248 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
249 static BOOL EDIT_EM_Undo(EDITSTATE *es);
251 * WM_XXX message handlers
253 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
254 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
255 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
256 static void EDIT_WM_Copy(EDITSTATE *es);
257 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
258 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
259 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
260 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
261 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
262 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
263 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
264 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
265 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
266 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
267 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
268 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
269 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
270 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
271 static void EDIT_WM_Paste(EDITSTATE *es);
272 static void EDIT_WM_SetFocus(EDITSTATE *es);
273 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
274 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
275 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
276 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
277 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
278 static void EDIT_WM_Timer(EDITSTATE *es);
279 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
280 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
281 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
283 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
284 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
286 /*********************************************************************
287 * edit class descriptor
289 const struct builtin_class_descr EDIT_builtin_class =
292 CS_DBLCLKS | CS_PARENTDC, /* style */
293 EditWndProcA, /* procA */
294 EditWndProcW, /* procW */
295 sizeof(EDITSTATE *), /* extra */
296 IDC_IBEAM, /* cursor */
301 /*********************************************************************
306 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
308 return (es->undo_insert_count || strlenW(es->undo_text));
312 /*********************************************************************
317 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
319 es->undo_insert_count = 0;
320 *es->undo_text = '\0';
324 /*********************************************************************
329 static inline void EDIT_WM_Clear(EDITSTATE *es)
331 static const WCHAR empty_stringW[] = {0};
333 /* Protect read-only edit control from modification */
334 if(es->style & ES_READONLY)
337 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
341 /*********************************************************************
346 static inline void EDIT_WM_Cut(EDITSTATE *es)
353 /**********************************************************************
356 * Returns the window version in case Wine emulates a later version
357 * of windows than the application expects.
359 * In a number of cases when windows runs an application that was
360 * designed for an earlier windows version, windows reverts
361 * to "old" behaviour of that earlier version.
363 * An example is a disabled edit control that needs to be painted.
364 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
365 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
366 * applications with an expected version 0f 4.0 or higher.
369 static DWORD get_app_version(void)
371 static DWORD version;
374 DWORD dwEmulatedVersion;
376 DWORD dwProcVersion = GetProcessVersion(0);
378 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
379 GetVersionExW( &info );
380 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
381 /* FIXME: this may not be 100% correct; see discussion on the
382 * wine developer list in Nov 1999 */
383 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
389 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
393 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
394 msg = WM_CTLCOLORSTATIC;
396 msg = WM_CTLCOLOREDIT;
398 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
399 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
402 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
405 return DefWindowProcW(hwnd, msg, wParam, lParam);
407 return DefWindowProcA(hwnd, msg, wParam, lParam);
410 /*********************************************************************
414 * The messages are in the order of the actual integer values
415 * (which can be found in include/windows.h)
416 * Wherever possible the 16 bit versions are converted to
417 * the 32 bit ones, so that we can 'fall through' to the
418 * helper functions. These are mostly 32 bit (with a few
419 * exceptions, clearly indicated by a '16' extension to their
423 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
424 WPARAM wParam, LPARAM lParam, BOOL unicode )
426 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
429 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
431 if (!es && msg != WM_NCCREATE)
432 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
434 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
442 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
446 if ((short)LOWORD(lParam) == -1)
447 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
449 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
451 EDIT_EM_ScrollCaret(es);
455 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
456 EDIT_EM_ScrollCaret(es);
463 RECT16 *r16 = MapSL(lParam);
464 r16->left = es->format_rect.left;
465 r16->top = es->format_rect.top;
466 r16->right = es->format_rect.right;
467 r16->bottom = es->format_rect.bottom;
472 CopyRect((LPRECT)lParam, &es->format_rect);
476 if ((es->style & ES_MULTILINE) && lParam) {
478 RECT16 *r16 = MapSL(lParam);
481 rc.right = r16->right;
482 rc.bottom = r16->bottom;
483 EDIT_SetRectNP(es, &rc);
484 EDIT_UpdateText(es, NULL, TRUE);
488 if ((es->style & ES_MULTILINE) && lParam) {
489 EDIT_SetRectNP(es, (LPRECT)lParam);
490 EDIT_UpdateText(es, NULL, TRUE);
495 if ((es->style & ES_MULTILINE) && lParam) {
497 RECT16 *r16 = MapSL(lParam);
500 rc.right = r16->right;
501 rc.bottom = r16->bottom;
502 EDIT_SetRectNP(es, &rc);
506 if ((es->style & ES_MULTILINE) && lParam)
507 EDIT_SetRectNP(es, (LPRECT)lParam);
512 result = EDIT_EM_Scroll(es, (INT)wParam);
515 case EM_LINESCROLL16:
516 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
517 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
520 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
523 case EM_SCROLLCARET16:
525 EDIT_EM_ScrollCaret(es);
531 result = ((es->flags & EF_MODIFIED) != 0);
537 es->flags |= EF_MODIFIED;
539 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
542 case EM_GETLINECOUNT16:
543 case EM_GETLINECOUNT:
544 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
548 if ((INT16)wParam == -1)
552 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
556 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
559 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
563 result = (LRESULT)EDIT_EM_GetHandle16(es);
566 result = (LRESULT)EDIT_EM_GetHandle(es);
571 result = EDIT_EM_GetThumb(es);
574 /* these messages missing from specs */
583 FIXME("undocumented message 0x%x, please report\n", msg);
584 result = DefWindowProcW(hwnd, msg, wParam, lParam);
587 case EM_LINELENGTH16:
589 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
592 case EM_REPLACESEL16:
593 lParam = (LPARAM)MapSL(lParam);
594 unicode = FALSE; /* 16-bit message is always ascii */
601 textW = (LPWSTR)lParam;
604 LPSTR textA = (LPSTR)lParam;
605 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
606 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
607 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
610 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
614 HeapFree(GetProcessHeap(), 0, textW);
619 lParam = (LPARAM)MapSL(lParam);
620 unicode = FALSE; /* 16-bit message is always ascii */
623 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
627 case EM_SETLIMITTEXT:
628 EDIT_EM_SetLimitText(es, (INT)wParam);
633 result = (LRESULT)EDIT_EM_CanUndo(es);
639 result = (LRESULT)EDIT_EM_Undo(es);
644 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
647 case EM_LINEFROMCHAR16:
648 case EM_LINEFROMCHAR:
649 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
652 case EM_SETTABSTOPS16:
653 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
656 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
659 case EM_SETPASSWORDCHAR16:
660 unicode = FALSE; /* 16-bit message is always ascii */
662 case EM_SETPASSWORDCHAR:
667 charW = (WCHAR)wParam;
671 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
674 EDIT_EM_SetPasswordChar(es, charW);
678 case EM_EMPTYUNDOBUFFER16:
679 case EM_EMPTYUNDOBUFFER:
680 EDIT_EM_EmptyUndoBuffer(es);
683 case EM_GETFIRSTVISIBLELINE16:
684 result = es->y_offset;
686 case EM_GETFIRSTVISIBLELINE:
687 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
690 case EM_SETREADONLY16:
693 SetWindowLongW( hwnd, GWL_STYLE,
694 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
695 es->style |= ES_READONLY;
697 SetWindowLongW( hwnd, GWL_STYLE,
698 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
699 es->style &= ~ES_READONLY;
704 case EM_SETWORDBREAKPROC16:
705 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
707 case EM_SETWORDBREAKPROC:
708 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
711 case EM_GETWORDBREAKPROC16:
712 result = (LRESULT)es->word_break_proc16;
714 case EM_GETWORDBREAKPROC:
715 result = (LRESULT)es->word_break_proc;
718 case EM_GETPASSWORDCHAR16:
719 unicode = FALSE; /* 16-bit message is always ascii */
721 case EM_GETPASSWORDCHAR:
724 result = es->password_char;
727 WCHAR charW = es->password_char;
729 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
735 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
738 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
742 result = MAKELONG(es->left_margin, es->right_margin);
745 case EM_GETLIMITTEXT:
746 result = es->buffer_limit;
750 result = strlenW(es->text);
751 if ((INT)wParam >= result) result = -1;
752 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
756 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
759 /* End of the EM_ messages which were in numerical order; what order
760 * are these in? vaguely alphabetical?
764 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
768 result = EDIT_WM_Destroy(es);
773 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
775 if (es->style & ES_MULTILINE)
777 result |= DLGC_WANTALLKEYS;
781 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
783 int vk = (int)((LPMSG)lParam)->wParam;
785 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
787 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
788 result |= DLGC_WANTMESSAGE;
799 strng[0] = wParam >> 8;
800 strng[1] = wParam & 0xff;
801 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
802 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
803 EDIT_WM_Char(es, charW);
816 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
819 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
821 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
822 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
825 EDIT_WM_Char(es, charW);
834 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
838 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
847 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
850 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
854 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
855 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
856 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
858 result = EDIT_WM_Create(es, nameW);
859 HeapFree(GetProcessHeap(), 0, nameW);
868 es->bEnableState = (BOOL) wParam;
869 EDIT_UpdateText(es, NULL, TRUE);
873 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
877 result = (LRESULT)es->font;
881 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
884 case WM_GETTEXTLENGTH:
885 if (unicode) result = strlenW(es->text);
886 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
887 NULL, 0, NULL, NULL );
891 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
895 result = EDIT_WM_KeyDown(es, (INT)wParam);
899 result = EDIT_WM_KillFocus(es);
902 case WM_LBUTTONDBLCLK:
903 result = EDIT_WM_LButtonDblClk(es);
907 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
911 result = EDIT_WM_LButtonUp(es);
915 result = EDIT_WM_MButtonDown(es);
919 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
923 EDIT_WM_Paint(es, (HDC)wParam);
931 EDIT_WM_SetFocus(es);
935 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
939 /* FIXME: actually set an internal flag and behave accordingly */
943 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
948 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
951 case WM_STYLECHANGED:
952 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
955 case WM_STYLECHANGING:
956 result = 0; /* See EDIT_WM_StyleChanged */
960 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
968 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
973 int gcWheelDelta = 0;
974 UINT pulScrollLines = 3;
975 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
977 if (wParam & (MK_SHIFT | MK_CONTROL)) {
978 result = DefWindowProcW(hwnd, msg, wParam, lParam);
981 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
982 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
984 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
985 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
986 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
991 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
995 if (es) EDIT_UnlockBuffer(es, FALSE);
997 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1002 /*********************************************************************
1004 * EditWndProcW (USER32.@)
1006 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1008 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1011 /*********************************************************************
1013 * EditWndProc (USER32.@)
1015 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1017 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1020 /*********************************************************************
1022 * EDIT_BuildLineDefs_ML
1024 * Build linked list of text lines.
1025 * Lines can end with '\0' (last line), a character (if it is wrapped),
1026 * a soft return '\r\r\n' or a hard return '\r\n'
1029 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1033 LPWSTR current_position, cp;
1035 LINEDEF *current_line;
1036 LINEDEF *previous_line;
1037 LINEDEF *start_line;
1038 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1039 INT line_count = es->line_count;
1040 INT orig_net_length;
1043 if (istart == iend && delta == 0)
1046 dc = GetDC(es->hwndSelf);
1048 old_font = SelectObject(dc, es->font);
1050 previous_line = NULL;
1051 current_line = es->first_line_def;
1053 /* Find starting line. istart must lie inside an existing line or
1054 * at the end of buffer */
1056 if (istart < current_line->index + current_line->length ||
1057 current_line->ending == END_0)
1060 previous_line = current_line;
1061 current_line = current_line->next;
1063 } while (current_line);
1065 if (!current_line) /* Error occurred start is not inside previous buffer */
1067 FIXME(" modification occurred outside buffer\n");
1068 ReleaseDC(es->hwndSelf, dc);
1072 /* Remember start of modifications in order to calculate update region */
1073 nstart_line = line_index;
1074 nstart_index = current_line->index;
1076 /* We must start to reformat from the previous line since the modifications
1077 * may have caused the line to wrap upwards. */
1078 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1081 current_line = previous_line;
1083 start_line = current_line;
1085 fw = es->format_rect.right - es->format_rect.left;
1086 current_position = es->text + current_line->index;
1088 if (current_line != start_line)
1090 if (!current_line || current_line->index + delta > current_position - es->text)
1092 /* The buffer has been expanded, create a new line and
1093 insert it into the link list */
1094 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1095 new_line->next = previous_line->next;
1096 previous_line->next = new_line;
1097 current_line = new_line;
1100 else if (current_line->index + delta < current_position - es->text)
1102 /* The previous line merged with this line so we delete this extra entry */
1103 previous_line->next = current_line->next;
1104 HeapFree(GetProcessHeap(), 0, current_line);
1105 current_line = previous_line->next;
1109 else /* current_line->index + delta == current_position */
1111 if (current_position - es->text > iend)
1112 break; /* We reached end of line modifications */
1113 /* else recalulate this line */
1117 current_line->index = current_position - es->text;
1118 orig_net_length = current_line->net_length;
1120 /* Find end of line */
1121 cp = current_position;
1123 if (*cp == '\n') break;
1124 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1129 /* Mark type of line termination */
1131 current_line->ending = END_0;
1132 current_line->net_length = strlenW(current_position);
1133 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1134 current_line->ending = END_SOFT;
1135 current_line->net_length = cp - current_position - 1;
1136 } else if (*cp == '\n') {
1137 current_line->ending = END_RICH;
1138 current_line->net_length = cp - current_position;
1140 current_line->ending = END_HARD;
1141 current_line->net_length = cp - current_position;
1144 /* Calculate line width */
1145 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1146 current_position, current_line->net_length,
1147 es->tabs_count, es->tabs));
1149 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1150 if (!(es->style & ES_AUTOHSCROLL)) {
1151 if (current_line->width > fw) {
1156 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1157 prev + 1, current_line->net_length, WB_RIGHT);
1158 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1159 current_position, next, es->tabs_count, es->tabs));
1160 } while (current_line->width <= fw);
1161 if (!prev) { /* Didn't find a line break so force a break */
1166 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1167 current_position, next, es->tabs_count, es->tabs));
1168 } while (current_line->width <= fw);
1173 /* If the first line we are calculating, wrapped before istart, we must
1174 * adjust istart in order for this to be reflected in the update region. */
1175 if (current_line->index == nstart_index && istart > current_line->index + prev)
1176 istart = current_line->index + prev;
1177 /* else if we are updating the previous line before the first line we
1178 * are re-calculating and it expanded */
1179 else if (current_line == start_line &&
1180 current_line->index != nstart_index && orig_net_length < prev)
1182 /* Line expanded due to an upwards line wrap so we must partially include
1183 * previous line in update region */
1184 nstart_line = line_index;
1185 nstart_index = current_line->index;
1186 istart = current_line->index + orig_net_length;
1189 current_line->net_length = prev;
1190 current_line->ending = END_WRAP;
1191 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1192 current_line->net_length, es->tabs_count, es->tabs));
1194 else if (orig_net_length < current_line->net_length &&
1195 current_line == start_line &&
1196 current_line->index != nstart_index) {
1197 /* The previous line expanded but it's still not as wide as the client rect */
1198 /* The expansion is due to an upwards line wrap so we must partially include
1199 it in the update region */
1200 nstart_line = line_index;
1201 nstart_index = current_line->index;
1202 istart = current_line->index + orig_net_length;
1207 /* Adjust length to include line termination */
1208 switch (current_line->ending) {
1210 current_line->length = current_line->net_length + 3;
1213 current_line->length = current_line->net_length + 1;
1216 current_line->length = current_line->net_length + 2;
1220 current_line->length = current_line->net_length;
1223 es->text_width = max(es->text_width, current_line->width);
1224 current_position += current_line->length;
1225 previous_line = current_line;
1226 current_line = current_line->next;
1228 } while (previous_line->ending != END_0);
1230 /* Finish adjusting line indexes by delta or remove hanging lines */
1231 if (previous_line->ending == END_0)
1233 LINEDEF *pnext = NULL;
1235 previous_line->next = NULL;
1236 while (current_line)
1238 pnext = current_line->next;
1239 HeapFree(GetProcessHeap(), 0, current_line);
1240 current_line = pnext;
1244 else if (delta != 0)
1246 while (current_line)
1248 current_line->index += delta;
1249 current_line = current_line->next;
1253 /* Calculate rest of modification rectangle */
1258 * We calculate two rectangles. One for the first line which may have
1259 * an indent with respect to the format rect. The other is a format-width
1260 * rectangle that spans the rest of the lines that changed or moved.
1262 rc.top = es->format_rect.top + nstart_line * es->line_height -
1263 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1264 rc.bottom = rc.top + es->line_height;
1265 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1266 rc.left = es->format_rect.left;
1268 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1269 es->text + nstart_index, istart - nstart_index,
1270 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1271 rc.right = es->format_rect.right;
1272 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1275 rc.left = es->format_rect.left;
1276 rc.right = es->format_rect.right;
1278 * If lines were added or removed we must re-paint the remainder of the
1279 * lines since the remaining lines were either shifted up or down.
1281 if (line_count < es->line_count) /* We added lines */
1282 rc.bottom = es->line_count * es->line_height;
1283 else if (line_count > es->line_count) /* We removed lines */
1284 rc.bottom = line_count * es->line_height;
1286 rc.bottom = line_index * es->line_height;
1287 rc.bottom += es->format_rect.top;
1288 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1289 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1290 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1291 DeleteObject(tmphrgn);
1295 SelectObject(dc, old_font);
1297 ReleaseDC(es->hwndSelf, dc);
1300 /*********************************************************************
1302 * EDIT_CalcLineWidth_SL
1305 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1312 text = EDIT_GetPasswordPointer_SL(es);
1314 dc = GetDC(es->hwndSelf);
1316 old_font = SelectObject(dc, es->font);
1318 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1321 SelectObject(dc, old_font);
1322 ReleaseDC(es->hwndSelf, dc);
1324 if (es->style & ES_PASSWORD)
1325 HeapFree(GetProcessHeap(), 0, text);
1327 es->text_width = size.cx;
1330 /*********************************************************************
1332 * EDIT_CallWordBreakProc
1334 * Call appropriate WordBreakProc (internal or external).
1336 * Note: The "start" argument should always be an index referring
1337 * to es->text. The actual wordbreak proc might be
1338 * 16 bit, so we can't always pass any 32 bit LPSTR.
1339 * Hence we assume that es->text is the buffer that holds
1340 * the string under examination (we can decide this for ourselves).
1343 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1347 if (es->word_break_proc16) {
1354 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1355 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1356 segptr = K32WOWGlobalLock16(hglob16);
1357 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1358 args[4] = SELECTOROF(segptr);
1359 args[3] = OFFSETOF(segptr);
1363 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1364 ret = LOWORD(result);
1365 GlobalUnlock16(hglob16);
1366 GlobalFree16(hglob16);
1368 else if (es->word_break_proc)
1372 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1374 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1375 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1376 ret = wbpW(es->text + start, index, count, action);
1380 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1384 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1385 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1386 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1387 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1388 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1389 ret = wbpA(textA, index, countA, action);
1390 HeapFree(GetProcessHeap(), 0, textA);
1394 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1400 /*********************************************************************
1404 * Beware: This is not the function called on EM_CHARFROMPOS
1405 * The position _can_ be outside the formatting / client
1407 * The return value is only the character index
1410 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1415 INT x_high = 0, x_low = 0;
1417 if (es->style & ES_MULTILINE) {
1418 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1420 LINEDEF *line_def = es->first_line_def;
1422 while ((line > 0) && line_def->next) {
1423 line_index += line_def->length;
1424 line_def = line_def->next;
1427 x += es->x_offset - es->format_rect.left;
1428 if (es->style & ES_RIGHT)
1429 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1430 else if (es->style & ES_CENTER)
1431 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1432 if (x >= line_def->width) {
1434 *after_wrap = (line_def->ending == END_WRAP);
1435 return line_index + line_def->net_length;
1439 *after_wrap = FALSE;
1442 dc = GetDC(es->hwndSelf);
1444 old_font = SelectObject(dc, es->font);
1446 high = line_index + line_def->net_length + 1;
1447 while (low < high - 1)
1449 INT mid = (low + high) / 2;
1450 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1459 if (abs(x_high - x) <= abs(x_low - x) + 1)
1465 *after_wrap = ((index == line_index + line_def->net_length) &&
1466 (line_def->ending == END_WRAP));
1471 *after_wrap = FALSE;
1472 x -= es->format_rect.left;
1474 return es->x_offset;
1478 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1479 if (es->style & ES_RIGHT)
1481 else if (es->style & ES_CENTER)
1485 text = EDIT_GetPasswordPointer_SL(es);
1486 dc = GetDC(es->hwndSelf);
1488 old_font = SelectObject(dc, es->font);
1492 INT high = es->x_offset;
1493 while (low < high - 1)
1495 INT mid = (low + high) / 2;
1496 GetTextExtentPoint32W( dc, text + mid,
1497 es->x_offset - mid, &size );
1506 if (abs(x_high + x) <= abs(x_low + x) + 1)
1513 INT low = es->x_offset;
1514 INT high = strlenW(es->text) + 1;
1515 while (low < high - 1)
1517 INT mid = (low + high) / 2;
1518 GetTextExtentPoint32W( dc, text + es->x_offset,
1519 mid - es->x_offset, &size );
1528 if (abs(x_high - x) <= abs(x_low - x) + 1)
1533 if (es->style & ES_PASSWORD)
1534 HeapFree(GetProcessHeap(), 0, text);
1537 SelectObject(dc, old_font);
1538 ReleaseDC(es->hwndSelf, dc);
1543 /*********************************************************************
1547 * adjusts the point to be within the formatting rectangle
1548 * (so CharFromPos returns the nearest _visible_ character)
1551 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1553 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1554 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1558 /*********************************************************************
1562 * Calculates the bounding rectangle for a line from a starting
1563 * column to an ending column.
1566 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1568 INT line_index = EDIT_EM_LineIndex(es, line);
1570 if (es->style & ES_MULTILINE)
1571 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1573 rc->top = es->format_rect.top;
1574 rc->bottom = rc->top + es->line_height;
1575 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1576 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1580 /*********************************************************************
1582 * EDIT_GetPasswordPointer_SL
1584 * note: caller should free the (optionally) allocated buffer
1587 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1589 if (es->style & ES_PASSWORD) {
1590 INT len = strlenW(es->text);
1591 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1593 while(len) text[--len] = es->password_char;
1600 /*********************************************************************
1604 * This acts as a LocalLock16(), but it locks only once. This way
1605 * you can call it whenever you like, without unlocking.
1607 * Initially the edit control allocates a HLOCAL32 buffer
1608 * (32 bit linear memory handler). However, 16 bit application
1609 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1610 * handler). From that moment on we have to keep using this 16 bit memory
1611 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1612 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1616 static void EDIT_LockBuffer(EDITSTATE *es)
1618 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1619 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1620 HANDLE16 oldDS = stack16->ds;
1625 BOOL _16bit = FALSE;
1631 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1632 textA = LocalLock(es->hloc32A);
1633 countA = strlen(textA) + 1;
1637 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1638 stack16->ds = hInstance;
1639 textA = MapSL(LocalLock16(es->hloc16));
1640 stack16->ds = oldDS;
1641 countA = strlen(textA) + 1;
1646 ERR("no buffer ... please report\n");
1653 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1654 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1655 if(countW_new > es->buffer_size + 1)
1657 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1658 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1659 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1662 es->hloc32W = hloc32W_new;
1663 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1664 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1667 WARN("FAILED! Will synchronize partially\n");
1671 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1672 es->text = LocalLock(es->hloc32W);
1676 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1679 stack16->ds = hInstance;
1680 LocalUnlock16(es->hloc16);
1681 stack16->ds = oldDS;
1684 LocalUnlock(es->hloc32A);
1691 /*********************************************************************
1693 * EDIT_SL_InvalidateText
1695 * Called from EDIT_InvalidateText().
1696 * Does the job for single-line controls only.
1699 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1704 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1705 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1706 EDIT_UpdateText(es, &rc, TRUE);
1710 /*********************************************************************
1712 * EDIT_ML_InvalidateText
1714 * Called from EDIT_InvalidateText().
1715 * Does the job for multi-line controls only.
1718 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1720 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1721 INT sl = EDIT_EM_LineFromChar(es, start);
1722 INT el = EDIT_EM_LineFromChar(es, end);
1731 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1734 sc = start - EDIT_EM_LineIndex(es, sl);
1735 ec = end - EDIT_EM_LineIndex(es, el);
1736 if (sl < es->y_offset) {
1740 if (el > es->y_offset + vlc) {
1741 el = es->y_offset + vlc;
1742 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1744 GetClientRect(es->hwndSelf, &rc1);
1745 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1747 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1748 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1749 EDIT_UpdateText(es, &rcUpdate, TRUE);
1751 EDIT_GetLineRect(es, sl, sc,
1752 EDIT_EM_LineLength(es,
1753 EDIT_EM_LineIndex(es, sl)),
1755 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1756 EDIT_UpdateText(es, &rcUpdate, TRUE);
1757 for (l = sl + 1 ; l < el ; l++) {
1758 EDIT_GetLineRect(es, l, 0,
1759 EDIT_EM_LineLength(es,
1760 EDIT_EM_LineIndex(es, l)),
1762 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1763 EDIT_UpdateText(es, &rcUpdate, TRUE);
1765 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1766 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1767 EDIT_UpdateText(es, &rcUpdate, TRUE);
1772 /*********************************************************************
1774 * EDIT_InvalidateText
1776 * Invalidate the text from offset start upto, but not including,
1777 * offset end. Useful for (re)painting the selection.
1778 * Regions outside the linewidth are not invalidated.
1779 * end == -1 means end == TextLength.
1780 * start and end need not be ordered.
1783 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1789 end = strlenW(es->text);
1797 if (es->style & ES_MULTILINE)
1798 EDIT_ML_InvalidateText(es, start, end);
1800 EDIT_SL_InvalidateText(es, start, end);
1804 /*********************************************************************
1808 * Try to fit size + 1 characters in the buffer.
1810 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1814 if (size <= es->buffer_size)
1817 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1819 /* Force edit to unlock it's buffer. es->text now NULL */
1820 EDIT_UnlockBuffer(es, TRUE);
1823 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1824 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1825 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1826 es->hloc32W = hNew32W;
1827 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1831 EDIT_LockBuffer(es);
1833 if (es->buffer_size < size) {
1834 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1835 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1838 TRACE("We now have %d+1\n", es->buffer_size);
1844 /*********************************************************************
1848 * Try to fit size + 1 bytes in the undo buffer.
1851 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1855 if (size <= es->undo_buffer_size)
1858 TRACE("trying to ReAlloc to %d+1\n", size);
1860 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1861 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1862 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1867 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1873 /*********************************************************************
1878 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1880 INT e = es->selection_end;
1884 if ((es->style & ES_MULTILINE) && e &&
1885 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1887 if (e && (es->text[e - 1] == '\r'))
1891 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1892 EDIT_EM_ScrollCaret(es);
1896 /*********************************************************************
1900 * Only for multi line controls
1901 * Move the caret one line down, on a column with the nearest
1902 * x coordinate on the screen (might be a different column).
1905 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1907 INT s = es->selection_start;
1908 INT e = es->selection_end;
1909 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1910 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1911 INT x = (short)LOWORD(pos);
1912 INT y = (short)HIWORD(pos);
1914 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1917 EDIT_EM_SetSel(es, s, e, after_wrap);
1918 EDIT_EM_ScrollCaret(es);
1922 /*********************************************************************
1927 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1929 BOOL after_wrap = FALSE;
1932 /* Pass a high value in x to make sure of receiving the end of the line */
1933 if (es->style & ES_MULTILINE)
1934 e = EDIT_CharFromPos(es, 0x3fffffff,
1935 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1937 e = strlenW(es->text);
1938 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1939 EDIT_EM_ScrollCaret(es);
1943 /*********************************************************************
1948 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1950 INT e = es->selection_end;
1954 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1955 if (es->text[e] == '\n')
1957 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1961 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1962 EDIT_EM_ScrollCaret(es);
1966 /*********************************************************************
1970 * Home key: move to beginning of line.
1973 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1977 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1978 if (es->style & ES_MULTILINE)
1979 e = EDIT_CharFromPos(es, -es->x_offset,
1980 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1983 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1984 EDIT_EM_ScrollCaret(es);
1988 /*********************************************************************
1990 * EDIT_MovePageDown_ML
1992 * Only for multi line controls
1993 * Move the caret one page down, on a column with the nearest
1994 * x coordinate on the screen (might be a different column).
1997 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1999 INT s = es->selection_start;
2000 INT e = es->selection_end;
2001 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2002 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2003 INT x = (short)LOWORD(pos);
2004 INT y = (short)HIWORD(pos);
2006 e = EDIT_CharFromPos(es, x,
2007 y + (es->format_rect.bottom - es->format_rect.top),
2011 EDIT_EM_SetSel(es, s, e, after_wrap);
2012 EDIT_EM_ScrollCaret(es);
2016 /*********************************************************************
2018 * EDIT_MovePageUp_ML
2020 * Only for multi line controls
2021 * Move the caret one page up, on a column with the nearest
2022 * x coordinate on the screen (might be a different column).
2025 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2027 INT s = es->selection_start;
2028 INT e = es->selection_end;
2029 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2030 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2031 INT x = (short)LOWORD(pos);
2032 INT y = (short)HIWORD(pos);
2034 e = EDIT_CharFromPos(es, x,
2035 y - (es->format_rect.bottom - es->format_rect.top),
2039 EDIT_EM_SetSel(es, s, e, after_wrap);
2040 EDIT_EM_ScrollCaret(es);
2044 /*********************************************************************
2048 * Only for multi line controls
2049 * Move the caret one line up, on a column with the nearest
2050 * x coordinate on the screen (might be a different column).
2053 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2055 INT s = es->selection_start;
2056 INT e = es->selection_end;
2057 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2058 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2059 INT x = (short)LOWORD(pos);
2060 INT y = (short)HIWORD(pos);
2062 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2065 EDIT_EM_SetSel(es, s, e, after_wrap);
2066 EDIT_EM_ScrollCaret(es);
2070 /*********************************************************************
2072 * EDIT_MoveWordBackward
2075 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2077 INT s = es->selection_start;
2078 INT e = es->selection_end;
2083 l = EDIT_EM_LineFromChar(es, e);
2084 ll = EDIT_EM_LineLength(es, e);
2085 li = EDIT_EM_LineIndex(es, l);
2088 li = EDIT_EM_LineIndex(es, l - 1);
2089 e = li + EDIT_EM_LineLength(es, li);
2092 e = li + (INT)EDIT_CallWordBreakProc(es,
2093 li, e - li, ll, WB_LEFT);
2097 EDIT_EM_SetSel(es, s, e, FALSE);
2098 EDIT_EM_ScrollCaret(es);
2102 /*********************************************************************
2104 * EDIT_MoveWordForward
2107 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2109 INT s = es->selection_start;
2110 INT e = es->selection_end;
2115 l = EDIT_EM_LineFromChar(es, e);
2116 ll = EDIT_EM_LineLength(es, e);
2117 li = EDIT_EM_LineIndex(es, l);
2119 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2120 e = EDIT_EM_LineIndex(es, l + 1);
2122 e = li + EDIT_CallWordBreakProc(es,
2123 li, e - li + 1, ll, WB_RIGHT);
2127 EDIT_EM_SetSel(es, s, e, FALSE);
2128 EDIT_EM_ScrollCaret(es);
2132 /*********************************************************************
2137 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2139 INT s = es->selection_start;
2140 INT e = es->selection_end;
2147 if (es->style & ES_MULTILINE) {
2148 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2149 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2154 TRACE("line=%d\n", line);
2156 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2157 x = (short)LOWORD(pos);
2158 y = (short)HIWORD(pos);
2159 li = EDIT_EM_LineIndex(es, line);
2160 ll = EDIT_EM_LineLength(es, li);
2161 s = min(es->selection_start, es->selection_end);
2162 e = max(es->selection_start, es->selection_end);
2163 s = min(li + ll, max(li, s));
2164 e = min(li + ll, max(li, e));
2165 if (rev && (s != e) &&
2166 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2167 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2168 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2169 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2171 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2175 /*********************************************************************
2180 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2191 BkMode = GetBkMode(dc);
2192 BkColor = GetBkColor(dc);
2193 TextColor = GetTextColor(dc);
2195 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2196 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2197 SetBkMode( dc, OPAQUE);
2199 li = EDIT_EM_LineIndex(es, line);
2200 if (es->style & ES_MULTILINE) {
2201 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2202 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2204 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2205 TextOutW(dc, x, y, text + li + col, count);
2206 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2208 if (es->style & ES_PASSWORD)
2209 HeapFree(GetProcessHeap(), 0, text);
2212 SetBkColor(dc, BkColor);
2213 SetTextColor(dc, TextColor);
2214 SetBkMode( dc, BkMode);
2220 /*********************************************************************
2225 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2228 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2229 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2230 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2234 /*********************************************************************
2238 * note: this is not (exactly) the handler called on EM_SETRECTNP
2239 * it is also used to set the rect of a single line control
2242 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2247 CopyRect(&es->format_rect, rc);
2248 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2249 if ((es->style & WS_POPUP) && !(ExStyle & WS_EX_CLIENTEDGE)) {
2250 if (es->style & WS_BORDER) {
2251 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2252 es->format_rect.left += bw;
2253 es->format_rect.right -= bw;
2254 if (es->line_height + 2 * bw <=
2255 es->format_rect.bottom - es->format_rect.top) {
2256 es->format_rect.top += bw;
2257 es->format_rect.bottom -= bw;
2261 if (es->line_height + 2 <=
2262 es->format_rect.bottom - es->format_rect.top) {
2263 es->format_rect.top++;
2264 es->format_rect.bottom--;
2266 es->format_rect.left++;
2267 es->format_rect.right--;
2269 es->format_rect.left += es->left_margin;
2270 es->format_rect.right -= es->right_margin;
2271 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2272 if (es->style & ES_MULTILINE)
2274 INT fw, vlc, max_x_offset, max_y_offset;
2276 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2277 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2279 /* correct es->x_offset */
2280 fw = es->format_rect.right - es->format_rect.left;
2281 max_x_offset = es->text_width - fw;
2282 if(max_x_offset < 0) max_x_offset = 0;
2283 if(es->x_offset > max_x_offset)
2284 es->x_offset = max_x_offset;
2286 /* correct es->y_offset */
2287 max_y_offset = es->line_count - vlc;
2288 if(max_y_offset < 0) max_y_offset = 0;
2289 if(es->y_offset > max_y_offset)
2290 es->y_offset = max_y_offset;
2292 /* force scroll info update */
2293 EDIT_UpdateScrollInfo(es);
2296 /* Windows doesn't care to fix text placement for SL controls */
2297 es->format_rect.bottom = es->format_rect.top + es->line_height;
2299 /* Always stay within the client area */
2300 GetClientRect(es->hwndSelf, &ClientRect);
2301 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2303 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2304 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2306 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2310 /*********************************************************************
2315 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2318 /* Edit window might be already destroyed */
2319 if(!IsWindow(es->hwndSelf))
2321 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2325 if (!es->lock_count) {
2326 ERR("lock_count == 0 ... please report\n");
2330 ERR("es->text == 0 ... please report\n");
2334 if (force || (es->lock_count == 1)) {
2338 UINT countW = strlenW(es->text) + 1;
2339 STACK16FRAME* stack16 = NULL;
2344 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2345 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2346 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2347 countA = LocalSize(es->hloc32A);
2348 if(countA_new > countA)
2351 UINT alloc_size = ROUND_TO_GROW(countA_new);
2352 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2353 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2356 es->hloc32A = hloc32A_new;
2357 countA = LocalSize(hloc32A_new);
2358 TRACE("Real new size %d bytes\n", countA);
2361 WARN("FAILED! Will synchronize partially\n");
2363 textA = LocalLock(es->hloc32A);
2367 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2369 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2370 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2372 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2373 oldDS = stack16->ds;
2374 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2376 countA = LocalSize16(es->hloc16);
2377 if(countA_new > countA)
2379 HLOCAL16 hloc16_new;
2380 UINT alloc_size = ROUND_TO_GROW(countA_new);
2381 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2382 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2385 es->hloc16 = hloc16_new;
2386 countA = LocalSize16(hloc16_new);
2387 TRACE("Real new size %d bytes\n", countA);
2390 WARN("FAILED! Will synchronize partially\n");
2392 textA = MapSL(LocalLock16(es->hloc16));
2397 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2399 LocalUnlock16(es->hloc16);
2401 LocalUnlock(es->hloc32A);
2404 if (stack16) stack16->ds = oldDS;
2405 LocalUnlock(es->hloc32W);
2409 ERR("no buffer ... please report\n");
2417 /*********************************************************************
2419 * EDIT_UpdateScrollInfo
2422 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2424 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2427 si.cbSize = sizeof(SCROLLINFO);
2428 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2430 si.nMax = es->line_count - 1;
2431 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2432 si.nPos = es->y_offset;
2433 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2434 si.nMin, si.nMax, si.nPage, si.nPos);
2435 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2438 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2441 si.cbSize = sizeof(SCROLLINFO);
2442 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2444 si.nMax = es->text_width - 1;
2445 si.nPage = es->format_rect.right - es->format_rect.left;
2446 si.nPos = es->x_offset;
2447 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2448 si.nMin, si.nMax, si.nPage, si.nPos);
2449 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2453 /*********************************************************************
2455 * EDIT_WordBreakProc
2457 * Find the beginning of words.
2458 * Note: unlike the specs for a WordBreakProc, this function only
2459 * allows to be called without linebreaks between s[0] upto
2460 * s[count - 1]. Remember it is only called
2461 * internally, so we can decide this for ourselves.
2464 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2468 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2478 if (s[index] == ' ') {
2479 while (index && (s[index] == ' '))
2482 while (index && (s[index] != ' '))
2484 if (s[index] == ' ')
2488 while (index && (s[index] != ' '))
2490 if (s[index] == ' ')
2500 if (s[index] == ' ')
2501 while ((index < count) && (s[index] == ' ')) index++;
2503 while (s[index] && (s[index] != ' ') && (index < count))
2505 while ((s[index] == ' ') && (index < count)) index++;
2509 case WB_ISDELIMITER:
2510 ret = (s[index] == ' ');
2513 ERR("unknown action code, please report !\n");
2520 /*********************************************************************
2524 * returns line number (not index) in high-order word of result.
2525 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2526 * to Richedit, not to the edit control. Original documentation is valid.
2527 * FIXME: do the specs mean to return -1 if outside client area or
2528 * if outside formatting rectangle ???
2531 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2539 GetClientRect(es->hwndSelf, &rc);
2540 if (!PtInRect(&rc, pt))
2543 index = EDIT_CharFromPos(es, x, y, NULL);
2544 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2548 /*********************************************************************
2552 * Enable or disable soft breaks.
2554 * This means: insert or remove the soft linebreak character (\r\r\n).
2555 * Take care to check if the text still fits the buffer after insertion.
2556 * If not, notify with EN_ERRSPACE.
2559 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2561 es->flags &= ~EF_USE_SOFTBRK;
2563 es->flags |= EF_USE_SOFTBRK;
2564 FIXME("soft break enabled, not implemented\n");
2570 /*********************************************************************
2574 * Hopefully this won't fire back at us.
2575 * We always start with a fixed buffer in the local heap.
2576 * Despite of the documentation says that the local heap is used
2577 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2578 * buffer on the local heap.
2581 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2585 if (!(es->style & ES_MULTILINE))
2589 hLocal = es->hloc32W;
2595 UINT countA, alloc_size;
2596 TRACE("Allocating 32-bit ANSI alias buffer\n");
2597 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2598 alloc_size = ROUND_TO_GROW(countA);
2599 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2601 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2604 textA = LocalLock(es->hloc32A);
2605 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2606 LocalUnlock(es->hloc32A);
2608 hLocal = es->hloc32A;
2611 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2616 /*********************************************************************
2620 * Hopefully this won't fire back at us.
2621 * We always start with a buffer in 32 bit linear memory.
2622 * However, with this message a 16 bit application requests
2623 * a handle of 16 bit local heap memory, where it expects to find
2625 * It's a pitty that from this moment on we have to use this
2626 * local heap, because applications may rely on the handle
2629 * In this function we'll try to switch to local heap.
2631 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2634 UINT countA, alloc_size;
2635 STACK16FRAME* stack16;
2638 if (!(es->style & ES_MULTILINE))
2644 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2645 oldDS = stack16->ds;
2646 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2648 if (!LocalHeapSize16()) {
2650 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2651 ERR("could not initialize local heap\n");
2654 TRACE("local heap initialized\n");
2657 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2658 alloc_size = ROUND_TO_GROW(countA);
2660 TRACE("Allocating 16-bit ANSI alias buffer\n");
2661 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2662 ERR("could not allocate new 16 bit buffer\n");
2666 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2667 ERR("could not lock new 16 bit buffer\n");
2668 LocalFree16(es->hloc16);
2673 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2674 LocalUnlock16(es->hloc16);
2676 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2679 stack16->ds = oldDS;
2684 /*********************************************************************
2689 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2692 INT line_len, dst_len;
2695 if (es->style & ES_MULTILINE) {
2696 if (line >= es->line_count)
2700 i = EDIT_EM_LineIndex(es, line);
2702 line_len = EDIT_EM_LineLength(es, i);
2703 dst_len = *(WORD *)dst;
2706 if(dst_len <= line_len)
2708 memcpy(dst, src, dst_len * sizeof(WCHAR));
2711 else /* Append 0 if enough space */
2713 memcpy(dst, src, line_len * sizeof(WCHAR));
2720 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2721 if(!ret) /* Insufficient buffer size */
2723 if(ret < dst_len) /* Append 0 if enough space */
2724 ((LPSTR)dst)[ret] = 0;
2730 /*********************************************************************
2735 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2737 UINT s = es->selection_start;
2738 UINT e = es->selection_end;
2745 return MAKELONG(s, e);
2749 /*********************************************************************
2753 * FIXME: is this right ? (or should it be only VSCROLL)
2754 * (and maybe only for edit controls that really have their
2755 * own scrollbars) (and maybe only for multiline controls ?)
2756 * All in all: very poorly documented
2759 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2761 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2762 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2766 /*********************************************************************
2771 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2776 if (!(es->style & ES_MULTILINE))
2778 if (index > (INT)strlenW(es->text))
2779 return es->line_count - 1;
2781 index = min(es->selection_start, es->selection_end);
2784 line_def = es->first_line_def;
2785 index -= line_def->length;
2786 while ((index >= 0) && line_def->next) {
2788 line_def = line_def->next;
2789 index -= line_def->length;
2795 /*********************************************************************
2800 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2805 if (!(es->style & ES_MULTILINE))
2807 if (line >= es->line_count)
2811 line_def = es->first_line_def;
2813 INT index = es->selection_end - line_def->length;
2814 while ((index >= 0) && line_def->next) {
2815 line_index += line_def->length;
2816 line_def = line_def->next;
2817 index -= line_def->length;
2821 line_index += line_def->length;
2822 line_def = line_def->next;
2830 /*********************************************************************
2835 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2839 if (!(es->style & ES_MULTILINE))
2840 return strlenW(es->text);
2843 /* get the number of remaining non-selected chars of selected lines */
2844 INT32 l; /* line number */
2845 INT32 li; /* index of first char in line */
2847 l = EDIT_EM_LineFromChar(es, es->selection_start);
2848 /* # chars before start of selection area */
2849 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2850 l = EDIT_EM_LineFromChar(es, es->selection_end);
2851 /* # chars after end of selection */
2852 li = EDIT_EM_LineIndex(es, l);
2853 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2856 line_def = es->first_line_def;
2857 index -= line_def->length;
2858 while ((index >= 0) && line_def->next) {
2859 line_def = line_def->next;
2860 index -= line_def->length;
2862 return line_def->net_length;
2866 /*********************************************************************
2870 * NOTE: dx is in average character widths, dy - in lines;
2873 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2875 if (!(es->style & ES_MULTILINE))
2878 dx *= es->char_width;
2879 return EDIT_EM_LineScroll_internal(es, dx, dy);
2882 /*********************************************************************
2884 * EDIT_EM_LineScroll_internal
2886 * Version of EDIT_EM_LineScroll for internal use.
2887 * It doesn't refuse if ES_MULTILINE is set and assumes that
2888 * dx is in pixels, dy - in lines.
2891 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2894 INT x_offset_in_pixels;
2895 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2898 if (es->style & ES_MULTILINE)
2900 x_offset_in_pixels = es->x_offset;
2905 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2908 if (-dx > x_offset_in_pixels)
2909 dx = -x_offset_in_pixels;
2910 if (dx > es->text_width - x_offset_in_pixels)
2911 dx = es->text_width - x_offset_in_pixels;
2912 nyoff = max(0, es->y_offset + dy);
2913 if (nyoff >= es->line_count - lines_per_page)
2914 nyoff = max(0, es->line_count - lines_per_page);
2915 dy = (es->y_offset - nyoff) * es->line_height;
2920 es->y_offset = nyoff;
2921 if(es->style & ES_MULTILINE)
2924 es->x_offset += dx / es->char_width;
2926 GetClientRect(es->hwndSelf, &rc1);
2927 IntersectRect(&rc, &rc1, &es->format_rect);
2928 ScrollWindowEx(es->hwndSelf, -dx, dy,
2929 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2930 /* force scroll info update */
2931 EDIT_UpdateScrollInfo(es);
2933 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2934 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2935 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2936 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2941 /*********************************************************************
2946 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2948 INT len = strlenW(es->text);
2961 index = min(index, len);
2962 dc = GetDC(es->hwndSelf);
2964 old_font = SelectObject(dc, es->font);
2965 if (es->style & ES_MULTILINE) {
2966 l = EDIT_EM_LineFromChar(es, index);
2967 y = (l - es->y_offset) * es->line_height;
2968 li = EDIT_EM_LineIndex(es, l);
2969 if (after_wrap && (li == index) && l) {
2971 line_def = es->first_line_def;
2973 line_def = line_def->next;
2976 if (line_def->ending == END_WRAP) {
2978 y -= es->line_height;
2979 li = EDIT_EM_LineIndex(es, l);
2983 line_def = es->first_line_def;
2984 while (line_def->index != li)
2985 line_def = line_def->next;
2987 ll = line_def->net_length;
2988 lw = line_def->width;
2990 w = es->format_rect.right - es->format_rect.left;
2991 if (es->style & ES_RIGHT)
2993 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
2994 es->tabs_count, es->tabs)) - es->x_offset;
2997 else if (es->style & ES_CENTER)
2999 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3000 es->tabs_count, es->tabs)) - es->x_offset;
3005 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3006 es->tabs_count, es->tabs)) - es->x_offset;
3009 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3010 if (index < es->x_offset) {
3011 GetTextExtentPoint32W(dc, text + index,
3012 es->x_offset - index, &size);
3015 GetTextExtentPoint32W(dc, text + es->x_offset,
3016 index - es->x_offset, &size);
3019 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3021 w = es->format_rect.right - es->format_rect.left;
3022 if (w > es->text_width)
3024 if (es->style & ES_RIGHT)
3025 x += w - es->text_width;
3026 else if (es->style & ES_CENTER)
3027 x += (w - es->text_width) / 2;
3032 if (es->style & ES_PASSWORD)
3033 HeapFree(GetProcessHeap(), 0, text);
3035 x += es->format_rect.left;
3036 y += es->format_rect.top;
3038 SelectObject(dc, old_font);
3039 ReleaseDC(es->hwndSelf, dc);
3040 return MAKELONG((INT16)x, (INT16)y);
3044 /*********************************************************************
3048 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3051 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3053 UINT strl = strlenW(lpsz_replace);
3054 UINT tl = strlenW(es->text);
3065 TRACE("%s, can_undo %d, send_update %d\n",
3066 debugstr_w(lpsz_replace), can_undo, send_update);
3068 s = es->selection_start;
3069 e = es->selection_end;
3071 if ((s == e) && !strl)
3076 size = tl - (e - s) + strl;
3080 /* Issue the EN_MAXTEXT notification and continue with replacing text
3081 * such that buffer limit is honored. */
3082 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3083 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3084 strl = es->buffer_limit - (tl - (e-s));
3087 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3091 /* there is something to be deleted */
3092 TRACE("deleting stuff.\n");
3094 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3096 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3097 buf[bufl] = 0; /* ensure 0 termination */
3099 strcpyW(es->text + s, es->text + e);
3102 /* there is an insertion */
3103 tl = strlenW(es->text);
3104 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));
3105 for (p = es->text + tl ; p >= es->text + s ; p--)
3107 for (i = 0 , p = es->text + s ; i < strl ; i++)
3108 p[i] = lpsz_replace[i];
3109 if(es->style & ES_UPPERCASE)
3110 CharUpperBuffW(p, strl);
3111 else if(es->style & ES_LOWERCASE)
3112 CharLowerBuffW(p, strl);
3114 if (es->style & ES_MULTILINE)
3116 INT st = min(es->selection_start, es->selection_end);
3117 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3119 hrgn = CreateRectRgn(0, 0, 0, 0);
3120 EDIT_BuildLineDefs_ML(es, st, st + strl,
3121 strl - abs(es->selection_end - es->selection_start), hrgn);
3122 /* if text is too long undo all changes */
3123 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3125 strcpyW(es->text + e, es->text + e + strl);
3127 for (i = 0 , p = es->text ; i < e - s ; i++)
3129 EDIT_BuildLineDefs_ML(es, s, e,
3130 abs(es->selection_end - es->selection_start) - strl, hrgn);
3133 hrgn = CreateRectRgn(0, 0, 0, 0);
3134 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3138 INT fw = es->format_rect.right - es->format_rect.left;
3139 EDIT_CalcLineWidth_SL(es);
3140 /* remove chars that don't fit */
3141 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3142 while ((es->text_width > fw) && s + strl >= s) {
3143 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3145 EDIT_CalcLineWidth_SL(es);
3147 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3153 utl = strlenW(es->undo_text);
3154 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3155 /* undo-buffer is extended to the right */
3156 EDIT_MakeUndoFit(es, utl + e - s);
3157 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3158 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3159 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3160 /* undo-buffer is extended to the left */
3161 EDIT_MakeUndoFit(es, utl + e - s);
3162 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3164 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3166 es->undo_position = s;
3168 /* new undo-buffer */
3169 EDIT_MakeUndoFit(es, e - s);
3170 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3171 es->undo_text[e - s] = 0; /* ensure 0 termination */
3172 es->undo_position = s;
3174 /* any deletion makes the old insertion-undo invalid */
3175 es->undo_insert_count = 0;
3177 EDIT_EM_EmptyUndoBuffer(es);
3181 if ((s == es->undo_position) ||
3182 ((es->undo_insert_count) &&
3183 (s == es->undo_position + es->undo_insert_count)))
3185 * insertion is new and at delete position or
3186 * an extension to either left or right
3188 es->undo_insert_count += strl;
3190 /* new insertion undo */
3191 es->undo_position = s;
3192 es->undo_insert_count = strl;
3193 /* new insertion makes old delete-buffer invalid */
3194 *es->undo_text = '\0';
3197 EDIT_EM_EmptyUndoBuffer(es);
3201 HeapFree(GetProcessHeap(), 0, buf);
3205 /* If text has been deleted and we're right or center aligned then scroll rightward */
3206 if (es->style & (ES_RIGHT | ES_CENTER))
3208 INT delta = strl - abs(es->selection_end - es->selection_start);
3210 if (delta < 0 && es->x_offset)
3212 if (abs(delta) > es->x_offset)
3215 es->x_offset += delta;
3219 EDIT_EM_SetSel(es, s, s, FALSE);
3220 es->flags |= EF_MODIFIED;
3221 if (send_update) es->flags |= EF_UPDATE;
3224 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3228 EDIT_UpdateText(es, NULL, TRUE);
3230 EDIT_EM_ScrollCaret(es);
3232 /* force scroll info update */
3233 EDIT_UpdateScrollInfo(es);
3236 if(send_update || (es->flags & EF_UPDATE))
3238 es->flags &= ~EF_UPDATE;
3239 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3244 /*********************************************************************
3249 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3253 if (!(es->style & ES_MULTILINE))
3254 return (LRESULT)FALSE;
3264 if (es->y_offset < es->line_count - 1)
3269 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3272 if (es->y_offset < es->line_count - 1)
3273 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3276 return (LRESULT)FALSE;
3279 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3280 /* check if we are going to move too far */
3281 if(es->y_offset + dy > es->line_count - vlc)
3282 dy = es->line_count - vlc - es->y_offset;
3284 /* Notification is done in EDIT_EM_LineScroll */
3286 EDIT_EM_LineScroll(es, 0, dy);
3288 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3292 /*********************************************************************
3297 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3299 if (es->style & ES_MULTILINE) {
3304 INT cw = es->char_width;
3309 l = EDIT_EM_LineFromChar(es, es->selection_end);
3310 li = EDIT_EM_LineIndex(es, l);
3311 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3312 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3313 if (l >= es->y_offset + vlc)
3314 dy = l - vlc + 1 - es->y_offset;
3315 if (l < es->y_offset)
3316 dy = l - es->y_offset;
3317 ww = es->format_rect.right - es->format_rect.left;
3318 if (x < es->format_rect.left)
3319 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3320 if (x > es->format_rect.right)
3321 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3322 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3324 /* check if we are going to move too far */
3325 if(es->x_offset + dx + ww > es->text_width)
3326 dx = es->text_width - ww - es->x_offset;
3327 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3328 EDIT_EM_LineScroll_internal(es, dx, dy);
3335 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3336 format_width = es->format_rect.right - es->format_rect.left;
3337 if (x < es->format_rect.left) {
3338 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3341 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3342 } while ((x < goal) && es->x_offset);
3343 /* FIXME: use ScrollWindow() somehow to improve performance */
3344 EDIT_UpdateText(es, NULL, TRUE);
3345 } else if (x > es->format_rect.right) {
3347 INT len = strlenW(es->text);
3348 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3351 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3352 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3353 } while ((x > goal) && (x_last > es->format_rect.right));
3354 /* FIXME: use ScrollWindow() somehow to improve performance */
3355 EDIT_UpdateText(es, NULL, TRUE);
3359 if(es->flags & EF_FOCUSED)
3360 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3364 /*********************************************************************
3368 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3371 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3373 if (!(es->style & ES_MULTILINE))
3377 WARN("called with NULL handle\n");
3381 EDIT_UnlockBuffer(es, TRUE);
3385 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3386 HANDLE16 oldDS = stack16->ds;
3388 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3389 LocalFree16(es->hloc16);
3390 stack16->ds = oldDS;
3398 LocalFree(es->hloc32A);
3410 countA = LocalSize(hloc);
3411 textA = LocalLock(hloc);
3412 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3413 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3415 ERR("Could not allocate new unicode buffer\n");
3418 textW = LocalLock(hloc32W_new);
3419 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3420 LocalUnlock(hloc32W_new);
3424 LocalFree(es->hloc32W);
3426 es->hloc32W = hloc32W_new;
3430 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3432 EDIT_LockBuffer(es);
3434 es->x_offset = es->y_offset = 0;
3435 es->selection_start = es->selection_end = 0;
3436 EDIT_EM_EmptyUndoBuffer(es);
3437 es->flags &= ~EF_MODIFIED;
3438 es->flags &= ~EF_UPDATE;
3439 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3440 EDIT_UpdateText(es, NULL, TRUE);
3441 EDIT_EM_ScrollCaret(es);
3442 /* force scroll info update */
3443 EDIT_UpdateScrollInfo(es);
3447 /*********************************************************************
3451 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3454 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3456 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3457 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3458 HANDLE16 oldDS = stack16->ds;
3464 if (!(es->style & ES_MULTILINE))
3468 WARN("called with NULL handle\n");
3472 EDIT_UnlockBuffer(es, TRUE);
3476 LocalFree(es->hloc32A);
3480 stack16->ds = hInstance;
3481 countA = LocalSize16(hloc);
3482 textA = MapSL(LocalLock16(hloc));
3483 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3484 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3486 ERR("Could not allocate new unicode buffer\n");
3489 textW = LocalLock(hloc32W_new);
3490 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3491 LocalUnlock(hloc32W_new);
3492 LocalUnlock16(hloc);
3493 stack16->ds = oldDS;
3496 LocalFree(es->hloc32W);
3498 es->hloc32W = hloc32W_new;
3501 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3503 EDIT_LockBuffer(es);
3505 es->x_offset = es->y_offset = 0;
3506 es->selection_start = es->selection_end = 0;
3507 EDIT_EM_EmptyUndoBuffer(es);
3508 es->flags &= ~EF_MODIFIED;
3509 es->flags &= ~EF_UPDATE;
3510 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3511 EDIT_UpdateText(es, NULL, TRUE);
3512 EDIT_EM_ScrollCaret(es);
3513 /* force scroll info update */
3514 EDIT_UpdateScrollInfo(es);
3518 /*********************************************************************
3522 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3523 * However, the windows version is not complied to yet in all of edit.c
3525 * Additionally as the wrapper for RichEdit controls we need larger buffers
3526 * at present -1 will represent nolimit
3528 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3530 if (limit == 0xFFFFFFFF)
3531 es->buffer_limit = -1;
3532 else if (es->style & ES_MULTILINE) {
3534 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3536 es->buffer_limit = BUFLIMIT_MULTI;
3539 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3541 es->buffer_limit = BUFLIMIT_SINGLE;
3546 /*********************************************************************
3550 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3551 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3552 * margin according to the textmetrics of the current font.
3554 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3555 * of the char's width as the margin, but this is not how Windows handles this.
3556 * For all other fonts Windows sets the margins to zero.
3559 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3560 INT left, INT right)
3563 INT default_left_margin = 0; /* in pixels */
3564 INT default_right_margin = 0; /* in pixels */
3566 /* Set the default margins depending on the font */
3567 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3568 HDC dc = GetDC(es->hwndSelf);
3569 HFONT old_font = SelectObject(dc, es->font);
3570 GetTextMetricsW(dc, &tm);
3571 /* The default margins are only non zero for TrueType or Vector fonts */
3572 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3573 /* This must be calculated more exactly! But how? */
3574 default_left_margin = tm.tmAveCharWidth / 3;
3575 default_right_margin = tm.tmAveCharWidth / 3;
3577 SelectObject(dc, old_font);
3578 ReleaseDC(es->hwndSelf, dc);
3581 if (action & EC_LEFTMARGIN) {
3582 if (left != EC_USEFONTINFO)
3583 es->left_margin = left;
3585 es->left_margin = default_left_margin;
3588 if (action & EC_RIGHTMARGIN) {
3589 if (right != EC_USEFONTINFO)
3590 es->right_margin = right;
3592 es->right_margin = default_right_margin;
3594 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3598 /*********************************************************************
3600 * EM_SETPASSWORDCHAR
3603 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3607 if (es->style & ES_MULTILINE)
3610 if (es->password_char == c)
3613 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3614 es->password_char = c;
3616 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3617 es->style |= ES_PASSWORD;
3619 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3620 es->style &= ~ES_PASSWORD;
3622 EDIT_UpdateText(es, NULL, TRUE);
3626 /*********************************************************************
3630 * note: unlike the specs say: the order of start and end
3631 * _is_ preserved in Windows. (i.e. start can be > end)
3632 * In other words: this handler is OK
3635 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3637 UINT old_start = es->selection_start;
3638 UINT old_end = es->selection_end;
3639 UINT len = strlenW(es->text);
3641 if (start == (UINT)-1) {
3642 start = es->selection_end;
3643 end = es->selection_end;
3645 start = min(start, len);
3646 end = min(end, len);
3648 es->selection_start = start;
3649 es->selection_end = end;
3651 es->flags |= EF_AFTER_WRAP;
3653 es->flags &= ~EF_AFTER_WRAP;
3654 /* Compute the necessary invalidation region. */
3655 /* Note that we don't need to invalidate regions which have
3656 * "never" been selected, or those which are "still" selected.
3657 * In fact, every time we hit a selection boundary, we can
3658 * *toggle* whether we need to invalidate. Thus we can optimize by
3659 * *sorting* the interval endpoints. Let's assume that we sort them
3661 * start <= end <= old_start <= old_end
3662 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3663 * in 5 comparisons; ie it's impossible to do better than the
3665 ORDER_UINT(end, old_end);
3666 ORDER_UINT(start, old_start);
3667 ORDER_UINT(old_start, old_end);
3668 ORDER_UINT(start, end);
3669 /* Note that at this point 'end' and 'old_start' are not in order, but
3670 * start is definitely the min. and old_end is definitely the max. */
3671 if (end != old_start)
3675 * ORDER_UINT32(end, old_start);
3676 * EDIT_InvalidateText(es, start, end);
3677 * EDIT_InvalidateText(es, old_start, old_end);
3678 * in place of the following if statement.
3679 * (That would complete the optimal five-comparison four-element sort.)
3681 if (old_start > end )
3683 EDIT_InvalidateText(es, start, end);
3684 EDIT_InvalidateText(es, old_start, old_end);
3688 EDIT_InvalidateText(es, start, old_start);
3689 EDIT_InvalidateText(es, end, old_end);
3692 else EDIT_InvalidateText(es, start, old_end);
3696 /*********************************************************************
3701 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3703 if (!(es->style & ES_MULTILINE))
3705 HeapFree(GetProcessHeap(), 0, es->tabs);
3706 es->tabs_count = count;
3710 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3711 memcpy(es->tabs, tabs, count * sizeof(INT));
3717 /*********************************************************************
3722 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3724 if (!(es->style & ES_MULTILINE))
3726 HeapFree(GetProcessHeap(), 0, es->tabs);
3727 es->tabs_count = count;
3732 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3733 for (i = 0 ; i < count ; i++)
3734 es->tabs[i] = *tabs++;
3740 /*********************************************************************
3742 * EM_SETWORDBREAKPROC
3745 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3747 if (es->word_break_proc == wbp)
3750 es->word_break_proc = wbp;
3751 es->word_break_proc16 = NULL;
3753 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3754 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3755 EDIT_UpdateText(es, NULL, TRUE);
3760 /*********************************************************************
3762 * EM_SETWORDBREAKPROC16
3765 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3767 if (es->word_break_proc16 == wbp)
3770 es->word_break_proc = NULL;
3771 es->word_break_proc16 = wbp;
3772 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3773 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3774 EDIT_UpdateText(es, NULL, TRUE);
3779 /*********************************************************************
3784 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3789 /* As per MSDN spec, for a single-line edit control,
3790 the return value is always TRUE */
3791 if( es->style & ES_READONLY )
3792 return !(es->style & ES_MULTILINE);
3794 ulength = strlenW(es->undo_text);
3796 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3798 strcpyW(utext, es->undo_text);
3800 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3801 es->undo_insert_count, debugstr_w(utext));
3803 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3804 EDIT_EM_EmptyUndoBuffer(es);
3805 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3806 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3807 /* send the notification after the selection start and end are set */
3808 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3809 EDIT_EM_ScrollCaret(es);
3810 HeapFree(GetProcessHeap(), 0, utext);
3812 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3813 es->undo_insert_count, debugstr_w(es->undo_text));
3818 /*********************************************************************
3823 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3827 control = GetKeyState(VK_CONTROL) & 0x8000;
3831 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3832 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3835 if (es->style & ES_MULTILINE) {
3836 if (es->style & ES_READONLY) {
3837 EDIT_MoveHome(es, FALSE);
3838 EDIT_MoveDown_ML(es, FALSE);
3840 static const WCHAR cr_lfW[] = {'\r','\n',0};
3841 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3846 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3848 static const WCHAR tabW[] = {'\t',0};
3849 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3853 if (!(es->style & ES_READONLY) && !control) {
3854 if (es->selection_start != es->selection_end)
3857 /* delete character left of caret */
3858 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3859 EDIT_MoveBackward(es, TRUE);
3865 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3868 if (!(es->style & ES_READONLY))
3869 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3872 if (!(es->style & ES_READONLY))
3873 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3877 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3878 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3881 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3885 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3892 /*********************************************************************
3897 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3899 if (code || control)
3919 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3920 EDIT_EM_ScrollCaret(es);
3923 ERR("unknown menu item, please report\n");
3929 /*********************************************************************
3933 * Note: the resource files resource/sysres_??.rc cannot define a
3934 * single popup menu. Hence we use a (dummy) menubar
3935 * containing the single popup menu as its first item.
3937 * FIXME: the message identifiers have been chosen arbitrarily,
3938 * hence we use MF_BYPOSITION.
3939 * We might as well use the "real" values (anybody knows ?)
3940 * The menu definition is in resources/sysres_??.rc.
3941 * Once these are OK, we better use MF_BYCOMMAND here
3942 * (as we do in EDIT_WM_Command()).
3945 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3947 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3948 HMENU popup = GetSubMenu(menu, 0);
3949 UINT start = es->selection_start;
3950 UINT end = es->selection_end;
3952 ORDER_UINT(start, end);
3955 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3957 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3959 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3961 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3963 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3965 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3967 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3972 /*********************************************************************
3977 static void EDIT_WM_Copy(EDITSTATE *es)
3979 INT s = min(es->selection_start, es->selection_end);
3980 INT e = max(es->selection_start, es->selection_end);
3988 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3989 dst = GlobalLock(hdst);
3990 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3991 dst[len] = 0; /* ensure 0 termination */
3992 TRACE("%s\n", debugstr_w(dst));
3994 OpenClipboard(es->hwndSelf);
3996 SetClipboardData(CF_UNICODETEXT, hdst);
4001 /*********************************************************************
4006 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4008 TRACE("%s\n", debugstr_w(name));
4010 * To initialize some final structure members, we call some helper
4011 * functions. However, since the EDITSTATE is not consistent (i.e.
4012 * not fully initialized), we should be very careful which
4013 * functions can be called, and in what order.
4015 EDIT_WM_SetFont(es, 0, FALSE);
4016 EDIT_EM_EmptyUndoBuffer(es);
4018 if (name && *name) {
4019 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
4020 /* if we insert text to the editline, the text scrolls out
4021 * of the window, as the caret is placed after the insert
4022 * pos normally; thus we reset es->selection... to 0 and
4025 es->selection_start = es->selection_end = 0;
4026 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4027 * Messages are only to be sent when the USER does something to
4028 * change the contents. So I am removing this EN_CHANGE
4030 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4032 EDIT_EM_ScrollCaret(es);
4034 /* force scroll info update */
4035 EDIT_UpdateScrollInfo(es);
4036 /* The rule seems to return 1 here for success */
4037 /* Power Builder masked edit controls will crash */
4039 /* FIXME: is that in all cases so ? */
4044 /*********************************************************************
4049 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4054 while (LocalUnlock(es->hloc32W)) ;
4055 LocalFree(es->hloc32W);
4058 while (LocalUnlock(es->hloc32A)) ;
4059 LocalFree(es->hloc32A);
4062 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
4063 HANDLE16 oldDS = stack16->ds;
4065 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4066 while (LocalUnlock16(es->hloc16)) ;
4067 LocalFree16(es->hloc16);
4068 stack16->ds = oldDS;
4071 pc = es->first_line_def;
4075 HeapFree(GetProcessHeap(), 0, pc);
4079 SetWindowLongW( es->hwndSelf, 0, 0 );
4080 HeapFree(GetProcessHeap(), 0, es);
4086 /*********************************************************************
4091 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4093 /* we do the proper erase in EDIT_WM_Paint */
4098 /*********************************************************************
4103 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4105 if(!count) return 0;
4109 lstrcpynW(dst, es->text, count);
4110 return strlenW(dst);
4114 LPSTR textA = (LPSTR)dst;
4115 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4116 textA[count - 1] = 0; /* ensure 0 termination */
4117 return strlen(textA);
4121 /*********************************************************************
4126 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4131 if (!(es->style & ES_MULTILINE))
4134 if (!(es->style & ES_AUTOHSCROLL))
4138 fw = es->format_rect.right - es->format_rect.left;
4141 TRACE("SB_LINELEFT\n");
4143 dx = -es->char_width;
4146 TRACE("SB_LINERIGHT\n");
4147 if (es->x_offset < es->text_width)
4148 dx = es->char_width;
4151 TRACE("SB_PAGELEFT\n");
4153 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4156 TRACE("SB_PAGERIGHT\n");
4157 if (es->x_offset < es->text_width)
4158 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4166 TRACE("SB_RIGHT\n");
4167 if (es->x_offset < es->text_width)
4168 dx = es->text_width - es->x_offset;
4171 TRACE("SB_THUMBTRACK %d\n", pos);
4172 es->flags |= EF_HSCROLL_TRACK;
4173 if(es->style & WS_HSCROLL)
4174 dx = pos - es->x_offset;
4179 if(pos < 0 || pos > 100) return 0;
4180 /* Assume default scroll range 0-100 */
4181 fw = es->format_rect.right - es->format_rect.left;
4182 new_x = pos * (es->text_width - fw) / 100;
4183 dx = es->text_width ? (new_x - es->x_offset) : 0;
4186 case SB_THUMBPOSITION:
4187 TRACE("SB_THUMBPOSITION %d\n", pos);
4188 es->flags &= ~EF_HSCROLL_TRACK;
4189 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4190 dx = pos - es->x_offset;
4195 if(pos < 0 || pos > 100) return 0;
4196 /* Assume default scroll range 0-100 */
4197 fw = es->format_rect.right - es->format_rect.left;
4198 new_x = pos * (es->text_width - fw) / 100;
4199 dx = es->text_width ? (new_x - es->x_offset) : 0;
4202 /* force scroll info update */
4203 EDIT_UpdateScrollInfo(es);
4204 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
4208 TRACE("SB_ENDSCROLL\n");
4211 * FIXME : the next two are undocumented !
4212 * Are we doing the right thing ?
4213 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4214 * although it's also a regular control message.
4216 case EM_GETTHUMB: /* this one is used by NT notepad */
4220 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4221 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4224 /* Assume default scroll range 0-100 */
4225 INT fw = es->format_rect.right - es->format_rect.left;
4226 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4228 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4231 case EM_LINESCROLL16:
4232 TRACE("EM_LINESCROLL16\n");
4237 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4243 INT fw = es->format_rect.right - es->format_rect.left;
4244 /* check if we are going to move too far */
4245 if(es->x_offset + dx + fw > es->text_width)
4246 dx = es->text_width - fw - es->x_offset;
4248 EDIT_EM_LineScroll_internal(es, dx, 0);
4254 /*********************************************************************
4259 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4261 HWND hLBox = es->hwndListBox;
4269 hCombo = GetParent(es->hwndSelf);
4273 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4275 if (key == VK_UP || key == VK_DOWN)
4277 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4280 if (msg == WM_KEYDOWN || nEUI)
4281 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4287 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4289 /* make sure ComboLBox pops up */
4290 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4295 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4298 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4300 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4302 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4307 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4313 /*********************************************************************
4317 * Handling of special keys that don't produce a WM_CHAR
4318 * (i.e. non-printable keys) & Backspace & Delete
4321 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4326 if (GetKeyState(VK_MENU) & 0x8000)
4329 shift = GetKeyState(VK_SHIFT) & 0x8000;
4330 control = GetKeyState(VK_CONTROL) & 0x8000;
4335 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4340 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4341 EDIT_MoveUp_ML(es, shift);
4344 EDIT_MoveWordBackward(es, shift);
4346 EDIT_MoveBackward(es, shift);
4349 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4353 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4354 EDIT_MoveDown_ML(es, shift);
4356 EDIT_MoveWordForward(es, shift);
4358 EDIT_MoveForward(es, shift);
4361 EDIT_MoveHome(es, shift);
4364 EDIT_MoveEnd(es, shift);
4367 if (es->style & ES_MULTILINE)
4368 EDIT_MovePageUp_ML(es, shift);
4370 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4373 if (es->style & ES_MULTILINE)
4374 EDIT_MovePageDown_ML(es, shift);
4376 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4379 if (!(es->style & ES_READONLY) && !(shift && control)) {
4380 if (es->selection_start != es->selection_end) {
4387 /* delete character left of caret */
4388 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4389 EDIT_MoveBackward(es, TRUE);
4391 } else if (control) {
4392 /* delete to end of line */
4393 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4394 EDIT_MoveEnd(es, TRUE);
4397 /* delete character right of caret */
4398 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4399 EDIT_MoveForward(es, TRUE);
4407 if (!(es->style & ES_READONLY))
4413 /* If the edit doesn't want the return send a message to the default object */
4414 if(!(es->style & ES_WANTRETURN))
4416 HWND hwndParent = GetParent(es->hwndSelf);
4417 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4418 if (HIWORD(dw) == DC_HASDEFID)
4420 SendMessageW( hwndParent, WM_COMMAND,
4421 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4422 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4431 /*********************************************************************
4436 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4438 es->flags &= ~EF_FOCUSED;
4440 if(!(es->style & ES_NOHIDESEL))
4441 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4442 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4447 /*********************************************************************
4451 * The caret position has been set on the WM_LBUTTONDOWN message
4454 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4457 INT e = es->selection_end;
4462 if (!(es->flags & EF_FOCUSED))
4465 l = EDIT_EM_LineFromChar(es, e);
4466 li = EDIT_EM_LineIndex(es, l);
4467 ll = EDIT_EM_LineLength(es, e);
4468 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4469 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4470 EDIT_EM_SetSel(es, s, e, FALSE);
4471 EDIT_EM_ScrollCaret(es);
4476 /*********************************************************************
4481 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4486 SetFocus(es->hwndSelf);
4487 if (!(es->flags & EF_FOCUSED))
4490 es->bCaptureState = TRUE;
4491 SetCapture(es->hwndSelf);
4492 EDIT_ConfinePoint(es, &x, &y);
4493 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4494 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4495 EDIT_EM_ScrollCaret(es);
4496 es->region_posx = es->region_posy = 0;
4497 SetTimer(es->hwndSelf, 0, 100, NULL);
4502 /*********************************************************************
4507 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4509 if (es->bCaptureState) {
4510 KillTimer(es->hwndSelf, 0);
4511 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4513 es->bCaptureState = FALSE;
4518 /*********************************************************************
4523 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4525 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4530 /*********************************************************************
4535 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4541 /* If the mouse has been captured by process other than the edit control itself,
4542 * the windows edit controls will not select the strings with mouse move.
4544 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4548 * FIXME: gotta do some scrolling if outside client
4549 * area. Maybe reset the timer ?
4552 EDIT_ConfinePoint(es, &x, &y);
4553 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4554 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4555 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4556 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4557 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4562 /*********************************************************************
4566 * See also EDIT_WM_StyleChanged
4568 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4573 TRACE("Creating %s edit control, style = %08lx\n",
4574 unicode ? "Unicode" : "ANSI", lpcs->style);
4576 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4578 SetWindowLongW( hwnd, 0, (LONG)es );
4581 * Note: since the EDITSTATE has not been fully initialized yet,
4582 * we can't use any API calls that may send
4583 * WM_XXX messages before WM_NCCREATE is completed.
4586 es->is_unicode = unicode;
4587 es->style = lpcs->style;
4589 es->bEnableState = !(es->style & WS_DISABLED);
4591 es->hwndSelf = hwnd;
4592 /* Save parent, which will be notified by EN_* messages */
4593 es->hwndParent = lpcs->hwndParent;
4595 if (es->style & ES_COMBO)
4596 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4598 /* Number overrides lowercase overrides uppercase (at least it
4599 * does in Win95). However I'll bet that ES_NUMBER would be
4600 * invalid under Win 3.1.
4602 if (es->style & ES_NUMBER) {
4603 ; /* do not override the ES_NUMBER */
4604 } else if (es->style & ES_LOWERCASE) {
4605 es->style &= ~ES_UPPERCASE;
4607 if (es->style & ES_MULTILINE) {
4608 es->buffer_limit = BUFLIMIT_MULTI;
4609 if (es->style & WS_VSCROLL)
4610 es->style |= ES_AUTOVSCROLL;
4611 if (es->style & WS_HSCROLL)
4612 es->style |= ES_AUTOHSCROLL;
4613 es->style &= ~ES_PASSWORD;
4614 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4615 /* Confirmed - RIGHT overrides CENTER */
4616 if (es->style & ES_RIGHT)
4617 es->style &= ~ES_CENTER;
4618 es->style &= ~WS_HSCROLL;
4619 es->style &= ~ES_AUTOHSCROLL;
4622 es->buffer_limit = BUFLIMIT_SINGLE;
4623 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4624 es->style &= ~ES_CENTER;
4625 es->style &= ~WS_HSCROLL;
4626 es->style &= ~WS_VSCROLL;
4627 if (es->style & ES_PASSWORD)
4628 es->password_char = '*';
4631 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4632 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4634 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4636 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4638 es->undo_buffer_size = es->buffer_size;
4640 if (es->style & ES_MULTILINE)
4641 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4646 * In Win95 look and feel, the WS_BORDER style is replaced by the
4647 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4648 * control a nonclient area so we don't need to draw the border.
4649 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4650 * a nonclient area and we should handle painting the border ourselves.
4652 * When making modifications please ensure that the code still works
4653 * for edit controls created directly with style 0x50800000, exStyle 0
4654 * (which should have a single pixel border)
4656 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4657 es->style &= ~WS_BORDER;
4658 else if (es->style & WS_BORDER)
4659 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4664 /*********************************************************************
4669 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4680 BOOL rev = es->bEnableState &&
4681 ((es->flags & EF_FOCUSED) ||
4682 (es->style & ES_NOHIDESEL));
4683 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4685 GetClientRect(es->hwndSelf, &rcClient);
4687 /* paint the background */
4688 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
4689 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
4690 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4691 GetClipBox(dc, &rc);
4692 FillRect(dc, &rc, brush);
4694 /* draw the border */
4695 if(es->style & WS_BORDER) {
4697 if(es->style & ES_MULTILINE) {
4698 if(es->style & WS_HSCROLL) rc.bottom++;
4699 if(es->style & WS_VSCROLL) rc.right++;
4701 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4703 IntersectClipRect(dc, es->format_rect.left,
4704 es->format_rect.top,
4705 es->format_rect.right,
4706 es->format_rect.bottom);
4707 if (es->style & ES_MULTILINE) {
4709 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4712 old_font = SelectObject(dc, es->font);
4713 EDIT_NotifyCtlColor(es, dc);
4715 if (!es->bEnableState)
4716 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4717 GetClipBox(dc, &rcRgn);
4718 if (es->style & ES_MULTILINE) {
4719 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4720 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4721 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4722 if (IntersectRect(&rc, &rcRgn, &rcLine))
4723 EDIT_PaintLine(es, dc, i, rev);
4726 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4727 if (IntersectRect(&rc, &rcRgn, &rcLine))
4728 EDIT_PaintLine(es, dc, 0, rev);
4731 SelectObject(dc, old_font);
4734 EndPaint(es->hwndSelf, &ps);
4738 /*********************************************************************
4743 static void EDIT_WM_Paste(EDITSTATE *es)
4748 /* Protect read-only edit control from modification */
4749 if(es->style & ES_READONLY)
4752 OpenClipboard(es->hwndSelf);
4753 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4754 src = (LPWSTR)GlobalLock(hsrc);
4755 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4762 /*********************************************************************
4767 static void EDIT_WM_SetFocus(EDITSTATE *es)
4769 es->flags |= EF_FOCUSED;
4770 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4771 EDIT_SetCaretPos(es, es->selection_end,
4772 es->flags & EF_AFTER_WRAP);
4773 if(!(es->style & ES_NOHIDESEL))
4774 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4775 ShowCaret(es->hwndSelf);
4776 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4780 /*********************************************************************
4784 * With Win95 look the margins are set to default font value unless
4785 * the system font (font == 0) is being set, in which case they are left
4789 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4797 dc = GetDC(es->hwndSelf);
4799 old_font = SelectObject(dc, font);
4800 GetTextMetricsW(dc, &tm);
4801 es->line_height = tm.tmHeight;
4802 es->char_width = tm.tmAveCharWidth;
4804 SelectObject(dc, old_font);
4805 ReleaseDC(es->hwndSelf, dc);
4806 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4807 EC_USEFONTINFO, EC_USEFONTINFO);
4809 /* Force the recalculation of the format rect for each font change */
4810 GetClientRect(es->hwndSelf, &r);
4811 EDIT_SetRectNP(es, &r);
4813 if (es->style & ES_MULTILINE)
4814 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4816 EDIT_CalcLineWidth_SL(es);
4819 EDIT_UpdateText(es, NULL, TRUE);
4820 if (es->flags & EF_FOCUSED) {
4822 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4823 EDIT_SetCaretPos(es, es->selection_end,
4824 es->flags & EF_AFTER_WRAP);
4825 ShowCaret(es->hwndSelf);
4830 /*********************************************************************
4835 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4836 * The modified flag is reset. No notifications are sent.
4838 * For single-line controls, reception of WM_SETTEXT triggers:
4839 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4842 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4844 if (!unicode && text)
4846 LPCSTR textA = (LPCSTR)text;
4847 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4848 LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
4850 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4854 if (es->flags & EF_UPDATE)
4855 /* fixed this bug once; complain if we see it about to happen again. */
4856 ERR("SetSel may generate UPDATE message whose handler may reset "
4859 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4862 TRACE("%s\n", debugstr_w(text));
4863 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4865 HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
4869 static const WCHAR empty_stringW[] = {0};
4871 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4874 es->flags &= ~EF_MODIFIED;
4875 EDIT_EM_SetSel(es, 0, 0, FALSE);
4877 /* Send the notification after the selection start and end have been set
4878 * edit control doesn't send notification on WM_SETTEXT
4879 * if it is multiline, or it is part of combobox
4881 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4883 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4884 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4886 EDIT_EM_ScrollCaret(es);
4887 EDIT_UpdateScrollInfo(es);
4891 /*********************************************************************
4896 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4898 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4900 TRACE("width = %d, height = %d\n", width, height);
4901 SetRect(&rc, 0, 0, width, height);
4902 EDIT_SetRectNP(es, &rc);
4903 EDIT_UpdateText(es, NULL, TRUE);
4908 /*********************************************************************
4912 * This message is sent by SetWindowLong on having changed either the Style
4913 * or the extended style.
4915 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4917 * See also EDIT_WM_NCCreate
4919 * It appears that the Windows version of the edit control allows the style
4920 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4921 * style variable which will generally be different. In this function we
4922 * update the internal style based on what changed in the externally visible
4925 * Much of this content as based upon the MSDN, especially:
4926 * Platform SDK Documentation -> User Interface Services ->
4927 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4928 * Edit Control Styles
4930 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4932 if (GWL_STYLE == which) {
4933 DWORD style_change_mask;
4935 /* Only a subset of changes can be applied after the control
4938 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4940 if (es->style & ES_MULTILINE)
4941 style_change_mask |= ES_WANTRETURN;
4943 new_style = style->styleNew & style_change_mask;
4945 /* Number overrides lowercase overrides uppercase (at least it
4946 * does in Win95). However I'll bet that ES_NUMBER would be
4947 * invalid under Win 3.1.
4949 if (new_style & ES_NUMBER) {
4950 ; /* do not override the ES_NUMBER */
4951 } else if (new_style & ES_LOWERCASE) {
4952 new_style &= ~ES_UPPERCASE;
4955 es->style = (es->style & ~style_change_mask) | new_style;
4956 } else if (GWL_EXSTYLE == which) {
4957 ; /* FIXME - what is needed here */
4959 WARN ("Invalid style change %d\n",which);
4965 /*********************************************************************
4970 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4972 if ((key == VK_BACK) && (key_data & 0x2000)) {
4973 if (EDIT_EM_CanUndo(es))
4976 } else if (key == VK_UP || key == VK_DOWN) {
4977 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4980 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4984 /*********************************************************************
4989 static void EDIT_WM_Timer(EDITSTATE *es)
4991 if (es->region_posx < 0) {
4992 EDIT_MoveBackward(es, TRUE);
4993 } else if (es->region_posx > 0) {
4994 EDIT_MoveForward(es, TRUE);
4997 * FIXME: gotta do some vertical scrolling here, like
4998 * EDIT_EM_LineScroll(hwnd, 0, 1);
5002 /*********************************************************************
5007 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5011 if (!(es->style & ES_MULTILINE))
5014 if (!(es->style & ES_AUTOVSCROLL))
5023 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5024 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5025 (action == SB_PAGEUP ? "SB_PAGEUP" :
5027 EDIT_EM_Scroll(es, action);
5034 TRACE("SB_BOTTOM\n");
5035 dy = es->line_count - 1 - es->y_offset;
5038 TRACE("SB_THUMBTRACK %d\n", pos);
5039 es->flags |= EF_VSCROLL_TRACK;
5040 if(es->style & WS_VSCROLL)
5041 dy = pos - es->y_offset;
5044 /* Assume default scroll range 0-100 */
5047 if(pos < 0 || pos > 100) return 0;
5048 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5049 new_y = pos * (es->line_count - vlc) / 100;
5050 dy = es->line_count ? (new_y - es->y_offset) : 0;
5051 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5052 es->line_count, es->y_offset, pos, dy);
5055 case SB_THUMBPOSITION:
5056 TRACE("SB_THUMBPOSITION %d\n", pos);
5057 es->flags &= ~EF_VSCROLL_TRACK;
5058 if(es->style & WS_VSCROLL)
5059 dy = pos - es->y_offset;
5062 /* Assume default scroll range 0-100 */
5065 if(pos < 0 || pos > 100) return 0;
5066 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5067 new_y = pos * (es->line_count - vlc) / 100;
5068 dy = es->line_count ? (new_y - es->y_offset) : 0;
5069 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5070 es->line_count, es->y_offset, pos, dy);
5074 /* force scroll info update */
5075 EDIT_UpdateScrollInfo(es);
5076 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
5080 TRACE("SB_ENDSCROLL\n");
5083 * FIXME : the next two are undocumented !
5084 * Are we doing the right thing ?
5085 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5086 * although it's also a regular control message.
5088 case EM_GETTHUMB: /* this one is used by NT notepad */
5092 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5093 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5096 /* Assume default scroll range 0-100 */
5097 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5098 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5100 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5103 case EM_LINESCROLL16:
5104 TRACE("EM_LINESCROLL16 %d\n", pos);
5109 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5114 EDIT_EM_LineScroll(es, 0, dy);
5118 /*********************************************************************
5123 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5125 if (es->flags & EF_UPDATE) {
5126 es->flags &= ~EF_UPDATE;
5127 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5129 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5133 /*********************************************************************
5138 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5140 if (es->flags & EF_UPDATE) {
5141 es->flags &= ~EF_UPDATE;
5142 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5144 InvalidateRect(es->hwndSelf, rc, bErase);